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

297 lines
8.1 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/service"
"github.com/cortezaproject/corteza/server/system/types"
"github.com/stretchr/testify/require"
)
func Test_profileForm(t *testing.T) {
var (
user = makeMockUser()
req = &http.Request{
URL: &url.URL{},
}
authService authService
userService userService
authHandlers *AuthHandlers
authReq *request.AuthReq
authSettings = &settings.Settings{
EmailConfirmationRequired: false,
}
rq = require.New(t)
)
userService = &userServiceMocked{
findByAny: func(ctx context.Context, i interface{}) (*types.User, error) {
u := makeMockUser()
u.ID = i.(uint64)
return u, nil
},
}
authHandlers = prepareClientAuthHandlers(authService, authSettings)
authHandlers.UserService = userService
authReq = prepareClientAuthReq(authHandlers, req, user)
avatarUrl := fmt.Sprintf("//system/attachment/avatar/%d/original/%s", user.Meta.AvatarID, types.AttachmentKindAvatar)
userForm := map[string]string{
"email": user.Email,
"handle": user.Handle,
"name": user.Name,
"avatarUrl": avatarUrl,
"preferredLanguage": user.Meta.PreferredLanguage,
"initialTextColor": user.Meta.AvatarColor,
"initialBgColor": user.Meta.AvatarBgColor,
}
authReq.SetKV(map[string]string{})
err := authHandlers.profileForm(authReq)
rq.NoError(err)
rq.Equal(TmplProfile, authReq.Template)
rq.Equal(authReq.Data["form"], userForm)
rq.Equal(authReq.Data["emailConfirmationRequired"], false)
}
func Test_profileFormProc(t *testing.T) {
var (
user = makeMockUser()
req = &http.Request{
PostForm: url.Values{},
}
authService authService
userService userService
authHandlers *AuthHandlers
authReq *request.AuthReq
)
tcc := []testingExpect{
{
name: "success",
err: "request Content-Type isn't multipart/form-data",
alerts: []request.Alert{{Type: "primary", Text: "profile.alerts.profile-updated", Html: ""}},
link: GetLinks().Profile,
payload: map[string]string(nil),
fn: func(_ *settings.Settings) {
req.PostForm.Add("handle", "handle")
req.PostForm.Add("name", "name")
authService = &authServiceMocked{}
userService = &userServiceMocked{
update: func(c context.Context, u *types.User) (*types.User, error) {
u = makeMockUser()
u.SetRoles()
return u, nil
},
findByAny: func(ctx context.Context, i interface{}) (*types.User, error) {
u := makeMockUser()
u.ID = i.(uint64)
return u, nil
},
}
},
},
{
name: "proc invalid ID",
err: "",
alerts: []request.Alert{{Type: "danger", Text: "profile.alerts.profile-update-fail", Html: ""}},
link: GetLinks().Profile,
payload: map[string]string{
"email": "mockuser@example.tld",
"error": "invalid ID",
"handle": "handle",
"name": "name",
"initialBgColor": "",
"initialTextColor": "",
"avatarUrl": "//system/attachment/avatar/0/original/avatar",
},
fn: func(_ *settings.Settings) {
req.PostForm.Add("handle", "handle")
req.PostForm.Add("name", "name")
userService = &userServiceMocked{
update: func(c context.Context, u *types.User) (*types.User, error) {
return nil, service.UserErrInvalidID()
},
findByAny: func(ctx context.Context, i interface{}) (*types.User, error) {
u := makeMockUser()
u.ID = i.(uint64)
return u, nil
},
}
},
},
{
name: "proc invalid handle",
err: "",
alerts: []request.Alert{{Type: "danger", Text: "profile.alerts.profile-update-fail", Html: ""}},
link: GetLinks().Profile,
payload: map[string]string{
"email": "mockuser@example.tld",
"error": "invalid handle",
"handle": "handle",
"name": "name",
"initialBgColor": "",
"initialTextColor": "",
"avatarUrl": "//system/attachment/avatar/0/original/avatar",
},
fn: func(_ *settings.Settings) {
req.PostForm.Add("handle", "handle")
req.PostForm.Add("name", "name")
userService = &userServiceMocked{
update: func(c context.Context, u *types.User) (*types.User, error) {
return nil, service.UserErrInvalidHandle()
},
findByAny: func(ctx context.Context, i interface{}) (*types.User, error) {
u := makeMockUser()
u.ID = i.(uint64)
return u, nil
},
}
},
},
{
name: "proc invalid email",
err: "",
alerts: []request.Alert{{Type: "danger", Text: "profile.alerts.profile-update-fail", Html: ""}},
link: GetLinks().Profile,
payload: map[string]string{
"email": "mockuser@example.tld",
"error": "invalid email",
"handle": "handle",
"name": "name",
"initialBgColor": "",
"initialTextColor": "",
"avatarUrl": "//system/attachment/avatar/0/original/avatar",
},
fn: func(_ *settings.Settings) {
req.PostForm.Add("handle", "handle")
req.PostForm.Add("name", "name")
userService = &userServiceMocked{
update: func(c context.Context, u *types.User) (*types.User, error) {
return nil, service.UserErrInvalidEmail()
},
findByAny: func(ctx context.Context, i interface{}) (*types.User, error) {
u := makeMockUser()
u.ID = i.(uint64)
return u, nil
},
}
},
},
{
name: "proc handle not unique",
err: "",
alerts: []request.Alert{{Type: "danger", Text: "profile.alerts.profile-update-fail", Html: ""}},
link: GetLinks().Profile,
payload: map[string]string{
"email": "mockuser@example.tld",
"error": "handle not unique",
"handle": "handle",
"name": "name",
"initialBgColor": "",
"initialTextColor": "",
"avatarUrl": "//system/attachment/avatar/0/original/avatar",
},
fn: func(_ *settings.Settings) {
req.PostForm.Add("handle", "handle")
req.PostForm.Add("name", "name")
userService = &userServiceMocked{
update: func(c context.Context, u *types.User) (*types.User, error) {
return nil, service.UserErrHandleNotUnique()
},
findByAny: func(ctx context.Context, i interface{}) (*types.User, error) {
u := makeMockUser()
u.ID = i.(uint64)
return u, nil
},
}
},
},
{
name: "user.errors.notAllowedToUpdate",
err: "",
alerts: []request.Alert{{Type: "danger", Text: "profile.alerts.profile-update-fail", Html: ""}},
link: GetLinks().Profile,
payload: map[string]string{
"email": "mockuser@example.tld",
"error": "not allowed to update this user",
"handle": "handle",
"name": "name",
"initialBgColor": "",
"initialTextColor": "",
"avatarUrl": "//system/attachment/avatar/0/original/avatar",
},
fn: func(_ *settings.Settings) {
req.PostForm.Add("handle", "handle")
req.PostForm.Add("name", "name")
userService = &userServiceMocked{
update: func(c context.Context, u *types.User) (*types.User, error) {
return nil, service.UserErrNotAllowedToUpdate()
},
findByAny: func(ctx context.Context, i interface{}) (*types.User, error) {
u := makeMockUser()
u.ID = i.(uint64)
return u, nil
},
}
},
},
}
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)
authHandlers.UserService = userService
authReq = prepareClientAuthReq(authHandlers, req, user)
err := authHandlers.profileProc(authReq)
rq.NoError(err)
rq.Equal(tc.template, authReq.Template)
rq.Equal(tc.payload, authReq.GetKV())
rq.Equal(tc.alerts, authReq.NewAlerts)
rq.Equal(tc.link, authReq.RedirectTo)
})
}
}