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 c4f6499 commit 9ade669
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions handlers/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,35 @@ 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)

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)

})

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 @@ -824,35 +853,6 @@ 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 9ade669

Please sign in to comment.