Skip to content

Commit

Permalink
Merge pull request #1828 from AhsanFarooqDev/Write_Test_For_GetUserRoles
Browse files Browse the repository at this point in the history
Write GetUserRoles UT
  • Loading branch information
elraphty authored Jul 2, 2024
2 parents 1f570ed + 89977db commit 67fc346
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type Database interface {
DeleteWorkspaceUser(orgUser WorkspaceUsersData, org string) WorkspaceUsersData
GetBountyRoles() []BountyRoles
CreateUserRoles(roles []WorkspaceUserRoles, uuid string, pubkey string) []WorkspaceUserRoles
GetUserRoles(uuid string, pubkey string) []WorkspaceUserRoles
GetUserCreatedWorkspaces(pubkey string) []Workspace
GetUserAssignedWorkspaces(pubkey string) []WorkspaceUsers
AddBudgetHistory(budget BudgetHistory) BudgetHistory
Expand Down
4 changes: 2 additions & 2 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,11 @@ func (oh *workspaceHandler) AddUserRoles(w http.ResponseWriter, r *http.Request)
json.NewEncoder(w).Encode(insertRoles)
}

func GetUserRoles(w http.ResponseWriter, r *http.Request) {
func (oh *workspaceHandler) GetUserRoles(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
user := chi.URLParam(r, "user")

userRoles := db.DB.GetUserRoles(uuid, user)
userRoles := oh.db.GetUserRoles(uuid, user)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(userRoles)
Expand Down
67 changes: 67 additions & 0 deletions handlers/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,74 @@ func TestAddUserRoles(t *testing.T) {
}

func TestGetUserRoles(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
oHandler := NewWorkspaceHandler(db.TestDB)

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

person2 := db.Person{
Uuid: uuid.New().String(),
OwnerAlias: "alias2",
UniqueName: "unique_name2",
OwnerPubKey: "pubkey2",
PriceToMeet: 0,
Description: "description2",
}
db.TestDB.CreateOrEditPerson(person)
db.TestDB.CreateOrEditPerson(person2)

workspace := db.Workspace{
Uuid: uuid.New().String(),
Name: uuid.New().String(),
OwnerPubKey: person2.OwnerPubKey,
Github: "gtihub",
Website: "website",
Description: "description",
}
db.TestDB.CreateOrEditWorkspace(workspace)

userRoles := []db.WorkspaceUserRoles{
db.WorkspaceUserRoles{
WorkspaceUuid: workspace.Uuid,
OwnerPubKey: person2.OwnerPubKey,
Role: "ADD BOUNTY",
},
}

db.TestDB.CreateUserRoles(userRoles, workspace.Uuid, person2.OwnerPubKey)

t.Run("Should test that the ADD BOUNTY role is returned for person2 from the API call response and the API response array length is 1", func(t *testing.T) {

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

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.MethodGet, "/users/role/"+workspace.Uuid+"/"+person2.OwnerPubKey, nil)
if err != nil {
t.Fatal(err)
}

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

var returnedUserRole []db.WorkspaceUserRoles
err = json.Unmarshal(rr.Body.Bytes(), &returnedUserRole)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, userRoles[0].Role, returnedUserRole[0].Role)
assert.Equal(t, 1, len(returnedUserRole))

})
}

func TestCreateWorkspaceUser(t *testing.T) {
Expand Down
49 changes: 49 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion routes/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func WorkspaceRoutes() chi.Router {

r.Get("/foruser/{uuid}", handlers.GetWorkspaceUser)
r.Get("/bounty/roles", handlers.GetBountyRoles)
r.Get("/users/role/{uuid}/{user}", handlers.GetUserRoles)
r.Get("/users/role/{uuid}/{user}", workspaceHandlers.GetUserRoles)
r.Get("/budget/{uuid}", workspaceHandlers.GetWorkspaceBudget)
r.Get("/budget/history/{uuid}", workspaceHandlers.GetWorkspaceBudgetHistory)
r.Get("/payments/{uuid}", handlers.GetPaymentHistory)
Expand Down

0 comments on commit 67fc346

Please sign in to comment.