Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for Channel handlers #1543

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions handlers/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import (
"github.com/stakwork/sphinx-tribes/db"
)

func DeleteChannel(w http.ResponseWriter, r *http.Request) {
type channelHandler struct {
db db.Database
}

func NewChannelHandler(db db.Database) *channelHandler {
return &channelHandler{db: db}
}

func (ch *channelHandler) DeleteChannel(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)

Expand All @@ -30,8 +38,8 @@ func DeleteChannel(w http.ResponseWriter, r *http.Request) {
return
}

existing := db.DB.GetChannel(uint(id))
existingTribe := db.DB.GetTribe(existing.TribeUUID)
existing := ch.db.GetChannel(uint(id))
existingTribe := ch.db.GetTribe(existing.TribeUUID)
if existing.ID == 0 {
fmt.Println("existing id is 0")
w.WriteHeader(http.StatusUnauthorized)
Expand All @@ -43,15 +51,15 @@ func DeleteChannel(w http.ResponseWriter, r *http.Request) {
return
}

db.DB.UpdateChannel(uint(id), map[string]interface{}{
ch.db.UpdateChannel(uint(id), map[string]interface{}{
"deleted": true,
})

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(true)
}

func CreateChannel(w http.ResponseWriter, r *http.Request) {
func (ch *channelHandler) CreateChannel(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)

Expand All @@ -66,14 +74,14 @@ func CreateChannel(w http.ResponseWriter, r *http.Request) {
}

//check that the tribe has the same pubKeyFromAuth
tribe := db.DB.GetTribe(channel.TribeUUID)
tribe := ch.db.GetTribe(channel.TribeUUID)
if tribe.OwnerPubKey != pubKeyFromAuth {
fmt.Println(err)
w.WriteHeader(http.StatusNotAcceptable)
return
}

tribeChannels := db.DB.GetChannelsByTribe(channel.TribeUUID)
tribeChannels := ch.db.GetChannelsByTribe(channel.TribeUUID)
for _, tribeChannel := range tribeChannels {
if tribeChannel.Name == channel.Name {
fmt.Println("Channel name already in use")
Expand All @@ -83,7 +91,7 @@ func CreateChannel(w http.ResponseWriter, r *http.Request) {
}
}

channel, err = db.DB.CreateChannel(channel)
channel, err = ch.db.CreateChannel(channel)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusNotAcceptable)
Expand Down
55 changes: 55 additions & 0 deletions handlers/channel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package handlers

import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
mocks "github.com/stakwork/sphinx-tribes/mocks"
)

func TestCreateChannel(t *testing.T) {
mockDB := new(mocks.Database)
handler := NewChannelHandler(mockDB)

authPubKey := "authPubKey"
ctx := context.WithValue(context.Background(), auth.ContextKey, authPubKey)

tribeUUID := uuid.New().String()
mockChannel := db.Channel{Name: "TestChannel", TribeUUID: tribeUUID}
mockTribe := db.Tribe{UUID: tribeUUID, OwnerPubKey: authPubKey}

mockDB.On("GetTribe", tribeUUID).Return(mockTribe, nil)
mockDB.On("CreateChannel", mock.AnythingOfType("db.Channel")).Return(mockChannel, nil)

channelData, _ := json.Marshal(mockChannel)
req, err := http.NewRequest("POST", "/channel", bytes.NewBuffer(channelData))
assert.NoError(t, err)
req = req.WithContext(ctx)

rr := httptest.NewRecorder()

r := chi.NewRouter()
r.Post("/channel", handler.CreateChannel)

r.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code, "Expected status OK")

var responseChannel db.Channel
err = json.Unmarshal(rr.Body.Bytes(), &responseChannel)
assert.NoError(t, err)
assert.Equal(t, mockChannel.Name, responseChannel.Name, "Channel names should match")

mockDB.AssertExpectations(t)
}
Loading