From c7446a032a9f1fb582bca4e454a6097c314026f3 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Sat, 18 Sep 2021 05:58:10 +0200 Subject: [PATCH] Simplifiy auth test mock functions --- auth/handlers/handle_change-password_test.go | 16 +++++------ auth/handlers/handle_create-password_test.go | 19 +++++++------ auth/handlers/handle_login_test.go | 16 +++++------ auth/handlers/handle_logout_test.go | 8 +++--- auth/handlers/handle_mfa_test.go | 7 +++-- auth/handlers/handle_oauth2_test.go | 14 ++++------ auth/handlers/handle_password-reset_test.go | 28 +++++++++----------- auth/handlers/handle_profile_test.go | 16 +++++------ auth/handlers/handle_security_test.go | 21 +++++++-------- auth/handlers/handle_signup_test.go | 14 +++++----- auth/handlers/mock_test.go | 23 +++++----------- 11 files changed, 75 insertions(+), 107 deletions(-) diff --git a/auth/handlers/handle_change-password_test.go b/auth/handlers/handle_change-password_test.go index f21a193af..996b41fac 100644 --- a/auth/handlers/handle_change-password_test.go +++ b/auth/handlers/handle_change-password_test.go @@ -15,8 +15,7 @@ import ( func Test_changePasswordForm_setValues(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -32,9 +31,9 @@ func Test_changePasswordForm_setValues(t *testing.T) { authSettings := &settings.Settings{} - authService = prepareClientAuthService(ctx, user) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authService = prepareClientAuthService() + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) payload := map[string]string{"key": "value"} authReq.SetKV(payload) @@ -48,8 +47,7 @@ func Test_changePasswordForm_setValues(t *testing.T) { func Test_changePasswordProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -133,8 +131,8 @@ func Test_changePasswordProc(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.changePasswordProc(authReq) diff --git a/auth/handlers/handle_create-password_test.go b/auth/handlers/handle_create-password_test.go index 6831a3766..c3bbf7498 100644 --- a/auth/handlers/handle_create-password_test.go +++ b/auth/handlers/handle_create-password_test.go @@ -3,11 +3,12 @@ package handlers import ( "context" "errors" - "github.com/cortezaproject/corteza-server/system/service" "net/http" "net/url" "testing" + "github.com/cortezaproject/corteza-server/system/service" + "github.com/cortezaproject/corteza-server/auth/request" "github.com/cortezaproject/corteza-server/auth/settings" "github.com/cortezaproject/corteza-server/system/types" @@ -16,7 +17,6 @@ import ( func Test_createPasswordForm(t *testing.T) { var ( - ctx = context.Background() req = &http.Request{ URL: &url.URL{}, } @@ -42,7 +42,7 @@ func Test_createPasswordForm(t *testing.T) { return false }, validatePasswordCreateToken: func(ctx context.Context, token string) (user *types.User, err error) { - u := makeMockUser(ctx) + u := makeMockUser() u.SetRoles() return u, nil @@ -77,7 +77,7 @@ func Test_createPasswordForm(t *testing.T) { template: TmplCreatePassword, fn: func(_ *settings.Settings) { req.URL = &url.URL{RawQuery: "token=NOT_EMPTY"} - user = makeMockUser(ctx) + user = makeMockUser() authService = &authServiceMocked{ passwordSet: func(ctx context.Context, s string) bool { return false @@ -102,8 +102,8 @@ func Test_createPasswordForm(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) // unset so we get to the main functionality authReq.AuthUser = nil @@ -121,8 +121,7 @@ func Test_createPasswordForm(t *testing.T) { func Test_createPasswordProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -168,8 +167,8 @@ func Test_createPasswordProc(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.createPasswordProc(authReq) diff --git a/auth/handlers/handle_login_test.go b/auth/handlers/handle_login_test.go index 6d83fb125..2a75328db 100644 --- a/auth/handlers/handle_login_test.go +++ b/auth/handlers/handle_login_test.go @@ -22,8 +22,7 @@ type ( func Test_loginForm_setValues(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -39,9 +38,9 @@ func Test_loginForm_setValues(t *testing.T) { authSettings := &settings.Settings{} - authService = prepareClientAuthService(ctx, user) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authService = prepareClientAuthService() + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) payload := map[string]string{"key": "value"} authReq.SetKV(payload) @@ -59,8 +58,7 @@ func Test_loginForm_setValues(t *testing.T) { func Test_loginProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -202,8 +200,8 @@ func Test_loginProc(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.loginProc(authReq) diff --git a/auth/handlers/handle_logout_test.go b/auth/handlers/handle_logout_test.go index 05b39b7b6..c94b0e619 100644 --- a/auth/handlers/handle_logout_test.go +++ b/auth/handlers/handle_logout_test.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "net/http" "net/url" "testing" @@ -15,8 +14,7 @@ import ( func Test_logoutProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -33,8 +31,8 @@ func Test_logoutProc(t *testing.T) { service.CurrentSettings.Auth.Internal.Enabled = true authService = &authServiceMocked{} - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) req.PostForm = url.Values{} req.PostForm.Add("back", "/back") diff --git a/auth/handlers/handle_mfa_test.go b/auth/handlers/handle_mfa_test.go index f889fc6af..c0f2d6503 100644 --- a/auth/handlers/handle_mfa_test.go +++ b/auth/handlers/handle_mfa_test.go @@ -15,8 +15,7 @@ import ( func Test_mfaProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -199,8 +198,8 @@ func Test_mfaProc(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.mfaProc(authReq) diff --git a/auth/handlers/handle_oauth2_test.go b/auth/handlers/handle_oauth2_test.go index 34a64893e..b1b878494 100644 --- a/auth/handlers/handle_oauth2_test.go +++ b/auth/handlers/handle_oauth2_test.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "errors" "net/http" "net/url" @@ -15,8 +14,7 @@ import ( func Test_oauth2AuthorizeSuccess(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ Form: url.Values{}, @@ -66,13 +64,13 @@ func Test_oauth2AuthorizeSuccess(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) authHandlers = &AuthHandlers{ Log: zap.NewNop(), AuthService: authService, Settings: authSettings, OAuth2: oauthService, } + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.oauth2Authorize(authReq) @@ -89,8 +87,7 @@ func Test_oauth2AuthorizeSuccess(t *testing.T) { func Test_oauth2AuthorizeSuccessSetParams(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ Form: url.Values{}, @@ -112,15 +109,14 @@ func Test_oauth2AuthorizeSuccessSetParams(t *testing.T) { }, } - authReq = prepareClientAuthReq(ctx, req, user) - authReq.Session.Values["oauth2AuthParams"] = url.Values{"foo": []string{"bar"}} - authHandlers = &AuthHandlers{ Log: zap.NewNop(), AuthService: authService, Settings: authSettings, OAuth2: oauthService, } + authReq = prepareClientAuthReq(authHandlers, req, user) + authReq.Session.Values["oauth2AuthParams"] = url.Values{"foo": []string{"bar"}} err := authHandlers.oauth2Authorize(authReq) diff --git a/auth/handlers/handle_password-reset_test.go b/auth/handlers/handle_password-reset_test.go index 5a9617a45..346b6fbdd 100644 --- a/auth/handlers/handle_password-reset_test.go +++ b/auth/handlers/handle_password-reset_test.go @@ -16,8 +16,7 @@ import ( func Test_requestPasswordResetForm(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ URL: &url.URL{}, @@ -32,8 +31,8 @@ func Test_requestPasswordResetForm(t *testing.T) { rq = require.New(t) ) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) payload := map[string]string{"foo": "bar"} authReq.SetKV(payload) @@ -47,7 +46,6 @@ func Test_requestPasswordResetForm(t *testing.T) { func Test_resetPasswordForm(t *testing.T) { var ( - ctx = context.Background() req = &http.Request{ URL: &url.URL{}, } @@ -69,7 +67,7 @@ func Test_resetPasswordForm(t *testing.T) { authService = &authServiceMocked{ validatePasswordResetToken: func(ctx context.Context, token string) (user *types.User, err error) { - u := makeMockUser(ctx) + u := makeMockUser() u.SetRoles() return u, nil @@ -107,8 +105,8 @@ func Test_resetPasswordForm(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, nil) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, nil) // unset so we get to the main functionality authReq.AuthUser = nil @@ -126,8 +124,7 @@ func Test_resetPasswordForm(t *testing.T) { func Test_requestPasswordReset(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -178,8 +175,8 @@ func Test_requestPasswordReset(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.requestPasswordResetProc(authReq) @@ -193,8 +190,7 @@ func Test_requestPasswordReset(t *testing.T) { func Test_requestPasswordProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{} @@ -240,8 +236,8 @@ func Test_requestPasswordProc(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.resetPasswordProc(authReq) diff --git a/auth/handlers/handle_profile_test.go b/auth/handlers/handle_profile_test.go index 4fc777fe1..660961780 100644 --- a/auth/handlers/handle_profile_test.go +++ b/auth/handlers/handle_profile_test.go @@ -15,8 +15,7 @@ import ( func Test_profileForm(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ URL: &url.URL{}, @@ -33,8 +32,8 @@ func Test_profileForm(t *testing.T) { rq = require.New(t) ) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) userForm := map[string]string{ "email": user.Email, @@ -55,8 +54,7 @@ func Test_profileForm(t *testing.T) { func Test_profileFormProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ PostForm: url.Values{}, @@ -81,7 +79,7 @@ func Test_profileFormProc(t *testing.T) { userService = &userServiceMocked{ update: func(c context.Context, u *types.User) (*types.User, error) { - u = makeMockUser(ctx) + u = makeMockUser() u.SetRoles() return u, nil @@ -188,9 +186,9 @@ func Test_profileFormProc(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) authHandlers.UserService = userService + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.profileProc(authReq) diff --git a/auth/handlers/handle_security_test.go b/auth/handlers/handle_security_test.go index 7ad345ec7..8db654ade 100644 --- a/auth/handlers/handle_security_test.go +++ b/auth/handlers/handle_security_test.go @@ -15,8 +15,7 @@ import ( func Test_securityForm(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ URL: &url.URL{}, @@ -31,8 +30,8 @@ func Test_securityForm(t *testing.T) { rq = require.New(t) ) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.securityForm(authReq) @@ -44,8 +43,7 @@ func Test_securityForm(t *testing.T) { func Test_securityProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ PostForm: url.Values{}, @@ -102,8 +100,8 @@ func Test_securityProc(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) authReq.Session.Values = map[interface{}]interface{}{"totpSecret": "SECRET_VALUE"} @@ -125,8 +123,7 @@ func Test_securityProc(t *testing.T) { func Test_securityProcDisableEmailOTPSuccess(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ Form: url.Values{}, @@ -150,8 +147,8 @@ func Test_securityProcDisableEmailOTPSuccess(t *testing.T) { }, } - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) authReq.Session.Values = map[interface{}]interface{}{"totpSecret": "SECRET_VALUE"} diff --git a/auth/handlers/handle_signup_test.go b/auth/handlers/handle_signup_test.go index fbe56f1b6..82e33f49e 100644 --- a/auth/handlers/handle_signup_test.go +++ b/auth/handlers/handle_signup_test.go @@ -15,8 +15,7 @@ import ( func Test_signupForm(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ URL: &url.URL{}, @@ -31,8 +30,8 @@ func Test_signupForm(t *testing.T) { rq = require.New(t) ) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) userForm := map[string]string{ "email": user.Email, @@ -51,8 +50,7 @@ func Test_signupForm(t *testing.T) { func Test_signupProc(t *testing.T) { var ( - ctx = context.Background() - user = makeMockUser(ctx) + user = makeMockUser() req = &http.Request{ PostForm: url.Values{}, @@ -186,8 +184,8 @@ func Test_signupProc(t *testing.T) { tc.fn(authSettings) - authReq = prepareClientAuthReq(ctx, req, user) - authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) + authHandlers = prepareClientAuthHandlers(authService, authSettings) + authReq = prepareClientAuthReq(authHandlers, req, user) err := authHandlers.signupProc(authReq) diff --git a/auth/handlers/mock_test.go b/auth/handlers/mock_test.go index f113280a7..1a5331f65 100644 --- a/auth/handlers/mock_test.go +++ b/auth/handlers/mock_test.go @@ -30,8 +30,6 @@ type ( providerValidator func(string) error } - prepareFn func() - mockNotificationService struct { settings *types.AppSettings opt options.AuthOpt @@ -263,7 +261,7 @@ func (ma mockAuthService) SendEmailOTP(ctx context.Context) error { return nil } -func (ma mockedAuthService) ValidateTOTP(ctx context.Context, code string) (err error) { +func (ma mockAuthService) ValidateTOTP(ctx context.Context, code string) (err error) { err = nil return } @@ -303,7 +301,7 @@ func (ms mockSession) Save(r *http.Request, w http.ResponseWriter, s *sessions.S // // Helpers // -func makeMockAuthService(ctx context.Context) *mockAuthService { +func makeMockAuthService() *mockAuthService { service.DefaultAuthNotification = mockNotificationService{ settings: service.CurrentSettings, opt: options.AuthOpt{}, @@ -322,7 +320,7 @@ func makeMockAuthService(ctx context.Context) *mockAuthService { return &svc } -func makeMockUser(ctx context.Context) *types.User { +func makeMockUser() *types.User { u := &types.User{ID: 1, Username: "mock.user", Email: "mockuser@example.tld", Meta: &types.UserMeta{}} u.Meta.SecurityPolicy.MFA.EnforcedEmailOTP = true u.Meta.SecurityPolicy.MFA.EnforcedTOTP = false @@ -330,7 +328,7 @@ func makeMockUser(ctx context.Context) *types.User { return u } -func prepareClientAuthReq(ctx context.Context, req *http.Request, user *types.User) *request.AuthReq { +func prepareClientAuthReq(h *AuthHandlers, req *http.Request, user *types.User) *request.AuthReq { s := &settings.Settings{} s.MultiFactor.EmailOTP.Enabled = true s.MultiFactor.EmailOTP.Enforced = true @@ -358,12 +356,12 @@ func prepareClientAuthReq(ctx context.Context, req *http.Request, user *types.Us return authReq } -func prepareClientAuthService(ctx context.Context, user *types.User) *mockAuthService { - authService := makeMockAuthService(ctx) +func prepareClientAuthService() *mockAuthService { + authService := makeMockAuthService() return authService } -func prepareClientAuthHandlers(ctx context.Context, authService authService, s *settings.Settings) *AuthHandlers { +func prepareClientAuthHandlers(authService authService, s *settings.Settings) *AuthHandlers { return &AuthHandlers{ Log: zap.NewNop(), Locale: locale.Static(), @@ -371,10 +369,3 @@ func prepareClientAuthHandlers(ctx context.Context, authService authService, s * Settings: s, } } - -// wrapper around time.Now() that will aid service testing -func now() *time.Time { - c := time.Now() - // c := time.Now().Round(time.Second) - return &c -}