3
0

Simplifiy auth test mock functions

This commit is contained in:
Denis Arh
2021-09-18 05:58:10 +02:00
parent 98eda7d474
commit c7446a032a
11 changed files with 75 additions and 107 deletions
+7 -9
View File
@@ -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)
+9 -10
View File
@@ -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)
+7 -9
View File
@@ -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)
+3 -5
View File
@@ -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")
+3 -4
View File
@@ -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)
+5 -9
View File
@@ -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)
+12 -16
View File
@@ -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)
+7 -9
View File
@@ -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)
+9 -12
View File
@@ -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"}
+6 -8
View File
@@ -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)
+7 -16
View File
@@ -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
}