2020-06-25 01:20:58 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
2021-08-16 20:51:55 +02:00
|
|
|
"testing"
|
2020-11-21 18:08:41 +01:00
|
|
|
|
2021-08-17 22:49:32 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2021-08-18 20:18:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2021-08-17 22:49:32 +02:00
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestFindUserByName(t *testing.T) {
|
2020-06-25 01:20:58 +02:00
|
|
|
t.Run("admin", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
m := FindUserByLogin("admin")
|
2020-06-25 01:20:58 +02:00
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 1, m.ID)
|
2020-10-03 13:50:30 +02:00
|
|
|
assert.NotEmpty(t, m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
assert.Equal(t, "admin", m.Username)
|
|
|
|
assert.Equal(t, "admin", m.UserName())
|
|
|
|
m.Username = "Admin "
|
|
|
|
assert.Equal(t, "admin", m.UserName())
|
|
|
|
assert.Equal(t, "Admin ", m.Username)
|
|
|
|
assert.Equal(t, "Admin", m.DisplayName)
|
|
|
|
assert.Equal(t, acl.RoleAdmin, m.AclRole())
|
|
|
|
assert.False(t, m.IsEditor())
|
|
|
|
assert.False(t, m.IsViewer())
|
|
|
|
assert.False(t, m.IsGuest())
|
|
|
|
assert.True(t, m.SuperAdmin)
|
|
|
|
assert.True(t, m.CanLogin)
|
|
|
|
assert.True(t, m.CanInvite)
|
2021-08-10 12:30:04 +02:00
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("alice", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
m := FindUserByLogin("alice")
|
2021-08-10 12:30:04 +02:00
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 5, m.ID)
|
|
|
|
assert.Equal(t, "uqxetse3cy5eo9z2", m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
assert.Equal(t, "alice", m.Username)
|
|
|
|
assert.Equal(t, "Alice", m.DisplayName)
|
|
|
|
assert.Equal(t, "alice@example.com", m.Email)
|
|
|
|
assert.True(t, m.SuperAdmin)
|
|
|
|
assert.Equal(t, acl.RoleAdmin, m.AclRole())
|
|
|
|
assert.NotEqual(t, acl.RoleGuest, m.AclRole())
|
|
|
|
assert.False(t, m.IsEditor())
|
|
|
|
assert.False(t, m.IsViewer())
|
|
|
|
assert.False(t, m.IsGuest())
|
|
|
|
assert.True(t, m.CanLogin)
|
2021-08-10 12:30:04 +02:00
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bob", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
m := FindUserByLogin("bob")
|
2021-08-10 12:30:04 +02:00
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 7, m.ID)
|
|
|
|
assert.Equal(t, "uqxc08w3d0ej2283", m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
assert.Equal(t, "bob", m.Username)
|
|
|
|
assert.Equal(t, "Bob", m.DisplayName)
|
|
|
|
assert.Equal(t, "bob@example.com", m.Email)
|
|
|
|
assert.False(t, m.SuperAdmin)
|
|
|
|
assert.True(t, m.IsEditor())
|
|
|
|
assert.False(t, m.IsViewer())
|
|
|
|
assert.False(t, m.IsGuest())
|
|
|
|
assert.True(t, m.CanLogin)
|
2020-06-25 01:20:58 +02:00
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
m := FindUserByLogin("")
|
2020-06-25 01:20:58 +02:00
|
|
|
|
|
|
|
if m != nil {
|
|
|
|
t.Fatal("result should be nil")
|
|
|
|
}
|
|
|
|
})
|
2020-07-10 11:44:51 +02:00
|
|
|
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
m := FindUserByLogin("xxx")
|
2020-07-10 11:44:51 +02:00
|
|
|
|
|
|
|
if m != nil {
|
|
|
|
t.Fatal("result should be nil")
|
|
|
|
}
|
|
|
|
})
|
2020-06-25 01:20:58 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
func TestUser_Create(t *testing.T) {
|
|
|
|
t.Run("Slug", func(t *testing.T) {
|
|
|
|
var m = User{
|
|
|
|
Username: "example",
|
|
|
|
UserRole: acl.RoleEditor.String(),
|
|
|
|
DisplayName: "Example",
|
|
|
|
SuperAdmin: false,
|
|
|
|
CanLogin: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Create(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "example", m.UserName())
|
|
|
|
assert.Equal(t, "example", m.Username)
|
|
|
|
assert.Equal(t, m.Username, m.UserSlug)
|
|
|
|
|
|
|
|
if err := m.UpdateName("example-editor"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "example-editor", m.UserName())
|
|
|
|
assert.Equal(t, "example-editor", m.Username)
|
|
|
|
assert.Equal(t, m.Username, m.UserSlug)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_SetName(t *testing.T) {
|
|
|
|
t.Run("photoprism", func(t *testing.T) {
|
|
|
|
m := FindUserByLogin("admin")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "admin", m.UserName())
|
|
|
|
assert.Equal(t, "admin", m.Username)
|
|
|
|
|
|
|
|
m.SetUsername("photoprism")
|
|
|
|
|
|
|
|
assert.Equal(t, "photoprism", m.UserName())
|
|
|
|
assert.Equal(t, "photoprism", m.Username)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_InvalidPassword(t *testing.T) {
|
2020-06-25 01:20:58 +02:00
|
|
|
t.Run("admin", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
m := FindUserByLogin("admin")
|
2020-06-25 01:20:58 +02:00
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.False(t, m.InvalidPassword("photoprism"))
|
|
|
|
})
|
2021-08-27 15:33:33 +02:00
|
|
|
t.Run("admin invalid password", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
m := FindUserByLogin("admin")
|
2021-08-27 15:33:33 +02:00
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, m.InvalidPassword("wrong-password"))
|
|
|
|
})
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("no password existing", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000010", Username: "Hans", DisplayName: ""}
|
2021-08-10 12:30:04 +02:00
|
|
|
err := p.Save()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.True(t, p.InvalidPassword("abcdef"))
|
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("not registered", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u12", Username: "", DisplayName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.True(t, p.InvalidPassword("abcdef"))
|
|
|
|
})
|
|
|
|
t.Run("password empty", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000011", Username: "User", DisplayName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.True(t, p.InvalidPassword(""))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_Save(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("success", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{}
|
2020-07-10 11:44:51 +02:00
|
|
|
|
|
|
|
err := p.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestFirstOrCreateUser(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("not existing", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := &User{ID: 555}
|
2020-07-10 11:44:51 +02:00
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
result := FirstOrCreateUser(p)
|
2020-07-10 11:44:51 +02:00
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotEmpty(t, result.ID)
|
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("existing", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := &User{ID: 1234}
|
2020-07-10 11:44:51 +02:00
|
|
|
err := p.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
result := FirstOrCreateUser(p)
|
2020-07-10 11:44:51 +02:00
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
assert.NotEmpty(t, result.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestFindUserByUID(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("guest", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
m := FindUserByUID("u000000000000002")
|
2020-07-10 11:44:51 +02:00
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, -2, m.ID)
|
2020-10-03 13:50:30 +02:00
|
|
|
assert.NotEmpty(t, m.UserUID)
|
2022-09-02 22:55:57 +02:00
|
|
|
assert.Equal(t, "guest", m.Username)
|
2022-09-02 21:30:50 +02:00
|
|
|
assert.Equal(t, "Guest", m.DisplayName)
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
m := FindUserByUID("")
|
2020-07-10 11:44:51 +02:00
|
|
|
|
|
|
|
if m != nil {
|
|
|
|
t.Fatal("result should be nil")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
m := FindUserByUID("xxx")
|
2020-07-10 11:44:51 +02:00
|
|
|
|
|
|
|
if m != nil {
|
|
|
|
t.Fatal("result should be nil")
|
|
|
|
}
|
|
|
|
})
|
2021-08-12 15:13:15 +02:00
|
|
|
|
|
|
|
t.Run("alice", func(t *testing.T) {
|
|
|
|
m := FindUserByUID("uqxetse3cy5eo9z2")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 5, m.ID)
|
|
|
|
assert.Equal(t, "uqxetse3cy5eo9z2", m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
assert.Equal(t, "alice", m.UserName())
|
|
|
|
assert.Equal(t, "Alice", m.DisplayName)
|
|
|
|
assert.Equal(t, "alice@example.com", m.Email)
|
|
|
|
assert.True(t, m.SuperAdmin)
|
|
|
|
assert.True(t, m.IsAdmin())
|
|
|
|
assert.False(t, m.IsEditor())
|
|
|
|
assert.False(t, m.IsViewer())
|
|
|
|
assert.False(t, m.IsGuest())
|
|
|
|
assert.True(t, m.CanLogin)
|
2021-08-12 15:13:15 +02:00
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bob", func(t *testing.T) {
|
|
|
|
m := FindUserByUID("uqxc08w3d0ej2283")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 7, m.ID)
|
|
|
|
assert.Equal(t, "uqxc08w3d0ej2283", m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
assert.Equal(t, "bob", m.Username)
|
|
|
|
assert.Equal(t, "Bob", m.DisplayName)
|
|
|
|
assert.Equal(t, "bob@example.com", m.Email)
|
|
|
|
assert.False(t, m.SuperAdmin)
|
|
|
|
assert.False(t, m.IsAdmin())
|
|
|
|
assert.True(t, m.IsEditor())
|
|
|
|
assert.False(t, m.IsViewer())
|
|
|
|
assert.False(t, m.IsGuest())
|
|
|
|
assert.True(t, m.CanLogin)
|
2021-08-12 15:13:15 +02:00
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
2021-08-12 20:29:15 +02:00
|
|
|
t.Run("friend", func(t *testing.T) {
|
|
|
|
m := FindUserByUID("uqxqg7i1kperxvu7")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 8, m.ID)
|
|
|
|
assert.Equal(t, "uqxqg7i1kperxvu7", m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
assert.False(t, m.SuperAdmin)
|
|
|
|
assert.False(t, m.IsAdmin())
|
|
|
|
assert.False(t, m.IsEditor())
|
|
|
|
assert.True(t, m.IsViewer())
|
|
|
|
assert.False(t, m.IsGuest())
|
|
|
|
assert.True(t, m.CanLogin)
|
2021-08-12 20:29:15 +02:00
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
2021-08-12 15:13:15 +02:00
|
|
|
|
2020-07-10 11:44:51 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_String(t *testing.T) {
|
2021-11-12 09:10:15 +01:00
|
|
|
t.Run("UID", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "abc123", Username: "", DisplayName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Equal(t, "abc123", p.String())
|
|
|
|
})
|
2022-09-02 21:30:50 +02:00
|
|
|
t.Run("FullName", func(t *testing.T) {
|
|
|
|
p := User{UserUID: "abc123", Username: "", DisplayName: "Test"}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Equal(t, "Test", p.String())
|
|
|
|
})
|
2022-09-02 21:30:50 +02:00
|
|
|
t.Run("Username", func(t *testing.T) {
|
|
|
|
p := User{UserUID: "abc123", Username: "Super-User ", DisplayName: "Test"}
|
2021-11-12 09:10:15 +01:00
|
|
|
assert.Equal(t, "super-user", p.String())
|
2020-07-10 11:44:51 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_Admin(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
t.Run("SuperAdmin", func(t *testing.T) {
|
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: "", SuperAdmin: true}
|
|
|
|
assert.True(t, p.IsAdmin())
|
|
|
|
})
|
|
|
|
t.Run("RoleAdmin", func(t *testing.T) {
|
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: "", UserRole: acl.RoleAdmin.String()}
|
|
|
|
assert.True(t, p.IsAdmin())
|
2020-07-10 11:44:51 +02:00
|
|
|
})
|
|
|
|
t.Run("false", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: "", SuperAdmin: false, UserRole: ""}
|
|
|
|
assert.False(t, p.IsAdmin())
|
2020-07-10 11:44:51 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_Anonymous(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("true", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "", Username: "Hanna", DisplayName: ""}
|
|
|
|
assert.True(t, p.IsAnonymous())
|
2020-07-10 11:44:51 +02:00
|
|
|
})
|
|
|
|
t.Run("false", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: "", SuperAdmin: true}
|
|
|
|
assert.False(t, p.IsAnonymous())
|
2020-07-10 11:44:51 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_Guest(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("true", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "", Username: "Hanna", DisplayName: "", UserRole: "guest"}
|
|
|
|
assert.True(t, p.IsGuest())
|
2020-07-10 11:44:51 +02:00
|
|
|
})
|
|
|
|
t.Run("false", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "", Username: "Hanna", DisplayName: ""}
|
|
|
|
assert.False(t, p.IsGuest())
|
2020-07-10 11:44:51 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_SetPassword(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("success", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: ""}
|
2022-04-12 19:44:43 +02:00
|
|
|
if err := p.SetPassword("insecure"); err != nil {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("not registered", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "", Username: "Hanna", DisplayName: ""}
|
2022-04-12 19:44:43 +02:00
|
|
|
assert.Error(t, p.SetPassword("insecure"))
|
2020-07-10 11:44:51 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("password too short", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: ""}
|
2022-05-20 19:25:19 +02:00
|
|
|
assert.Error(t, p.SetPassword("cat"))
|
2020-07-10 11:44:51 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
func TestUser_InitLogin(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("success", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000009", Username: "Hanna", DisplayName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Nil(t, FindPassword("u000000000000009"))
|
2022-09-02 21:30:50 +02:00
|
|
|
p.InitAccount("admin", "insecure")
|
2020-07-10 11:44:51 +02:00
|
|
|
m := FindPassword("u000000000000009")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("already existing", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000010", Username: "Hans", DisplayName: ""}
|
2021-08-11 13:21:05 +02:00
|
|
|
|
2021-08-10 12:30:04 +02:00
|
|
|
if err := p.Save(); err != nil {
|
2022-01-05 11:40:44 +01:00
|
|
|
t.Logf("cannot user %s: ", err)
|
2021-08-10 12:30:04 +02:00
|
|
|
}
|
2021-08-11 13:21:05 +02:00
|
|
|
|
2022-04-12 19:44:43 +02:00
|
|
|
if err := p.SetPassword("insecure"); err != nil {
|
2021-08-10 12:30:04 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-11 13:21:05 +02:00
|
|
|
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.NotNil(t, FindPassword("u000000000000010"))
|
2022-09-02 21:30:50 +02:00
|
|
|
p.InitAccount("admin", "insecure")
|
2020-07-10 11:44:51 +02:00
|
|
|
m := FindPassword("u000000000000010")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("not registered", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u12", Username: "", DisplayName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Nil(t, FindPassword("u12"))
|
2022-09-02 21:30:50 +02:00
|
|
|
p.InitAccount("admin", "insecure")
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Nil(t, FindPassword("u12"))
|
|
|
|
})
|
|
|
|
t.Run("password empty", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000011", Username: "User", DisplayName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Nil(t, FindPassword("u000000000000011"))
|
2022-09-02 21:30:50 +02:00
|
|
|
p.InitAccount("admin", "")
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Nil(t, FindPassword("u000000000000011"))
|
|
|
|
})
|
2020-06-25 01:20:58 +02:00
|
|
|
}
|
2020-07-10 13:57:51 +02:00
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_Role(t *testing.T) {
|
2020-07-10 13:57:51 +02:00
|
|
|
t.Run("admin", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: "", SuperAdmin: true, UserRole: acl.RoleAdmin.String()}
|
|
|
|
assert.Equal(t, acl.Role("admin"), p.AclRole())
|
|
|
|
assert.True(t, p.IsAdmin())
|
|
|
|
assert.False(t, p.IsGuest())
|
2020-07-10 13:57:51 +02:00
|
|
|
})
|
|
|
|
t.Run("guest", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: "", UserRole: acl.RoleGuest.String()}
|
|
|
|
assert.Equal(t, acl.Role("guest"), p.AclRole())
|
|
|
|
assert.False(t, p.IsAdmin())
|
|
|
|
assert.True(t, p.IsGuest())
|
2020-07-10 13:57:51 +02:00
|
|
|
})
|
|
|
|
t.Run("default", func(t *testing.T) {
|
2022-09-02 21:30:50 +02:00
|
|
|
p := User{UserUID: "u000000000000008", Username: "Hanna", DisplayName: ""}
|
|
|
|
assert.Equal(t, acl.Role("*"), p.AclRole())
|
|
|
|
assert.False(t, p.IsAdmin())
|
|
|
|
assert.False(t, p.IsGuest())
|
2020-07-10 13:57:51 +02:00
|
|
|
})
|
|
|
|
}
|
2021-08-16 20:51:55 +02:00
|
|
|
|
|
|
|
func TestUser_Validate(t *testing.T) {
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
u := &User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "validate",
|
|
|
|
DisplayName: "Validate",
|
|
|
|
Email: "validate@example.com",
|
2021-08-16 20:51:55 +02:00
|
|
|
}
|
|
|
|
err := u.Validate()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
t.Run("username empty", func(t *testing.T) {
|
|
|
|
u := &User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "",
|
|
|
|
DisplayName: "Validate",
|
|
|
|
Email: "validate@example.com",
|
2021-08-16 20:51:55 +02:00
|
|
|
}
|
|
|
|
err := u.Validate()
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("username too short", func(t *testing.T) {
|
|
|
|
u := &User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "va",
|
|
|
|
DisplayName: "Validate",
|
|
|
|
Email: "validate@example.com",
|
2021-08-16 20:51:55 +02:00
|
|
|
}
|
|
|
|
err := u.Validate()
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("username not unique", func(t *testing.T) {
|
2021-08-18 20:18:39 +02:00
|
|
|
FirstOrCreateUser(&User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "notunique1",
|
2021-08-16 20:51:55 +02:00
|
|
|
})
|
2021-08-27 15:33:33 +02:00
|
|
|
u := &User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "notunique1",
|
|
|
|
DisplayName: "Not Unique",
|
|
|
|
Email: "notunique1@example.com",
|
2021-08-18 20:18:39 +02:00
|
|
|
}
|
2021-08-27 15:33:33 +02:00
|
|
|
assert.Error(t, u.Validate())
|
2021-08-16 20:51:55 +02:00
|
|
|
})
|
|
|
|
t.Run("email not unique", func(t *testing.T) {
|
2021-08-18 20:18:39 +02:00
|
|
|
FirstOrCreateUser(&User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Email: "notunique2@example.com",
|
2021-08-18 20:18:39 +02:00
|
|
|
})
|
2021-08-27 15:33:33 +02:00
|
|
|
u := &User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "notunique2",
|
|
|
|
Email: "notunique2@example.com",
|
|
|
|
DisplayName: "Not Unique",
|
2021-08-18 20:18:39 +02:00
|
|
|
}
|
2021-08-27 15:33:33 +02:00
|
|
|
assert.Error(t, u.Validate())
|
2021-08-18 20:18:39 +02:00
|
|
|
})
|
|
|
|
t.Run("update user - email not unique", func(t *testing.T) {
|
|
|
|
FirstOrCreateUser(&User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "notunique3",
|
|
|
|
Email: "notunique3@example.com",
|
|
|
|
DisplayName: "Not Unique",
|
2021-08-18 20:18:39 +02:00
|
|
|
})
|
2021-08-16 20:51:55 +02:00
|
|
|
u := FirstOrCreateUser(&User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "notunique30",
|
|
|
|
Email: "notunique3@example.com",
|
|
|
|
DisplayName: "Not Unique",
|
2021-08-16 20:51:55 +02:00
|
|
|
})
|
2022-09-02 21:30:50 +02:00
|
|
|
u.Username = "notunique3"
|
2021-08-18 20:18:39 +02:00
|
|
|
assert.Error(t, u.Validate())
|
2021-08-16 20:51:55 +02:00
|
|
|
})
|
2021-08-27 15:33:33 +02:00
|
|
|
t.Run("primary email empty", func(t *testing.T) {
|
|
|
|
FirstOrCreateUser(&User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "nnomail",
|
2021-08-27 15:33:33 +02:00
|
|
|
})
|
|
|
|
u := &User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "nomail",
|
|
|
|
Email: "",
|
|
|
|
DisplayName: "No Mail",
|
2021-08-27 15:33:33 +02:00
|
|
|
}
|
|
|
|
assert.Nil(t, u.Validate())
|
|
|
|
})
|
2021-08-16 20:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateWithPassword(t *testing.T) {
|
|
|
|
t.Run("password too short", func(t *testing.T) {
|
2021-08-18 20:18:39 +02:00
|
|
|
u := form.UserCreate{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "thomas1",
|
2021-08-18 20:18:39 +02:00
|
|
|
Email: "thomas1@example.com",
|
|
|
|
Password: "hel",
|
2021-08-16 20:51:55 +02:00
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2021-08-18 20:18:39 +02:00
|
|
|
err := CreateWithPassword(u)
|
2021-08-16 20:51:55 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
2021-08-18 20:18:39 +02:00
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
u := form.UserCreate{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "thomas2",
|
2021-08-18 20:18:39 +02:00
|
|
|
Email: "thomas2@example.com",
|
|
|
|
Password: "helloworld",
|
2021-08-16 20:51:55 +02:00
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2021-08-18 20:18:39 +02:00
|
|
|
err := CreateWithPassword(u)
|
|
|
|
assert.Nil(t, err)
|
2021-08-16 20:51:55 +02:00
|
|
|
})
|
|
|
|
}
|
2021-08-23 11:52:37 +02:00
|
|
|
|
|
|
|
func TestDeleteUser(t *testing.T) {
|
|
|
|
t.Run("delete user - success", func(t *testing.T) {
|
|
|
|
u := &User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "thomasdel",
|
|
|
|
Email: "thomasdel@example.com",
|
|
|
|
DisplayName: "Thomas Delete",
|
2021-08-23 11:52:37 +02:00
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2021-08-23 11:52:37 +02:00
|
|
|
u = FirstOrCreateUser(u)
|
|
|
|
err := u.Delete()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("delete user - not in db", func(t *testing.T) {
|
|
|
|
u := &User{
|
2022-09-02 21:30:50 +02:00
|
|
|
Username: "thomasdel2",
|
|
|
|
Email: "thomasdel2@example.com",
|
|
|
|
DisplayName: "Thomas Delete 2",
|
2021-08-23 11:52:37 +02:00
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2021-08-23 11:52:37 +02:00
|
|
|
err := u.Delete()
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2021-08-27 15:33:33 +02:00
|
|
|
|
|
|
|
func TestUser_Deleted(t *testing.T) {
|
|
|
|
assert.False(t, UserFixtures.Pointer("alice").Deleted())
|
|
|
|
assert.True(t, UserFixtures.Pointer("deleted").Deleted())
|
|
|
|
}
|