-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write Test For GetUserDropdownWorkspaces
#1813
Changes from all commits
acd70a2
9139d39
0c096ca
4983d12
8738526
7c088ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"math/rand" | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
@@ -1200,7 +1201,83 @@ func TestGetWorkspaceUsers(t *testing.T) { | |
} | ||
|
||
func TestGetUserDropdownWorkspaces(t *testing.T) { | ||
teardownSuite := SetupSuite(t) | ||
defer teardownSuite(t) | ||
|
||
db.TestDB.DeleteWorkSpaceAllData() | ||
|
||
oHandler := NewWorkspaceHandler(db.TestDB) | ||
|
||
person := db.Person{ | ||
Uuid: uuid.New().String(), | ||
OwnerAlias: "test-alias", | ||
UniqueName: "test-unique-name", | ||
OwnerPubKey: "test-pubkey", | ||
PriceToMeet: 0, | ||
Description: "test-description", | ||
} | ||
person2 := db.Person{ | ||
Uuid: uuid.New().String(), | ||
OwnerAlias: "test-alias2", | ||
UniqueName: "test-unique-name2", | ||
OwnerPubKey: "test-pubkey2", | ||
PriceToMeet: 0, | ||
Description: "test-description2", | ||
} | ||
db.TestDB.CreateOrEditPerson(person) | ||
db.TestDB.CreateOrEditPerson(person2) | ||
|
||
workspace := db.Workspace{ | ||
Uuid: uuid.New().String(), | ||
Name: "test-workspace" + uuid.New().String(), | ||
OwnerPubKey: person2.OwnerPubKey, | ||
Github: "https://github.com/test", | ||
Website: "https://www.testwebsite.com", | ||
Description: "test-description", | ||
} | ||
db.TestDB.CreateOrEditWorkspace(workspace) | ||
workspace = db.TestDB.GetWorkspaceByUuid(workspace.Uuid) | ||
ctx := context.WithValue(context.Background(), auth.ContextKey, workspace.OwnerPubKey) | ||
|
||
roles := []db.WorkspaceUserRoles{ | ||
{WorkspaceUuid: workspace.Uuid, OwnerPubKey: person2.OwnerPubKey, Role: "ADD BOUNTY"}, | ||
{WorkspaceUuid: workspace.Uuid, OwnerPubKey: person2.OwnerPubKey, Role: "UPDATE BOUNTY"}, | ||
{WorkspaceUuid: workspace.Uuid, OwnerPubKey: person2.OwnerPubKey, Role: "DELETE BOUNTY"}, | ||
{WorkspaceUuid: workspace.Uuid, OwnerPubKey: person2.OwnerPubKey, Role: "PAY BOUNTY"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a "VIEW REPORT" role to the user, which will make the user have 5 roles, it will fix the userHasAccess error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elraphty I have try but i got same error: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dropped a comment |
||
{WorkspaceUuid: workspace.Uuid, OwnerPubKey: person2.OwnerPubKey, Role: "VIEW REPORT"}, | ||
} | ||
db.TestDB.CreateUserRoles(roles, workspace.Uuid, person2.OwnerPubKey) | ||
|
||
dbPerson := db.TestDB.GetPersonByUuid(person2.Uuid) | ||
|
||
t.Run("should return user dropdown workspaces", func(t *testing.T) { | ||
rr := httptest.NewRecorder() | ||
rctx := chi.NewRouteContext() | ||
rctx.URLParams.Add("userId", strconv.Itoa(int(dbPerson.ID))) | ||
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/user/dropdown/"+strconv.Itoa(int(dbPerson.ID)), nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
handler := http.HandlerFunc(oHandler.GetUserDropdownWorkspaces) | ||
handler.ServeHTTP(rr, req) | ||
|
||
assert.Equal(t, http.StatusOK, rr.Code) | ||
|
||
var responseWorkspaces []db.Workspace | ||
err = json.Unmarshal(rr.Body.Bytes(), &responseWorkspaces) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.NotEmpty(t, responseWorkspaces) | ||
assert.Equal(t, workspace.Uuid, responseWorkspaces[0].Uuid) | ||
assert.Equal(t, workspace.Name, responseWorkspaces[0].Name) | ||
assert.Equal(t, workspace.OwnerPubKey, responseWorkspaces[0].OwnerPubKey) | ||
assert.Equal(t, workspace.Github, responseWorkspaces[0].Github) | ||
assert.Equal(t, workspace.Website, responseWorkspaces[0].Website) | ||
assert.Equal(t, workspace.Description, responseWorkspaces[0].Description) | ||
}) | ||
} | ||
|
||
func TestCreateOrEditWorkspaceRepository(t *testing.T) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put these functions in the db/test_config.go file, since we only need them for tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elraphty ok