-
Notifications
You must be signed in to change notification settings - Fork 60
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 3 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 |
---|---|---|
|
@@ -454,7 +454,7 @@ func (oh *workspaceHandler) GetUserRoles(w http.ResponseWriter, r *http.Request) | |
json.NewEncoder(w).Encode(userRoles) | ||
} | ||
|
||
func GetUserWorkspaces(w http.ResponseWriter, r *http.Request) { | ||
func (oh *workspaceHandler) GetUserWorkspaces(w http.ResponseWriter, r *http.Request) { | ||
userIdParam := chi.URLParam(r, "userId") | ||
userId, _ := utils.ConvertStringToUint(userIdParam) | ||
|
||
|
@@ -464,9 +464,9 @@ func GetUserWorkspaces(w http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
user := db.DB.GetPerson(userId) | ||
user := oh.db.GetPerson(userId) | ||
// get the user workspaces | ||
workspaces := GetAllUserWorkspaces(user.OwnerPubKey) | ||
workspaces := oh.GetAllUserWorkspaces(user.OwnerPubKey) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(workspaces) | ||
|
@@ -482,23 +482,23 @@ func (oh *workspaceHandler) GetUserDropdownWorkspaces(w http.ResponseWriter, r * | |
return | ||
} | ||
|
||
user := db.DB.GetPerson(userId) | ||
user := oh.db.GetPerson(userId) | ||
|
||
// get the workspaces created by the user, then get all the workspaces | ||
// the user has been added to, loop through to get the workspace | ||
workspaces := GetCreatedWorkspaces(user.OwnerPubKey) | ||
assignedWorkspaces := db.DB.GetUserAssignedWorkspaces(user.OwnerPubKey) | ||
workspaces := oh.GetCreatedWorkspaces(user.OwnerPubKey) | ||
assignedWorkspaces := oh.db.GetUserAssignedWorkspaces(user.OwnerPubKey) | ||
for _, value := range assignedWorkspaces { | ||
uuid := value.WorkspaceUuid | ||
workspace := db.DB.GetWorkspaceByUuid(uuid) | ||
bountyCount := db.DB.GetWorkspaceBountyCount(uuid) | ||
hasRole := db.UserHasAccess(user.OwnerPubKey, uuid, db.ViewReport) | ||
workspace := oh.db.GetWorkspaceByUuid(uuid) | ||
bountyCount := oh.db.GetWorkspaceBountyCount(uuid) | ||
hasRole := oh.userHasAccess(user.OwnerPubKey, uuid, db.ViewReport) | ||
hasBountyRoles := oh.userHasManageBountyRoles(user.OwnerPubKey, uuid) | ||
|
||
// don't add deleted workspaces to the list | ||
if !workspace.Deleted && hasBountyRoles { | ||
if hasRole { | ||
budget := db.DB.GetWorkspaceBudget(uuid) | ||
budget := oh.db.GetWorkspaceBudget(uuid) | ||
workspace.Budget = budget.TotalBudget | ||
} else { | ||
workspace.Budget = 0 | ||
|
@@ -512,16 +512,16 @@ func (oh *workspaceHandler) GetUserDropdownWorkspaces(w http.ResponseWriter, r * | |
json.NewEncoder(w).Encode(workspaces) | ||
} | ||
|
||
func GetCreatedWorkspaces(pubkey string) []db.Workspace { | ||
workspaces := db.DB.GetUserCreatedWorkspaces(pubkey) | ||
func (oh *workspaceHandler) GetCreatedWorkspaces(pubkey string) []db.Workspace { | ||
workspaces := oh.db.GetUserCreatedWorkspaces(pubkey) | ||
// add bounty count to the workspace | ||
for index, value := range workspaces { | ||
uuid := value.Uuid | ||
bountyCount := db.DB.GetWorkspaceBountyCount(uuid) | ||
hasRole := db.UserHasAccess(pubkey, uuid, db.ViewReport) | ||
bountyCount := oh.db.GetWorkspaceBountyCount(uuid) | ||
hasRole := oh.userHasAccess(pubkey, uuid, db.ViewReport) | ||
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. UserHasAccess is in the Database interface so you can change it to 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 The same error still persists. I tried everything, but the UserHasAccess error is not fixed. Without verifying UserHasAccess, the unit test does not pass. 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. Push your latest changes, let me give it a look 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. ok |
||
|
||
if hasRole { | ||
budget := db.DB.GetWorkspaceBudget(uuid) | ||
budget := oh.db.GetWorkspaceBudget(uuid) | ||
workspaces[index].Budget = budget.TotalBudget | ||
} else { | ||
workspaces[index].Budget = 0 | ||
|
@@ -687,7 +687,7 @@ func (oh *workspaceHandler) PollUserWorkspacesBudget(w http.ResponseWriter, r *h | |
} | ||
|
||
// get the user workspaces | ||
workspaces := GetAllUserWorkspaces(pubKeyFromAuth) | ||
workspaces := oh.GetAllUserWorkspaces(pubKeyFromAuth) | ||
// loop through the worksppaces and get each workspace invoice | ||
for _, space := range workspaces { | ||
// get all workspace invoice | ||
|
@@ -737,7 +737,7 @@ func GetInvoicesCount(w http.ResponseWriter, r *http.Request) { | |
json.NewEncoder(w).Encode(invoiceCount) | ||
} | ||
|
||
func GetAllUserInvoicesCount(w http.ResponseWriter, r *http.Request) { | ||
func (oh *workspaceHandler) GetAllUserInvoicesCount(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) | ||
|
||
|
@@ -748,9 +748,9 @@ func GetAllUserInvoicesCount(w http.ResponseWriter, r *http.Request) { | |
} | ||
|
||
allCount := int64(0) | ||
workspaces := GetAllUserWorkspaces(pubKeyFromAuth) | ||
workspaces := oh.GetAllUserWorkspaces(pubKeyFromAuth) | ||
for _, space := range workspaces { | ||
invoiceCount := db.DB.GetWorkspaceInvoicesCount(space.Uuid) | ||
invoiceCount := oh.db.GetWorkspaceInvoicesCount(space.Uuid) | ||
allCount += invoiceCount | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
|
@@ -995,20 +995,20 @@ func (oh *workspaceHandler) GetFeaturesByWorkspaceUuid(w http.ResponseWriter, r | |
json.NewEncoder(w).Encode(workspaceFeatures) | ||
} | ||
|
||
func GetAllUserWorkspaces(pubkey string) []db.Workspace { | ||
func (oh *workspaceHandler) GetAllUserWorkspaces(pubkey string) []db.Workspace { | ||
// get the workspaces created by the user, then get all the workspaces | ||
// the user has been added to, loop through to get the workspace | ||
workspaces := GetCreatedWorkspaces(pubkey) | ||
assignedWorkspaces := db.DB.GetUserAssignedWorkspaces(pubkey) | ||
workspaces := oh.GetCreatedWorkspaces(pubkey) | ||
assignedWorkspaces := oh.db.GetUserAssignedWorkspaces(pubkey) | ||
for _, value := range assignedWorkspaces { | ||
uuid := value.WorkspaceUuid | ||
workspace := db.DB.GetWorkspaceByUuid(uuid) | ||
bountyCount := db.DB.GetWorkspaceBountyCount(uuid) | ||
workspace := oh.db.GetWorkspaceByUuid(uuid) | ||
bountyCount := oh.db.GetWorkspaceBountyCount(uuid) | ||
hasRole := db.UserHasAccess(pubkey, uuid, db.ViewReport) | ||
// don't add deleted workspaces to the list | ||
if !workspace.Deleted { | ||
if hasRole { | ||
budget := db.DB.GetWorkspaceBudget(uuid) | ||
budget := oh.db.GetWorkspaceBudget(uuid) | ||
workspace.Budget = budget.TotalBudget | ||
} else { | ||
workspace.Budget = 0 | ||
|
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) | ||
oHandler.userHasAccess = db.TestDB.DeleteWorkSpaceUserAccessData | ||
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. @MuhammadUmer44 This defeats the purpose, the function returns true it does not check if the user has access. |
||
|
||
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 |
||
} | ||
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