Remove handle field on signup
This commit is contained in:
committed by
Jože Fortun
parent
26e8dd0e4c
commit
979b467cb7
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/mail"
|
||||
@@ -1033,20 +1034,24 @@ func uniqueUserCheck(ctx context.Context, s store.Storer, u *types.User) (err er
|
||||
|
||||
func createUserHandle(ctx context.Context, s store.Users, u *types.User) {
|
||||
if u.Handle == "" {
|
||||
n := []string{
|
||||
fmt.Sprintf("%s_%s", u.Name, u.Username),
|
||||
regexp.
|
||||
MustCompile("(@.*)$").
|
||||
ReplaceAllString(u.Email, ""),
|
||||
}
|
||||
|
||||
for i := 1; i <= 10; i++ {
|
||||
n = append(n, fmt.Sprintf("%s_%s%d", u.Name, u.Username, i))
|
||||
}
|
||||
|
||||
u.Handle, _ = handle.Cast(
|
||||
// Must not exist before
|
||||
func(lookup string) bool {
|
||||
e, err := s.LookupUserByHandle(ctx, lookup)
|
||||
return err == store.ErrNotFound && (e == nil || e.ID == u.ID)
|
||||
},
|
||||
// use name or username
|
||||
u.Name,
|
||||
u.Username,
|
||||
// use email w/o domain
|
||||
regexp.
|
||||
MustCompile("(@.*)$").
|
||||
ReplaceAllString(u.Email, ""),
|
||||
//
|
||||
n...,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
a "github.com/cortezaproject/corteza/server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza/server/pkg/eventbus"
|
||||
"github.com/cortezaproject/corteza/server/pkg/rbac"
|
||||
@@ -15,6 +16,13 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type (
|
||||
u struct {
|
||||
store.Users
|
||||
L func(ctx context.Context, handle string) (*types.User, error)
|
||||
}
|
||||
)
|
||||
|
||||
func TestUser_ProtectedSearch(t *testing.T) {
|
||||
const testRoleID = 123
|
||||
|
||||
@@ -95,6 +103,84 @@ func TestUser_ProtectedSearch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_createUserHandle(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Define test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
user *types.User
|
||||
expected string
|
||||
s u
|
||||
}{
|
||||
{
|
||||
name: "Test with valid name",
|
||||
user: &types.User{
|
||||
Name: "John Doe",
|
||||
},
|
||||
expected: "JohnDoe_1",
|
||||
s: u{
|
||||
L: func(ctx context.Context, handle string) (*types.User, error) {
|
||||
return nil, store.ErrNotFound
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test with valid name and username",
|
||||
user: &types.User{
|
||||
Name: "John",
|
||||
Username: "username_provided_here123",
|
||||
},
|
||||
expected: "John_username_provided_here123",
|
||||
s: u{
|
||||
L: func(ctx context.Context, handle string) (*types.User, error) {
|
||||
if handle == "John1" {
|
||||
return nil, store.ErrNotUnique
|
||||
}
|
||||
return nil, store.ErrNotFound
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test with valid name and email",
|
||||
user: &types.User{
|
||||
Name: "John John",
|
||||
Email: "jj+test@example.ltd",
|
||||
},
|
||||
expected: "jjTest",
|
||||
s: u{
|
||||
L: func(ctx context.Context, handle string) (*types.User, error) {
|
||||
return nil, store.ErrNotFound
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test with valid name, not unique handle",
|
||||
user: &types.User{
|
||||
Name: "John Gardener",
|
||||
Email: "johng@example.ltd",
|
||||
},
|
||||
expected: "JohnGardener_1",
|
||||
s: u{
|
||||
L: func(ctx context.Context, handle string) (*types.User, error) {
|
||||
if handle == "johng" {
|
||||
return nil, store.ErrNotUnique
|
||||
}
|
||||
return nil, store.ErrNotFound
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Run test cases
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
createUserHandle(ctx, tc.s, tc.user)
|
||||
assert.Equal(t, tc.expected, tc.user.Handle)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_processAvatarInitials(t *testing.T) {
|
||||
// Define test cases
|
||||
tests := []struct {
|
||||
@@ -175,3 +261,7 @@ func Test_processAvatarInitials(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (uu u) LookupUserByHandle(ctx context.Context, handle string) (*types.User, error) {
|
||||
return uu.L(ctx, handle)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user