Skip to content

Commit

Permalink
Merge branch 'master' into write-unit-test-GetFeatureByUuid-
Browse files Browse the repository at this point in the history
  • Loading branch information
saithsab877 committed Jul 2, 2024
2 parents 0a10c69 + 3654c19 commit 7742502
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions handlers/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,80 @@ func TestCreateOrEditFeatures(t *testing.T) {
}

func TestDeleteFeature(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
oHandler := 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)
feature = db.TestDB.GetFeatureByUuid(feature.Uuid)

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

t.Run("should return error if not authorized", func(t *testing.T) {
featureUUID := feature.Uuid
rr := httptest.NewRecorder()
handler := http.HandlerFunc(oHandler.DeleteFeature)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", featureUUID)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodDelete, "/features/"+featureUUID, nil)
if err != nil {
t.Fatal(err)
}

handler.ServeHTTP(rr, req)

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

t.Run("should delete feature on successful delete", func(t *testing.T) {
featureUUID := feature.Uuid

rr := httptest.NewRecorder()
handler := http.HandlerFunc(oHandler.DeleteFeature)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", featureUUID)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodDelete, "/features/"+featureUUID, nil)
if err != nil {
t.Fatal(err)
}

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)

deletedFeature := db.TestDB.GetFeatureByUuid(featureUUID)
assert.Equal(t, db.WorkspaceFeatures{}, deletedFeature)
})
}

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

0 comments on commit 7742502

Please sign in to comment.