Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend [Integration Test] bounty.go handlers GetBountyByCreated, GetNextBountyByCreated, GetPreviousBountyByCreated, GetOrganizationNextBountyByCreated, & GetOrganizationPreviousBountyByCreated #1525

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import (
)

type bountyHandler struct {
httpClient HttpClient
db db.Database
httpClient HttpClient
db db.Database
generateBountyResponse func(bounties []db.Bounty) []db.BountyResponse
}

func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler {
return &bountyHandler{
httpClient: httpClient,
db: db,
httpClient: httpClient,
db: db,
generateBountyResponse: GenerateBountyResponse,
}
}

Expand Down Expand Up @@ -54,8 +56,8 @@ func GetBountyById(w http.ResponseWriter, r *http.Request) {
}
}

func GetNextBountyByCreated(w http.ResponseWriter, r *http.Request) {
bounties, err := db.DB.GetNextBountyByCreated(r)
func (h *bountyHandler) GetNextBountyByCreated(w http.ResponseWriter, r *http.Request) {
bounties, err := h.db.GetNextBountyByCreated(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
Expand All @@ -65,8 +67,8 @@ func GetNextBountyByCreated(w http.ResponseWriter, r *http.Request) {
}
}

func GetPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) {
bounties, err := db.DB.GetPreviousBountyByCreated(r)
func (h *bountyHandler) GetPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) {
bounties, err := h.db.GetPreviousBountyByCreated(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
Expand All @@ -76,8 +78,8 @@ func GetPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) {
}
}

func GetOrganizationNextBountyByCreated(w http.ResponseWriter, r *http.Request) {
bounties, err := db.DB.GetNextOrganizationBountyByCreated(r)
func (h *bountyHandler) GetOrganizationNextBountyByCreated(w http.ResponseWriter, r *http.Request) {
bounties, err := h.db.GetNextOrganizationBountyByCreated(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
Expand All @@ -87,8 +89,8 @@ func GetOrganizationNextBountyByCreated(w http.ResponseWriter, r *http.Request)
}
}

func GetOrganizationPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) {
bounties, err := db.DB.GetPreviousOrganizationBountyByCreated(r)
func (h *bountyHandler) GetOrganizationPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) {
bounties, err := h.db.GetPreviousOrganizationBountyByCreated(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
Expand All @@ -108,17 +110,17 @@ func GetBountyIndexById(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(bountyIndex)
}

func GetBountyByCreated(w http.ResponseWriter, r *http.Request) {
func (h *bountyHandler) GetBountyByCreated(w http.ResponseWriter, r *http.Request) {
created := chi.URLParam(r, "created")
if created == "" {
w.WriteHeader(http.StatusNotFound)
}
bounties, err := db.DB.GetBountyDataByCreated(created)
bounties, err := h.db.GetBountyDataByCreated(created)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
} else {
var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties)
var bountyResponse []db.BountyResponse = h.generateBountyResponse(bounties)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
}
Expand Down
114 changes: 114 additions & 0 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,117 @@ func TestDeleteBounty(t *testing.T) {
mockDb.AssertExpectations(t)
})
}

func TestGetBountyByCreated(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
mockDb := dbMocks.NewDatabase(t)
mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
return []db.BountyResponse{} // Mocked response
}
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("Should return bounty by its created value", func(t *testing.T) {
bHandler.generateBountyResponse = mockGenerateBountyResponse

expectedBounty := []db.Bounty{{
ID: 1,
Type: "type1",
Title: "Test Bounty",
Description: "Description",
Created: 123456789,
}}
mockDb.On("GetBountyDataByCreated", "123456789").Return(expectedBounty, nil).Once()

rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetBountyByCreated)

req, err := http.NewRequestWithContext(ctx, "GET", "/bounty/123456789", nil)
if err != nil {
t.Fatal(err)
}
chiCtx := chi.NewRouteContext()
chiCtx.URLParams.Add("created", "123456789")
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chiCtx))

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
})
}

func TestGetNextBountyByCreated(t *testing.T) {
ctx := context.Background()
mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("Should test that the next bounty on the bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) {
mockDb.On("GetNextBountyByCreated", mock.Anything).Return(uint(1), nil).Once()

rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/next/123456789", nil)

bHandler.GetNextBountyByCreated(rr, req.WithContext(ctx))

assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)
})
}

func TestGetPreviousBountyByCreated(t *testing.T) {
ctx := context.Background()
mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("Should test that the previous bounty on the bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) {
mockDb.On("GetPreviousBountyByCreated", mock.Anything).Return(uint(1), nil).Once()

rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/previous/123456789", nil)

bHandler.GetPreviousBountyByCreated(rr, req.WithContext(ctx))

assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)
})
}

func TestGetOrganizationNextBountyByCreated(t *testing.T) {
ctx := context.Background()
mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("Should test that the next bounty on the organization bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) {
mockDb.On("GetNextOrganizationBountyByCreated", mock.AnythingOfType("*http.Request")).Return(uint(1), nil).Once()

rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/org/next/org-uuid/123456789", nil)

bHandler.GetOrganizationNextBountyByCreated(rr, req.WithContext(ctx))

assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)
})
}

func TestGetOrganizationPreviousBountyByCreated(t *testing.T) {
ctx := context.Background()
mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("Should test that the previous bounty on the organization bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) {
mockDb.On("GetPreviousOrganizationBountyByCreated", mock.AnythingOfType("*http.Request")).Return(uint(1), nil).Once()

rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/org/previous/org-uuid/123456789", nil)

bHandler.GetOrganizationPreviousBountyByCreated(rr, req.WithContext(ctx))

assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)
})
}
10 changes: 5 additions & 5 deletions routes/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func BountyRoutes() chi.Router {
r.Get("/all", bountyHandler.GetAllBounties)
r.Get("/id/{bountyId}", handlers.GetBountyById)
r.Get("/index/{bountyId}", handlers.GetBountyIndexById)
r.Get("/next/{created}", handlers.GetNextBountyByCreated)
r.Get("/previous/{created}", handlers.GetPreviousBountyByCreated)
r.Get("/org/next/{uuid}/{created}", handlers.GetOrganizationNextBountyByCreated)
r.Get("/org/previous/{uuid}/{created}", handlers.GetOrganizationPreviousBountyByCreated)
r.Get("/created/{created}", handlers.GetBountyByCreated)
r.Get("/next/{created}", bountyHandler.GetNextBountyByCreated)
r.Get("/previous/{created}", bountyHandler.GetPreviousBountyByCreated)
r.Get("/org/next/{uuid}/{created}", bountyHandler.GetOrganizationNextBountyByCreated)
r.Get("/org/previous/{uuid}/{created}", bountyHandler.GetOrganizationPreviousBountyByCreated)
r.Get("/created/{created}", bountyHandler.GetBountyByCreated)
r.Get("/count/{personKey}/{tabType}", handlers.GetUserBountyCount)
r.Get("/count", handlers.GetBountyCount)
r.Get("/invoice/{paymentRequest}", handlers.GetInvoiceData)
Expand Down
Loading