Skip to content

Commit

Permalink
Refactor TestCreateOrEditTribe UT
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadUmer44 committed Jun 26, 2024
1 parent c7019ee commit a1062c1
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 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 @@ -370,55 +371,51 @@ func TestSetTribePreview(t *testing.T) {
}

func TestCreateOrEditTribe(t *testing.T) {
mockDb := mocks.NewDatabase(t)
tHandler := NewTribeHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

tHandler := NewTribeHandler(db.TestDB)

t.Run("Should test that a tribe can be created when the right data is passed", func(t *testing.T) {
// Mock data
mockPubKey := "valid_pubkey"
mockUUID := "valid_uuid"
mockName := "Test Tribe"
mockDescription := "This is a test tribe."
mockTags := []string{"tag1", "tag2"}

tribe := db.Tribe{
UUID: uuid.New().String(),
OwnerPubKey: "pubkey",
Name: "name",
Description: "description",
Tags: []string{"tag3", "tag4"},
AppURL: "valid_app_url",
Badges: []string{},
}

requestBody := map[string]interface{}{
"UUID": tribe.UUID,
"OwnerPubkey": tribe.OwnerPubKey,
"Name": tribe.Name,
"Description": tribe.Description,
"Tags": tribe.Tags,
"AppURL": tribe.AppURL,
"Badges": tribe.Badges,
}
mockVerifyTribeUUID := func(uuid string, checkTimestamp bool) (string, error) {
return mockPubKey, nil
return tribe.OwnerPubKey, nil
}

tHandler.verifyTribeUUID = mockVerifyTribeUUID

// Mock request body
requestBody := map[string]interface{}{
"UUID": mockUUID,
"Name": mockName,
"Description": mockDescription,
"Tags": mockTags,
}
requestBodyBytes, err := json.Marshal(requestBody)
if err != nil {
t.Fatal(err)
}

// Mock database calls
mockDb.On("GetTribe", mock.Anything).Return(db.Tribe{
UUID: mockUUID,
OwnerPubKey: mockPubKey,
}).Once()
mockDb.On("CreateOrEditTribe", mock.Anything).Return(db.Tribe{
UUID: mockUUID,
}, nil)

// Create request with mock body
req, err := http.NewRequest("POST", "/", bytes.NewBuffer(requestBodyBytes))
if err != nil {
t.Fatal(err)
}

// Set context with mock pub key
ctx := context.WithValue(req.Context(), auth.ContextKey, mockPubKey)
ctx := context.WithValue(req.Context(), auth.ContextKey, tribe.OwnerPubKey)
req = req.WithContext(ctx)

// Serve request
rr := httptest.NewRecorder()
handler := http.HandlerFunc(tHandler.CreateOrEditTribe)
handler.ServeHTTP(rr, req)
Expand All @@ -430,7 +427,13 @@ func TestCreateOrEditTribe(t *testing.T) {
if err != nil {
t.Fatalf("Error decoding JSON response: %s", err)
}
assert.Equal(t, mockUUID, responseData["uuid"])

// Assert that the response data is equal to the tribe POST data sent to the request
assert.Equal(t, tribe.UUID, responseData["uuid"])
assert.Equal(t, tribe.Name, responseData["name"])
assert.Equal(t, tribe.Description, responseData["description"])
assert.ElementsMatch(t, tribe.Tags, responseData["tags"])
assert.Equal(t, tribe.OwnerPubKey, responseData["owner_pubkey"])
})
}

Expand Down

0 comments on commit a1062c1

Please sign in to comment.