Skip to content

Commit

Permalink
Refactored Delete Bounty Test FLow TO Use Real DB
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Jun 17, 2024
1 parent edf471b commit 711bdb4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
17 changes: 17 additions & 0 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
64 changes: 47 additions & 17 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"strconv"
Expand All @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -447,44 +478,43 @@ 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)
}
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))
})
}

Expand Down

0 comments on commit 711bdb4

Please sign in to comment.