Skip to content

Commit

Permalink
Merge pull request #1837 from AbdulWahab3181/GetFeaturePhases-UT
Browse files Browse the repository at this point in the history
Write Test For GetFeaturePhases
  • Loading branch information
elraphty authored Jul 3, 2024
2 parents 4f91a5b + 4cf9529 commit 2bdaf86
Show file tree
Hide file tree
Showing 2 changed files with 93 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 @@ -200,6 +200,14 @@ func (oh *featureHandler) CreateOrEditFeaturePhase(w http.ResponseWriter, r *htt
}

func (oh *featureHandler) GetFeaturePhases(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")
phases := oh.db.GetPhasesByFeatureUuid(featureUuid)

Expand Down
85 changes: 85 additions & 0 deletions handlers/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/go-chi/chi"
"github.com/google/uuid"
Expand Down Expand Up @@ -443,7 +444,91 @@ func TestCreateOrEditFeaturePhase(t *testing.T) {
}

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

oHandler := NewFeatureHandler(db.TestDB)

person := db.Person{
Uuid: "uuid",
OwnerAlias: "alias",
UniqueName: "unique_name",
OwnerPubKey: "pubkey",
PriceToMeet: 0,
Description: "description",
}
db.TestDB.CreateOrEditPerson(person)

workspace := db.Workspace{
Uuid: "workspace_uuid",
Name: "workspace_name",
OwnerPubKey: "person.OwnerPubkey",
Github: "gtihub",
Website: "website",
Description: "description",
}
db.TestDB.CreateOrEditWorkspace(workspace)

feature := db.WorkspaceFeatures{
Uuid: "feature_uuid",
WorkspaceUuid: workspace.Uuid,
Name: "feature_name",
Url: "feature_url",
Priority: 0,
}
db.TestDB.CreateOrEditFeature(feature)

featurePhase := db.FeaturePhase{
Uuid: "feature_phase_uuid",
FeatureUuid: feature.Uuid,
Name: "feature_phase_name",
Priority: 0,
}
db.TestDB.CreateOrEditFeaturePhase(featurePhase)

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

t.Run("Should test that it throws a 401 error if a user is not authorized", func(t *testing.T) {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", feature.Uuid)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/features/"+feature.Uuid+"/phase", nil)
if err != nil {
t.Fatal(err)
}

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

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

t.Run("Should test that the workspace features phases array returned from the API has the feature phases created", func(t *testing.T) {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("feature_uuid", feature.Uuid)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/features/"+feature.Uuid+"/phase", nil)
if err != nil {
t.Fatal(err)
}

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

var returnedFeaturePhases []db.FeaturePhase
err = json.Unmarshal(rr.Body.Bytes(), &returnedFeaturePhases)
assert.NoError(t, err)

updatedFeaturePhases := db.TestDB.GetPhasesByFeatureUuid(feature.Uuid)

for i := range updatedFeaturePhases {
created := updatedFeaturePhases[i].Created.In(time.UTC)
updated := updatedFeaturePhases[i].Updated.In(time.UTC)
updatedFeaturePhases[i].Created = &created
updatedFeaturePhases[i].Updated = &updated
}

assert.Equal(t, returnedFeaturePhases, updatedFeaturePhases)
assert.Equal(t, http.StatusOK, rr.Code)
})
}

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

0 comments on commit 2bdaf86

Please sign in to comment.