2020-06-25 01:20:58 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-11-21 18:08:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
m := FindUserByName("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)
|
2020-06-25 01:20:58 +02:00
|
|
|
assert.Equal(t, "admin", m.UserName)
|
2020-10-03 13:50:30 +02:00
|
|
|
assert.Equal(t, "Admin", m.FullName)
|
2021-08-10 12:30:04 +02:00
|
|
|
assert.True(t, m.RoleAdmin)
|
|
|
|
assert.False(t, m.RoleGuest)
|
|
|
|
assert.False(t, m.UserDisabled)
|
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("alice", func(t *testing.T) {
|
|
|
|
m := FindUserByName("alice")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 5, m.ID)
|
|
|
|
assert.Equal(t, "uqxetse3cy5eo9z2", m.UserUID)
|
|
|
|
assert.Equal(t, "alice", m.UserName)
|
|
|
|
assert.Equal(t, "Alice", m.FullName)
|
|
|
|
assert.Equal(t, "alice@example.com", m.PrimaryEmail)
|
|
|
|
assert.True(t, m.RoleAdmin)
|
|
|
|
assert.False(t, m.RoleGuest)
|
|
|
|
assert.False(t, m.UserDisabled)
|
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bob", func(t *testing.T) {
|
|
|
|
m := FindUserByName("bob")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 7, m.ID)
|
|
|
|
assert.Equal(t, "uqxc08w3d0ej2283", m.UserUID)
|
|
|
|
assert.Equal(t, "bob", m.UserName)
|
|
|
|
assert.Equal(t, "Bob", m.FullName)
|
|
|
|
assert.Equal(t, "bob@example.com", m.PrimaryEmail)
|
|
|
|
assert.False(t, m.RoleAdmin)
|
|
|
|
assert.False(t, m.RoleGuest)
|
|
|
|
assert.False(t, m.UserDisabled)
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
m := FindUserByName("")
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
m := FindUserByName("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
|
|
|
}
|
|
|
|
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
m := FindUserByName("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"))
|
|
|
|
})
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("no password existing", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000010", UserName: "Hans", FullName: ""}
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u12", UserName: "", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.True(t, p.InvalidPassword("abcdef"))
|
|
|
|
})
|
|
|
|
t.Run("password empty", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000011", UserName: "User", FullName: ""}
|
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)
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Equal(t, "", m.UserName)
|
2020-10-03 13:50:30 +02:00
|
|
|
assert.Equal(t, "Guest", m.FullName)
|
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)
|
|
|
|
assert.Equal(t, "alice", m.UserName)
|
|
|
|
assert.Equal(t, "Alice", m.FullName)
|
|
|
|
assert.Equal(t, "alice@example.com", m.PrimaryEmail)
|
|
|
|
assert.True(t, m.RoleAdmin)
|
|
|
|
assert.False(t, m.RoleGuest)
|
|
|
|
assert.False(t, m.UserDisabled)
|
|
|
|
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)
|
|
|
|
assert.Equal(t, "bob", m.UserName)
|
|
|
|
assert.Equal(t, "Bob", m.FullName)
|
|
|
|
assert.Equal(t, "bob@example.com", m.PrimaryEmail)
|
|
|
|
assert.False(t, m.RoleAdmin)
|
|
|
|
assert.False(t, m.RoleGuest)
|
|
|
|
assert.False(t, m.UserDisabled)
|
|
|
|
assert.NotEmpty(t, m.CreatedAt)
|
|
|
|
assert.NotEmpty(t, m.UpdatedAt)
|
|
|
|
})
|
|
|
|
|
2020-07-10 11:44:51 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_String(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("return UID", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "abc123", UserName: "", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Equal(t, "abc123", p.String())
|
|
|
|
})
|
|
|
|
t.Run("return display name", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "abc123", UserName: "", FullName: "Test"}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Equal(t, "Test", p.String())
|
|
|
|
})
|
|
|
|
t.Run("return user name", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "abc123", UserName: "Super-User", FullName: "Test"}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Equal(t, "Super-User", p.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_Admin(t *testing.T) {
|
2020-07-10 11:44:51 +02:00
|
|
|
t.Run("true", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: "", RoleAdmin: true}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.True(t, p.Admin())
|
|
|
|
})
|
|
|
|
t.Run("false", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: "", RoleAdmin: false}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.False(t, p.Admin())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "", UserName: "Hanna", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.True(t, p.Anonymous())
|
|
|
|
})
|
|
|
|
t.Run("false", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: "", RoleAdmin: true}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.False(t, p.Anonymous())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "", UserName: "Hanna", FullName: "", RoleGuest: true}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.True(t, p.Guest())
|
|
|
|
})
|
|
|
|
t.Run("false", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "", UserName: "Hanna", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.False(t, p.Guest())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
if err := p.SetPassword("abcdefgt"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("not registered", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "", UserName: "Hanna", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Error(t, p.SetPassword("abchjy"))
|
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("password too short", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Error(t, p.SetPassword("abc"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
func TestUser_InitPassword(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{UserUID: "u000000000000009", UserName: "Hanna", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Nil(t, FindPassword("u000000000000009"))
|
|
|
|
p.InitPassword("abcdek")
|
|
|
|
m := FindPassword("u000000000000009")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("already existing", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000010", UserName: "Hans", FullName: ""}
|
2021-08-11 13:21:05 +02:00
|
|
|
|
2021-08-10 12:30:04 +02:00
|
|
|
if err := p.Save(); err != nil {
|
2021-08-11 13:21:05 +02:00
|
|
|
t.Logf("can't user %s: ", err)
|
2021-08-10 12:30:04 +02:00
|
|
|
}
|
2021-08-11 13:21:05 +02:00
|
|
|
|
2021-08-10 12:30:04 +02:00
|
|
|
if err := p.SetPassword("hutfdt"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-11 13:21:05 +02:00
|
|
|
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.NotNil(t, FindPassword("u000000000000010"))
|
|
|
|
p.InitPassword("hutfdt")
|
|
|
|
m := FindPassword("u000000000000010")
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("not registered", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u12", UserName: "", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Nil(t, FindPassword("u12"))
|
|
|
|
p.InitPassword("dcjygkh")
|
|
|
|
assert.Nil(t, FindPassword("u12"))
|
|
|
|
})
|
|
|
|
t.Run("password empty", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000011", UserName: "User", FullName: ""}
|
2020-07-10 11:44:51 +02:00
|
|
|
assert.Nil(t, FindPassword("u000000000000011"))
|
|
|
|
p.InitPassword("")
|
|
|
|
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) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: "", RoleAdmin: true}
|
2020-07-10 13:57:51 +02:00
|
|
|
assert.Equal(t, acl.Role("admin"), p.Role())
|
|
|
|
})
|
|
|
|
t.Run("child", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: "", RoleChild: true}
|
2020-07-10 13:57:51 +02:00
|
|
|
assert.Equal(t, acl.Role("child"), p.Role())
|
|
|
|
})
|
|
|
|
t.Run("family", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: "", RoleFamily: true}
|
2020-07-10 13:57:51 +02:00
|
|
|
assert.Equal(t, acl.Role("family"), p.Role())
|
|
|
|
})
|
|
|
|
t.Run("friend", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: "", RoleFriend: true}
|
2020-07-10 13:57:51 +02:00
|
|
|
assert.Equal(t, acl.Role("friend"), p.Role())
|
|
|
|
})
|
|
|
|
t.Run("guest", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: "", RoleGuest: true}
|
2020-07-10 13:57:51 +02:00
|
|
|
assert.Equal(t, acl.Role("guest"), p.Role())
|
|
|
|
})
|
|
|
|
t.Run("default", func(t *testing.T) {
|
2020-10-03 13:50:30 +02:00
|
|
|
p := User{UserUID: "u000000000000008", UserName: "Hanna", FullName: ""}
|
2020-07-10 13:57:51 +02:00
|
|
|
assert.Equal(t, acl.Role("*"), p.Role())
|
|
|
|
})
|
|
|
|
}
|