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..96f118109 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 user 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 delete 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) {