Skip to content

Commit

Permalink
Write AddUserRoles UT fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
AhsanFarooqDev committed Jul 2, 2024
1 parent ce2dcaa commit c4f6499
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions handlers/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"math/rand"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -657,7 +656,7 @@ func TestAddUserRoles(t *testing.T) {
oHandler := NewWorkspaceHandler(db.TestDB)

person := db.Person{
Uuid: "uuid",
Uuid: uuid.New().String(),
OwnerAlias: "alias",
UniqueName: "unique_name",
OwnerPubKey: "pubkey",
Expand All @@ -666,7 +665,7 @@ func TestAddUserRoles(t *testing.T) {
}

person2 := db.Person{
Uuid: "uuid2",
Uuid: uuid.New().String(),
OwnerAlias: "alias2",
UniqueName: "unique_name2",
OwnerPubKey: "pubkey2",
Expand All @@ -677,7 +676,7 @@ func TestAddUserRoles(t *testing.T) {
db.TestDB.CreateOrEditPerson(person2)

workspace := db.Workspace{
Uuid: "workspace_uuid",
Uuid: uuid.New().String(),
Name: "workspace_name",
OwnerPubKey: person2.OwnerPubKey,
Github: "gtihub",
Expand All @@ -702,38 +701,6 @@ func TestAddUserRoles(t *testing.T) {

db.TestDB.CreateWorkspaceUser(workspaceUser)

t.Run("Should test that when the right conditions are met a user can be added to a workspace", func(t *testing.T) {
handlerUserHasAccess := func(pubKeyFromAuth string, uuid string, role string) bool {
return true
}
oHandler.userHasAccess = handlerUserHasAccess

ctx := context.WithValue(context.Background(), auth.ContextKey, "pub-key")

requestBody, _ := json.Marshal(userRoles)
rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", workspace.Uuid)
rctx.URLParams.Add("user", person2.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodPost, "/users/role/"+workspace.Uuid+"/"+person2.OwnerPubKey, bytes.NewReader(requestBody))
if err != nil {
t.Fatal(err)
}

fetchedWorkspaceUser := db.TestDB.GetWorkspaceUser(person2.OwnerPubKey, workspace.Uuid)
fetchedUserRole := db.TestDB.GetUserRoles(workspace.Uuid, person2.OwnerPubKey)

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.AddUserRoles).ServeHTTP(rr, req)
t.Log("Response code:", rr.Code, "Response body:", rr.Body.String())
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, person2.OwnerPubKey, fetchedWorkspaceUser.OwnerPubKey)

require.NotEmpty(t, fetchedUserRole, "No roles fetched for user %s in workspace %s", person2.OwnerPubKey, workspace.Uuid)

assert.Equal(t, userRoles[0].Role, fetchedUserRole[0].Role)

})

t.Run("Should test that when an unauthorized user hits the endpoint it returns a 401 error", func(t *testing.T) {
workspaceUUID := workspace.Uuid

Expand Down Expand Up @@ -857,6 +824,35 @@ func TestAddUserRoles(t *testing.T) {
assert.Equal(t, http.StatusUnauthorized, rr.Code)
})

t.Run("Should test that when the right conditions are met a user can be added to a workspace", func(t *testing.T) {
handlerUserHasAccess := func(pubKeyFromAuth string, uuid string, role string) bool {
return true
}
oHandler.userHasAccess = handlerUserHasAccess

ctx := context.WithValue(context.Background(), auth.ContextKey, "pub-key")

requestBody, _ := json.Marshal(userRoles)
rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", workspace.Uuid)
rctx.URLParams.Add("user", person2.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodPost, "/users/role/"+workspace.Uuid+"/"+person2.OwnerPubKey, bytes.NewReader(requestBody))
if err != nil {
t.Fatal(err)
}

fetchedWorkspaceUser := db.TestDB.GetWorkspaceUser(person2.OwnerPubKey, workspace.Uuid)

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

fetchedUserRole := db.TestDB.GetUserRoles(workspace.Uuid, person2.OwnerPubKey)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, person2.OwnerPubKey, fetchedWorkspaceUser.OwnerPubKey)
assert.Equal(t, userRoles[0].Role, fetchedUserRole[0].Role)

})

}

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

0 comments on commit c4f6499

Please sign in to comment.