Files
upstream-corteza/server/auth/handlers/handle_security_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

160 lines
3.6 KiB
Go

package handlers
import (
"context"
"fmt"
"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/types"
"github.com/stretchr/testify/require"
)
func Test_securityForm(t *testing.T) {
var (
user = makeMockUser()
req = &http.Request{
URL: &url.URL{},
}
authService authService
authHandlers *AuthHandlers
authReq *request.AuthReq
authSettings = &settings.Settings{}
rq = require.New(t)
)
authHandlers = prepareClientAuthHandlers(authService, authSettings)
authReq = prepareClientAuthReq(authHandlers, req, user)
err := authHandlers.securityForm(authReq)
rq.NoError(err)
rq.Equal(TmplSecurity, authReq.Template)
rq.Equal(true, authReq.Data["emailOtpEnforced"])
rq.Equal(false, authReq.Data["totpEnforced"])
}
func Test_securityProc(t *testing.T) {
var (
user = makeMockUser()
req = &http.Request{
PostForm: url.Values{},
}
authService authService
authHandlers *AuthHandlers
authReq *request.AuthReq
)
tcc := []testingExpect{
{
name: "reconfigureTOTP",
link: GetLinks().MfaTotpNewSecret,
payload: map[interface{}]interface{}{},
fn: func(_ *settings.Settings) {
req.Form.Set("action", "reconfigureTOTP")
},
},
{
name: "disableTOTP",
link: GetLinks().MfaTotpDisable,
payload: map[interface{}]interface{}{"totpSecret": "SECRET_VALUE"},
fn: func(_ *settings.Settings) {
req.Form.Set("action", "disableTOTP")
},
},
{
name: "disableEmailOTP failure",
err: "custom error",
link: GetLinks().Security,
payload: map[interface{}]interface{}{"totpSecret": "SECRET_VALUE"},
fn: func(_ *settings.Settings) {
req.Form.Set("action", "disableEmailOTP")
authService = &authServiceMocked{
configureEmailOTP: func(c context.Context, u uint64, b bool) (user *types.User, err error) {
return nil, fmt.Errorf("custom error")
},
}
},
},
}
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{}
authSettings := &settings.Settings{}
tc.fn(authSettings)
authHandlers = prepareClientAuthHandlers(authService, authSettings)
authReq = prepareClientAuthReq(authHandlers, req, user)
authReq.Session.Values = map[interface{}]interface{}{"totpSecret": "SECRET_VALUE"}
err := authHandlers.securityProc(authReq)
rq.Equal(tc.payload, authReq.Session.Values)
rq.Equal(tc.link, authReq.RedirectTo)
if tc.alerts != nil {
rq.Equal(tc.alerts, authReq.NewAlerts)
}
if tc.err != "" {
rq.Equal(tc.err, err.Error())
}
})
}
}
func Test_securityProcDisableEmailOTPSuccess(t *testing.T) {
var (
user = makeMockUser()
req = &http.Request{
Form: url.Values{},
PostForm: url.Values{},
}
authService authService
authHandlers *AuthHandlers
authReq *request.AuthReq
authSettings = &settings.Settings{}
rq = require.New(t)
)
req.Form.Set("action", "disableEmailOTP")
authService = &authServiceMocked{
configureEmailOTP: func(c context.Context, u uint64, b bool) (*types.User, error) {
return user, nil
},
}
authHandlers = prepareClientAuthHandlers(authService, authSettings)
authReq = prepareClientAuthReq(authHandlers, req, user)
authReq.Session.Values = map[interface{}]interface{}{"totpSecret": "SECRET_VALUE"}
err := authHandlers.securityProc(authReq)
rq.NoError(err)
rq.Equal([]request.Alert{{Type: "primary", Text: "security.template.mfa.email.disabled", Html: ""}}, authReq.NewAlerts)
}