Tests: Add user fixtures and unit test

This commit is contained in:
theresa 2021-08-27 15:33:33 +02:00
parent 3bdabdedef
commit 64ad674f5f
2 changed files with 44 additions and 4 deletions

View file

@ -53,6 +53,19 @@ var UserFixtures = UserMap{
UserDisabled: true,
PrimaryEmail: "friend@example.com",
},
"deleted": {
ID: 10000008,
AddressID: 1000000,
UserUID: "uqxqg7i1kperxvu8",
UserName: "deleted",
FullName: "Deleted User",
RoleAdmin: false,
RoleGuest: true,
RoleFriend: false,
UserDisabled: false,
PrimaryEmail: "",
DeletedAt: &deleteTime,
},
}
// CreateUserFixtures inserts known entities into the database for testing.

View file

@ -93,6 +93,15 @@ func TestUser_InvalidPassword(t *testing.T) {
assert.False(t, m.InvalidPassword("photoprism"))
})
t.Run("admin invalid password", func(t *testing.T) {
m := FindUserByName("admin")
if m == nil {
t.Fatal("result should not be nil")
}
assert.True(t, m.InvalidPassword("wrong-password"))
})
t.Run("no password existing", func(t *testing.T) {
p := User{UserUID: "u000000000000010", UserName: "Hans", FullName: ""}
err := p.Save()
@ -414,26 +423,26 @@ func TestUser_Validate(t *testing.T) {
AddressID: 1,
UserName: "notunique1",
})
ures := &User{
u := &User{
AddressID: 1,
UserName: "notunique1",
FullName: "Not Unique",
PrimaryEmail: "notunique1@example.com",
}
assert.Error(t, ures.Validate())
assert.Error(t, u.Validate())
})
t.Run("email not unique", func(t *testing.T) {
FirstOrCreateUser(&User{
AddressID: 1,
PrimaryEmail: "notunique2@example.com",
})
ures := &User{
u := &User{
AddressID: 1,
UserName: "notunique2",
FullName: "Not Unique",
PrimaryEmail: "notunique2@example.com",
}
assert.Error(t, ures.Validate())
assert.Error(t, u.Validate())
})
t.Run("update user - email not unique", func(t *testing.T) {
FirstOrCreateUser(&User{
@ -451,6 +460,19 @@ func TestUser_Validate(t *testing.T) {
u.UserName = "notunique3"
assert.Error(t, u.Validate())
})
t.Run("primary email empty", func(t *testing.T) {
FirstOrCreateUser(&User{
AddressID: 1,
UserName: "nnomail",
})
u := &User{
AddressID: 1,
UserName: "nomail",
FullName: "No Mail",
PrimaryEmail: "",
}
assert.Nil(t, u.Validate())
})
}
func TestCreateWithPassword(t *testing.T) {
@ -499,3 +521,8 @@ func TestDeleteUser(t *testing.T) {
assert.Error(t, err)
})
}
func TestUser_Deleted(t *testing.T) {
assert.False(t, UserFixtures.Pointer("alice").Deleted())
assert.True(t, UserFixtures.Pointer("deleted").Deleted())
}