Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Validate space name with a regexp in the controller (OSIO#3580) #2100

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions controller/comments_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,10 @@ func CreateSecuredSpace(t *testing.T, db application.DB, config SpaceConfigurati
spaceCtrl := NewSpaceController(svc, db, config, &DummyResourceManager{})
require.NotNil(t, spaceCtrl)
spacePayload := &app.CreateSpacePayload{
Data: &app.Space{
Data: &app.CreateSpace{
Type: "spaces",
Attributes: &app.SpaceAttributes{
Name: ptr.String("TestCollaborators-space-" + uuid.NewV4().String()),
Attributes: &app.CreateSpaceAttributes{
Name: fmt.Sprintf("TestCollaborators-space-%v", uuid.NewV4()),
Description: ptr.String("description"),
},
},
Expand Down
8 changes: 4 additions & 4 deletions controller/namedspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func (rest *TestNamedSpaceREST) TestSuccessQuerySpace() {
assert.Fail(t, "Failed to create an identity")
}

name := testsupport.CreateRandomValidTestName("Test 24")
name := testsupport.CreateRandomValidTestName("Test")

p := newCreateSpacePayload(&name, nil)
p := newCreateSpacePayload(name, nil)

_, created := test.CreateSpaceCreated(t, spaceSvc.Context, spaceSvc, spaceCtrl, p)
assert.NotNil(t, created.Data)
Expand Down Expand Up @@ -109,9 +109,9 @@ func (rest *TestNamedSpaceREST) TestSuccessListSpaces() {
assert.Fail(t, "Failed to create an identity")
}

name := testsupport.CreateRandomValidTestName("Test 24")
name := testsupport.CreateRandomValidTestName("Test")

p := newCreateSpacePayload(&name, nil)
p := newCreateSpacePayload(name, nil)

_, created := test.CreateSpaceCreated(t, spaceSvc.Context, spaceSvc, spaceCtrl, p)
assert.NotNil(t, created.Data)
Expand Down
14 changes: 7 additions & 7 deletions controller/search_spaces_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func (rest *TestSearchSpacesREST) UnSecuredController() (*goa.Service, *SearchCo

func (rest *TestSearchSpacesREST) TestSpacesSearchOK() {
// given
prefix := time.Now().Format("2006_Jan_2_15_04_05_") // using a unique prefix to make sure the test data will not collide with existing, older spaces.
prefix := time.Now().Format("2006-Jan-2-15-04-05-") // using a unique prefix to make sure the test data will not collide with existing, older spaces.
idents, err := createTestData(rest.db, prefix)
require.NoError(rest.T(), err)
tests := []okScenario{
{"With uppercase fullname query", args{offset("0"), limit(10), prefix + "TEST_AB"}, expects{totalCount(1)}},
{"With lowercase fullname query", args{offset("0"), limit(10), prefix + "TEST_AB"}, expects{totalCount(1)}},
{"With uppercase description query", args{offset("0"), limit(10), "DESCRIPTION FOR " + prefix + "TEST_AB"}, expects{totalCount(1)}},
{"With lowercase description query", args{offset("0"), limit(10), "description for " + prefix + "test_ab"}, expects{totalCount(1)}},
{"With uppercase fullname query", args{offset("0"), limit(10), prefix + "TEST-AB"}, expects{totalCount(1)}},
{"With lowercase fullname query", args{offset("0"), limit(10), prefix + "TEST-AB"}, expects{totalCount(1)}},
{"With uppercase description query", args{offset("0"), limit(10), "DESCRIPTION FOR " + prefix + "TEST-AB"}, expects{totalCount(1)}},
{"With lowercase description query", args{offset("0"), limit(10), "description for " + prefix + "TEST-ab"}, expects{totalCount(1)}},
{"with special chars", args{offset("0"), limit(10), "&:\n!#%?*"}, expects{totalCount(0)}},
{"with * to list all", args{offset("0"), limit(10), "*"}, expects{totalCountAtLeast(len(idents))}},
{"with multi page", args{offset("0"), limit(10), prefix + "TEST"}, expects{hasLinks("Next")}},
Expand All @@ -92,9 +92,9 @@ func (rest *TestSearchSpacesREST) TestSpacesSearchOK() {
}

func createTestData(db application.DB, prefix string) ([]space.Space, error) {
names := []string{prefix + "TEST_A", prefix + "TEST_AB", prefix + "TEST_B", prefix + "TEST_C"}
names := []string{prefix + "TEST-A", prefix + "TEST-AB", prefix + "TEST-B", prefix + "TEST-C"}
for i := 0; i < 20; i++ {
names = append(names, prefix+"TEST_"+strconv.Itoa(i))
names = append(names, prefix+"TEST-"+strconv.Itoa(i))
}

spaces := []space.Space{}
Expand Down
31 changes: 1 addition & 30 deletions controller/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,8 @@ func (c *SpaceController) Create(ctx *app.CreateSpaceContext) error {
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}

err = validateCreateSpace(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}

reqSpace := ctx.Payload.Data
spaceName := *reqSpace.Attributes.Name
spaceName := reqSpace.Attributes.Name
spaceID := uuid.NewV4()
if reqSpace.ID != nil {
spaceID = *reqSpace.ID
Expand Down Expand Up @@ -546,29 +540,6 @@ func (c *SpaceController) Update(ctx *app.UpdateSpaceContext) error {
return ctx.OK(&response)
}

func validateCreateSpace(ctx *app.CreateSpaceContext) error {
if ctx.Payload.Data == nil {
return errors.NewBadParameterError("data", nil).Expected("not nil")
}
if ctx.Payload.Data.Attributes == nil {
return errors.NewBadParameterError("data.attributes", nil).Expected("not nil")
}
if ctx.Payload.Data.Attributes.Name == nil {
return errors.NewBadParameterError("data.attributes.name", nil).Expected("not nil")
}
// // TODO(kwk): Comment back in once space template is official
// if ctx.Payload.Data.Relationships == nil {
// return errors.NewBadParameterError("data.relationships", nil).Expected("not nil")
// }
// if ctx.Payload.Data.Relationships.SpaceTemplate == nil {
// return errors.NewBadParameterError("data.relationships.spacetemplate", nil).Expected("not nil")
// }
// if ctx.Payload.Data.Relationships.SpaceTemplate.Data == nil {
// return errors.NewBadParameterError("data.relationships.spacetemplate.data", nil).Expected("not nil")
// }
return nil
}

func validateUpdateSpace(ctx *app.UpdateSpaceContext) error {
if ctx.Payload.Data == nil {
return errors.NewBadParameterError("data", nil).Expected("not nil")
Expand Down
Loading