Skip to content

Commit

Permalink
Implemented GetWorkspaceFeaturesCount UT
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulWahab3181 committed Jul 2, 2024
1 parent ba520dc commit dee895c
Showing 1 changed file with 82 additions and 2 deletions.
84 changes: 82 additions & 2 deletions handlers/features_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package handlers

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

"github.com/go-chi/chi"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stretchr/testify/assert"
)

func TestCreateOrEditFeatures(t *testing.T) {

Expand All @@ -15,7 +26,76 @@ func TestGetFeaturesByWorkspaceUuid(t *testing.T) {
}

func TestGetWorkspaceFeaturesCount(t *testing.T) {

teardownSuite := SetupSuite(t)
defer teardownSuite(t)

oHandler := NewFeatureHandler(db.TestDB)

person := db.Person{
Uuid: "uuid",
OwnerAlias: "alias",
UniqueName: "unique_name",
OwnerPubKey: "pubkey",
PriceToMeet: 0,
Description: "description",
}
db.TestDB.CreateOrEditPerson(person)

workspace := db.Workspace{
Uuid: "workspace_uuid",
Name: "workspace_name",
OwnerPubKey: "person.OwnerPubkey",
Github: "gtihub",
Website: "website",
Description: "description",
}
db.TestDB.CreateOrEditWorkspace(workspace)

feature := db.WorkspaceFeatures{
Uuid: "feature_uuid",
WorkspaceUuid: workspace.Uuid,
Name: "feature_name",
Url: "feature_url",
Priority: 0,
}
db.TestDB.CreateOrEditFeature(feature)

ctx := context.WithValue(context.Background(), auth.ContextKey, workspace.OwnerPubKey)

t.Run("Should test that it throws a 401 error if a user is not authorized", func(t *testing.T) {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", workspace.Uuid)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/workspace/count/"+workspace.Uuid, nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.GetWorkspaceFeaturesCount).ServeHTTP(rr, req)

assert.Equal(t, http.StatusUnauthorized, rr.Code)
})

t.Run("Should test that the features count returned from the API response for the workspace is equal to the number of features created for the workspace", func(t *testing.T) {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", workspace.Uuid)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/workspace/count/"+workspace.Uuid, nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.GetWorkspaceFeaturesCount).ServeHTTP(rr, req)

var returnedWorkspaceFeatures int64
err = json.Unmarshal(rr.Body.Bytes(), &returnedWorkspaceFeatures)
assert.NoError(t, err)

featureCount := db.TestDB.GetWorkspaceFeaturesCount(workspace.Uuid)

assert.Equal(t, returnedWorkspaceFeatures, featureCount)
assert.Equal(t, http.StatusOK, rr.Code)
})
}

func TestGetFeatureByUuid(t *testing.T) {
Expand Down

0 comments on commit dee895c

Please sign in to comment.