-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c9a5c8
commit 050599d
Showing
10 changed files
with
632 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
services/auth/tests/internal/usecases/acl/get_all_permission_by_role_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
services/auth/tests/internal/usecases/acl/get_all_permission_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
services/auth/tests/internal/usecases/acl/get_all_role_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
65
services/auth/tests/internal/usecases/acl/get_all_user_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
Oops, something went wrong.