Skip to content

Commit

Permalink
Refactor TestUnitCreateOrEditWorkspace UT
Browse files Browse the repository at this point in the history
  • Loading branch information
AhsanFarooqDev committed Jun 26, 2024
1 parent 20f56e5 commit efb5502
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
7 changes: 3 additions & 4 deletions handlers/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/lib/pq"
"github.com/google/uuid"
"net/http"
"net/http/httptest"
"strconv"
Expand All @@ -14,9 +14,8 @@ import (
"github.com/lib/pq"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
mocks "github.com/stakwork/sphinx-tribes/mocks"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestCreateChannel(t *testing.T) {
Expand Down Expand Up @@ -73,7 +72,7 @@ func TestCreateChannel(t *testing.T) {
})

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")
person, tribe := createTestPersonAndTribe("person_chan_pubkey", uuid.New().String(), "New Tribe")

requestBody := map[string]interface{}{
"tribe_uuid": tribe.UUID,
Expand Down
67 changes: 49 additions & 18 deletions handlers/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"golang.org/x/exp/rand"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/go-chi/chi"
"github.com/stakwork/sphinx-tribes/auth"
Expand All @@ -20,9 +23,9 @@ import (
)

func TestUnitCreateOrEditWorkspace(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
mockDb := mocks.NewDatabase(t)
oHandler := NewWorkspaceHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
oHandler := NewWorkspaceHandler(db.TestDB)

t.Run("should return error if body is not a valid json", func(t *testing.T) {
rr := httptest.NewRecorder()
Expand Down Expand Up @@ -61,6 +64,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) {
handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace)

invalidJson := []byte(`{"name": ""}`)
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson))
if err != nil {
t.Fatal(err)
Expand All @@ -76,6 +80,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) {
handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace)

invalidJson := []byte(`{"name": "DemoTestingNewWorkspace"}`)
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson))
if err != nil {
t.Fatal(err)
Expand All @@ -91,6 +96,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) {
handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace)

invalidJson := []byte(`{"name": " "}`)
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson))
if err != nil {
t.Fatal(err)
Expand All @@ -105,13 +111,20 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace)

mockDb.On("GetWorkspaceByUuid", mock.AnythingOfType("string")).Return(db.Workspace{}).Once()
mockDb.On("GetWorkspaceByName", "Abdul").Return(db.Workspace{}).Once()
mockDb.On("CreateOrEditWorkspace", mock.MatchedBy(func(org db.Workspace) bool {
return org.Name == "Abdul" && org.Uuid != "" && org.Updated != nil && org.Created != nil
})).Return(db.Workspace{Name: "Abdul"}, nil).Once()
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
rand.Seed(uint64(time.Now().UnixNano()))

b := make([]byte, 10)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
name := string(b)

spacedName := " " + name + " "

jsonInput := []byte(fmt.Sprintf(`{"name": "%s", "owner_pubkey": "test-key", "description": "Workspace Bounties Description"}`, spacedName))

jsonInput := []byte(`{"name": " Abdul ", "owner_pubkey": "test-key" ,"description": "Test"}`)
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(jsonInput))
if err != nil {
t.Fatal(err)
Expand All @@ -127,28 +140,46 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) {
t.Fatal(err)
}

assert.Equal(t, "Abdul", responseOrg.Name)
assert.Equal(t, name, responseOrg.Name)
})

t.Run("should successfully add workspace if request is valid", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace)

mockDb.On("GetWorkspaceByUuid", mock.AnythingOfType("string")).Return(db.Workspace{}).Once()
mockDb.On("GetWorkspaceByName", "TestWorkspace").Return(db.Workspace{}).Once()
mockDb.On("CreateOrEditWorkspace", mock.MatchedBy(func(org db.Workspace) bool {
return org.Name == "TestWorkspace" && org.Uuid != "" && org.Updated != nil && org.Created != nil
})).Return(db.Workspace{}, nil).Once()
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
rand.Seed(uint64(time.Now().UnixNano()))

invalidJson := []byte(`{"name": "TestWorkspace", "owner_pubkey": "test-key" ,"description": "Test"}`)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson))
b := make([]byte, 10)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
name := string(b)

workspace := db.Workspace{
Uuid: uuid.New().String(),
Name: name,
OwnerPubKey: uuid.New().String(),
Github: "https://github.com/bounties",
Website: "https://www.bountieswebsite.com",
Description: "Workspace Bounties Description",
}
db.TestDB.CreateOrEditWorkspace(workspace)

Workspace := db.TestDB.GetWorkspaceByUuid(workspace.Uuid)
workspace.ID = Workspace.ID

requestBody, _ := json.Marshal(workspace)
ctx := context.WithValue(context.Background(), auth.ContextKey, workspace.OwnerPubKey)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(requestBody))
if err != nil {
t.Fatal(err)
}

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, workspace, Workspace)
})
t.Run("should return error if org description is empty or too long", func(t *testing.T) {
tests := []struct {
Expand All @@ -164,7 +195,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace)
invalidJson := []byte(fmt.Sprintf(`{"name": "TestWorkspace", "owner_pubkey": "test-key", "description": "%s"}`, tc.description))

ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson))
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit efb5502

Please sign in to comment.