Skip to content

Commit

Permalink
Refactor TestCreateChannel UT
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadUmer44 committed Jun 25, 2024
1 parent c7019ee commit 6644340
Showing 1 changed file with 70 additions and 28 deletions.
98 changes: 70 additions & 28 deletions handlers/channel_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/lib/pq"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -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)
})
}

Expand Down

0 comments on commit 6644340

Please sign in to comment.