Skip to content

Commit

Permalink
Merge pull request stakwork#1790 from MirzaHanan/TestGetFirstTribeByF…
Browse files Browse the repository at this point in the history
…eed-UT

Refactor `TestGetFirstTribeByFeed` To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 27, 2024
2 parents 1ce44ba + 2da3ec8 commit 9ad6112
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions handlers/tribes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,45 +365,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)
Expand Down Expand Up @@ -564,6 +525,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)
Expand Down

0 comments on commit 9ad6112

Please sign in to comment.