Skip to content

Commit

Permalink
create channel test
Browse files Browse the repository at this point in the history
  • Loading branch information
Vayras committed Feb 21, 2024
1 parent f247c84 commit 2ec4cff
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
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)
}

0 comments on commit 2ec4cff

Please sign in to comment.