MG-2216 - Rename delete policy function (#2218)

Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
This commit is contained in:
Steve Munene
2024-06-21 11:07:52 +03:00
committed by GitHub
parent ef1322be57
commit 424aa7cf80
30 changed files with 1021 additions and 853 deletions
+552 -397
View File
File diff suppressed because it is too large Load Diff
+16 -3
View File
@@ -23,7 +23,7 @@ service AuthService {
rpc Authorize(AuthorizeReq) returns (AuthorizeRes) {}
rpc AddPolicy(AddPolicyReq) returns (AddPolicyRes) {}
rpc AddPolicies(AddPoliciesReq) returns (AddPoliciesRes) {}
rpc DeletePolicy(DeletePolicyReq) returns (DeletePolicyRes) {}
rpc DeletePolicyFilter(DeletePolicyFilterReq) returns (DeletePolicyFilterRes) {}
rpc DeletePolicies(DeletePoliciesReq) returns (DeletePoliciesRes) {}
rpc ListObjects(ListObjectsReq) returns (ListObjectsRes) {}
rpc ListAllObjects(ListObjectsReq) returns (ListObjectsRes) {}
@@ -102,7 +102,7 @@ message AddPolicyRes { bool added = 1; }
message AddPoliciesRes { bool added = 1; }
message DeletePolicyReq {
message DeletePolicyFilterReq {
string domain = 1;
string subject_type = 2;
string subject_relation = 3;
@@ -115,11 +115,24 @@ message DeletePolicyReq {
string object_type = 10;
}
message DeletePolicyFilterRes { bool deleted = 1; }
message DeletePoliciesReq {
repeated DeletePolicyReq deletePoliciesReq = 1;
}
message DeletePolicyRes { bool deleted = 1; }
message DeletePolicyReq {
string domain = 1;
string subject_type = 2;
string subject_relation = 3;
string subject_kind = 4;
string subject = 5;
string relation = 6;
string permission = 7;
string object = 8;
string object_kind = 9;
string object_type = 10;
}
message DeletePoliciesRes { bool deleted = 1; }
+31 -31
View File
@@ -24,22 +24,22 @@ const svcName = "magistrala.AuthService"
var _ magistrala.AuthServiceClient = (*grpcClient)(nil)
type grpcClient struct {
issue endpoint.Endpoint
refresh endpoint.Endpoint
identify endpoint.Endpoint
authorize endpoint.Endpoint
addPolicy endpoint.Endpoint
addPolicies endpoint.Endpoint
deletePolicy endpoint.Endpoint
deletePolicies endpoint.Endpoint
listObjects endpoint.Endpoint
listAllObjects endpoint.Endpoint
countObjects endpoint.Endpoint
listSubjects endpoint.Endpoint
listAllSubjects endpoint.Endpoint
countSubjects endpoint.Endpoint
listPermissions endpoint.Endpoint
timeout time.Duration
issue endpoint.Endpoint
refresh endpoint.Endpoint
identify endpoint.Endpoint
authorize endpoint.Endpoint
addPolicy endpoint.Endpoint
addPolicies endpoint.Endpoint
deletePolicyFilter endpoint.Endpoint
deletePolicies endpoint.Endpoint
listObjects endpoint.Endpoint
listAllObjects endpoint.Endpoint
countObjects endpoint.Endpoint
listSubjects endpoint.Endpoint
listAllSubjects endpoint.Endpoint
countSubjects endpoint.Endpoint
listPermissions endpoint.Endpoint
timeout time.Duration
}
// NewClient returns new gRPC client instance.
@@ -93,13 +93,13 @@ func NewClient(conn *grpc.ClientConn, timeout time.Duration) magistrala.AuthServ
decodeAddPoliciesResponse,
magistrala.AddPoliciesRes{},
).Endpoint(),
deletePolicy: kitgrpc.NewClient(
deletePolicyFilter: kitgrpc.NewClient(
conn,
svcName,
"DeletePolicy",
encodeDeletePolicyRequest,
decodeDeletePolicyResponse,
magistrala.DeletePolicyRes{},
"DeletePolicyFilter",
encodeDeletePolicyFilterRequest,
decodeDeletePolicyFilterResponse,
magistrala.DeletePolicyFilterRes{},
).Endpoint(),
deletePolicies: kitgrpc.NewClient(
conn,
@@ -379,11 +379,11 @@ func encodeAddPoliciesRequest(_ context.Context, grpcReq interface{}) (interface
return &magistrala.AddPoliciesReq{AddPoliciesReq: addPolicies}, nil
}
func (client grpcClient) DeletePolicy(ctx context.Context, in *magistrala.DeletePolicyReq, opts ...grpc.CallOption) (*magistrala.DeletePolicyRes, error) {
func (client grpcClient) DeletePolicyFilter(ctx context.Context, in *magistrala.DeletePolicyFilterReq, opts ...grpc.CallOption) (*magistrala.DeletePolicyFilterRes, error) {
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
res, err := client.deletePolicy(ctx, policyReq{
res, err := client.deletePolicyFilter(ctx, policyReq{
Domain: in.GetDomain(),
SubjectType: in.GetSubjectType(),
SubjectKind: in.GetSubjectKind(),
@@ -395,21 +395,21 @@ func (client grpcClient) DeletePolicy(ctx context.Context, in *magistrala.Delete
Object: in.GetObject(),
})
if err != nil {
return &magistrala.DeletePolicyRes{}, decodeError(err)
return &magistrala.DeletePolicyFilterRes{}, decodeError(err)
}
dpr := res.(deletePolicyRes)
return &magistrala.DeletePolicyRes{Deleted: dpr.deleted}, nil
dpr := res.(deletePolicyFilterRes)
return &magistrala.DeletePolicyFilterRes{Deleted: dpr.deleted}, nil
}
func decodeDeletePolicyResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(*magistrala.DeletePolicyRes)
return deletePolicyRes{deleted: res.GetDeleted()}, nil
func decodeDeletePolicyFilterResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(*magistrala.DeletePolicyFilterRes)
return deletePolicyFilterRes{deleted: res.GetDeleted()}, nil
}
func encodeDeletePolicyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
func encodeDeletePolicyFilterRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(policyReq)
return &magistrala.DeletePolicyReq{
return &magistrala.DeletePolicyFilterReq{
Domain: req.Domain,
SubjectType: req.SubjectType,
SubjectKind: req.SubjectKind,
+5 -5
View File
@@ -151,14 +151,14 @@ func addPoliciesEndpoint(svc auth.Service) endpoint.Endpoint {
}
}
func deletePolicyEndpoint(svc auth.Service) endpoint.Endpoint {
func deletePolicyFilterEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(policyReq)
if err := req.validate(); err != nil {
return deletePolicyRes{}, err
return deletePolicyFilterRes{}, err
}
err := svc.DeletePolicy(ctx, auth.PolicyReq{
err := svc.DeletePolicyFilter(ctx, auth.PolicyReq{
Domain: req.Domain,
SubjectKind: req.SubjectKind,
SubjectType: req.SubjectType,
@@ -170,9 +170,9 @@ func deletePolicyEndpoint(svc auth.Service) endpoint.Endpoint {
Object: req.Object,
})
if err != nil {
return deletePolicyRes{}, err
return deletePolicyFilterRes{}, err
}
return deletePolicyRes{deleted: true}, nil
return deletePolicyFilterRes{deleted: true}, nil
}
}
+44 -44
View File
@@ -124,10 +124,10 @@ func TestIssue(t *testing.T) {
}
for _, tc := range cases {
repoCall := svc.On("Issue", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.issueResponse, tc.err)
svcCall := svc.On("Issue", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.issueResponse, tc.err)
_, err := client.Issue(context.Background(), &magistrala.IssueReq{UserId: tc.userId, DomainId: &tc.domainID, Type: uint32(tc.kind)})
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -170,10 +170,10 @@ func TestRefresh(t *testing.T) {
}
for _, tc := range cases {
repoCall := svc.On("Issue", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.issueResponse, tc.err)
svcCall := svc.On("Issue", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.issueResponse, tc.err)
_, err := client.Refresh(context.Background(), &magistrala.RefreshReq{DomainId: &tc.domainID, RefreshToken: tc.token})
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -211,13 +211,13 @@ func TestIdentify(t *testing.T) {
}
for _, tc := range cases {
repoCall := svc.On("Identify", mock.Anything, mock.Anything, mock.Anything).Return(auth.Key{Subject: id, User: email, Domain: domainID}, tc.svcErr)
svcCall := svc.On("Identify", mock.Anything, mock.Anything, mock.Anything).Return(auth.Key{Subject: id, User: email, Domain: domainID}, tc.svcErr)
idt, err := client.Identify(context.Background(), &magistrala.IdentityReq{Token: tc.token})
if idt != nil {
assert.Equal(t, tc.idt, idt, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.idt, idt))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -333,13 +333,13 @@ func TestAuthorize(t *testing.T) {
},
}
for _, tc := range cases {
repocall := svc.On("Authorize", mock.Anything, mock.Anything).Return(tc.err)
svccall := svc.On("Authorize", mock.Anything, mock.Anything).Return(tc.err)
ar, err := client.Authorize(context.Background(), tc.authRequest)
if ar != nil {
assert.Equal(t, tc.authResponse, ar, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.authResponse, ar))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repocall.Unset()
svccall.Unset()
}
}
@@ -387,13 +387,13 @@ func TestAddPolicy(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("AddPolicy", mock.Anything, mock.Anything).Return(tc.err)
svcCall := svc.On("AddPolicy", mock.Anything, mock.Anything).Return(tc.err)
apr, err := client.AddPolicy(context.Background(), tc.addPolicyReq)
if apr != nil {
assert.Equal(t, tc.addPolicyRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.addPolicyRes, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -449,17 +449,17 @@ func TestAddPolicies(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("AddPolicies", mock.Anything, mock.Anything).Return(tc.err)
svcCall := svc.On("AddPolicies", mock.Anything, mock.Anything).Return(tc.err)
apr, err := client.AddPolicies(context.Background(), tc.pr)
if apr != nil {
assert.Equal(t, tc.ar, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.ar, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
func TestDeletePolicy(t *testing.T) {
func TestDeletePolicyFilter(t *testing.T) {
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)
@@ -468,16 +468,16 @@ func TestDeletePolicy(t *testing.T) {
thingID := "thing"
cases := []struct {
desc string
token string
deletePolicyReq *magistrala.DeletePolicyReq
deletePolicyRes *magistrala.DeletePolicyRes
err error
desc string
token string
deletePolicyFilterReq *magistrala.DeletePolicyFilterReq
deletePolicyFilterRes *magistrala.DeletePolicyFilterRes
err error
}{
{
desc: "delete valid policy",
token: validToken,
deletePolicyReq: &magistrala.DeletePolicyReq{
deletePolicyFilterReq: &magistrala.DeletePolicyFilterReq{
Subject: id,
SubjectType: usersType,
Object: thingID,
@@ -485,13 +485,13 @@ func TestDeletePolicy(t *testing.T) {
Relation: readRelation,
Permission: readRelation,
},
deletePolicyRes: &magistrala.DeletePolicyRes{Deleted: true},
err: nil,
deletePolicyFilterRes: &magistrala.DeletePolicyFilterRes{Deleted: true},
err: nil,
},
{
desc: "delete invalid policy with invalid token",
token: inValidToken,
deletePolicyReq: &magistrala.DeletePolicyReq{
deletePolicyFilterReq: &magistrala.DeletePolicyFilterReq{
Subject: id,
SubjectType: usersType,
Object: thingID,
@@ -499,16 +499,16 @@ func TestDeletePolicy(t *testing.T) {
Relation: readRelation,
Permission: readRelation,
},
deletePolicyRes: &magistrala.DeletePolicyRes{Deleted: false},
err: svcerr.ErrAuthorization,
deletePolicyFilterRes: &magistrala.DeletePolicyFilterRes{Deleted: false},
err: svcerr.ErrAuthorization,
},
}
for _, tc := range cases {
repoCall := svc.On("DeletePolicy", mock.Anything, mock.Anything).Return(tc.err)
dpr, err := client.DeletePolicy(context.Background(), tc.deletePolicyReq)
assert.Equal(t, tc.deletePolicyRes.GetDeleted(), dpr.GetDeleted(), fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.deletePolicyRes.GetDeleted(), dpr.GetDeleted()))
svcCall := svc.On("DeletePolicyFilter", mock.Anything, mock.Anything).Return(tc.err)
dpr, err := client.DeletePolicyFilter(context.Background(), tc.deletePolicyFilterReq)
assert.Equal(t, tc.deletePolicyFilterRes.GetDeleted(), dpr.GetDeleted(), fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.deletePolicyFilterRes.GetDeleted(), dpr.GetDeleted()))
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -565,13 +565,13 @@ func TestDeletePolicies(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("DeletePolicies", mock.Anything, mock.Anything).Return(tc.err)
svcCall := svc.On("DeletePolicies", mock.Anything, mock.Anything).Return(tc.err)
apr, err := client.DeletePolicies(context.Background(), tc.deletePoliciesReq)
if apr != nil {
assert.Equal(t, tc.deletePoliciesRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.deletePoliciesRes, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -615,13 +615,13 @@ func TestListObjects(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("ListObjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.PolicyPage{Policies: tc.listObjectsRes.Policies}, tc.err)
svcCall := svc.On("ListObjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.PolicyPage{Policies: tc.listObjectsRes.Policies}, tc.err)
apr, err := client.ListObjects(context.Background(), tc.listObjectsReq)
if apr != nil {
assert.Equal(t, tc.listObjectsRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.listObjectsRes, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -665,13 +665,13 @@ func TestListAllObjects(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("ListAllObjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.PolicyPage{Policies: tc.listAllObjectsRes.Policies}, tc.err)
svcCall := svc.On("ListAllObjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.PolicyPage{Policies: tc.listAllObjectsRes.Policies}, tc.err)
apr, err := client.ListAllObjects(context.Background(), tc.listAllObjectsReq)
if apr != nil {
assert.Equal(t, tc.listAllObjectsRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.listAllObjectsRes, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -715,13 +715,13 @@ func TestCountObects(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("CountObjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.countObjectsRes.Count, tc.err)
svcCall := svc.On("CountObjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.countObjectsRes.Count, tc.err)
apr, err := client.CountObjects(context.Background(), tc.countObjectsReq)
if apr != nil {
assert.Equal(t, tc.countObjectsRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.countObjectsRes, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -765,13 +765,13 @@ func TestListSubjects(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("ListSubjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.PolicyPage{Policies: tc.listSubjectsRes.Policies}, tc.err)
svcCall := svc.On("ListSubjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.PolicyPage{Policies: tc.listSubjectsRes.Policies}, tc.err)
apr, err := client.ListSubjects(context.Background(), tc.listSubjectsReq)
if apr != nil {
assert.Equal(t, tc.listSubjectsRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.listSubjectsRes, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -815,13 +815,13 @@ func TestListAllSubjects(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("ListAllSubjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.PolicyPage{Policies: tc.listSubjectsRes.Policies}, tc.err)
svcCall := svc.On("ListAllSubjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.PolicyPage{Policies: tc.listSubjectsRes.Policies}, tc.err)
apr, err := client.ListAllSubjects(context.Background(), tc.listSubjectsReq)
if apr != nil {
assert.Equal(t, tc.listSubjectsRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.listSubjectsRes, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -868,7 +868,7 @@ func TestCountSubjects(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("CountSubjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.countSubjectsRes.Count, tc.err)
svcCall := svc.On("CountSubjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.countSubjectsRes.Count, tc.err)
apr, err := client.CountSubjects(context.Background(), tc.countSubjectsReq)
if apr != nil {
assert.Equal(t, tc.countSubjectsRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.countSubjectsRes, apr))
@@ -876,7 +876,7 @@ func TestCountSubjects(t *testing.T) {
e, ok := status.FromError(err)
assert.True(t, ok, "gRPC status can't be extracted from the error")
assert.Equal(t, tc.code, e.Code(), fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.code, e.Code()))
repoCall.Unset()
svcCall.Unset()
}
}
@@ -1001,12 +1001,12 @@ func TestListPermissions(t *testing.T) {
},
}
for _, tc := range cases {
repoCall := svc.On("ListPermissions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.Permissions{"view"}, tc.err)
svcCall := svc.On("ListPermissions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(auth.Permissions{"view"}, tc.err)
apr, err := client.ListPermissions(context.Background(), tc.listPermissionsReq)
if apr != nil {
assert.Equal(t, tc.listPermissionsRes, apr, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.listPermissionsRes, apr))
}
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
repoCall.Unset()
svcCall.Unset()
}
}
+1 -1
View File
@@ -27,7 +27,7 @@ type addPoliciesRes struct {
added bool
}
type deletePolicyRes struct {
type deletePolicyFilterRes struct {
deleted bool
}
+27 -27
View File
@@ -20,21 +20,21 @@ var _ magistrala.AuthServiceServer = (*grpcServer)(nil)
type grpcServer struct {
magistrala.UnimplementedAuthServiceServer
issue kitgrpc.Handler
refresh kitgrpc.Handler
identify kitgrpc.Handler
authorize kitgrpc.Handler
addPolicy kitgrpc.Handler
addPolicies kitgrpc.Handler
deletePolicy kitgrpc.Handler
deletePolicies kitgrpc.Handler
listObjects kitgrpc.Handler
listAllObjects kitgrpc.Handler
countObjects kitgrpc.Handler
listSubjects kitgrpc.Handler
listAllSubjects kitgrpc.Handler
countSubjects kitgrpc.Handler
listPermissions kitgrpc.Handler
issue kitgrpc.Handler
refresh kitgrpc.Handler
identify kitgrpc.Handler
authorize kitgrpc.Handler
addPolicy kitgrpc.Handler
addPolicies kitgrpc.Handler
deletePolicyFilter kitgrpc.Handler
deletePolicies kitgrpc.Handler
listObjects kitgrpc.Handler
listAllObjects kitgrpc.Handler
countObjects kitgrpc.Handler
listSubjects kitgrpc.Handler
listAllSubjects kitgrpc.Handler
countSubjects kitgrpc.Handler
listPermissions kitgrpc.Handler
}
// NewServer returns new AuthServiceServer instance.
@@ -70,10 +70,10 @@ func NewServer(svc auth.Service) magistrala.AuthServiceServer {
decodeAddPoliciesRequest,
encodeAddPoliciesResponse,
),
deletePolicy: kitgrpc.NewServer(
(deletePolicyEndpoint(svc)),
decodeDeletePolicyRequest,
encodeDeletePolicyResponse,
deletePolicyFilter: kitgrpc.NewServer(
(deletePolicyFilterEndpoint(svc)),
decodeDeletePolicyFilterRequest,
encodeDeletePolicyFilterResponse,
),
deletePolicies: kitgrpc.NewServer(
(deletePoliciesEndpoint(svc)),
@@ -166,12 +166,12 @@ func (s *grpcServer) AddPolicies(ctx context.Context, req *magistrala.AddPolicie
return res.(*magistrala.AddPoliciesRes), nil
}
func (s *grpcServer) DeletePolicy(ctx context.Context, req *magistrala.DeletePolicyReq) (*magistrala.DeletePolicyRes, error) {
_, res, err := s.deletePolicy.ServeGRPC(ctx, req)
func (s *grpcServer) DeletePolicyFilter(ctx context.Context, req *magistrala.DeletePolicyFilterReq) (*magistrala.DeletePolicyFilterRes, error) {
_, res, err := s.deletePolicyFilter.ServeGRPC(ctx, req)
if err != nil {
return nil, encodeError(err)
}
return res.(*magistrala.DeletePolicyRes), nil
return res.(*magistrala.DeletePolicyFilterRes), nil
}
func (s *grpcServer) DeletePolicies(ctx context.Context, req *magistrala.DeletePoliciesReq) (*magistrala.DeletePoliciesRes, error) {
@@ -335,8 +335,8 @@ func encodeAddPoliciesResponse(_ context.Context, grpcRes interface{}) (interfac
return &magistrala.AddPoliciesRes{Added: res.added}, nil
}
func decodeDeletePolicyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*magistrala.DeletePolicyReq)
func decodeDeletePolicyFilterRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*magistrala.DeletePolicyFilterReq)
return policyReq{
Domain: req.GetDomain(),
SubjectType: req.GetSubjectType(),
@@ -350,9 +350,9 @@ func decodeDeletePolicyRequest(_ context.Context, grpcReq interface{}) (interfac
}, nil
}
func encodeDeletePolicyResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(deletePolicyRes)
return &magistrala.DeletePolicyRes{Deleted: res.deleted}, nil
func encodeDeletePolicyFilterResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(deletePolicyFilterRes)
return &magistrala.DeletePolicyFilterRes{Deleted: res.deleted}, nil
}
func decodeDeletePoliciesRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
+4 -4
View File
@@ -293,7 +293,7 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR
return lm.svc.AddPolicies(ctx, prs)
}
func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq) (err error) {
func (lm *loggingMiddleware) DeletePolicyFilter(ctx context.Context, pr auth.PolicyReq) (err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
@@ -307,12 +307,12 @@ func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn("Delete policy failed to complete successfully", args...)
lm.logger.Warn("Delete policy filter failed to complete successfully", args...)
return
}
lm.logger.Info("Delete policy completed successfully", args...)
lm.logger.Info("Delete policy filter completed successfully", args...)
}(time.Now())
return lm.svc.DeletePolicy(ctx, pr)
return lm.svc.DeletePolicyFilter(ctx, pr)
}
func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) (err error) {
+4 -4
View File
@@ -152,12 +152,12 @@ func (ms *metricsMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR
return ms.svc.AddPolicies(ctx, prs)
}
func (ms *metricsMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq) error {
func (ms *metricsMiddleware) DeletePolicyFilter(ctx context.Context, pr auth.PolicyReq) error {
defer func(begin time.Time) {
ms.counter.With("method", "delete_policy").Add(1)
ms.latency.With("method", "delete_policy").Observe(time.Since(begin).Seconds())
ms.counter.With("method", "delete_policy_filter").Add(1)
ms.latency.With("method", "delete_policy_filter").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.DeletePolicy(ctx, pr)
return ms.svc.DeletePolicyFilter(ctx, pr)
}
func (ms *metricsMiddleware) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) error {
+3 -3
View File
@@ -89,12 +89,12 @@ func (_m *PolicyAgent) DeletePolicies(ctx context.Context, pr []auth.PolicyReq)
return r0
}
// DeletePolicy provides a mock function with given fields: ctx, pr
func (_m *PolicyAgent) DeletePolicy(ctx context.Context, pr auth.PolicyReq) error {
// DeletePolicyFilter provides a mock function with given fields: ctx, pr
func (_m *PolicyAgent) DeletePolicyFilter(ctx context.Context, pr auth.PolicyReq) error {
ret := _m.Called(ctx, pr)
if len(ret) == 0 {
panic("no return value specified for DeletePolicy")
panic("no return value specified for DeletePolicyFilter")
}
var r0 error
+2 -2
View File
@@ -71,10 +71,10 @@ func (m *AuthClient) AddPolicies(ctx context.Context, in *magistrala.AddPolicies
return ret.Get(0).(*magistrala.AddPoliciesRes), ret.Error(1)
}
func (m *AuthClient) DeletePolicy(ctx context.Context, in *magistrala.DeletePolicyReq, opts ...grpc.CallOption) (*magistrala.DeletePolicyRes, error) {
func (m *AuthClient) DeletePolicyFilter(ctx context.Context, in *magistrala.DeletePolicyFilterReq, opts ...grpc.CallOption) (*magistrala.DeletePolicyFilterRes, error) {
ret := m.Called(ctx, in)
return ret.Get(0).(*magistrala.DeletePolicyRes), ret.Error(1)
return ret.Get(0).(*magistrala.DeletePolicyFilterRes), ret.Error(1)
}
func (m *AuthClient) DeletePolicies(ctx context.Context, in *magistrala.DeletePoliciesReq, opts ...grpc.CallOption) (*magistrala.DeletePoliciesRes, error) {
+3 -3
View File
@@ -145,12 +145,12 @@ func (_m *Authz) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) error
return r0
}
// DeletePolicy provides a mock function with given fields: ctx, pr
func (_m *Authz) DeletePolicy(ctx context.Context, pr auth.PolicyReq) error {
// DeletePolicyFilter provides a mock function with given fields: ctx, pr
func (_m *Authz) DeletePolicyFilter(ctx context.Context, pr auth.PolicyReq) error {
ret := _m.Called(ctx, pr)
if len(ret) == 0 {
panic("no return value specified for DeletePolicy")
panic("no return value specified for DeletePolicyFilter")
}
var r0 error
+3 -3
View File
@@ -219,12 +219,12 @@ func (_m *Service) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) err
return r0
}
// DeletePolicy provides a mock function with given fields: ctx, pr
func (_m *Service) DeletePolicy(ctx context.Context, pr auth.PolicyReq) error {
// DeletePolicyFilter provides a mock function with given fields: ctx, pr
func (_m *Service) DeletePolicyFilter(ctx context.Context, pr auth.PolicyReq) error {
ret := _m.Called(ctx, pr)
if len(ret) == 0 {
panic("no return value specified for DeletePolicy")
panic("no return value specified for DeletePolicyFilter")
}
var r0 error
+5 -5
View File
@@ -143,8 +143,8 @@ type Authz interface {
// only allowed to use as an admin.
AddPolicies(ctx context.Context, prs []PolicyReq) error
// DeletePolicy removes a policy.
DeletePolicy(ctx context.Context, pr PolicyReq) error
// DeletePolicyFilter removes policy for given policy filter request.
DeletePolicyFilter(ctx context.Context, pr PolicyReq) error
// DeletePolicies deletes policies for given subjects. This method is
// only allowed to use as an admin.
@@ -191,10 +191,10 @@ type PolicyAgent interface {
// AddPolicies creates a Bulk Policies for the given request
AddPolicies(ctx context.Context, prs []PolicyReq) error
// DeletePolicy removes a policy.
DeletePolicy(ctx context.Context, pr PolicyReq) error
// DeletePolicyFilter removes policy for given policy filter request.
DeletePolicyFilter(ctx context.Context, pr PolicyReq) error
// DeletePolicy removes a policy.
// DeletePolicies removes a bulk policies for the given request.
DeletePolicies(ctx context.Context, pr []PolicyReq) error
// RetrieveObjects
+2 -2
View File
@@ -305,8 +305,8 @@ func (svc service) AddPolicies(ctx context.Context, prs []PolicyReq) error {
return svc.agent.AddPolicies(ctx, prs)
}
func (svc service) DeletePolicy(ctx context.Context, pr PolicyReq) error {
return svc.agent.DeletePolicy(ctx, pr)
func (svc service) DeletePolicyFilter(ctx context.Context, pr PolicyReq) error {
return svc.agent.DeletePolicyFilter(ctx, pr)
}
func (svc service) DeletePolicies(ctx context.Context, prs []PolicyReq) error {
+14 -14
View File
@@ -1336,8 +1336,8 @@ func TestDeletePolicy(t *testing.T) {
}
for _, tc := range cases {
repocall := prepo.On("DeletePolicy", mock.Anything, mock.Anything).Return(tc.err)
err := svc.DeletePolicy(context.Background(), tc.pr)
repocall := prepo.On("DeletePolicyFilter", context.Background(), mock.Anything).Return(tc.err)
err := svc.DeletePolicyFilter(context.Background(), tc.pr)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s expected %s got %s\n", tc.desc, tc.err, err))
repocall.Unset()
}
@@ -1398,7 +1398,7 @@ func TestDeletePolicies(t *testing.T) {
}
for _, tc := range cases {
repocall := prepo.On("DeletePolicies", mock.Anything, mock.Anything).Return(tc.err)
repocall := prepo.On("DeletePolicies", context.Background(), mock.Anything).Return(tc.err)
err := svc.DeletePolicies(context.Background(), tc.pr)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s expected %s got %s\n", tc.desc, tc.err, err))
repocall.Unset()
@@ -1448,7 +1448,7 @@ func TestListObjects(t *testing.T) {
},
}
for _, tc := range cases {
repocall2 := prepo.On("RetrieveObjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(expectedPolicies, mock.Anything, tc.err)
repocall2 := prepo.On("RetrieveObjects", context.Background(), mock.Anything, mock.Anything, mock.Anything).Return(expectedPolicies, mock.Anything, tc.err)
page, err := svc.ListObjects(context.Background(), tc.pr, tc.nextPageToken, tc.limit)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("listing policies expected to succeed: %s", err))
if err == nil {
@@ -1501,7 +1501,7 @@ func TestListAllObjects(t *testing.T) {
},
}
for _, tc := range cases {
repocall2 := prepo.On("RetrieveAllObjects", mock.Anything, mock.Anything).Return(expectedPolicies, tc.err)
repocall2 := prepo.On("RetrieveAllObjects", context.Background(), mock.Anything).Return(expectedPolicies, tc.err)
page, err := svc.ListAllObjects(context.Background(), tc.pr)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("listing policies expected to succeed: %s", err))
if err == nil {
@@ -1516,7 +1516,7 @@ func TestCountObjects(t *testing.T) {
pageLen := uint64(15)
repocall2 := prepo.On("RetrieveAllObjectsCount", mock.Anything, mock.Anything, mock.Anything).Return(pageLen, nil)
repocall2 := prepo.On("RetrieveAllObjectsCount", context.Background(), mock.Anything, mock.Anything).Return(pageLen, nil)
count, err := svc.CountObjects(context.Background(), auth.PolicyReq{Subject: id, SubjectType: auth.UserType, ObjectType: auth.ThingType, Permission: auth.ViewPermission})
assert.Nil(t, err, fmt.Sprintf("counting policies expected to succeed: %s", err))
assert.Equal(t, pageLen, count, fmt.Sprintf("unexpected listing page size, expected %d, got %d: %v", pageLen, count, err))
@@ -1566,13 +1566,13 @@ func TestListSubjects(t *testing.T) {
},
}
for _, tc := range cases {
repocall2 := prepo.On("RetrieveSubjects", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(expectedPolicies, mock.Anything, tc.err)
repocall := prepo.On("RetrieveSubjects", context.Background(), mock.Anything, mock.Anything, mock.Anything).Return(expectedPolicies, mock.Anything, tc.err)
page, err := svc.ListSubjects(context.Background(), tc.pr, tc.nextPageToken, tc.limit)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("listing policies expected to succeed: %s", err))
if err == nil {
assert.Equal(t, pageLen, len(page.Policies), fmt.Sprintf("unexpected listing page size, expected %d, got %d: %v", pageLen, len(page.Policies), err))
}
repocall2.Unset()
repocall.Unset()
}
}
@@ -1619,13 +1619,13 @@ func TestListAllSubjects(t *testing.T) {
},
}
for _, tc := range cases {
repocall2 := prepo.On("RetrieveAllSubjects", mock.Anything, mock.Anything).Return(expectedPolicies, tc.err)
repocall := prepo.On("RetrieveAllSubjects", context.Background(), mock.Anything).Return(expectedPolicies, tc.err)
page, err := svc.ListAllSubjects(context.Background(), tc.pr)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("listing policies expected to succeed: %s", err))
if err == nil {
assert.Equal(t, pageLen, len(page.Policies), fmt.Sprintf("unexpected listing page size, expected %d, got %d: %v", pageLen, len(page.Policies), err))
}
repocall2.Unset()
repocall.Unset()
}
}
@@ -1633,11 +1633,11 @@ func TestCountSubjects(t *testing.T) {
svc, _ := newService()
pageLen := uint64(15)
repocall2 := prepo.On("RetrieveAllSubjectsCount", mock.Anything, mock.Anything, mock.Anything).Return(pageLen, nil)
repocall := prepo.On("RetrieveAllSubjectsCount", mock.Anything, mock.Anything, mock.Anything).Return(pageLen, nil)
count, err := svc.CountSubjects(context.Background(), auth.PolicyReq{Object: id, ObjectType: auth.ThingType, Permission: auth.ViewPermission})
assert.Nil(t, err, fmt.Sprintf("counting policies expected to succeed: %s", err))
assert.Equal(t, pageLen, count, fmt.Sprintf("unexpected listing page size, expected %d, got %d: %v", pageLen, count, err))
repocall2.Unset()
repocall.Unset()
}
func TestListPermissions(t *testing.T) {
@@ -1653,10 +1653,10 @@ func TestListPermissions(t *testing.T) {
}
filterPermisions := []string{auth.ViewPermission, auth.AdminPermission}
repoCall1 := prepo.On("RetrievePermissions", mock.Anything, pr, filterPermisions).Return(auth.Permissions{}, nil)
repoCall := prepo.On("RetrievePermissions", context.Background(), pr, filterPermisions).Return(auth.Permissions{}, nil)
_, err := svc.ListPermissions(context.Background(), pr, filterPermisions)
assert.Nil(t, err, fmt.Sprintf("listing policies expected to succeed: %s", err))
repoCall1.Unset()
repoCall.Unset()
}
func TestSwitchToPermission(t *testing.T) {
+1 -1
View File
@@ -153,7 +153,7 @@ func (pa *policyAgent) DeletePolicies(ctx context.Context, prs []auth.PolicyReq)
return nil
}
func (pa *policyAgent) DeletePolicy(ctx context.Context, pr auth.PolicyReq) error {
func (pa *policyAgent) DeletePolicyFilter(ctx context.Context, pr auth.PolicyReq) error {
req := &v1.DeleteRelationshipsRequest{
RelationshipFilter: &v1.RelationshipFilter{
ResourceType: pr.ObjectType,
+2 -2
View File
@@ -96,7 +96,7 @@ func (tm *tracingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR
return tm.svc.AddPolicies(ctx, prs)
}
func (tm *tracingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq) error {
func (tm *tracingMiddleware) DeletePolicyFilter(ctx context.Context, pr auth.PolicyReq) error {
ctx, span := tm.tracer.Start(ctx, "delete_policy", trace.WithAttributes(
attribute.String("subject", pr.Subject),
attribute.String("subject_type", pr.SubjectType),
@@ -108,7 +108,7 @@ func (tm *tracingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq
))
defer span.End()
return tm.svc.DeletePolicy(ctx, pr)
return tm.svc.DeletePolicyFilter(ctx, pr)
}
func (tm *tracingMiddleware) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) error {
+29 -29
View File
@@ -116,21 +116,21 @@ var AuthzService_ServiceDesc = grpc.ServiceDesc{
}
const (
AuthService_Issue_FullMethodName = "/magistrala.AuthService/Issue"
AuthService_Refresh_FullMethodName = "/magistrala.AuthService/Refresh"
AuthService_Identify_FullMethodName = "/magistrala.AuthService/Identify"
AuthService_Authorize_FullMethodName = "/magistrala.AuthService/Authorize"
AuthService_AddPolicy_FullMethodName = "/magistrala.AuthService/AddPolicy"
AuthService_AddPolicies_FullMethodName = "/magistrala.AuthService/AddPolicies"
AuthService_DeletePolicy_FullMethodName = "/magistrala.AuthService/DeletePolicy"
AuthService_DeletePolicies_FullMethodName = "/magistrala.AuthService/DeletePolicies"
AuthService_ListObjects_FullMethodName = "/magistrala.AuthService/ListObjects"
AuthService_ListAllObjects_FullMethodName = "/magistrala.AuthService/ListAllObjects"
AuthService_CountObjects_FullMethodName = "/magistrala.AuthService/CountObjects"
AuthService_ListSubjects_FullMethodName = "/magistrala.AuthService/ListSubjects"
AuthService_ListAllSubjects_FullMethodName = "/magistrala.AuthService/ListAllSubjects"
AuthService_CountSubjects_FullMethodName = "/magistrala.AuthService/CountSubjects"
AuthService_ListPermissions_FullMethodName = "/magistrala.AuthService/ListPermissions"
AuthService_Issue_FullMethodName = "/magistrala.AuthService/Issue"
AuthService_Refresh_FullMethodName = "/magistrala.AuthService/Refresh"
AuthService_Identify_FullMethodName = "/magistrala.AuthService/Identify"
AuthService_Authorize_FullMethodName = "/magistrala.AuthService/Authorize"
AuthService_AddPolicy_FullMethodName = "/magistrala.AuthService/AddPolicy"
AuthService_AddPolicies_FullMethodName = "/magistrala.AuthService/AddPolicies"
AuthService_DeletePolicyFilter_FullMethodName = "/magistrala.AuthService/DeletePolicyFilter"
AuthService_DeletePolicies_FullMethodName = "/magistrala.AuthService/DeletePolicies"
AuthService_ListObjects_FullMethodName = "/magistrala.AuthService/ListObjects"
AuthService_ListAllObjects_FullMethodName = "/magistrala.AuthService/ListAllObjects"
AuthService_CountObjects_FullMethodName = "/magistrala.AuthService/CountObjects"
AuthService_ListSubjects_FullMethodName = "/magistrala.AuthService/ListSubjects"
AuthService_ListAllSubjects_FullMethodName = "/magistrala.AuthService/ListAllSubjects"
AuthService_CountSubjects_FullMethodName = "/magistrala.AuthService/CountSubjects"
AuthService_ListPermissions_FullMethodName = "/magistrala.AuthService/ListPermissions"
)
// AuthServiceClient is the client API for AuthService service.
@@ -143,7 +143,7 @@ type AuthServiceClient interface {
Authorize(ctx context.Context, in *AuthorizeReq, opts ...grpc.CallOption) (*AuthorizeRes, error)
AddPolicy(ctx context.Context, in *AddPolicyReq, opts ...grpc.CallOption) (*AddPolicyRes, error)
AddPolicies(ctx context.Context, in *AddPoliciesReq, opts ...grpc.CallOption) (*AddPoliciesRes, error)
DeletePolicy(ctx context.Context, in *DeletePolicyReq, opts ...grpc.CallOption) (*DeletePolicyRes, error)
DeletePolicyFilter(ctx context.Context, in *DeletePolicyFilterReq, opts ...grpc.CallOption) (*DeletePolicyFilterRes, error)
DeletePolicies(ctx context.Context, in *DeletePoliciesReq, opts ...grpc.CallOption) (*DeletePoliciesRes, error)
ListObjects(ctx context.Context, in *ListObjectsReq, opts ...grpc.CallOption) (*ListObjectsRes, error)
ListAllObjects(ctx context.Context, in *ListObjectsReq, opts ...grpc.CallOption) (*ListObjectsRes, error)
@@ -216,9 +216,9 @@ func (c *authServiceClient) AddPolicies(ctx context.Context, in *AddPoliciesReq,
return out, nil
}
func (c *authServiceClient) DeletePolicy(ctx context.Context, in *DeletePolicyReq, opts ...grpc.CallOption) (*DeletePolicyRes, error) {
out := new(DeletePolicyRes)
err := c.cc.Invoke(ctx, AuthService_DeletePolicy_FullMethodName, in, out, opts...)
func (c *authServiceClient) DeletePolicyFilter(ctx context.Context, in *DeletePolicyFilterReq, opts ...grpc.CallOption) (*DeletePolicyFilterRes, error) {
out := new(DeletePolicyFilterRes)
err := c.cc.Invoke(ctx, AuthService_DeletePolicyFilter_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -307,7 +307,7 @@ type AuthServiceServer interface {
Authorize(context.Context, *AuthorizeReq) (*AuthorizeRes, error)
AddPolicy(context.Context, *AddPolicyReq) (*AddPolicyRes, error)
AddPolicies(context.Context, *AddPoliciesReq) (*AddPoliciesRes, error)
DeletePolicy(context.Context, *DeletePolicyReq) (*DeletePolicyRes, error)
DeletePolicyFilter(context.Context, *DeletePolicyFilterReq) (*DeletePolicyFilterRes, error)
DeletePolicies(context.Context, *DeletePoliciesReq) (*DeletePoliciesRes, error)
ListObjects(context.Context, *ListObjectsReq) (*ListObjectsRes, error)
ListAllObjects(context.Context, *ListObjectsReq) (*ListObjectsRes, error)
@@ -341,8 +341,8 @@ func (UnimplementedAuthServiceServer) AddPolicy(context.Context, *AddPolicyReq)
func (UnimplementedAuthServiceServer) AddPolicies(context.Context, *AddPoliciesReq) (*AddPoliciesRes, error) {
return nil, status.Errorf(codes.Unimplemented, "method AddPolicies not implemented")
}
func (UnimplementedAuthServiceServer) DeletePolicy(context.Context, *DeletePolicyReq) (*DeletePolicyRes, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeletePolicy not implemented")
func (UnimplementedAuthServiceServer) DeletePolicyFilter(context.Context, *DeletePolicyFilterReq) (*DeletePolicyFilterRes, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeletePolicyFilter not implemented")
}
func (UnimplementedAuthServiceServer) DeletePolicies(context.Context, *DeletePoliciesReq) (*DeletePoliciesRes, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeletePolicies not implemented")
@@ -489,20 +489,20 @@ func _AuthService_AddPolicies_Handler(srv interface{}, ctx context.Context, dec
return interceptor(ctx, in, info, handler)
}
func _AuthService_DeletePolicy_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DeletePolicyReq)
func _AuthService_DeletePolicyFilter_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DeletePolicyFilterReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(AuthServiceServer).DeletePolicy(ctx, in)
return srv.(AuthServiceServer).DeletePolicyFilter(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: AuthService_DeletePolicy_FullMethodName,
FullMethod: AuthService_DeletePolicyFilter_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(AuthServiceServer).DeletePolicy(ctx, req.(*DeletePolicyReq))
return srv.(AuthServiceServer).DeletePolicyFilter(ctx, req.(*DeletePolicyFilterReq))
}
return interceptor(ctx, in, info, handler)
}
@@ -683,8 +683,8 @@ var AuthService_ServiceDesc = grpc.ServiceDesc{
Handler: _AuthService_AddPolicies_Handler,
},
{
MethodName: "DeletePolicy",
Handler: _AuthService_DeletePolicy_Handler,
MethodName: "DeletePolicyFilter",
Handler: _AuthService_DeletePolicyFilter_Handler,
},
{
MethodName: "DeletePolicies",
+2 -2
View File
@@ -179,7 +179,7 @@ var domainAssignCmds = []cobra.Command{
Short: "Assign users",
Long: "Assign users to a domain\n" +
"Usage:\n" +
"\tmagistrala-cli groups assign users <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <domain_id> $TOKEN\n",
"\tmagistrala-cli domains assign users <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <domain_id> $TOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 4 {
logUsage(cmd.Use)
@@ -205,7 +205,7 @@ var domainUnassignCmds = []cobra.Command{
Short: "Unassign users",
Long: "Unassign users from a domain\n" +
"Usage:\n" +
"\tmagistrala-cli groups unassign users <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <domain_id> $TOKEN\n",
"\tmagistrala-cli domains unassign users <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <domain_id> $TOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 4 {
logUsage(cmd.Use)
+4 -4
View File
@@ -591,7 +591,7 @@ func (svc service) DeleteGroup(ctx context.Context, token, groupID string) error
}
// Remove policy of child groups
if _, err := svc.auth.DeletePolicy(ctx, &magistrala.DeletePolicyReq{
if _, err := svc.auth.DeletePolicyFilter(ctx, &magistrala.DeletePolicyFilterReq{
SubjectType: auth.GroupType,
Subject: groupID,
ObjectType: auth.GroupType,
@@ -600,7 +600,7 @@ func (svc service) DeleteGroup(ctx context.Context, token, groupID string) error
}
// Remove policy of things
if _, err := svc.auth.DeletePolicy(ctx, &magistrala.DeletePolicyReq{
if _, err := svc.auth.DeletePolicyFilter(ctx, &magistrala.DeletePolicyFilterReq{
SubjectType: auth.GroupType,
Subject: groupID,
ObjectType: auth.ThingType,
@@ -609,7 +609,7 @@ func (svc service) DeleteGroup(ctx context.Context, token, groupID string) error
}
// Remove policy from domain
if _, err := svc.auth.DeletePolicy(ctx, &magistrala.DeletePolicyReq{
if _, err := svc.auth.DeletePolicyFilter(ctx, &magistrala.DeletePolicyFilterReq{
SubjectType: auth.DomainType,
Object: groupID,
ObjectType: auth.GroupType,
@@ -623,7 +623,7 @@ func (svc service) DeleteGroup(ctx context.Context, token, groupID string) error
}
// Remove policy of users
if _, err := svc.auth.DeletePolicy(ctx, &magistrala.DeletePolicyReq{
if _, err := svc.auth.DeletePolicyFilter(ctx, &magistrala.DeletePolicyFilterReq{
SubjectType: auth.UserType,
Object: groupID,
ObjectType: auth.GroupType,
+100 -100
View File
@@ -287,8 +287,8 @@ func TestCreateGroup(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
authCall := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
authcall1 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
SubjectType: auth.UserType,
SubjectKind: auth.UsersKind,
Subject: tc.idResp.GetId(),
@@ -296,7 +296,7 @@ func TestCreateGroup(t *testing.T) {
Object: tc.idResp.GetDomainId(),
ObjectType: auth.DomainType,
}).Return(tc.authzResp, tc.authzErr)
authCall1 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall2 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
SubjectType: auth.UserType,
SubjectKind: auth.TokenKind,
Subject: tc.token,
@@ -304,9 +304,9 @@ func TestCreateGroup(t *testing.T) {
Object: tc.group.Parent,
ObjectType: auth.GroupType,
}).Return(tc.authzTknResp, tc.authzTknErr)
repocall1 := repo.On("Save", context.Background(), mock.Anything).Return(tc.repoResp, tc.repoErr)
authCall2 := authsvc.On("AddPolicies", context.Background(), mock.Anything).Return(tc.addPolResp, tc.addPolErr)
authCall3 := authsvc.On("DeletePolicies", mock.Anything, mock.Anything).Return(tc.deletePolResp, tc.deletePolErr)
repocall := repo.On("Save", context.Background(), mock.Anything).Return(tc.repoResp, tc.repoErr)
authcall3 := authsvc.On("AddPolicies", context.Background(), mock.Anything).Return(tc.addPolResp, tc.addPolErr)
authCall4 := authsvc.On("DeletePolicies", mock.Anything, mock.Anything).Return(tc.deletePolResp, tc.deletePolErr)
got, err := svc.CreateGroup(context.Background(), tc.token, tc.kind, tc.group)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("expected error %v to contain %v", err, tc.err))
if err == nil {
@@ -314,15 +314,15 @@ func TestCreateGroup(t *testing.T) {
assert.NotEmpty(t, got.CreatedAt)
assert.NotEmpty(t, got.Domain)
assert.WithinDuration(t, time.Now(), got.CreatedAt, 2*time.Second)
ok := repocall1.Parent.AssertCalled(t, "Save", context.Background(), mock.Anything)
ok := repocall.Parent.AssertCalled(t, "Save", context.Background(), mock.Anything)
assert.True(t, ok, fmt.Sprintf("Save was not called on %s", tc.desc))
}
authcall.Unset()
authcall1.Unset()
authcall2.Unset()
repocall.Unset()
authCall.Unset()
authCall1.Unset()
repocall1.Unset()
authCall2.Unset()
authCall3.Unset()
authcall3.Unset()
authCall4.Unset()
})
}
}
@@ -461,8 +461,8 @@ func TestViewGroupPerms(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
repocall1 := authsvc.On("ListPermissions", context.Background(), &magistrala.ListPermissionsReq{
authcall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
authcall1 := authsvc.On("ListPermissions", context.Background(), &magistrala.ListPermissionsReq{
SubjectType: auth.UserType,
Subject: tc.idResp.GetId(),
Object: tc.id,
@@ -473,8 +473,8 @@ func TestViewGroupPerms(t *testing.T) {
if err == nil {
assert.Equal(t, tc.listResp.Permissions, got)
}
repocall.Unset()
repocall1.Unset()
authcall.Unset()
authcall1.Unset()
})
}
}
@@ -632,7 +632,7 @@ func TestEnableGroup(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
SubjectType: auth.UserType,
SubjectKind: auth.TokenKind,
Subject: tc.token,
@@ -640,8 +640,8 @@ func TestEnableGroup(t *testing.T) {
Object: tc.id,
ObjectType: auth.GroupType,
}).Return(tc.authzResp, tc.authzErr)
repocall1 := repo.On("RetrieveByID", context.Background(), tc.id).Return(tc.retrieveResp, tc.retrieveErr)
repocall2 := repo.On("ChangeStatus", context.Background(), mock.Anything).Return(tc.changeResp, tc.changeErr)
repocall := repo.On("RetrieveByID", context.Background(), tc.id).Return(tc.retrieveResp, tc.retrieveErr)
repocall1 := repo.On("ChangeStatus", context.Background(), mock.Anything).Return(tc.changeResp, tc.changeErr)
got, err := svc.EnableGroup(context.Background(), tc.token, tc.id)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("expected error %v to contain %v", err, tc.err))
if err == nil {
@@ -649,9 +649,9 @@ func TestEnableGroup(t *testing.T) {
ok := repo.AssertCalled(t, "RetrieveByID", context.Background(), tc.id)
assert.True(t, ok, fmt.Sprintf("RetrieveByID was not called on %s", tc.desc))
}
authcall.Unset()
repocall.Unset()
repocall1.Unset()
repocall2.Unset()
})
}
}
@@ -732,7 +732,7 @@ func TestDisableGroup(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
SubjectType: auth.UserType,
SubjectKind: auth.TokenKind,
Subject: tc.token,
@@ -740,8 +740,8 @@ func TestDisableGroup(t *testing.T) {
Object: tc.id,
ObjectType: auth.GroupType,
}).Return(tc.authzResp, tc.authzErr)
repocall1 := repo.On("RetrieveByID", context.Background(), tc.id).Return(tc.retrieveResp, tc.retrieveErr)
repocall2 := repo.On("ChangeStatus", context.Background(), mock.Anything).Return(tc.changeResp, tc.changeErr)
repocall := repo.On("RetrieveByID", context.Background(), tc.id).Return(tc.retrieveResp, tc.retrieveErr)
repocall1 := repo.On("ChangeStatus", context.Background(), mock.Anything).Return(tc.changeResp, tc.changeErr)
got, err := svc.DisableGroup(context.Background(), tc.token, tc.id)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("expected error %v to contain %v", err, tc.err))
if err == nil {
@@ -749,9 +749,9 @@ func TestDisableGroup(t *testing.T) {
ok := repo.AssertCalled(t, "RetrieveByID", context.Background(), tc.id)
assert.True(t, ok, fmt.Sprintf("RetrieveByID was not called on %s", tc.desc))
}
authcall.Unset()
repocall.Unset()
repocall1.Unset()
repocall2.Unset()
})
}
}
@@ -860,7 +860,7 @@ func TestListMembers(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
SubjectType: auth.UserType,
SubjectKind: auth.TokenKind,
Subject: tc.token,
@@ -868,13 +868,13 @@ func TestListMembers(t *testing.T) {
Object: tc.groupID,
ObjectType: auth.GroupType,
}).Return(tc.authzResp, tc.authzErr)
repocall1 := authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
authcall1 := authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
SubjectType: auth.GroupType,
Subject: tc.groupID,
Relation: auth.GroupRelation,
ObjectType: auth.ThingType,
}).Return(tc.listObjectResp, tc.listObjectErr)
repocall2 := authsvc.On("ListAllSubjects", context.Background(), &magistrala.ListSubjectsReq{
authcall2 := authsvc.On("ListAllSubjects", context.Background(), &magistrala.ListSubjectsReq{
SubjectType: auth.UserType,
Permission: tc.permission,
Object: tc.groupID,
@@ -885,9 +885,9 @@ func TestListMembers(t *testing.T) {
if err == nil {
assert.NotEmpty(t, got)
}
repocall.Unset()
repocall1.Unset()
repocall2.Unset()
authcall.Unset()
authcall1.Unset()
authcall2.Unset()
})
}
}
@@ -1513,14 +1513,14 @@ func TestListGroups(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
repocall1 := &mock.Call{}
repocall2 := &mock.Call{}
repocall3 := &mock.Call{}
authcall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
authcall1 := &mock.Call{}
authcall2 := &mock.Call{}
authcall3 := &mock.Call{}
adminCheck := &mock.Call{}
switch tc.memberKind {
case auth.ThingsKind:
repocall1 = authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall1 = authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
Domain: tc.idResp.GetDomainId(),
SubjectType: auth.UserType,
SubjectKind: auth.UsersKind,
@@ -1529,20 +1529,20 @@ func TestListGroups(t *testing.T) {
Object: tc.memberID,
ObjectType: auth.ThingType,
}).Return(tc.authzResp, tc.authzErr)
repocall2 = authsvc.On("ListAllSubjects", context.Background(), &magistrala.ListSubjectsReq{
authcall2 = authsvc.On("ListAllSubjects", context.Background(), &magistrala.ListSubjectsReq{
SubjectType: auth.GroupType,
Permission: auth.GroupRelation,
ObjectType: auth.ThingType,
Object: tc.memberID,
}).Return(tc.listSubjectResp, tc.listSubjectErr)
repocall3 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
authcall3 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
SubjectType: auth.UserType,
Subject: tc.idResp.GetId(),
Permission: tc.page.Permission,
ObjectType: auth.GroupType,
}).Return(tc.listObjectFilterResp, tc.listObjectFilterErr)
case auth.GroupsKind:
repocall1 = authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall1 = authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
Domain: tc.idResp.GetDomainId(),
SubjectType: auth.UserType,
SubjectKind: auth.UsersKind,
@@ -1551,20 +1551,20 @@ func TestListGroups(t *testing.T) {
Object: tc.memberID,
ObjectType: auth.GroupType,
}).Return(tc.authzResp, tc.authzErr)
repocall2 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
authcall2 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
SubjectType: auth.GroupType,
Subject: tc.memberID,
Permission: auth.ParentGroupRelation,
ObjectType: auth.GroupType,
}).Return(tc.listObjectResp, tc.listObjectErr)
repocall3 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
authcall3 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
SubjectType: auth.UserType,
Subject: tc.idResp.GetId(),
Permission: tc.page.Permission,
ObjectType: auth.GroupType,
}).Return(tc.listObjectFilterResp, tc.listObjectFilterErr)
case auth.ChannelsKind:
repocall1 = authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall1 = authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
Domain: tc.idResp.GetDomainId(),
SubjectType: auth.UserType,
SubjectKind: auth.UsersKind,
@@ -1573,13 +1573,13 @@ func TestListGroups(t *testing.T) {
Object: tc.memberID,
ObjectType: auth.GroupType,
}).Return(tc.authzResp, tc.authzErr)
repocall2 = authsvc.On("ListAllSubjects", context.Background(), &magistrala.ListSubjectsReq{
authcall2 = authsvc.On("ListAllSubjects", context.Background(), &magistrala.ListSubjectsReq{
SubjectType: auth.GroupType,
Permission: auth.ParentGroupRelation,
ObjectType: auth.GroupType,
Object: tc.memberID,
}).Return(tc.listSubjectResp, tc.listSubjectErr)
repocall3 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
authcall3 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
SubjectType: auth.UserType,
Subject: tc.idResp.GetId(),
Permission: tc.page.Permission,
@@ -1607,39 +1607,39 @@ func TestListGroups(t *testing.T) {
authReq.Domain = ""
authReq.Permission = auth.MembershipPermission
}
repocall1 = authsvc.On("Authorize", context.Background(), authReq).Return(tc.authzResp, tc.authzErr)
repocall2 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
authcall1 = authsvc.On("Authorize", context.Background(), authReq).Return(tc.authzResp, tc.authzErr)
authcall2 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
SubjectType: auth.UserType,
Subject: auth.EncodeDomainUserID(tc.idResp.GetDomainId(), tc.memberID),
Permission: tc.page.Permission,
ObjectType: auth.GroupType,
}).Return(tc.listObjectResp, tc.listObjectErr)
repocall3 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
authcall3 = authsvc.On("ListAllObjects", context.Background(), &magistrala.ListObjectsReq{
SubjectType: auth.UserType,
Subject: tc.idResp.GetId(),
Permission: tc.page.Permission,
ObjectType: auth.GroupType,
}).Return(tc.listObjectFilterResp, tc.listObjectFilterErr)
}
repocall4 := repo.On("RetrieveByIDs", context.Background(), mock.Anything, mock.Anything).Return(tc.repoResp, tc.repoErr)
repocall5 := authsvc.On("ListPermissions", mock.Anything, mock.Anything).Return(tc.listPermResp, tc.listPermErr)
repocall := repo.On("RetrieveByIDs", context.Background(), mock.Anything, mock.Anything).Return(tc.repoResp, tc.repoErr)
authcall4 := authsvc.On("ListPermissions", mock.Anything, mock.Anything).Return(tc.listPermResp, tc.listPermErr)
got, err := svc.ListGroups(context.Background(), tc.token, tc.memberKind, tc.memberID, tc.page)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("expected error %v to contain %v", err, tc.err))
if err == nil {
assert.NotEmpty(t, got)
}
authcall.Unset()
repocall.Unset()
switch tc.memberKind {
case auth.ThingsKind, auth.GroupsKind, auth.ChannelsKind, auth.UsersKind:
repocall1.Unset()
repocall2.Unset()
repocall3.Unset()
authcall1.Unset()
authcall2.Unset()
authcall3.Unset()
authcall4.Unset()
if tc.memberID == "" {
adminCheck.Unset()
}
}
repocall4.Unset()
repocall5.Unset()
})
}
}
@@ -1965,8 +1965,8 @@ func TestAssign(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
repocall1 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
authcall1 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
Domain: tc.idResp.GetDomainId(),
SubjectType: auth.UserType,
SubjectKind: auth.UsersKind,
@@ -2038,12 +2038,12 @@ func TestAssign(t *testing.T) {
})
}
}
repocall2 := authsvc.On("AddPolicies", context.Background(), &policies).Return(tc.addPoliciesRes, tc.addPoliciesErr)
authcall2 := authsvc.On("AddPolicies", context.Background(), &policies).Return(tc.addPoliciesRes, tc.addPoliciesErr)
err := svc.Assign(context.Background(), tc.token, tc.groupID, tc.relation, tc.memberKind, tc.memberIDs...)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("expected error %v to contain %v", err, tc.err))
repocall.Unset()
repocall1.Unset()
repocall2.Unset()
authcall.Unset()
authcall1.Unset()
authcall2.Unset()
if tc.memberKind == auth.GroupsKind {
retrieveByIDsCall.Unset()
deletePoliciesCall.Unset()
@@ -2374,8 +2374,8 @@ func TestUnassign(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
repocall1 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
authcall1 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
Domain: tc.idResp.GetDomainId(),
SubjectType: auth.UserType,
SubjectKind: auth.UsersKind,
@@ -2447,12 +2447,12 @@ func TestUnassign(t *testing.T) {
})
}
}
repocall2 := authsvc.On("DeletePolicies", context.Background(), &policies).Return(tc.deletePoliciesRes, tc.deletePoliciesErr)
authcall2 := authsvc.On("DeletePolicies", context.Background(), &policies).Return(tc.deletePoliciesRes, tc.deletePoliciesErr)
err := svc.Unassign(context.Background(), tc.token, tc.groupID, tc.relation, tc.memberKind, tc.memberIDs...)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("expected error %v to contain %v", err, tc.err))
repocall.Unset()
repocall1.Unset()
repocall2.Unset()
authcall.Unset()
authcall1.Unset()
authcall2.Unset()
if tc.memberKind == auth.GroupsKind {
retrieveByIDsCall.Unset()
addPoliciesCall.Unset()
@@ -2475,13 +2475,13 @@ func TestDeleteGroup(t *testing.T) {
idErr error
authzResp *magistrala.AuthorizeRes
authzErr error
deleteChildPoliciesRes *magistrala.DeletePolicyRes
deleteChildPoliciesRes *magistrala.DeletePolicyFilterRes
deleteChildPoliciesErr error
deleteThingsPoliciesRes *magistrala.DeletePolicyRes
deleteThingsPoliciesRes *magistrala.DeletePolicyFilterRes
deleteThingsPoliciesErr error
deleteDomainsPoliciesRes *magistrala.DeletePolicyRes
deleteDomainsPoliciesRes *magistrala.DeletePolicyFilterRes
deleteDomainsPoliciesErr error
deleteUsersPoliciesRes *magistrala.DeletePolicyRes
deleteUsersPoliciesRes *magistrala.DeletePolicyFilterRes
deleteUsersPoliciesErr error
repoErr error
err error
@@ -2497,16 +2497,16 @@ func TestDeleteGroup(t *testing.T) {
authzResp: &magistrala.AuthorizeRes{
Authorized: true,
},
deleteChildPoliciesRes: &magistrala.DeletePolicyRes{
deleteChildPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteThingsPoliciesRes: &magistrala.DeletePolicyRes{
deleteThingsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteDomainsPoliciesRes: &magistrala.DeletePolicyRes{
deleteDomainsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteUsersPoliciesRes: &magistrala.DeletePolicyRes{
deleteUsersPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
},
@@ -2543,7 +2543,7 @@ func TestDeleteGroup(t *testing.T) {
authzResp: &magistrala.AuthorizeRes{
Authorized: true,
},
deleteChildPoliciesRes: &magistrala.DeletePolicyRes{
deleteChildPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: false,
},
deleteChildPoliciesErr: svcerr.ErrAuthorization,
@@ -2560,10 +2560,10 @@ func TestDeleteGroup(t *testing.T) {
authzResp: &magistrala.AuthorizeRes{
Authorized: true,
},
deleteChildPoliciesRes: &magistrala.DeletePolicyRes{
deleteChildPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteThingsPoliciesRes: &magistrala.DeletePolicyRes{
deleteThingsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: false,
},
deleteThingsPoliciesErr: svcerr.ErrAuthorization,
@@ -2580,13 +2580,13 @@ func TestDeleteGroup(t *testing.T) {
authzResp: &magistrala.AuthorizeRes{
Authorized: true,
},
deleteChildPoliciesRes: &magistrala.DeletePolicyRes{
deleteChildPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteThingsPoliciesRes: &magistrala.DeletePolicyRes{
deleteThingsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteDomainsPoliciesRes: &magistrala.DeletePolicyRes{
deleteDomainsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: false,
},
deleteDomainsPoliciesErr: svcerr.ErrAuthorization,
@@ -2603,16 +2603,16 @@ func TestDeleteGroup(t *testing.T) {
authzResp: &magistrala.AuthorizeRes{
Authorized: true,
},
deleteChildPoliciesRes: &magistrala.DeletePolicyRes{
deleteChildPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteThingsPoliciesRes: &magistrala.DeletePolicyRes{
deleteThingsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteDomainsPoliciesRes: &magistrala.DeletePolicyRes{
deleteDomainsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteUsersPoliciesRes: &magistrala.DeletePolicyRes{
deleteUsersPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: false,
},
deleteUsersPoliciesErr: svcerr.ErrAuthorization,
@@ -2629,13 +2629,13 @@ func TestDeleteGroup(t *testing.T) {
authzResp: &magistrala.AuthorizeRes{
Authorized: true,
},
deleteChildPoliciesRes: &magistrala.DeletePolicyRes{
deleteChildPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteThingsPoliciesRes: &magistrala.DeletePolicyRes{
deleteThingsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
deleteDomainsPoliciesRes: &magistrala.DeletePolicyRes{
deleteDomainsPoliciesRes: &magistrala.DeletePolicyFilterRes{
Deleted: true,
},
repoErr: repoerr.ErrNotFound,
@@ -2645,8 +2645,8 @@ func TestDeleteGroup(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
repocall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
repocall1 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
authcall := authsvc.On("Identify", context.Background(), &magistrala.IdentityReq{Token: tc.token}).Return(tc.idResp, tc.idErr)
authcall1 := authsvc.On("Authorize", context.Background(), &magistrala.AuthorizeReq{
Domain: tc.idResp.GetDomainId(),
SubjectType: auth.UserType,
SubjectKind: auth.UsersKind,
@@ -2655,36 +2655,36 @@ func TestDeleteGroup(t *testing.T) {
Object: tc.groupID,
ObjectType: auth.GroupType,
}).Return(tc.authzResp, tc.authzErr)
repocall2 := authsvc.On("DeletePolicy", context.Background(), &magistrala.DeletePolicyReq{
authcall2 := authsvc.On("DeletePolicyFilter", context.Background(), &magistrala.DeletePolicyFilterReq{
SubjectType: auth.GroupType,
Subject: tc.groupID,
ObjectType: auth.GroupType,
}).Return(tc.deleteChildPoliciesRes, tc.deleteChildPoliciesErr)
repocall3 := authsvc.On("DeletePolicy", context.Background(), &magistrala.DeletePolicyReq{
authcall3 := authsvc.On("DeletePolicyFilter", context.Background(), &magistrala.DeletePolicyFilterReq{
SubjectType: auth.GroupType,
Subject: tc.groupID,
ObjectType: auth.ThingType,
}).Return(tc.deleteThingsPoliciesRes, tc.deleteThingsPoliciesErr)
repocall4 := authsvc.On("DeletePolicy", context.Background(), &magistrala.DeletePolicyReq{
authcall4 := authsvc.On("DeletePolicyFilter", context.Background(), &magistrala.DeletePolicyFilterReq{
SubjectType: auth.DomainType,
Object: tc.groupID,
ObjectType: auth.GroupType,
}).Return(tc.deleteDomainsPoliciesRes, tc.deleteDomainsPoliciesErr)
repocall5 := repo.On("Delete", context.Background(), tc.groupID).Return(tc.repoErr)
repocall6 := authsvc.On("DeletePolicy", context.Background(), &magistrala.DeletePolicyReq{
authcall5 := repo.On("Delete", context.Background(), tc.groupID).Return(tc.repoErr)
authcall6 := authsvc.On("DeletePolicyFilter", context.Background(), &magistrala.DeletePolicyFilterReq{
SubjectType: auth.UserType,
Object: tc.groupID,
ObjectType: auth.GroupType,
}).Return(tc.deleteUsersPoliciesRes, tc.deleteUsersPoliciesErr)
err := svc.DeleteGroup(context.Background(), tc.token, tc.groupID)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("expected error %v to contain %v", err, tc.err))
repocall.Unset()
repocall1.Unset()
repocall2.Unset()
repocall3.Unset()
repocall4.Unset()
repocall5.Unset()
repocall6.Unset()
authcall.Unset()
authcall1.Unset()
authcall2.Unset()
authcall3.Unset()
authcall4.Unset()
authcall5.Unset()
authcall6.Unset()
})
}
}
+62 -62
View File
@@ -262,14 +262,14 @@ func TestListChannels(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
authCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
authCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
if tc.token == invalidToken {
repoCall = auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: invalidToken}).Return(&magistrala.IdentityRes{}, svcerr.ErrAuthentication)
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
authCall = auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: invalidToken}).Return(&magistrala.IdentityRes{}, svcerr.ErrAuthentication)
authCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
}
repoCall2 := auth.On("ListAllObjects", mock.Anything, mock.Anything).Return(&magistrala.ListObjectsRes{Policies: toIDs(tc.response)}, nil)
repoCall3 := grepo.On("RetrieveByIDs", mock.Anything, mock.Anything, mock.Anything).Return(mggroups.Page{Groups: convertChannels(tc.response)}, tc.err)
authCall2 := auth.On("ListAllObjects", mock.Anything, mock.Anything).Return(&magistrala.ListObjectsRes{Policies: toIDs(tc.response)}, nil)
repoCall := grepo.On("RetrieveByIDs", mock.Anything, mock.Anything, mock.Anything).Return(mggroups.Page{Groups: convertChannels(tc.response)}, tc.err)
pm := sdk.PageMetadata{
Offset: tc.offset,
Limit: tc.limit,
@@ -279,13 +279,13 @@ func TestListChannels(t *testing.T) {
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
assert.Equal(t, len(tc.response), len(page.Channels), fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.response, page))
if tc.err == nil {
ok := repoCall3.Parent.AssertCalled(t, "RetrieveByIDs", mock.Anything, mock.Anything, mock.Anything)
ok := repoCall.Parent.AssertCalled(t, "RetrieveByIDs", mock.Anything, mock.Anything, mock.Anything)
assert.True(t, ok, fmt.Sprintf("RetrieveByIDs was not called on %s", tc.desc))
}
authCall.Unset()
authCall1.Unset()
authCall2.Unset()
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
repoCall3.Unset()
}
}
@@ -338,8 +338,8 @@ func TestViewChannel(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall1 := grepo.On("RetrieveByID", mock.Anything, tc.channelID).Return(convertChannel(tc.response), tc.err)
authCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall := grepo.On("RetrieveByID", mock.Anything, tc.channelID).Return(convertChannel(tc.response), tc.err)
grp, err := mgsdk.Channel(tc.channelID, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
if len(tc.response.Children) == 0 {
@@ -350,11 +350,11 @@ func TestViewChannel(t *testing.T) {
}
assert.Equal(t, tc.response, grp, fmt.Sprintf("%s: expected metadata %v got %v\n", tc.desc, tc.response, grp))
if tc.err == nil {
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, tc.channelID)
ok := repoCall.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, tc.channelID)
assert.True(t, ok, fmt.Sprintf("RetrieveByID was not called on %s", tc.desc))
}
authCall.Unset()
repoCall.Unset()
repoCall1.Unset()
}
}
@@ -505,16 +505,16 @@ func TestUpdateChannel(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall1 := grepo.On("Update", mock.Anything, mock.Anything).Return(convertChannel(tc.response), tc.err)
authCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall := grepo.On("Update", mock.Anything, mock.Anything).Return(convertChannel(tc.response), tc.err)
_, err := mgsdk.UpdateChannel(tc.channel, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
if tc.err == nil {
ok := repoCall1.Parent.AssertCalled(t, "Update", mock.Anything, mock.Anything)
ok := repoCall.Parent.AssertCalled(t, "Update", mock.Anything, mock.Anything)
assert.True(t, ok, fmt.Sprintf("Update was not called on %s", tc.desc))
}
authCall.Unset()
repoCall.Unset()
repoCall1.Unset()
}
}
@@ -623,19 +623,19 @@ func TestListChannelsByThing(t *testing.T) {
}
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall2 := auth.On("ListAllSubjects", mock.Anything, mock.Anything).Return(&magistrala.ListSubjectsRes{Policies: toIDs(tc.response)}, nil)
repoCall3 := auth.On("ListAllObjects", mock.Anything, mock.Anything).Return(&magistrala.ListObjectsRes{Policies: toIDs(tc.response)}, nil)
repoCall4 := grepo.On("RetrieveByIDs", mock.Anything, mock.Anything, mock.Anything).Return(mggroups.Page{Groups: convertChannels(tc.response)}, tc.err)
authCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
authCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
authCall2 := auth.On("ListAllSubjects", mock.Anything, mock.Anything).Return(&magistrala.ListSubjectsRes{Policies: toIDs(tc.response)}, nil)
authCall3 := auth.On("ListAllObjects", mock.Anything, mock.Anything).Return(&magistrala.ListObjectsRes{Policies: toIDs(tc.response)}, nil)
repoCall := grepo.On("RetrieveByIDs", mock.Anything, mock.Anything, mock.Anything).Return(mggroups.Page{Groups: convertChannels(tc.response)}, tc.err)
page, err := mgsdk.ChannelsByThing(tc.clientID, tc.page, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
assert.Equal(t, tc.response, page.Channels, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.response, page.Channels))
authCall.Unset()
authCall1.Unset()
authCall2.Unset()
authCall3.Unset()
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
repoCall3.Unset()
repoCall4.Unset()
}
}
@@ -657,16 +657,16 @@ func TestEnableChannel(t *testing.T) {
Status: mgclients.Disabled,
}
repoCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall1 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
repoCall2 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
authCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
repoCall1 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
_, err := mgsdk.EnableChannel("wrongID", validToken)
assert.Equal(t, errors.NewSDKErrorWithStatus(svcerr.ErrViewEntity, http.StatusBadRequest), err, fmt.Sprintf("Enable channel with wrong id: expected %v got %v", svcerr.ErrViewEntity, err))
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
ok := repoCall.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
assert.True(t, ok, "RetrieveByID was not called on enabling channel")
authCall.Unset()
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
ch := mggroups.Group{
ID: channel.ID,
@@ -675,19 +675,19 @@ func TestEnableChannel(t *testing.T) {
UpdatedAt: creationTime,
Status: mgclients.DisabledStatus,
}
repoCall = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall1 = grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(ch, nil)
repoCall2 = grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(ch, nil)
authCall = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall = grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(ch, nil)
repoCall1 = grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(ch, nil)
res, err := mgsdk.EnableChannel(channel.ID, validToken)
assert.Nil(t, err, fmt.Sprintf("Enable channel with correct id: expected %v got %v", nil, err))
assert.Equal(t, channel, res, fmt.Sprintf("Enable channel with correct id: expected %v got %v", channel, res))
ok = repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, channel.ID)
ok = repoCall.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, channel.ID)
assert.True(t, ok, "RetrieveByID was not called on enabling channel")
ok = repoCall2.Parent.AssertCalled(t, "ChangeStatus", mock.Anything, mock.Anything)
ok = repoCall1.Parent.AssertCalled(t, "ChangeStatus", mock.Anything, mock.Anything)
assert.True(t, ok, "ChangeStatus was not called on enabling channel")
authCall.Unset()
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
}
func TestDisableChannel(t *testing.T) {
@@ -709,16 +709,16 @@ func TestDisableChannel(t *testing.T) {
Status: mgclients.Enabled,
}
repoCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall1 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
repoCall2 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
authCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
repoCall1 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
_, err := mgsdk.DisableChannel("wrongID", validToken)
assert.Equal(t, err, errors.NewSDKErrorWithStatus(svcerr.ErrViewEntity, http.StatusBadRequest), fmt.Sprintf("Disable channel with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
ok := repoCall.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
assert.True(t, ok, "Memberships was not called on disabling channel with wrong id")
authCall.Unset()
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
ch := mggroups.Group{
ID: channel.ID,
@@ -729,19 +729,19 @@ func TestDisableChannel(t *testing.T) {
Status: mgclients.EnabledStatus,
}
repoCall = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall1 = grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(ch, nil)
repoCall2 = grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(ch, nil)
authCall = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall = grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(ch, nil)
repoCall1 = grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(ch, nil)
res, err := mgsdk.DisableChannel(channel.ID, validToken)
assert.Nil(t, err, fmt.Sprintf("Disable channel with correct id: expected %v got %v", nil, err))
assert.Equal(t, channel, res, fmt.Sprintf("Disable channel with correct id: expected %v got %v", channel, res))
ok = repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, channel.ID)
ok = repoCall.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, channel.ID)
assert.True(t, ok, "RetrieveByID was not called on disabling channel with correct id")
ok = repoCall2.Parent.AssertCalled(t, "ChangeStatus", mock.Anything, mock.Anything)
ok = repoCall1.Parent.AssertCalled(t, "ChangeStatus", mock.Anything, mock.Anything)
assert.True(t, ok, "ChangeStatus was not called on disabling channel with correct id")
authCall.Unset()
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
}
func TestDeleteChannel(t *testing.T) {
@@ -762,27 +762,27 @@ func TestDeleteChannel(t *testing.T) {
Status: mgclients.Enabled,
}
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, nil)
repoCall2 := grepo.On("Delete", mock.Anything, mock.Anything).Return(nil)
authCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
authCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, nil)
repoCall := grepo.On("Delete", mock.Anything, mock.Anything).Return(nil)
err := mgsdk.DeleteChannel("wrongID", validToken)
assert.Equal(t, err, errors.NewSDKErrorWithStatus(svcerr.ErrAuthorization, http.StatusForbidden), fmt.Sprintf("Delete channel with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
authCall.Unset()
authCall1.Unset()
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
repoCall = auth.On("DeletePolicy", mock.Anything, mock.Anything, mock.Anything).Return(&magistrala.DeletePolicyRes{Deleted: true}, nil)
repoCall1 = auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
repoCall2 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall3 := grepo.On("Delete", mock.Anything, mock.Anything).Return(nil)
authCall = auth.On("DeletePolicyFilter", mock.Anything, mock.Anything, mock.Anything).Return(&magistrala.DeletePolicyFilterRes{Deleted: true}, nil)
authCall1 = auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
authCall2 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall = grepo.On("Delete", mock.Anything, mock.Anything).Return(nil)
err = mgsdk.DeleteChannel(channel.ID, validToken)
assert.Nil(t, err, fmt.Sprintf("Delete channel with correct id: expected %v got %v", nil, err))
ok := repoCall3.Parent.AssertCalled(t, "Delete", mock.Anything, channel.ID)
ok := repoCall.Parent.AssertCalled(t, "Delete", mock.Anything, channel.ID)
assert.True(t, ok, "Delete was not called on deleting channel with correct id")
authCall.Unset()
authCall1.Unset()
authCall2.Unset()
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
repoCall3.Unset()
}
func toIDs(objects interface{}) []string {
+1 -1
View File
@@ -1282,7 +1282,7 @@ func TestDeleteThing(t *testing.T) {
repoCall1.Unset()
repoCall2.Unset()
repoCall = auth.On("DeletePolicy", mock.Anything, mock.Anything, mock.Anything).Return(&magistrala.DeletePolicyRes{Deleted: true}, nil)
repoCall = auth.On("DeletePolicyFilter", mock.Anything, mock.Anything, mock.Anything).Return(&magistrala.DeletePolicyFilterRes{Deleted: true}, nil)
repoCall1 = auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
repoCall2 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall3 := cRepo.On("Delete", mock.Anything, mock.Anything).Return(nil)
+1 -1
View File
@@ -957,7 +957,7 @@ func TestUpdateClientRole(t *testing.T) {
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, svcerr.ErrAuthentication)
}
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall2 := auth.On("DeletePolicy", mock.Anything, mock.Anything).Return(&magistrala.DeletePolicyRes{Deleted: true}, nil)
repoCall2 := auth.On("DeletePolicyFilter", mock.Anything, mock.Anything).Return(&magistrala.DeletePolicyFilterRes{Deleted: true}, nil)
repoCall3 := auth.On("AddPolicy", mock.Anything, mock.Anything).Return(&magistrala.AddPolicyRes{Added: true}, nil)
repoCall4 := crepo.On("UpdateRole", mock.Anything, mock.Anything).Return(convertClient(tc.response), tc.err)
uClient, err := mgsdk.UpdateUserRole(tc.client, tc.token)
+3 -3
View File
@@ -444,7 +444,7 @@ func (svc service) DeleteClient(ctx context.Context, token, id string) error {
}
// Remove policy of groups
if _, err := svc.auth.DeletePolicy(ctx, &magistrala.DeletePolicyReq{
if _, err := svc.auth.DeletePolicyFilter(ctx, &magistrala.DeletePolicyFilterReq{
SubjectType: auth.GroupType,
Object: id,
ObjectType: auth.ThingType,
@@ -453,7 +453,7 @@ func (svc service) DeleteClient(ctx context.Context, token, id string) error {
}
// Remove policy from domain
if _, err := svc.auth.DeletePolicy(ctx, &magistrala.DeletePolicyReq{
if _, err := svc.auth.DeletePolicyFilter(ctx, &magistrala.DeletePolicyFilterReq{
SubjectType: auth.DomainType,
Object: id,
ObjectType: auth.ThingType,
@@ -467,7 +467,7 @@ func (svc service) DeleteClient(ctx context.Context, token, id string) error {
}
// Remove policy of users
if _, err := svc.auth.DeletePolicy(ctx, &magistrala.DeletePolicyReq{
if _, err := svc.auth.DeletePolicyFilter(ctx, &magistrala.DeletePolicyFilterReq{
SubjectType: auth.UserType,
Object: id,
ObjectType: auth.ThingType,
+17 -17
View File
@@ -1613,9 +1613,9 @@ func TestDeleteClient(t *testing.T) {
token string
identifyResponse *magistrala.IdentityRes
authorizeResponse *magistrala.AuthorizeRes
deletePolicyResponse *magistrala.DeletePolicyRes
deletePolicyResponse1 *magistrala.DeletePolicyRes
deletePolicyResponse2 *magistrala.DeletePolicyRes
deletePolicyResponse *magistrala.DeletePolicyFilterRes
deletePolicyResponse1 *magistrala.DeletePolicyFilterRes
deletePolicyResponse2 *magistrala.DeletePolicyFilterRes
clientID string
identifyErr error
authorizeErr error
@@ -1632,9 +1632,9 @@ func TestDeleteClient(t *testing.T) {
clientID: client.ID,
identifyResponse: &magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: true},
deletePolicyResponse1: &magistrala.DeletePolicyRes{Deleted: true},
deletePolicyResponse2: &magistrala.DeletePolicyRes{Deleted: true},
deletePolicyResponse: &magistrala.DeletePolicyFilterRes{Deleted: true},
deletePolicyResponse1: &magistrala.DeletePolicyFilterRes{Deleted: true},
deletePolicyResponse2: &magistrala.DeletePolicyFilterRes{Deleted: true},
err: nil,
},
{
@@ -1660,8 +1660,8 @@ func TestDeleteClient(t *testing.T) {
clientID: client.ID,
identifyResponse: &magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: true},
deletePolicyResponse1: &magistrala.DeletePolicyRes{Deleted: true},
deletePolicyResponse: &magistrala.DeletePolicyFilterRes{Deleted: true},
deletePolicyResponse1: &magistrala.DeletePolicyFilterRes{Deleted: true},
deleteErr: repoerr.ErrRemoveEntity,
err: repoerr.ErrRemoveEntity,
},
@@ -1680,7 +1680,7 @@ func TestDeleteClient(t *testing.T) {
clientID: client.ID,
identifyResponse: &magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: false},
deletePolicyResponse: &magistrala.DeletePolicyFilterRes{Deleted: false},
deletePolicyErr: errRemovePolicies,
err: errRemovePolicies,
},
@@ -1690,8 +1690,8 @@ func TestDeleteClient(t *testing.T) {
clientID: client.ID,
identifyResponse: &magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: true},
deletePolicyResponse1: &magistrala.DeletePolicyRes{Deleted: false},
deletePolicyResponse: &magistrala.DeletePolicyFilterRes{Deleted: true},
deletePolicyResponse1: &magistrala.DeletePolicyFilterRes{Deleted: false},
deletePolicyErr1: errRemovePolicies,
err: errRemovePolicies,
},
@@ -1701,9 +1701,9 @@ func TestDeleteClient(t *testing.T) {
clientID: client.ID,
identifyResponse: &magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: true},
deletePolicyResponse1: &magistrala.DeletePolicyRes{Deleted: true},
deletePolicyResponse2: &magistrala.DeletePolicyRes{Deleted: false},
deletePolicyResponse: &magistrala.DeletePolicyFilterRes{Deleted: true},
deletePolicyResponse1: &magistrala.DeletePolicyFilterRes{Deleted: true},
deletePolicyResponse2: &magistrala.DeletePolicyFilterRes{Deleted: false},
deletePolicyErr2: errRemovePolicies,
err: errRemovePolicies,
},
@@ -1713,18 +1713,18 @@ func TestDeleteClient(t *testing.T) {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(tc.identifyResponse, tc.identifyErr)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(tc.authorizeResponse, tc.authorizeErr)
repoCall2 := cache.On("Remove", mock.Anything, tc.clientID).Return(tc.removeErr)
repoCall3 := auth.On("DeletePolicy", context.Background(), &magistrala.DeletePolicyReq{
repoCall3 := auth.On("DeletePolicyFilter", context.Background(), &magistrala.DeletePolicyFilterReq{
SubjectType: authsvc.GroupType,
Object: tc.clientID,
ObjectType: authsvc.ThingType,
}).Return(tc.deletePolicyResponse, tc.deletePolicyErr)
repoCall4 := auth.On("DeletePolicy", mock.Anything, &magistrala.DeletePolicyReq{
repoCall4 := auth.On("DeletePolicyFilter", mock.Anything, &magistrala.DeletePolicyFilterReq{
SubjectType: authsvc.DomainType,
Object: tc.clientID,
ObjectType: authsvc.ThingType,
}).Return(tc.deletePolicyResponse1, tc.deletePolicyErr1)
repoCall5 := cRepo.On("Delete", context.Background(), tc.clientID).Return(tc.deleteErr)
repoCall6 := auth.On("DeletePolicy", mock.Anything, &magistrala.DeletePolicyReq{
repoCall6 := auth.On("DeletePolicyFilter", mock.Anything, &magistrala.DeletePolicyFilterReq{
SubjectType: authsvc.UserType,
Object: tc.clientID,
ObjectType: authsvc.ThingType,
+1 -1
View File
@@ -62,7 +62,7 @@ func (repo singleUserRepo) AddPolicies(ctx context.Context, in *magistrala.AddPo
return nil, nil
}
func (repo singleUserRepo) DeletePolicy(ctx context.Context, in *magistrala.DeletePolicyReq, opts ...grpc.CallOption) (*magistrala.DeletePolicyRes, error) {
func (repo singleUserRepo) DeletePolicyFilter(ctx context.Context, in *magistrala.DeletePolicyFilterReq, opts ...grpc.CallOption) (*magistrala.DeletePolicyFilterRes, error) {
return nil, nil
}
+1 -1
View File
@@ -710,7 +710,7 @@ func (svc service) updateClientPolicy(ctx context.Context, userID string, role m
case mgclients.UserRole:
fallthrough
default:
resp, err := svc.auth.DeletePolicy(ctx, &magistrala.DeletePolicyReq{
resp, err := svc.auth.DeletePolicyFilter(ctx, &magistrala.DeletePolicyFilterReq{
SubjectType: auth.UserType,
Subject: userID,
Relation: auth.AdministratorRelation,
+81 -81
View File
@@ -998,25 +998,25 @@ func TestUpdateClientRole(t *testing.T) {
}
cases := []struct {
desc string
client mgclients.Client
identifyResponse *magistrala.IdentityRes
superAdminAuthReq *magistrala.AuthorizeReq
membershipAuthReq *magistrala.AuthorizeReq
superAdminAuthRes *magistrala.AuthorizeRes
membershipAuthRes *magistrala.AuthorizeRes
deletePolicyResponse *magistrala.DeletePolicyRes
addPolicyResponse *magistrala.AddPolicyRes
updateRoleResponse mgclients.Client
token string
identifyErr error
authorizeErr error
membershipAuthErr error
deletePolicyErr error
addPolicyErr error
updateRoleErr error
checkSuperAdminErr error
err error
desc string
client mgclients.Client
identifyResponse *magistrala.IdentityRes
superAdminAuthReq *magistrala.AuthorizeReq
membershipAuthReq *magistrala.AuthorizeReq
superAdminAuthRes *magistrala.AuthorizeRes
membershipAuthRes *magistrala.AuthorizeRes
deletePolicyFilterResponse *magistrala.DeletePolicyFilterRes
addPolicyResponse *magistrala.AddPolicyRes
updateRoleResponse mgclients.Client
token string
identifyErr error
authorizeErr error
membershipAuthErr error
deletePolicyErr error
addPolicyErr error
updateRoleErr error
checkSuperAdminErr error
err error
}{
{
desc: "update client role successfully",
@@ -1088,75 +1088,75 @@ func TestUpdateClientRole(t *testing.T) {
err: svcerr.ErrAddPolicies,
},
{
desc: "update client role to user role successfully ",
client: client2,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthReq: superAdminAuthReq,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: true},
updateRoleResponse: client2,
token: validToken,
err: nil,
desc: "update client role to user role successfully ",
client: client2,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthReq: superAdminAuthReq,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyFilterResponse: &magistrala.DeletePolicyFilterRes{Deleted: true},
updateRoleResponse: client2,
token: validToken,
err: nil,
},
{
desc: "update client role to user role with failed to delete policy",
client: client2,
superAdminAuthReq: superAdminAuthReq,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: false},
updateRoleResponse: mgclients.Client{},
token: validToken,
deletePolicyErr: svcerr.ErrAuthorization,
err: svcerr.ErrAuthorization,
desc: "update client role to user role with failed to delete policy",
client: client2,
superAdminAuthReq: superAdminAuthReq,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyFilterResponse: &magistrala.DeletePolicyFilterRes{Deleted: false},
updateRoleResponse: mgclients.Client{},
token: validToken,
deletePolicyErr: svcerr.ErrAuthorization,
err: svcerr.ErrAuthorization,
},
{
desc: "update client role to user role with failed to delete policy with error",
client: client2,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthReq: superAdminAuthReq,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: false},
updateRoleResponse: mgclients.Client{},
token: validToken,
deletePolicyErr: svcerr.ErrMalformedEntity,
err: svcerr.ErrDeletePolicies,
desc: "update client role to user role with failed to delete policy with error",
client: client2,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthReq: superAdminAuthReq,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
deletePolicyFilterResponse: &magistrala.DeletePolicyFilterRes{Deleted: false},
updateRoleResponse: mgclients.Client{},
token: validToken,
deletePolicyErr: svcerr.ErrMalformedEntity,
err: svcerr.ErrDeletePolicies,
},
{
desc: "Update client with failed repo update and roll back",
client: client,
superAdminAuthReq: superAdminAuthReq,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
addPolicyResponse: &magistrala.AddPolicyRes{Added: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: true},
updateRoleResponse: mgclients.Client{},
token: validToken,
updateRoleErr: svcerr.ErrAuthentication,
err: svcerr.ErrAuthentication,
desc: "Update client with failed repo update and roll back",
client: client,
superAdminAuthReq: superAdminAuthReq,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
addPolicyResponse: &magistrala.AddPolicyRes{Added: true},
deletePolicyFilterResponse: &magistrala.DeletePolicyFilterRes{Deleted: true},
updateRoleResponse: mgclients.Client{},
token: validToken,
updateRoleErr: svcerr.ErrAuthentication,
err: svcerr.ErrAuthentication,
},
{
desc: "Update client with failed repo update and failedroll back",
client: client,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthReq: superAdminAuthReq,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
addPolicyResponse: &magistrala.AddPolicyRes{Added: true},
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: false},
updateRoleResponse: mgclients.Client{},
token: validToken,
updateRoleErr: svcerr.ErrAuthentication,
err: svcerr.ErrAuthentication,
desc: "Update client with failed repo update and failedroll back",
client: client,
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
superAdminAuthReq: superAdminAuthReq,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
membershipAuthReq: membershipAuthReq,
membershipAuthRes: &magistrala.AuthorizeRes{Authorized: true},
addPolicyResponse: &magistrala.AddPolicyRes{Added: true},
deletePolicyFilterResponse: &magistrala.DeletePolicyFilterRes{Deleted: false},
updateRoleResponse: mgclients.Client{},
token: validToken,
updateRoleErr: svcerr.ErrAuthentication,
err: svcerr.ErrAuthentication,
},
{
desc: "update client role with failed MembershipPermission authorization",
@@ -1178,7 +1178,7 @@ func TestUpdateClientRole(t *testing.T) {
repoCall := cRepo.On("CheckSuperAdmin", context.Background(), mock.Anything).Return(tc.checkSuperAdminErr)
authCall2 := auth.On("Authorize", context.Background(), tc.membershipAuthReq).Return(tc.membershipAuthRes, tc.membershipAuthErr)
authCall3 := auth.On("AddPolicy", context.Background(), mock.Anything).Return(tc.addPolicyResponse, tc.addPolicyErr)
authCall4 := auth.On("DeletePolicy", context.Background(), mock.Anything).Return(tc.deletePolicyResponse, tc.deletePolicyErr)
authCall4 := auth.On("DeletePolicyFilter", context.Background(), mock.Anything).Return(tc.deletePolicyFilterResponse, tc.deletePolicyErr)
repoCall1 := cRepo.On("UpdateRole", context.Background(), mock.Anything).Return(tc.updateRoleResponse, tc.updateRoleErr)
updatedClient, err := svc.UpdateClientRole(context.Background(), tc.token, tc.client)