proxy: Move to mockery for generating mocks

To align with what we're using everywhere else.
This commit is contained in:
Ralf Haferkamp
2023-03-15 13:10:29 +01:00
committed by Ralf Haferkamp
parent cd84a57a5e
commit f5cfa7e126
6 changed files with 160 additions and 276 deletions

View File

@@ -14,8 +14,9 @@ import (
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend"
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend/test"
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend/mocks"
"github.com/stretchr/testify/assert"
"github.com/test-go/testify/mock"
)
func TestTokenIsAddedWithMailClaim(t *testing.T) {
@@ -119,15 +120,12 @@ func newMockAccountResolver(userBackendResult *userv1beta1.User, userBackendErr
token, _ = tokenManager.MintToken(context.Background(), userBackendResult, s)
}
mock := &test.UserBackendMock{
GetUserByClaimsFunc: func(ctx context.Context, claim string, value string, withRoles bool) (*userv1beta1.User, string, error) {
return userBackendResult, token, userBackendErr
},
}
ub := mocks.UserBackend{}
ub.On("GetUserByClaims", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(userBackendResult, token, userBackendErr)
return AccountResolver(
Logger(log.NewLogger()),
UserProvider(mock),
UserProvider(&ub),
TokenManagerConfig(config.TokenManager{JWTSecret: "secret"}),
UserOIDCClaim(oidcclaim),
UserCS3Claim(cs3claim),

View File

@@ -1,42 +1,41 @@
package middleware
import (
"context"
"net/http"
"net/http/httptest"
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
. "github.com/onsi/ginkgo/v2"
"github.com/test-go/testify/mock"
. "github.com/onsi/gomega"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend"
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend/test"
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend/mocks"
)
var _ = Describe("Authenticating requests", Label("BasicAuthenticator"), func() {
var authenticator Authenticator
ub := mocks.UserBackend{}
ub.On("Authenticate", mock.Anything, "testuser", "testpassword").Return(
&userv1beta1.User{
Id: &userv1beta1.UserId{
Idp: "IdpId",
OpaqueId: "OpaqueId",
},
Username: "testuser",
Mail: "testuser@example.com",
},
"",
nil,
)
ub.On("Authenticate", mock.Anything, mock.Anything, mock.Anything).Return(nil, "", backend.ErrAccountNotFound)
BeforeEach(func() {
authenticator = BasicAuthenticator{
Logger: log.NewLogger(),
UserProvider: &test.UserBackendMock{
AuthenticateFunc: func(ctx context.Context, username, password string) (*userv1beta1.User, string, error) {
var user *userv1beta1.User
if username == "testuser" && password == "testpassword" {
user = &userv1beta1.User{
Id: &userv1beta1.UserId{
Idp: "IdpId",
OpaqueId: "OpaqueId",
},
Username: "testuser",
Mail: "testuser@example.com",
}
return user, "", nil
}
return nil, "", backend.ErrAccountNotFound
},
},
Logger: log.NewLogger(),
UserProvider: &ub,
}
})

View File

@@ -21,6 +21,8 @@ var (
ErrNotSupported = errors.New("operation not supported")
)
//go:generate mockery --name=UserBackend
// UserBackend allows the proxy to retrieve users from different user-backends (accounts-service, CS3)
type UserBackend interface {
GetUserByClaims(ctx context.Context, claim, value string, withRoles bool) (*cs3.User, string, error)

View File

@@ -0,0 +1,118 @@
// Code generated by mockery v2.14.0. DO NOT EDIT.
package mocks
import (
context "context"
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
mock "github.com/stretchr/testify/mock"
)
// UserBackend is an autogenerated mock type for the UserBackend type
type UserBackend struct {
mock.Mock
}
// Authenticate provides a mock function with given fields: ctx, username, password
func (_m *UserBackend) Authenticate(ctx context.Context, username string, password string) (*userv1beta1.User, string, error) {
ret := _m.Called(ctx, username, password)
var r0 *userv1beta1.User
if rf, ok := ret.Get(0).(func(context.Context, string, string) *userv1beta1.User); ok {
r0 = rf(ctx, username, password)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*userv1beta1.User)
}
}
var r1 string
if rf, ok := ret.Get(1).(func(context.Context, string, string) string); ok {
r1 = rf(ctx, username, password)
} else {
r1 = ret.Get(1).(string)
}
var r2 error
if rf, ok := ret.Get(2).(func(context.Context, string, string) error); ok {
r2 = rf(ctx, username, password)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// CreateUserFromClaims provides a mock function with given fields: ctx, claims
func (_m *UserBackend) CreateUserFromClaims(ctx context.Context, claims map[string]interface{}) (*userv1beta1.User, error) {
ret := _m.Called(ctx, claims)
var r0 *userv1beta1.User
if rf, ok := ret.Get(0).(func(context.Context, map[string]interface{}) *userv1beta1.User); ok {
r0 = rf(ctx, claims)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*userv1beta1.User)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, map[string]interface{}) error); ok {
r1 = rf(ctx, claims)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// GetUserByClaims provides a mock function with given fields: ctx, claim, value, withRoles
func (_m *UserBackend) GetUserByClaims(ctx context.Context, claim string, value string, withRoles bool) (*userv1beta1.User, string, error) {
ret := _m.Called(ctx, claim, value, withRoles)
var r0 *userv1beta1.User
if rf, ok := ret.Get(0).(func(context.Context, string, string, bool) *userv1beta1.User); ok {
r0 = rf(ctx, claim, value, withRoles)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*userv1beta1.User)
}
}
var r1 string
if rf, ok := ret.Get(1).(func(context.Context, string, string, bool) string); ok {
r1 = rf(ctx, claim, value, withRoles)
} else {
r1 = ret.Get(1).(string)
}
var r2 error
if rf, ok := ret.Get(2).(func(context.Context, string, string, bool) error); ok {
r2 = rf(ctx, claim, value, withRoles)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// GetUserGroups provides a mock function with given fields: ctx, userID
func (_m *UserBackend) GetUserGroups(ctx context.Context, userID string) {
_m.Called(ctx, userID)
}
type mockConstructorTestingTNewUserBackend interface {
mock.TestingT
Cleanup(func())
}
// NewUserBackend creates a new instance of UserBackend. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewUserBackend(t mockConstructorTestingTNewUserBackend) *UserBackend {
mock := &UserBackend{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}

View File

@@ -1,248 +0,0 @@
// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq
package test
import (
"context"
"sync"
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend"
)
// Ensure, that UserBackendMock does implement UserBackend.
// If this is not the case, regenerate this file with moq.
var _ backend.UserBackend = &UserBackendMock{}
// UserBackendMock is a mock implementation of UserBackend.
//
// func TestSomethingThatUsesUserBackend(t *testing.T) {
//
// // make and configure a mocked UserBackend
// mockedUserBackend := &UserBackendMock{
// AuthenticateFunc: func(ctx context.Context, username string, password string) (*userv1beta1.User, error) {
// panic("mock out the Authenticate method")
// },
// CreateUserFromClaimsFunc: func(ctx context.Context, claims *oidc.StandardClaims) (*userv1beta1.User, error) {
// panic("mock out the CreateUserFromClaims method")
// },
// GetUserByClaimsFunc: func(ctx context.Context, claim string, value string, withRoles bool) (*userv1beta1.User, error) {
// panic("mock out the GetUserByClaims method")
// },
// GetUserGroupsFunc: func(ctx context.Context, userID string) {
// panic("mock out the GetUserGroups method")
// },
// }
//
// // use mockedUserBackend in code that requires UserBackend
// // and then make assertions.
//
// }
type UserBackendMock struct {
// AuthenticateFunc mocks the Authenticate method.
AuthenticateFunc func(ctx context.Context, username string, password string) (*userv1beta1.User, string, error)
// CreateUserFromClaimsFunc mocks the CreateUserFromClaims method.
CreateUserFromClaimsFunc func(ctx context.Context, claims map[string]interface{}) (*userv1beta1.User, error)
// GetUserByClaimsFunc mocks the GetUserByClaims method.
GetUserByClaimsFunc func(ctx context.Context, claim string, value string, withRoles bool) (*userv1beta1.User, string, error)
// GetUserGroupsFunc mocks the GetUserGroups method.
GetUserGroupsFunc func(ctx context.Context, userID string)
// calls tracks calls to the methods.
calls struct {
// Authenticate holds details about calls to the Authenticate method.
Authenticate []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// Username is the username argument value.
Username string
// Password is the password argument value.
Password string
}
// CreateUserFromClaims holds details about calls to the CreateUserFromClaims method.
CreateUserFromClaims []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// Claims is the claims argument value.
Claims map[string]interface{}
}
// GetUserByClaims holds details about calls to the GetUserByClaims method.
GetUserByClaims []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// Claim is the claim argument value.
Claim string
// Value is the value argument value.
Value string
// WithRoles is the withRoles argument value.
WithRoles bool
}
// GetUserGroups holds details about calls to the GetUserGroups method.
GetUserGroups []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// UserID is the userID argument value.
UserID string
}
}
lockAuthenticate sync.RWMutex
lockCreateUserFromClaims sync.RWMutex
lockGetUserByClaims sync.RWMutex
lockGetUserGroups sync.RWMutex
}
// Authenticate calls AuthenticateFunc.
func (mock *UserBackendMock) Authenticate(ctx context.Context, username string, password string) (*userv1beta1.User, string, error) {
if mock.AuthenticateFunc == nil {
panic("UserBackendMock.AuthenticateFunc: method is nil but UserBackend.Authenticate was just called")
}
callInfo := struct {
Ctx context.Context
Username string
Password string
}{
Ctx: ctx,
Username: username,
Password: password,
}
mock.lockAuthenticate.Lock()
mock.calls.Authenticate = append(mock.calls.Authenticate, callInfo)
mock.lockAuthenticate.Unlock()
return mock.AuthenticateFunc(ctx, username, password)
}
// AuthenticateCalls gets all the calls that were made to Authenticate.
// Check the length with:
// len(mockedUserBackend.AuthenticateCalls())
func (mock *UserBackendMock) AuthenticateCalls() []struct {
Ctx context.Context
Username string
Password string
} {
var calls []struct {
Ctx context.Context
Username string
Password string
}
mock.lockAuthenticate.RLock()
calls = mock.calls.Authenticate
mock.lockAuthenticate.RUnlock()
return calls
}
// CreateUserFromClaims calls CreateUserFromClaimsFunc.
func (mock *UserBackendMock) CreateUserFromClaims(ctx context.Context, claims map[string]interface{}) (*userv1beta1.User, error) {
if mock.CreateUserFromClaimsFunc == nil {
panic("UserBackendMock.CreateUserFromClaimsFunc: method is nil but UserBackend.CreateUserFromClaims was just called")
}
callInfo := struct {
Ctx context.Context
Claims map[string]interface{}
}{
Ctx: ctx,
Claims: claims,
}
mock.lockCreateUserFromClaims.Lock()
mock.calls.CreateUserFromClaims = append(mock.calls.CreateUserFromClaims, callInfo)
mock.lockCreateUserFromClaims.Unlock()
return mock.CreateUserFromClaimsFunc(ctx, claims)
}
// CreateUserFromClaimsCalls gets all the calls that were made to CreateUserFromClaims.
// Check the length with:
// len(mockedUserBackend.CreateUserFromClaimsCalls())
func (mock *UserBackendMock) CreateUserFromClaimsCalls() []struct {
Ctx context.Context
Claims map[string]interface{}
} {
var calls []struct {
Ctx context.Context
Claims map[string]interface{}
}
mock.lockCreateUserFromClaims.RLock()
calls = mock.calls.CreateUserFromClaims
mock.lockCreateUserFromClaims.RUnlock()
return calls
}
// GetUserByClaims calls GetUserByClaimsFunc.
func (mock *UserBackendMock) GetUserByClaims(ctx context.Context, claim string, value string, withRoles bool) (*userv1beta1.User, string, error) {
if mock.GetUserByClaimsFunc == nil {
panic("UserBackendMock.GetUserByClaimsFunc: method is nil but UserBackend.GetUserByClaims was just called")
}
callInfo := struct {
Ctx context.Context
Claim string
Value string
WithRoles bool
}{
Ctx: ctx,
Claim: claim,
Value: value,
WithRoles: withRoles,
}
mock.lockGetUserByClaims.Lock()
mock.calls.GetUserByClaims = append(mock.calls.GetUserByClaims, callInfo)
mock.lockGetUserByClaims.Unlock()
return mock.GetUserByClaimsFunc(ctx, claim, value, withRoles)
}
// GetUserByClaimsCalls gets all the calls that were made to GetUserByClaims.
// Check the length with:
// len(mockedUserBackend.GetUserByClaimsCalls())
func (mock *UserBackendMock) GetUserByClaimsCalls() []struct {
Ctx context.Context
Claim string
Value string
WithRoles bool
} {
var calls []struct {
Ctx context.Context
Claim string
Value string
WithRoles bool
}
mock.lockGetUserByClaims.RLock()
calls = mock.calls.GetUserByClaims
mock.lockGetUserByClaims.RUnlock()
return calls
}
// GetUserGroups calls GetUserGroupsFunc.
func (mock *UserBackendMock) GetUserGroups(ctx context.Context, userID string) {
if mock.GetUserGroupsFunc == nil {
panic("UserBackendMock.GetUserGroupsFunc: method is nil but UserBackend.GetUserGroups was just called")
}
callInfo := struct {
Ctx context.Context
UserID string
}{
Ctx: ctx,
UserID: userID,
}
mock.lockGetUserGroups.Lock()
mock.calls.GetUserGroups = append(mock.calls.GetUserGroups, callInfo)
mock.lockGetUserGroups.Unlock()
mock.GetUserGroupsFunc(ctx, userID)
}
// GetUserGroupsCalls gets all the calls that were made to GetUserGroups.
// Check the length with:
// len(mockedUserBackend.GetUserGroupsCalls())
func (mock *UserBackendMock) GetUserGroupsCalls() []struct {
Ctx context.Context
UserID string
} {
var calls []struct {
Ctx context.Context
UserID string
}
mock.lockGetUserGroups.RLock()
calls = mock.calls.GetUserGroups
mock.lockGetUserGroups.RUnlock()
return calls
}

View File

@@ -1,4 +1,4 @@
// Code generated by mockery v2.10.4. DO NOT EDIT.
// Code generated by mockery v2.14.0. DO NOT EDIT.
package mocks
@@ -362,3 +362,18 @@ func (_m *Manager) WriteValue(value *v0.Value) (*v0.Value, error) {
return r0, r1
}
type mockConstructorTestingTNewManager interface {
mock.TestingT
Cleanup(func())
}
// NewManager creates a new instance of Manager. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewManager(t mockConstructorTestingTNewManager) *Manager {
mock := &Manager{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}