Files
upstream-corteza/server/auth/handlers/handle_mfa_test.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

213 lines
5.8 KiB
Go

package handlers
import (
"context"
"net/http"
"net/url"
"testing"
"github.com/cortezaproject/corteza/server/auth/request"
"github.com/cortezaproject/corteza/server/auth/settings"
"github.com/cortezaproject/corteza/server/system/service"
"github.com/cortezaproject/corteza/server/system/types"
"github.com/stretchr/testify/require"
)
func Test_mfaProc(t *testing.T) {
var (
user = makeMockUser()
req = &http.Request{}
authService authService
authHandlers *AuthHandlers
authReq *request.AuthReq
)
service.CurrentSettings = &types.AppSettings{}
tcc := []testingExpect{
{
name: "Email: successful login",
payload: map[string]string(nil),
alerts: []request.Alert{{Type: "primary", Text: "mfa.alerts.email.confirmed"}},
link: GetLinks().Profile,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyEmailOtp")
req.PostForm.Add("code", "123456")
authService = &authServiceMocked{
validateEmailOTP: func(ctx context.Context, code string) (err error) {
return nil
},
}
},
},
{
name: "TOTP: successful login",
payload: map[string]string(nil),
alerts: []request.Alert{{Type: "primary", Text: "mfa.alerts.totp.confirmed"}},
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyTotp")
req.PostForm.Add("code", "123456")
authService = &authServiceMocked{
validateTOTP: func(ctx context.Context, code string) (err error) {
return nil
},
}
},
},
{
name: "Email: disabled",
payload: map[string]string{"emailOtpError": "multi factor authentication with email OTP is disabled"},
alerts: []request.Alert(nil),
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyEmailOtp")
authService = &authServiceMocked{
validateEmailOTP: func(ctx context.Context, code string) (err error) {
return service.AuthErrDisabledMFAWithEmailOTP()
},
}
},
},
{
name: "Email: auth failed for disabled user",
payload: map[string]string{"emailOtpError": "invalid username and password combination"},
alerts: []request.Alert(nil),
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyEmailOtp")
authService = &authServiceMocked{
validateEmailOTP: func(ctx context.Context, code string) (err error) {
return service.AuthErrFailedForUnknownUser()
},
}
},
},
{
name: "Email: invalid token",
payload: map[string]string{"emailOtpError": "invalid code"},
alerts: []request.Alert(nil),
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyEmailOtp")
req.PostForm.Add("code", "token_TOO_LONG")
authService = &authServiceMocked{
validateEmailOTP: func(ctx context.Context, code string) (err error) {
return service.AuthErrInvalidEmailOTP()
},
}
},
},
{
name: "Email: no token in credentials db",
payload: map[string]string{"emailOtpError": "invalid code"},
alerts: []request.Alert(nil),
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyEmailOtp")
req.PostForm.Add("code", "123456")
authService = &authServiceMocked{
validateEmailOTP: func(ctx context.Context, code string) (err error) {
return service.AuthErrInvalidEmailOTP()
},
}
},
},
{
name: "TOTP: disabled",
payload: map[string]string{"totpError": "multi factor authentication with TOTP is disabled"},
alerts: []request.Alert(nil),
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyTotp")
authService = &authServiceMocked{
validateTOTP: func(ctx context.Context, code string) (err error) {
return service.AuthErrDisabledMFAWithTOTP()
},
}
},
},
{
name: "TOTP: auth failed for disabled user",
payload: map[string]string{"totpError": "invalid username and password combination"},
alerts: []request.Alert(nil),
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyTotp")
authService = &authServiceMocked{
validateTOTP: func(ctx context.Context, code string) (err error) {
return service.AuthErrFailedForUnknownUser()
},
}
},
},
{
name: "TOTP: invalid token",
payload: map[string]string{"totpError": "invalid code"},
alerts: []request.Alert(nil),
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyTotp")
req.PostForm.Add("code", "token_TOO_LONG")
authService = &authServiceMocked{
validateTOTP: func(ctx context.Context, code string) (err error) {
return service.AuthErrInvalidTOTP()
},
}
},
},
{
name: "TOTP: no token in credentials db",
payload: map[string]string{"totpError": "invalid code"},
alerts: []request.Alert(nil),
link: GetLinks().Mfa,
fn: func(_ *settings.Settings) {
req.Form.Set("action", "verifyTotp")
req.PostForm.Add("code", "123456")
authService = &authServiceMocked{
validateTOTP: func(ctx context.Context, code string) (err error) {
return service.AuthErrInvalidTOTP()
},
}
},
},
}
for _, tc := range tcc {
t.Run(tc.name, func(t *testing.T) {
rq := require.New(t)
// reset from previous
req.Form = url.Values{}
req.PostForm = url.Values{}
user.Meta = &types.UserMeta{}
authSettings := &settings.Settings{}
tc.fn(authSettings)
authHandlers = prepareClientAuthHandlers(authService, authSettings)
authReq = prepareClientAuthReq(authHandlers, req, user)
err := authHandlers.mfaProc(authReq)
rq.NoError(err)
rq.Equal(tc.payload, authReq.GetKV())
rq.Equal(tc.alerts, authReq.NewAlerts)
rq.Equal(tc.link, authReq.RedirectTo)
})
}
}