diff --git a/pkg/sdk/go/policies.go b/pkg/sdk/go/policies.go index e7ad6111..30404623 100644 --- a/pkg/sdk/go/policies.go +++ b/pkg/sdk/go/policies.go @@ -21,6 +21,7 @@ type Policy struct { Subject string `json:"subject"` Object string `json:"object"` Actions []string `json:"actions"` + External bool `json:"external,omitempty"` // This is specificially used in things service. If set to true, it means the subject is userID otherwise it is thingID. CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } diff --git a/pkg/sdk/go/policies_test.go b/pkg/sdk/go/policies_test.go index 31d60571..86f87ac6 100644 --- a/pkg/sdk/go/policies_test.go +++ b/pkg/sdk/go/policies_test.go @@ -699,7 +699,7 @@ func TestDeletePolicy(t *testing.T) { repoCall1 = pRepo.On("RetrieveAll", mock.Anything, mock.Anything).Return(convertUserPolicyPage(sdk.PolicyPage{Policies: []sdk.Policy{cpr}}), nil) repoCall2 = pRepo.On("Delete", mock.Anything, mock.Anything).Return(sdk.ErrFailedRemoval) err = mfsdk.DeleteUserPolicy(pr, invalidToken) - assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.ErrAuthentication, http.StatusUnauthorized), fmt.Sprintf("expected %s got %s", pr, err)) + assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.ErrAuthentication, http.StatusUnauthorized), fmt.Sprintf("expected %v got %v", pr, err)) ok = repoCall.Parent.AssertCalled(t, "Delete", mock.Anything, mock.Anything) assert.True(t, ok, "Delete was not called on invalid policy") repoCall2.Unset() @@ -741,7 +741,7 @@ func TestUnassign(t *testing.T) { repoCall1 = pRepo.On("RetrieveAll", mock.Anything, mock.Anything).Return(convertUserPolicyPage(sdk.PolicyPage{Policies: []sdk.Policy{cpr}}), nil) repoCall2 = pRepo.On("Delete", mock.Anything, mock.Anything).Return(sdk.ErrFailedRemoval) err = mfsdk.Unassign(pr.Subject, pr.Object, invalidToken) - assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.ErrAuthentication, http.StatusUnauthorized), fmt.Sprintf("expected %s got %s", pr, err)) + assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.ErrAuthentication, http.StatusUnauthorized), fmt.Sprintf("expected %v got %v", pr, err)) ok = repoCall.Parent.AssertCalled(t, "Delete", mock.Anything, mock.Anything) assert.True(t, ok, "Delete was not called on invalid policy") repoCall2.Unset() @@ -1037,7 +1037,7 @@ func TestDisconnectThing(t *testing.T) { repoCall = pRepo.On("Delete", mock.Anything, mock.Anything).Return(sdk.ErrFailedRemoval) err = mfsdk.DisconnectThing(pr.Subject, pr.Object, invalidToken) - assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.ErrAuthorization, http.StatusUnauthorized), fmt.Sprintf("expected %s got %s", pr, err)) + assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.ErrAuthorization, http.StatusUnauthorized), fmt.Sprintf("expected %v got %v", pr, err)) ok = repoCall.Parent.AssertCalled(t, "Delete", mock.Anything, mock.Anything) assert.True(t, ok, "Delete was not called on invalid policy") repoCall.Unset() @@ -1076,7 +1076,7 @@ func TestDisconnect(t *testing.T) { repoCall = pRepo.On("Delete", mock.Anything, mock.Anything).Return(sdk.ErrFailedRemoval) conn = sdk.ConnectionIDs{ChannelIDs: []string{pr.Object}, ThingIDs: []string{pr.Subject}} err = mfsdk.Disconnect(conn, invalidToken) - assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.ErrAuthorization, http.StatusUnauthorized), fmt.Sprintf("expected %s got %s", pr, err)) + assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.ErrAuthorization, http.StatusUnauthorized), fmt.Sprintf("expected %v got %v", pr, err)) ok = repoCall.Parent.AssertCalled(t, "Delete", mock.Anything, mock.Anything) assert.True(t, ok, "Delete was not called on invalid policy") repoCall.Unset() diff --git a/pkg/sdk/go/things.go b/pkg/sdk/go/things.go index 0c1787da..b07622b4 100644 --- a/pkg/sdk/go/things.go +++ b/pkg/sdk/go/things.go @@ -252,11 +252,12 @@ func (sdk mfSDK) IdentifyThing(key string) (string, errors.SDKError) { } func (sdk mfSDK) ShareThing(groupID, userID string, actions []string, token string) errors.SDKError { - policy := ConnectionIDs{ - ChannelIDs: []string{groupID}, - ThingIDs: []string{userID}, - Actions: actions, + policy := Policy{ + Subject: userID, + Object: groupID, + Actions: actions, + External: true, } - return sdk.Connect(policy, token) + return sdk.CreateThingPolicy(policy, token) }