Skip to content

Commit

Permalink
add an ACL test tool
Browse files Browse the repository at this point in the history
  • Loading branch information
febrihidayan committed Jan 20, 2024
1 parent 6c9a5c8 commit 050599d
Show file tree
Hide file tree
Showing 10 changed files with 632 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.10.2
golang.org/x/crypto v0.17.0
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848
google.golang.org/api v0.155.0
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.31.0
Expand Down Expand Up @@ -75,7 +76,6 @@ require (
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package acl

import (
"context"
"errors"

"github.com/febrihidayan/go-architecture-monorepo/pkg/exceptions"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/domain/entities"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/mock"
)

func (x *AclUsecaseSuite) TestGetAllPermissionByRole() {
var (
permissionRoles []*entities.PermissionRole
permissions []*entities.Permission
)

permissionRoles = append(permissionRoles, x.permissionRole)
permissions = append(permissions, x.permission)

args := []struct {
name string
args Any
tests func(arg Any)
}{
{
name: "Success Positive Case",
tests: func(arg Any) {
x.permissionRoleRepo.Mock.On("AllByRoleId", x.id.String()).Return(permissionRoles, nil)

x.permissionRepo.Mock.On("Find", x.permissionRole.PermissionId).Return(x.permission, nil)

result, err := x.aclUsecase.GetAllPermissionByRole(context.Background(), x.id.String())
x.Nil(err)
x.Equal(result, permissions)
},
},
{
name: "Failed Negatif Case",
tests: func(arg Any) {
x.permissionRoleRepo.Mock.On("AllByRoleId", x.id.String()).Return(nil, errors.New(mock.Anything))

_, err := x.aclUsecase.GetAllPermissionByRole(context.Background(), x.id.String())

e := &exceptions.CustomError{
Status: exceptions.ERRREPOSITORY,
Errors: multierror.Append(errors.New(mock.Anything)),
}

x.Equal(err, e)
},
},
}

for _, arg := range args {
x.Run(arg.name, func() {
x.SetupTest()
arg.tests(arg.args)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package acl

import (
"context"
"errors"

"github.com/febrihidayan/go-architecture-monorepo/pkg/exceptions"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/domain/entities"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/mock"
)

func (x *AclUsecaseSuite) TestGetAllPermission() {
var (
permissions []*entities.Permission
)

permissions = append(permissions, x.permission)

args := []struct {
name string
args Any
tests func(arg Any)
}{
{
name: "Success Positive Case",
tests: func(arg Any) {
x.permissionRepo.Mock.On("All").Return(permissions, nil)

result, err := x.aclUsecase.GetAllPermission(context.Background())
x.Nil(err)
x.Equal(result, permissions)
},
},
{
name: "Failed Negatif Case",
tests: func(arg Any) {
x.permissionRepo.Mock.On("All").Return(nil, errors.New(mock.Anything))

_, err := x.aclUsecase.GetAllPermission(context.Background())

e := &exceptions.CustomError{
Status: exceptions.ERRREPOSITORY,
Errors: multierror.Append(errors.New(mock.Anything)),
}

x.Equal(err, e)
},
},
}

for _, arg := range args {
x.Run(arg.name, func() {
x.SetupTest()
arg.tests(arg.args)
})
}
}
58 changes: 58 additions & 0 deletions services/auth/tests/internal/usecases/acl/get_all_role_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package acl

import (
"context"
"errors"

"github.com/febrihidayan/go-architecture-monorepo/pkg/exceptions"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/domain/entities"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/mock"
)

func (x *AclUsecaseSuite) TestGetAllRole() {
var (
roles []*entities.Role
)

roles = append(roles, x.role)

args := []struct {
name string
args Any
tests func(arg Any)
}{
{
name: "Success Positive Case",
tests: func(arg Any) {
x.roleRepo.Mock.On("All").Return(roles, nil)

result, err := x.aclUsecase.GetAllRole(context.Background())
x.Nil(err)
x.Equal(result, roles)
},
},
{
name: "Failed Negatif Case",
tests: func(arg Any) {
x.roleRepo.Mock.On("All").Return(nil, errors.New(mock.Anything))

_, err := x.aclUsecase.GetAllRole(context.Background())

e := &exceptions.CustomError{
Status: exceptions.ERRREPOSITORY,
Errors: multierror.Append(errors.New(mock.Anything)),
}

x.Equal(err, e)
},
},
}

for _, arg := range args {
x.Run(arg.name, func() {
x.SetupTest()
arg.tests(arg.args)
})
}
}
65 changes: 65 additions & 0 deletions services/auth/tests/internal/usecases/acl/get_all_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package acl

import (
"context"
"errors"

"github.com/febrihidayan/go-architecture-monorepo/pkg/exceptions"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/domain/entities"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/mock"
)

func (x *AclUsecaseSuite) TestGetAllUser() {
var (
roles []*entities.Role
permissions []*entities.Permission
)

roles = append(roles, x.role)
permissions = append(permissions, x.permission)

args := []struct {
name string
args Any
tests func(arg Any)
}{
{
name: "Success Positive Case",
tests: func(arg Any) {
x.authRepo.Mock.On("FindByUserId", x.id.String()).Return(x.auth, nil)
x.roleRepo.Mock.On("AllByUserId", x.id.String()).Return(roles, nil)
x.permissionRepo.Mock.On("AllByUserId", x.id.String()).Return(permissions, nil)

result, err := x.aclUsecase.GetAllUser(context.Background(), x.id.String())
x.Nil(err)
x.Equal(result, &entities.AclMeta{
Permissions: permissions,
Roles: roles,
})
},
},
{
name: "Failed Negatif Case",
tests: func(arg Any) {
x.authRepo.Mock.On("FindByUserId", x.id.String()).Return(nil, errors.New(mock.Anything))

_, err := x.aclUsecase.GetAllUser(context.Background(), x.id.String())

e := &exceptions.CustomError{
Status: exceptions.ERRREPOSITORY,
Errors: multierror.Append(errors.New(mock.Anything)),
}

x.Equal(err, e)
},
},
}

for _, arg := range args {
x.Run(arg.name, func() {
x.SetupTest()
arg.tests(arg.args)
})
}
}
121 changes: 121 additions & 0 deletions services/auth/tests/internal/usecases/acl/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package acl

import (
"testing"
"time"

"bou.ke/monkey"
"github.com/febrihidayan/go-architecture-monorepo/pkg/common"
"github.com/febrihidayan/go-architecture-monorepo/pkg/middleware"
"github.com/febrihidayan/go-architecture-monorepo/pkg/utils"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/domain/entities"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/domain/usecases"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/internal/config"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/internal/repositories/factories"
"github.com/febrihidayan/go-architecture-monorepo/services/auth/internal/usecases/acl"
mongo_repositories "github.com/febrihidayan/go-architecture-monorepo/services/auth/tests/mocks/repositories/mongo"
"github.com/google/uuid"
"github.com/stretchr/testify/suite"
)

type AclUsecaseSuite struct {
suite.Suite
cfg *config.AuthConfig
mongoFactory *factories.MongoFactory
authRepo *mongo_repositories.AuthRepositoryMock
permissionRepo *mongo_repositories.PermissionRepositoryMock
permissionRoleRepo *mongo_repositories.PermissionRoleRepositoryMock
permissionUserRepo *mongo_repositories.PermissionUserRepositoryMock
roleRepo *mongo_repositories.RoleRepositoryMock
roleUserRepo *mongo_repositories.RoleUserRepositoryMock
aclUsecase usecases.AclUsecase

role *entities.Role
roleUser *entities.RoleUser
permissionRole *entities.PermissionRole
permissionUser *entities.PermissionUser
permission *entities.Permission
auth *entities.Auth
id uuid.UUID
}

func (x *AclUsecaseSuite) SetupTest() {
x.cfg = &config.AuthConfig{}

x.authRepo = new(mongo_repositories.AuthRepositoryMock)
x.permissionRepo = new(mongo_repositories.PermissionRepositoryMock)
x.permissionRoleRepo = new(mongo_repositories.PermissionRoleRepositoryMock)
x.permissionUserRepo = new(mongo_repositories.PermissionUserRepositoryMock)
x.roleUserRepo = new(mongo_repositories.RoleUserRepositoryMock)
x.roleRepo = new(mongo_repositories.RoleRepositoryMock)

x.mongoFactory = &factories.MongoFactory{
AuthRepo: x.authRepo,
PermissionRepo: x.permissionRepo,
PermissionRoleRepo: x.permissionRoleRepo,
PermissionUserRepo: x.permissionUserRepo,
RoleUserRepo: x.roleUserRepo,
RoleRepo: x.roleRepo,
}

x.aclUsecase = acl.NewAclInteractor(x.cfg, x.mongoFactory)

// fake time now for testing
monkey.Patch(time.Now, func() time.Time {
return time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC)
})

// storage data
x.id, _ = common.StringToID("83f8619c-608c-4e5d-8941-3c394551085d")

x.role = &entities.Role{
ID: x.id,
Name: middleware.ROLE_SUPERADMINISTRATOR,
DisplayName: middleware.ROLE_SUPERADMINISTRATOR,
Description: middleware.ROLE_SUPERADMINISTRATOR,
CreatedAt: utils.TimeUTC(),
UpdatedAt: utils.TimeUTC(),
}

x.roleUser = &entities.RoleUser{
ID: x.id,
RoleId: x.id.String(),
UserId: x.id.String(),
}

x.permission = &entities.Permission{
ID: x.id,
Name: "users_create",
DisplayName: "users create",
Description: "users create",
CreatedAt: utils.TimeUTC(),
UpdatedAt: utils.TimeUTC(),
}

x.permissionRole = &entities.PermissionRole{
ID: x.id,
RoleId: x.id.String(),
PermissionId: x.id.String(),
}

x.permissionUser = &entities.PermissionUser{
ID: x.id,
UserId: x.id.String(),
PermissionId: x.id.String(),
}

x.auth = &entities.Auth{
ID: x.id,
UserId: x.id.String(),
}
}

func TestAclUsecase(t *testing.T) {
suite.Run(t, new(AclUsecaseSuite))
}

type Any []interface{}

func (a Any) Get(i int) interface{} {
return a[i]
}
Loading

0 comments on commit 050599d

Please sign in to comment.