From 2da3ec890d0032cecd5db97978301638f142f77a Mon Sep 17 00:00:00 2001 From: Mirza Hanan Date: Thu, 27 Jun 2024 23:17:39 +0500 Subject: [PATCH] Refactor TestGetFirstTribeByFeed UT --- handlers/tribes_test.go | 90 +++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index c24a01820..29121c6a5 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -367,45 +367,6 @@ func TestDeleteTribe(t *testing.T) { }) } -func TestGetFirstTribeByFeed(t *testing.T) { - mockDb := mocks.NewDatabase(t) - tHandler := NewTribeHandler(mockDb) - - t.Run("Should test that a tribe can be gotten by passing the feed URL", func(t *testing.T) { - // Mock data - mockFeedURL := "valid_feed_url" - mockTribe := db.Tribe{ - UUID: "valid_uuid", - } - mockChannels := []db.Channel{ - {ID: 1, TribeUUID: mockTribe.UUID}, - } - - mockDb.On("GetFirstTribeByFeedURL", mockFeedURL).Return(mockTribe).Once() - mockDb.On("GetChannelsByTribe", mockTribe.UUID).Return(mockChannels).Once() - - // Create request with valid feed URL - req, err := http.NewRequest("GET", "/tribe_by_feed?url="+mockFeedURL, nil) - if err != nil { - t.Fatal(err) - } - - // Serve request - rr := httptest.NewRecorder() - handler := http.HandlerFunc(tHandler.GetFirstTribeByFeed) - handler.ServeHTTP(rr, req) - - // Verify response - assert.Equal(t, http.StatusOK, rr.Code) - var responseData map[string]interface{} - err = json.Unmarshal(rr.Body.Bytes(), &responseData) - if err != nil { - t.Fatalf("Error decoding JSON response: %s", err) - } - assert.Equal(t, mockTribe.UUID, responseData["uuid"]) - }) -} - func TestSetTribePreview(t *testing.T) { teardownSuite := SetupSuite(t) defer teardownSuite(t) @@ -566,6 +527,57 @@ func TestCreateOrEditTribe(t *testing.T) { }) } +func TestGetFirstTribeByFeed(t *testing.T) { + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + tHandler := NewTribeHandler(db.TestDB) + + t.Run("Should test that a tribe can be gotten by passing the feed URL", func(t *testing.T) { + + db.TestDB.DeleteTribe() + + tribe := db.Tribe{ + UUID: uuid.New().String(), + OwnerPubKey: "pubkey", + Name: "name", + Description: "description", + Tags: []string{"tag2", "tag3"}, + AppURL: "app_url", + FeedURL: "valid_feed_url", + Badges: pq.StringArray{}, + } + db.TestDB.CreateOrEditTribe(tribe) + + mockFeedURL := tribe.FeedURL + + // Create request with valid feed URL + req, err := http.NewRequest("GET", "/tribe_by_feed?url="+mockFeedURL, nil) + if err != nil { + t.Fatal(err) + } + + // Serve request + rr := httptest.NewRecorder() + handler := http.HandlerFunc(tHandler.GetFirstTribeByFeed) + handler.ServeHTTP(rr, req) + + // Verify response + assert.Equal(t, http.StatusOK, rr.Code) + var responseData map[string]interface{} + err = json.Unmarshal(rr.Body.Bytes(), &responseData) + if err != nil { + t.Fatalf("Error decoding JSON response: %s", err) + } + 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.AppURL, responseData["app_url"]) + assert.Equal(t, tribe.FeedURL, responseData["feed_url"]) + }) +} + func TestGetTribeByUniqueName(t *testing.T) { teardownSuite := SetupSuite(t) defer teardownSuite(t)