mirror of
https://github.com/absmach/supermq.git
synced 2026-06-23 07:10:19 +00:00
NOISSUE - Remove duplicate errors (#2086)
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
kitgrpc "github.com/go-kit/kit/transport/grpc"
|
||||
"google.golang.org/grpc"
|
||||
@@ -725,13 +726,13 @@ func decodeError(err error) error {
|
||||
if st, ok := status.FromError(err); ok {
|
||||
switch st.Code() {
|
||||
case codes.NotFound:
|
||||
return errors.Wrap(errors.ErrNotFound, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrNotFound, errors.New(st.Message()))
|
||||
case codes.InvalidArgument:
|
||||
return errors.Wrap(errors.ErrMalformedEntity, errors.New(st.Message()))
|
||||
case codes.AlreadyExists:
|
||||
return errors.Wrap(errors.ErrConflict, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrConflict, errors.New(st.Message()))
|
||||
case codes.Unauthenticated:
|
||||
return errors.Wrap(errors.ErrAuthentication, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrAuthentication, errors.New(st.Message()))
|
||||
case codes.OK:
|
||||
if msg := st.Message(); msg != "" {
|
||||
return errors.Wrap(errors.ErrUnidentified, errors.New(msg))
|
||||
@@ -740,7 +741,7 @@ func decodeError(err error) error {
|
||||
case codes.FailedPrecondition:
|
||||
return errors.Wrap(errors.ErrMalformedEntity, errors.New(st.Message()))
|
||||
case codes.PermissionDenied:
|
||||
return errors.Wrap(errors.ErrAuthorization, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrAuthorization, errors.New(st.Message()))
|
||||
default:
|
||||
return errors.Wrap(fmt.Errorf("unexpected gRPC status: %s (status code:%v)", st.Code().String(), st.Code()), errors.New(st.Message()))
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ func TestIssue(t *testing.T) {
|
||||
domainID: domainID,
|
||||
kind: auth.APIKey,
|
||||
issueResponse: auth.Token{},
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "issue for invalid key type",
|
||||
@@ -119,7 +119,7 @@ func TestIssue(t *testing.T) {
|
||||
domainID: "",
|
||||
kind: auth.APIKey,
|
||||
issueResponse: auth.Token{},
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ func TestRefresh(t *testing.T) {
|
||||
token: inValidToken,
|
||||
domainID: domainID,
|
||||
issueResponse: auth.Token{},
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "refresh token with empty token",
|
||||
|
||||
@@ -538,19 +538,17 @@ func encodeError(err error) error {
|
||||
err == apiutil.ErrMissingPolicyObj,
|
||||
err == apiutil.ErrMalformedPolicyAct:
|
||||
return status.Error(codes.InvalidArgument, err.Error())
|
||||
case errors.Contains(err, errors.ErrAuthentication),
|
||||
errors.Contains(err, svcerr.ErrAuthentication),
|
||||
case errors.Contains(err, svcerr.ErrAuthentication),
|
||||
errors.Contains(err, auth.ErrKeyExpired),
|
||||
err == apiutil.ErrMissingEmail,
|
||||
err == apiutil.ErrBearerToken:
|
||||
return status.Error(codes.Unauthenticated, err.Error())
|
||||
case errors.Contains(err, errors.ErrAuthorization),
|
||||
errors.Contains(err, svcerr.ErrAuthorization),
|
||||
errors.Contains(err, errors.ErrDomainAuthorization):
|
||||
case errors.Contains(err, svcerr.ErrAuthorization),
|
||||
errors.Contains(err, svcerr.ErrDomainAuthorization):
|
||||
return status.Error(codes.PermissionDenied, err.Error())
|
||||
case errors.Contains(err, errors.ErrNotFound):
|
||||
case errors.Contains(err, svcerr.ErrNotFound):
|
||||
return status.Error(codes.NotFound, err.Error())
|
||||
case errors.Contains(err, errors.ErrConflict):
|
||||
case errors.Contains(err, svcerr.ErrConflict):
|
||||
return status.Error(codes.AlreadyExists, err.Error())
|
||||
default:
|
||||
return status.Error(codes.Internal, err.Error())
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
"github.com/absmach/magistrala/auth/mocks"
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
@@ -240,21 +240,21 @@ func TestRetrieve(t *testing.T) {
|
||||
id: "non-existing",
|
||||
token: token.AccessToken,
|
||||
status: http.StatusNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve a key with an invalid token",
|
||||
id: k.AccessToken,
|
||||
token: "wrong",
|
||||
status: http.StatusUnauthorized,
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "retrieve a key with an empty token",
|
||||
token: "",
|
||||
id: k.AccessToken,
|
||||
status: http.StatusUnauthorized,
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/absmach/magistrala/auth"
|
||||
authjwt "github.com/absmach/magistrala/auth/jwt"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/lestrrat-go/jwx/v2/jwa"
|
||||
"github.com/lestrrat-go/jwx/v2/jwt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -119,7 +120,7 @@ func TestParse(t *testing.T) {
|
||||
desc: "parse invalid key",
|
||||
key: auth.Key{},
|
||||
token: "invalid",
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "parse expired key",
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/lestrrat-go/jwx/v2/jwa"
|
||||
"github.com/lestrrat-go/jwx/v2/jwt"
|
||||
)
|
||||
@@ -68,7 +69,7 @@ func (repo *tokenizer) Issue(key auth.Key) (string, error) {
|
||||
}
|
||||
tkn, err := builder.Build()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(errors.ErrAuthentication, err)
|
||||
return "", errors.Wrap(svcerr.ErrAuthentication, err)
|
||||
}
|
||||
signedTkn, err := jwt.Sign(tkn, jwt.WithKey(jwa.HS512, repo.secret))
|
||||
if err != nil {
|
||||
@@ -88,7 +89,7 @@ func (repo *tokenizer) Parse(token string) (auth.Key, error) {
|
||||
return auth.Key{}, ErrExpiry
|
||||
}
|
||||
|
||||
return auth.Key{}, errors.Wrap(errors.ErrAuthentication, err)
|
||||
return auth.Key{}, errors.Wrap(svcerr.ErrAuthentication, err)
|
||||
}
|
||||
validator := jwt.ValidatorFunc(func(_ context.Context, t jwt.Token) jwt.ValidationError {
|
||||
if t.Issuer() != issuerName {
|
||||
@@ -111,11 +112,11 @@ func (repo *tokenizer) Parse(token string) (auth.Key, error) {
|
||||
|
||||
tType, ok := tkn.Get(tokenType)
|
||||
if !ok {
|
||||
return auth.Key{}, errors.Wrap(errors.ErrAuthentication, err)
|
||||
return auth.Key{}, errors.Wrap(svcerr.ErrAuthentication, err)
|
||||
}
|
||||
ktype, err := strconv.ParseInt(fmt.Sprintf("%v", tType), 10, 64)
|
||||
if err != nil {
|
||||
return auth.Key{}, errors.Wrap(errors.ErrAuthentication, err)
|
||||
return auth.Key{}, errors.Wrap(svcerr.ErrAuthentication, err)
|
||||
}
|
||||
|
||||
key.ID = tkn.JwtID()
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
context "context"
|
||||
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"google.golang.org/grpc"
|
||||
@@ -54,7 +53,7 @@ func (m *AuthClient) Authorize(ctx context.Context, in *magistrala.AuthorizeReq,
|
||||
return &magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization
|
||||
}
|
||||
if in.GetObject() == InvalidValue || in.GetObject() == "" {
|
||||
return &magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization
|
||||
return &magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization
|
||||
}
|
||||
|
||||
return ret.Get(0).(*magistrala.AuthorizeRes), ret.Error(1)
|
||||
|
||||
@@ -269,7 +269,7 @@ func (repo domainRepo) Update(ctx context.Context, id, userID string, dr auth.Do
|
||||
|
||||
dbd, err := toDBDomain(d)
|
||||
if err != nil {
|
||||
return auth.Domain{}, errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return auth.Domain{}, errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
row, err := repo.db.NamedQueryContext(ctx, q, dbd)
|
||||
if err != nil {
|
||||
@@ -516,7 +516,7 @@ type dbDomainsPage struct {
|
||||
func toDBClientsPage(pm auth.Page) (dbDomainsPage, error) {
|
||||
_, data, err := postgres.CreateMetadataQuery("", pm.Metadata)
|
||||
if err != nil {
|
||||
return dbDomainsPage{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return dbDomainsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
return dbDomainsPage{
|
||||
Total: pm.Total,
|
||||
@@ -571,7 +571,7 @@ func buildPageQuery(pm auth.Page) (string, error) {
|
||||
|
||||
mq, _, err := postgres.CreateMetadataQuery("", pm.Metadata)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(errors.ErrViewEntity, err)
|
||||
return "", errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
if mq != "" {
|
||||
query = append(query, mq)
|
||||
|
||||
@@ -55,7 +55,7 @@ func TestAddPolicyCopy(t *testing.T) {
|
||||
ObjectType: "unknown",
|
||||
ObjectID: "unknown",
|
||||
},
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ func TestSave(t *testing.T) {
|
||||
UpdatedBy: userID,
|
||||
Status: auth.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -226,13 +226,13 @@ func TestRetrieveByID(t *testing.T) {
|
||||
desc: "retrieve non-existing client",
|
||||
domainID: inValid,
|
||||
response: auth.Domain{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve with empty client id",
|
||||
domainID: "",
|
||||
response: auth.Domain{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -530,7 +530,7 @@ func TestRetrieveAllByIDs(t *testing.T) {
|
||||
response: auth.DomainsPage{
|
||||
Page: auth.Page{},
|
||||
},
|
||||
err: errors.ErrViewEntity,
|
||||
err: repoerr.ErrViewEntity,
|
||||
},
|
||||
{
|
||||
desc: "retrieve all by ids and id",
|
||||
@@ -851,7 +851,7 @@ func TestListDomains(t *testing.T) {
|
||||
response: auth.DomainsPage{
|
||||
Page: auth.Page{},
|
||||
},
|
||||
err: errors.ErrViewEntity,
|
||||
err: repoerr.ErrViewEntity,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -976,7 +976,7 @@ func TestUpdate(t *testing.T) {
|
||||
Metadata: &clients.Metadata{"key": make(chan int)},
|
||||
},
|
||||
response: auth.Domain{},
|
||||
err: errors.ErrUpdateEntity,
|
||||
err: repoerr.ErrUpdateEntity,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/internal/postgres"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -48,7 +49,7 @@ func (kr *repo) Retrieve(ctx context.Context, issuerID, id string) (auth.Key, er
|
||||
key := dbKey{}
|
||||
if err := kr.db.QueryRowxContext(ctx, q, issuerID, id).StructScan(&key); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return auth.Key{}, errors.ErrNotFound
|
||||
return auth.Key{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return auth.Key{}, postgres.HandleError(errRetrieve, err)
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/auth/postgres"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -63,7 +64,7 @@ func TestKeySave(t *testing.T) {
|
||||
IssuedAt: time.Now(),
|
||||
ExpiresAt: expTime,
|
||||
},
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "save with empty id",
|
||||
@@ -184,19 +185,19 @@ func TestKeyRetrieve(t *testing.T) {
|
||||
desc: "retrieve key with empty issuer id",
|
||||
id: key.ID,
|
||||
issuer: "",
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve non-existent key",
|
||||
id: "",
|
||||
issuer: key.Issuer,
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve non-existent key with empty issuer id",
|
||||
id: "",
|
||||
issuer: "",
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -189,7 +189,7 @@ func (svc service) Authorize(ctx context.Context, pr PolicyReq) error {
|
||||
}
|
||||
if key.Subject == "" {
|
||||
if pr.ObjectType == GroupType || pr.ObjectType == ThingType || pr.ObjectType == DomainType {
|
||||
return errors.ErrDomainAuthorization
|
||||
return svcerr.ErrDomainAuthorization
|
||||
}
|
||||
return svcerr.ErrAuthentication
|
||||
}
|
||||
@@ -208,7 +208,7 @@ func (svc service) checkPolicy(ctx context.Context, pr PolicyReq) error {
|
||||
domainID := pr.Domain
|
||||
if domainID == "" {
|
||||
if pr.ObjectType != DomainType {
|
||||
return errors.ErrDomainAuthorization
|
||||
return svcerr.ErrDomainAuthorization
|
||||
}
|
||||
domainID = pr.Object
|
||||
}
|
||||
@@ -238,7 +238,7 @@ func (svc service) checkDomain(ctx context.Context, subjectType, subject, domain
|
||||
Object: domainID,
|
||||
ObjectType: DomainType,
|
||||
}); err != nil {
|
||||
return errors.ErrDomainAuthorization
|
||||
return svcerr.ErrDomainAuthorization
|
||||
}
|
||||
case FreezeStatus:
|
||||
if err := svc.agent.CheckPolicy(ctx, PolicyReq{
|
||||
@@ -248,10 +248,10 @@ func (svc service) checkDomain(ctx context.Context, subjectType, subject, domain
|
||||
Object: MagistralaObject,
|
||||
ObjectType: PlatformType,
|
||||
}); err != nil {
|
||||
return errors.ErrDomainAuthorization
|
||||
return svcerr.ErrDomainAuthorization
|
||||
}
|
||||
default:
|
||||
return errors.ErrDomainAuthorization
|
||||
return svcerr.ErrDomainAuthorization
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -543,7 +543,7 @@ func (svc service) CreateDomain(ctx context.Context, token string, d Domain) (do
|
||||
|
||||
domainID, err := svc.idProvider.ID()
|
||||
if err != nil {
|
||||
return Domain{}, errors.Wrap(svcerr.ErrUniqueID, err)
|
||||
return Domain{}, err
|
||||
}
|
||||
d.ID = domainID
|
||||
|
||||
|
||||
+22
-22
@@ -199,10 +199,10 @@ func TestIssue(t *testing.T) {
|
||||
ObjectType: auth.PlatformType,
|
||||
Permission: auth.AdminPermission,
|
||||
},
|
||||
checkPolicyErr: errors.ErrNotFound,
|
||||
checkPolicyErr: repoerr.ErrNotFound,
|
||||
retrieveByIDResponse: auth.Domain{},
|
||||
retreiveByIDErr: repoerr.ErrNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "issue login key with failed check on platform admin with enabled status",
|
||||
@@ -351,8 +351,8 @@ func TestIssue(t *testing.T) {
|
||||
IssuedAt: time.Now(),
|
||||
},
|
||||
token: accessToken,
|
||||
saveErr: errors.ErrNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
saveErr: repoerr.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases3 {
|
||||
@@ -406,7 +406,7 @@ func TestIssue(t *testing.T) {
|
||||
},
|
||||
token: refreshToken,
|
||||
checkPolicyErr: svcerr.ErrAuthorization,
|
||||
retrieveByIDErr: errors.ErrNotFound,
|
||||
retrieveByIDErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
@@ -463,7 +463,7 @@ func TestIssue(t *testing.T) {
|
||||
},
|
||||
token: refreshToken,
|
||||
checkPolicyErr: svcerr.ErrAuthorization,
|
||||
retrieveByIDErr: errors.ErrNotFound,
|
||||
retrieveByIDErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
@@ -678,7 +678,7 @@ func TestIdentify(t *testing.T) {
|
||||
desc: "identify invalid key",
|
||||
key: "invalid",
|
||||
idt: "",
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "identify invalid key type",
|
||||
@@ -782,7 +782,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.GroupType,
|
||||
Permission: auth.AdminPermission,
|
||||
},
|
||||
err: errors.ErrDomainAuthorization,
|
||||
err: svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "authorize token with disabled domain",
|
||||
@@ -848,8 +848,8 @@ func TestAuthorize(t *testing.T) {
|
||||
Name: groupName,
|
||||
Status: auth.DisabledStatus,
|
||||
},
|
||||
checkPolicyErr1: errors.ErrDomainAuthorization,
|
||||
err: errors.ErrDomainAuthorization,
|
||||
checkPolicyErr1: svcerr.ErrDomainAuthorization,
|
||||
err: svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "authorize token with frozen domain",
|
||||
@@ -915,8 +915,8 @@ func TestAuthorize(t *testing.T) {
|
||||
Name: groupName,
|
||||
Status: auth.FreezeStatus,
|
||||
},
|
||||
checkPolicyErr1: errors.ErrDomainAuthorization,
|
||||
err: errors.ErrDomainAuthorization,
|
||||
checkPolicyErr1: svcerr.ErrDomainAuthorization,
|
||||
err: svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "authorize token with domain with invalid status",
|
||||
@@ -949,7 +949,7 @@ func TestAuthorize(t *testing.T) {
|
||||
Name: groupName,
|
||||
Status: auth.AllStatus,
|
||||
},
|
||||
err: errors.ErrDomainAuthorization,
|
||||
err: svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
|
||||
{
|
||||
@@ -1006,7 +1006,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.PlatformKind,
|
||||
Permission: auth.AdminPermission,
|
||||
},
|
||||
err: errors.ErrDomainAuthorization,
|
||||
err: svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "authorize a user key successfully",
|
||||
@@ -1043,7 +1043,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.PlatformType,
|
||||
Permission: auth.AdminPermission,
|
||||
},
|
||||
err: errors.ErrDomainAuthorization,
|
||||
err: svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -1770,14 +1770,14 @@ func TestRetrieveDomain(t *testing.T) {
|
||||
desc: "retrieve non-existing domain",
|
||||
token: accessToken,
|
||||
domainID: inValid,
|
||||
domainRepoErr: errors.ErrNotFound,
|
||||
domainRepoErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "retrieve domain with failed to retrieve by id",
|
||||
token: accessToken,
|
||||
domainID: validID,
|
||||
domainRepoErr1: errors.ErrNotFound,
|
||||
domainRepoErr1: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
@@ -1829,14 +1829,14 @@ func TestRetrieveDomainPermissions(t *testing.T) {
|
||||
desc: "retrieve domain permissions with failed to retrieve permissions",
|
||||
token: accessToken,
|
||||
domainID: validID,
|
||||
retreivePermissionsErr: errors.ErrNotFound,
|
||||
retreivePermissionsErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve domain permissions with failed to retrieve by id",
|
||||
token: accessToken,
|
||||
domainID: validID,
|
||||
retreiveByIDErr: errors.ErrNotFound,
|
||||
retreiveByIDErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
@@ -1905,7 +1905,7 @@ func TestUpdateDomain(t *testing.T) {
|
||||
Name: &valid,
|
||||
Alias: &valid,
|
||||
},
|
||||
retrieveByIDErr: errors.ErrNotFound,
|
||||
retrieveByIDErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
@@ -1973,7 +1973,7 @@ func TestChangeDomainStatus(t *testing.T) {
|
||||
domainReq: auth.DomainReq{
|
||||
Status: &disabledStatus,
|
||||
},
|
||||
retreieveByIDErr: errors.ErrNotFound,
|
||||
retreieveByIDErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
@@ -2607,7 +2607,7 @@ func TestListUsersDomains(t *testing.T) {
|
||||
Limit: 10,
|
||||
Permission: auth.AdminPermission,
|
||||
},
|
||||
listDomainErr: errors.ErrNotFound,
|
||||
listDomainErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrViewEntity,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
|
||||
"github.com/authzed/authzed-go/v1"
|
||||
@@ -76,7 +77,7 @@ func (pa *policyAgent) CheckPolicy(ctx context.Context, pr auth.PolicyReq) error
|
||||
if reason, ok := v1.CheckPermissionResponse_Permissionship_name[int32(resp.Permissionship)]; ok {
|
||||
return errors.Wrap(svcerr.ErrAuthorization, errors.New(reason))
|
||||
}
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
|
||||
func (pa *policyAgent) AddPolicies(ctx context.Context, prs []auth.PolicyReq) error {
|
||||
@@ -776,13 +777,13 @@ func convertToGrpcStatus(gst *gstatus.Status) *status.Status {
|
||||
func convertGRPCStatusToError(st *status.Status) error {
|
||||
switch st.Code() {
|
||||
case codes.NotFound:
|
||||
return errors.Wrap(errors.ErrNotFound, errors.New(st.Message()))
|
||||
return errors.Wrap(repoerr.ErrNotFound, errors.New(st.Message()))
|
||||
case codes.InvalidArgument:
|
||||
return errors.Wrap(errors.ErrMalformedEntity, errors.New(st.Message()))
|
||||
case codes.AlreadyExists:
|
||||
return errors.Wrap(errors.ErrConflict, errors.New(st.Message()))
|
||||
return errors.Wrap(repoerr.ErrConflict, errors.New(st.Message()))
|
||||
case codes.Unauthenticated:
|
||||
return errors.Wrap(errors.ErrAuthentication, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrAuthentication, errors.New(st.Message()))
|
||||
case codes.Internal:
|
||||
return errors.Wrap(errInternal, errors.New(st.Message()))
|
||||
case codes.OK:
|
||||
@@ -793,7 +794,7 @@ func convertGRPCStatusToError(st *status.Status) error {
|
||||
case codes.FailedPrecondition:
|
||||
return errors.Wrap(errors.ErrMalformedEntity, errors.New(st.Message()))
|
||||
case codes.PermissionDenied:
|
||||
return errors.Wrap(errors.ErrAuthorization, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrAuthorization, errors.New(st.Message()))
|
||||
default:
|
||||
return errors.Wrap(fmt.Errorf("unexpected gRPC status: %s (status code:%v)", st.Code().String(), st.Code()), errors.New(st.Message()))
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
|
||||
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
@@ -92,7 +93,7 @@ var (
|
||||
|
||||
missingIDRes = toJSON(apiutil.ErrorRes{Err: apiutil.ErrMissingID.Error(), Msg: apiutil.ErrValidation.Error()})
|
||||
missingKeyRes = toJSON(apiutil.ErrorRes{Err: apiutil.ErrBearerKey.Error(), Msg: apiutil.ErrValidation.Error()})
|
||||
bsErrorRes = toJSON(apiutil.ErrorRes{Err: errors.ErrNotFound.Error(), Msg: bootstrap.ErrBootstrap.Error()})
|
||||
bsErrorRes = toJSON(apiutil.ErrorRes{Err: svcerr.ErrNotFound.Error(), Msg: bootstrap.ErrBootstrap.Error()})
|
||||
extKeyRes = toJSON(apiutil.ErrorRes{Msg: bootstrap.ErrExternalKey.Error()})
|
||||
extSecKeyRes = toJSON(apiutil.ErrorRes{Err: "encoding/hex: invalid byte: U+002D '-'", Msg: bootstrap.ErrExternalKeySecure.Error()})
|
||||
)
|
||||
|
||||
@@ -295,11 +295,10 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
||||
errors.Contains(err, bootstrap.ErrExternalKeySecure),
|
||||
errors.Contains(err, svcerr.ErrAuthorization):
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
case errors.Contains(err, svcerr.ErrConflict):
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
case errors.Contains(err, bootstrap.ErrThings):
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
|
||||
case errors.Contains(err, svcerr.ErrConflict):
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
case errors.Contains(err, svcerr.ErrCreateEntity),
|
||||
errors.Contains(err, svcerr.ErrUpdateEntity),
|
||||
errors.Contains(err, svcerr.ErrViewEntity),
|
||||
|
||||
+12
-11
@@ -11,7 +11,8 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/absmach/magistrala/bootstrap"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
)
|
||||
|
||||
const emptyState = -1
|
||||
@@ -39,7 +40,7 @@ func (crm *configRepositoryMock) Save(_ context.Context, config bootstrap.Config
|
||||
|
||||
for _, v := range crm.configs {
|
||||
if v.ThingID == config.ThingID || v.ExternalID == config.ExternalID {
|
||||
return "", errors.ErrConflict
|
||||
return "", repoerr.ErrConflict
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,10 +69,10 @@ func (crm *configRepositoryMock) RetrieveByID(_ context.Context, token, id strin
|
||||
|
||||
c, ok := crm.configs[id]
|
||||
if !ok {
|
||||
return bootstrap.Config{}, errors.ErrNotFound
|
||||
return bootstrap.Config{}, repoerr.ErrNotFound
|
||||
}
|
||||
if c.Owner != token {
|
||||
return bootstrap.Config{}, errors.ErrAuthentication
|
||||
return bootstrap.Config{}, svcerr.ErrAuthentication
|
||||
}
|
||||
|
||||
return c, nil
|
||||
@@ -131,7 +132,7 @@ func (crm *configRepositoryMock) RetrieveByExternalID(_ context.Context, externa
|
||||
}
|
||||
}
|
||||
|
||||
return bootstrap.Config{}, errors.ErrNotFound
|
||||
return bootstrap.Config{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
func (crm *configRepositoryMock) Update(_ context.Context, config bootstrap.Config) error {
|
||||
@@ -140,7 +141,7 @@ func (crm *configRepositoryMock) Update(_ context.Context, config bootstrap.Conf
|
||||
|
||||
cfg, ok := crm.configs[config.ThingID]
|
||||
if !ok || cfg.Owner != config.Owner {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
cfg.Name = config.Name
|
||||
@@ -161,7 +162,7 @@ func (crm *configRepositoryMock) UpdateCert(_ context.Context, owner, thingID, c
|
||||
}
|
||||
}
|
||||
if _, ok := crm.configs[forUpdate.ThingID]; !ok {
|
||||
return bootstrap.Config{}, errors.ErrNotFound
|
||||
return bootstrap.Config{}, repoerr.ErrNotFound
|
||||
}
|
||||
forUpdate.ClientCert = clientCert
|
||||
forUpdate.ClientKey = clientKey
|
||||
@@ -177,7 +178,7 @@ func (crm *configRepositoryMock) UpdateConnections(_ context.Context, token, id
|
||||
|
||||
config, ok := crm.configs[id]
|
||||
if !ok {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
for _, ch := range channels {
|
||||
@@ -188,7 +189,7 @@ func (crm *configRepositoryMock) UpdateConnections(_ context.Context, token, id
|
||||
for _, conn := range connections {
|
||||
ch, ok := crm.channels[conn]
|
||||
if !ok {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
config.Channels = append(config.Channels, ch)
|
||||
}
|
||||
@@ -217,10 +218,10 @@ func (crm *configRepositoryMock) ChangeState(_ context.Context, token, id string
|
||||
|
||||
config, ok := crm.configs[id]
|
||||
if !ok {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
if config.Owner != token {
|
||||
return errors.ErrAuthentication
|
||||
return svcerr.ErrAuthentication
|
||||
}
|
||||
|
||||
config.State = state
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/absmach/magistrala/internal/postgres"
|
||||
"github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
@@ -52,7 +53,7 @@ func (cr configRepository) Save(ctx context.Context, cfg bootstrap.Config, chsCo
|
||||
|
||||
tx, err := cr.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(errors.ErrCreateEntity, err)
|
||||
return "", errors.Wrap(repoerr.ErrCreateEntity, err)
|
||||
}
|
||||
|
||||
dbcfg := toDBConfig(cfg)
|
||||
@@ -60,11 +61,11 @@ func (cr configRepository) Save(ctx context.Context, cfg bootstrap.Config, chsCo
|
||||
if _, err := tx.NamedExec(q, dbcfg); err != nil {
|
||||
e := err
|
||||
if pgErr, ok := err.(*pgconn.PgError); ok && pgErr.Code == pgerrcode.UniqueViolation {
|
||||
e = errors.ErrConflict
|
||||
e = repoerr.ErrConflict
|
||||
}
|
||||
|
||||
cr.rollback("Failed to insert a Config", tx)
|
||||
return "", errors.Wrap(errors.ErrCreateEntity, e)
|
||||
return "", errors.Wrap(repoerr.ErrCreateEntity, e)
|
||||
}
|
||||
|
||||
if err := insertChannels(ctx, cfg.Owner, cfg.Channels, tx); err != nil {
|
||||
@@ -97,14 +98,14 @@ func (cr configRepository) RetrieveByID(ctx context.Context, owner, id string) (
|
||||
row, err := cr.db.NamedQueryContext(ctx, q, dbcfg)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrNotFound, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrNotFound, err)
|
||||
}
|
||||
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
if ok := row.Next(); !ok {
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrNotFound, row.Err())
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrNotFound, row.Err())
|
||||
}
|
||||
|
||||
if err := row.StructScan(&dbcfg); err != nil {
|
||||
@@ -119,7 +120,7 @@ func (cr configRepository) RetrieveByID(ctx context.Context, owner, id string) (
|
||||
rows, err := cr.db.NamedQueryContext(ctx, q, dbcfg)
|
||||
if err != nil {
|
||||
cr.log.Error(fmt.Sprintf("Failed to retrieve connected due to %s", err))
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -128,13 +129,13 @@ func (cr configRepository) RetrieveByID(ctx context.Context, owner, id string) (
|
||||
dbch := dbChannel{}
|
||||
if err := rows.StructScan(&dbch); err != nil {
|
||||
cr.log.Error(fmt.Sprintf("Failed to read connected thing due to %s", err))
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
dbch.Owner = nullString(dbcfg.Owner)
|
||||
|
||||
ch, err := toChannel(dbch)
|
||||
if err != nil {
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
chans = append(chans, ch)
|
||||
}
|
||||
@@ -202,17 +203,17 @@ func (cr configRepository) RetrieveByExternalID(ctx context.Context, externalID
|
||||
row, err := cr.db.NamedQueryContext(ctx, q, dbcfg)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrNotFound, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrNotFound, err)
|
||||
}
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
if ok := row.Next(); !ok {
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrNotFound, row.Err())
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrNotFound, row.Err())
|
||||
}
|
||||
|
||||
if err := row.StructScan(&dbcfg); err != nil {
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
q = `SELECT magistrala_channel, name, metadata FROM channels ch
|
||||
@@ -223,7 +224,7 @@ func (cr configRepository) RetrieveByExternalID(ctx context.Context, externalID
|
||||
rows, err := cr.db.NamedQueryContext(ctx, q, dbcfg)
|
||||
if err != nil {
|
||||
cr.log.Error(fmt.Sprintf("Failed to retrieve connected due to %s", err))
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -232,13 +233,13 @@ func (cr configRepository) RetrieveByExternalID(ctx context.Context, externalID
|
||||
dbch := dbChannel{}
|
||||
if err := rows.StructScan(&dbch); err != nil {
|
||||
cr.log.Error(fmt.Sprintf("Failed to read connected thing due to %s", err))
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
ch, err := toChannel(dbch)
|
||||
if err != nil {
|
||||
cr.log.Error(fmt.Sprintf("Failed to deserialize channel due to %s", err))
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
channels = append(channels, ch)
|
||||
@@ -262,16 +263,16 @@ func (cr configRepository) Update(ctx context.Context, cfg bootstrap.Config) err
|
||||
|
||||
res, err := cr.db.NamedExecContext(ctx, q, dbcfg)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
cnt, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
if cnt == 0 {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -291,12 +292,12 @@ func (cr configRepository) UpdateCert(ctx context.Context, owner, thingID, clien
|
||||
|
||||
row, err := cr.db.NamedQueryContext(ctx, q, dbcfg)
|
||||
if err != nil {
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
defer row.Close()
|
||||
|
||||
if ok := row.Next(); !ok {
|
||||
return bootstrap.Config{}, errors.Wrap(errors.ErrNotFound, row.Err())
|
||||
return bootstrap.Config{}, errors.Wrap(repoerr.ErrNotFound, row.Err())
|
||||
}
|
||||
|
||||
if err := row.StructScan(&dbcfg); err != nil {
|
||||
@@ -309,27 +310,27 @@ func (cr configRepository) UpdateCert(ctx context.Context, owner, thingID, clien
|
||||
func (cr configRepository) UpdateConnections(ctx context.Context, owner, id string, channels []bootstrap.Channel, connections []string) error {
|
||||
tx, err := cr.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
if err := insertChannels(ctx, owner, channels, tx); err != nil {
|
||||
cr.rollback("Failed to insert Channels during the update", tx)
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
if err := updateConnections(ctx, owner, id, connections, tx); err != nil {
|
||||
if e, ok := err.(*pgconn.PgError); ok {
|
||||
if e.Code == pgerrcode.ForeignKeyViolation {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
}
|
||||
cr.rollback("Failed to update connections during the update", tx)
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
cr.rollback("Failed to commit Config update", tx)
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -343,7 +344,7 @@ func (cr configRepository) Remove(ctx context.Context, owner, id string) error {
|
||||
}
|
||||
|
||||
if _, err := cr.db.NamedExecContext(ctx, q, dbcfg); err != nil {
|
||||
return errors.Wrap(errors.ErrRemoveEntity, err)
|
||||
return errors.Wrap(repoerr.ErrRemoveEntity, err)
|
||||
}
|
||||
|
||||
if _, err := cr.db.ExecContext(ctx, cleanupQuery); err != nil {
|
||||
@@ -364,16 +365,16 @@ func (cr configRepository) ChangeState(ctx context.Context, owner, id string, st
|
||||
|
||||
res, err := cr.db.NamedExecContext(ctx, q, dbcfg)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
cnt, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
if cnt == 0 {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -393,14 +394,14 @@ func (cr configRepository) ListExisting(ctx context.Context, owner string, ids [
|
||||
q := "SELECT magistrala_channel, name, metadata FROM channels WHERE owner = $1 AND magistrala_channel = ANY ($2)"
|
||||
rows, err := cr.db.QueryxContext(ctx, q, owner, chans)
|
||||
if err != nil {
|
||||
return []bootstrap.Channel{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return []bootstrap.Channel{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var dbch dbChannel
|
||||
if err := rows.StructScan(&dbch); err != nil {
|
||||
cr.log.Error(fmt.Sprintf("Failed to read retrieved channels due to %s", err))
|
||||
return []bootstrap.Channel{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return []bootstrap.Channel{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
ch, err := toChannel(dbch)
|
||||
@@ -423,7 +424,7 @@ func (cr configRepository) RemoveThing(ctx context.Context, id string) error {
|
||||
cr.log.Warn("Failed to clean dangling channels after removal")
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrRemoveEntity, err)
|
||||
return errors.Wrap(repoerr.ErrRemoveEntity, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -431,7 +432,7 @@ func (cr configRepository) RemoveThing(ctx context.Context, id string) error {
|
||||
func (cr configRepository) UpdateChannel(ctx context.Context, c bootstrap.Channel) error {
|
||||
dbch, err := toDBChannel("", c)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
q := `UPDATE channels SET name = :name, metadata = :metadata, updated_at = :updated_at, updated_by = :updated_by
|
||||
@@ -506,7 +507,7 @@ func insertChannels(_ context.Context, owner string, channels []bootstrap.Channe
|
||||
if _, err := tx.NamedExec(q, chans); err != nil {
|
||||
e := err
|
||||
if pqErr, ok := err.(*pgconn.PgError); ok && pqErr.Code == pgerrcode.UniqueViolation {
|
||||
e = errors.ErrConflict
|
||||
e = repoerr.ErrConflict
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/absmach/magistrala/bootstrap"
|
||||
"github.com/absmach/magistrala/bootstrap/postgres"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -75,19 +76,19 @@ func TestSave(t *testing.T) {
|
||||
desc: "save config with same Thing ID",
|
||||
config: duplicateThing,
|
||||
connections: nil,
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "save config with same external ID",
|
||||
config: duplicateExternal,
|
||||
connections: nil,
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "save config with same Channels",
|
||||
config: duplicateChannels,
|
||||
connections: channels,
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -134,19 +135,19 @@ func TestRetrieveByID(t *testing.T) {
|
||||
desc: "retrieve config with wrong owner",
|
||||
owner: "2",
|
||||
id: id,
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve a non-existing config",
|
||||
owner: c.Owner,
|
||||
id: nonexistentConfID.String(),
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve a config with invalid ID",
|
||||
owner: c.Owner,
|
||||
id: "invalid",
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -260,7 +261,7 @@ func TestRetrieveByExternalID(t *testing.T) {
|
||||
{
|
||||
desc: "retrieve with invalid external ID",
|
||||
externalID: strconv.Itoa(numConfigs + 1),
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve with external key",
|
||||
@@ -305,7 +306,7 @@ func TestUpdate(t *testing.T) {
|
||||
{
|
||||
desc: "update with wrong owner",
|
||||
config: wrongOwner,
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update a config",
|
||||
@@ -359,7 +360,7 @@ func TestUpdateCert(t *testing.T) {
|
||||
ca: "",
|
||||
owner: "wrong",
|
||||
expectedConfig: bootstrap.Config{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update a config",
|
||||
@@ -425,7 +426,7 @@ func TestUpdateConnections(t *testing.T) {
|
||||
id: "unknown",
|
||||
channels: nil,
|
||||
connections: []string{channels[1]},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update connections",
|
||||
@@ -481,7 +482,7 @@ func TestRemove(t *testing.T) {
|
||||
assert.Nil(t, err, fmt.Sprintf("%d: failed to remove config due to: %s", i, err))
|
||||
|
||||
_, err = repo.RetrieveByID(context.Background(), c.Owner, id)
|
||||
assert.True(t, errors.Contains(err, errors.ErrNotFound), fmt.Sprintf("%d: expected %s got %s", i, errors.ErrNotFound, err))
|
||||
assert.True(t, errors.Contains(err, repoerr.ErrNotFound), fmt.Sprintf("%d: expected %s got %s", i, repoerr.ErrNotFound, err))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -512,13 +513,13 @@ func TestChangeState(t *testing.T) {
|
||||
desc: "change state with wrong owner",
|
||||
id: saved,
|
||||
owner: "2",
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "change state with wrong id",
|
||||
id: "wrong",
|
||||
owner: c.Owner,
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "change state to Active",
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
|
||||
)
|
||||
@@ -241,7 +242,7 @@ func (bs bootstrapService) UpdateConnections(ctx context.Context, token, id stri
|
||||
|
||||
for _, c := range disconnect {
|
||||
if err := bs.sdk.DisconnectThing(id, c, token); err != nil {
|
||||
if errors.Contains(err, errors.ErrNotFound) {
|
||||
if errors.Contains(err, repoerr.ErrNotFound) {
|
||||
continue
|
||||
}
|
||||
return ErrThings
|
||||
@@ -330,7 +331,7 @@ func (bs bootstrapService) ChangeState(ctx context.Context, token, id string, st
|
||||
case Inactive:
|
||||
for _, c := range cfg.Channels {
|
||||
if err := bs.sdk.DisconnectThing(cfg.ThingID, c.ID, token); err != nil {
|
||||
if errors.Contains(err, errors.ErrNotFound) {
|
||||
if errors.Contains(err, repoerr.ErrNotFound) {
|
||||
continue
|
||||
}
|
||||
return ErrThings
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/absmach/magistrala/certs"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
)
|
||||
|
||||
var _ certs.Repository = (*certsRepoMock)(nil)
|
||||
@@ -63,7 +63,7 @@ func (c *certsRepoMock) RetrieveAll(ctx context.Context, ownerID string, offset,
|
||||
|
||||
oc, ok := c.certsByThingID[ownerID]
|
||||
if !ok {
|
||||
return certs.Page{}, errors.ErrNotFound
|
||||
return certs.Page{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
var crts []certs.Cert
|
||||
@@ -89,7 +89,7 @@ func (c *certsRepoMock) Remove(ctx context.Context, ownerID, serial string) erro
|
||||
defer c.mu.Unlock()
|
||||
crt, ok := c.certsBySerial[serial]
|
||||
if !ok {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
delete(c.certsBySerial, crt.Serial)
|
||||
delete(c.certsByThingID, crt.ThingID)
|
||||
@@ -105,7 +105,7 @@ func (c *certsRepoMock) RetrieveByThing(ctx context.Context, ownerID, thingID st
|
||||
|
||||
cs, ok := c.certsByThingID[ownerID][thingID]
|
||||
if !ok {
|
||||
return certs.Page{}, errors.ErrNotFound
|
||||
return certs.Page{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
var crts []certs.Cert
|
||||
@@ -130,7 +130,7 @@ func (c *certsRepoMock) RetrieveBySerial(ctx context.Context, ownerID, serialID
|
||||
|
||||
crt, ok := c.certsBySerial[serialID]
|
||||
if !ok {
|
||||
return certs.Cert{}, errors.ErrNotFound
|
||||
return certs.Cert{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return crt, nil
|
||||
|
||||
+2
-1
@@ -20,6 +20,7 @@ import (
|
||||
|
||||
"github.com/absmach/magistrala/certs/pki"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
)
|
||||
|
||||
const keyBits = 2048
|
||||
@@ -151,7 +152,7 @@ func (a *agent) Read(serial string) (pki.Cert, error) {
|
||||
|
||||
crt, ok := a.certs[serial]
|
||||
if !ok {
|
||||
return pki.Cert{}, errors.ErrNotFound
|
||||
return pki.Cert{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return crt, nil
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/absmach/magistrala/certs"
|
||||
"github.com/absmach/magistrala/internal/postgres"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jmoiron/sqlx"
|
||||
@@ -77,7 +78,7 @@ func (cr certsRepository) Save(ctx context.Context, cert certs.Cert) (string, er
|
||||
|
||||
tx, err := cr.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(errors.ErrCreateEntity, err)
|
||||
return "", errors.Wrap(repoerr.ErrCreateEntity, err)
|
||||
}
|
||||
|
||||
dbcrt := toDBCert(cert)
|
||||
@@ -90,7 +91,7 @@ func (cr certsRepository) Save(ctx context.Context, cert certs.Cert) (string, er
|
||||
|
||||
cr.rollback("Failed to insert a Cert", tx, err)
|
||||
|
||||
return "", errors.Wrap(errors.ErrCreateEntity, e)
|
||||
return "", errors.Wrap(repoerr.ErrCreateEntity, e)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
@@ -102,14 +103,14 @@ func (cr certsRepository) Save(ctx context.Context, cert certs.Cert) (string, er
|
||||
|
||||
func (cr certsRepository) Remove(ctx context.Context, ownerID, serial string) error {
|
||||
if _, err := cr.RetrieveBySerial(ctx, ownerID, serial); err != nil {
|
||||
return errors.Wrap(errors.ErrRemoveEntity, err)
|
||||
return errors.Wrap(repoerr.ErrRemoveEntity, err)
|
||||
}
|
||||
q := `DELETE FROM certs WHERE serial = :serial`
|
||||
var c certs.Cert
|
||||
c.Serial = serial
|
||||
dbcrt := toDBCert(c)
|
||||
if _, err := cr.db.NamedExecContext(ctx, q, dbcrt); err != nil {
|
||||
return errors.Wrap(errors.ErrRemoveEntity, err)
|
||||
return errors.Wrap(repoerr.ErrRemoveEntity, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -156,10 +157,10 @@ func (cr certsRepository) RetrieveBySerial(ctx context.Context, ownerID, serialI
|
||||
if err := cr.db.QueryRowxContext(ctx, q, ownerID, serialID).StructScan(&dbcrt); err != nil {
|
||||
pqErr, ok := err.(*pgconn.PgError)
|
||||
if err == sql.ErrNoRows || ok && pgerrcode.InvalidTextRepresentation == pqErr.Code {
|
||||
return c, errors.Wrap(errors.ErrNotFound, err)
|
||||
return c, errors.Wrap(repoerr.ErrNotFound, err)
|
||||
}
|
||||
|
||||
return c, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return c, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
c = toCert(dbcrt)
|
||||
|
||||
|
||||
+2
-2
@@ -32,7 +32,7 @@ import (
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/auth"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/groups"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
"github.com/absmach/magistrala/users"
|
||||
@@ -298,7 +298,7 @@ func createAdminPolicy(ctx context.Context, clientID string, authClient magistra
|
||||
return err
|
||||
}
|
||||
if !addPolicyRes.Added {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
+7
-6
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/messaging"
|
||||
)
|
||||
|
||||
@@ -63,10 +64,10 @@ func (svc *adapterService) Publish(ctx context.Context, key string, msg *messagi
|
||||
}
|
||||
res, err := svc.auth.Authorize(ctx, ar)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrAuthorization, err)
|
||||
return errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
msg.Publisher = res.GetId()
|
||||
|
||||
@@ -83,10 +84,10 @@ func (svc *adapterService) Subscribe(ctx context.Context, key, chanID, subtopic
|
||||
}
|
||||
res, err := svc.auth.Authorize(ctx, ar)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrAuthorization, err)
|
||||
return errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
subject := fmt.Sprintf("%s.%s", chansPrefix, chanID)
|
||||
if subtopic != "" {
|
||||
@@ -111,10 +112,10 @@ func (svc *adapterService) Unsubscribe(ctx context.Context, key, chanID, subtopi
|
||||
}
|
||||
res, err := svc.auth.Authorize(ctx, ar)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrAuthorization, err)
|
||||
return errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
subject := fmt.Sprintf("%s.%s", chansPrefix, chanID)
|
||||
if subtopic != "" {
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/coap"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/messaging"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/plgd-dev/go-coap/v2/message"
|
||||
@@ -98,16 +99,16 @@ func handler(w mux.ResponseWriter, m *mux.Message) {
|
||||
resp.Code = codes.Created
|
||||
err = service.Publish(m.Context, key, msg)
|
||||
default:
|
||||
err = errors.ErrNotFound
|
||||
err = svcerr.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
switch {
|
||||
case err == errBadOptions:
|
||||
resp.Code = codes.BadOption
|
||||
case err == errors.ErrNotFound:
|
||||
case err == svcerr.ErrNotFound:
|
||||
resp.Code = codes.NotFound
|
||||
case errors.Contains(err, errors.ErrAuthorization),
|
||||
errors.Contains(err, errors.ErrAuthentication):
|
||||
case errors.Contains(err, svcerr.ErrAuthorization),
|
||||
errors.Contains(err, svcerr.ErrAuthentication):
|
||||
resp.Code = codes.Unauthorized
|
||||
default:
|
||||
resp.Code = codes.InternalServerError
|
||||
@@ -174,7 +175,7 @@ func parseKey(msg *mux.Message) (string, error) {
|
||||
}
|
||||
vars := strings.Split(authKey, "=")
|
||||
if len(vars) != 2 || vars[0] != authQuery {
|
||||
return "", errors.ErrAuthorization
|
||||
return "", svcerr.ErrAuthorization
|
||||
}
|
||||
return vars[1], nil
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/absmach/magistrala/consumers/notifiers"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
)
|
||||
|
||||
var _ notifiers.SubscriptionsRepository = (*subRepoMock)(nil)
|
||||
@@ -30,11 +30,11 @@ func (srm *subRepoMock) Save(_ context.Context, sub notifiers.Subscription) (str
|
||||
srm.mu.Lock()
|
||||
defer srm.mu.Unlock()
|
||||
if _, ok := srm.subs[sub.ID]; ok {
|
||||
return "", errors.ErrConflict
|
||||
return "", repoerr.ErrConflict
|
||||
}
|
||||
for _, s := range srm.subs {
|
||||
if s.Contact == sub.Contact && s.Topic == sub.Topic {
|
||||
return "", errors.ErrConflict
|
||||
return "", repoerr.ErrConflict
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func (srm *subRepoMock) Retrieve(_ context.Context, id string) (notifiers.Subscr
|
||||
defer srm.mu.Unlock()
|
||||
ret, ok := srm.subs[id]
|
||||
if !ok {
|
||||
return notifiers.Subscription{}, errors.ErrNotFound
|
||||
return notifiers.Subscription{}, repoerr.ErrNotFound
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func (srm *subRepoMock) RetrieveAll(_ context.Context, pm notifiers.PageMetadata
|
||||
}
|
||||
|
||||
if len(subs) == 0 {
|
||||
return notifiers.Page{}, errors.ErrNotFound
|
||||
return notifiers.Page{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
ret := notifiers.Page{
|
||||
@@ -124,7 +124,7 @@ func (srm *subRepoMock) Remove(_ context.Context, id string) error {
|
||||
srm.mu.Lock()
|
||||
defer srm.mu.Unlock()
|
||||
if _, ok := srm.subs[id]; !ok {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
delete(srm.subs, id)
|
||||
return nil
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/absmach/magistrala/consumers/notifiers"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
@@ -41,9 +42,9 @@ func (repo subscriptionsRepo) Save(ctx context.Context, sub notifiers.Subscripti
|
||||
row, err := repo.db.NamedQueryContext(ctx, q, dbSub)
|
||||
if err != nil {
|
||||
if pqErr, ok := err.(*pgconn.PgError); ok && pqErr.Code == pgerrcode.UniqueViolation {
|
||||
return "", errors.Wrap(errors.ErrConflict, err)
|
||||
return "", errors.Wrap(repoerr.ErrConflict, err)
|
||||
}
|
||||
return "", errors.Wrap(errors.ErrCreateEntity, err)
|
||||
return "", errors.Wrap(repoerr.ErrCreateEntity, err)
|
||||
}
|
||||
defer row.Close()
|
||||
|
||||
@@ -55,9 +56,9 @@ func (repo subscriptionsRepo) Retrieve(ctx context.Context, id string) (notifier
|
||||
sub := dbSubscription{}
|
||||
if err := repo.db.QueryRowxContext(ctx, q, id).StructScan(&sub); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return notifiers.Subscription{}, errors.Wrap(errors.ErrNotFound, err)
|
||||
return notifiers.Subscription{}, errors.Wrap(repoerr.ErrNotFound, err)
|
||||
}
|
||||
return notifiers.Subscription{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return notifiers.Subscription{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
return fromDBSub(sub), nil
|
||||
@@ -90,7 +91,7 @@ func (repo subscriptionsRepo) RetrieveAll(ctx context.Context, pm notifiers.Page
|
||||
|
||||
rows, err := repo.db.NamedQueryContext(ctx, q, args)
|
||||
if err != nil {
|
||||
return notifiers.Page{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return notifiers.Page{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -98,19 +99,19 @@ func (repo subscriptionsRepo) RetrieveAll(ctx context.Context, pm notifiers.Page
|
||||
for rows.Next() {
|
||||
sub := dbSubscription{}
|
||||
if err := rows.StructScan(&sub); err != nil {
|
||||
return notifiers.Page{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return notifiers.Page{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
subs = append(subs, fromDBSub(sub))
|
||||
}
|
||||
|
||||
if len(subs) == 0 {
|
||||
return notifiers.Page{}, errors.ErrNotFound
|
||||
return notifiers.Page{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
cq := fmt.Sprintf(`SELECT COUNT(*) FROM subscriptions %s`, condition)
|
||||
total, err := total(ctx, repo.db, cq, args)
|
||||
if err != nil {
|
||||
return notifiers.Page{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return notifiers.Page{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
ret := notifiers.Page{
|
||||
@@ -126,7 +127,7 @@ func (repo subscriptionsRepo) Remove(ctx context.Context, id string) error {
|
||||
q := `DELETE from subscriptions WHERE id = $1`
|
||||
|
||||
if r := repo.db.QueryRowxContext(ctx, q, id); r.Err() != nil {
|
||||
return errors.Wrap(errors.ErrRemoveEntity, r.Err())
|
||||
return errors.Wrap(repoerr.ErrRemoveEntity, r.Err())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/absmach/magistrala/consumers/notifiers"
|
||||
"github.com/absmach/magistrala/consumers/notifiers/postgres"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.opentelemetry.io/otel"
|
||||
@@ -59,7 +60,7 @@ func TestSave(t *testing.T) {
|
||||
desc: "save duplicate",
|
||||
sub: sub2,
|
||||
id: "",
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -104,7 +105,7 @@ func TestView(t *testing.T) {
|
||||
desc: "retrieve not existing",
|
||||
sub: notifiers.Subscription{},
|
||||
id: "non-existing",
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/absmach/magistrala/consumers/notifiers/mocks"
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/messaging"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
@@ -60,7 +61,7 @@ func TestCreateSubscription(t *testing.T) {
|
||||
token: exampleUser1,
|
||||
sub: notifiers.Subscription{Contact: exampleUser1, Topic: "valid.topic"},
|
||||
id: "",
|
||||
err: svcerr.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "test with empty token",
|
||||
|
||||
+2
-1
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/messaging"
|
||||
"github.com/absmach/mproxy/pkg/session"
|
||||
)
|
||||
@@ -156,7 +157,7 @@ func (h *handler) Publish(ctx context.Context, topic *string, payload *[]byte) e
|
||||
return err
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
msg.Publisher = res.GetId()
|
||||
|
||||
|
||||
@@ -124,18 +124,15 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
|
||||
errors.Contains(err, apiutil.ErrValidation):
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
case errors.Contains(err, svcerr.ErrAuthentication),
|
||||
errors.Contains(err, errors.ErrAuthentication),
|
||||
errors.Contains(err, apiutil.ErrBearerToken):
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
case errors.Contains(err, svcerr.ErrNotFound):
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
case errors.Contains(err, svcerr.ErrConflict),
|
||||
errors.Contains(err, postgres.ErrMemberAlreadyAssigned),
|
||||
errors.Contains(err, errors.ErrConflict):
|
||||
case errors.Contains(err, postgres.ErrMemberAlreadyAssigned),
|
||||
errors.Contains(err, svcerr.ErrConflict):
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
case errors.Contains(err, svcerr.ErrAuthorization),
|
||||
errors.Contains(err, errors.ErrAuthorization),
|
||||
errors.Contains(err, errors.ErrDomainAuthorization):
|
||||
errors.Contains(err, svcerr.ErrDomainAuthorization):
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
case errors.Contains(err, apiutil.ErrUnsupportedContentType):
|
||||
w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||
|
||||
@@ -256,7 +256,7 @@ func TestEncodeError(t *testing.T) {
|
||||
desc: "Unauthorized",
|
||||
errs: []error{
|
||||
svcerr.ErrAuthentication,
|
||||
errors.ErrAuthentication,
|
||||
svcerr.ErrAuthentication,
|
||||
apiutil.ErrBearerToken,
|
||||
},
|
||||
code: http.StatusUnauthorized,
|
||||
@@ -273,7 +273,7 @@ func TestEncodeError(t *testing.T) {
|
||||
desc: "Conflict",
|
||||
errs: []error{
|
||||
svcerr.ErrConflict,
|
||||
errors.ErrConflict,
|
||||
svcerr.ErrConflict,
|
||||
},
|
||||
code: http.StatusConflict,
|
||||
},
|
||||
@@ -281,8 +281,8 @@ func TestEncodeError(t *testing.T) {
|
||||
desc: "Forbidden",
|
||||
errs: []error{
|
||||
svcerr.ErrAuthorization,
|
||||
errors.ErrAuthorization,
|
||||
errors.ErrDomainAuthorization,
|
||||
svcerr.ErrAuthorization,
|
||||
svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
code: http.StatusForbidden,
|
||||
},
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -339,11 +340,11 @@ func TestLoggingErrorEncoder(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
desc: "error contains ErrValidation",
|
||||
err: errors.Wrap(apiutil.ErrValidation, errors.ErrAuthentication),
|
||||
err: errors.Wrap(apiutil.ErrValidation, svcerr.ErrAuthentication),
|
||||
},
|
||||
{
|
||||
desc: "error does not contain ErrValidation",
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
"github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/groups"
|
||||
"github.com/absmach/magistrala/pkg/groups/mocks"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -96,9 +97,9 @@ func TestCreateGroupEndpoint(t *testing.T) {
|
||||
},
|
||||
},
|
||||
svcResp: groups.Group{},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: createGroupRes{created: false},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -157,9 +158,9 @@ func TestViewGroupEndpoint(t *testing.T) {
|
||||
id: testsutil.GenerateUUID(t),
|
||||
},
|
||||
svcResp: groups.Group{},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: viewGroupRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -214,9 +215,9 @@ func TestViewGroupPermsEndpoint(t *testing.T) {
|
||||
id: testsutil.GenerateUUID(t),
|
||||
},
|
||||
svcResp: []string{},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: viewGroupPermsRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -269,9 +270,9 @@ func TestEnableGroupEndpoint(t *testing.T) {
|
||||
id: testsutil.GenerateUUID(t),
|
||||
},
|
||||
svcResp: groups.Group{},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: changeStatusRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -324,9 +325,9 @@ func TestDisableGroupEndpoint(t *testing.T) {
|
||||
id: testsutil.GenerateUUID(t),
|
||||
},
|
||||
svcResp: groups.Group{},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: changeStatusRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -376,9 +377,9 @@ func TestDeleteGroupEndpoint(t *testing.T) {
|
||||
token: valid,
|
||||
id: testsutil.GenerateUUID(t),
|
||||
},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: deleteGroupRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -439,9 +440,9 @@ func TestUpdateGroupEndpoint(t *testing.T) {
|
||||
Name: valid,
|
||||
},
|
||||
svcResp: groups.Group{},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: updateGroupRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -574,9 +575,9 @@ func TestListGroupsEndpoint(t *testing.T) {
|
||||
memberID: testsutil.GenerateUUID(t),
|
||||
},
|
||||
svcResp: groups.Page{},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: groupPageRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -676,9 +677,9 @@ func TestListMembersEndpoint(t *testing.T) {
|
||||
groupID: testsutil.GenerateUUID(t),
|
||||
},
|
||||
svcResp: groups.MembersPage{},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: listMembersRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -779,9 +780,9 @@ func TestAssignMembersEndpoint(t *testing.T) {
|
||||
testsutil.GenerateUUID(t),
|
||||
},
|
||||
},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: assignRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -890,9 +891,9 @@ func TestUnassignMembersEndpoint(t *testing.T) {
|
||||
testsutil.GenerateUUID(t),
|
||||
},
|
||||
},
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
resp: unassignRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/groups"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
@@ -270,7 +272,7 @@ func (svc service) listUserGroupPermission(ctx context.Context, userID, groupID
|
||||
return []string{}, err
|
||||
}
|
||||
if len(lp.GetPermissions()) == 0 {
|
||||
return []string{}, errors.ErrAuthorization
|
||||
return []string{}, svcerr.ErrAuthorization
|
||||
}
|
||||
return lp.GetPermissions(), nil
|
||||
}
|
||||
@@ -287,7 +289,7 @@ func (svc service) checkSuperAdmin(ctx context.Context, userID string) error {
|
||||
return err
|
||||
}
|
||||
if !res.Authorized {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -463,7 +465,7 @@ func (svc service) assignParentGroup(ctx context.Context, domain, parentGroupID
|
||||
var deletePolicies magistrala.DeletePoliciesReq
|
||||
for _, group := range groupsPage.Groups {
|
||||
if group.Parent != "" {
|
||||
return errors.Wrap(errors.ErrConflict, fmt.Errorf("%s group already have parent", group.ID))
|
||||
return errors.Wrap(repoerr.ErrConflict, fmt.Errorf("%s group already have parent", group.ID))
|
||||
}
|
||||
addPolicies.AddPoliciesReq = append(addPolicies.AddPoliciesReq, &magistrala.AddPolicyReq{
|
||||
Domain: domain,
|
||||
@@ -509,7 +511,7 @@ func (svc service) unassignParentGroup(ctx context.Context, domain, parentGroupI
|
||||
var deletePolicies magistrala.DeletePoliciesReq
|
||||
for _, group := range groupsPage.Groups {
|
||||
if group.Parent != "" && group.Parent != parentGroupID {
|
||||
return errors.Wrap(errors.ErrConflict, fmt.Errorf("%s group doesn't have same parent", group.ID))
|
||||
return errors.Wrap(repoerr.ErrConflict, fmt.Errorf("%s group doesn't have same parent", group.ID))
|
||||
}
|
||||
addPolicies.AddPoliciesReq = append(addPolicies.AddPoliciesReq, &magistrala.AddPolicyReq{
|
||||
Domain: domain,
|
||||
@@ -707,7 +709,7 @@ func (svc service) identify(ctx context.Context, token string) (*magistrala.Iden
|
||||
return nil, err
|
||||
}
|
||||
if res.GetId() == "" || res.GetDomainId() == "" {
|
||||
return nil, errors.ErrDomainAuthorization
|
||||
return nil, svcerr.ErrDomainAuthorization
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
@@ -726,7 +728,7 @@ func (svc service) authorizeToken(ctx context.Context, subjectType, subject, per
|
||||
return "", err
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return "", errors.ErrAuthorization
|
||||
return "", svcerr.ErrAuthorization
|
||||
}
|
||||
return res.GetId(), nil
|
||||
}
|
||||
@@ -746,7 +748,7 @@ func (svc service) authorizeKind(ctx context.Context, domainID, subjectType, sub
|
||||
return "", err
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return "", errors.ErrAuthorization
|
||||
return "", svcerr.ErrAuthorization
|
||||
}
|
||||
return res.GetId(), nil
|
||||
}
|
||||
|
||||
+116
-114
@@ -19,6 +19,8 @@ import (
|
||||
"github.com/absmach/magistrala/pkg/clients"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
mggroups "github.com/absmach/magistrala/pkg/groups"
|
||||
"github.com/absmach/magistrala/pkg/groups/mocks"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
@@ -97,8 +99,8 @@ func TestCreateGroup(t *testing.T) {
|
||||
kind: auth.NewGroupKind,
|
||||
group: validGroup,
|
||||
idResp: &magistrala.IdentityRes{},
|
||||
idErr: errors.ErrAuthentication,
|
||||
err: errors.ErrAuthentication,
|
||||
idErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "with empty id or domain id but with no grpc error",
|
||||
@@ -107,7 +109,7 @@ func TestCreateGroup(t *testing.T) {
|
||||
group: validGroup,
|
||||
idResp: &magistrala.IdentityRes{},
|
||||
idErr: nil,
|
||||
err: errors.ErrDomainAuthorization,
|
||||
err: svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with failed to authorize domain membership",
|
||||
@@ -121,7 +123,7 @@ func TestCreateGroup(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with failed to authorize domain membership with grpc error",
|
||||
@@ -135,8 +137,8 @@ func TestCreateGroup(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with invalid status",
|
||||
@@ -204,7 +206,7 @@ func TestCreateGroup(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
authzTknResp: &magistrala.AuthorizeRes{},
|
||||
authzTknErr: errors.ErrAuthorization,
|
||||
authzTknErr: svcerr.ErrAuthorization,
|
||||
repoResp: mggroups.Group{
|
||||
ID: testsutil.GenerateUUID(t),
|
||||
Parent: testsutil.GenerateUUID(t),
|
||||
@@ -212,7 +214,7 @@ func TestCreateGroup(t *testing.T) {
|
||||
addPolResp: &magistrala.AddPoliciesRes{
|
||||
Added: true,
|
||||
},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with repo error",
|
||||
@@ -252,8 +254,8 @@ func TestCreateGroup(t *testing.T) {
|
||||
ID: testsutil.GenerateUUID(t),
|
||||
},
|
||||
addPolResp: &magistrala.AddPoliciesRes{},
|
||||
addPolErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
addPolErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -328,8 +330,8 @@ func TestViewGroup(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with failed to authorize",
|
||||
@@ -339,7 +341,7 @@ func TestViewGroup(t *testing.T) {
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: nil,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -400,8 +402,8 @@ func TestViewGroupPerms(t *testing.T) {
|
||||
token: token,
|
||||
id: testsutil.GenerateUUID(t),
|
||||
idResp: &magistrala.IdentityRes{},
|
||||
idErr: errors.ErrAuthentication,
|
||||
err: errors.ErrAuthentication,
|
||||
idErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "with failed to list permissions",
|
||||
@@ -411,8 +413,8 @@ func TestViewGroupPerms(t *testing.T) {
|
||||
Id: testsutil.GenerateUUID(t),
|
||||
DomainId: testsutil.GenerateUUID(t),
|
||||
},
|
||||
listErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with empty permissions",
|
||||
@@ -425,7 +427,7 @@ func TestViewGroupPerms(t *testing.T) {
|
||||
listResp: &magistrala.ListPermissionsRes{
|
||||
Permissions: []string{},
|
||||
},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -486,8 +488,8 @@ func TestUpdateGroup(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with failed to authorize",
|
||||
@@ -500,7 +502,7 @@ func TestUpdateGroup(t *testing.T) {
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: nil,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -562,8 +564,8 @@ func TestEnableGroup(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with failed to authorize",
|
||||
@@ -573,7 +575,7 @@ func TestEnableGroup(t *testing.T) {
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: nil,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with enabled group",
|
||||
@@ -595,8 +597,8 @@ func TestEnableGroup(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
retrieveResp: mggroups.Group{},
|
||||
retrieveErr: errors.ErrNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
retrieveErr: repoerr.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -662,8 +664,8 @@ func TestDisableGroup(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with failed to authorize",
|
||||
@@ -673,7 +675,7 @@ func TestDisableGroup(t *testing.T) {
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: nil,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with enabled group",
|
||||
@@ -695,8 +697,8 @@ func TestDisableGroup(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
retrieveResp: mggroups.Group{},
|
||||
retrieveErr: errors.ErrNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
retrieveErr: repoerr.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -795,7 +797,7 @@ func TestListMembers(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "failed to list objects with things kind",
|
||||
@@ -808,8 +810,8 @@ func TestListMembers(t *testing.T) {
|
||||
listObjectResp: &magistrala.ListObjectsRes{
|
||||
Policies: []string{},
|
||||
},
|
||||
listObjectErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listObjectErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "failed to list subjects with users kind",
|
||||
@@ -823,8 +825,8 @@ func TestListMembers(t *testing.T) {
|
||||
listSubjectResp: &magistrala.ListSubjectsRes{
|
||||
Policies: []string{},
|
||||
},
|
||||
listSubjectErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listSubjectErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1087,8 +1089,8 @@ func TestListGroups(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with users kind admin with nil error",
|
||||
@@ -1106,7 +1108,7 @@ func TestListGroups(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with things kind due to failed to authorize",
|
||||
@@ -1124,8 +1126,8 @@ func TestListGroups(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with things kind due to failed to list subjects",
|
||||
@@ -1144,8 +1146,8 @@ func TestListGroups(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
listSubjectResp: &magistrala.ListSubjectsRes{},
|
||||
listSubjectErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listSubjectErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with things kind due to failed to list filtered objects",
|
||||
@@ -1167,8 +1169,8 @@ func TestListGroups(t *testing.T) {
|
||||
Policies: allowedIDs,
|
||||
},
|
||||
listObjectFilterResp: &magistrala.ListObjectsRes{},
|
||||
listObjectFilterErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listObjectFilterErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to authorize",
|
||||
@@ -1186,8 +1188,8 @@ func TestListGroups(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to list subjects",
|
||||
@@ -1206,8 +1208,8 @@ func TestListGroups(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
listObjectResp: &magistrala.ListObjectsRes{},
|
||||
listObjectErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listObjectErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to list filtered objects",
|
||||
@@ -1229,8 +1231,8 @@ func TestListGroups(t *testing.T) {
|
||||
Policies: allowedIDs,
|
||||
},
|
||||
listObjectFilterResp: &magistrala.ListObjectsRes{},
|
||||
listObjectFilterErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listObjectFilterErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with channels kind due to failed to authorize",
|
||||
@@ -1248,8 +1250,8 @@ func TestListGroups(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with channels kind due to failed to list subjects",
|
||||
@@ -1268,8 +1270,8 @@ func TestListGroups(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
listSubjectResp: &magistrala.ListSubjectsRes{},
|
||||
listSubjectErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listSubjectErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with channels kind due to failed to list filtered objects",
|
||||
@@ -1291,8 +1293,8 @@ func TestListGroups(t *testing.T) {
|
||||
Policies: allowedIDs,
|
||||
},
|
||||
listObjectFilterResp: &magistrala.ListObjectsRes{},
|
||||
listObjectFilterErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listObjectFilterErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with users kind due to failed to authorize",
|
||||
@@ -1310,8 +1312,8 @@ func TestListGroups(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with users kind due to failed to list subjects",
|
||||
@@ -1330,8 +1332,8 @@ func TestListGroups(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
listObjectResp: &magistrala.ListObjectsRes{},
|
||||
listObjectErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listObjectErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with users kind due to failed to list filtered objects",
|
||||
@@ -1353,8 +1355,8 @@ func TestListGroups(t *testing.T) {
|
||||
Policies: allowedIDs,
|
||||
},
|
||||
listObjectFilterResp: &magistrala.ListObjectsRes{},
|
||||
listObjectFilterErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listObjectFilterErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "successfully with users kind admin",
|
||||
@@ -1430,8 +1432,8 @@ func TestListGroups(t *testing.T) {
|
||||
Policies: allowedIDs,
|
||||
},
|
||||
repoResp: mggroups.Page{},
|
||||
repoErr: errors.ErrViewEntity,
|
||||
err: errors.ErrViewEntity,
|
||||
repoErr: repoerr.ErrViewEntity,
|
||||
err: repoerr.ErrViewEntity,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with things kind due to failed to list permissions",
|
||||
@@ -1463,8 +1465,8 @@ func TestListGroups(t *testing.T) {
|
||||
},
|
||||
},
|
||||
listPermResp: &magistrala.ListPermissionsRes{},
|
||||
listPermErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
listPermErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with invalid token",
|
||||
@@ -1476,8 +1478,8 @@ func TestListGroups(t *testing.T) {
|
||||
ListPerms: true,
|
||||
},
|
||||
idResp: &magistrala.IdentityRes{},
|
||||
idErr: errors.ErrAuthentication,
|
||||
err: errors.ErrAuthentication,
|
||||
idErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1736,8 +1738,8 @@ func TestAssign(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
repoResp: mggroups.Page{},
|
||||
repoErr: errors.ErrViewEntity,
|
||||
err: errors.ErrViewEntity,
|
||||
repoErr: repoerr.ErrViewEntity,
|
||||
err: repoerr.ErrViewEntity,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to empty page",
|
||||
@@ -1780,7 +1782,7 @@ func TestAssign(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to add policies",
|
||||
@@ -1806,8 +1808,8 @@ func TestAssign(t *testing.T) {
|
||||
addPoliciesRes: &magistrala.AddPoliciesRes{
|
||||
Added: false,
|
||||
},
|
||||
addPoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
addPoliciesErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to assign parent",
|
||||
@@ -1833,8 +1835,8 @@ func TestAssign(t *testing.T) {
|
||||
addPoliciesRes: &magistrala.AddPoliciesRes{
|
||||
Added: true,
|
||||
},
|
||||
repoParentGroupErr: errors.ErrConflict,
|
||||
err: errors.ErrConflict,
|
||||
repoParentGroupErr: repoerr.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to assign parent and delete policy",
|
||||
@@ -1863,8 +1865,8 @@ func TestAssign(t *testing.T) {
|
||||
deleteParentPoliciesRes: &magistrala.DeletePoliciesRes{
|
||||
Deleted: false,
|
||||
},
|
||||
deleteParentPoliciesErr: errors.ErrAuthorization,
|
||||
repoParentGroupErr: errors.ErrConflict,
|
||||
deleteParentPoliciesErr: svcerr.ErrAuthorization,
|
||||
repoParentGroupErr: repoerr.ErrConflict,
|
||||
err: apiutil.ErrRollbackTx,
|
||||
},
|
||||
{
|
||||
@@ -1891,8 +1893,8 @@ func TestAssign(t *testing.T) {
|
||||
memberKind: auth.UsersKind,
|
||||
memberIDs: allowedIDs,
|
||||
idResp: &magistrala.IdentityRes{},
|
||||
idErr: errors.ErrAuthentication,
|
||||
err: errors.ErrAuthentication,
|
||||
idErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with failed to authorize",
|
||||
@@ -1908,8 +1910,8 @@ func TestAssign(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with failed to add policies",
|
||||
@@ -1928,8 +1930,8 @@ func TestAssign(t *testing.T) {
|
||||
addPoliciesRes: &magistrala.AddPoliciesRes{
|
||||
Added: false,
|
||||
},
|
||||
addPoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
addPoliciesErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2145,8 +2147,8 @@ func TestUnassign(t *testing.T) {
|
||||
Authorized: true,
|
||||
},
|
||||
repoResp: mggroups.Page{},
|
||||
repoErr: errors.ErrViewEntity,
|
||||
err: errors.ErrViewEntity,
|
||||
repoErr: repoerr.ErrViewEntity,
|
||||
err: repoerr.ErrViewEntity,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to empty page",
|
||||
@@ -2189,7 +2191,7 @@ func TestUnassign(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to add policies",
|
||||
@@ -2215,8 +2217,8 @@ func TestUnassign(t *testing.T) {
|
||||
deletePoliciesRes: &magistrala.DeletePoliciesRes{
|
||||
Deleted: false,
|
||||
},
|
||||
deletePoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
deletePoliciesErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to unassign parent",
|
||||
@@ -2242,8 +2244,8 @@ func TestUnassign(t *testing.T) {
|
||||
deletePoliciesRes: &magistrala.DeletePoliciesRes{
|
||||
Deleted: true,
|
||||
},
|
||||
repoParentGroupErr: errors.ErrConflict,
|
||||
err: errors.ErrConflict,
|
||||
repoParentGroupErr: repoerr.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with groups kind due to failed to unassign parent and add policy",
|
||||
@@ -2269,12 +2271,12 @@ func TestUnassign(t *testing.T) {
|
||||
deletePoliciesRes: &magistrala.DeletePoliciesRes{
|
||||
Deleted: true,
|
||||
},
|
||||
repoParentGroupErr: errors.ErrConflict,
|
||||
repoParentGroupErr: repoerr.ErrConflict,
|
||||
addParentPoliciesRes: &magistrala.AddPoliciesRes{
|
||||
Added: false,
|
||||
},
|
||||
addParentPoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrConflict,
|
||||
addParentPoliciesErr: svcerr.ErrAuthorization,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with invalid kind",
|
||||
@@ -2300,8 +2302,8 @@ func TestUnassign(t *testing.T) {
|
||||
memberKind: auth.UsersKind,
|
||||
memberIDs: allowedIDs,
|
||||
idResp: &magistrala.IdentityRes{},
|
||||
idErr: errors.ErrAuthentication,
|
||||
err: errors.ErrAuthentication,
|
||||
idErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with failed to authorize",
|
||||
@@ -2317,8 +2319,8 @@ func TestUnassign(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with failed to add policies",
|
||||
@@ -2337,8 +2339,8 @@ func TestUnassign(t *testing.T) {
|
||||
deletePoliciesRes: &magistrala.DeletePoliciesRes{
|
||||
Deleted: false,
|
||||
},
|
||||
deletePoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
deletePoliciesErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2485,8 +2487,8 @@ func TestDeleteGroup(t *testing.T) {
|
||||
token: token,
|
||||
groupID: testsutil.GenerateUUID(t),
|
||||
idResp: &magistrala.IdentityRes{},
|
||||
idErr: errors.ErrAuthentication,
|
||||
err: errors.ErrAuthentication,
|
||||
idErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with authorization error",
|
||||
@@ -2499,8 +2501,8 @@ func TestDeleteGroup(t *testing.T) {
|
||||
authzResp: &magistrala.AuthorizeRes{
|
||||
Authorized: false,
|
||||
},
|
||||
authzErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
authzErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with failed to remove child group policy",
|
||||
@@ -2516,8 +2518,8 @@ func TestDeleteGroup(t *testing.T) {
|
||||
deleteChildPoliciesRes: &magistrala.DeletePolicyRes{
|
||||
Deleted: false,
|
||||
},
|
||||
deleteChildPoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
deleteChildPoliciesErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with failed to remove things policy",
|
||||
@@ -2536,8 +2538,8 @@ func TestDeleteGroup(t *testing.T) {
|
||||
deleteThingsPoliciesRes: &magistrala.DeletePolicyRes{
|
||||
Deleted: false,
|
||||
},
|
||||
deleteThingsPoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
deleteThingsPoliciesErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with failed to remove domain policy",
|
||||
@@ -2559,8 +2561,8 @@ func TestDeleteGroup(t *testing.T) {
|
||||
deleteDomainsPoliciesRes: &magistrala.DeletePolicyRes{
|
||||
Deleted: false,
|
||||
},
|
||||
deleteDomainsPoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
deleteDomainsPoliciesErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with failed to remove user policy",
|
||||
@@ -2585,8 +2587,8 @@ func TestDeleteGroup(t *testing.T) {
|
||||
deleteUsersPoliciesRes: &magistrala.DeletePolicyRes{
|
||||
Deleted: false,
|
||||
},
|
||||
deleteUsersPoliciesErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
deleteUsersPoliciesErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "unsuccessfully with repo err",
|
||||
@@ -2608,8 +2610,8 @@ func TestDeleteGroup(t *testing.T) {
|
||||
deleteDomainsPoliciesRes: &magistrala.DeletePolicyRes{
|
||||
Deleted: true,
|
||||
},
|
||||
repoErr: errors.ErrNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
repoErr: repoerr.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/absmach/magistrala/invitations/api"
|
||||
"github.com/absmach/magistrala/invitations/mocks"
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
@@ -111,7 +111,7 @@ func TestSendInvitation(t *testing.T) {
|
||||
data: fmt.Sprintf(`{"user_id": "%s", "domain_id": "%s", "relation": "%s"}`, validID, validID, "domain"),
|
||||
status: http.StatusForbidden,
|
||||
contentType: validContenType,
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -283,7 +283,7 @@ func TestListInvitation(t *testing.T) {
|
||||
token: validToken,
|
||||
status: http.StatusForbidden,
|
||||
contentType: validContenType,
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ func TestViewInvitation(t *testing.T) {
|
||||
domainID: validID,
|
||||
status: http.StatusForbidden,
|
||||
contentType: validContenType,
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with empty user_id",
|
||||
@@ -426,7 +426,7 @@ func TestDeleteInvitation(t *testing.T) {
|
||||
domainID: validID,
|
||||
status: http.StatusForbidden,
|
||||
contentType: validContenType,
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "with empty user_id",
|
||||
@@ -507,7 +507,7 @@ func TestAcceptInvitation(t *testing.T) {
|
||||
data: fmt.Sprintf(`{"domain_id": "%s"}`, validID),
|
||||
status: http.StatusForbidden,
|
||||
contentType: validContenType,
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "invalid content type",
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/absmach/magistrala/invitations"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
@@ -23,7 +23,7 @@ func (m *Repository) Create(ctx context.Context, invitation invitations.Invitati
|
||||
ret := m.Called(ctx, invitation)
|
||||
|
||||
if invitation.UserID == Invalid || invitation.DomainID == Invalid || invitation.InvitedBy == Invalid {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Error(0)
|
||||
@@ -33,7 +33,7 @@ func (m *Repository) Retrieve(ctx context.Context, userID, domainID string) (inv
|
||||
ret := m.Called(ctx, userID, domainID)
|
||||
|
||||
if userID == Invalid || domainID == Invalid {
|
||||
return invitations.Invitation{}, errors.ErrNotFound
|
||||
return invitations.Invitation{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Get(0).(invitations.Invitation), ret.Error(1)
|
||||
@@ -43,7 +43,7 @@ func (m *Repository) RetrieveAll(ctx context.Context, page invitations.Page) (in
|
||||
ret := m.Called(ctx, page)
|
||||
|
||||
if page.UserID == Invalid || page.DomainID == Invalid {
|
||||
return invitations.InvitationPage{}, errors.ErrNotFound
|
||||
return invitations.InvitationPage{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Get(0).(invitations.InvitationPage), ret.Error(1)
|
||||
@@ -53,7 +53,7 @@ func (m *Repository) UpdateToken(ctx context.Context, invitation invitations.Inv
|
||||
ret := m.Called(ctx, invitation)
|
||||
|
||||
if invitation.UserID == Invalid || invitation.DomainID == Invalid {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Error(0)
|
||||
@@ -63,7 +63,7 @@ func (m *Repository) UpdateConfirmation(ctx context.Context, invitation invitati
|
||||
ret := m.Called(ctx, invitation)
|
||||
|
||||
if invitation.UserID == Invalid || invitation.DomainID == Invalid {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Error(0)
|
||||
@@ -73,7 +73,7 @@ func (m *Repository) Delete(ctx context.Context, userID, domainID string) error
|
||||
ret := m.Called(ctx, userID, domainID)
|
||||
|
||||
if userID == Invalid || domainID == Invalid {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Error(0)
|
||||
|
||||
@@ -7,7 +7,8 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/absmach/magistrala/invitations"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
@@ -21,7 +22,7 @@ func (svc *Service) SendInvitation(ctx context.Context, token string, invitation
|
||||
ret := svc.Called(ctx, token, invitation)
|
||||
|
||||
if token == Invalid || invitation.UserID == Invalid || invitation.DomainID == Invalid || invitation.InvitedBy == Invalid {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Error(0)
|
||||
@@ -31,7 +32,7 @@ func (svc *Service) ViewInvitation(ctx context.Context, token, userID, domainID
|
||||
ret := svc.Called(ctx, token, userID, domainID)
|
||||
|
||||
if token == Invalid || userID == Invalid || domainID == Invalid {
|
||||
return invitations.Invitation{}, errors.ErrNotFound
|
||||
return invitations.Invitation{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Get(0).(invitations.Invitation), ret.Error(1)
|
||||
@@ -41,7 +42,7 @@ func (svc *Service) ListInvitations(ctx context.Context, token string, page invi
|
||||
ret := svc.Called(ctx, token, page)
|
||||
|
||||
if token == Invalid {
|
||||
return invitations.InvitationPage{}, errors.ErrAuthentication
|
||||
return invitations.InvitationPage{}, svcerr.ErrAuthentication
|
||||
}
|
||||
|
||||
return ret.Get(0).(invitations.InvitationPage), ret.Error(1)
|
||||
@@ -51,7 +52,7 @@ func (svc *Service) AcceptInvitation(ctx context.Context, token, domainID string
|
||||
ret := svc.Called(ctx, token, domainID)
|
||||
|
||||
if token == Invalid {
|
||||
return errors.ErrAuthentication
|
||||
return svcerr.ErrAuthentication
|
||||
}
|
||||
|
||||
return ret.Error(0)
|
||||
@@ -61,11 +62,11 @@ func (svc *Service) DeleteInvitation(ctx context.Context, token, userID, domainI
|
||||
ret := svc.Called(ctx, token, userID, domainID)
|
||||
|
||||
if token == Invalid {
|
||||
return errors.ErrAuthentication
|
||||
return svcerr.ErrAuthentication
|
||||
}
|
||||
|
||||
if userID == Invalid || domainID == Invalid {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return ret.Error(0)
|
||||
|
||||
+2
-1
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/mqtt/events"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/messaging"
|
||||
"github.com/absmach/mproxy/pkg/session"
|
||||
)
|
||||
@@ -235,7 +236,7 @@ func (h *handler) authAccess(ctx context.Context, password, topic, action string
|
||||
return err
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/absmach/magistrala/mqtt"
|
||||
"github.com/absmach/magistrala/mqtt/mocks"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/mproxy/pkg/session"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
@@ -197,7 +198,7 @@ func TestAuthSubscribe(t *testing.T) {
|
||||
{
|
||||
desc: "subscribe with invalid channel ID",
|
||||
session: &sessionClient,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
topic: &invalidChanIDTopics,
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1353,7 +1353,7 @@ func TestUpdate(t *testing.T) {
|
||||
"update": namegen.Generate(),
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update name for enabled client",
|
||||
@@ -1371,7 +1371,7 @@ func TestUpdate(t *testing.T) {
|
||||
ID: client2.ID,
|
||||
Name: namegen.Generate(),
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update name and metadata for enabled client",
|
||||
@@ -1395,7 +1395,7 @@ func TestUpdate(t *testing.T) {
|
||||
"update": namegen.Generate(),
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update metadata for invalid client",
|
||||
@@ -1406,7 +1406,7 @@ func TestUpdate(t *testing.T) {
|
||||
"update": namegen.Generate(),
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update name for invalid client",
|
||||
@@ -1415,7 +1415,7 @@ func TestUpdate(t *testing.T) {
|
||||
ID: testsutil.GenerateUUID(t),
|
||||
Name: namegen.Generate(),
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update name and metadata for invalid client",
|
||||
@@ -1427,7 +1427,7 @@ func TestUpdate(t *testing.T) {
|
||||
"update": namegen.Generate(),
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update metadata for empty client",
|
||||
@@ -1437,7 +1437,7 @@ func TestUpdate(t *testing.T) {
|
||||
"update": namegen.Generate(),
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update name for empty client",
|
||||
@@ -1445,7 +1445,7 @@ func TestUpdate(t *testing.T) {
|
||||
client: mgclients.Client{
|
||||
Name: namegen.Generate(),
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "update name and metadata for empty client",
|
||||
@@ -1456,7 +1456,7 @@ func TestUpdate(t *testing.T) {
|
||||
"update": namegen.Generate(),
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
@@ -1511,7 +1511,7 @@ func TestUpdateTags(t *testing.T) {
|
||||
ID: client2.ID,
|
||||
Tags: namegen.GenerateNames(5),
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for invalid client",
|
||||
@@ -1519,12 +1519,12 @@ func TestUpdateTags(t *testing.T) {
|
||||
ID: testsutil.GenerateUUID(t),
|
||||
Tags: namegen.GenerateNames(5),
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for empty client",
|
||||
client: mgclients.Client{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
@@ -1575,7 +1575,7 @@ func TestUpdateSecret(t *testing.T) {
|
||||
Secret: "newpassword",
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for invalid client",
|
||||
@@ -1585,12 +1585,12 @@ func TestUpdateSecret(t *testing.T) {
|
||||
Secret: "newpassword",
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for empty client",
|
||||
client: mgclients.Client{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
@@ -1643,7 +1643,7 @@ func TestUpdateIdentity(t *testing.T) {
|
||||
Identity: namegen.Generate() + emailSuffix,
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for invalid client",
|
||||
@@ -1653,12 +1653,12 @@ func TestUpdateIdentity(t *testing.T) {
|
||||
Identity: namegen.Generate() + emailSuffix,
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for empty client",
|
||||
client: mgclients.Client{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
@@ -1713,12 +1713,12 @@ func TestChangeStatus(t *testing.T) {
|
||||
ID: testsutil.GenerateUUID(t),
|
||||
Status: mgclients.DisabledStatus,
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for empty client",
|
||||
client: mgclients.Client{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1766,7 +1766,7 @@ func TestUpdateRole(t *testing.T) {
|
||||
ID: client2.ID,
|
||||
Role: mgclients.AdminRole,
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for invalid client",
|
||||
@@ -1774,12 +1774,12 @@ func TestUpdateRole(t *testing.T) {
|
||||
ID: testsutil.GenerateUUID(t),
|
||||
Role: mgclients.AdminRole,
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "for empty client",
|
||||
client: mgclients.Client{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,17 @@ import "github.com/absmach/magistrala/pkg/errors"
|
||||
// Wrapper for Service errors.
|
||||
var (
|
||||
// ErrAuthentication indicates failure occurred while authenticating the entity.
|
||||
ErrAuthentication = errors.New("authentication error")
|
||||
ErrAuthentication = errors.New("failed to perform authentication over the entity")
|
||||
|
||||
// ErrAuthorization indicates failure occurred while authorizing the entity.
|
||||
ErrAuthorization = errors.New("failed to perform authorization over the entity")
|
||||
|
||||
// ErrDomainAuthorization indicates failure occurred while authorizing the domain.
|
||||
ErrDomainAuthorization = errors.New("failed to perform authorization over the domain")
|
||||
|
||||
// ErrLogin indicates wrong login credentials.
|
||||
ErrLogin = errors.New("invalid user id or secret")
|
||||
|
||||
// ErrMalformedEntity indicates a malformed entity specification.
|
||||
ErrMalformedEntity = errors.New("malformed entity specification")
|
||||
|
||||
@@ -34,9 +40,6 @@ var (
|
||||
// ErrUpdateEntity indicates error in updating entity or entities.
|
||||
ErrUpdateEntity = errors.New("update entity failed")
|
||||
|
||||
// ErrUniqueID indicates an error in generating a unique ID.
|
||||
ErrUniqueID = errors.New("failed to generate unique identifier")
|
||||
|
||||
// ErrInvalidStatus indicates an invalid status.
|
||||
ErrInvalidStatus = errors.New("invalid status")
|
||||
|
||||
|
||||
@@ -6,45 +6,9 @@ package errors
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrAuthentication indicates failure occurred while authenticating the entity.
|
||||
ErrAuthentication = New("failed to perform authentication over the entity")
|
||||
|
||||
// ErrAuthorization indicates failure occurred while authorizing the entity.
|
||||
ErrAuthorization = New("failed to perform authorization over the entity")
|
||||
|
||||
// ErrDomainAuthorization indicates failure occurred while authorizing the domain.
|
||||
ErrDomainAuthorization = New("failed to perform authorization over the domain")
|
||||
|
||||
// ErrMalformedEntity indicates a malformed entity specification.
|
||||
ErrMalformedEntity = New("malformed entity specification")
|
||||
|
||||
// ErrNotFound indicates a non-existent entity request.
|
||||
ErrNotFound = New("entity not found")
|
||||
|
||||
// ErrConflict indicates that entity already exists.
|
||||
ErrConflict = New("entity already exists")
|
||||
|
||||
// ErrCreateEntity indicates error in creating entity or entities.
|
||||
ErrCreateEntity = New("failed to create entity in the db")
|
||||
|
||||
// ErrViewEntity indicates error in viewing entity or entities.
|
||||
ErrViewEntity = New("view entity failed")
|
||||
|
||||
// ErrUpdateEntity indicates error in updating entity or entities.
|
||||
ErrUpdateEntity = New("update entity failed")
|
||||
|
||||
// ErrRemoveEntity indicates error in removing entity.
|
||||
ErrRemoveEntity = New("failed to remove entity")
|
||||
|
||||
// ErrScanMetadata indicates problem with metadata in db.
|
||||
ErrScanMetadata = New("failed to scan metadata in db")
|
||||
|
||||
// ErrWrongSecret indicates a wrong secret was provided.
|
||||
ErrWrongSecret = New("wrong secret")
|
||||
|
||||
// ErrLogin indicates wrong login credentials.
|
||||
ErrLogin = New("invalid user id or secret")
|
||||
|
||||
// ErrUnsupportedContentType indicates invalid content type.
|
||||
ErrUnsupportedContentType = errors.New("invalid content type")
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
sdk "github.com/absmach/magistrala/pkg/sdk/go"
|
||||
thmocks "github.com/absmach/magistrala/things/mocks"
|
||||
@@ -261,7 +262,7 @@ func TestViewCertByThing(t *testing.T) {
|
||||
desc: "get non-existent cert",
|
||||
thingID: "43",
|
||||
token: token,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, errors.ErrNotFound), http.StatusInternalServerError),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, repoerr.ErrNotFound), http.StatusInternalServerError),
|
||||
response: sdk.Subscription{},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
mggroups "github.com/absmach/magistrala/pkg/groups"
|
||||
"github.com/absmach/magistrala/pkg/groups/mocks"
|
||||
@@ -109,7 +110,7 @@ func TestCreateChannel(t *testing.T) {
|
||||
Status: mgclients.EnabledStatus.String(),
|
||||
},
|
||||
token: token,
|
||||
err: errors.NewSDKErrorWithStatus(errors.ErrCreateEntity, http.StatusInternalServerError),
|
||||
err: errors.NewSDKErrorWithStatus(repoerr.ErrCreateEntity, http.StatusInternalServerError),
|
||||
},
|
||||
{
|
||||
desc: "create channel with missing name",
|
||||
@@ -655,7 +656,7 @@ func TestEnableChannel(t *testing.T) {
|
||||
}
|
||||
|
||||
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{}, errors.ErrNotFound)
|
||||
repoCall1 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
|
||||
repoCall2 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
|
||||
_, err := mgsdk.EnableChannel("wrongID", validToken)
|
||||
assert.Equal(t, errors.NewSDKErrorWithStatus(svcerr.ErrNotFound, http.StatusNotFound), err, fmt.Sprintf("Enable channel with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
|
||||
@@ -708,7 +709,7 @@ func TestDisableChannel(t *testing.T) {
|
||||
|
||||
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{}, errors.ErrNotFound)
|
||||
repoCall2 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
|
||||
_, err := mgsdk.DisableChannel("wrongID", validToken)
|
||||
assert.Equal(t, err, errors.NewSDKErrorWithStatus(svcerr.ErrNotFound, http.StatusNotFound), fmt.Sprintf("Disable channel with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
|
||||
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
mggroups "github.com/absmach/magistrala/pkg/groups"
|
||||
"github.com/absmach/magistrala/pkg/groups/mocks"
|
||||
@@ -789,7 +790,7 @@ func TestEnableGroup(t *testing.T) {
|
||||
}
|
||||
|
||||
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{}, errors.ErrNotFound)
|
||||
repoCall1 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
|
||||
repoCall2 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
|
||||
_, err := mgsdk.EnableGroup("wrongID", validToken)
|
||||
assert.Equal(t, err, errors.NewSDKErrorWithStatus(svcerr.ErrNotFound, http.StatusNotFound), fmt.Sprintf("Enable group with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
|
||||
@@ -843,7 +844,7 @@ func TestDisableGroup(t *testing.T) {
|
||||
|
||||
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{}, errors.ErrNotFound)
|
||||
repoCall2 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
|
||||
_, err := mgsdk.DisableGroup("wrongID", validToken)
|
||||
assert.Equal(t, err, errors.NewSDKErrorWithStatus(svcerr.ErrNotFound, http.StatusNotFound), fmt.Sprintf("Disable group with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
|
||||
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestSendMessage(t *testing.T) {
|
||||
mgsdk := sdk.NewSDK(sdkConf)
|
||||
|
||||
auth.On("Authorize", mock.Anything, &magistrala.AuthorizeReq{Subject: atoken, Object: chanID, Domain: "", SubjectType: "thing", Permission: "publish", ObjectType: "group"}).Return(&magistrala.AuthorizeRes{Authorized: true, Id: ""}, nil)
|
||||
auth.On("Authorize", mock.Anything, &magistrala.AuthorizeReq{Subject: invalidToken, Object: chanID, Domain: "", SubjectType: "thing", Permission: "publish", ObjectType: "group"}).Return(&magistrala.AuthorizeRes{Authorized: true, Id: ""}, errors.ErrAuthentication)
|
||||
auth.On("Authorize", mock.Anything, &magistrala.AuthorizeReq{Subject: invalidToken, Object: chanID, Domain: "", SubjectType: "thing", Permission: "publish", ObjectType: "group"}).Return(&magistrala.AuthorizeRes{Authorized: true, Id: ""}, svcerr.ErrAuthentication)
|
||||
auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false, Id: ""}, nil)
|
||||
|
||||
cases := map[string]struct {
|
||||
@@ -76,13 +76,13 @@ func TestSendMessage(t *testing.T) {
|
||||
chanID: chanID,
|
||||
msg: msg,
|
||||
auth: "",
|
||||
err: errors.NewSDKErrorWithStatus(errors.ErrAuthorization, http.StatusBadRequest),
|
||||
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthorization, http.StatusBadRequest),
|
||||
},
|
||||
"publish message with invalid authorization token": {
|
||||
chanID: chanID,
|
||||
msg: msg,
|
||||
auth: invalidToken,
|
||||
err: errors.NewSDKErrorWithStatus(errors.ErrAuthentication, http.StatusBadRequest),
|
||||
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusBadRequest),
|
||||
},
|
||||
"publish message with wrong content type": {
|
||||
chanID: chanID,
|
||||
|
||||
+22
-22
@@ -361,7 +361,7 @@ func TestListThings(t *testing.T) {
|
||||
token: authmocks.InvalidValue,
|
||||
offset: offset,
|
||||
limit: limit,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrAuthentication, errors.ErrAuthentication), http.StatusUnauthorized),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
|
||||
response: nil,
|
||||
},
|
||||
{
|
||||
@@ -369,7 +369,7 @@ func TestListThings(t *testing.T) {
|
||||
token: "",
|
||||
offset: offset,
|
||||
limit: limit,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrAuthentication, errors.ErrAuthentication), http.StatusUnauthorized),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
|
||||
response: nil,
|
||||
},
|
||||
{
|
||||
@@ -464,9 +464,9 @@ func TestListThings(t *testing.T) {
|
||||
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
|
||||
repoCall2 := auth.On("ListAllObjects", mock.Anything, mock.Anything).Return(&magistrala.ListObjectsRes{Policies: toIDs(tc.response)}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
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: false}, svcerr.ErrAuthorization)
|
||||
repoCall2 = auth.On("ListAllObjects", mock.Anything, mock.Anything).Return(&magistrala.ListObjectsRes{}, errors.ErrAuthorization)
|
||||
repoCall2 = auth.On("ListAllObjects", mock.Anything, mock.Anything).Return(&magistrala.ListObjectsRes{}, svcerr.ErrAuthorization)
|
||||
}
|
||||
repoCall3 := cRepo.On("RetrieveAllByIDs", mock.Anything, mock.Anything).Return(mgclients.ClientsPage{Page: convertClientPage(pm), Clients: convertThings(tc.response...)}, tc.err)
|
||||
page, err := mgsdk.Things(pm, validToken)
|
||||
@@ -573,7 +573,7 @@ func TestListThingsByChannel(t *testing.T) {
|
||||
channelID: testsutil.GenerateUUID(t),
|
||||
page: sdk.PageMetadata{},
|
||||
response: []sdk.Thing(nil),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
|
||||
},
|
||||
{
|
||||
desc: "list things with an invalid id",
|
||||
@@ -646,7 +646,7 @@ func TestThing(t *testing.T) {
|
||||
response: sdk.Thing{},
|
||||
token: validToken,
|
||||
thingID: wrongID,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrNotFound, errors.ErrNotFound), http.StatusNotFound),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(repoerr.ErrNotFound, repoerr.ErrNotFound), http.StatusNotFound),
|
||||
},
|
||||
{
|
||||
desc: "view thing with an invalid token and invalid thing id",
|
||||
@@ -660,7 +660,7 @@ func TestThing(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
|
||||
}
|
||||
repoCall1 := cRepo.On("RetrieveByID", mock.Anything, tc.thingID).Return(convertThing(tc.response), tc.err)
|
||||
rClient, err := mgsdk.Thing(tc.thingID, tc.token)
|
||||
@@ -745,7 +745,7 @@ func TestUpdateThing(t *testing.T) {
|
||||
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)
|
||||
if tc.token != validToken {
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
|
||||
}
|
||||
repoCall2 := cRepo.On("Update", mock.Anything, mock.Anything).Return(convertThing(tc.response), tc.err)
|
||||
uClient, err := mgsdk.UpdateThing(tc.thing, tc.token)
|
||||
@@ -830,7 +830,7 @@ func TestUpdateThingTags(t *testing.T) {
|
||||
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)
|
||||
if tc.token != validToken {
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
|
||||
}
|
||||
repoCall2 := cRepo.On("UpdateTags", mock.Anything, mock.Anything).Return(convertThing(tc.response), tc.err)
|
||||
uClient, err := mgsdk.UpdateThingTags(tc.thing, tc.token)
|
||||
@@ -883,8 +883,8 @@ func TestUpdateThingSecret(t *testing.T) {
|
||||
newSecret: "newPassword",
|
||||
token: "non-existent",
|
||||
response: sdk.Thing{},
|
||||
repoErr: errors.ErrAuthorization,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrUpdateEntity, errors.ErrAuthorization), http.StatusForbidden),
|
||||
repoErr: svcerr.ErrAuthorization,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrUpdateEntity, svcerr.ErrAuthorization), http.StatusForbidden),
|
||||
},
|
||||
{
|
||||
desc: "update thing secret with wrong old secret",
|
||||
@@ -900,7 +900,7 @@ func TestUpdateThingSecret(t *testing.T) {
|
||||
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)
|
||||
if tc.token != validToken {
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
|
||||
}
|
||||
repoCall2 := cRepo.On("UpdateSecret", mock.Anything, mock.Anything).Return(convertThing(tc.response), tc.repoErr)
|
||||
uClient, err := mgsdk.UpdateThingSecret(tc.oldSecret, tc.newSecret, tc.token)
|
||||
@@ -965,7 +965,7 @@ func TestEnableThing(t *testing.T) {
|
||||
thing: sdk.Thing{},
|
||||
response: sdk.Thing{},
|
||||
repoErr: sdk.ErrFailedEnable,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(sdk.ErrFailedEnable, errors.ErrNotFound), http.StatusNotFound),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(sdk.ErrFailedEnable, repoerr.ErrNotFound), http.StatusNotFound),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -973,7 +973,7 @@ func TestEnableThing(t *testing.T) {
|
||||
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)
|
||||
if tc.token != validToken {
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
|
||||
}
|
||||
repoCall2 := cRepo.On("RetrieveByID", mock.Anything, tc.id).Return(convertThing(tc.thing), tc.repoErr)
|
||||
repoCall3 := cRepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(convertThing(tc.response), tc.repoErr)
|
||||
@@ -1036,7 +1036,7 @@ func TestEnableThing(t *testing.T) {
|
||||
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: true}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall1 = 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.Things)}, nil)
|
||||
repoCall3 := cRepo.On("RetrieveAllByIDs", mock.Anything, mock.Anything).Return(convertThingsPage(tc.response), nil)
|
||||
@@ -1100,7 +1100,7 @@ func TestDisableThing(t *testing.T) {
|
||||
token: validToken,
|
||||
response: sdk.Thing{},
|
||||
repoErr: sdk.ErrFailedDisable,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(sdk.ErrFailedDisable, errors.ErrNotFound), http.StatusNotFound),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(sdk.ErrFailedDisable, repoerr.ErrNotFound), http.StatusNotFound),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1108,7 +1108,7 @@ func TestDisableThing(t *testing.T) {
|
||||
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)
|
||||
if tc.token != validToken {
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
|
||||
}
|
||||
repoCall2 := cRepo.On("RetrieveByID", mock.Anything, tc.id).Return(convertThing(tc.thing), tc.repoErr)
|
||||
repoCall3 := cRepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(convertThing(tc.response), tc.repoErr)
|
||||
@@ -1173,7 +1173,7 @@ func TestDisableThing(t *testing.T) {
|
||||
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: true}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall1 = 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.Things)}, nil)
|
||||
repoCall3 := cRepo.On("RetrieveAllByIDs", mock.Anything, mock.Anything).Return(convertThingsPage(tc.response), nil)
|
||||
@@ -1218,15 +1218,15 @@ func TestShareThing(t *testing.T) {
|
||||
channelID: generateUUID(t),
|
||||
thingID: "thingID",
|
||||
token: invalidToken,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
|
||||
},
|
||||
{
|
||||
desc: "share thing with valid token for unauthorized user",
|
||||
channelID: generateUUID(t),
|
||||
thingID: "thingID",
|
||||
token: validToken,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrAuthorization, errors.ErrAuthorization), http.StatusForbidden),
|
||||
repoErr: errors.ErrAuthorization,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthorization, svcerr.ErrAuthorization), http.StatusForbidden),
|
||||
repoErr: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1235,7 +1235,7 @@ func TestShareThing(t *testing.T) {
|
||||
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, tc.repoErr)
|
||||
repoCall2 := auth.On("AddPolicies", mock.Anything, mock.Anything).Return(&magistrala.AddPoliciesRes{Added: true}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization)
|
||||
repoCall1 = auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization)
|
||||
}
|
||||
repoCall3 := auth.On("AddPolicy", mock.Anything, mock.Anything).Return(&magistrala.AddPolicyRes{Added: true}, nil)
|
||||
req := sdk.UsersRelationRequest{
|
||||
|
||||
+15
-15
@@ -182,7 +182,7 @@ func TestCreateClient(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{UserId: validID}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, svcerr.ErrAuthentication)
|
||||
}
|
||||
repoCall1 := auth.On("AddPolicies", mock.Anything, mock.Anything).Return(&magistrala.AddPoliciesRes{Added: true}, nil)
|
||||
repoCall2 := auth.On("DeletePolicies", mock.Anything, mock.Anything).Return(&magistrala.DeletePoliciesRes{Deleted: true}, nil)
|
||||
@@ -261,7 +261,7 @@ func TestListClients(t *testing.T) {
|
||||
token: invalidToken,
|
||||
offset: offset,
|
||||
limit: limit,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrNotFound, errors.ErrNotFound), http.StatusNotFound),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(repoerr.ErrNotFound, repoerr.ErrNotFound), http.StatusNotFound),
|
||||
response: nil,
|
||||
},
|
||||
{
|
||||
@@ -269,7 +269,7 @@ func TestListClients(t *testing.T) {
|
||||
token: "",
|
||||
offset: offset,
|
||||
limit: limit,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrNotFound, errors.ErrNotFound), http.StatusNotFound),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(repoerr.ErrNotFound, repoerr.ErrNotFound), http.StatusNotFound),
|
||||
response: nil,
|
||||
},
|
||||
{
|
||||
@@ -277,7 +277,7 @@ func TestListClients(t *testing.T) {
|
||||
token: token,
|
||||
offset: offset,
|
||||
limit: 0,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrNotFound, errors.ErrNotFound), http.StatusNotFound),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(repoerr.ErrNotFound, repoerr.ErrNotFound), http.StatusNotFound),
|
||||
response: nil,
|
||||
},
|
||||
{
|
||||
@@ -430,7 +430,7 @@ func TestClient(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{UserId: validID}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
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 := crepo.On("RetrieveByID", mock.Anything, tc.clientID).Return(convertClient(tc.response), tc.err)
|
||||
@@ -489,7 +489,7 @@ func TestProfile(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{UserId: validID}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, svcerr.ErrAuthentication)
|
||||
}
|
||||
repoCal1 := crepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(convertClient(tc.response), tc.err)
|
||||
rClient, err := mgsdk.UserProfile(tc.token)
|
||||
@@ -578,7 +578,7 @@ func TestUpdateClient(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{UserId: validID}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
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 := crepo.On("Update", mock.Anything, mock.Anything).Return(convertClient(tc.response), tc.err)
|
||||
@@ -670,7 +670,7 @@ func TestUpdateClientTags(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{UserId: validID}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
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 := crepo.On("UpdateTags", mock.Anything, mock.Anything).Return(convertClient(tc.response), tc.err)
|
||||
@@ -760,7 +760,7 @@ func TestUpdateClientIdentity(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{UserId: validID}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
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 := crepo.On("UpdateIdentity", mock.Anything, mock.Anything).Return(convertClient(tc.response), tc.err)
|
||||
@@ -816,8 +816,8 @@ func TestUpdateClientSecret(t *testing.T) {
|
||||
newSecret: "newPassword",
|
||||
token: "non-existent",
|
||||
response: sdk.User{},
|
||||
repoErr: errors.ErrAuthentication,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, errors.ErrAuthentication), http.StatusUnauthorized),
|
||||
repoErr: svcerr.ErrAuthentication,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
|
||||
},
|
||||
{
|
||||
desc: "update client secret with wrong old secret",
|
||||
@@ -833,7 +833,7 @@ func TestUpdateClientSecret(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{UserId: user.ID}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, svcerr.ErrAuthentication)
|
||||
}
|
||||
repoCall1 := auth.On("Issue", mock.Anything, mock.Anything).Return(&magistrala.Token{AccessToken: validToken}, nil)
|
||||
repoCall2 := crepo.On("RetrieveByID", mock.Anything, user.ID).Return(convertClient(tc.response), tc.repoErr)
|
||||
@@ -927,7 +927,7 @@ func TestUpdateClientRole(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: validToken}).Return(&magistrala.IdentityRes{UserId: validID}, nil)
|
||||
if tc.token != validToken {
|
||||
repoCall = auth.On("Identify", mock.Anything, mock.Anything).Return(&magistrala.IdentityRes{}, errors.ErrAuthentication)
|
||||
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)
|
||||
@@ -999,7 +999,7 @@ func TestEnableClient(t *testing.T) {
|
||||
client: sdk.User{},
|
||||
response: sdk.User{},
|
||||
repoErr: sdk.ErrFailedEnable,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(sdk.ErrFailedEnable, errors.ErrNotFound), http.StatusNotFound),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(sdk.ErrFailedEnable, repoerr.ErrNotFound), http.StatusNotFound),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1128,7 +1128,7 @@ func TestDisableClient(t *testing.T) {
|
||||
token: validToken,
|
||||
response: sdk.User{},
|
||||
repoErr: sdk.ErrFailedDisable,
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(sdk.ErrFailedDisable, errors.ErrNotFound), http.StatusNotFound),
|
||||
err: errors.NewSDKErrorWithStatus(errors.Wrap(sdk.ErrFailedDisable, repoerr.ErrNotFound), http.StatusNotFound),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/provision"
|
||||
"github.com/absmach/magistrala/provision/api"
|
||||
"github.com/absmach/magistrala/provision/mocks"
|
||||
@@ -126,7 +126,7 @@ func TestProvision(t *testing.T) {
|
||||
data: fmt.Sprintf(`{"name": "test", "external_id": "%s", "external_key": "%s"}`, validID, validID),
|
||||
status: http.StatusForbidden,
|
||||
contentType: validContenType,
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ func TestMapping(t *testing.T) {
|
||||
token: validToken,
|
||||
status: http.StatusForbidden,
|
||||
contentType: validContenType,
|
||||
svcErr: errors.ErrAuthorization,
|
||||
svcErr: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
sdk "github.com/absmach/magistrala/pkg/sdk/go"
|
||||
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
|
||||
"github.com/absmach/magistrala/provision"
|
||||
@@ -41,7 +43,7 @@ func TestMapping(t *testing.T) {
|
||||
desc: "invalid token",
|
||||
token: "invalid",
|
||||
content: map[string]interface{}{},
|
||||
sdkerr: errors.NewSDKErrorWithStatus(errors.ErrAuthentication, 401),
|
||||
sdkerr: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, 401),
|
||||
err: provision.ErrUnauthorized,
|
||||
},
|
||||
}
|
||||
@@ -138,7 +140,7 @@ func TestCert(t *testing.T) {
|
||||
key: "",
|
||||
sdkThingErr: nil,
|
||||
sdkCertErr: nil,
|
||||
sdkTokenErr: errors.NewSDKErrorWithStatus(errors.ErrAuthentication, 401),
|
||||
sdkTokenErr: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, 401),
|
||||
err: provision.ErrFailedToCreateToken,
|
||||
},
|
||||
{
|
||||
@@ -165,7 +167,7 @@ func TestCert(t *testing.T) {
|
||||
ttl: "1h",
|
||||
cert: "",
|
||||
key: "",
|
||||
sdkThingErr: errors.NewSDKErrorWithStatus(errors.ErrAuthentication, 401),
|
||||
sdkThingErr: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, 401),
|
||||
sdkCertErr: nil,
|
||||
sdkTokenErr: nil,
|
||||
err: provision.ErrUnauthorized,
|
||||
@@ -178,7 +180,7 @@ func TestCert(t *testing.T) {
|
||||
ttl: "1h",
|
||||
cert: "",
|
||||
key: "",
|
||||
sdkThingErr: errors.NewSDKErrorWithStatus(errors.ErrNotFound, 404),
|
||||
sdkThingErr: errors.NewSDKErrorWithStatus(repoerr.ErrNotFound, 404),
|
||||
sdkCertErr: nil,
|
||||
sdkTokenErr: nil,
|
||||
err: provision.ErrUnauthorized,
|
||||
@@ -193,8 +195,8 @@ func TestCert(t *testing.T) {
|
||||
key: "",
|
||||
sdkThingErr: nil,
|
||||
sdkTokenErr: nil,
|
||||
sdkCertErr: errors.NewSDKError(errors.ErrCreateEntity),
|
||||
err: errors.ErrCreateEntity,
|
||||
sdkCertErr: errors.NewSDKError(repoerr.ErrCreateEntity),
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/readers"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
)
|
||||
@@ -21,7 +22,7 @@ func listMessagesEndpoint(svc readers.MessageRepository, uauth magistrala.AuthSe
|
||||
}
|
||||
|
||||
if err := authorize(ctx, req, uauth, taauth); err != nil {
|
||||
return nil, errors.Wrap(errors.ErrAuthorization, err)
|
||||
return nil, errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
|
||||
page, err := svc.ReadAll(req.chanID, req.pageMeta)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
kitgrpc "github.com/go-kit/kit/transport/grpc"
|
||||
"google.golang.org/grpc"
|
||||
@@ -78,17 +79,17 @@ func decodeError(err error) error {
|
||||
if st, ok := status.FromError(err); ok {
|
||||
switch st.Code() {
|
||||
case codes.Unauthenticated:
|
||||
return errors.Wrap(errors.ErrAuthentication, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrAuthentication, errors.New(st.Message()))
|
||||
case codes.PermissionDenied:
|
||||
return errors.Wrap(errors.ErrAuthorization, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrAuthorization, errors.New(st.Message()))
|
||||
case codes.InvalidArgument:
|
||||
return errors.Wrap(errors.ErrMalformedEntity, errors.New(st.Message()))
|
||||
case codes.FailedPrecondition:
|
||||
return errors.Wrap(errors.ErrMalformedEntity, errors.New(st.Message()))
|
||||
case codes.NotFound:
|
||||
return errors.Wrap(errors.ErrNotFound, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrNotFound, errors.New(st.Message()))
|
||||
case codes.AlreadyExists:
|
||||
return errors.Wrap(errors.ErrConflict, errors.New(st.Message()))
|
||||
return errors.Wrap(svcerr.ErrConflict, errors.New(st.Message()))
|
||||
case codes.OK:
|
||||
if msg := st.Message(); msg != "" {
|
||||
return errors.Wrap(errors.ErrUnidentified, errors.New(msg))
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
grpcapi "github.com/absmach/magistrala/things/api/grpc"
|
||||
"github.com/absmach/magistrala/things/mocks"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -87,7 +88,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.GroupType,
|
||||
},
|
||||
res: &magistrala.AuthorizeRes{},
|
||||
err: errors.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "authorize with missing ID",
|
||||
@@ -113,7 +114,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.GroupType,
|
||||
},
|
||||
res: &magistrala.AuthorizeRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "authorize with invalid permission",
|
||||
@@ -126,7 +127,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.GroupType,
|
||||
},
|
||||
res: &magistrala.AuthorizeRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "authorize with invalid channel ID",
|
||||
@@ -139,7 +140,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.GroupType,
|
||||
},
|
||||
res: &magistrala.AuthorizeRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "authorize with empty channel ID",
|
||||
@@ -152,7 +153,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.GroupType,
|
||||
},
|
||||
res: &magistrala.AuthorizeRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "authorize with empty permission",
|
||||
@@ -165,7 +166,7 @@ func TestAuthorize(t *testing.T) {
|
||||
ObjectType: auth.GroupType,
|
||||
},
|
||||
res: &magistrala.AuthorizeRes{},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -65,13 +65,12 @@ func encodeError(err error) error {
|
||||
err == apiutil.ErrMissingPolicyObj,
|
||||
err == apiutil.ErrMalformedPolicyAct:
|
||||
return status.Error(codes.InvalidArgument, err.Error())
|
||||
case errors.Contains(err, errors.ErrAuthentication),
|
||||
case errors.Contains(err, svcerr.ErrAuthentication),
|
||||
errors.Contains(err, auth.ErrKeyExpired),
|
||||
err == apiutil.ErrMissingEmail,
|
||||
err == apiutil.ErrBearerToken:
|
||||
return status.Error(codes.Unauthenticated, err.Error())
|
||||
case errors.Contains(err, errors.ErrAuthorization),
|
||||
errors.Contains(err, svcerr.ErrAuthorization):
|
||||
case errors.Contains(err, svcerr.ErrAuthorization):
|
||||
return status.Error(codes.PermissionDenied, err.Error())
|
||||
default:
|
||||
return status.Error(codes.Internal, err.Error())
|
||||
|
||||
@@ -129,7 +129,7 @@ func TestCreateThing(t *testing.T) {
|
||||
token: validToken,
|
||||
contentType: contentType,
|
||||
status: http.StatusConflict,
|
||||
err: errors.ErrConflict,
|
||||
err: svcerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "register a new thing with an empty token",
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
)
|
||||
|
||||
type createClientReq struct {
|
||||
@@ -310,7 +311,7 @@ type connectChannelThingRequest struct {
|
||||
|
||||
func (req *connectChannelThingRequest) validate() error {
|
||||
if req.ThingID == "" || req.ChannelID == "" {
|
||||
return errors.ErrCreateEntity
|
||||
return svcerr.ErrCreateEntity
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -323,7 +324,7 @@ type disconnectChannelThingRequest struct {
|
||||
|
||||
func (req *disconnectChannelThingRequest) validate() error {
|
||||
if req.ThingID == "" || req.ChannelID == "" {
|
||||
return errors.ErrCreateEntity
|
||||
return svcerr.ErrCreateEntity
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -340,7 +341,7 @@ func (req *thingShareRequest) validate() error {
|
||||
return errors.ErrMalformedEntity
|
||||
}
|
||||
if req.Relation == "" || len(req.UserIDs) == 0 {
|
||||
return errors.ErrCreateEntity
|
||||
return svcerr.ErrCreateEntity
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -357,7 +358,7 @@ func (req *thingUnshareRequest) validate() error {
|
||||
return errors.ErrMalformedEntity
|
||||
}
|
||||
if req.Relation == "" || len(req.UserIDs) == 0 {
|
||||
return errors.ErrCreateEntity
|
||||
return svcerr.ErrCreateEntity
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -731,7 +732,7 @@ func TestConnectChannelThingRequestValidate(t *testing.T) {
|
||||
ChannelID: "",
|
||||
ThingID: validID,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: svcerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "empty thing id",
|
||||
@@ -740,7 +741,7 @@ func TestConnectChannelThingRequestValidate(t *testing.T) {
|
||||
ChannelID: validID,
|
||||
ThingID: "",
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: svcerr.ErrCreateEntity,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
@@ -771,7 +772,7 @@ func TestDisconnectChannelThingRequestValidate(t *testing.T) {
|
||||
ChannelID: "",
|
||||
ThingID: validID,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: svcerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "empty thing id",
|
||||
@@ -780,7 +781,7 @@ func TestDisconnectChannelThingRequestValidate(t *testing.T) {
|
||||
ChannelID: validID,
|
||||
ThingID: "",
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: svcerr.ErrCreateEntity,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
@@ -823,7 +824,7 @@ func TestThingShareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{},
|
||||
Relation: valid,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: svcerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "empty relation",
|
||||
@@ -833,7 +834,7 @@ func TestThingShareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{validID},
|
||||
Relation: "",
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: svcerr.ErrCreateEntity,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
@@ -876,7 +877,7 @@ func TestThingUnshareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{},
|
||||
Relation: valid,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: svcerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "empty relation",
|
||||
@@ -886,7 +887,7 @@ func TestThingUnshareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{validID},
|
||||
Relation: "",
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: svcerr.ErrCreateEntity,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
|
||||
Vendored
+8
-7
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/absmach/magistrala/things"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
@@ -35,16 +36,16 @@ func NewCache(client *redis.Client, duration time.Duration) things.Cache {
|
||||
|
||||
func (tc *thingCache) Save(ctx context.Context, thingKey, thingID string) error {
|
||||
if thingKey == "" || thingID == "" {
|
||||
return errors.Wrap(errors.ErrCreateEntity, errors.New("thing key or thing id is empty"))
|
||||
return errors.Wrap(repoerr.ErrCreateEntity, errors.New("thing key or thing id is empty"))
|
||||
}
|
||||
tkey := fmt.Sprintf("%s:%s", keyPrefix, thingKey)
|
||||
if err := tc.client.Set(ctx, tkey, thingID, tc.keyDuration).Err(); err != nil {
|
||||
return errors.Wrap(errors.ErrCreateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrCreateEntity, err)
|
||||
}
|
||||
|
||||
tid := fmt.Sprintf("%s:%s", idPrefix, thingID)
|
||||
if err := tc.client.Set(ctx, tid, thingKey, tc.keyDuration).Err(); err != nil {
|
||||
return errors.Wrap(errors.ErrCreateEntity, err)
|
||||
return errors.Wrap(repoerr.ErrCreateEntity, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -52,13 +53,13 @@ func (tc *thingCache) Save(ctx context.Context, thingKey, thingID string) error
|
||||
|
||||
func (tc *thingCache) ID(ctx context.Context, thingKey string) (string, error) {
|
||||
if thingKey == "" {
|
||||
return "", errors.ErrNotFound
|
||||
return "", repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
tkey := fmt.Sprintf("%s:%s", keyPrefix, thingKey)
|
||||
thingID, err := tc.client.Get(ctx, tkey).Result()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(errors.ErrNotFound, err)
|
||||
return "", errors.Wrap(repoerr.ErrNotFound, err)
|
||||
}
|
||||
|
||||
return thingID, nil
|
||||
@@ -72,12 +73,12 @@ func (tc *thingCache) Remove(ctx context.Context, thingID string) error {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrRemoveEntity, err)
|
||||
return errors.Wrap(repoerr.ErrRemoveEntity, err)
|
||||
}
|
||||
|
||||
tkey := fmt.Sprintf("%s:%s", keyPrefix, key)
|
||||
if err := tc.client.Del(ctx, tkey, tid).Err(); err != nil {
|
||||
return errors.Wrap(errors.ErrRemoveEntity, err)
|
||||
return errors.Wrap(repoerr.ErrRemoveEntity, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Vendored
+9
-8
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/absmach/magistrala/things/cache"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -55,31 +56,31 @@ func TestSave(t *testing.T) {
|
||||
desc: "Save thing with long key ",
|
||||
key: strings.Repeat("a", 513*1024*1024),
|
||||
id: testID,
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "Save thing with long id ",
|
||||
key: testKey,
|
||||
id: strings.Repeat("a", 513*1024*1024),
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "Save thing with empty key",
|
||||
key: "",
|
||||
id: testID,
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "Save thing with empty id",
|
||||
key: testKey,
|
||||
id: "",
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "Save thing with empty key and id",
|
||||
key: "",
|
||||
id: "",
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -117,13 +118,13 @@ func TestID(t *testing.T) {
|
||||
desc: "Get thing ID from cache for non existing thing",
|
||||
key: "nonExistingKey",
|
||||
id: "",
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "Get thing ID from cache for empty key",
|
||||
key: "",
|
||||
id: "",
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -167,7 +168,7 @@ func TestRemove(t *testing.T) {
|
||||
{
|
||||
desc: "Remove thing with long id from cache",
|
||||
key: strings.Repeat("a", 513*1024*1024),
|
||||
err: errors.ErrRemoveEntity,
|
||||
err: repoerr.ErrRemoveEntity,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ func TestClientsSave(t *testing.T) {
|
||||
Metadata: clients.Metadata{},
|
||||
Status: clients.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "add new client with duplicate secret",
|
||||
@@ -89,7 +89,7 @@ func TestClientsSave(t *testing.T) {
|
||||
Metadata: clients.Metadata{},
|
||||
Status: clients.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "add new client without domain id",
|
||||
@@ -118,7 +118,7 @@ func TestClientsSave(t *testing.T) {
|
||||
Metadata: clients.Metadata{},
|
||||
Status: clients.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "add client with invalid client name",
|
||||
@@ -133,7 +133,7 @@ func TestClientsSave(t *testing.T) {
|
||||
Metadata: clients.Metadata{},
|
||||
Status: clients.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "add client with invalid client domain id",
|
||||
@@ -147,7 +147,7 @@ func TestClientsSave(t *testing.T) {
|
||||
Metadata: clients.Metadata{},
|
||||
Status: clients.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "add client with invalid client identity",
|
||||
@@ -161,7 +161,7 @@ func TestClientsSave(t *testing.T) {
|
||||
Metadata: clients.Metadata{},
|
||||
Status: clients.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrCreateEntity,
|
||||
err: repoerr.ErrCreateEntity,
|
||||
},
|
||||
{
|
||||
desc: "add client with a missing client identity",
|
||||
@@ -253,13 +253,13 @@ func TestClientsRetrieveBySecret(t *testing.T) {
|
||||
desc: "retrieve client by invalid secret",
|
||||
secret: "non-existent-secret",
|
||||
response: clients.Client{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve client by empty secret",
|
||||
secret: "",
|
||||
response: clients.Client{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -80,14 +80,14 @@ func (svc service) CreateThings(ctx context.Context, token string, cls ...mgclie
|
||||
if c.ID == "" {
|
||||
clientID, err := svc.idProvider.ID()
|
||||
if err != nil {
|
||||
return []mgclients.Client{}, errors.Wrap(svcerr.ErrUniqueID, err)
|
||||
return []mgclients.Client{}, err
|
||||
}
|
||||
c.ID = clientID
|
||||
}
|
||||
if c.Credentials.Secret == "" {
|
||||
key, err := svc.idProvider.ID()
|
||||
if err != nil {
|
||||
return []mgclients.Client{}, errors.Wrap(svcerr.ErrUniqueID, err)
|
||||
return []mgclients.Client{}, err
|
||||
}
|
||||
c.Credentials.Secret = key
|
||||
}
|
||||
@@ -154,7 +154,7 @@ func (svc service) ViewClientPerms(ctx context.Context, token, id string) ([]str
|
||||
return nil, err
|
||||
}
|
||||
if len(permissions) == 0 {
|
||||
return nil, errors.ErrAuthorization
|
||||
return nil, svcerr.ErrAuthorization
|
||||
}
|
||||
return permissions, nil
|
||||
}
|
||||
@@ -587,10 +587,10 @@ func (svc service) Identify(ctx context.Context, key string) (string, error) {
|
||||
func (svc service) identify(ctx context.Context, token string) (*magistrala.IdentityRes, error) {
|
||||
res, err := svc.auth.Identify(ctx, &magistrala.IdentityReq{Token: token})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.ErrAuthentication, err)
|
||||
return nil, errors.Wrap(svcerr.ErrAuthentication, err)
|
||||
}
|
||||
if res.GetId() == "" || res.GetDomainId() == "" {
|
||||
return nil, errors.ErrDomainAuthorization
|
||||
return nil, svcerr.ErrDomainAuthorization
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
@@ -610,7 +610,7 @@ func (svc *service) authorize(ctx context.Context, domainID, subjType, subjKind,
|
||||
return "", err
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return "", errors.Wrap(errors.ErrAuthorization, err)
|
||||
return "", errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
|
||||
return res.GetId(), nil
|
||||
|
||||
+16
-16
@@ -84,8 +84,8 @@ func TestCreateThings(t *testing.T) {
|
||||
thing: client,
|
||||
token: validToken,
|
||||
authResponse: &magistrala.AuthorizeRes{Authorized: true},
|
||||
saveErr: errors.ErrConflict,
|
||||
err: svcerr.ErrConflict,
|
||||
saveErr: repoerr.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "create a new thing without secret",
|
||||
@@ -482,7 +482,7 @@ func TestListClients(t *testing.T) {
|
||||
},
|
||||
token: validToken,
|
||||
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: ""},
|
||||
err: errors.ErrDomainAuthorization,
|
||||
err: svcerr.ErrDomainAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "list all clients as non admin with failed to retrieve all",
|
||||
@@ -1622,8 +1622,8 @@ func TestDeleteClient(t *testing.T) {
|
||||
token: authmocks.InvalidValue,
|
||||
clientID: client.ID,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
err: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
desc: "Delete invalid client",
|
||||
@@ -1632,7 +1632,7 @@ func TestDeleteClient(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
authorizeErr: svcerr.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "Delete client with repo error ",
|
||||
@@ -1642,8 +1642,8 @@ func TestDeleteClient(t *testing.T) {
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
|
||||
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: true},
|
||||
deletePolicyResponse1: &magistrala.DeletePolicyRes{Deleted: true},
|
||||
deleteErr: errors.ErrRemoveEntity,
|
||||
err: errors.ErrRemoveEntity,
|
||||
deleteErr: repoerr.ErrRemoveEntity,
|
||||
err: repoerr.ErrRemoveEntity,
|
||||
},
|
||||
{
|
||||
desc: "Delete client with cache error ",
|
||||
@@ -1652,7 +1652,7 @@ func TestDeleteClient(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
|
||||
removeErr: svcerr.ErrRemoveEntity,
|
||||
err: errors.ErrRemoveEntity,
|
||||
err: repoerr.ErrRemoveEntity,
|
||||
},
|
||||
{
|
||||
desc: "Delete client with failed to delete groups policy",
|
||||
@@ -1974,7 +1974,7 @@ func TestIdentify(t *testing.T) {
|
||||
desc: "identify client with valid key from repo",
|
||||
key: valid,
|
||||
cacheIDResponse: "",
|
||||
cacheIDErr: errors.ErrNotFound,
|
||||
cacheIDErr: repoerr.ErrNotFound,
|
||||
repoIDResponse: client,
|
||||
err: nil,
|
||||
},
|
||||
@@ -1982,16 +1982,16 @@ func TestIdentify(t *testing.T) {
|
||||
desc: "identify client with invalid key",
|
||||
key: invalid,
|
||||
cacheIDResponse: "",
|
||||
cacheIDErr: errors.ErrNotFound,
|
||||
cacheIDErr: repoerr.ErrNotFound,
|
||||
repoIDResponse: mgclients.Client{},
|
||||
retrieveBySecretErr: errors.ErrNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
retrieveBySecretErr: repoerr.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "identify client with failed to save to cache",
|
||||
key: valid,
|
||||
cacheIDResponse: "",
|
||||
cacheIDErr: errors.ErrNotFound,
|
||||
cacheIDErr: repoerr.ErrNotFound,
|
||||
repoIDResponse: client,
|
||||
saveErr: errors.ErrMalformedEntity,
|
||||
err: svcerr.ErrAuthorization,
|
||||
@@ -2037,8 +2037,8 @@ func TestAuthorize(t *testing.T) {
|
||||
key: invalid,
|
||||
request: &magistrala.AuthorizeReq{Subject: invalid, Object: inValidToken, Permission: "admin"},
|
||||
response: &magistrala.AuthorizeRes{Authorized: false},
|
||||
identifyErr: errors.ErrNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
identifyErr: repoerr.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "authorize with invalid token",
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/absmach/magistrala"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
|
||||
@@ -216,7 +216,6 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
case errors.Contains(err, svcerr.ErrConflict):
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
|
||||
case errors.Contains(err, svcerr.ErrCreateEntity),
|
||||
errors.Contains(err, svcerr.ErrUpdateEntity),
|
||||
errors.Contains(err, svcerr.ErrViewEntity),
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
"github.com/absmach/magistrala/twins"
|
||||
)
|
||||
@@ -35,7 +35,7 @@ func (trm *twinRepositoryMock) Save(ctx context.Context, twin twins.Twin) (strin
|
||||
|
||||
for _, tw := range trm.twins {
|
||||
if tw.ID == twin.ID {
|
||||
return "", errors.ErrConflict
|
||||
return "", repoerr.ErrConflict
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func (trm *twinRepositoryMock) Update(ctx context.Context, twin twins.Twin) erro
|
||||
|
||||
dbKey := key(twin.Owner, twin.ID)
|
||||
if _, ok := trm.twins[dbKey]; !ok {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
trm.twins[dbKey] = twin
|
||||
@@ -68,7 +68,7 @@ func (trm *twinRepositoryMock) RetrieveByID(_ context.Context, twinID string) (t
|
||||
}
|
||||
}
|
||||
|
||||
return twins.Twin{}, errors.ErrNotFound
|
||||
return twins.Twin{}, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
func (trm *twinRepositoryMock) RetrieveByAttribute(ctx context.Context, channel, subtopic string) ([]string, error) {
|
||||
|
||||
+10
-9
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/absmach/magistrala/twins"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
@@ -39,7 +40,7 @@ func (tr *twinRepository) Save(ctx context.Context, tw twins.Twin) (string, erro
|
||||
coll := tr.db.Collection(twinsCollection)
|
||||
|
||||
if _, err := coll.InsertOne(ctx, tw); err != nil {
|
||||
return "", errors.Wrap(errors.ErrCreateEntity, err)
|
||||
return "", errors.Wrap(repoerr.ErrCreateEntity, err)
|
||||
}
|
||||
|
||||
return tw.ID, nil
|
||||
@@ -60,7 +61,7 @@ func (tr *twinRepository) Update(ctx context.Context, tw twins.Twin) error {
|
||||
}
|
||||
|
||||
if res.ModifiedCount < 1 {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -72,7 +73,7 @@ func (tr *twinRepository) RetrieveByID(ctx context.Context, twinID string) (twin
|
||||
|
||||
filter := bson.M{"id": twinID}
|
||||
if err := coll.FindOne(ctx, filter).Decode(&tw); err != nil {
|
||||
return tw, errors.ErrNotFound
|
||||
return tw, repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return tw, nil
|
||||
@@ -108,7 +109,7 @@ func (tr *twinRepository) RetrieveByAttribute(ctx context.Context, channel, subt
|
||||
|
||||
cur, err := coll.Aggregate(ctx, []bson.M{prj1, match, prj2}, findOptions)
|
||||
if err != nil {
|
||||
return []string{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return []string{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
|
||||
@@ -151,17 +152,17 @@ func (tr *twinRepository) RetrieveAll(ctx context.Context, owner string, offset,
|
||||
}
|
||||
cur, err := coll.Find(ctx, filter, findOptions)
|
||||
if err != nil {
|
||||
return twins.Page{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return twins.Page{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
results, err := decodeTwins(ctx, cur)
|
||||
if err != nil {
|
||||
return twins.Page{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return twins.Page{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
total, err := coll.CountDocuments(ctx, filter)
|
||||
if err != nil {
|
||||
return twins.Page{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return twins.Page{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
return twins.Page{
|
||||
@@ -180,11 +181,11 @@ func (tr *twinRepository) Remove(ctx context.Context, twinID string) error {
|
||||
filter := bson.M{"id": twinID}
|
||||
res, err := coll.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrRemoveEntity, err)
|
||||
return errors.Wrap(repoerr.ErrRemoveEntity, err)
|
||||
}
|
||||
|
||||
if res.DeletedCount < 1 {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"testing"
|
||||
|
||||
mglog "github.com/absmach/magistrala/logger"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
"github.com/absmach/magistrala/twins"
|
||||
@@ -378,7 +377,7 @@ func TestTwinsRemove(t *testing.T) {
|
||||
{
|
||||
desc: "remove a non-existing twin",
|
||||
id: nonexistentTwinID,
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -110,7 +110,7 @@ func (ts *twinsService) AddTwin(ctx context.Context, token string, twin Twin, de
|
||||
|
||||
twin.ID, err = ts.idProvider.ID()
|
||||
if err != nil {
|
||||
return Twin{}, errors.Wrap(svcerr.ErrUniqueID, err)
|
||||
return Twin{}, err
|
||||
}
|
||||
|
||||
twin.Owner = res.GetId()
|
||||
|
||||
@@ -128,7 +128,7 @@ func TestRegisterClient(t *testing.T) {
|
||||
token: validToken,
|
||||
contentType: contentType,
|
||||
status: http.StatusConflict,
|
||||
err: errors.ErrConflict,
|
||||
err: svcerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "register a new user with an empty token",
|
||||
@@ -1093,7 +1093,7 @@ func TestPasswordResetRequest(t *testing.T) {
|
||||
data: fmt.Sprintf(`{"email": "%s", "host": "%s"}`, "invalid", testhost),
|
||||
contentType: contentType,
|
||||
status: http.StatusNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "password reset with malformed data",
|
||||
|
||||
@@ -5,6 +5,7 @@ package mocks
|
||||
|
||||
import (
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/users"
|
||||
)
|
||||
|
||||
@@ -27,7 +28,7 @@ func (hm *hasherMock) Hash(pwd string) (string, error) {
|
||||
|
||||
func (hm *hasherMock) Compare(plain, hashed string) error {
|
||||
if plain != hashed {
|
||||
return errors.ErrAuthentication
|
||||
return svcerr.ErrAuthentication
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
pgclients "github.com/absmach/magistrala/pkg/clients/postgres"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
)
|
||||
|
||||
var _ mgclients.Repository = (*clientRepo)(nil)
|
||||
@@ -78,17 +79,17 @@ func (repo clientRepo) CheckSuperAdmin(ctx context.Context, adminID string) erro
|
||||
q := "SELECT 1 FROM clients WHERE id = $1 AND role = $2"
|
||||
rows, err := repo.DB.QueryContext(ctx, q, adminID, mgclients.AdminRole)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.ErrAuthorization, err)
|
||||
return errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
if err := rows.Err(); err != nil {
|
||||
return errors.Wrap(errors.ErrAuthorization, err)
|
||||
return errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
|
||||
func (repo clientRepo) RetrieveByID(ctx context.Context, id string) (mgclients.Client, error) {
|
||||
@@ -125,7 +126,7 @@ func (repo clientRepo) RetrieveByID(ctx context.Context, id string) (mgclients.C
|
||||
func (repo clientRepo) RetrieveAll(ctx context.Context, pm mgclients.Page) (mgclients.ClientsPage, error) {
|
||||
query, err := pgclients.PageQuery(pm)
|
||||
if err != nil {
|
||||
return mgclients.ClientsPage{}, errors.Wrap(errors.ErrViewEntity, err)
|
||||
return mgclients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
|
||||
}
|
||||
|
||||
q := fmt.Sprintf(`SELECT c.id, c.name, c.tags, c.identity, c.metadata, c.status, c.role,
|
||||
@@ -181,17 +182,17 @@ func (repo clientRepo) UpdateRole(ctx context.Context, client mgclients.Client)
|
||||
|
||||
dbc, err := pgclients.ToDBClient(client)
|
||||
if err != nil {
|
||||
return mgclients.Client{}, errors.Wrap(errors.ErrUpdateEntity, err)
|
||||
return mgclients.Client{}, errors.Wrap(repoerr.ErrUpdateEntity, err)
|
||||
}
|
||||
|
||||
row, err := repo.DB.NamedQueryContext(ctx, query, dbc)
|
||||
if err != nil {
|
||||
return mgclients.Client{}, postgres.HandleError(err, errors.ErrUpdateEntity)
|
||||
return mgclients.Client{}, postgres.HandleError(err, repoerr.ErrUpdateEntity)
|
||||
}
|
||||
|
||||
defer row.Close()
|
||||
if ok := row.Next(); !ok {
|
||||
return mgclients.Client{}, errors.Wrap(errors.ErrNotFound, row.Err())
|
||||
return mgclients.Client{}, errors.Wrap(repoerr.ErrNotFound, row.Err())
|
||||
}
|
||||
dbc = pgclients.DBClient{}
|
||||
if err := row.StructScan(&dbc); err != nil {
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
cpostgres "github.com/absmach/magistrala/users/postgres"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -69,7 +71,7 @@ func TestClientsSave(t *testing.T) {
|
||||
Metadata: mgclients.Metadata{},
|
||||
Status: mgclients.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "add client with duplicate client name",
|
||||
@@ -83,7 +85,7 @@ func TestClientsSave(t *testing.T) {
|
||||
Metadata: mgclients.Metadata{},
|
||||
Status: mgclients.EnabledStatus,
|
||||
},
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "add client with invalid client id",
|
||||
@@ -230,7 +232,7 @@ func TestIsPlatformAdmin(t *testing.T) {
|
||||
Status: mgclients.EnabledStatus,
|
||||
Role: mgclients.UserRole,
|
||||
},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -276,12 +278,12 @@ func TestRetrieveByID(t *testing.T) {
|
||||
{
|
||||
desc: "retrieve non-existing client",
|
||||
clientID: invalidName,
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "retrieve with empty client id",
|
||||
clientID: "",
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -737,7 +739,7 @@ func TestUpdateRole(t *testing.T) {
|
||||
desc: "update role with invalid client id",
|
||||
client: mgclients.Client{ID: invalidName},
|
||||
newRole: mgclients.AdminRole,
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -77,7 +77,7 @@ func (svc service) RegisterClient(ctx context.Context, token string, cli mgclien
|
||||
|
||||
clientID, err := svc.idProvider.ID()
|
||||
if err != nil {
|
||||
return mgclients.Client{}, errors.Wrap(svcerr.ErrUniqueID, err)
|
||||
return mgclients.Client{}, err
|
||||
}
|
||||
|
||||
if cli.Credentials.Secret == "" {
|
||||
@@ -120,7 +120,7 @@ func (svc service) IssueToken(ctx context.Context, identity, secret, domainID st
|
||||
return &magistrala.Token{}, errors.Wrap(repoerr.ErrNotFound, err)
|
||||
}
|
||||
if err := svc.hasher.Compare(secret, dbUser.Credentials.Secret); err != nil {
|
||||
return &magistrala.Token{}, errors.Wrap(errors.ErrLogin, err)
|
||||
return &magistrala.Token{}, errors.Wrap(svcerr.ErrLogin, err)
|
||||
}
|
||||
|
||||
var d string
|
||||
@@ -284,7 +284,7 @@ func (svc service) UpdateClientIdentity(ctx context.Context, token, clientID, id
|
||||
func (svc service) GenerateResetToken(ctx context.Context, email, host string) error {
|
||||
client, err := svc.clients.RetrieveByIdentity(ctx, email)
|
||||
if err != nil || client.Credentials.Identity == "" {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
issueReq := &magistrala.IssueReq{
|
||||
UserId: client.ID,
|
||||
@@ -308,7 +308,7 @@ func (svc service) ResetSecret(ctx context.Context, resetToken, secret string) e
|
||||
return errors.Wrap(repoerr.ErrNotFound, err)
|
||||
}
|
||||
if c.Credentials.Identity == "" {
|
||||
return errors.ErrNotFound
|
||||
return repoerr.ErrNotFound
|
||||
}
|
||||
if !svc.passRegex.MatchString(secret) {
|
||||
return ErrPasswordFormat
|
||||
@@ -618,7 +618,7 @@ func (svc service) addClientPolicy(ctx context.Context, userID string, role mgcl
|
||||
return err
|
||||
}
|
||||
if !resp.Added {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -648,7 +648,7 @@ func (svc service) addClientPolicyRollback(ctx context.Context, userID string, r
|
||||
return err
|
||||
}
|
||||
if !resp.Deleted {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+53
-53
@@ -87,7 +87,7 @@ func TestRegisterClient(t *testing.T) {
|
||||
deletePoliciesResponse: &magistrala.DeletePoliciesRes{Deleted: true},
|
||||
token: validToken,
|
||||
saveErr: repoerr.ErrConflict,
|
||||
err: errors.ErrConflict,
|
||||
err: repoerr.ErrConflict,
|
||||
},
|
||||
{
|
||||
desc: "register a new enabled client with name",
|
||||
@@ -226,7 +226,7 @@ func TestRegisterClient(t *testing.T) {
|
||||
Role: mgclients.AdminRole,
|
||||
},
|
||||
addPoliciesResponse: &magistrala.AddPoliciesRes{Added: false},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "register a new client with failed to add policies with err",
|
||||
@@ -336,7 +336,7 @@ func TestRegisterClient(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: wrongID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "register a new client as admin with failed check on super admin",
|
||||
@@ -344,8 +344,8 @@ func TestRegisterClient(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: validID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
checkSuperAdminErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
checkSuperAdminErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases2 {
|
||||
@@ -417,7 +417,7 @@ func TestViewClient(t *testing.T) {
|
||||
retrieveByIDResponse: mgclients.Client{},
|
||||
token: validToken,
|
||||
clientID: client.ID,
|
||||
retrieveByIDErr: errors.ErrNotFound,
|
||||
retrieveByIDErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
@@ -434,7 +434,7 @@ func TestViewClient(t *testing.T) {
|
||||
desc: "view client as admin user with invalid token",
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
token: inValidToken,
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
@@ -443,7 +443,7 @@ func TestViewClient(t *testing.T) {
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
clientID: client.ID,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "view client as admin user with failed check on super admin",
|
||||
@@ -451,8 +451,8 @@ func TestViewClient(t *testing.T) {
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
clientID: client.ID,
|
||||
checkSuperAdminErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
checkSuperAdminErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -547,7 +547,7 @@ func TestListClients(t *testing.T) {
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
|
||||
retrieveAllResponse: mgclients.ClientsPage{},
|
||||
token: validToken,
|
||||
retrieveAllErr: errors.ErrNotFound,
|
||||
retrieveAllErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
@@ -558,7 +558,7 @@ func TestListClients(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
superAdminErr: errors.ErrAuthorization,
|
||||
superAdminErr: svcerr.ErrAuthorization,
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
@@ -592,7 +592,7 @@ func TestListClients(t *testing.T) {
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
retrieveAllResponse: mgclients.ClientsPage{},
|
||||
token: validToken,
|
||||
retrieveAllErr: errors.ErrNotFound,
|
||||
retrieveAllErr: repoerr.ErrNotFound,
|
||||
err: svcerr.ErrNotFound,
|
||||
},
|
||||
}
|
||||
@@ -659,7 +659,7 @@ func TestUpdateClient(t *testing.T) {
|
||||
client: client1,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
token: inValidToken,
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
@@ -693,7 +693,7 @@ func TestUpdateClient(t *testing.T) {
|
||||
desc: "update client name as admin with invalid token",
|
||||
client: client1,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
token: inValidToken,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
@@ -703,7 +703,7 @@ func TestUpdateClient(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: wrongID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client with failed check on super admin",
|
||||
@@ -711,8 +711,8 @@ func TestUpdateClient(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: adminID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
checkSuperAdminErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
checkSuperAdminErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client name as admin with repo error on update",
|
||||
@@ -777,7 +777,7 @@ func TestUpdateClientTags(t *testing.T) {
|
||||
desc: "update client tags as normal user with invalid token",
|
||||
client: client,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
token: inValidToken,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
@@ -802,7 +802,7 @@ func TestUpdateClientTags(t *testing.T) {
|
||||
desc: "update client tags as admin with invalid token",
|
||||
client: client,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
token: inValidToken,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
@@ -812,16 +812,16 @@ func TestUpdateClientTags(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: wrongID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client tags as admin with failed check on super admin",
|
||||
client: client,
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: adminID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
checkSuperAdminErr: errors.ErrAuthorization,
|
||||
checkSuperAdminErr: svcerr.ErrAuthorization,
|
||||
token: validToken,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client tags as admin with repo error on update",
|
||||
@@ -890,7 +890,7 @@ func TestUpdateClientIdentity(t *testing.T) {
|
||||
token: inValidToken,
|
||||
id: client.ID,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
@@ -918,7 +918,7 @@ func TestUpdateClientIdentity(t *testing.T) {
|
||||
token: inValidToken,
|
||||
id: client.ID,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
@@ -928,7 +928,7 @@ func TestUpdateClientIdentity(t *testing.T) {
|
||||
id: client.ID,
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: wrongID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client identity as admin with failed check on super admin",
|
||||
@@ -937,8 +937,8 @@ func TestUpdateClientIdentity(t *testing.T) {
|
||||
id: client.ID,
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: adminID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
checkSuperAdminErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
checkSuperAdminErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client identity as admin with repo error on update",
|
||||
@@ -1010,7 +1010,7 @@ func TestUpdateClientRole(t *testing.T) {
|
||||
desc: "update client role with invalid token",
|
||||
client: client,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
token: inValidToken,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
@@ -1020,16 +1020,16 @@ func TestUpdateClientRole(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: wrongID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
token: validToken,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client role with failed check on super admin",
|
||||
client: client,
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
checkSuperAdminErr: errors.ErrAuthorization,
|
||||
checkSuperAdminErr: svcerr.ErrAuthorization,
|
||||
token: validToken,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client role with failed authorization on add policy",
|
||||
@@ -1038,7 +1038,7 @@ func TestUpdateClientRole(t *testing.T) {
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
|
||||
addPolicyResponse: &magistrala.AddPolicyRes{Added: false},
|
||||
token: validToken,
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "update client role with failed to add policy",
|
||||
@@ -1174,7 +1174,7 @@ func TestUpdateClientSecret(t *testing.T) {
|
||||
newSecret: newSecret,
|
||||
token: inValidToken,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
@@ -1214,7 +1214,7 @@ func TestUpdateClientSecret(t *testing.T) {
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: client.ID},
|
||||
retrieveByIDResponse: client,
|
||||
retrieveByIdentityResponse: rClient,
|
||||
err: errors.ErrLogin,
|
||||
err: svcerr.ErrLogin,
|
||||
},
|
||||
{
|
||||
desc: "update client secret with too long new secret",
|
||||
@@ -1308,7 +1308,7 @@ func TestEnableClient(t *testing.T) {
|
||||
token: inValidToken,
|
||||
client: disabledClient1,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
@@ -1318,7 +1318,7 @@ func TestEnableClient(t *testing.T) {
|
||||
client: disabledClient1,
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: disabledClient1.ID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "enable disabled client with normal user token",
|
||||
@@ -1327,8 +1327,8 @@ func TestEnableClient(t *testing.T) {
|
||||
client: disabledClient1,
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: validID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
checkSuperAdminErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
checkSuperAdminErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "enable disabled client with failed to retrieve client by ID",
|
||||
@@ -1495,7 +1495,7 @@ func TestDisableClient(t *testing.T) {
|
||||
token: inValidToken,
|
||||
client: enabledClient1,
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
{
|
||||
@@ -1505,7 +1505,7 @@ func TestDisableClient(t *testing.T) {
|
||||
client: enabledClient1,
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: disabledClient1.ID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "disable enabled client with normal user token",
|
||||
@@ -1514,8 +1514,8 @@ func TestDisableClient(t *testing.T) {
|
||||
client: enabledClient1,
|
||||
identifyResponse: &magistrala.IdentityRes{UserId: enabledClient1.ID},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
checkSuperAdminErr: errors.ErrAuthorization,
|
||||
err: errors.ErrAuthorization,
|
||||
checkSuperAdminErr: svcerr.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "disable enabled client with failed to retrieve client by ID",
|
||||
@@ -1846,7 +1846,7 @@ func TestListMembers(t *testing.T) {
|
||||
Object: validID,
|
||||
},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "list members with of the things kind with failed to list all subjects",
|
||||
@@ -1871,9 +1871,9 @@ func TestListMembers(t *testing.T) {
|
||||
ObjectType: authsvc.ThingType,
|
||||
},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: true},
|
||||
listAllSubjectsErr: errors.ErrNotFound,
|
||||
listAllSubjectsErr: repoerr.ErrNotFound,
|
||||
listAllSubjectsResponse: &magistrala.ListSubjectsRes{},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "list members with of the things kind with failed to retrieve all",
|
||||
@@ -2000,7 +2000,7 @@ func TestListMembers(t *testing.T) {
|
||||
Object: validID,
|
||||
},
|
||||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
|
||||
err: errors.ErrAuthorization,
|
||||
err: svcerr.ErrAuthorization,
|
||||
},
|
||||
{
|
||||
desc: "list members with no policies successfully of the groups kind",
|
||||
@@ -2084,7 +2084,7 @@ func TestListMembers(t *testing.T) {
|
||||
token: inValidToken,
|
||||
page: mgclients.Page{Offset: 0, Limit: 100, Permission: "read"},
|
||||
identifyResponse: &magistrala.IdentityRes{},
|
||||
identifyErr: errors.ErrAuthentication,
|
||||
identifyErr: svcerr.ErrAuthentication,
|
||||
err: svcerr.ErrAuthentication,
|
||||
},
|
||||
}
|
||||
@@ -2138,14 +2138,14 @@ func TestIssueToken(t *testing.T) {
|
||||
desc: "issue token for a non-existing client",
|
||||
client: client,
|
||||
retrieveByIdentityResponse: mgclients.Client{},
|
||||
retrieveByIdentityErr: errors.ErrNotFound,
|
||||
retrieveByIdentityErr: repoerr.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "issue token for a client with wrong secret",
|
||||
client: client,
|
||||
retrieveByIdentityResponse: rClient3,
|
||||
err: errors.ErrLogin,
|
||||
err: svcerr.ErrLogin,
|
||||
},
|
||||
{
|
||||
desc: "issue token with non-empty domain id",
|
||||
@@ -2268,8 +2268,8 @@ func TestGenerateResetToken(t *testing.T) {
|
||||
Identity: "",
|
||||
},
|
||||
},
|
||||
retrieveByIdentityErr: errors.ErrNotFound,
|
||||
err: errors.ErrNotFound,
|
||||
retrieveByIdentityErr: repoerr.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "generate reset token with failed to issue token",
|
||||
@@ -2359,7 +2359,7 @@ func TestResetSecret(t *testing.T) {
|
||||
Identity: "",
|
||||
},
|
||||
},
|
||||
err: errors.ErrNotFound,
|
||||
err: repoerr.ErrNotFound,
|
||||
},
|
||||
{
|
||||
desc: "reset secret with invalid secret format",
|
||||
|
||||
+3
-2
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/absmach/magistrala/pkg/messaging"
|
||||
)
|
||||
|
||||
@@ -105,10 +106,10 @@ func (svc *adapterService) authorize(ctx context.Context, thingKey, chanID, acti
|
||||
}
|
||||
res, err := svc.auth.Authorize(ctx, ar)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(errors.ErrAuthorization, err)
|
||||
return "", errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return "", errors.Wrap(errors.ErrAuthorization, err)
|
||||
return "", errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
|
||||
return res.GetId(), nil
|
||||
|
||||
+1
-1
@@ -177,7 +177,7 @@ func (h *handler) Publish(ctx context.Context, topic *string, payload *[]byte) e
|
||||
return err
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return errors.ErrAuthorization
|
||||
return svcerr.ErrAuthorization
|
||||
}
|
||||
|
||||
msg := messaging.Message{
|
||||
|
||||
Reference in New Issue
Block a user