From a0208cd21d6f669e3daf3eaa39e29f896685a014 Mon Sep 17 00:00:00 2001 From: Anuj Khandelwal Date: Tue, 27 Aug 2024 15:00:44 +0530 Subject: [PATCH 1/5] Add mocks for org service --- .mockery.yaml | 4 + core/organization/mocks/authn_service.go | 109 ++++ core/organization/mocks/policy_service.go | 258 +++++++++ .../organization/mocks/preferences_service.go | 94 +++ core/organization/mocks/relation_service.go | 201 +++++++ core/organization/mocks/repository.go | 535 ++++++++++++++++++ core/organization/mocks/user_service.go | 95 ++++ core/organization/service_test.go | 1 + 8 files changed, 1297 insertions(+) create mode 100644 core/organization/mocks/authn_service.go create mode 100644 core/organization/mocks/policy_service.go create mode 100644 core/organization/mocks/preferences_service.go create mode 100644 core/organization/mocks/relation_service.go create mode 100644 core/organization/mocks/repository.go create mode 100644 core/organization/mocks/user_service.go create mode 100644 core/organization/service_test.go diff --git a/.mockery.yaml b/.mockery.yaml index 93454d8a9..b7116ee85 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -42,4 +42,8 @@ packages: github.com/raystack/frontier/core/project: config: dir: "core/project/mocks" + all: true + github.com/raystack/frontier/core/organization: + config: + dir: "core/organization/mocks" all: true \ No newline at end of file diff --git a/core/organization/mocks/authn_service.go b/core/organization/mocks/authn_service.go new file mode 100644 index 000000000..6d30464a6 --- /dev/null +++ b/core/organization/mocks/authn_service.go @@ -0,0 +1,109 @@ +// Code generated by mockery v2.40.2. DO NOT EDIT. + +package mocks + +import ( + context "context" + + authenticate "github.com/raystack/frontier/core/authenticate" + + mock "github.com/stretchr/testify/mock" +) + +// AuthnService is an autogenerated mock type for the AuthnService type +type AuthnService struct { + mock.Mock +} + +type AuthnService_Expecter struct { + mock *mock.Mock +} + +func (_m *AuthnService) EXPECT() *AuthnService_Expecter { + return &AuthnService_Expecter{mock: &_m.Mock} +} + +// GetPrincipal provides a mock function with given fields: ctx, via +func (_m *AuthnService) GetPrincipal(ctx context.Context, via ...authenticate.ClientAssertion) (authenticate.Principal, error) { + _va := make([]interface{}, len(via)) + for _i := range via { + _va[_i] = via[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + panic("no return value specified for GetPrincipal") + } + + var r0 authenticate.Principal + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, ...authenticate.ClientAssertion) (authenticate.Principal, error)); ok { + return rf(ctx, via...) + } + if rf, ok := ret.Get(0).(func(context.Context, ...authenticate.ClientAssertion) authenticate.Principal); ok { + r0 = rf(ctx, via...) + } else { + r0 = ret.Get(0).(authenticate.Principal) + } + + if rf, ok := ret.Get(1).(func(context.Context, ...authenticate.ClientAssertion) error); ok { + r1 = rf(ctx, via...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// AuthnService_GetPrincipal_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetPrincipal' +type AuthnService_GetPrincipal_Call struct { + *mock.Call +} + +// GetPrincipal is a helper method to define mock.On call +// - ctx context.Context +// - via ...authenticate.ClientAssertion +func (_e *AuthnService_Expecter) GetPrincipal(ctx interface{}, via ...interface{}) *AuthnService_GetPrincipal_Call { + return &AuthnService_GetPrincipal_Call{Call: _e.mock.On("GetPrincipal", + append([]interface{}{ctx}, via...)...)} +} + +func (_c *AuthnService_GetPrincipal_Call) Run(run func(ctx context.Context, via ...authenticate.ClientAssertion)) *AuthnService_GetPrincipal_Call { + _c.Call.Run(func(args mock.Arguments) { + variadicArgs := make([]authenticate.ClientAssertion, len(args)-1) + for i, a := range args[1:] { + if a != nil { + variadicArgs[i] = a.(authenticate.ClientAssertion) + } + } + run(args[0].(context.Context), variadicArgs...) + }) + return _c +} + +func (_c *AuthnService_GetPrincipal_Call) Return(_a0 authenticate.Principal, _a1 error) *AuthnService_GetPrincipal_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *AuthnService_GetPrincipal_Call) RunAndReturn(run func(context.Context, ...authenticate.ClientAssertion) (authenticate.Principal, error)) *AuthnService_GetPrincipal_Call { + _c.Call.Return(run) + return _c +} + +// NewAuthnService creates a new instance of AuthnService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewAuthnService(t interface { + mock.TestingT + Cleanup(func()) +}) *AuthnService { + mock := &AuthnService{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/core/organization/mocks/policy_service.go b/core/organization/mocks/policy_service.go new file mode 100644 index 000000000..d3ab038b0 --- /dev/null +++ b/core/organization/mocks/policy_service.go @@ -0,0 +1,258 @@ +// Code generated by mockery v2.40.2. DO NOT EDIT. + +package mocks + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" + + policy "github.com/raystack/frontier/core/policy" +) + +// PolicyService is an autogenerated mock type for the PolicyService type +type PolicyService struct { + mock.Mock +} + +type PolicyService_Expecter struct { + mock *mock.Mock +} + +func (_m *PolicyService) EXPECT() *PolicyService_Expecter { + return &PolicyService_Expecter{mock: &_m.Mock} +} + +// Create provides a mock function with given fields: ctx, _a1 +func (_m *PolicyService) Create(ctx context.Context, _a1 policy.Policy) (policy.Policy, error) { + ret := _m.Called(ctx, _a1) + + if len(ret) == 0 { + panic("no return value specified for Create") + } + + var r0 policy.Policy + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, policy.Policy) (policy.Policy, error)); ok { + return rf(ctx, _a1) + } + if rf, ok := ret.Get(0).(func(context.Context, policy.Policy) policy.Policy); ok { + r0 = rf(ctx, _a1) + } else { + r0 = ret.Get(0).(policy.Policy) + } + + if rf, ok := ret.Get(1).(func(context.Context, policy.Policy) error); ok { + r1 = rf(ctx, _a1) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PolicyService_Create_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Create' +type PolicyService_Create_Call struct { + *mock.Call +} + +// Create is a helper method to define mock.On call +// - ctx context.Context +// - _a1 policy.Policy +func (_e *PolicyService_Expecter) Create(ctx interface{}, _a1 interface{}) *PolicyService_Create_Call { + return &PolicyService_Create_Call{Call: _e.mock.On("Create", ctx, _a1)} +} + +func (_c *PolicyService_Create_Call) Run(run func(ctx context.Context, _a1 policy.Policy)) *PolicyService_Create_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(policy.Policy)) + }) + return _c +} + +func (_c *PolicyService_Create_Call) Return(_a0 policy.Policy, _a1 error) *PolicyService_Create_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *PolicyService_Create_Call) RunAndReturn(run func(context.Context, policy.Policy) (policy.Policy, error)) *PolicyService_Create_Call { + _c.Call.Return(run) + return _c +} + +// Delete provides a mock function with given fields: ctx, id +func (_m *PolicyService) Delete(ctx context.Context, id string) error { + ret := _m.Called(ctx, id) + + if len(ret) == 0 { + panic("no return value specified for Delete") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string) error); ok { + r0 = rf(ctx, id) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// PolicyService_Delete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Delete' +type PolicyService_Delete_Call struct { + *mock.Call +} + +// Delete is a helper method to define mock.On call +// - ctx context.Context +// - id string +func (_e *PolicyService_Expecter) Delete(ctx interface{}, id interface{}) *PolicyService_Delete_Call { + return &PolicyService_Delete_Call{Call: _e.mock.On("Delete", ctx, id)} +} + +func (_c *PolicyService_Delete_Call) Run(run func(ctx context.Context, id string)) *PolicyService_Delete_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *PolicyService_Delete_Call) Return(_a0 error) *PolicyService_Delete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *PolicyService_Delete_Call) RunAndReturn(run func(context.Context, string) error) *PolicyService_Delete_Call { + _c.Call.Return(run) + return _c +} + +// List provides a mock function with given fields: ctx, flt +func (_m *PolicyService) List(ctx context.Context, flt policy.Filter) ([]policy.Policy, error) { + ret := _m.Called(ctx, flt) + + if len(ret) == 0 { + panic("no return value specified for List") + } + + var r0 []policy.Policy + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, policy.Filter) ([]policy.Policy, error)); ok { + return rf(ctx, flt) + } + if rf, ok := ret.Get(0).(func(context.Context, policy.Filter) []policy.Policy); ok { + r0 = rf(ctx, flt) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]policy.Policy) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, policy.Filter) error); ok { + r1 = rf(ctx, flt) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PolicyService_List_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'List' +type PolicyService_List_Call struct { + *mock.Call +} + +// List is a helper method to define mock.On call +// - ctx context.Context +// - flt policy.Filter +func (_e *PolicyService_Expecter) List(ctx interface{}, flt interface{}) *PolicyService_List_Call { + return &PolicyService_List_Call{Call: _e.mock.On("List", ctx, flt)} +} + +func (_c *PolicyService_List_Call) Run(run func(ctx context.Context, flt policy.Filter)) *PolicyService_List_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(policy.Filter)) + }) + return _c +} + +func (_c *PolicyService_List_Call) Return(_a0 []policy.Policy, _a1 error) *PolicyService_List_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *PolicyService_List_Call) RunAndReturn(run func(context.Context, policy.Filter) ([]policy.Policy, error)) *PolicyService_List_Call { + _c.Call.Return(run) + return _c +} + +// OrgMemberCount provides a mock function with given fields: ctx, id +func (_m *PolicyService) OrgMemberCount(ctx context.Context, id string) (policy.MemberCount, error) { + ret := _m.Called(ctx, id) + + if len(ret) == 0 { + panic("no return value specified for OrgMemberCount") + } + + var r0 policy.MemberCount + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string) (policy.MemberCount, error)); ok { + return rf(ctx, id) + } + if rf, ok := ret.Get(0).(func(context.Context, string) policy.MemberCount); ok { + r0 = rf(ctx, id) + } else { + r0 = ret.Get(0).(policy.MemberCount) + } + + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PolicyService_OrgMemberCount_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'OrgMemberCount' +type PolicyService_OrgMemberCount_Call struct { + *mock.Call +} + +// OrgMemberCount is a helper method to define mock.On call +// - ctx context.Context +// - id string +func (_e *PolicyService_Expecter) OrgMemberCount(ctx interface{}, id interface{}) *PolicyService_OrgMemberCount_Call { + return &PolicyService_OrgMemberCount_Call{Call: _e.mock.On("OrgMemberCount", ctx, id)} +} + +func (_c *PolicyService_OrgMemberCount_Call) Run(run func(ctx context.Context, id string)) *PolicyService_OrgMemberCount_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *PolicyService_OrgMemberCount_Call) Return(_a0 policy.MemberCount, _a1 error) *PolicyService_OrgMemberCount_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *PolicyService_OrgMemberCount_Call) RunAndReturn(run func(context.Context, string) (policy.MemberCount, error)) *PolicyService_OrgMemberCount_Call { + _c.Call.Return(run) + return _c +} + +// NewPolicyService creates a new instance of PolicyService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewPolicyService(t interface { + mock.TestingT + Cleanup(func()) +}) *PolicyService { + mock := &PolicyService{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/core/organization/mocks/preferences_service.go b/core/organization/mocks/preferences_service.go new file mode 100644 index 000000000..39e6f1b08 --- /dev/null +++ b/core/organization/mocks/preferences_service.go @@ -0,0 +1,94 @@ +// Code generated by mockery v2.40.2. DO NOT EDIT. + +package mocks + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" +) + +// PreferencesService is an autogenerated mock type for the PreferencesService type +type PreferencesService struct { + mock.Mock +} + +type PreferencesService_Expecter struct { + mock *mock.Mock +} + +func (_m *PreferencesService) EXPECT() *PreferencesService_Expecter { + return &PreferencesService_Expecter{mock: &_m.Mock} +} + +// LoadPlatformPreferences provides a mock function with given fields: ctx +func (_m *PreferencesService) LoadPlatformPreferences(ctx context.Context) (map[string]string, error) { + ret := _m.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for LoadPlatformPreferences") + } + + var r0 map[string]string + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) (map[string]string, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) map[string]string); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(map[string]string) + } + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PreferencesService_LoadPlatformPreferences_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LoadPlatformPreferences' +type PreferencesService_LoadPlatformPreferences_Call struct { + *mock.Call +} + +// LoadPlatformPreferences is a helper method to define mock.On call +// - ctx context.Context +func (_e *PreferencesService_Expecter) LoadPlatformPreferences(ctx interface{}) *PreferencesService_LoadPlatformPreferences_Call { + return &PreferencesService_LoadPlatformPreferences_Call{Call: _e.mock.On("LoadPlatformPreferences", ctx)} +} + +func (_c *PreferencesService_LoadPlatformPreferences_Call) Run(run func(ctx context.Context)) *PreferencesService_LoadPlatformPreferences_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context)) + }) + return _c +} + +func (_c *PreferencesService_LoadPlatformPreferences_Call) Return(_a0 map[string]string, _a1 error) *PreferencesService_LoadPlatformPreferences_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *PreferencesService_LoadPlatformPreferences_Call) RunAndReturn(run func(context.Context) (map[string]string, error)) *PreferencesService_LoadPlatformPreferences_Call { + _c.Call.Return(run) + return _c +} + +// NewPreferencesService creates a new instance of PreferencesService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewPreferencesService(t interface { + mock.TestingT + Cleanup(func()) +}) *PreferencesService { + mock := &PreferencesService{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/core/organization/mocks/relation_service.go b/core/organization/mocks/relation_service.go new file mode 100644 index 000000000..1b87a340d --- /dev/null +++ b/core/organization/mocks/relation_service.go @@ -0,0 +1,201 @@ +// Code generated by mockery v2.40.2. DO NOT EDIT. + +package mocks + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" + + relation "github.com/raystack/frontier/core/relation" +) + +// RelationService is an autogenerated mock type for the RelationService type +type RelationService struct { + mock.Mock +} + +type RelationService_Expecter struct { + mock *mock.Mock +} + +func (_m *RelationService) EXPECT() *RelationService_Expecter { + return &RelationService_Expecter{mock: &_m.Mock} +} + +// Create provides a mock function with given fields: ctx, rel +func (_m *RelationService) Create(ctx context.Context, rel relation.Relation) (relation.Relation, error) { + ret := _m.Called(ctx, rel) + + if len(ret) == 0 { + panic("no return value specified for Create") + } + + var r0 relation.Relation + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, relation.Relation) (relation.Relation, error)); ok { + return rf(ctx, rel) + } + if rf, ok := ret.Get(0).(func(context.Context, relation.Relation) relation.Relation); ok { + r0 = rf(ctx, rel) + } else { + r0 = ret.Get(0).(relation.Relation) + } + + if rf, ok := ret.Get(1).(func(context.Context, relation.Relation) error); ok { + r1 = rf(ctx, rel) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// RelationService_Create_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Create' +type RelationService_Create_Call struct { + *mock.Call +} + +// Create is a helper method to define mock.On call +// - ctx context.Context +// - rel relation.Relation +func (_e *RelationService_Expecter) Create(ctx interface{}, rel interface{}) *RelationService_Create_Call { + return &RelationService_Create_Call{Call: _e.mock.On("Create", ctx, rel)} +} + +func (_c *RelationService_Create_Call) Run(run func(ctx context.Context, rel relation.Relation)) *RelationService_Create_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(relation.Relation)) + }) + return _c +} + +func (_c *RelationService_Create_Call) Return(_a0 relation.Relation, _a1 error) *RelationService_Create_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *RelationService_Create_Call) RunAndReturn(run func(context.Context, relation.Relation) (relation.Relation, error)) *RelationService_Create_Call { + _c.Call.Return(run) + return _c +} + +// Delete provides a mock function with given fields: ctx, rel +func (_m *RelationService) Delete(ctx context.Context, rel relation.Relation) error { + ret := _m.Called(ctx, rel) + + if len(ret) == 0 { + panic("no return value specified for Delete") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, relation.Relation) error); ok { + r0 = rf(ctx, rel) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// RelationService_Delete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Delete' +type RelationService_Delete_Call struct { + *mock.Call +} + +// Delete is a helper method to define mock.On call +// - ctx context.Context +// - rel relation.Relation +func (_e *RelationService_Expecter) Delete(ctx interface{}, rel interface{}) *RelationService_Delete_Call { + return &RelationService_Delete_Call{Call: _e.mock.On("Delete", ctx, rel)} +} + +func (_c *RelationService_Delete_Call) Run(run func(ctx context.Context, rel relation.Relation)) *RelationService_Delete_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(relation.Relation)) + }) + return _c +} + +func (_c *RelationService_Delete_Call) Return(_a0 error) *RelationService_Delete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *RelationService_Delete_Call) RunAndReturn(run func(context.Context, relation.Relation) error) *RelationService_Delete_Call { + _c.Call.Return(run) + return _c +} + +// LookupResources provides a mock function with given fields: ctx, rel +func (_m *RelationService) LookupResources(ctx context.Context, rel relation.Relation) ([]string, error) { + ret := _m.Called(ctx, rel) + + if len(ret) == 0 { + panic("no return value specified for LookupResources") + } + + var r0 []string + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, relation.Relation) ([]string, error)); ok { + return rf(ctx, rel) + } + if rf, ok := ret.Get(0).(func(context.Context, relation.Relation) []string); ok { + r0 = rf(ctx, rel) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, relation.Relation) error); ok { + r1 = rf(ctx, rel) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// RelationService_LookupResources_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LookupResources' +type RelationService_LookupResources_Call struct { + *mock.Call +} + +// LookupResources is a helper method to define mock.On call +// - ctx context.Context +// - rel relation.Relation +func (_e *RelationService_Expecter) LookupResources(ctx interface{}, rel interface{}) *RelationService_LookupResources_Call { + return &RelationService_LookupResources_Call{Call: _e.mock.On("LookupResources", ctx, rel)} +} + +func (_c *RelationService_LookupResources_Call) Run(run func(ctx context.Context, rel relation.Relation)) *RelationService_LookupResources_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(relation.Relation)) + }) + return _c +} + +func (_c *RelationService_LookupResources_Call) Return(_a0 []string, _a1 error) *RelationService_LookupResources_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *RelationService_LookupResources_Call) RunAndReturn(run func(context.Context, relation.Relation) ([]string, error)) *RelationService_LookupResources_Call { + _c.Call.Return(run) + return _c +} + +// NewRelationService creates a new instance of RelationService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewRelationService(t interface { + mock.TestingT + Cleanup(func()) +}) *RelationService { + mock := &RelationService{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/core/organization/mocks/repository.go b/core/organization/mocks/repository.go new file mode 100644 index 000000000..a727bf9da --- /dev/null +++ b/core/organization/mocks/repository.go @@ -0,0 +1,535 @@ +// Code generated by mockery v2.40.2. DO NOT EDIT. + +package mocks + +import ( + context "context" + + organization "github.com/raystack/frontier/core/organization" + mock "github.com/stretchr/testify/mock" +) + +// Repository is an autogenerated mock type for the Repository type +type Repository struct { + mock.Mock +} + +type Repository_Expecter struct { + mock *mock.Mock +} + +func (_m *Repository) EXPECT() *Repository_Expecter { + return &Repository_Expecter{mock: &_m.Mock} +} + +// Create provides a mock function with given fields: ctx, org +func (_m *Repository) Create(ctx context.Context, org organization.Organization) (organization.Organization, error) { + ret := _m.Called(ctx, org) + + if len(ret) == 0 { + panic("no return value specified for Create") + } + + var r0 organization.Organization + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, organization.Organization) (organization.Organization, error)); ok { + return rf(ctx, org) + } + if rf, ok := ret.Get(0).(func(context.Context, organization.Organization) organization.Organization); ok { + r0 = rf(ctx, org) + } else { + r0 = ret.Get(0).(organization.Organization) + } + + if rf, ok := ret.Get(1).(func(context.Context, organization.Organization) error); ok { + r1 = rf(ctx, org) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Repository_Create_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Create' +type Repository_Create_Call struct { + *mock.Call +} + +// Create is a helper method to define mock.On call +// - ctx context.Context +// - org organization.Organization +func (_e *Repository_Expecter) Create(ctx interface{}, org interface{}) *Repository_Create_Call { + return &Repository_Create_Call{Call: _e.mock.On("Create", ctx, org)} +} + +func (_c *Repository_Create_Call) Run(run func(ctx context.Context, org organization.Organization)) *Repository_Create_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(organization.Organization)) + }) + return _c +} + +func (_c *Repository_Create_Call) Return(_a0 organization.Organization, _a1 error) *Repository_Create_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Repository_Create_Call) RunAndReturn(run func(context.Context, organization.Organization) (organization.Organization, error)) *Repository_Create_Call { + _c.Call.Return(run) + return _c +} + +// Delete provides a mock function with given fields: ctx, id +func (_m *Repository) Delete(ctx context.Context, id string) error { + ret := _m.Called(ctx, id) + + if len(ret) == 0 { + panic("no return value specified for Delete") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string) error); ok { + r0 = rf(ctx, id) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Repository_Delete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Delete' +type Repository_Delete_Call struct { + *mock.Call +} + +// Delete is a helper method to define mock.On call +// - ctx context.Context +// - id string +func (_e *Repository_Expecter) Delete(ctx interface{}, id interface{}) *Repository_Delete_Call { + return &Repository_Delete_Call{Call: _e.mock.On("Delete", ctx, id)} +} + +func (_c *Repository_Delete_Call) Run(run func(ctx context.Context, id string)) *Repository_Delete_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *Repository_Delete_Call) Return(_a0 error) *Repository_Delete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Repository_Delete_Call) RunAndReturn(run func(context.Context, string) error) *Repository_Delete_Call { + _c.Call.Return(run) + return _c +} + +// GetByID provides a mock function with given fields: ctx, id +func (_m *Repository) GetByID(ctx context.Context, id string) (organization.Organization, error) { + ret := _m.Called(ctx, id) + + if len(ret) == 0 { + panic("no return value specified for GetByID") + } + + var r0 organization.Organization + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string) (organization.Organization, error)); ok { + return rf(ctx, id) + } + if rf, ok := ret.Get(0).(func(context.Context, string) organization.Organization); ok { + r0 = rf(ctx, id) + } else { + r0 = ret.Get(0).(organization.Organization) + } + + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Repository_GetByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetByID' +type Repository_GetByID_Call struct { + *mock.Call +} + +// GetByID is a helper method to define mock.On call +// - ctx context.Context +// - id string +func (_e *Repository_Expecter) GetByID(ctx interface{}, id interface{}) *Repository_GetByID_Call { + return &Repository_GetByID_Call{Call: _e.mock.On("GetByID", ctx, id)} +} + +func (_c *Repository_GetByID_Call) Run(run func(ctx context.Context, id string)) *Repository_GetByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *Repository_GetByID_Call) Return(_a0 organization.Organization, _a1 error) *Repository_GetByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Repository_GetByID_Call) RunAndReturn(run func(context.Context, string) (organization.Organization, error)) *Repository_GetByID_Call { + _c.Call.Return(run) + return _c +} + +// GetByIDs provides a mock function with given fields: ctx, ids +func (_m *Repository) GetByIDs(ctx context.Context, ids []string) ([]organization.Organization, error) { + ret := _m.Called(ctx, ids) + + if len(ret) == 0 { + panic("no return value specified for GetByIDs") + } + + var r0 []organization.Organization + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, []string) ([]organization.Organization, error)); ok { + return rf(ctx, ids) + } + if rf, ok := ret.Get(0).(func(context.Context, []string) []organization.Organization); ok { + r0 = rf(ctx, ids) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]organization.Organization) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, []string) error); ok { + r1 = rf(ctx, ids) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Repository_GetByIDs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetByIDs' +type Repository_GetByIDs_Call struct { + *mock.Call +} + +// GetByIDs is a helper method to define mock.On call +// - ctx context.Context +// - ids []string +func (_e *Repository_Expecter) GetByIDs(ctx interface{}, ids interface{}) *Repository_GetByIDs_Call { + return &Repository_GetByIDs_Call{Call: _e.mock.On("GetByIDs", ctx, ids)} +} + +func (_c *Repository_GetByIDs_Call) Run(run func(ctx context.Context, ids []string)) *Repository_GetByIDs_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].([]string)) + }) + return _c +} + +func (_c *Repository_GetByIDs_Call) Return(_a0 []organization.Organization, _a1 error) *Repository_GetByIDs_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Repository_GetByIDs_Call) RunAndReturn(run func(context.Context, []string) ([]organization.Organization, error)) *Repository_GetByIDs_Call { + _c.Call.Return(run) + return _c +} + +// GetByName provides a mock function with given fields: ctx, name +func (_m *Repository) GetByName(ctx context.Context, name string) (organization.Organization, error) { + ret := _m.Called(ctx, name) + + if len(ret) == 0 { + panic("no return value specified for GetByName") + } + + var r0 organization.Organization + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string) (organization.Organization, error)); ok { + return rf(ctx, name) + } + if rf, ok := ret.Get(0).(func(context.Context, string) organization.Organization); ok { + r0 = rf(ctx, name) + } else { + r0 = ret.Get(0).(organization.Organization) + } + + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, name) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Repository_GetByName_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetByName' +type Repository_GetByName_Call struct { + *mock.Call +} + +// GetByName is a helper method to define mock.On call +// - ctx context.Context +// - name string +func (_e *Repository_Expecter) GetByName(ctx interface{}, name interface{}) *Repository_GetByName_Call { + return &Repository_GetByName_Call{Call: _e.mock.On("GetByName", ctx, name)} +} + +func (_c *Repository_GetByName_Call) Run(run func(ctx context.Context, name string)) *Repository_GetByName_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *Repository_GetByName_Call) Return(_a0 organization.Organization, _a1 error) *Repository_GetByName_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Repository_GetByName_Call) RunAndReturn(run func(context.Context, string) (organization.Organization, error)) *Repository_GetByName_Call { + _c.Call.Return(run) + return _c +} + +// List provides a mock function with given fields: ctx, flt +func (_m *Repository) List(ctx context.Context, flt organization.Filter) ([]organization.Organization, error) { + ret := _m.Called(ctx, flt) + + if len(ret) == 0 { + panic("no return value specified for List") + } + + var r0 []organization.Organization + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, organization.Filter) ([]organization.Organization, error)); ok { + return rf(ctx, flt) + } + if rf, ok := ret.Get(0).(func(context.Context, organization.Filter) []organization.Organization); ok { + r0 = rf(ctx, flt) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]organization.Organization) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, organization.Filter) error); ok { + r1 = rf(ctx, flt) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Repository_List_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'List' +type Repository_List_Call struct { + *mock.Call +} + +// List is a helper method to define mock.On call +// - ctx context.Context +// - flt organization.Filter +func (_e *Repository_Expecter) List(ctx interface{}, flt interface{}) *Repository_List_Call { + return &Repository_List_Call{Call: _e.mock.On("List", ctx, flt)} +} + +func (_c *Repository_List_Call) Run(run func(ctx context.Context, flt organization.Filter)) *Repository_List_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(organization.Filter)) + }) + return _c +} + +func (_c *Repository_List_Call) Return(_a0 []organization.Organization, _a1 error) *Repository_List_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Repository_List_Call) RunAndReturn(run func(context.Context, organization.Filter) ([]organization.Organization, error)) *Repository_List_Call { + _c.Call.Return(run) + return _c +} + +// SetState provides a mock function with given fields: ctx, id, state +func (_m *Repository) SetState(ctx context.Context, id string, state organization.State) error { + ret := _m.Called(ctx, id, state) + + if len(ret) == 0 { + panic("no return value specified for SetState") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, organization.State) error); ok { + r0 = rf(ctx, id, state) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Repository_SetState_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SetState' +type Repository_SetState_Call struct { + *mock.Call +} + +// SetState is a helper method to define mock.On call +// - ctx context.Context +// - id string +// - state organization.State +func (_e *Repository_Expecter) SetState(ctx interface{}, id interface{}, state interface{}) *Repository_SetState_Call { + return &Repository_SetState_Call{Call: _e.mock.On("SetState", ctx, id, state)} +} + +func (_c *Repository_SetState_Call) Run(run func(ctx context.Context, id string, state organization.State)) *Repository_SetState_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(organization.State)) + }) + return _c +} + +func (_c *Repository_SetState_Call) Return(_a0 error) *Repository_SetState_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Repository_SetState_Call) RunAndReturn(run func(context.Context, string, organization.State) error) *Repository_SetState_Call { + _c.Call.Return(run) + return _c +} + +// UpdateByID provides a mock function with given fields: ctx, org +func (_m *Repository) UpdateByID(ctx context.Context, org organization.Organization) (organization.Organization, error) { + ret := _m.Called(ctx, org) + + if len(ret) == 0 { + panic("no return value specified for UpdateByID") + } + + var r0 organization.Organization + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, organization.Organization) (organization.Organization, error)); ok { + return rf(ctx, org) + } + if rf, ok := ret.Get(0).(func(context.Context, organization.Organization) organization.Organization); ok { + r0 = rf(ctx, org) + } else { + r0 = ret.Get(0).(organization.Organization) + } + + if rf, ok := ret.Get(1).(func(context.Context, organization.Organization) error); ok { + r1 = rf(ctx, org) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Repository_UpdateByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdateByID' +type Repository_UpdateByID_Call struct { + *mock.Call +} + +// UpdateByID is a helper method to define mock.On call +// - ctx context.Context +// - org organization.Organization +func (_e *Repository_Expecter) UpdateByID(ctx interface{}, org interface{}) *Repository_UpdateByID_Call { + return &Repository_UpdateByID_Call{Call: _e.mock.On("UpdateByID", ctx, org)} +} + +func (_c *Repository_UpdateByID_Call) Run(run func(ctx context.Context, org organization.Organization)) *Repository_UpdateByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(organization.Organization)) + }) + return _c +} + +func (_c *Repository_UpdateByID_Call) Return(_a0 organization.Organization, _a1 error) *Repository_UpdateByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Repository_UpdateByID_Call) RunAndReturn(run func(context.Context, organization.Organization) (organization.Organization, error)) *Repository_UpdateByID_Call { + _c.Call.Return(run) + return _c +} + +// UpdateByName provides a mock function with given fields: ctx, org +func (_m *Repository) UpdateByName(ctx context.Context, org organization.Organization) (organization.Organization, error) { + ret := _m.Called(ctx, org) + + if len(ret) == 0 { + panic("no return value specified for UpdateByName") + } + + var r0 organization.Organization + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, organization.Organization) (organization.Organization, error)); ok { + return rf(ctx, org) + } + if rf, ok := ret.Get(0).(func(context.Context, organization.Organization) organization.Organization); ok { + r0 = rf(ctx, org) + } else { + r0 = ret.Get(0).(organization.Organization) + } + + if rf, ok := ret.Get(1).(func(context.Context, organization.Organization) error); ok { + r1 = rf(ctx, org) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Repository_UpdateByName_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdateByName' +type Repository_UpdateByName_Call struct { + *mock.Call +} + +// UpdateByName is a helper method to define mock.On call +// - ctx context.Context +// - org organization.Organization +func (_e *Repository_Expecter) UpdateByName(ctx interface{}, org interface{}) *Repository_UpdateByName_Call { + return &Repository_UpdateByName_Call{Call: _e.mock.On("UpdateByName", ctx, org)} +} + +func (_c *Repository_UpdateByName_Call) Run(run func(ctx context.Context, org organization.Organization)) *Repository_UpdateByName_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(organization.Organization)) + }) + return _c +} + +func (_c *Repository_UpdateByName_Call) Return(_a0 organization.Organization, _a1 error) *Repository_UpdateByName_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Repository_UpdateByName_Call) RunAndReturn(run func(context.Context, organization.Organization) (organization.Organization, error)) *Repository_UpdateByName_Call { + _c.Call.Return(run) + return _c +} + +// NewRepository creates a new instance of Repository. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewRepository(t interface { + mock.TestingT + Cleanup(func()) +}) *Repository { + mock := &Repository{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/core/organization/mocks/user_service.go b/core/organization/mocks/user_service.go new file mode 100644 index 000000000..c4dc977fa --- /dev/null +++ b/core/organization/mocks/user_service.go @@ -0,0 +1,95 @@ +// Code generated by mockery v2.40.2. DO NOT EDIT. + +package mocks + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" + + user "github.com/raystack/frontier/core/user" +) + +// UserService is an autogenerated mock type for the UserService type +type UserService struct { + mock.Mock +} + +type UserService_Expecter struct { + mock *mock.Mock +} + +func (_m *UserService) EXPECT() *UserService_Expecter { + return &UserService_Expecter{mock: &_m.Mock} +} + +// GetByID provides a mock function with given fields: ctx, id +func (_m *UserService) GetByID(ctx context.Context, id string) (user.User, error) { + ret := _m.Called(ctx, id) + + if len(ret) == 0 { + panic("no return value specified for GetByID") + } + + var r0 user.User + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string) (user.User, error)); ok { + return rf(ctx, id) + } + if rf, ok := ret.Get(0).(func(context.Context, string) user.User); ok { + r0 = rf(ctx, id) + } else { + r0 = ret.Get(0).(user.User) + } + + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// UserService_GetByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetByID' +type UserService_GetByID_Call struct { + *mock.Call +} + +// GetByID is a helper method to define mock.On call +// - ctx context.Context +// - id string +func (_e *UserService_Expecter) GetByID(ctx interface{}, id interface{}) *UserService_GetByID_Call { + return &UserService_GetByID_Call{Call: _e.mock.On("GetByID", ctx, id)} +} + +func (_c *UserService_GetByID_Call) Run(run func(ctx context.Context, id string)) *UserService_GetByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *UserService_GetByID_Call) Return(_a0 user.User, _a1 error) *UserService_GetByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *UserService_GetByID_Call) RunAndReturn(run func(context.Context, string) (user.User, error)) *UserService_GetByID_Call { + _c.Call.Return(run) + return _c +} + +// NewUserService creates a new instance of UserService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewUserService(t interface { + mock.TestingT + Cleanup(func()) +}) *UserService { + mock := &UserService{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/core/organization/service_test.go b/core/organization/service_test.go new file mode 100644 index 000000000..55707144c --- /dev/null +++ b/core/organization/service_test.go @@ -0,0 +1 @@ +package organization_test \ No newline at end of file From e909716f5194f5a717799f01987830c1c222a92f Mon Sep 17 00:00:00 2001 From: Anuj Khandelwal Date: Wed, 28 Aug 2024 16:35:01 +0530 Subject: [PATCH 2/5] add unit tests for get method in org service --- core/organization/service_test.go | 70 ++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/core/organization/service_test.go b/core/organization/service_test.go index 55707144c..fd1bda6de 100644 --- a/core/organization/service_test.go +++ b/core/organization/service_test.go @@ -1 +1,69 @@ -package organization_test \ No newline at end of file +package organization_test + +import ( + "context" + "testing" + + "github.com/google/uuid" + "github.com/raystack/frontier/core/organization" + "github.com/raystack/frontier/core/organization/mocks" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" +) + +func TestService_Get(t *testing.T) { + mockRepo := mocks.NewRepository(t) + mockRelationSvc := mocks.NewRelationService(t) + mockUserSvc := mocks.NewUserService(t) + mockAuthnSvc := mocks.NewAuthnService(t) + mockPolicySvc := mocks.NewPolicyService(t) + mockPrefSvc := mocks.NewPreferencesService(t) + + svc := organization.NewService(mockRepo, mockRelationSvc, mockUserSvc, mockAuthnSvc, mockPolicySvc, mockPrefSvc) + + t.Run("should return orgs when fetched by id (by calling repo.GetByID)", func(t *testing.T) { + IDParam := uuid.New() + expectedOrg := organization.Organization{ + ID: IDParam.String(), + Name: "test-org", + Title: "test organization", + State: "enabled", + } + + mockRepo.On("GetByID", mock.Anything, IDParam.String()).Return(expectedOrg, nil).Once() + org, err := svc.Get(context.Background(), IDParam.String()) + assert.Nil(t, err) + assert.Equal(t, expectedOrg, org) + }) + + t.Run("should return orgs when fetched by name (by calling repo.GetByName)", func(t *testing.T) { + nameParam := "test-org" + expectedOrg := organization.Organization{ + ID: uuid.New().String(), + Name: nameParam, + Title: "test organization", + State: "enabled", + } + + mockRepo.On("GetByName", mock.Anything, nameParam).Return(expectedOrg, nil).Once() + org, err := svc.Get(context.Background(), nameParam) + assert.Nil(t, err) + assert.Equal(t, expectedOrg, org) + }) + + t.Run("should return an error if org being fetched is disabled", func(t *testing.T) { + nameParam := "test-org" + orgFromRepository := organization.Organization{ + ID: uuid.New().String(), + Name: nameParam, + Title: "test organization", + State: organization.Disabled, + } + + mockRepo.On("GetByName", mock.Anything, nameParam).Return(orgFromRepository, nil).Once() + org, err := svc.Get(context.Background(), nameParam) + assert.NotNil(t, err) + assert.Equal(t, err, organization.ErrDisabled) + assert.Equal(t, organization.Organization{}, org) + }) +} From 03c6d1a995f9b929db91ec105bdc23e683e900f5 Mon Sep 17 00:00:00 2001 From: Anuj Khandelwal Date: Thu, 29 Aug 2024 12:12:25 +0530 Subject: [PATCH 3/5] add tests for GetRaw method --- core/organization/service_test.go | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/core/organization/service_test.go b/core/organization/service_test.go index fd1bda6de..53f73541c 100644 --- a/core/organization/service_test.go +++ b/core/organization/service_test.go @@ -67,3 +67,59 @@ func TestService_Get(t *testing.T) { assert.Equal(t, organization.Organization{}, org) }) } + +func TestService_GetRaw(t *testing.T) { + mockRepo := mocks.NewRepository(t) + mockRelationSvc := mocks.NewRelationService(t) + mockUserSvc := mocks.NewUserService(t) + mockAuthnSvc := mocks.NewAuthnService(t) + mockPolicySvc := mocks.NewPolicyService(t) + mockPrefSvc := mocks.NewPreferencesService(t) + + svc := organization.NewService(mockRepo, mockRelationSvc, mockUserSvc, mockAuthnSvc, mockPolicySvc, mockPrefSvc) + + t.Run("should return an org based on ID passed", func(t *testing.T) { + IDParam := uuid.New() + expectedOrg := organization.Organization{ + ID: IDParam.String(), + Name: "test-org", + Title: "test organization", + State: "enabled", + } + + mockRepo.On("GetByID", mock.Anything, IDParam.String()).Return(expectedOrg, nil).Once() + org, err := svc.GetRaw(context.Background(), IDParam.String()) + assert.Nil(t, err) + assert.Equal(t, expectedOrg, org) + }) + + t.Run("should return an org based on name passed", func(t *testing.T) { + nameParam := "test-org" + expectedOrg := organization.Organization{ + ID: uuid.New().String(), + Name: nameParam, + Title: "test organization", + State: "enabled", + } + + mockRepo.On("GetByName", mock.Anything, nameParam).Return(expectedOrg, nil).Once() + org, err := svc.GetRaw(context.Background(), nameParam) + assert.Nil(t, err) + assert.Equal(t, expectedOrg, org) + }) + + t.Run("should return an org even if it is disabled", func(t *testing.T) { + nameParam := "test-org" + expectedOrg := organization.Organization{ + ID: uuid.New().String(), + Name: nameParam, + Title: "test organization", + State: organization.Disabled, + } + + mockRepo.On("GetByName", mock.Anything, nameParam).Return(expectedOrg, nil).Once() + org, err := svc.GetRaw(context.Background(), nameParam) + assert.Nil(t, err) + assert.Equal(t, expectedOrg, org) + }) +} From 77f49f4965adafad35254110d38bbd23fce45cd1 Mon Sep 17 00:00:00 2001 From: Anuj Khandelwal Date: Thu, 29 Aug 2024 15:16:33 +0530 Subject: [PATCH 4/5] add test for default org state on creation --- core/organization/service_test.go | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/core/organization/service_test.go b/core/organization/service_test.go index 53f73541c..635badfcd 100644 --- a/core/organization/service_test.go +++ b/core/organization/service_test.go @@ -2,11 +2,13 @@ package organization_test import ( "context" + "errors" "testing" "github.com/google/uuid" "github.com/raystack/frontier/core/organization" "github.com/raystack/frontier/core/organization/mocks" + "github.com/raystack/frontier/core/preference" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -123,3 +125,45 @@ func TestService_GetRaw(t *testing.T) { assert.Equal(t, expectedOrg, org) }) } + +func TestService_GetDefaultOrgStateOnCreate(t *testing.T) { + mockRepo := mocks.NewRepository(t) + mockRelationSvc := mocks.NewRelationService(t) + mockUserSvc := mocks.NewUserService(t) + mockAuthnSvc := mocks.NewAuthnService(t) + mockPolicySvc := mocks.NewPolicyService(t) + mockPrefSvc := mocks.NewPreferencesService(t) + + svc := organization.NewService(mockRepo, mockRelationSvc, mockUserSvc, mockAuthnSvc, mockPolicySvc, mockPrefSvc) + + t.Run("should return org state to be set on creation, as per preferences", func(t *testing.T) { + expectedPrefs := map[string]string{ + preference.PlatformDisableOrgsOnCreate: "true", + } + mockPrefSvc.On("LoadPlatformPreferences", mock.Anything).Return(expectedPrefs, nil).Once() + state, err := svc.GetDefaultOrgStateOnCreate(context.Background()) + assert.Nil(t, err) + assert.Equal(t, organization.Disabled, state) + }) + + t.Run("should return org state as enabled + error if preferences cannot be fetched", func(t *testing.T) { + expectedPrefs := map[string]string{} + mockPrefSvc.On("LoadPlatformPreferences", mock.Anything).Return(expectedPrefs, errors.New("an error occurred")).Once() + state, err := svc.GetDefaultOrgStateOnCreate(context.Background()) + assert.NotNil(t, err) + assert.Equal(t, "an error occurred", errors.Unwrap(err).Error()) + assert.Equal(t, organization.Enabled, state) + }) +} + +func TestService_AddMember(t *testing.T) { + t.Run("", func(t *testing.T) {}) + + t.Run("", func(t *testing.T) {}) +} + +func TestService_AttachToPlatform(t *testing.T) { + t.Run("", func(t *testing.T) {}) + + t.Run("", func(t *testing.T) {}) +} From df9620453f61936797c81b1b2845948729efacc8 Mon Sep 17 00:00:00 2001 From: Anuj Khandelwal Date: Mon, 2 Sep 2024 11:49:49 +0530 Subject: [PATCH 5/5] add test for adding users to org and attaching org to platform --- core/organization/service_test.go | 83 +++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/core/organization/service_test.go b/core/organization/service_test.go index 635badfcd..7b2f0979f 100644 --- a/core/organization/service_test.go +++ b/core/organization/service_test.go @@ -6,9 +6,13 @@ import ( "testing" "github.com/google/uuid" + "github.com/raystack/frontier/core/authenticate" "github.com/raystack/frontier/core/organization" "github.com/raystack/frontier/core/organization/mocks" + "github.com/raystack/frontier/core/policy" "github.com/raystack/frontier/core/preference" + "github.com/raystack/frontier/core/relation" + "github.com/raystack/frontier/internal/bootstrap/schema" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -157,13 +161,84 @@ func TestService_GetDefaultOrgStateOnCreate(t *testing.T) { } func TestService_AddMember(t *testing.T) { - t.Run("", func(t *testing.T) {}) + mockRepo := mocks.NewRepository(t) + mockRelationSvc := mocks.NewRelationService(t) + mockUserSvc := mocks.NewUserService(t) + mockAuthnSvc := mocks.NewAuthnService(t) + mockPolicySvc := mocks.NewPolicyService(t) + mockPrefSvc := mocks.NewPreferencesService(t) + + svc := organization.NewService(mockRepo, mockRelationSvc, mockUserSvc, mockAuthnSvc, mockPolicySvc, mockPrefSvc) - t.Run("", func(t *testing.T) {}) + t.Run("should create policy and relation for member as per role", func(t *testing.T) { + inputOrgID := "test-id" + inputRelationName := schema.MemberRelationName + inputPrincipal := authenticate.Principal{ + ID: "test-principal-id", + Type: schema.UserPrincipal, + } + + policyToBeCreated := policy.Policy{ + RoleID: organization.MemberRole, + ResourceID: inputOrgID, + ResourceType: schema.OrganizationNamespace, + PrincipalID: inputPrincipal.ID, + PrincipalType: inputPrincipal.Type, + } + + relationToBeCreated := relation.Relation{ + Object: relation.Object{ + ID: inputOrgID, + Namespace: schema.OrganizationNamespace, + }, + Subject: relation.Subject{ + ID: inputPrincipal.ID, + Namespace: inputPrincipal.Type, + }, + RelationName: schema.MemberRelationName, + } + mockPolicySvc.On("Create", mock.Anything, policyToBeCreated).Return(policy.Policy{}, nil) + mockRelationSvc.On("Create", mock.Anything, relationToBeCreated).Return(relation.Relation{}, nil) + + err := svc.AddMember(context.Background(), inputOrgID, inputRelationName, inputPrincipal) + assert.Nil(t, err) + }) } func TestService_AttachToPlatform(t *testing.T) { - t.Run("", func(t *testing.T) {}) + mockRepo := mocks.NewRepository(t) + mockRelationSvc := mocks.NewRelationService(t) + mockUserSvc := mocks.NewUserService(t) + mockAuthnSvc := mocks.NewAuthnService(t) + mockPolicySvc := mocks.NewPolicyService(t) + mockPrefSvc := mocks.NewPreferencesService(t) - t.Run("", func(t *testing.T) {}) + svc := organization.NewService(mockRepo, mockRelationSvc, mockUserSvc, mockAuthnSvc, mockPolicySvc, mockPrefSvc) + + inputOrgID := "some-org-id" + relationToBeCreated := relation.Relation{ + Object: relation.Object{ + ID: inputOrgID, + Namespace: schema.OrganizationNamespace, + }, + Subject: relation.Subject{ + ID: schema.PlatformID, + Namespace: schema.PlatformNamespace, + }, + RelationName: schema.PlatformRelationName, + } + + t.Run("should create a relation for org in platform namespace", func(t *testing.T) { + mockRelationSvc.On("Create", mock.Anything, relationToBeCreated).Return(relation.Relation{}, nil).Once() + err := svc.AttachToPlatform(context.Background(), inputOrgID) + assert.Nil(t, err) + }) + + t.Run("should return an error if relation creation fails", func(t *testing.T) { + expectedErr := errors.New("Internal error") + mockRelationSvc.On("Create", mock.Anything, relationToBeCreated).Return(relation.Relation{}, expectedErr).Once() + err := svc.AttachToPlatform(context.Background(), inputOrgID) + assert.NotNil(t, err) + assert.Equal(t, expectedErr, err) + }) }