From c754c95e78dfbba9298bba12cf6cec1589627c4e Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Thu, 4 Jul 2024 20:57:06 +0500 Subject: [PATCH 1/2] Write Test For DeleteStory --- handlers/features.go | 8 ++++ handlers/features_test.go | 79 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/handlers/features.go b/handlers/features.go index 8524dc0ac..54ca50858 100644 --- a/handlers/features.go +++ b/handlers/features.go @@ -343,6 +343,14 @@ func (oh *featureHandler) GetStoryByUuid(w http.ResponseWriter, r *http.Request) json.NewEncoder(w).Encode(story) } func (oh *featureHandler) DeleteStory(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) + if pubKeyFromAuth == "" { + fmt.Println("no pubkey from auth") + w.WriteHeader(http.StatusUnauthorized) + return + } + featureUuid := chi.URLParam(r, "feature_uuid") storyUuid := chi.URLParam(r, "story_uuid") diff --git a/handlers/features_test.go b/handlers/features_test.go index 65ebc5440..204bc0344 100644 --- a/handlers/features_test.go +++ b/handlers/features_test.go @@ -1111,6 +1111,85 @@ func TestGetStoryByUuid(t *testing.T) { func TestDeleteStory(t *testing.T) { + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + fHandler := NewFeatureHandler(db.TestDB) + + person := db.Person{ + Uuid: uuid.New().String(), + OwnerAlias: "test-alias", + UniqueName: "test-unique-name", + OwnerPubKey: "test-pubkey", + PriceToMeet: 0, + Description: "test-description", + } + db.TestDB.CreateOrEditPerson(person) + + workspace := db.Workspace{ + Uuid: uuid.New().String(), + Name: "test-workspace" + uuid.New().String(), + OwnerPubKey: person.OwnerPubKey, + Github: "https://github.com/test", + Website: "https://www.testwebsite.com", + Description: "test-description", + } + db.TestDB.CreateOrEditWorkspace(workspace) + workspace = db.TestDB.GetWorkspaceByUuid(workspace.Uuid) + + feature := db.WorkspaceFeatures{ + Uuid: uuid.New().String(), + WorkspaceUuid: workspace.Uuid, + Name: "test-feature", + Url: "https://github.com/test-feature", + Priority: 0, + } + db.TestDB.CreateOrEditFeature(feature) + + featureStory := db.FeatureStory{ + Uuid: uuid.New().String(), + FeatureUuid: feature.Uuid, + Description: "test-description", + Priority: 0, + } + db.TestDB.CreateOrEditFeatureStory(featureStory) + + t.Run("should return 401 error if not authorized", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(fHandler.DeleteStory) + + req, err := http.NewRequest(http.MethodDelete, "/"+feature.Uuid+"/story/"+featureStory.Uuid, nil) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code) + }) + + t.Run("should successfully add feature story if request is valid", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(fHandler.DeleteStory) + + ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey) + rctx := chi.NewRouteContext() + rctx.URLParams.Add("feature_uuid", feature.Uuid) + rctx.URLParams.Add("story_uuid", featureStory.Uuid) + req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodDelete, "/"+feature.Uuid+"/story/"+featureStory.Uuid, nil) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + deletedFeatureStory, _ := db.TestDB.GetFeatureStoryByUuid(feature.Uuid, featureStory.Uuid) + assert.Equal(t, db.FeatureStory{}, deletedFeatureStory) + + }) + } func TestGetBountiesByFeatureAndPhaseUuid(t *testing.T) { From 6cf7928e22a4ddecf7a67adec4c3c44020a8c4a0 Mon Sep 17 00:00:00 2001 From: elraphty Date: Thu, 4 Jul 2024 23:07:36 +0100 Subject: [PATCH 2/2] fixed merge conflicts --- handlers/features_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/features_test.go b/handlers/features_test.go index 204bc0344..96f118109 100644 --- a/handlers/features_test.go +++ b/handlers/features_test.go @@ -1154,7 +1154,7 @@ func TestDeleteStory(t *testing.T) { } db.TestDB.CreateOrEditFeatureStory(featureStory) - t.Run("should return 401 error if not authorized", func(t *testing.T) { + t.Run("should return 401 error if user not authorized", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(fHandler.DeleteStory) @@ -1168,7 +1168,7 @@ func TestDeleteStory(t *testing.T) { assert.Equal(t, http.StatusUnauthorized, rr.Code) }) - t.Run("should successfully add feature story if request is valid", func(t *testing.T) { + t.Run("should successfully delete feature story if request is valid", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(fHandler.DeleteStory)