Skip to content

Commit

Permalink
Merge pull request #1758 from MuhammadUmer44/Refactor-TestCreateChann…
Browse files Browse the repository at this point in the history
…el-UT

Refactor TestCreateChannel To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 25, 2024
2 parents 4bbc021 + 6644340 commit 20f56e5
Showing 1 changed file with 71 additions and 29 deletions.
100 changes: 71 additions & 29 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"
"strconv"
Expand All @@ -19,66 +20,107 @@ import (
)

func TestCreateChannel(t *testing.T) {
mockDb := mocks.NewDatabase(t)
cHandler := NewChannelHandler(mockDb)

// Mock data for testing
mockPubKey := "mock_pubkey"
mockTribeUUID := "mock_tribe_uuid"
mockChannelName := "mock_channel"
mockRequestBody := map[string]interface{}{
"tribe_uuid": mockTribeUUID,
"name": mockChannelName,
}
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

// Mock request body
requestBodyBytes, err := json.Marshal(mockRequestBody)
assert.NoError(t, err)
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)

return person, tribe
}

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 20f56e5

Please sign in to comment.