Skip to content

Commit

Permalink
Merge pull request #1913 from stakwork/feat/user_stories_webhook
Browse files Browse the repository at this point in the history
PR: Added TestGetFeatureStories  Webhook
  • Loading branch information
humansinstitute authored Nov 6, 2024
2 parents 00d1ad1 + a238db2 commit 97a3578
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
15 changes: 15 additions & 0 deletions db/structsv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ type V2TagRes struct {
Status string `json:"status"` // "COMPLETE", "PENDING", or "FAILED"
Error string `json:"error"`
}

type FeatureStories struct {
UserStory string `json:"userStory"`
Rationale string `json:"rationale"`
Order uint `json:"order"`
}

type FeatureOutput struct {
FeatureUuid string `json:"featureUuid"`
Stories []FeatureStories `json:"stories"`
}

type FeatureStoriesReponse struct {
Output FeatureOutput `json:"feature_output"`
}
44 changes: 44 additions & 0 deletions handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"

"github.com/go-chi/chi"
"github.com/rs/xid"
Expand Down Expand Up @@ -404,3 +406,45 @@ func (oh *featureHandler) GetBountiesCountByFeatureAndPhaseUuid(w http.ResponseW
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountiesCount)
}

func (oh *featureHandler) GetFeatureStories(w http.ResponseWriter, r *http.Request) {
featureStories := db.FeatureStoriesReponse{}

decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&featureStories)

featureUuid := featureStories.Output.FeatureUuid

if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
fmt.Fprintf(w, "Error decoding request body: %v", err)
return
}

for _, story := range featureStories.Output.Stories {
// check if feature story exists
feature := oh.db.GetFeatureByUuid(featureUuid)

if feature.ID == 0 {
log.Println("Feature ID does not exists", featureUuid)
continue
}

now := time.Now()

// Add story to database
featureStory := db.FeatureStory{
Uuid: xid.New().String(),
Description: story.UserStory,
FeatureUuid: featureUuid,
Created: &now,
Updated: &now,
}

oh.db.CreateOrEditFeatureStory(featureStory)
log.Println("Created user story for : ", featureStory.FeatureUuid)
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("User stories added successfuly")
}
156 changes: 156 additions & 0 deletions handlers/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,3 +1385,159 @@ func TestGetBountiesCountByFeatureAndPhaseUuid(t *testing.T) {
assert.Equal(t, http.StatusOK, rr.Code)
})
}

func TestGetFeatureStories(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

fHandler := NewFeatureHandler(db.TestDB)

person := db.Person{
Uuid: uuid.New().String(),
OwnerAlias: "test-get-feature-stories-alias",
UniqueName: "test-get-feature-stories-unique-name",
OwnerPubKey: "test-get-feature-stories-pubkey",
PriceToMeet: 0,
Description: "test-get-feature-stories-description",
}
db.TestDB.CreateOrEditPerson(person)

workspace := db.Workspace{
Uuid: uuid.New().String(),
Name: "test-get-feature-stories-workspace-name",
OwnerPubKey: person.OwnerPubKey,
Github: "https://github.com/test",
Website: "https://www.testwebsite.com",
Description: "test-get-feature-stories-description",
}
db.TestDB.CreateOrEditWorkspace(workspace)
workspace = db.TestDB.GetWorkspaceByUuid(workspace.Uuid)

feature := db.WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
Name: "test-get-feature-stories-feature-name",
Url: "https://github.com/test-get-feature-stories-feature-url",
Priority: 0,
}

feature2 := db.WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
Name: "test-get-feature-stories-feature-name-2",
Url: "https://github.com/test-get-feature-stories-feature-url-2",
Priority: 0,
}

db.TestDB.CreateOrEditFeature(feature)
db.TestDB.CreateOrEditFeature(feature2)

ctx := context.WithValue(context.Background(), auth.ContextKey, workspace.OwnerPubKey)

story := db.FeatureStories{
UserStory: "This is a test user story",
Rationale: "This is a test rationale",
Order: 1,
}

story2 := db.FeatureStories{
UserStory: "This is a test user story 2",
Rationale: "This is a test rationale 2",
Order: 2,
}

story3 := db.FeatureStories{
UserStory: "This is a test user story 3",
Rationale: "This is a test rationale 3",
Order: 3,
}

story4 := db.FeatureStories{
UserStory: "This is a test user story 4",
Rationale: "This is a test rationale 4",
Order: 4,
}

story5 := db.FeatureStories{
UserStory: "This is a test user story 5",
Rationale: "This is a test rationale 5",
Order: 5,
}

story6 := db.FeatureStories{
UserStory: "This is a test user story 6",
Rationale: "This is a test rationale 6",
Order: 6,
}

stories := []db.FeatureStories{
story,
story2,
story3,
}

stories2 := []db.FeatureStories{
story4,
story5,
story6,
}

featureStories := db.FeatureStoriesReponse{
Output: db.FeatureOutput{
FeatureUuid: feature.Uuid,
Stories: stories,
},
}

featureStories2 := db.FeatureStoriesReponse{
Output: db.FeatureOutput{
FeatureUuid: "Fake-feature-uuid",
Stories: stories2,
},
}

requestBody, _ := json.Marshal(featureStories)
requestBody2, _ := json.Marshal(featureStories2)

t.Run("Should add user stories from stakwork to the feature stories table", func(t *testing.T) {
rctx := chi.NewRouteContext()
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodPost, "/features/stories", bytes.NewReader(requestBody))
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(fHandler.GetFeatureStories).ServeHTTP(rr, req)

var featureStoriesReponse string
err = json.Unmarshal(rr.Body.Bytes(), &featureStoriesReponse)
assert.NoError(t, err)

featureStories, _ := db.TestDB.GetFeatureStoriesByFeatureUuid(feature.Uuid)
featureStoriesCount := len(featureStories)

assert.Equal(t, int64(featureStoriesCount), int64(3))
assert.Equal(t, http.StatusOK, rr.Code)
})

t.Run("Should not add user stories from stakwork to the feature stories table if the feature uuid is not found", func(t *testing.T) {
rctx := chi.NewRouteContext()
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodPost, "/features/stories", bytes.NewReader(requestBody2))
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(fHandler.GetFeatureStories).ServeHTTP(rr, req)

var featureStoriesReponse string
err = json.Unmarshal(rr.Body.Bytes(), &featureStoriesReponse)
assert.NoError(t, err)

featureStories, _ := db.TestDB.GetFeatureStoriesByFeatureUuid(feature2.Uuid)
featureStoriesCount := len(featureStories)

assert.Equal(t, int64(featureStoriesCount), int64(0))
assert.Equal(t, http.StatusOK, rr.Code)
})
}
6 changes: 6 additions & 0 deletions routes/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
func FeatureRoutes() chi.Router {
r := chi.NewRouter()
featureHandlers := handlers.NewFeatureHandler(&db.DB)

r.Group(func(r chi.Router) {
r.Post("/stories", featureHandlers.GetFeatureStories)

})

r.Group(func(r chi.Router) {
r.Use(auth.PubKeyContext)

Expand Down

0 comments on commit 97a3578

Please sign in to comment.