Skip to content

Commit

Permalink
Fix failing ci
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Aug 12, 2024
1 parent 5d1c0b4 commit ec356c5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 40 deletions.
38 changes: 20 additions & 18 deletions bootstrap/events/producer/streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
tauthmocks "github.com/absmach/magistrala/things/mocks"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -84,24 +85,25 @@ var (
}
)

func newService(t *testing.T, url string) (bootstrap.Service, *mocks.ConfigRepository, *authmocks.AuthClient, *sdkmocks.SDK) {
func newService(t *testing.T, url string) (bootstrap.Service, *mocks.ConfigRepository, *authmocks.AuthClient, *tauthmocks.ThingsAuthClient, *sdkmocks.SDK) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthClient)
tauth := new(tauthmocks.ThingsAuthClient)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, boot, sdk, encKey, idp)
svc := bootstrap.New(auth, tauth, boot, sdk, encKey, idp)
publisher, err := store.NewPublisher(context.Background(), url, streamID)
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
svc = producer.NewEventStoreMiddleware(svc, publisher)

return svc, boot, auth, sdk
return svc, boot, auth, tauth, sdk
}

func TestAdd(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, auth, sdk := newService(t, redisURL)
svc, boot, auth, tAuth, sdk := newService(t, redisURL)

var channels []string
for _, ch := range config.Channels {
Expand Down Expand Up @@ -211,7 +213,7 @@ func TestAdd(t *testing.T) {
authCall1 := auth.On("Authorize", context.Background(), mock.Anything).Return(tc.authResponse, tc.authorizeErr)
sdkCall := sdk.On("Thing", tc.config.ThingID, tc.token).Return(mgsdk.Thing{ID: tc.config.ThingID, Credentials: mgsdk.Credentials{Secret: tc.config.ThingKey}}, errors.NewSDKError(tc.thingErr))
repoCall := boot.On("ListExisting", context.Background(), domainID, mock.Anything).Return(tc.config.Channels, tc.listErr)
authCall2 := auth.On("VerifyConnections", mock.Anything, mock.Anything).Return(tc.verifyResponse, tc.verifyErr)
authCall2 := tAuth.On("VerifyConnections", mock.Anything, mock.Anything).Return(tc.verifyResponse, tc.verifyErr)
repoCall1 := boot.On("Save", context.Background(), mock.Anything, mock.Anything).Return(mock.Anything, tc.saveErr)

_, err := svc.Add(context.Background(), tc.token, tc.config)
Expand Down Expand Up @@ -244,7 +246,7 @@ func TestView(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, auth, _ := newService(t, redisURL)
svc, boot, auth, _, _ := newService(t, redisURL)

nonExisting := config
nonExisting.ThingID = unknownThingID
Expand Down Expand Up @@ -347,7 +349,7 @@ func TestUpdate(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, auth, _ := newService(t, redisURL)
svc, boot, auth, _, _ := newService(t, redisURL)

c := config

Expand Down Expand Up @@ -467,7 +469,7 @@ func TestUpdateConnections(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, auth, sdk := newService(t, redisURL)
svc, boot, auth, _, sdk := newService(t, redisURL)

cases := []struct {
desc string
Expand Down Expand Up @@ -610,7 +612,7 @@ func TestUpdateCert(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, auth, _ := newService(t, redisURL)
svc, boot, auth, _, _ := newService(t, redisURL)

cases := []struct {
desc string
Expand Down Expand Up @@ -777,7 +779,7 @@ func TestUpdateCert(t *testing.T) {
}

func TestList(t *testing.T) {
svc, boot, auth, _ := newService(t, redisURL)
svc, boot, auth, _, _ := newService(t, redisURL)
numThings := 101
var c bootstrap.Config
saved := make([]bootstrap.Config, 0)
Expand Down Expand Up @@ -1055,7 +1057,7 @@ func TestRemove(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, auth, _ := newService(t, redisURL)
svc, boot, auth, _, _ := newService(t, redisURL)

nonExisting := config
nonExisting.ThingID = unknownThingID
Expand Down Expand Up @@ -1150,7 +1152,7 @@ func TestBootstrap(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, _, _ := newService(t, redisURL)
svc, boot, _, _, _ := newService(t, redisURL)

cases := []struct {
desc string
Expand Down Expand Up @@ -1213,7 +1215,7 @@ func TestChangeState(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, auth, sdk := newService(t, redisURL)
svc, boot, auth, _, sdk := newService(t, redisURL)

cases := []struct {
desc string
Expand Down Expand Up @@ -1324,7 +1326,7 @@ func TestUpdateChannelHandler(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, _, _ := newService(t, redisURL)
svc, boot, _, _, _ := newService(t, redisURL)

cases := []struct {
desc string
Expand Down Expand Up @@ -1406,7 +1408,7 @@ func TestRemoveChannelHandler(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, _, _ := newService(t, redisURL)
svc, boot, _, _, _ := newService(t, redisURL)

cases := []struct {
desc string
Expand Down Expand Up @@ -1468,7 +1470,7 @@ func TestRemoveConfigHandler(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, _, _ := newService(t, redisURL)
svc, boot, _, _, _ := newService(t, redisURL)

cases := []struct {
desc string
Expand Down Expand Up @@ -1530,7 +1532,7 @@ func TestConnectThingHandler(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, _, _ := newService(t, redisURL)
svc, boot, _, _, _ := newService(t, redisURL)

cases := []struct {
desc string
Expand Down Expand Up @@ -1604,7 +1606,7 @@ func TestDisconnectThingHandler(t *testing.T) {
err := redisClient.FlushAll(context.Background()).Err()
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

svc, boot, _, _ := newService(t, redisURL)
svc, boot, _, _, _ := newService(t, redisURL)

cases := []struct {
desc string
Expand Down
6 changes: 4 additions & 2 deletions bootstrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,20 @@ type ConfigReader interface {

type bootstrapService struct {
auth magistrala.AuthServiceClient
tauth magistrala.AuthzServiceClient
configs ConfigRepository
sdk mgsdk.SDK
encKey []byte
idProvider magistrala.IDProvider
}

// New returns new Bootstrap service.
func New(uauth magistrala.AuthServiceClient, configs ConfigRepository, sdk mgsdk.SDK, encKey []byte, idp magistrala.IDProvider) Service {
func New(uauth magistrala.AuthServiceClient, tauth magistrala.AuthzServiceClient, configs ConfigRepository, sdk mgsdk.SDK, encKey []byte, idp magistrala.IDProvider) Service {
return &bootstrapService{
configs: configs,
sdk: sdk,
auth: uauth,
tauth: tauth,
encKey: encKey,
idProvider: idp,
}
Expand Down Expand Up @@ -175,7 +177,7 @@ func (bs bootstrapService) Add(ctx context.Context, token string, cfg Config) (C
}
}

resp, err := bs.auth.VerifyConnections(ctx, &magistrala.VerifyConnectionsReq{
resp, err := bs.tauth.VerifyConnections(ctx, &magistrala.VerifyConnectionsReq{
ThingsId: []string{cfg.ThingID},
GroupsId: bs.toIDList(cfg.Channels),
})
Expand Down
36 changes: 19 additions & 17 deletions bootstrap/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
tauthmocks "github.com/absmach/magistrala/things/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -62,13 +63,14 @@ var (
}
)

func newService() (bootstrap.Service, *mocks.ConfigRepository, *authmocks.AuthClient, *sdkmocks.SDK) {
func newService() (bootstrap.Service, *mocks.ConfigRepository, *authmocks.AuthClient, *tauthmocks.ThingsAuthClient, *sdkmocks.SDK) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthClient)
tauth := new(tauthmocks.ThingsAuthClient)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()

return bootstrap.New(auth, boot, sdk, encKey, idp), boot, auth, sdk
return bootstrap.New(auth, tauth, boot, sdk, encKey, idp), boot, auth, tauth, sdk
}

func enc(in []byte) ([]byte, error) {
Expand All @@ -87,7 +89,7 @@ func enc(in []byte) ([]byte, error) {
}

func TestAdd(t *testing.T) {
c, boot, auth, sdk := newService()
c, boot, auth, tAuth, sdk := newService()
neID := config
neID.ThingID = "non-existent"

Expand Down Expand Up @@ -231,7 +233,7 @@ func TestAdd(t *testing.T) {
repoCall1 := sdk.On("CreateThing", mock.Anything, tc.token).Return(mgsdk.Thing{}, tc.createThingErr)
repoCall2 := sdk.On("DeleteThing", tc.config.ThingID, tc.token).Return(tc.deleteThingErr)
repoCall3 := boot.On("ListExisting", context.Background(), tc.domainID, mock.Anything).Return(tc.config.Channels, tc.listExistingErr)
authCall2 := auth.On("VerifyConnections", mock.Anything, mock.Anything).Return(tc.verifyResponse, tc.verifyErr)
authCall2 := tAuth.On("VerifyConnections", mock.Anything, mock.Anything).Return(tc.verifyResponse, tc.verifyErr)
repoCall4 := boot.On("Save", context.Background(), mock.Anything, mock.Anything).Return(mock.Anything, tc.saveErr)

_, err := c.Add(context.Background(), tc.token, tc.config)
Expand All @@ -249,7 +251,7 @@ func TestAdd(t *testing.T) {
}

func TestView(t *testing.T) {
svc, boot, auth, _ := newService()
svc, boot, auth, _, _ := newService()

cases := []struct {
desc string
Expand Down Expand Up @@ -343,7 +345,7 @@ func TestView(t *testing.T) {
}

func TestUpdate(t *testing.T) {
svc, boot, auth, _ := newService()
svc, boot, auth, _, _ := newService()
c := config

ch := channel
Expand Down Expand Up @@ -430,7 +432,7 @@ func TestUpdate(t *testing.T) {
}

func TestUpdateCert(t *testing.T) {
svc, boot, auth, _ := newService()
svc, boot, auth, _, _ := newService()
c := config

ch := channel
Expand Down Expand Up @@ -541,7 +543,7 @@ func TestUpdateCert(t *testing.T) {
}

func TestUpdateConnections(t *testing.T) {
svc, boot, auth, sdk := newService()
svc, boot, auth, _, sdk := newService()
c := config
c.State = bootstrap.Inactive

Expand Down Expand Up @@ -655,7 +657,7 @@ func TestUpdateConnections(t *testing.T) {
}

func TestList(t *testing.T) {
svc, boot, auth, _ := newService()
svc, boot, auth, _, _ := newService()
numThings := 101
var saved []bootstrap.Config
for i := 0; i < numThings; i++ {
Expand Down Expand Up @@ -1016,7 +1018,7 @@ func TestList(t *testing.T) {
}

func TestRemove(t *testing.T) {
svc, boot, auth, _ := newService()
svc, boot, auth, _, _ := newService()
c := config
cases := []struct {
desc string
Expand Down Expand Up @@ -1099,7 +1101,7 @@ func TestRemove(t *testing.T) {
}

func TestBootstrap(t *testing.T) {
svc, boot, _, _ := newService()
svc, boot, _, _, _ := newService()
c := config
e, err := enc([]byte(c.ExternalKey))
assert.Nil(t, err, fmt.Sprintf("Encrypting external key expected to succeed: %s.\n", err))
Expand Down Expand Up @@ -1166,7 +1168,7 @@ func TestBootstrap(t *testing.T) {
}

func TestChangeState(t *testing.T) {
svc, boot, auth, sdk := newService()
svc, boot, auth, _, sdk := newService()

c := config
cases := []struct {
Expand Down Expand Up @@ -1267,7 +1269,7 @@ func TestChangeState(t *testing.T) {
}

func TestUpdateChannelHandler(t *testing.T) {
svc, boot, _, _ := newService()
svc, boot, _, _, _ := newService()
ch := bootstrap.Channel{
ID: channel.ID,
Name: "new name",
Expand Down Expand Up @@ -1300,7 +1302,7 @@ func TestUpdateChannelHandler(t *testing.T) {
}

func TestRemoveChannelHandler(t *testing.T) {
svc, boot, _, _ := newService()
svc, boot, _, _, _ := newService()

cases := []struct {
desc string
Expand Down Expand Up @@ -1328,7 +1330,7 @@ func TestRemoveChannelHandler(t *testing.T) {
}

func TestRemoveConfigHandler(t *testing.T) {
svc, boot, _, _ := newService()
svc, boot, _, _, _ := newService()

cases := []struct {
desc string
Expand Down Expand Up @@ -1356,7 +1358,7 @@ func TestRemoveConfigHandler(t *testing.T) {
}

func TestConnectThingsHandler(t *testing.T) {
svc, boot, _, _ := newService()
svc, boot, _, _, _ := newService()
cases := []struct {
desc string
thingID string
Expand Down Expand Up @@ -1386,7 +1388,7 @@ func TestConnectThingsHandler(t *testing.T) {
}

func TestDisconnectThingsHandler(t *testing.T) {
svc, boot, _, _ := newService()
svc, boot, _, _, _ := newService()
cases := []struct {
desc string
thingID string
Expand Down
Loading

0 comments on commit ec356c5

Please sign in to comment.