NOISSUE - Generate Users repository mocks (#160)

* Generate users repository mocks

Add generation of users repository mocks for easier testing purposes
rather than writing the mocks

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* Change output dir for generated mocks

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

---------

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
This commit is contained in:
b1ackd0t
2023-12-15 16:32:16 +03:00
committed by GitHub
parent 4bce9b6f2b
commit da79c82208
17 changed files with 941 additions and 721 deletions
+22 -10
View File
@@ -37,9 +37,11 @@ jobs:
- "pkg/messaging/message.proto"
- "pkg/messaging/*.pb.go"
sdk:
mocks:
- ".github/workflows/check-generated-files.yml"
- "pkg/sdk/go/sdk.go"
- "users/postgres/clients.go"
- "pkg/clients/clients.go"
- name: Set up protoc
if: steps.changes.outputs.proto == 'true'
@@ -80,18 +82,28 @@ jobs:
fi
done
- name: Check SDK is up to Date
if: steps.changes.outputs.sdk == 'true'
- name: Check Mocks are up to Date
if: steps.changes.outputs.mocks == 'true'
run: |
MOCKERY_VERSION=v2.38.0
go install github.com/vektra/mockery/v2@$MOCKERY_VERSION
mv ./pkg/sdk/go/mocks.go ./pkg/sdk/go/mocks.go.tmp
mv ./pkg/sdk/mocks/sdk.go ./pkg/sdk/mocks/sdk.go.tmp
mv ./users/mocks/repository.go ./users/mocks/repository.go.tmp
make mocks
if ! cmp -s ./pkg/sdk/go/mocks.go ./pkg/sdk/go/mocks.go.tmp; then
echo "Error: Generated mocks for SDK ./pkg/sdk/go/mocks.go are out of sync!"
echo "Please run 'make mocks' with mockery version $MOCKERY_VERSION and commit the changes."
exit 1
fi
check_mock_changes() {
local file_path=$1
local tmp_file_path=$1.tmp
local entity_name=$2
if ! cmp -s "$file_path" "$tmp_file_path"; then
echo "Error: Generated mocks for $entity_name are out of sync!"
echo "Please run 'make mocks' with mockery version $MOCKERY_VERSION and commit the changes."
exit 1
fi
}
check_mock_changes ./pkg/sdk/mocks/sdk.go "SDK ./pkg/sdk/mocks/sdk.go"
check_mock_changes ./users/mocks/repository.go "Users Repository ./users/mocks/repository.go"
+3
View File
@@ -26,6 +26,9 @@ linters-settings:
alias: svcerr
- pkg: github.com/absmach/magistrala/pkg/errors/repository
alias: repoerr
- pkg: github.com/absmach/magistrala/pkg/sdk/mocks
alias: sdkmocks
gocritic:
enabled-checks:
- captLocal
+3 -2
View File
@@ -28,6 +28,7 @@ import (
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
@@ -175,10 +176,10 @@ func dec(in []byte) ([]byte, error) {
return in, nil
}
func newService() (bootstrap.Service, *authmocks.Service, *mgsdk.MockSDK) {
func newService() (bootstrap.Service, *authmocks.Service, *sdkmocks.SDK) {
things := mocks.NewConfigsRepository()
auth := new(authmocks.Service)
sdk := &mgsdk.MockSDK{}
sdk := new(sdkmocks.SDK)
return bootstrap.New(auth, things, sdk, encKey), auth, sdk
}
+3 -2
View File
@@ -21,6 +21,7 @@ import (
"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/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -75,10 +76,10 @@ var (
}
)
func newService() (bootstrap.Service, *authmocks.Service, *mgsdk.MockSDK) {
func newService() (bootstrap.Service, *authmocks.Service, *sdkmocks.SDK) {
things := mocks.NewConfigsRepository()
auth := new(authmocks.Service)
sdk := &mgsdk.MockSDK{}
sdk := new(sdkmocks.SDK)
return bootstrap.New(auth, things, sdk, encKey), auth, sdk
}
+3 -2
View File
@@ -22,6 +22,7 @@ import (
"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/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
@@ -55,10 +56,10 @@ var (
}
)
func newService() (bootstrap.Service, *authmocks.Service, *mgsdk.MockSDK) {
func newService() (bootstrap.Service, *authmocks.Service, *sdkmocks.SDK) {
things := mocks.NewConfigsRepository()
auth := new(authmocks.Service)
sdk := &mgsdk.MockSDK{}
sdk := new(sdkmocks.SDK)
return bootstrap.New(auth, things, sdk, encKey), auth, sdk
}
+3 -2
View File
@@ -17,6 +17,7 @@ import (
"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/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
@@ -41,10 +42,10 @@ const (
instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
)
func newService(t *testing.T) (certs.Service, *authmocks.Service, *mgsdk.MockSDK) {
func newService(t *testing.T) (certs.Service, *authmocks.Service, *sdkmocks.SDK) {
auth := new(authmocks.Service)
sdk := &mgsdk.MockSDK{}
sdk := new(sdkmocks.SDK)
repo := mocks.NewCertsRepository()
tlsCert, caCert, err := certs.LoadCertificates(caPath, caKeyPath)
+1 -1
View File
@@ -57,6 +57,7 @@ require (
golang.org/x/net v0.17.0
golang.org/x/sync v0.4.0
gonum.org/v1/gonum v0.14.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
@@ -225,7 +226,6 @@ require (
golang.org/x/tools v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
+1
View File
@@ -116,6 +116,7 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
errors.Contains(err, apiutil.ErrNameSize):
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):
-36
View File
@@ -4,17 +4,10 @@
package testsutil
import (
"context"
"fmt"
"testing"
mgclients "github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/pkg/errors"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/absmach/magistrala/users"
cmocks "github.com/absmach/magistrala/users/mocks"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -24,32 +17,3 @@ func GenerateUUID(t *testing.T) string {
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
return ulid
}
func GenerateValidToken(t *testing.T, clientID string, svc users.Service, cRepo *cmocks.Repository, phasher users.Hasher, domainID string) string {
client := mgclients.Client{
ID: clientID,
Name: "validtoken",
Credentials: mgclients.Credentials{
Identity: "validtoken",
Secret: "secret",
},
Status: mgclients.EnabledStatus,
}
rClient := client
rClient.Credentials.Secret, _ = phasher.Hash(client.Credentials.Secret)
repoCall := cRepo.On("RetrieveByIdentity", context.Background(), client.Credentials.Identity).Return(rClient, nil)
token, err := svc.IssueToken(context.Background(), client.Credentials.Identity, client.Credentials.Secret, domainID)
assert.True(t, errors.Contains(err, nil), fmt.Sprintf("Create token expected nil got %s\n", err))
ok := repoCall.Parent.AssertCalled(t, "RetrieveByIdentity", context.Background(), client.Credentials.Identity)
assert.True(t, ok, "RetrieveByIdentity was not called on creating token")
repoCall.Unset()
return token.AccessToken
}
func CleanUpDB(t *testing.T, db *sqlx.DB) {
_, err := db.Exec("DELETE FROM groups")
require.Nil(t, err, fmt.Sprintf("clean groups unexpected error: %s", err))
_, err = db.Exec("DELETE FROM clients")
require.Nil(t, err, fmt.Sprintf("clean clients unexpected error: %s", err))
}
+1 -1
View File
@@ -110,7 +110,7 @@ type Credentials struct {
// SDK contains Magistrala API.
//
//go:generate mockery --name SDK --inpackage --filename mocks.go --quiet --note "Copyright (c) Abstract Machines"
//go:generate mockery --name SDK --output=../mocks --filename sdk.go --quiet --note "Copyright (c) Abstract Machines"
type SDK interface {
// CreateUser registers magistrala user.
//
+2 -2
View File
@@ -339,7 +339,7 @@ func TestListThings(t *testing.T) {
token: authmocks.InvalidValue,
offset: offset,
limit: limit,
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, errors.ErrAuthentication), http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrAuthentication, errors.ErrAuthentication), http.StatusUnauthorized),
response: nil,
},
{
@@ -347,7 +347,7 @@ func TestListThings(t *testing.T) {
token: "",
offset: offset,
limit: limit,
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, errors.ErrAuthentication), http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.Wrap(errors.ErrAuthentication, errors.ErrAuthentication), http.StatusUnauthorized),
response: nil,
},
{
+10 -9
View File
@@ -24,7 +24,7 @@ import (
sdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/absmach/magistrala/users"
"github.com/absmach/magistrala/users/api"
"github.com/absmach/magistrala/users/mocks"
umocks "github.com/absmach/magistrala/users/mocks"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -34,10 +34,11 @@ var (
id = generateUUID(&testing.T{})
validToken = "token"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
wrongID = testsutil.GenerateUUID(&testing.T{})
)
func newClientServer() (*httptest.Server, *mocks.Repository, *gmocks.Repository, *authmocks.Service) {
crepo := new(mocks.Repository)
func newClientServer() (*httptest.Server, *umocks.Repository, *gmocks.Repository, *authmocks.Service) {
crepo := new(umocks.Repository)
gRepo := new(gmocks.Repository)
auth := new(authmocks.Service)
@@ -113,7 +114,7 @@ func TestCreateClient(t *testing.T) {
desc: "register user with invalid identity",
client: sdk.User{
Credentials: sdk.Credentials{
Identity: mocks.WrongID,
Identity: wrongID,
Secret: "password",
},
},
@@ -428,14 +429,14 @@ func TestClient(t *testing.T) {
desc: "view client with valid token and invalid client id",
response: sdk.User{},
token: validToken,
clientID: mocks.WrongID,
clientID: wrongID,
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrNotFound, svcerr.ErrNotFound), http.StatusNotFound),
},
{
desc: "view client with an invalid token and invalid client id",
response: sdk.User{},
token: invalidToken,
clientID: mocks.WrongID,
clientID: wrongID,
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
},
}
@@ -830,7 +831,7 @@ func TestUpdateClientSecret(t *testing.T) {
token: "non-existent",
response: sdk.User{},
repoErr: errors.ErrAuthentication,
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, errors.ErrAuthentication), http.StatusUnauthorized),
},
{
desc: "update client secret with wrong old secret",
@@ -1008,7 +1009,7 @@ func TestEnableClient(t *testing.T) {
},
{
desc: "enable non-existing client",
id: mocks.WrongID,
id: wrongID,
token: validToken,
client: sdk.User{},
response: sdk.User{},
@@ -1137,7 +1138,7 @@ func TestDisableClient(t *testing.T) {
},
{
desc: "disable non-existing client",
id: mocks.WrongID,
id: wrongID,
client: sdk.User{},
token: validToken,
response: sdk.User{},
File diff suppressed because it is too large Load Diff
-180
View File
@@ -1,180 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package mocks
import (
"context"
mgclients "github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/pkg/errors"
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
"github.com/absmach/magistrala/users/postgres"
"github.com/stretchr/testify/mock"
)
const WrongID = "wrongID"
var _ postgres.Repository = (*Repository)(nil)
type Repository struct {
mock.Mock
}
func (m *Repository) ChangeStatus(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)
if client.ID == WrongID {
return mgclients.Client{}, repoerr.ErrNotFound
}
if client.Status != mgclients.EnabledStatus && client.Status != mgclients.DisabledStatus {
return mgclients.Client{}, repoerr.ErrMalformedEntity
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) Members(ctx context.Context, groupID string, pm mgclients.Page) (mgclients.MembersPage, error) {
ret := m.Called(ctx, groupID, pm)
if groupID == WrongID {
return mgclients.MembersPage{}, repoerr.ErrNotFound
}
return ret.Get(0).(mgclients.MembersPage), ret.Error(1)
}
func (m *Repository) RetrieveAll(ctx context.Context, pm mgclients.Page) (mgclients.ClientsPage, error) {
ret := m.Called(ctx, pm)
return ret.Get(0).(mgclients.ClientsPage), ret.Error(1)
}
func (m *Repository) RetrieveAllBasicInfo(ctx context.Context, pm mgclients.Page) (mgclients.ClientsPage, error) {
ret := m.Called(ctx, pm)
return ret.Get(0).(mgclients.ClientsPage), ret.Error(1)
}
func (m *Repository) RetrieveByID(ctx context.Context, id string) (mgclients.Client, error) {
ret := m.Called(ctx, id)
if id == WrongID {
return mgclients.Client{}, repoerr.ErrNotFound
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) RetrieveByIdentity(ctx context.Context, identity string) (mgclients.Client, error) {
ret := m.Called(ctx, identity)
if identity == "" {
return mgclients.Client{}, repoerr.ErrMalformedEntity
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) Save(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)
if client.Owner == WrongID {
return mgclients.Client{}, repoerr.ErrMalformedEntity
}
if client.Credentials.Secret == "" {
return mgclients.Client{}, repoerr.ErrMalformedEntity
}
return client, ret.Error(1)
}
func (m *Repository) Update(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)
if client.ID == WrongID {
return mgclients.Client{}, repoerr.ErrNotFound
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) UpdateIdentity(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)
if client.ID == WrongID {
return mgclients.Client{}, repoerr.ErrNotFound
}
if client.Credentials.Identity == "" {
return mgclients.Client{}, repoerr.ErrMalformedEntity
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) UpdateSecret(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)
if client.ID == WrongID {
return mgclients.Client{}, repoerr.ErrNotFound
}
if client.Credentials.Secret == "" {
return mgclients.Client{}, repoerr.ErrMalformedEntity
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) UpdateTags(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)
if client.ID == WrongID {
return mgclients.Client{}, repoerr.ErrNotFound
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) UpdateOwner(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)
if client.ID == WrongID {
return mgclients.Client{}, repoerr.ErrNotFound
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) UpdateRole(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)
if client.ID == WrongID {
return mgclients.Client{}, errors.ErrNotFound
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) RetrieveBySecret(ctx context.Context, key string) (mgclients.Client, error) {
ret := m.Called(ctx, key)
if key == "" {
return mgclients.Client{}, repoerr.ErrMalformedEntity
}
return ret.Get(0).(mgclients.Client), ret.Error(1)
}
func (m *Repository) CheckSuperAdmin(ctx context.Context, userID string) error {
ret := m.Called(ctx, userID)
if userID == WrongID {
return errors.ErrAuthorization
}
return ret.Error(0)
}
func (m *Repository) RetrieveAllByIDs(ctx context.Context, pm mgclients.Page) (mgclients.ClientsPage, error) {
ret := m.Called(ctx, pm)
return ret.Get(0).(mgclients.ClientsPage), ret.Error(1)
}
+414
View File
@@ -0,0 +1,414 @@
// Code generated by mockery v2.38.0. DO NOT EDIT.
// Copyright (c) Abstract Machines
package mocks
import (
context "context"
clients "github.com/absmach/magistrala/pkg/clients"
mock "github.com/stretchr/testify/mock"
)
// Repository is an autogenerated mock type for the Repository type
type Repository struct {
mock.Mock
}
// ChangeStatus provides a mock function with given fields: ctx, client
func (_m *Repository) ChangeStatus(ctx context.Context, client clients.Client) (clients.Client, error) {
ret := _m.Called(ctx, client)
if len(ret) == 0 {
panic("no return value specified for ChangeStatus")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) (clients.Client, error)); ok {
return rf(ctx, client)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) clients.Client); ok {
r0 = rf(ctx, client)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Client) error); ok {
r1 = rf(ctx, client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// CheckSuperAdmin provides a mock function with given fields: ctx, adminID
func (_m *Repository) CheckSuperAdmin(ctx context.Context, adminID string) error {
ret := _m.Called(ctx, adminID)
if len(ret) == 0 {
panic("no return value specified for CheckSuperAdmin")
}
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, string) error); ok {
r0 = rf(ctx, adminID)
} else {
r0 = ret.Error(0)
}
return r0
}
// RetrieveAll provides a mock function with given fields: ctx, pm
func (_m *Repository) RetrieveAll(ctx context.Context, pm clients.Page) (clients.ClientsPage, error) {
ret := _m.Called(ctx, pm)
if len(ret) == 0 {
panic("no return value specified for RetrieveAll")
}
var r0 clients.ClientsPage
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Page) (clients.ClientsPage, error)); ok {
return rf(ctx, pm)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Page) clients.ClientsPage); ok {
r0 = rf(ctx, pm)
} else {
r0 = ret.Get(0).(clients.ClientsPage)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Page) error); ok {
r1 = rf(ctx, pm)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// RetrieveAllBasicInfo provides a mock function with given fields: ctx, pm
func (_m *Repository) RetrieveAllBasicInfo(ctx context.Context, pm clients.Page) (clients.ClientsPage, error) {
ret := _m.Called(ctx, pm)
if len(ret) == 0 {
panic("no return value specified for RetrieveAllBasicInfo")
}
var r0 clients.ClientsPage
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Page) (clients.ClientsPage, error)); ok {
return rf(ctx, pm)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Page) clients.ClientsPage); ok {
r0 = rf(ctx, pm)
} else {
r0 = ret.Get(0).(clients.ClientsPage)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Page) error); ok {
r1 = rf(ctx, pm)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// RetrieveAllByIDs provides a mock function with given fields: ctx, pm
func (_m *Repository) RetrieveAllByIDs(ctx context.Context, pm clients.Page) (clients.ClientsPage, error) {
ret := _m.Called(ctx, pm)
if len(ret) == 0 {
panic("no return value specified for RetrieveAllByIDs")
}
var r0 clients.ClientsPage
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Page) (clients.ClientsPage, error)); ok {
return rf(ctx, pm)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Page) clients.ClientsPage); ok {
r0 = rf(ctx, pm)
} else {
r0 = ret.Get(0).(clients.ClientsPage)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Page) error); ok {
r1 = rf(ctx, pm)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// RetrieveByID provides a mock function with given fields: ctx, id
func (_m *Repository) RetrieveByID(ctx context.Context, id string) (clients.Client, error) {
ret := _m.Called(ctx, id)
if len(ret) == 0 {
panic("no return value specified for RetrieveByID")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string) (clients.Client, error)); ok {
return rf(ctx, id)
}
if rf, ok := ret.Get(0).(func(context.Context, string) clients.Client); ok {
r0 = rf(ctx, id)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
r1 = rf(ctx, id)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// RetrieveByIdentity provides a mock function with given fields: ctx, identity
func (_m *Repository) RetrieveByIdentity(ctx context.Context, identity string) (clients.Client, error) {
ret := _m.Called(ctx, identity)
if len(ret) == 0 {
panic("no return value specified for RetrieveByIdentity")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string) (clients.Client, error)); ok {
return rf(ctx, identity)
}
if rf, ok := ret.Get(0).(func(context.Context, string) clients.Client); ok {
r0 = rf(ctx, identity)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
r1 = rf(ctx, identity)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Save provides a mock function with given fields: ctx, client
func (_m *Repository) Save(ctx context.Context, client clients.Client) (clients.Client, error) {
ret := _m.Called(ctx, client)
if len(ret) == 0 {
panic("no return value specified for Save")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) (clients.Client, error)); ok {
return rf(ctx, client)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) clients.Client); ok {
r0 = rf(ctx, client)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Client) error); ok {
r1 = rf(ctx, client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Update provides a mock function with given fields: ctx, client
func (_m *Repository) Update(ctx context.Context, client clients.Client) (clients.Client, error) {
ret := _m.Called(ctx, client)
if len(ret) == 0 {
panic("no return value specified for Update")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) (clients.Client, error)); ok {
return rf(ctx, client)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) clients.Client); ok {
r0 = rf(ctx, client)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Client) error); ok {
r1 = rf(ctx, client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// UpdateIdentity provides a mock function with given fields: ctx, client
func (_m *Repository) UpdateIdentity(ctx context.Context, client clients.Client) (clients.Client, error) {
ret := _m.Called(ctx, client)
if len(ret) == 0 {
panic("no return value specified for UpdateIdentity")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) (clients.Client, error)); ok {
return rf(ctx, client)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) clients.Client); ok {
r0 = rf(ctx, client)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Client) error); ok {
r1 = rf(ctx, client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// UpdateOwner provides a mock function with given fields: ctx, client
func (_m *Repository) UpdateOwner(ctx context.Context, client clients.Client) (clients.Client, error) {
ret := _m.Called(ctx, client)
if len(ret) == 0 {
panic("no return value specified for UpdateOwner")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) (clients.Client, error)); ok {
return rf(ctx, client)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) clients.Client); ok {
r0 = rf(ctx, client)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Client) error); ok {
r1 = rf(ctx, client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// UpdateRole provides a mock function with given fields: ctx, client
func (_m *Repository) UpdateRole(ctx context.Context, client clients.Client) (clients.Client, error) {
ret := _m.Called(ctx, client)
if len(ret) == 0 {
panic("no return value specified for UpdateRole")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) (clients.Client, error)); ok {
return rf(ctx, client)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) clients.Client); ok {
r0 = rf(ctx, client)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Client) error); ok {
r1 = rf(ctx, client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// UpdateSecret provides a mock function with given fields: ctx, client
func (_m *Repository) UpdateSecret(ctx context.Context, client clients.Client) (clients.Client, error) {
ret := _m.Called(ctx, client)
if len(ret) == 0 {
panic("no return value specified for UpdateSecret")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) (clients.Client, error)); ok {
return rf(ctx, client)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) clients.Client); ok {
r0 = rf(ctx, client)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Client) error); ok {
r1 = rf(ctx, client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// UpdateTags provides a mock function with given fields: ctx, client
func (_m *Repository) UpdateTags(ctx context.Context, client clients.Client) (clients.Client, error) {
ret := _m.Called(ctx, client)
if len(ret) == 0 {
panic("no return value specified for UpdateTags")
}
var r0 clients.Client
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) (clients.Client, error)); ok {
return rf(ctx, client)
}
if rf, ok := ret.Get(0).(func(context.Context, clients.Client) clients.Client); ok {
r0 = rf(ctx, client)
} else {
r0 = ret.Get(0).(clients.Client)
}
if rf, ok := ret.Get(1).(func(context.Context, clients.Client) error); ok {
r1 = rf(ctx, client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// NewRepository creates a new instance of Repository. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewRepository(t interface {
mock.TestingT
Cleanup(func())
}) *Repository {
mock := &Repository{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
+3
View File
@@ -21,6 +21,9 @@ type clientRepo struct {
pgclients.ClientRepository
}
// Repository defines the required dependencies for Client repository.
//
//go:generate mockery --name Repository --output=../mocks --filename repository.go --quiet --note "Copyright (c) Abstract Machines"
type Repository interface {
mgclients.Repository
+17 -21
View File
@@ -8,7 +8,6 @@ import (
"fmt"
"regexp"
"testing"
"time"
"github.com/absmach/magistrala"
authmocks "github.com/absmach/magistrala/auth/mocks"
@@ -39,13 +38,13 @@ var (
Metadata: validCMetadata,
Status: mgclients.EnabledStatus,
}
withinDuration = 5 * time.Second
passRegex = regexp.MustCompile("^.{8,}$")
myKey = "mine"
validToken = "token"
inValidToken = "invalid"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
domainID = testsutil.GenerateUUID(&testing.T{})
passRegex = regexp.MustCompile("^.{8,}$")
myKey = "mine"
validToken = "token"
inValidToken = "invalid"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
domainID = testsutil.GenerateUUID(&testing.T{})
wrongID = testsutil.GenerateUUID(&testing.T{})
)
func TestRegisterClient(t *testing.T) {
@@ -202,7 +201,7 @@ func TestRegisterClient(t *testing.T) {
{
desc: "register a new client with invalid owner",
client: mgclients.Client{
Owner: mocks.WrongID,
Owner: wrongID,
Credentials: mgclients.Credentials{
Identity: "newclientwithinvalidowner@example.com",
Secret: secret,
@@ -243,13 +242,10 @@ func TestRegisterClient(t *testing.T) {
}
repoCall1 := auth.On("AddPolicies", mock.Anything, mock.Anything).Return(&magistrala.AddPoliciesRes{Authorized: true}, nil)
repoCall2 := auth.On("DeletePolicies", mock.Anything, mock.Anything).Return(&magistrala.DeletePoliciesRes{Deleted: true}, nil)
repoCall3 := cRepo.On("Save", context.Background(), mock.Anything).Return(&mgclients.Client{}, tc.err)
registerTime := time.Now()
repoCall3 := cRepo.On("Save", context.Background(), mock.Anything).Return(tc.client, tc.err)
expected, err := svc.RegisterClient(context.Background(), tc.token, tc.client)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
if err == nil {
assert.NotEmpty(t, expected.ID, fmt.Sprintf("%s: expected %s not to be empty\n", tc.desc, expected.ID))
assert.WithinDuration(t, expected.CreatedAt, registerTime, withinDuration, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, expected.CreatedAt, registerTime))
tc.client.ID = expected.ID
tc.client.CreatedAt = expected.CreatedAt
tc.client.UpdatedAt = expected.UpdatedAt
@@ -298,14 +294,14 @@ func TestViewClient(t *testing.T) {
desc: "view client with valid token and invalid client id",
response: mgclients.Client{},
token: validToken,
clientID: mocks.WrongID,
clientID: wrongID,
err: svcerr.ErrNotFound,
},
{
desc: "view client with an invalid token and invalid client id",
response: mgclients.Client{},
token: inValidToken,
clientID: mocks.WrongID,
clientID: wrongID,
err: svcerr.ErrAuthentication,
},
}
@@ -653,7 +649,7 @@ func TestUpdateClient(t *testing.T) {
{
desc: "update client name with invalid ID",
client: mgclients.Client{
ID: mocks.WrongID,
ID: wrongID,
Name: "Updated Client",
},
response: mgclients.Client{},
@@ -729,7 +725,7 @@ func TestUpdateClientTags(t *testing.T) {
{
desc: "update client name with invalid ID",
client: mgclients.Client{
ID: mocks.WrongID,
ID: wrongID,
Name: "Updated name",
},
response: mgclients.Client{},
@@ -788,7 +784,7 @@ func TestUpdateClientIdentity(t *testing.T) {
desc: "update client identity with invalid id",
identity: "updated@example.com",
token: validToken,
id: mocks.WrongID,
id: wrongID,
response: mgclients.Client{},
err: repoerr.ErrNotFound,
},
@@ -855,7 +851,7 @@ func TestUpdateClientRole(t *testing.T) {
{
desc: "update client role with invalid ID",
client: mgclients.Client{
ID: mocks.WrongID,
ID: wrongID,
Role: mgclients.AdminRole,
},
response: mgclients.Client{},
@@ -997,7 +993,7 @@ func TestEnableClient(t *testing.T) {
},
{
desc: "enable non-existing client",
id: mocks.WrongID,
id: wrongID,
token: validToken,
client: mgclients.Client{},
response: mgclients.Client{},
@@ -1127,7 +1123,7 @@ func TestDisableClient(t *testing.T) {
},
{
desc: "disable non-existing client",
id: mocks.WrongID,
id: wrongID,
client: mgclients.Client{},
token: validToken,
response: mgclients.Client{},