From 6644340f9e3e5e37218bda44796c78ec9b320552 Mon Sep 17 00:00:00 2001 From: MuhammadUmer44 Date: Tue, 25 Jun 2024 23:47:06 +0500 Subject: [PATCH] Refactor TestCreateChannel UT --- handlers/channel_test.go | 98 ++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 28 deletions(-) diff --git a/handlers/channel_test.go b/handlers/channel_test.go index 066d67101..cee372ee8 100644 --- a/handlers/channel_test.go +++ b/handlers/channel_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/lib/pq" "net/http" "net/http/httptest" "testing" @@ -17,66 +18,107 @@ import ( ) func TestCreateChannel(t *testing.T) { - mockDb := mocks.NewDatabase(t) - cHandler := NewChannelHandler(mockDb) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + cHandler := NewChannelHandler(db.TestDB) + + createTestPersonAndTribe := func(pubKey, tribeUUID, tribeName string) (db.Person, db.Tribe) { + person := db.Person{ + Uuid: "person_chan_uuid", + OwnerAlias: "person_chan", + UniqueName: "person_chan", + OwnerPubKey: pubKey, + PriceToMeet: 0, + Description: "This is test user chan", + Unlisted: false, + Tags: pq.StringArray{}, + GithubIssues: db.PropertyMap{}, + Extras: db.PropertyMap{"coding_languages": "Lightning"}, + } + db.TestDB.CreateOrEditPerson(person) + + tribe := db.Tribe{ + UUID: tribeUUID, + OwnerPubKey: person.OwnerPubKey, + OwnerAlias: person.OwnerAlias, + Name: tribeName, + Unlisted: false, + UniqueName: tribeName, + } + db.TestDB.CreateOrEditTribe(tribe) - // Mock data for testing - mockPubKey := "mock_pubkey" - mockTribeUUID := "mock_tribe_uuid" - mockChannelName := "mock_channel" - mockRequestBody := map[string]interface{}{ - "tribe_uuid": mockTribeUUID, - "name": mockChannelName, + return person, tribe } - // Mock request body - requestBodyBytes, err := json.Marshal(mockRequestBody) - assert.NoError(t, err) - t.Run("Should test that a user that is not authenticated cannot create a channel", func(t *testing.T) { + _, tribe := createTestPersonAndTribe("person_chan_pubkey", "tribe_uuid", "New Tribe") + + requestBody := map[string]interface{}{ + "tribe_uuid": tribe.UUID, + "name": "Test Channel", + } + requestBodyBytes, err := json.Marshal(requestBody) + assert.NoError(t, err) + req, err := http.NewRequest("POST", "/channel", bytes.NewBuffer(requestBodyBytes)) assert.NoError(t, err) rr := httptest.NewRecorder() - mockDb.On("GetTribe", mockTribeUUID).Return(db.Tribe{OwnerPubKey: mockPubKey}) - cHandler.CreateChannel(rr, req) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) t.Run("Should test that an authenticated user can create a channel", func(t *testing.T) { + person, tribe := createTestPersonAndTribe("person_chan_pubkey", "tribe_uuid", "New Tribe") + + requestBody := map[string]interface{}{ + "tribe_uuid": tribe.UUID, + "name": "New Channel", + } + requestBodyBytes, err := json.Marshal(requestBody) + assert.NoError(t, err) + req, err := http.NewRequest("POST", "/channel", bytes.NewBuffer(requestBodyBytes)) assert.NoError(t, err) - req = req.WithContext(context.WithValue(req.Context(), auth.ContextKey, mockPubKey)) + req = req.WithContext(context.WithValue(req.Context(), auth.ContextKey, person.OwnerPubKey)) rr := httptest.NewRecorder() - mockDb.On("GetTribe", mockTribeUUID).Return(db.Tribe{OwnerPubKey: mockPubKey}) - mockDb.On("GetChannelsByTribe", mockTribeUUID).Return([]db.Channel{}) - mockDb.On("CreateChannel", mock.Anything).Return(db.Channel{}, nil) - cHandler.CreateChannel(rr, req) assert.Equal(t, http.StatusOK, rr.Code) + + channels := db.TestDB.GetChannelsByTribe(tribe.UUID) + assert.Equal(t, 1, len(channels)) + assert.Equal(t, "New Channel", channels[0].Name) }) t.Run("Should test that a user cannot create a channel with a name that already exists", func(t *testing.T) { - mockDb.ExpectedCalls = nil + person, tribe := createTestPersonAndTribe("person_chan_pubkey", "tribe_uuid", "New Tribe") + + channel := db.Channel{ + TribeUUID: tribe.UUID, + Name: "Test Channel", + Deleted: false, + } + db.TestDB.CreateChannel(channel) + + requestBody := map[string]interface{}{ + "tribe_uuid": tribe.UUID, + "name": "Test Channel", + } + requestBodyBytes, err := json.Marshal(requestBody) + assert.NoError(t, err) req, err := http.NewRequest("POST", "/channel", bytes.NewBuffer(requestBodyBytes)) assert.NoError(t, err) - req = req.WithContext(context.WithValue(req.Context(), auth.ContextKey, mockPubKey)) + req = req.WithContext(context.WithValue(req.Context(), auth.ContextKey, person.OwnerPubKey)) rr := httptest.NewRecorder() - mockDb.On("GetTribe", mockTribeUUID).Return(db.Tribe{OwnerPubKey: mockPubKey}) - mockDb.On("GetChannelsByTribe", mockTribeUUID).Return([]db.Channel{{Name: mockChannelName}}) - cHandler.CreateChannel(rr, req) assert.Equal(t, http.StatusNotAcceptable, rr.Code) - - // Ensure that the expected methods were called - mockDb.AssertExpectations(t) }) }