Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Fix failing tests in SDK
Browse files Browse the repository at this point in the history
Fix failing tests in `sdk` package.
  • Loading branch information
rodneyosodo committed Nov 20, 2023
1 parent 7c5ad09 commit 1270096
Show file tree
Hide file tree
Showing 13 changed files with 733 additions and 495 deletions.
4 changes: 3 additions & 1 deletion internal/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
errors.Contains(err, apiutil.ErrEmptyList),
errors.Contains(err, apiutil.ErrMissingMemberType),
errors.Contains(err, apiutil.ErrMissingMemberKind),
errors.Contains(err, apiutil.ErrLimitSize),
errors.Contains(err, apiutil.ErrNameSize):
w.WriteHeader(http.StatusBadRequest)
case errors.Contains(err, errors.ErrAuthentication):
case errors.Contains(err, errors.ErrAuthentication),
errors.Contains(err, apiutil.ErrBearerToken):
w.WriteHeader(http.StatusUnauthorized)
case errors.Contains(err, errors.ErrNotFound):
w.WriteHeader(http.StatusNotFound)
Expand Down
3 changes: 3 additions & 0 deletions internal/groups/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func (req listGroupsReq) validate() error {
if req.Level < mggroups.MinLevel || req.Level > mggroups.MaxLevel {
return apiutil.ErrInvalidLevel
}
if req.Limit > api.MaxLimitSize || req.Limit < 1 {
return apiutil.ErrLimitSize
}

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func (svc service) authorize(ctx context.Context, subjectType, subject, permissi
}
res, err := svc.auth.Authorize(ctx, req)
if err != nil {
return "", errors.Wrap(errors.ErrAuthorization, err)
return "", err
}
if !res.GetAuthorized() {
return "", errors.ErrAuthorization
Expand All @@ -607,7 +607,7 @@ func (svc service) authorizeKind(ctx context.Context, subjectType, subjectKind,
}
res, err := svc.auth.Authorize(ctx, req)
if err != nil {
return "", errors.Wrap(errors.ErrAuthorization, err)
return "", err
}
if !res.GetAuthorized() {
return "", errors.ErrAuthorization
Expand Down
71 changes: 55 additions & 16 deletions pkg/sdk/go/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import (
"testing"
"time"

"github.com/absmach/magistrala"
authmocks "github.com/absmach/magistrala/auth/mocks"
"github.com/absmach/magistrala/certs"
httpapi "github.com/absmach/magistrala/certs/api"
"github.com/absmach/magistrala/certs/mocks"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/pkg/errors"
sdk "github.com/absmach/magistrala/pkg/sdk/go"
thmocks "github.com/absmach/magistrala/things/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand All @@ -31,8 +36,8 @@ var (
cfgSignHoursValid = "24h"
)

func newCertService() (certs.Service, error) {
server, _, _, auth := newThingsServer()
func newCertService() (certs.Service, *authmocks.Service, *thmocks.Repository, error) {
server, trepo, _, auth := newThingsServer()
config := sdk.Config{
ThingsURL: server.URL,
}
Expand All @@ -42,17 +47,17 @@ func newCertService() (certs.Service, error) {

tlsCert, caCert, err := certs.LoadCertificates(caPath, caKeyPath)
if err != nil {
return nil, err
return nil, auth, trepo, err
}

authTimeout, err := time.ParseDuration(cfgAuthTimeout)
if err != nil {
return nil, err
return nil, auth, trepo, err
}

pki := mocks.NewPkiAgent(tlsCert, caCert, cfgSignHoursValid, authTimeout)

return certs.New(auth, repo, mgsdk, pki), nil
return certs.New(auth, repo, mgsdk, pki), auth, trepo, nil
}

func newCertServer(svc certs.Service) *httptest.Server {
Expand All @@ -62,7 +67,7 @@ func newCertServer(svc certs.Service) *httptest.Server {
}

func TestIssueCert(t *testing.T) {
svc, err := newCertService()
svc, auth, trepo, err := newCertService()
require.Nil(t, err, fmt.Sprintf("unexpected error during creating service: %s", err))
ts := newCertServer(svc)
defer ts.Close()
Expand All @@ -86,21 +91,21 @@ func TestIssueCert(t *testing.T) {
desc: "create new cert with thing id and duration",
thingID: thingID,
duration: "10h",
token: adminToken,
token: validToken,
err: nil,
},
{
desc: "create new cert with empty thing id and duration",
thingID: "",
duration: "10h",
token: adminToken,
token: validToken,
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, apiutil.ErrMissingID), http.StatusBadRequest),
},
{
desc: "create new cert with invalid thing id and duration",
thingID: "ah",
duration: "10h",
token: adminToken,
token: validToken,
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, certs.ErrFailedCertCreation), http.StatusInternalServerError),
},
{
Expand Down Expand Up @@ -128,29 +133,35 @@ func TestIssueCert(t *testing.T) {
desc: "create new cert with invalid token",
thingID: thingID,
duration: "10h",
token: wrongValue,
token: authmocks.InvalidValue,
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, errors.ErrAuthentication), http.StatusUnauthorized),
},
{
desc: "create new empty cert",
thingID: "",
duration: "",
token: adminToken,
token: validToken,
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, apiutil.ErrMissingID), http.StatusBadRequest),
},
}

for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall2 := trepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(clients.Client{ID: tc.thingID}, tc.err)
cert, err := mgsdk.IssueCert(tc.thingID, tc.duration, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
if err == nil {
assert.NotEmpty(t, cert, fmt.Sprintf("%s: got empty cert", tc.desc))
}
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
}
}

func TestViewCert(t *testing.T) {
svc, err := newCertService()
svc, auth, trepo, err := newCertService()
require.Nil(t, err, fmt.Sprintf("unexpected error during creating service: %s", err))
ts := newCertServer(svc)
defer ts.Close()
Expand All @@ -163,8 +174,14 @@ func TestViewCert(t *testing.T) {

mgsdk := sdk.NewSDK(sdkConf)

repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall2 := trepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(clients.Client{ID: thingID}, nil)
cert, err := mgsdk.IssueCert(thingID, "10h", token)
require.Nil(t, err, fmt.Sprintf("unexpected error during creating cert: %s", err))
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()

cases := []struct {
desc string
Expand Down Expand Up @@ -197,16 +214,18 @@ func TestViewCert(t *testing.T) {
}

for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
cert, err := mgsdk.ViewCert(tc.certID, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
if err == nil {
assert.NotEmpty(t, cert, fmt.Sprintf("%s: got empty cert", tc.desc))
}
repoCall.Unset()
}
}

func TestViewCertByThing(t *testing.T) {
svc, err := newCertService()
svc, auth, trepo, err := newCertService()
require.Nil(t, err, fmt.Sprintf("unexpected error during creating service: %s", err))
ts := newCertServer(svc)
defer ts.Close()
Expand All @@ -219,8 +238,14 @@ func TestViewCertByThing(t *testing.T) {

mgsdk := sdk.NewSDK(sdkConf)

repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall2 := trepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(clients.Client{ID: thingID}, nil)
_, err = mgsdk.IssueCert(thingID, "10h", token)
require.Nil(t, err, fmt.Sprintf("unexpected error during creating cert: %s", err))
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()

cases := []struct {
desc string
Expand Down Expand Up @@ -253,16 +278,18 @@ func TestViewCertByThing(t *testing.T) {
}

for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
cert, err := mgsdk.ViewCertByThing(tc.thingID, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
if err == nil {
assert.NotEmpty(t, cert, fmt.Sprintf("%s: got empty cert", tc.desc))
}
repoCall.Unset()
}
}

func TestRevokeCert(t *testing.T) {
svc, err := newCertService()
svc, auth, trepo, err := newCertService()
require.Nil(t, err, fmt.Sprintf("unexpected error during creating service: %s", err))
ts := newCertServer(svc)
defer ts.Close()
Expand All @@ -275,8 +302,14 @@ func TestRevokeCert(t *testing.T) {

mgsdk := sdk.NewSDK(sdkConf)

_, err = mgsdk.IssueCert(thingID, "10h", adminToken)
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall2 := trepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(clients.Client{ID: thingID}, nil)
_, err = mgsdk.IssueCert(thingID, "10h", validToken)
require.Nil(t, err, fmt.Sprintf("unexpected error during creating cert: %s", err))
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()

cases := []struct {
desc string
Expand All @@ -287,7 +320,7 @@ func TestRevokeCert(t *testing.T) {
{
desc: "revoke cert with invalid token",
thingID: thingID,
token: wrongValue,
token: authmocks.InvalidValue,
err: errors.NewSDKErrorWithStatus(errors.ErrAuthentication, http.StatusUnauthorized),
},
{
Expand Down Expand Up @@ -323,10 +356,16 @@ func TestRevokeCert(t *testing.T) {
}

for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall2 := trepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(clients.Client{ID: tc.thingID}, nil)
response, err := mgsdk.RevokeCert(tc.thingID, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
if err == nil {
assert.NotEmpty(t, response, fmt.Sprintf("%s: got empty revocation time", tc.desc))
}
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
}
}
Loading

0 comments on commit 1270096

Please sign in to comment.