Skip to content

Commit

Permalink
Merge pull request #2547 from aliraza556/unit-test-GetStoriesByFeatur…
Browse files Browse the repository at this point in the history
…eUuid

[Unit Test]: `GetStoriesByFeatureUuid`
  • Loading branch information
elraphty authored Feb 13, 2025
2 parents ffe2f63 + 898e4e5 commit a0f0fbf
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 13 deletions.
9 changes: 8 additions & 1 deletion handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/stakwork/sphinx-tribes/websocket"
"io"
"log"
"net/http"
"time"

"github.com/stakwork/sphinx-tribes/websocket"

"github.com/stakwork/sphinx-tribes/utils"

"os"
Expand Down Expand Up @@ -447,6 +448,12 @@ func (oh *featureHandler) GetStoriesByFeatureUuid(w http.ResponseWriter, r *http
}

featureUuid := chi.URLParam(r, "feature_uuid")
if featureUuid == "" {
logger.Log.Info("empty feature uuid")
w.WriteHeader(http.StatusBadRequest)
return
}

stories, err := oh.db.GetFeatureStoriesByFeatureUuid(featureUuid)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
135 changes: 123 additions & 12 deletions handlers/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,117 @@ func TestGetStoriesByFeatureUuid(t *testing.T) {
assert.Equal(t, returnedFeatureStory, updatedFeatureStory)
assert.Equal(t, http.StatusOK, rr.Code)
})

t.Run("Valid Request with No Stories", func(t *testing.T) {
emptyFeature := db.WorkspaceFeatures{
Uuid: "empty_feature_uuid",
WorkspaceUuid: workspace.Uuid,
Name: "empty_feature",
}
db.TestDB.CreateOrEditFeature(emptyFeature)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", emptyFeature.Uuid)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/"+emptyFeature.Uuid+"/story", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.GetStoriesByFeatureUuid).ServeHTTP(rr, req)

var returnedFeatureStory []db.FeatureStory
err = json.Unmarshal(rr.Body.Bytes(), &returnedFeatureStory)
assert.NoError(t, err)
assert.Empty(t, returnedFeatureStory)
assert.Equal(t, http.StatusOK, rr.Code)
})

t.Run("Empty Feature UUID", func(t *testing.T) {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", "")
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/features/''/story", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.GetStoriesByFeatureUuid).ServeHTTP(rr, req)

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

t.Run("Non-Existent Feature UUID", func(t *testing.T) {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", "non_existent_uuid")
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/non_existent_uuid/story", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.GetStoriesByFeatureUuid).ServeHTTP(rr, req)

var returnedFeatureStory []db.FeatureStory
err = json.Unmarshal(rr.Body.Bytes(), &returnedFeatureStory)
assert.NoError(t, err)
assert.Empty(t, returnedFeatureStory)
assert.Equal(t, http.StatusOK, rr.Code)
})

t.Run("Large Number of Stories", func(t *testing.T) {
largeFeature := db.WorkspaceFeatures{
Uuid: "large_feature_uuid",
WorkspaceUuid: workspace.Uuid,
Name: "large_feature",
}
db.TestDB.CreateOrEditFeature(largeFeature)

// Create 100 stories
for i := 0; i < 100; i++ {
story := db.FeatureStory{
Uuid: fmt.Sprintf("story_uuid_%d", i),
FeatureUuid: largeFeature.Uuid,
Description: fmt.Sprintf("Story %d", i),
Priority: i,
}
db.TestDB.CreateOrEditFeatureStory(story)
}

rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", largeFeature.Uuid)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/"+largeFeature.Uuid+"/story", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.GetStoriesByFeatureUuid).ServeHTTP(rr, req)

var returnedFeatureStory []db.FeatureStory
err = json.Unmarshal(rr.Body.Bytes(), &returnedFeatureStory)
assert.NoError(t, err)
assert.Equal(t, 100, len(returnedFeatureStory))
assert.Equal(t, http.StatusOK, rr.Code)
})

t.Run("Malformed Feature UUID", func(t *testing.T) {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", "invalid-uuid-format")
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/invalid-uuid-format/story", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.GetStoriesByFeatureUuid).ServeHTTP(rr, req)

var returnedFeatureStory []db.FeatureStory
err = json.Unmarshal(rr.Body.Bytes(), &returnedFeatureStory)
assert.NoError(t, err)
assert.Empty(t, returnedFeatureStory)
assert.Equal(t, http.StatusOK, rr.Code)
})
}

func TestGetStoryByUuid(t *testing.T) {
Expand Down Expand Up @@ -1919,7 +2030,7 @@ func TestGetStoryByUuid(t *testing.T) {

feature := db.WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
WorkspaceUuid: workspace.Uuid,
Name: "test-feature",
Url: "https://github.com/test-feature",
Priority: 0,
Expand Down Expand Up @@ -1977,7 +2088,7 @@ func TestGetStoryByUuid(t *testing.T) {
})

t.Run("Valid Request with Existing Story", func(t *testing.T) {
rr := httptest.NewRecorder()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fHandler.GetStoryByUuid)

rctx := chi.NewRouteContext()
Expand All @@ -2002,7 +2113,7 @@ func TestGetStoryByUuid(t *testing.T) {
handler := http.HandlerFunc(fHandler.GetStoryByUuid)

nonExistentUUID := uuid.New().String()
rctx := chi.NewRouteContext()
rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", feature.Uuid)
rctx.URLParams.Add("story_uuid", nonExistentUUID)
ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey)
Expand All @@ -2015,7 +2126,7 @@ func TestGetStoryByUuid(t *testing.T) {
})

t.Run("Valid Request with Empty UUIDs", func(t *testing.T) {
rr := httptest.NewRecorder()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fHandler.GetStoryByUuid)

rctx := chi.NewRouteContext()
Expand All @@ -2024,7 +2135,7 @@ func TestGetStoryByUuid(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx),
http.MethodGet, "/features//story/", nil)
assert.NoError(t, err)
assert.NoError(t, err)

handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotFound, rr.Code)
Expand All @@ -2049,7 +2160,7 @@ func TestGetStoryByUuid(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fHandler.GetStoryByUuid)

rctx := chi.NewRouteContext()
rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", "invalid-uuid")
rctx.URLParams.Add("story_uuid", "invalid-uuid")
ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey)
Expand All @@ -2062,7 +2173,7 @@ func TestGetStoryByUuid(t *testing.T) {
})

t.Run("Story with Special Characters", func(t *testing.T) {
rr := httptest.NewRecorder()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fHandler.GetStoryByUuid)

specialCharsUUID := "!@#$%^&*()"
Expand All @@ -2072,7 +2183,7 @@ func TestGetStoryByUuid(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx),
http.MethodGet, "/features/"+feature.Uuid+"/story/"+url.QueryEscape(specialCharsUUID), nil)
assert.NoError(t, err)
assert.NoError(t, err)

handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotFound, rr.Code)
Expand All @@ -2084,7 +2195,7 @@ func TestGetStoryByUuid(t *testing.T) {

upperFeatureUUID := strings.ToUpper(feature.Uuid)
upperStoryUUID := strings.ToUpper(featureStory.Uuid)
rctx := chi.NewRouteContext()
rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", upperFeatureUUID)
rctx.URLParams.Add("story_uuid", upperStoryUUID)
ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey)
Expand All @@ -2097,7 +2208,7 @@ func TestGetStoryByUuid(t *testing.T) {
})

t.Run("Request with Long UUIDs", func(t *testing.T) {
rr := httptest.NewRecorder()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fHandler.GetStoryByUuid)

longUUID := feature.Uuid + "extra"
Expand All @@ -2107,11 +2218,11 @@ func TestGetStoryByUuid(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, person.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx),
http.MethodGet, "/features/"+longUUID+"/story/"+longUUID, nil)
assert.NoError(t, err)
assert.NoError(t, err)

handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotFound, rr.Code)
})
})
}

func TestDeleteStory(t *testing.T) {
Expand Down

0 comments on commit a0f0fbf

Please sign in to comment.