Skip to content

Commit

Permalink
Merge pull request #76 from imulab/fix/72
Browse files Browse the repository at this point in the history
fix(pkg, cmd): Compiling a path to schema extension fields fails
  • Loading branch information
imulab authored Jun 29, 2020
2 parents b892ecc + b700c79 commit 14db9d1
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/api/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/imulab/go-scim/cmd/internal/groupsync"
scimmongo "github.com/imulab/go-scim/mongo/v2"
"github.com/imulab/go-scim/pkg/v2/crud"
"github.com/imulab/go-scim/pkg/v2/db"
"github.com/imulab/go-scim/pkg/v2/service"
"github.com/imulab/go-scim/pkg/v2/service/filter"
Expand Down Expand Up @@ -73,6 +74,7 @@ func (ctx *applicationContext) UserResourceType() *spec.ResourceType {
panic(err)
}
ctx.userResourceType = u
crud.Register(ctx.userResourceType)
ctx.logInitialized("user resource type")
}
return ctx.userResourceType
Expand All @@ -87,6 +89,7 @@ func (ctx *applicationContext) GroupResourceType() *spec.ResourceType {
panic(err)
}
ctx.groupResourceType = g
crud.Register(ctx.groupResourceType)
ctx.logInitialized("group resource type")
}
return ctx.groupResourceType
Expand Down
39 changes: 38 additions & 1 deletion pkg/v2/crud/crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ func (s *CrudTestSuite) TestAdd() {
}, r.Navigator().Dot("emails").Current().Raw())
},
},
{
name: "add to an extension schema field",
getResource: func(t *testing.T) *prop.Resource {
return prop.NewResource(s.resourceType)
},
path: "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber",
value: "6546579",
expect: func(t *testing.T, r *prop.Resource, err error) {
assert.Nil(t, err)
assert.Equal(t, "6546579", r.Navigator().Dot("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User").Dot("employeeNumber").Current().Raw())
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -460,6 +472,10 @@ func (s *CrudTestSuite) SetupSuite() {
require.Nil(s.T(), json.Unmarshal([]byte(testMainSchema), schema))
spec.Schemas().Register(schema)

schemaExtension := new(spec.Schema)
require.Nil(s.T(), json.Unmarshal([]byte(testSchemaExtension), schemaExtension))
spec.Schemas().Register(schemaExtension)

s.resourceType = new(spec.ResourceType)
require.Nil(s.T(), json.Unmarshal([]byte(testResourceType), s.resourceType))
Register(s.resourceType)
Expand Down Expand Up @@ -562,12 +578,33 @@ const (
}
]
}
`
testSchemaExtension = `
{
"id": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
"name": "Enterprise User",
"attributes": [
{
"id": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber",
"name": "employeeNumber",
"type": "string",
"_index": 100,
"_path": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber"
}
]
}
`
testResourceType = `
{
"id": "Test",
"name": "Test",
"schema": "main"
"schema": "main",
"schemaExtensions": [
{
"schema": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
"required": false
}
]
}
`
)
10 changes: 10 additions & 0 deletions pkg/v2/groupsync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package groupsync
import (
"context"
"encoding/json"
"github.com/imulab/go-scim/pkg/v2/crud"
"github.com/imulab/go-scim/pkg/v2/db"
"github.com/imulab/go-scim/pkg/v2/prop"
"github.com/imulab/go-scim/pkg/v2/spec"
Expand Down Expand Up @@ -136,6 +137,13 @@ func (s *SyncServiceTestSuite) SetupSuite() {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/schemas/user_enterprise_extension_schema.json",
structure: new(spec.Schema),
post: func(parsed interface{}) {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/schemas/group_schema.json",
structure: new(spec.Schema),
Expand All @@ -148,13 +156,15 @@ func (s *SyncServiceTestSuite) SetupSuite() {
structure: new(spec.ResourceType),
post: func(parsed interface{}) {
s.userResourceType = parsed.(*spec.ResourceType)
crud.Register(s.userResourceType)
},
},
{
filepath: "../../../public/resource_types/group_resource_type.json",
structure: new(spec.ResourceType),
post: func(parsed interface{}) {
s.groupResourceType = parsed.(*spec.ResourceType)
crud.Register(s.groupResourceType)
},
},
} {
Expand Down
19 changes: 18 additions & 1 deletion pkg/v2/json/adapt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package json

import (
"encoding/json"
"github.com/imulab/go-scim/pkg/v2/crud"
"github.com/imulab/go-scim/pkg/v2/spec"
"github.com/stretchr/testify/assert"
"os"
Expand All @@ -17,13 +18,23 @@ func TestResourceTypeToSerializable(t *testing.T) {
assert.Nil(t, err)
spec.Schemas().Register(sch)

f, err = os.Open("../../../public/schemas/user_enterprise_extension_schema.json")
assert.Nil(t, err)

schExt := new(spec.Schema)
err = json.NewDecoder(f).Decode(schExt)
assert.Nil(t, err)
spec.Schemas().Register(schExt)

f, err = os.Open("../../../public/resource_types/user_resource_type.json")
assert.Nil(t, err)

rt := new(spec.ResourceType)
err = json.NewDecoder(f).Decode(rt)
assert.Nil(t, err)

crud.Register(rt)

raw, err := Serialize(ResourceTypeToSerializable(rt))
assert.Nil(t, err)

Expand All @@ -39,7 +50,13 @@ func TestResourceTypeToSerializable(t *testing.T) {
},
"name": "User",
"endpoint": "/Users",
"schema": "urn:ietf:params:scim:schemas:core:2.0:User"
"schema": "urn:ietf:params:scim:schemas:core:2.0:User",
"schemaExtensions": [
{
"schema": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
"required": false
}
]
}
`
Expand Down
9 changes: 9 additions & 0 deletions pkg/v2/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"github.com/imulab/go-scim/pkg/v2/crud"
"github.com/imulab/go-scim/pkg/v2/db"
"github.com/imulab/go-scim/pkg/v2/prop"
"github.com/imulab/go-scim/pkg/v2/service/filter"
Expand Down Expand Up @@ -182,11 +183,19 @@ func (s *CreateServiceTestSuite) SetupSuite() {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/schemas/user_enterprise_extension_schema.json",
structure: new(spec.Schema),
post: func(parsed interface{}) {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/resource_types/user_resource_type.json",
structure: new(spec.ResourceType),
post: func(parsed interface{}) {
s.resourceType = parsed.(*spec.ResourceType)
crud.Register(s.resourceType)
},
},
} {
Expand Down
9 changes: 9 additions & 0 deletions pkg/v2/service/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"github.com/imulab/go-scim/pkg/v2/crud"
"github.com/imulab/go-scim/pkg/v2/db"
"github.com/imulab/go-scim/pkg/v2/prop"
"github.com/imulab/go-scim/pkg/v2/spec"
Expand Down Expand Up @@ -103,11 +104,19 @@ func (s *DeleteServiceTestSuite) SetupSuite() {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/schemas/user_enterprise_extension_schema.json",
structure: new(spec.Schema),
post: func(parsed interface{}) {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/resource_types/user_resource_type.json",
structure: new(spec.ResourceType),
post: func(parsed interface{}) {
s.resourceType = parsed.(*spec.ResourceType)
crud.Register(s.resourceType)
},
},
} {
Expand Down
9 changes: 9 additions & 0 deletions pkg/v2/service/filter/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filter
import (
"context"
"encoding/json"
"github.com/imulab/go-scim/pkg/v2/crud"
"github.com/imulab/go-scim/pkg/v2/prop"
"github.com/imulab/go-scim/pkg/v2/spec"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -186,11 +187,19 @@ func (s *MetaFilterTestSuite) SetupSuite() {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../../public/schemas/user_enterprise_extension_schema.json",
structure: new(spec.Schema),
post: func(parsed interface{}) {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../../public/resource_types/user_resource_type.json",
structure: new(spec.ResourceType),
post: func(parsed interface{}) {
s.resourceType = parsed.(*spec.ResourceType)
crud.Register(s.resourceType)
},
},
} {
Expand Down
9 changes: 9 additions & 0 deletions pkg/v2/service/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"github.com/imulab/go-scim/pkg/v2/crud"
"github.com/imulab/go-scim/pkg/v2/db"
"github.com/imulab/go-scim/pkg/v2/prop"
"github.com/imulab/go-scim/pkg/v2/spec"
Expand Down Expand Up @@ -104,11 +105,19 @@ func (s *GetServiceTestSuite) SetupSuite() {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/schemas/user_enterprise_extension_schema.json",
structure: new(spec.Schema),
post: func(parsed interface{}) {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/resource_types/user_resource_type.json",
structure: new(spec.ResourceType),
post: func(parsed interface{}) {
s.resourceType = parsed.(*spec.ResourceType)
crud.Register(s.resourceType)
},
},
} {
Expand Down
63 changes: 63 additions & 0 deletions pkg/v2/service/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"encoding/json"
"github.com/imulab/go-scim/pkg/v2/crud"
"github.com/imulab/go-scim/pkg/v2/db"
"github.com/imulab/go-scim/pkg/v2/prop"
"github.com/imulab/go-scim/pkg/v2/service/filter"
Expand Down Expand Up @@ -262,6 +263,60 @@ func (s *PatchServiceTestSuite) TestDo() {
))
},
},
{
name: "patch a field in the schema extension",
setup: func(t *testing.T) Patch {
database := db.Memory()
err := database.Insert(context.TODO(), s.resourceOf(t, map[string]interface{}{
"schemas": []interface{}{
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
},
"id": "foo",
"userName": "foo",
"emails": []interface{}{
map[string]interface{}{
"value": "[email protected]",
"type": "home",
},
},
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": map[string]interface{}{
"employeeNumber": "1234567",
},
}))
require.Nil(t, err)
return PatchService(s.config, database, nil, []filter.ByResource{
filter.ByPropertyToByResource(
filter.ReadOnlyFilter(),
filter.BCryptFilter(),
),
filter.ByPropertyToByResource(filter.ValidationFilter(database)),
filter.MetaFilter(),
})
},
getRequest: func() *PatchRequest {
return &PatchRequest{
ResourceID: "foo",
PayloadSource: strings.NewReader(`
{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
"Operations": [
{
"op": "add",
"path": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber",
"value": "6546579"
}
]
}
`),
}
},
expect: func(t *testing.T, resp *PatchResponse, err error) {
assert.Nil(t, err)
assert.True(t, resp.Patched)
assert.Equal(t, "6546579", resp.Resource.Navigator().Dot("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User").Dot("employeeNumber").Current().Raw())
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -299,11 +354,19 @@ func (s *PatchServiceTestSuite) SetupSuite() {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/schemas/user_enterprise_extension_schema.json",
structure: new(spec.Schema),
post: func(parsed interface{}) {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/resource_types/user_resource_type.json",
structure: new(spec.ResourceType),
post: func(parsed interface{}) {
s.resourceType = parsed.(*spec.ResourceType)
crud.Register(s.resourceType)
},
},
} {
Expand Down
8 changes: 8 additions & 0 deletions pkg/v2/service/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,19 @@ func (s *QueryServiceTestSuite) SetupSuite() {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/schemas/user_enterprise_extension_schema.json",
structure: new(spec.Schema),
post: func(parsed interface{}) {
spec.Schemas().Register(parsed.(*spec.Schema))
},
},
{
filepath: "../../../public/resource_types/user_resource_type.json",
structure: new(spec.ResourceType),
post: func(parsed interface{}) {
s.resourceType = parsed.(*spec.ResourceType)
crud.Register(s.resourceType)
},
},
} {
Expand Down
Loading

0 comments on commit 14db9d1

Please sign in to comment.