MG-2193 - Generate mocks for proto interface (#2324)

Signed-off-by: JeffMboya <jangina.mboya@gmail.com>
This commit is contained in:
JMboya
2024-07-12 11:36:12 +03:00
committed by GitHub
parent 04c3594627
commit 468362aac4
23 changed files with 1346 additions and 207 deletions
@@ -76,6 +76,7 @@ jobs:
- "twins/states.go"
- "twins/service.go"
- "journal/journal.go"
- "magistrala/auth_grpc.pb.go"
- name: Set up protoc
if: steps.changes.outputs.proto == 'true'
@@ -160,6 +161,7 @@ jobs:
mv ./twins/mocks/cache.go ./twins/mocks/cache.go.tmp
mv ./journal/mocks/repository.go ./journal/mocks/repository.go.tmp
mv ./journal/mocks/service.go ./journal/mocks/service.go.tmp
mv ./auth/mocks/auth_client.go ./auth/mocks/auth_client.go.tmp
make mocks
@@ -213,3 +215,4 @@ jobs:
check_mock_changes ./twins/mocks/cache.go "Twins Cache ./twins/mocks/cache.go"
check_mock_changes ./journal/mocks/repository.go "Journal Repository ./journal/mocks/repository.go"
check_mock_changes ./journal/mocks/service.go "Journal Service ./journal/mocks/service.go"
check_mock_changes ./auth/mocks/auth_client.go "Auth Service Client ./auth/mocks/auth_client.go"
+1
View File
@@ -27,6 +27,7 @@ jobs:
uses: golangci/golangci-lint-action@v6
with:
version: v1.59.1
args: --config ./tools/config/.golangci.yml
- name: Build all Binaries
run: |
+2 -1
View File
@@ -99,7 +99,7 @@ FILTERED_SERVICES = $(filter-out $(RUN_ADDON_ARGS), $(SERVICES))
all: $(SERVICES)
.PHONY: all $(SERVICES) dockers dockers_dev latest release run run_addons grpc_mtls_certs check_mtls check_certs test_api
.PHONY: all $(SERVICES) dockers dockers_dev latest release run run_addons grpc_mtls_certs check_mtls check_certs test_api mocks
clean:
rm -rf ${BUILD_DIR}
@@ -121,6 +121,7 @@ install:
mocks:
@which mockery > /dev/null || go install github.com/vektra/mockery/v2@$(MOCKERY_VERSION)
@unset MOCKERY_VERSION && go generate ./...
mockery --config ./tools/config/mockery.yaml
DIRS = consumers readers postgres internal opcua
+1 -1
View File
@@ -1012,7 +1012,7 @@ func TestListPermissions(t *testing.T) {
}
func TestDeleteEntityPolicies(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)
+1176 -80
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -350,9 +350,9 @@ func (pa *policyAgent) RetrieveAllSubjectsCount(ctx context.Context, pr auth.Pol
}
func (pa *policyAgent) RetrievePermissions(ctx context.Context, pr auth.PolicyReq, filterPermission []string) (auth.Permissions, error) {
var permissionChecks []*v1.BulkCheckPermissionRequestItem
var permissionChecks []*v1.CheckBulkPermissionsRequestItem
for _, fp := range filterPermission {
permissionChecks = append(permissionChecks, &v1.BulkCheckPermissionRequestItem{
permissionChecks = append(permissionChecks, &v1.CheckBulkPermissionsRequestItem{
Resource: &v1.ObjectReference{
ObjectType: pr.ObjectType,
ObjectId: pr.Object,
@@ -367,7 +367,7 @@ func (pa *policyAgent) RetrievePermissions(ctx context.Context, pr auth.PolicyRe
},
})
}
resp, err := pa.client.ExperimentalServiceClient.BulkCheckPermission(ctx, &v1.BulkCheckPermissionRequest{
resp, err := pa.client.PermissionsServiceClient.CheckBulkPermissions(ctx, &v1.CheckBulkPermissionsRequest{
Consistency: &v1.Consistency{
Requirement: &v1.Consistency_FullyConsistent{
FullyConsistent: true,
+2 -4
View File
@@ -730,10 +730,9 @@ func TestConnectThing(t *testing.T) {
wrongID := testsutil.GenerateUUID(&testing.T{})
connectedThing := c
connectedThing.State = bootstrap.Active
randomThing := c
randomThingID, err := uuid.NewV4()
randomThingID, _ := uuid.NewV4()
randomThing.ThingID = randomThingID.String()
emptyThing := c
@@ -830,10 +829,9 @@ func TestDisconnectThing(t *testing.T) {
wrongID := testsutil.GenerateUUID(&testing.T{})
connectedThing := c
connectedThing.State = bootstrap.Active
randomThing := c
randomThingID, err := uuid.NewV4()
randomThingID, _ := uuid.NewV4()
randomThing.ThingID = randomThingID.String()
emptyThing := c
+13 -13
View File
@@ -13,7 +13,6 @@ import (
"strings"
"testing"
authmocks "github.com/absmach/magistrala/auth/mocks"
"github.com/absmach/magistrala/consumers/notifiers"
httpapi "github.com/absmach/magistrala/consumers/notifiers/api"
"github.com/absmach/magistrala/consumers/notifiers/mocks"
@@ -27,14 +26,15 @@ import (
)
const (
contentType = "application/json"
email = "user@example.com"
contact1 = "email1@example.com"
contact2 = "email2@example.com"
token = "token"
topic = "topic"
instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
contentType = "application/json"
email = "user@example.com"
contact1 = "email1@example.com"
contact2 = "email2@example.com"
token = "token"
invalidToken = "invalid"
topic = "topic"
instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
)
var (
@@ -145,7 +145,7 @@ func TestCreate(t *testing.T) {
desc: "add with invalid auth token",
req: data,
contentType: contentType,
auth: authmocks.InvalidValue,
auth: invalidToken,
status: http.StatusUnauthorized,
location: "",
err: svcerr.ErrAuthentication,
@@ -249,7 +249,7 @@ func TestView(t *testing.T) {
{
desc: "view with invalid auth token",
id: sub.ID,
auth: authmocks.InvalidValue,
auth: invalidToken,
status: http.StatusUnauthorized,
res: unauthRes,
err: svcerr.ErrAuthentication,
@@ -406,7 +406,7 @@ func TestList(t *testing.T) {
},
{
desc: "list with invalid auth token",
auth: authmocks.InvalidValue,
auth: invalidToken,
status: http.StatusUnauthorized,
res: unauthRes,
err: svcerr.ErrAuthentication,
@@ -477,7 +477,7 @@ func TestRemove(t *testing.T) {
{
desc: "view with invalid auth token",
id: id,
auth: authmocks.InvalidValue,
auth: invalidToken,
status: http.StatusUnauthorized,
res: unauthRes,
err: svcerr.ErrAuthentication,
+5 -2
View File
@@ -25,7 +25,10 @@ import (
"github.com/stretchr/testify/mock"
)
const instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
const (
instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
invalidValue = "invalid"
)
func newService(auth magistrala.AuthzServiceClient) (session.Handler, *pubsub.PubSub) {
pub := new(pubsub.PubSub)
@@ -84,7 +87,7 @@ func TestPublish(t *testing.T) {
ctSenmlCBOR := "application/senml+cbor"
ctJSON := "application/json"
thingKey := "thing_key"
invalidKey := authmocks.InvalidValue
invalidKey := invalidValue
msg := `[{"n":"current","t":-1,"v":1.6}]`
msgJSON := `{"field1":"val1","field2":"val2"}`
msgCBOR := `81A3616E6763757272656E746174206176FB3FF999999999999A`
+3 -2
View File
@@ -28,7 +28,8 @@ var (
DomainID: testsutil.GenerateUUID(&testing.T{}),
Relation: auth.ContributorRelation,
}
validToken = "token"
validToken = "token"
invalidToken = "invalid"
)
func TestSendInvitation(t *testing.T) {
@@ -262,7 +263,7 @@ func TestViewInvitation(t *testing.T) {
},
{
desc: "invalid token",
token: authmocks.InvalidValue,
token: invalidToken,
tokenUserID: "",
userID: validInvitation.UserID,
domainID: validInvitation.DomainID,
+5 -4
View File
@@ -29,7 +29,8 @@ const (
password = "password"
password1 = "password1"
chanID = "123e4567-e89b-12d3-a456-000000000001"
invalidID = authmocks.InvalidValue
invalidID = "invalidID"
invalidValue = "invalidValue"
clientID = "clientID"
clientID1 = "clientID1"
subtopic = "testSubtopic"
@@ -39,11 +40,11 @@ const (
var (
topicMsg = "channels/%s/messages"
topic = fmt.Sprintf(topicMsg, chanID)
invalidTopic = authmocks.InvalidValue
invalidTopic = invalidValue
payload = []byte("[{'n':'test-name', 'v': 1.2}]")
topics = []string{topic}
invalidTopics = []string{authmocks.InvalidValue}
invalidChanIDTopics = []string{fmt.Sprintf(topicMsg, authmocks.InvalidValue)}
invalidTopics = []string{invalidValue}
invalidChanIDTopics = []string{fmt.Sprintf(topicMsg, invalidValue)}
// Test log messages for cases the handler does not provide a return value.
logBuffer = bytes.Buffer{}
sessionClient = session.Session{
+2 -2
View File
@@ -293,8 +293,8 @@ func TestReadMessages(t *testing.T) {
Publisher: validID,
},
authRes: &magistrala.AuthorizeRes{Authorized: false, Id: ""},
authErr: svcerr.ErrAuthorization,
repoRes: readers.MessagesPage{},
repoErr: nil,
response: sdk.MessagesPage{},
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthorization, svcerr.ErrAuthorization), http.StatusUnauthorized),
},
@@ -311,8 +311,8 @@ func TestReadMessages(t *testing.T) {
Publisher: validID,
},
authRes: &magistrala.AuthorizeRes{Authorized: false, Id: ""},
authErr: svcerr.ErrAuthorization,
repoRes: readers.MessagesPage{},
repoErr: nil,
response: sdk.MessagesPage{},
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, apiutil.ErrBearerToken), http.StatusUnauthorized),
},
+3 -2
View File
@@ -30,6 +30,7 @@ const (
svcName = "test-service"
thingToken = "1"
userToken = "token"
invalidToken = "invalid"
email = "user@example.com"
invalid = "invalid"
numOfMessages = 100
@@ -213,7 +214,7 @@ func TestReadAll(t *testing.T) {
{
desc: "read page with invalid token as thing",
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=10", ts.URL, chanID),
token: authmocks.InvalidValue,
token: invalidToken,
authResponse: false,
status: http.StatusUnauthorized,
err: svcerr.ErrAuthorization,
@@ -622,7 +623,7 @@ func TestReadAll(t *testing.T) {
{
desc: "read page with invalid token as user",
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=10", ts.URL, chanID),
token: authmocks.InvalidValue,
token: invalidToken,
authResponse: false,
status: http.StatusUnauthorized,
err: svcerr.ErrAuthorization,
+4 -4
View File
@@ -375,7 +375,7 @@ func TestViewClient(t *testing.T) {
{
desc: "view client with an invalid token",
response: mgclients.Client{},
token: authmocks.InvalidValue,
token: inValidToken,
clientID: "",
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
authorizeErr: svcerr.ErrAuthorization,
@@ -1475,7 +1475,7 @@ func TestListMembers(t *testing.T) {
},
{
desc: "list members with an invalid token",
token: authmocks.InvalidValue,
token: inValidToken,
groupID: testsutil.GenerateUUID(t),
identifyResponse: &magistrala.IdentityRes{},
response: mgclients.MembersPage{
@@ -1637,7 +1637,7 @@ func TestDeleteClient(t *testing.T) {
},
{
desc: "Delete client with unauthorized token",
token: authmocks.InvalidValue,
token: inValidToken,
clientID: client.ID,
identifyResponse: &magistrala.IdentityRes{},
identifyErr: svcerr.ErrAuthentication,
@@ -1646,7 +1646,7 @@ func TestDeleteClient(t *testing.T) {
{
desc: "Delete invalid client",
token: validToken,
clientID: authmocks.InvalidValue,
clientID: wrongID,
identifyResponse: &magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
authorizeErr: svcerr.ErrAuthorization,
+3
View File
@@ -0,0 +1,3 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
+15
View File
@@ -0,0 +1,15 @@
# Copyright (c) Abstract Machines
# SPDX-License-Identifier: Apache-2.0
with-expecter: true
filename: "{{.InterfaceName}}.go"
outpkg: "mocks"
boilerplate-file: "./tools/config/boilerplate.txt"
packages:
github.com/absmach/magistrala:
interfaces:
AuthServiceClient:
config:
dir: "./auth/mocks"
mockname: "AuthClient"
filename: "auth_client.go"
+33 -22
View File
@@ -11,6 +11,7 @@ import (
"io"
"os"
"strconv"
"sync"
"time"
mglog "github.com/absmach/magistrala/logger"
@@ -34,7 +35,7 @@ func Benchmark(cfg Config) error {
defer func() {
if err = caFile.Close(); err != nil {
logger.Warn(fmt.Sprintf("Could not close file: %s", err))
logger.Warn(fmt.Sprintf("Could not close file: %s", err))
}
}()
if err != nil {
@@ -63,30 +64,41 @@ func Benchmark(cfg Config) error {
start := time.Now()
// Publishers
var wg sync.WaitGroup
errorChan := make(chan error, cfg.Test.Pubs)
for i := 0; i < cfg.Test.Pubs; i++ {
mgChan := mg.Channels[i%n]
mgThing := mg.Things[i%n]
wg.Add(1)
go func(i int) {
defer wg.Done()
mgChan := mg.Channels[i%n]
mgThing := mg.Things[i%n]
if cfg.MQTT.TLS.MTLS {
cert, err = tls.X509KeyPair([]byte(mgThing.MTLSCert), []byte(mgThing.MTLSKey))
if err != nil {
return err
if cfg.MQTT.TLS.MTLS {
cert, err = tls.X509KeyPair([]byte(mgThing.MTLSCert), []byte(mgThing.MTLSKey))
if err != nil {
errorChan <- err
return
}
}
}
c, err := makeClient(i, cfg, mgChan, mgThing, startStamp, caByte, cert)
c, err := makeClient(i, cfg, mgChan, mgThing, startStamp, caByte, cert)
if err != nil {
errorChan <- fmt.Errorf("unable to create message payload %s", err.Error())
return
}
c.publish(resCh, errorChan)
}(i)
}
go func() {
wg.Wait()
close(errorChan)
}()
for err := range errorChan {
if err != nil {
return fmt.Errorf("unable to create message payload %s", err.Error())
}
errorChan := make(chan error)
go c.publish(resCh, errorChan)
for {
err := <-errorChan
if err != nil {
return err
}
return err
}
}
@@ -112,7 +124,6 @@ func Benchmark(cfg Config) error {
return fmt.Errorf("totals not assigned")
}
// Print sats
printResults(results, totals, cfg.MQTT.Message.Format, cfg.Log.Quiet)
return nil
}
+22 -20
View File
@@ -24,8 +24,10 @@ import (
)
const (
numRecs = 100
publisher = "twins"
numRecs = 100
publisher = "twins"
validToken = "validToken"
invalidToken = "invalidToken"
)
var (
@@ -81,7 +83,7 @@ func TestListStates(t *testing.T) {
queryFmt := "%s?offset=%d&limit=%d"
cases := []struct {
desc string
auth string
token string
status int
url string
res []stateRes
@@ -92,7 +94,7 @@ func TestListStates(t *testing.T) {
}{
{
desc: "get a list of states",
auth: token,
token: validToken,
status: http.StatusOK,
url: baseURL,
res: data[0:10],
@@ -105,7 +107,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with valid offset and limit",
auth: token,
token: validToken,
status: http.StatusOK,
url: fmt.Sprintf(queryFmt, baseURL, 20, 15),
res: data[20:35],
@@ -118,7 +120,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with invalid token",
auth: authmocks.InvalidValue,
token: invalidToken,
status: http.StatusUnauthorized,
url: fmt.Sprintf(queryFmt, baseURL, 0, 5),
res: nil,
@@ -127,7 +129,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with empty token",
auth: "",
token: "",
status: http.StatusUnauthorized,
url: fmt.Sprintf(queryFmt, baseURL, 0, 5),
res: nil,
@@ -136,7 +138,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with + limit > total",
auth: token,
token: validToken,
status: http.StatusOK,
url: fmt.Sprintf(queryFmt, baseURL, 91, 20),
res: data[91:],
@@ -149,7 +151,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with negative offset",
auth: token,
token: validToken,
status: http.StatusBadRequest,
url: fmt.Sprintf(queryFmt, baseURL, -1, 5),
res: nil,
@@ -159,7 +161,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with negative limit",
auth: token,
token: validToken,
status: http.StatusBadRequest,
url: fmt.Sprintf(queryFmt, baseURL, 0, -5),
res: nil,
@@ -169,7 +171,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with zero limit",
auth: token,
token: validToken,
status: http.StatusBadRequest,
url: fmt.Sprintf(queryFmt, baseURL, 0, 0),
res: nil,
@@ -179,7 +181,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with limit greater than max",
auth: token,
token: validToken,
status: http.StatusBadRequest,
url: fmt.Sprintf(queryFmt, baseURL, 0, 110),
res: nil,
@@ -189,7 +191,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with invalid offset",
auth: token,
token: validToken,
status: http.StatusBadRequest,
url: fmt.Sprintf("%s?offset=invalid&limit=%d", baseURL, 15),
res: nil,
@@ -199,7 +201,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with invalid limit",
auth: token,
token: validToken,
status: http.StatusBadRequest,
url: fmt.Sprintf("%s?offset=%d&limit=invalid", baseURL, 0),
res: nil,
@@ -209,7 +211,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states without offset",
auth: token,
token: validToken,
status: http.StatusOK,
url: fmt.Sprintf("%s?limit=%d", baseURL, 15),
res: data[0:15],
@@ -222,7 +224,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states without limit",
auth: token,
token: validToken,
status: http.StatusOK,
url: fmt.Sprintf("%s?offset=%d", baseURL, 14),
res: data[14:24],
@@ -235,7 +237,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with invalid number of parameters",
auth: token,
token: validToken,
status: http.StatusBadRequest,
url: fmt.Sprintf("%s%s", baseURL, "?offset=4&limit=4&limit=5&offset=5"),
res: nil,
@@ -245,7 +247,7 @@ func TestListStates(t *testing.T) {
},
{
desc: "get a list of states with redundant query parameters",
auth: token,
token: validToken,
status: http.StatusOK,
url: fmt.Sprintf("%s?offset=%d&limit=%d&value=something", baseURL, 0, 5),
res: data[0:5],
@@ -259,13 +261,13 @@ func TestListStates(t *testing.T) {
}
for _, tc := range cases {
authCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.auth}).Return(&magistrala.IdentityRes{Id: tc.userID}, tc.identifyErr)
authCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: tc.userID}, tc.identifyErr)
repoCall := stateRepo.On("RetrieveAll", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.page, tc.err)
req := testRequest{
client: ts.Client(),
method: http.MethodGet,
url: tc.url,
token: tc.auth,
token: tc.token,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
+16 -19
View File
@@ -14,7 +14,6 @@ import (
"testing"
"github.com/absmach/magistrala"
authmocks "github.com/absmach/magistrala/auth/mocks"
"github.com/absmach/magistrala/internal/testsutil"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/apiutil"
@@ -26,15 +25,16 @@ import (
)
const (
twinName = "name"
contentType = "application/json"
email = "user@example.com"
token = "token"
wrongID = 0
maxNameSize = 1024
instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
retained = "saved"
validID = "123e4567-e89b-12d3-a456-426614174000"
twinName = "name"
contentType = "application/json"
email = "user@example.com"
token = "token"
invalidtoken = "invalid"
wrongID = 0
maxNameSize = 1024
instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
retained = "saved"
validID = "123e4567-e89b-12d3-a456-426614174000"
)
var invalidName = strings.Repeat("m", maxNameSize+1)
@@ -153,7 +153,7 @@ func TestAddTwin(t *testing.T) {
desc: "add twin with invalid auth token",
req: data,
contentType: contentType,
auth: authmocks.InvalidValue,
auth: invalidtoken,
status: http.StatusUnauthorized,
location: "",
err: svcerr.ErrAuthentication,
@@ -318,7 +318,7 @@ func TestUpdateTwin(t *testing.T) {
req: data,
id: twin.ID,
contentType: contentType,
auth: authmocks.InvalidValue,
auth: invalidtoken,
status: http.StatusUnauthorized,
err: svcerr.ErrAuthentication,
retrieveErr: svcerr.ErrNotFound,
@@ -468,7 +468,7 @@ func TestViewTwin(t *testing.T) {
{
desc: "view twin by passing invalid token",
id: twin.ID,
auth: authmocks.InvalidValue,
auth: invalidtoken,
status: http.StatusForbidden,
res: twinRes{},
err: svcerr.ErrAuthentication,
@@ -560,7 +560,7 @@ func TestListTwins(t *testing.T) {
},
{
desc: "get a list of twins with invalid token",
auth: authmocks.InvalidValue,
auth: invalidtoken,
status: http.StatusUnauthorized,
url: fmt.Sprintf(queryFmt, baseURL, 0, 1),
res: nil,
@@ -767,10 +767,7 @@ func TestRemoveTwin(t *testing.T) {
defer ts.Close()
twin := twins.Twin{
Owner: email,
ID: testsutil.GenerateUUID(t),
Name: twinName,
Revision: 50,
ID: testsutil.GenerateUUID(t),
}
cases := []struct {
@@ -816,7 +813,7 @@ func TestRemoveTwin(t *testing.T) {
{
desc: "delete twin with invalid token",
id: twin.ID,
auth: authmocks.InvalidValue,
auth: invalidtoken,
status: http.StatusUnauthorized,
err: svcerr.ErrAuthentication,
removeErr: svcerr.ErrRemoveEntity,
+14 -13
View File
@@ -23,13 +23,14 @@ import (
)
const (
twinName = "name"
wrongID = ""
token = "token"
email = "user@example.com"
numRecs = 100
retained = "saved"
validID = "123e4567-e89b-12d3-a456-426614174000"
twinName = "name"
wrongID = ""
token = "token"
invalidToken = "invalidToken"
email = "user@example.com"
numRecs = 100
retained = "saved"
validID = "123e4567-e89b-12d3-a456-426614174000"
)
var (
@@ -75,7 +76,7 @@ func TestAddTwin(t *testing.T) {
{
desc: "add twin with wrong credentials",
twin: twin,
token: authmocks.InvalidValue,
token: invalidToken,
err: svcerr.ErrAuthentication,
saveErr: svcerr.ErrCreateEntity,
identifyErr: svcerr.ErrAuthentication,
@@ -128,7 +129,7 @@ func TestUpdateTwin(t *testing.T) {
{
desc: "update twin with wrong credentials",
twin: twin,
token: authmocks.InvalidValue,
token: invalidToken,
err: svcerr.ErrAuthentication,
retrieveErr: svcerr.ErrNotFound,
updateErr: svcerr.ErrUpdateEntity,
@@ -188,7 +189,7 @@ func TestViewTwin(t *testing.T) {
{
desc: "view twin with wrong credentials",
id: twin.ID,
token: authmocks.InvalidValue,
token: invalidToken,
err: svcerr.ErrAuthentication,
identifyErr: svcerr.ErrAuthentication,
},
@@ -265,7 +266,7 @@ func TestListTwins(t *testing.T) {
},
{
desc: "list with wrong credentials",
token: authmocks.InvalidValue,
token: invalidToken,
limit: 0,
offset: n,
err: svcerr.ErrAuthentication,
@@ -303,7 +304,7 @@ func TestRemoveTwin(t *testing.T) {
{
desc: "remove twin with wrong credentials",
id: twin.ID,
token: authmocks.InvalidValue,
token: invalidToken,
err: svcerr.ErrAuthentication,
removeErr: svcerr.ErrRemoveEntity,
identifyErr: svcerr.ErrAuthentication,
@@ -545,7 +546,7 @@ func TestListStates(t *testing.T) {
{
desc: "get a list with wrong user token",
id: twin.ID,
token: authmocks.InvalidValue,
token: invalidToken,
offset: 0,
limit: 10,
size: 0,
+9 -6
View File
@@ -394,12 +394,15 @@ func TestViewClient(t *testing.T) {
checkSuperAdminErr: svcerr.ErrAuthorization,
},
{
desc: "view client with an invalid token",
identifyResponse: &magistrala.IdentityRes{},
response: mgclients.Client{},
token: inValidToken,
err: svcerr.ErrAuthentication,
checkSuperAdminErr: svcerr.ErrAuthorization,
desc: "view client with an invalid token",
token: inValidToken,
clientID: clientID,
identifyResponse: &magistrala.IdentityRes{},
authorizeResponse: &magistrala.AuthorizeRes{},
retrieveByIDResponse: mgclients.Client{},
response: mgclients.Client{},
identifyErr: svcerr.ErrAuthentication,
err: svcerr.ErrAuthentication,
},
{
desc: "view client as normal user with failed to retrieve client",
+11 -9
View File
@@ -20,11 +20,13 @@ import (
)
const (
chanID = "1"
id = "1"
thingKey = "thing_key"
subTopic = "subtopic"
protocol = "ws"
chanID = "1"
invalidID = "invalidID"
invalidKey = "invalidKey"
id = "1"
thingKey = "thing_key"
subTopic = "subtopic"
protocol = "ws"
)
var msg = messaging.Message{
@@ -77,10 +79,10 @@ func TestSubscribe(t *testing.T) {
},
{
desc: "subscribe to channel with invalid chanID and invalid thingKey",
thingKey: authmocks.InvalidValue,
chanID: authmocks.InvalidValue,
thingKey: invalidKey,
chanID: invalidID,
subtopic: subTopic,
err: svcerr.ErrAuthorization,
err: ws.ErrFailedSubscription,
},
{
desc: "subscribe to channel with empty channel",
@@ -109,7 +111,7 @@ func TestSubscribe(t *testing.T) {
thingID := testsutil.GenerateUUID(t)
subConfig := messaging.SubscriberConfig{
ID: thingID,
Topic: "channels." + chanID + "." + subTopic,
Topic: "channels." + tc.chanID + "." + subTopic,
Handler: c,
}
repocall := pubsub.On("Subscribe", mock.Anything, subConfig).Return(tc.err)