Skip to content

Commit

Permalink
Merge pull request #1853 from MahtabBukhari/Write_Test_For_DeleteStory
Browse files Browse the repository at this point in the history
Write Test For DeleteStory
  • Loading branch information
elraphty committed Jul 5, 2024
2 parents 2752d7d + 6cf7928 commit 65cd05c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
8 changes: 8 additions & 0 deletions handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
79 changes: 79 additions & 0 deletions handlers/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 65cd05c

Please sign in to comment.