Skip to content
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 pkg/v2/service/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func (s *patchService) Do(ctx context.Context, req *PatchRequest) (resp *PatchRe
return
}

resource, err := s.database.Get(ctx, req.ResourceID, nil)
ref, err := s.database.Get(ctx, req.ResourceID, nil)
if err != nil {
return
}

if s.config.ETag.Supported && req.MatchCriteria != nil {
if !req.MatchCriteria(resource) {
if !req.MatchCriteria(ref) {
err = fmt.Errorf("%w: resource does not meet pre condition", spec.ErrConflict)
return
}
Expand All @@ -98,7 +98,7 @@ func (s *patchService) Do(ctx context.Context, req *PatchRequest) (resp *PatchRe
// To save another database round trip, we use Clone to retain independent copy of the fetched resource.
// However, because the cloned instance share subscribers, it is better to work on the original instance.
// Hence, we assign reference to the clone, which will not be modified.
ref := resource.Clone()
resource := ref.Clone()

for _, f := range s.preFilters {
if err = f.FilterRef(ctx, resource, ref); err != nil {
Expand Down
40 changes: 40 additions & 0 deletions pkg/v2/service/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func (s *PatchServiceTestSuite) TestDo() {
err := database.Insert(context.TODO(), s.resourceOf(t, map[string]interface{}{
"schemas": []interface{}{"urn:ietf:params:scim:schemas:core:2.0:User"},
"id": "foo",
"meta": map[string]interface{}{
"resourceType": "User",
"created": "2019-11-20T13:09:00",
"lastModified": "2019-11-20T13:09:00",
"location": "https://identity.imulab.io/Users/foo",
"version": "W/\"1\"",
},
"userName": "foo",
"timezone": "Asia/Shanghai",
"emails": []interface{}{
Expand Down Expand Up @@ -90,6 +97,7 @@ func (s *PatchServiceTestSuite) TestDo() {
},
expect: func(t *testing.T, resp *PatchResponse, err error) {
assert.Nil(t, err)
require.NotNil(t, resp)
assert.True(t, resp.Patched)
assert.NotEmpty(t, resp.Resource.MetaVersionOrEmpty())
assert.NotEqual(t, resp.Ref.MetaVersionOrEmpty(), resp.Resource.MetaVersionOrEmpty())
Expand All @@ -105,6 +113,13 @@ func (s *PatchServiceTestSuite) TestDo() {
err := database.Insert(context.TODO(), s.resourceOf(t, map[string]interface{}{
"schemas": []interface{}{"urn:ietf:params:scim:schemas:core:2.0:User"},
"id": "foo",
"meta": map[string]interface{}{
"resourceType": "User",
"created": "2019-11-20T13:09:00",
"lastModified": "2019-11-20T13:09:00",
"location": "https://identity.imulab.io/Users/foo",
"version": "W/\"1\"",
},
"userName": "foo",
"timezone": "Asia/Shanghai",
"emails": []interface{}{
Expand Down Expand Up @@ -143,6 +158,7 @@ func (s *PatchServiceTestSuite) TestDo() {
},
expect: func(t *testing.T, resp *PatchResponse, err error) {
assert.Nil(t, err)
require.NotNil(t, resp)
assert.False(t, resp.Patched)
},
},
Expand All @@ -153,6 +169,13 @@ func (s *PatchServiceTestSuite) TestDo() {
err := database.Insert(context.TODO(), s.resourceOf(t, map[string]interface{}{
"schemas": []interface{}{"urn:ietf:params:scim:schemas:core:2.0:User"},
"id": "foo",
"meta": map[string]interface{}{
"resourceType": "User",
"created": "2019-11-20T13:09:00",
"lastModified": "2019-11-20T13:09:00",
"location": "https://identity.imulab.io/Users/foo",
"version": "W/\"1\"",
},
"userName": "foo",
"timezone": "Asia/Shanghai",
"emails": []interface{}{
Expand Down Expand Up @@ -200,6 +223,7 @@ func (s *PatchServiceTestSuite) TestDo() {
},
expect: func(t *testing.T, resp *PatchResponse, err error) {
assert.Nil(t, err)
require.NotNil(t, resp)
assert.True(t, resp.Patched)
assert.NotEmpty(t, resp.Resource.MetaVersionOrEmpty())
assert.NotEqual(t, resp.Ref.MetaVersionOrEmpty(), resp.Resource.MetaVersionOrEmpty())
Expand All @@ -215,6 +239,13 @@ func (s *PatchServiceTestSuite) TestDo() {
err := database.Insert(context.TODO(), s.resourceOf(t, map[string]interface{}{
"schemas": []interface{}{"urn:ietf:params:scim:schemas:core:2.0:User"},
"id": "foo",
"meta": map[string]interface{}{
"resourceType": "User",
"created": "2019-11-20T13:09:00",
"lastModified": "2019-11-20T13:09:00",
"location": "https://identity.imulab.io/Users/foo",
"version": "W/\"1\"",
},
"userName": "foo",
"timezone": "Asia/Shanghai",
"emails": []interface{}{
Expand Down Expand Up @@ -255,6 +286,7 @@ func (s *PatchServiceTestSuite) TestDo() {
},
expect: func(t *testing.T, resp *PatchResponse, err error) {
assert.Nil(t, err)
require.NotNil(t, resp)
assert.True(t, resp.Patched)
assert.Equal(t, "bar", resp.Resource.Navigator().Dot("userName").Current().Raw())
assert.Nil(t, bcrypt.CompareHashAndPassword(
Expand All @@ -273,6 +305,13 @@ func (s *PatchServiceTestSuite) TestDo() {
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
},
"id": "foo",
"meta": map[string]interface{}{
"resourceType": "User",
"created": "2019-11-20T13:09:00",
"lastModified": "2019-11-20T13:09:00",
"location": "https://identity.imulab.io/Users/foo",
"version": "W/\"1\"",
},
"userName": "foo",
"emails": []interface{}{
map[string]interface{}{
Expand Down Expand Up @@ -313,6 +352,7 @@ func (s *PatchServiceTestSuite) TestDo() {
},
expect: func(t *testing.T, resp *PatchResponse, err error) {
assert.Nil(t, err)
require.NotNil(t, resp)
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())
},
Expand Down