diff --git a/handlers/workspaces_test.go b/handlers/workspaces_test.go index 7e1180940..9ddcd1fbb 100644 --- a/handlers/workspaces_test.go +++ b/handlers/workspaces_test.go @@ -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 @@ -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) {