Skip to content

Commit

Permalink
Merge pull request stakwork#1830 from MahtabBukhari/Write_Test_For_Ge…
Browse files Browse the repository at this point in the history
…tFeaturesByWorkspaceUuid

Write Test For GetFeaturesByWorkspaceUuid
  • Loading branch information
elraphty authored Jul 3, 2024
2 parents e510e0b + a588e18 commit e3e3769
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 10 deletions.
79 changes: 79 additions & 0 deletions handlers/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,85 @@ func TestDeleteFeature(t *testing.T) {
}

func TestGetFeaturesByWorkspaceUuid(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

oHandler := NewWorkspaceHandler(db.TestDB)

t.Run("should return error if a user is not authorized", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(oHandler.GetFeaturesByWorkspaceUuid)

ctx := context.WithValue(context.Background(), auth.ContextKey, "")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/forworkspace/"+workspace.Uuid, nil)
if err != nil {
t.Fatal(err)
}

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusUnauthorized, rr.Code)
})

t.Run("created feature should be present in the returned array", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(oHandler.GetFeaturesByWorkspaceUuid)

person := db.Person{
Uuid: uuid.New().String(),
OwnerAlias: "alias",
UniqueName: "unique_name",
OwnerPubKey: "pubkey",
PriceToMeet: 0,
Description: "description",
}
db.TestDB.CreateOrEditPerson(person)
workspace := db.Workspace{
Uuid: uuid.New().String(),
Name: "unique_workspace_name" + uuid.New().String(),
OwnerPubKey: person.OwnerPubKey,
Github: "gtihub",
Website: "website",
Description: "description",
}
db.TestDB.CreateOrEditWorkspace(workspace)
feature := db.WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
Name: "feature_name",
Url: "https://www.bountieswebsite.com",
Priority: 0,
}
db.TestDB.CreateOrEditFeature(feature)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("workspace_uuid", workspace.Uuid)
ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/forworkspace/"+workspace.Uuid, nil)
if err != nil {
t.Fatal(err)
}

handler.ServeHTTP(rr, req)

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

assert.Equal(t, http.StatusOK, rr.Code)
// Verify that the created feature is present in the returned array
found := false
for _, f := range returnedWorkspaceFeatures {
if f.Uuid == feature.Uuid {
assert.Equal(t, feature.Name, f.Name)
assert.Equal(t, feature.Url, f.Url)
assert.Equal(t, feature.Priority, f.Priority)
found = true
break
}
}
assert.True(t, found, "The created feature should be present in the returned array")
})

}

Expand Down
23 changes: 13 additions & 10 deletions handlers/people_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,10 @@ func TestGetListedPeople(t *testing.T) {
db.TestDB.CreateOrEditPerson(person2)
db.TestDB.CreateOrEditPerson(person3)

fetchedPerson := db.TestDB.GetPerson(person2.ID)
fetchedPerson2 := db.TestDB.GetPerson(person3.ID)
fetchedPerson2 := db.TestDB.GetPerson(person2.ID)
fetchedPerson3 := db.TestDB.GetPerson(person3.ID)
person2.ID = fetchedPerson2.ID
person3.ID = fetchedPerson3.ID

t.Run("should return all listed users", func(t *testing.T) {
rr := httptest.NewRecorder()
Expand All @@ -477,8 +479,8 @@ func TestGetListedPeople(t *testing.T) {
assert.NoError(t, err)

expectedPeople := []db.Person{
fetchedPerson,
fetchedPerson2,
fetchedPerson3,
}

handler.ServeHTTP(rr, req)
Expand All @@ -487,8 +489,8 @@ func TestGetListedPeople(t *testing.T) {
err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.EqualValues(t, person2, fetchedPerson)
assert.EqualValues(t, person3, fetchedPerson2)
assert.EqualValues(t, person2, fetchedPerson2)
assert.EqualValues(t, person3, fetchedPerson3)
assert.EqualValues(t, expectedPeople, returnedPeople)
})

Expand All @@ -501,7 +503,7 @@ func TestGetListedPeople(t *testing.T) {
assert.NoError(t, err)

expectedPeople := []db.Person{
fetchedPerson,
fetchedPerson2,
}

handler.ServeHTTP(rr, req)
Expand All @@ -510,7 +512,7 @@ func TestGetListedPeople(t *testing.T) {
err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.EqualValues(t, person2, fetchedPerson)
assert.EqualValues(t, person2, fetchedPerson2)
assert.EqualValues(t, expectedPeople, returnedPeople)
})

Expand All @@ -519,16 +521,17 @@ func TestGetListedPeople(t *testing.T) {
handler := http.HandlerFunc(pHandler.GetListedPeople)

rctx := chi.NewRouteContext()
languages := person2.Extras["coding_languages"].(string)
req, err := http.NewRequestWithContext(
context.WithValue(context.Background(), chi.RouteCtxKey, rctx),
http.MethodGet,
"page=1&limit=10&languages="+person2.Extras["coding_languages"].(string),
"page=1&limit=10&languages="+languages,
nil,
)
assert.NoError(t, err)

expectedPeople := []db.Person{
fetchedPerson,
fetchedPerson2,
}

handler.ServeHTTP(rr, req)
Expand All @@ -537,7 +540,7 @@ func TestGetListedPeople(t *testing.T) {
err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.EqualValues(t, person2, fetchedPerson)
assert.EqualValues(t, person2, fetchedPerson2)
assert.EqualValues(t, expectedPeople, returnedPeople)
})

Expand Down

0 comments on commit e3e3769

Please sign in to comment.