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

Refactor TestDeleteTribe To Use A Real Postgres DB For The Test #1769

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
62 changes: 43 additions & 19 deletions handlers/tribes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/google/uuid"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -191,23 +192,43 @@ func TestGetTribesByAppUrl(t *testing.T) {
}

func TestDeleteTribe(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, "owner_pubkey")
mockDb := mocks.NewDatabase(t)
tHandler := NewTribeHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

personUUID := uuid.New().String()
person := db.Person{
Uuid: personUUID,
OwnerAlias: "person_alias",
UniqueName: "person_unique_name",
OwnerPubKey: "owner_pubkey",
PriceToMeet: 0,
Description: "this is test user 1",
}
db.TestDB.CreateOrEditPerson(person)

tribeUUID := uuid.New().String()
tribe := db.Tribe{
UUID: tribeUUID,
OwnerPubKey: person.OwnerPubKey,
Name: "tribe_name",
Description: "description",
Tags: []string{"tag3", "tag4"},
AppURL: "tribe_app_url",
}
db.TestDB.CreateOrEditTribe(tribe)

tHandler := NewTribeHandler(db.TestDB)

t.Run("Should test that the owner of a tribe can delete a tribe", func(t *testing.T) {
// Mock data
mockUUID := "valid_uuid"
mockOwnerPubKey := "owner_pubkey"
mockUUID := tribe.AppURL
mockOwnerPubKey := person.OwnerPubKey

mockVerifyTribeUUID := func(uuid string, checkTimestamp bool) (string, error) {
return mockOwnerPubKey, nil
}
mockDb.On("UpdateTribe", mock.Anything, map[string]interface{}{"deleted": true}).Return(true)

tHandler.verifyTribeUUID = mockVerifyTribeUUID

// Create and serve request
ctx := context.WithValue(context.Background(), auth.ContextKey, mockOwnerPubKey)
rr := httptest.NewRecorder()
handler := http.HandlerFunc(tHandler.DeleteTribe)

Expand All @@ -216,32 +237,35 @@ func TestDeleteTribe(t *testing.T) {
t.Fatal(err)
}
chiCtx := chi.NewRouteContext()
chiCtx.URLParams.Add("uuid", "mockUUID")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aliraza556 you did not assert that the tribe is deleted from the DB using the db.TestDB.GetTribe() function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elraphty Fixed

chiCtx.URLParams.Add("uuid", tribeUUID)
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chiCtx))

handler.ServeHTTP(rr, req)

// Verify response
assert.Equal(t, http.StatusOK, rr.Code)
var responseData bool
errors := json.Unmarshal(rr.Body.Bytes(), &responseData)
assert.NoError(t, errors)
err = json.Unmarshal(rr.Body.Bytes(), &responseData)
assert.NoError(t, err)
assert.True(t, responseData)

// Assert that the tribe is deleted from the DB
deletedTribe := db.TestDB.GetTribe(tribeUUID)
assert.NoError(t, err)
assert.Empty(t, deletedTribe)
assert.Equal(t, db.Tribe{}, deletedTribe)
})

t.Run("Should test that a 401 error is returned when a tribe is attempted to be deleted by someone other than the owner", func(t *testing.T) {
// Mock data
ctx := context.WithValue(context.Background(), auth.ContextKey, "pubkey")
mockUUID := "valid_uuid"
mockOwnerPubKey := "owner_pubkey"
ctx := context.WithValue(context.Background(), auth.ContextKey, "other_pubkey")
mockUUID := tribe.AppURL
mockOwnerPubKey := person.OwnerPubKey

mockVerifyTribeUUID := func(uuid string, checkTimestamp bool) (string, error) {
return mockOwnerPubKey, nil
}

tHandler.verifyTribeUUID = mockVerifyTribeUUID

// Create and serve request
rr := httptest.NewRecorder()
handler := http.HandlerFunc(tHandler.DeleteTribe)

Expand All @@ -250,7 +274,7 @@ func TestDeleteTribe(t *testing.T) {
t.Fatal(err)
}
chiCtx := chi.NewRouteContext()
chiCtx.URLParams.Add("uuid", "mockUUID")
chiCtx.URLParams.Add("uuid", tribeUUID)
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chiCtx))

handler.ServeHTTP(rr, req)
Expand Down
Loading