From af1a5ad4120bcf86e6bc605b45b15586a64df0cb Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Wed, 3 Jul 2024 05:36:30 +0500 Subject: [PATCH 1/6] Write Test For GetFeaturesByWorkspaceUuid --- handlers/features_test.go | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/handlers/features_test.go b/handlers/features_test.go index e39422594..863fa48b2 100644 --- a/handlers/features_test.go +++ b/handlers/features_test.go @@ -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") + }) } From 5f7604cf99a53ed26823a554728a4f756b4567ef Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Wed, 3 Jul 2024 05:59:32 +0500 Subject: [PATCH 2/6] fixed unexpected error in other unit test --- handlers/people_test.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/handlers/people_test.go b/handlers/people_test.go index 294d9f0b0..845056fd0 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -464,8 +464,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() @@ -476,8 +478,8 @@ func TestGetListedPeople(t *testing.T) { assert.NoError(t, err) expectedPeople := []db.Person{ - fetchedPerson, fetchedPerson2, + fetchedPerson3, } handler.ServeHTTP(rr, req) @@ -486,8 +488,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) }) @@ -500,7 +502,7 @@ func TestGetListedPeople(t *testing.T) { assert.NoError(t, err) expectedPeople := []db.Person{ - fetchedPerson, + fetchedPerson2, } handler.ServeHTTP(rr, req) @@ -509,7 +511,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) }) @@ -527,7 +529,7 @@ func TestGetListedPeople(t *testing.T) { assert.NoError(t, err) expectedPeople := []db.Person{ - fetchedPerson, + fetchedPerson2, } handler.ServeHTTP(rr, req) @@ -536,7 +538,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) }) From 00122a5606eceeb0974ef98bf9da75cb63252639 Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Wed, 3 Jul 2024 06:05:56 +0500 Subject: [PATCH 3/6] pushed with fixed error --- handlers/people_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/people_test.go b/handlers/people_test.go index 845056fd0..7d4c77a69 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -478,8 +478,8 @@ func TestGetListedPeople(t *testing.T) { assert.NoError(t, err) expectedPeople := []db.Person{ - fetchedPerson2, fetchedPerson3, + fetchedPerson2, } handler.ServeHTTP(rr, req) From 87944304f92ec83f16e504ba1c9508cb679aaa82 Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Wed, 3 Jul 2024 07:16:10 +0500 Subject: [PATCH 4/6] pushed with fixed error --- handlers/people_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/handlers/people_test.go b/handlers/people_test.go index 7d4c77a69..6100bc224 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -442,6 +442,7 @@ func TestGetListedPeople(t *testing.T) { PriceToMeet: 0, Description: "This is test user 2", Unlisted: false, + Deleted: false, Tags: pq.StringArray{}, GithubIssues: db.PropertyMap{}, Extras: db.PropertyMap{"coding_languages": "Golang"}, From 685f8fd4aba241cc7df4555e6c73f2a0dfc9fbbc Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Wed, 3 Jul 2024 12:06:07 +0500 Subject: [PATCH 5/6] unrelated unit test error fixed --- handlers/people_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/handlers/people_test.go b/handlers/people_test.go index 6100bc224..c13f6fe08 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "math/rand" "net/http" "net/http/httptest" "strconv" @@ -421,7 +422,7 @@ func TestGetListedPeople(t *testing.T) { db.CleanDB() person := db.Person{ - ID: 101, + ID: uint(rand.Intn(1000)), Uuid: "person_101_uuid", OwnerAlias: "person101", UniqueName: "person101", @@ -434,7 +435,7 @@ func TestGetListedPeople(t *testing.T) { Extras: db.PropertyMap{"coding_languages": "Typescript"}, } person2 := db.Person{ - ID: 102, + ID: uint(rand.Intn(1000)), Uuid: "person_102_uuid", OwnerAlias: "person102", UniqueName: "person102", @@ -442,13 +443,12 @@ func TestGetListedPeople(t *testing.T) { PriceToMeet: 0, Description: "This is test user 2", Unlisted: false, - Deleted: false, Tags: pq.StringArray{}, GithubIssues: db.PropertyMap{}, Extras: db.PropertyMap{"coding_languages": "Golang"}, } person3 := db.Person{ - ID: 103, + ID: uint(rand.Intn(1000)), Uuid: "person_103_uuid", OwnerAlias: "person103", UniqueName: "person103", @@ -524,13 +524,13 @@ func TestGetListedPeople(t *testing.T) { 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="+person3.Extras["coding_languages"].(string), nil, ) assert.NoError(t, err) expectedPeople := []db.Person{ - fetchedPerson2, + fetchedPerson3, } handler.ServeHTTP(rr, req) @@ -539,7 +539,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, fetchedPerson2) + assert.EqualValues(t, person3, fetchedPerson3) assert.EqualValues(t, expectedPeople, returnedPeople) }) From a588e18fdb2d47467aa24e0f2219949962df2d2f Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Wed, 3 Jul 2024 12:56:23 +0500 Subject: [PATCH 6/6] fixed --- handlers/people_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/handlers/people_test.go b/handlers/people_test.go index c13f6fe08..92f121c5e 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -479,8 +479,8 @@ func TestGetListedPeople(t *testing.T) { assert.NoError(t, err) expectedPeople := []db.Person{ - fetchedPerson3, fetchedPerson2, + fetchedPerson3, } handler.ServeHTTP(rr, req) @@ -521,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="+person3.Extras["coding_languages"].(string), + "page=1&limit=10&languages="+languages, nil, ) assert.NoError(t, err) expectedPeople := []db.Person{ - fetchedPerson3, + fetchedPerson2, } handler.ServeHTTP(rr, req) @@ -539,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, person3, fetchedPerson3) + assert.EqualValues(t, person2, fetchedPerson2) assert.EqualValues(t, expectedPeople, returnedPeople) })