Skip to content

Commit

Permalink
MG-2403 - Add wrapper for policy CRUD (absmach#2402)
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
felixgateru authored and nyagamunene committed Oct 8, 2024
1 parent 858045b commit 29d0bf4
Show file tree
Hide file tree
Showing 17 changed files with 969 additions and 526 deletions.
25 changes: 13 additions & 12 deletions bootstrap/events/producer/streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/pkg/events/store"
policymocks "github.com/absmach/magistrala/pkg/policy/mocks"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
Expand Down Expand Up @@ -88,14 +89,14 @@ type testVariable struct {
svc bootstrap.Service
boot *mocks.ConfigRepository
auth *authmocks.AuthServiceClient
policy *authmocks.PolicyServiceClient
policy *policymocks.PolicyService
sdk *sdkmocks.SDK
}

func newTestVariable(t *testing.T, redisURL string) testVariable {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -814,7 +815,7 @@ func TestList(t *testing.T) {
identifyErr error
superAdminAuthRes *magistrala.AuthorizeRes
domainAdminAuthRes *magistrala.AuthorizeRes
listObjectsResponse *magistrala.ListObjectsRes
listObjectsResponse []string
listObjectsErr error
superAdmiAuthErr error
domainAdmiAuthErr error
Expand All @@ -837,7 +838,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
err: nil,
event: map[string]interface{}{
"thing_id": c.ThingID,
Expand Down Expand Up @@ -866,7 +867,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
err: nil,
event: map[string]interface{}{
"thing_id": c.ThingID,
Expand Down Expand Up @@ -895,7 +896,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
err: nil,
event: map[string]interface{}{
"thing_id": c.ThingID,
Expand Down Expand Up @@ -926,7 +927,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
superAdmiAuthErr: svcerr.ErrAuthorization,
err: nil,
Expand All @@ -940,7 +941,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
superAdmiAuthErr: svcerr.ErrAuthorization,
Expand All @@ -956,7 +957,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsErr: svcerr.ErrNotFound,
Expand All @@ -972,7 +973,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
retrieveErr: nil,
err: nil,
Expand All @@ -986,7 +987,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
retrieveErr: nil,
Expand All @@ -1001,7 +1002,7 @@ func TestList(t *testing.T) {
filter: bootstrap.Filter{},
offset: 0,
limit: 10,
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
retrieveErr: nil,
Expand Down
9 changes: 5 additions & 4 deletions bootstrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/absmach/magistrala/pkg/errors"
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/pkg/policy"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
)

Expand Down Expand Up @@ -121,20 +122,20 @@ type ConfigReader interface {

type bootstrapService struct {
auth grpcclient.AuthServiceClient
policy magistrala.PolicyServiceClient
policy policy.PolicyService
configs ConfigRepository
sdk mgsdk.SDK
encKey []byte
idProvider magistrala.IDProvider
}

// New returns new Bootstrap service.
func New(auth grpcclient.AuthServiceClient, policy magistrala.PolicyServiceClient, configs ConfigRepository, sdk mgsdk.SDK, encKey []byte, idp magistrala.IDProvider) Service {
func New(auth grpcclient.AuthServiceClient, policyService policy.PolicyService, configs ConfigRepository, sdk mgsdk.SDK, encKey []byte, idp magistrala.IDProvider) Service {
return &bootstrapService{
configs: configs,
sdk: sdk,
auth: auth,
policy: policy,
policy: policyService,
encKey: encKey,
idProvider: idp,
}
Expand Down Expand Up @@ -314,7 +315,7 @@ func (bs bootstrapService) listClientIDs(ctx context.Context, userID string) ([]
if err != nil {
return nil, errors.Wrap(svcerr.ErrNotFound, err)
}
return tids.Policies, nil
return tids, nil
}

func (bs bootstrapService) checkSuperAdmin(ctx context.Context, userID string) error {
Expand Down
49 changes: 25 additions & 24 deletions bootstrap/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/absmach/magistrala/internal/testsutil"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
policymocks "github.com/absmach/magistrala/pkg/policy/mocks"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
Expand Down Expand Up @@ -77,7 +78,7 @@ func enc(in []byte) ([]byte, error) {
func TestAdd(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -213,7 +214,7 @@ func TestAdd(t *testing.T) {
func TestView(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -312,7 +313,7 @@ func TestView(t *testing.T) {
func TestUpdate(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -404,7 +405,7 @@ func TestUpdate(t *testing.T) {
func TestUpdateCert(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -520,7 +521,7 @@ func TestUpdateCert(t *testing.T) {
func TestUpdateConnections(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -640,7 +641,7 @@ func TestUpdateConnections(t *testing.T) {
func TestList(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -670,7 +671,7 @@ func TestList(t *testing.T) {
domainAdminAuthRes *magistrala.AuthorizeRes
superAdmiAuthErr error
domainAdmiAuthErr error
listObjectsResponse *magistrala.ListObjectsRes
listObjectsResponse []string
authorizeErr error
identifyErr error
listObjectsErr error
Expand All @@ -690,7 +691,7 @@ func TestList(t *testing.T) {
userID: validID,
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
offset: 0,
limit: 10,
err: nil,
Expand All @@ -703,7 +704,7 @@ func TestList(t *testing.T) {
userID: validID,
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
offset: 0,
limit: 10,
err: nil,
Expand All @@ -722,7 +723,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
offset: 0,
limit: 10,
err: nil,
Expand All @@ -736,7 +737,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
offset: 0,
limit: 10,
err: nil,
Expand All @@ -755,7 +756,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse: []string{"test", "test"},
offset: 0,
limit: 10,
err: nil,
Expand Down Expand Up @@ -809,7 +810,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse: []string{"test", "test"},
offset: 0,
limit: 100,
err: nil,
Expand Down Expand Up @@ -890,7 +891,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse: []string{"test", "test"},
offset: 95,
limit: 10,
err: nil,
Expand Down Expand Up @@ -945,7 +946,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse: []string{"test", "test"},
offset: 35,
limit: 20,
err: nil,
Expand All @@ -961,7 +962,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
listObjectsErr: svcerr.ErrNotFound,
err: svcerr.ErrNotFound,
},
Expand Down Expand Up @@ -1007,7 +1008,7 @@ func TestList(t *testing.T) {
func TestRemove(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1096,7 +1097,7 @@ func TestRemove(t *testing.T) {
func TestBootstrap(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1169,7 +1170,7 @@ func TestBootstrap(t *testing.T) {
func TestChangeState(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1275,7 +1276,7 @@ func TestChangeState(t *testing.T) {
func TestUpdateChannelHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1314,7 +1315,7 @@ func TestUpdateChannelHandler(t *testing.T) {
func TestRemoveChannelHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1347,7 +1348,7 @@ func TestRemoveChannelHandler(t *testing.T) {
func TestRemoveConfigHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1380,7 +1381,7 @@ func TestRemoveConfigHandler(t *testing.T) {
func TestConnectThingsHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1416,7 +1417,7 @@ func TestConnectThingsHandler(t *testing.T) {
func TestDisconnectThingsHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down
5 changes: 4 additions & 1 deletion cmd/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/absmach/magistrala/bootstrap/events/producer"
bootstrappg "github.com/absmach/magistrala/bootstrap/postgres"
"github.com/absmach/magistrala/bootstrap/tracing"
mgpolicy "github.com/absmach/magistrala/internal/policy"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/events"
"github.com/absmach/magistrala/pkg/events/store"
Expand Down Expand Up @@ -193,7 +194,9 @@ func newService(ctx context.Context, authClient authclient.AuthServiceClient, po
sdk := mgsdk.NewSDK(config)
idp := uuid.New()

svc := bootstrap.New(authClient, policyClient, repoConfig, sdk, []byte(cfg.EncKey), idp)
policyService := mgpolicy.NewService(policyClient)

svc := bootstrap.New(authClient, policyService, repoConfig, sdk, []byte(cfg.EncKey), idp)

publisher, err := store.NewPublisher(ctx, cfg.ESURL, streamID)
if err != nil {
Expand Down
Loading

0 comments on commit 29d0bf4

Please sign in to comment.