diff --git a/handlers/bounty.go b/handlers/bounty.go index a141d9f65..f432ef76a 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -317,6 +317,23 @@ func (h *bountyHandler) DeleteBounty(w http.ResponseWriter, r *http.Request) { return } + // get bounty by created + createdUint, _ := utils.ConvertStringToUint(created) + createdBounty, err := h.db.GetBountyByCreated(createdUint) + if err != nil { + fmt.Println("[bounty] failed to delete bounty", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode("failed to delete bounty") + return + } + + if createdBounty.ID == 0 { + fmt.Println("[bounty] failed to delete bounty") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode("failed to delete bounty") + return + } + b, err := h.db.DeleteBounty(pubkey, created) if err != nil { fmt.Println("[bounty] failed to delete bounty", err.Error()) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index baefbb08a..47a40eb1a 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "log" "net/http" "net/http/httptest" "strconv" @@ -29,6 +30,22 @@ import ( "github.com/stretchr/testify/mock" ) +func setupSuite(_ *testing.T) func(tb testing.TB) { + db.InitTestDB() + + return func(_ testing.TB) { + log.Println("Teardown test") + } +} + +func addExisitingDB(existingBounty db.NewBounty) { + bounty := db.TestDB.GetBounty(1) + if bounty.ID == 0 { + // add existing bounty to db + db.TestDB.CreateOrEditBounty(existingBounty) + } +} + func TestCreateOrEditBounty(t *testing.T) { ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") @@ -393,9 +410,23 @@ func TestPayLightningInvoice(t *testing.T) { } func TestDeleteBounty(t *testing.T) { - mockDb := dbMocks.NewDatabase(t) + teardownSuite := setupSuite(t) + defer teardownSuite(t) + + existingBounty := db.NewBounty{ + Type: "coding", + Title: "existing bounty", + Description: "existing bounty description", + WorkspaceUuid: "work-1", + OwnerID: "first-user", + Price: 2000, + } + + // Add initial Bounty + addExisitingDB(existingBounty) + mockHttpClient := mocks.NewHttpClient(t) - bHandler := NewBountyHandler(mockHttpClient, mockDb) + bHandler := NewBountyHandler(mockHttpClient, db.TestDB) ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") t.Run("should return unauthorized error if users public key not present", func(t *testing.T) { @@ -447,11 +478,11 @@ func TestDeleteBounty(t *testing.T) { t.Run("should return error if failed to delete from db", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(bHandler.DeleteBounty) - mockDb.On("DeleteBounty", "pub-key", "1111").Return(db.NewBounty{}, errors.New("some-error")).Once() rctx := chi.NewRouteContext() rctx.URLParams.Add("pubkey", "pub-key") rctx.URLParams.Add("created", "1111") + req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodDelete, "/pub-key/createdAt", nil) if err != nil { t.Fatal(err) @@ -459,32 +490,31 @@ func TestDeleteBounty(t *testing.T) { handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusInternalServerError, rr.Code) - mockDb.AssertExpectations(t) }) t.Run("should successfully delete bounty from db", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(bHandler.DeleteBounty) - existingBounty := db.NewBounty{ - OwnerID: "pub-key", - Created: 1111, - } - mockDb.On("DeleteBounty", "pub-key", "1111").Return(existingBounty, nil).Once() + existingBounty := db.TestDB.GetBounty(1) rctx := chi.NewRouteContext() - rctx.URLParams.Add("pubkey", "pub-key") - rctx.URLParams.Add("created", "1111") - req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodDelete, "/pub-key/1111", nil) + rctx.URLParams.Add("pubkey", existingBounty.OwnerID) + + created := fmt.Sprintf("%d", existingBounty.Created) + rctx.URLParams.Add("created", created) + + route := fmt.Sprintf("/%s/%d", existingBounty.OwnerID, existingBounty.Created) + req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodDelete, route, nil) + if err != nil { t.Fatal(err) } handler.ServeHTTP(rr, req) - var returnedBounty db.NewBounty - _ = json.Unmarshal(rr.Body.Bytes(), &returnedBounty) - assert.Equal(t, http.StatusOK, rr.Code) - assert.EqualValues(t, existingBounty, returnedBounty) - mockDb.AssertExpectations(t) + // get Bounty from DB + checkBounty := db.TestDB.GetBounty(1) + // chcek that the bounty's ID is now zero + assert.Equal(t, 0, int(checkBounty.ID)) }) }