From a1062c14662bd4036fb847a419562514c9486e29 Mon Sep 17 00:00:00 2001 From: MuhammadUmer44 Date: Wed, 26 Jun 2024 20:55:25 +0500 Subject: [PATCH] Refactor TestCreateOrEditTribe UT --- handlers/tribes_test.go | 63 +++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index c6d984623..c3eda2c52 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/google/uuid" "net/http" "net/http/httptest" "strings" @@ -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) @@ -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"]) }) }