mirror of
https://github.com/absmach/supermq.git
synced 2026-06-23 07:10:19 +00:00
NOISSUE - Add property based testing to users service (#2087)
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
This commit is contained in:
+18
-9
@@ -10,7 +10,6 @@ import (
|
||||
|
||||
"github.com/absmach/magistrala"
|
||||
"github.com/absmach/magistrala/internal/apiutil"
|
||||
"github.com/absmach/magistrala/internal/postgres"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
@@ -115,25 +114,32 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
|
||||
errors.Contains(err, apiutil.ErrLimitSize),
|
||||
errors.Contains(err, apiutil.ErrBearerKey),
|
||||
errors.Contains(err, apiutil.ErrNameSize),
|
||||
errors.Contains(err, svcerr.ErrInvalidStatus),
|
||||
errors.Contains(err, apiutil.ErrInvalidIDFormat),
|
||||
errors.Contains(err, apiutil.ErrInvalidQueryParams),
|
||||
errors.Contains(err, apiutil.ErrInvalidStatus),
|
||||
errors.Contains(err, apiutil.ErrMissingRelation),
|
||||
errors.Contains(err, apiutil.ErrValidation),
|
||||
errors.Contains(err, svcerr.ErrInvalidStatus),
|
||||
errors.Contains(err, apiutil.ErrInvitationState),
|
||||
errors.Contains(err, apiutil.ErrInvalidRole),
|
||||
errors.Contains(err, apiutil.ErrMissingEmail),
|
||||
errors.Contains(err, apiutil.ErrMissingHost),
|
||||
errors.Contains(err, apiutil.ErrMissingIdentity),
|
||||
errors.Contains(err, apiutil.ErrMissingSecret),
|
||||
errors.Contains(err, apiutil.ErrMissingPass),
|
||||
errors.Contains(err, apiutil.ErrMissingConfPass),
|
||||
errors.Contains(err, apiutil.ErrPasswordFormat):
|
||||
errors.Contains(err, apiutil.ErrInvalidResetPass),
|
||||
errors.Contains(err, apiutil.ErrMissingRelation),
|
||||
errors.Contains(err, svcerr.ErrPasswordFormat),
|
||||
errors.Contains(err, apiutil.ErrInvalidLevel),
|
||||
errors.Contains(err, apiutil.ErrInvalidQueryParams),
|
||||
errors.Contains(err, apiutil.ErrValidation):
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
case errors.Contains(err, svcerr.ErrAuthentication),
|
||||
errors.Contains(err, svcerr.ErrLogin),
|
||||
errors.Contains(err, apiutil.ErrBearerToken):
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
case errors.Contains(err, svcerr.ErrNotFound):
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
case errors.Contains(err, postgres.ErrMemberAlreadyAssigned),
|
||||
errors.Contains(err, svcerr.ErrConflict):
|
||||
case errors.Contains(err, svcerr.ErrConflict),
|
||||
errors.Contains(err, errors.ErrStatusAlreadyAssigned):
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
case errors.Contains(err, svcerr.ErrAuthorization),
|
||||
errors.Contains(err, svcerr.ErrDomainAuthorization):
|
||||
@@ -142,9 +148,12 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||
case errors.Contains(err, svcerr.ErrCreateEntity),
|
||||
errors.Contains(err, svcerr.ErrUpdateEntity),
|
||||
errors.Contains(err, svcerr.ErrFailedUpdateRole),
|
||||
errors.Contains(err, svcerr.ErrViewEntity),
|
||||
errors.Contains(err, svcerr.ErrAddPolicies),
|
||||
errors.Contains(err, svcerr.ErrDeletePolicies),
|
||||
errors.Contains(err, svcerr.ErrRemoveEntity):
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
default:
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -294,14 +294,14 @@ func TestEncodeError(t *testing.T) {
|
||||
code: http.StatusUnsupportedMediaType,
|
||||
},
|
||||
{
|
||||
desc: "InternalServerError",
|
||||
desc: "StatusUnprocessableEntity",
|
||||
errs: []error{
|
||||
svcerr.ErrCreateEntity,
|
||||
svcerr.ErrUpdateEntity,
|
||||
svcerr.ErrViewEntity,
|
||||
svcerr.ErrRemoveEntity,
|
||||
},
|
||||
code: http.StatusInternalServerError,
|
||||
code: http.StatusUnprocessableEntity,
|
||||
},
|
||||
{
|
||||
desc: "InternalServerError",
|
||||
|
||||
@@ -114,12 +114,12 @@ var (
|
||||
// ErrMissingRelation indicates missing relation.
|
||||
ErrMissingRelation = errors.New("missing relation")
|
||||
|
||||
// ErrInvalidRelation indicates an invalid relation.
|
||||
ErrInvalidRelation = errors.New("invalid relation")
|
||||
|
||||
// ErrInvalidAPIKey indicates an invalid API key type.
|
||||
ErrInvalidAPIKey = errors.New("invalid api key type")
|
||||
|
||||
// ErrMaxLevelExceeded indicates an invalid group level.
|
||||
ErrMaxLevelExceeded = errors.New("invalid group level (should be lower than 5)")
|
||||
|
||||
// ErrBootstrapState indicates an invalid bootstrap state.
|
||||
ErrBootstrapState = errors.New("invalid bootstrap state")
|
||||
|
||||
|
||||
@@ -86,9 +86,9 @@ type groupPageRes struct {
|
||||
}
|
||||
|
||||
type pageRes struct {
|
||||
Limit uint64 `json:"limit"`
|
||||
Limit uint64 `json:"limit,omitempty"`
|
||||
Offset uint64 `json:"offset"`
|
||||
Total uint64 `json:"total,omitempty"`
|
||||
Total uint64 `json:"total"`
|
||||
Level uint64 `json:"level,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
@@ -160,17 +160,17 @@ func (repo groupRepository) RetrieveAll(ctx context.Context, gm mggroups.Page) (
|
||||
|
||||
dbPage, err := toDBGroupPage(gm)
|
||||
if err != nil {
|
||||
return mggroups.Page{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
|
||||
return mggroups.Page{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
|
||||
}
|
||||
rows, err := repo.db.NamedQueryContext(ctx, q, dbPage)
|
||||
if err != nil {
|
||||
return mggroups.Page{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
|
||||
return mggroups.Page{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
items, err := repo.processRows(rows)
|
||||
if err != nil {
|
||||
return mggroups.Page{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
|
||||
return mggroups.Page{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
|
||||
}
|
||||
|
||||
cq := "SELECT COUNT(*) FROM groups g"
|
||||
@@ -180,7 +180,7 @@ func (repo groupRepository) RetrieveAll(ctx context.Context, gm mggroups.Page) (
|
||||
|
||||
total, err := postgres.Total(ctx, repo.db, cq, dbPage)
|
||||
if err != nil {
|
||||
return mggroups.Page{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
|
||||
return mggroups.Page{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
|
||||
}
|
||||
|
||||
page := gm
|
||||
@@ -208,17 +208,17 @@ func (repo groupRepository) RetrieveByIDs(ctx context.Context, gm mggroups.Page,
|
||||
|
||||
dbPage, err := toDBGroupPage(gm)
|
||||
if err != nil {
|
||||
return mggroups.Page{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
|
||||
return mggroups.Page{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
|
||||
}
|
||||
rows, err := repo.db.NamedQueryContext(ctx, q, dbPage)
|
||||
if err != nil {
|
||||
return mggroups.Page{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
|
||||
return mggroups.Page{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
items, err := repo.processRows(rows)
|
||||
if err != nil {
|
||||
return mggroups.Page{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
|
||||
return mggroups.Page{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
|
||||
}
|
||||
|
||||
cq := "SELECT COUNT(*) FROM groups g"
|
||||
@@ -228,7 +228,7 @@ func (repo groupRepository) RetrieveByIDs(ctx context.Context, gm mggroups.Page,
|
||||
|
||||
total, err := postgres.Total(ctx, repo.db, cq, dbPage)
|
||||
if err != nil {
|
||||
return mggroups.Page{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
|
||||
return mggroups.Page{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
|
||||
}
|
||||
|
||||
page := gm
|
||||
|
||||
@@ -22,8 +22,6 @@ import (
|
||||
var (
|
||||
errParentUnAuthz = errors.New("failed to authorize parent group")
|
||||
errMemberKind = errors.New("invalid member kind")
|
||||
errAddPolicies = errors.New("failed to add policies")
|
||||
errDeletePolicies = errors.New("failed to delete policies")
|
||||
errRetrieveGroups = errors.New("failed to retrieve groups")
|
||||
errGroupIDs = errors.New("invalid group ids")
|
||||
)
|
||||
@@ -286,7 +284,7 @@ func (svc service) checkSuperAdmin(ctx context.Context, userID string) error {
|
||||
Object: auth.MagistralaObject,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
if !res.Authorized {
|
||||
return svcerr.ErrAuthorization
|
||||
@@ -447,7 +445,7 @@ func (svc service) Assign(ctx context.Context, token, groupID, relation, memberK
|
||||
}
|
||||
|
||||
if _, err := svc.auth.AddPolicies(ctx, &policies); err != nil {
|
||||
return errors.Wrap(errAddPolicies, err)
|
||||
return errors.Wrap(svcerr.ErrAddPolicies, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -598,7 +596,7 @@ func (svc service) Unassign(ctx context.Context, token, groupID, relation, membe
|
||||
}
|
||||
|
||||
if _, err := svc.auth.DeletePolicies(ctx, &policies); err != nil {
|
||||
return errors.Wrap(errDeletePolicies, err)
|
||||
return errors.Wrap(svcerr.ErrDeletePolicies, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -696,7 +694,7 @@ func (svc service) changeGroupStatus(ctx context.Context, token string, group gr
|
||||
return groups.Group{}, err
|
||||
}
|
||||
if dbGroup.Status == group.Status {
|
||||
return groups.Group{}, mgclients.ErrStatusAlreadyAssigned
|
||||
return groups.Group{}, errors.ErrStatusAlreadyAssigned
|
||||
}
|
||||
|
||||
group.UpdatedBy = id
|
||||
@@ -725,7 +723,7 @@ func (svc service) authorizeToken(ctx context.Context, subjectType, subject, per
|
||||
}
|
||||
res, err := svc.auth.Authorize(ctx, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return "", svcerr.ErrAuthorization
|
||||
@@ -745,7 +743,7 @@ func (svc service) authorizeKind(ctx context.Context, domainID, subjectType, sub
|
||||
}
|
||||
res, err := svc.auth.Authorize(ctx, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", errors.Wrap(svcerr.ErrAuthorization, err)
|
||||
}
|
||||
if !res.GetAuthorized() {
|
||||
return "", svcerr.ErrAuthorization
|
||||
|
||||
@@ -17,7 +17,6 @@ import (
|
||||
"github.com/absmach/magistrala/internal/groups"
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
"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"
|
||||
@@ -587,7 +586,7 @@ func TestEnableGroup(t *testing.T) {
|
||||
retrieveResp: mggroups.Group{
|
||||
Status: clients.Status(groups.EnabledStatus),
|
||||
},
|
||||
err: mgclients.ErrStatusAlreadyAssigned,
|
||||
err: errors.ErrStatusAlreadyAssigned,
|
||||
},
|
||||
{
|
||||
desc: "with retrieve error",
|
||||
@@ -687,7 +686,7 @@ func TestDisableGroup(t *testing.T) {
|
||||
retrieveResp: mggroups.Group{
|
||||
Status: clients.Status(groups.DisabledStatus),
|
||||
},
|
||||
err: mgclients.ErrStatusAlreadyAssigned,
|
||||
err: errors.ErrStatusAlreadyAssigned,
|
||||
},
|
||||
{
|
||||
desc: "with retrieve error",
|
||||
|
||||
@@ -6,42 +6,9 @@ package postgres
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrAssignToGroup indicates failure to assign member to a group.
|
||||
ErrAssignToGroup = errors.New("failed to assign member to a group")
|
||||
|
||||
// ErrUnassignFromGroup indicates failure to unassign member from a group.
|
||||
ErrUnassignFromGroup = errors.New("failed to unassign member from a group")
|
||||
|
||||
// ErrMissingParent indicates that parent can't be found.
|
||||
ErrMissingParent = errors.New("failed to retrieve parent")
|
||||
|
||||
// ErrGroupNotEmpty indicates group is not empty, can't be deleted.
|
||||
ErrGroupNotEmpty = errors.New("group is not empty")
|
||||
|
||||
// ErrMemberAlreadyAssigned indicates that members is already assigned.
|
||||
ErrMemberAlreadyAssigned = errors.New("member is already assigned")
|
||||
|
||||
// ErrFailedToRetrieveMembers failed to retrieve group members.
|
||||
ErrFailedToRetrieveMembers = errors.New("failed to retrieve group members")
|
||||
|
||||
// ErrFailedToRetrieveMembership failed to retrieve memberships.
|
||||
ErrFailedToRetrieveMembership = errors.New("failed to retrieve memberships")
|
||||
|
||||
// ErrFailedToRetrieveAll failed to retrieve groups.
|
||||
ErrFailedToRetrieveAll = errors.New("failed to retrieve all groups")
|
||||
|
||||
// ErrFailedToRetrieveParents failed to retrieve groups.
|
||||
ErrFailedToRetrieveParents = errors.New("failed to retrieve all groups")
|
||||
|
||||
// ErrFailedToRetrieveChildren failed to retrieve groups.
|
||||
ErrFailedToRetrieveChildren = errors.New("failed to retrieve all groups")
|
||||
)
|
||||
|
||||
func CreateMetadataQuery(entity string, um map[string]interface{}) (string, []byte, error) {
|
||||
if len(um) == 0 {
|
||||
return "", nil, nil
|
||||
|
||||
Reference in New Issue
Block a user