Auth: Add user and password fixtures #98

This commit is contained in:
Timo Volkmann 2021-08-10 12:30:04 +02:00
parent 6d40f2dc36
commit e5b1b7b5f6
5 changed files with 149 additions and 3 deletions

View file

@ -25,4 +25,6 @@ func CreateTestFixtures() {
CreateFileSyncFixtures()
CreateLensFixtures()
CreateMarkerFixtures()
CreateUserFixtures()
CreatePasswordFixtures()
}

View file

@ -0,0 +1,31 @@
package entity
type PasswordMap map[string]Password
func (m PasswordMap) Get(name string) Password {
if result, ok := m[name]; ok {
return result
}
return Password{}
}
func (m PasswordMap) Pointer(name string) *Password {
if result, ok := m[name]; ok {
return &result
}
return &Password{}
}
var PasswordFixtures = PasswordMap{
"alice": NewPassword("uqxetse3cy5eo9z2", "Alice123!"),
"bob": NewPassword("uqxc08w3d0ej2283", "Bobbob123!"),
}
// CreatePasswordFixtures inserts known entities into the database for testing.
func CreatePasswordFixtures() {
for _, entity := range PasswordFixtures {
Db().Create(&entity)
}
}

View file

@ -68,6 +68,20 @@ func TestFindPassword(t *testing.T) {
r := FindPassword("abc567")
assert.NotEmpty(t, r)
})
t.Run("alice", func(t *testing.T) {
if p := FindPassword("uqxetse3cy5eo9z2"); p == nil {
t.Fatal("password not found")
} else {
assert.False(t, p.InvalidPassword("Alice123!"))
}
})
t.Run("bob", func(t *testing.T) {
if p := FindPassword("uqxc08w3d0ej2283"); p == nil {
t.Fatal("password not found")
} else {
assert.False(t, p.InvalidPassword("Bobbob123!"))
}
})
}
func TestPassword_String(t *testing.T) {

View file

@ -0,0 +1,51 @@
package entity
type UserMap map[string]User
func (m UserMap) Get(name string) User {
if result, ok := m[name]; ok {
return result
}
return User{}
}
func (m UserMap) Pointer(name string) *User {
if result, ok := m[name]; ok {
return &result
}
return &User{}
}
var UserFixtures = UserMap{
"alice": {
ID: 5,
AddressID: 1,
UserUID: "uqxetse3cy5eo9z2",
UserName: "alice",
FullName: "Alice",
RoleAdmin: true,
RoleGuest: false,
UserDisabled: false,
PrimaryEmail: "alice@example.com",
},
"bob": {
ID: 7,
AddressID: 1,
UserUID: "uqxc08w3d0ej2283",
UserName: "bob",
FullName: "Bob",
RoleAdmin: false,
RoleGuest: false,
UserDisabled: false,
PrimaryEmail: "bob@example.com",
},
}
// CreateUserFixtures inserts known entities into the database for testing.
func CreateUserFixtures() {
for _, entity := range UserFixtures {
Db().Create(&entity)
}
}

View file

@ -20,6 +20,47 @@ func TestFindUserByName(t *testing.T) {
assert.NotEmpty(t, m.UserUID)
assert.Equal(t, "admin", m.UserName)
assert.Equal(t, "Admin", m.FullName)
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)
assert.NotEmpty(t, m.CreatedAt)
assert.NotEmpty(t, m.UpdatedAt)
})
@ -53,7 +94,10 @@ func TestUser_InvalidPassword(t *testing.T) {
})
t.Run("no password existing", func(t *testing.T) {
p := User{UserUID: "u000000000000010", UserName: "Hans", FullName: ""}
p.Save()
err := p.Save()
if err != nil {
t.Fatal(err)
}
assert.True(t, p.InvalidPassword("abcdef"))
})
@ -220,8 +264,12 @@ func TestUser_InitPassword(t *testing.T) {
})
t.Run("already existing", func(t *testing.T) {
p := User{UserUID: "u000000000000010", UserName: "Hans", FullName: ""}
p.Save()
p.SetPassword("hutfdt")
if err := p.Save(); err != nil {
t.Fatal(err)
}
if err := p.SetPassword("hutfdt"); err != nil {
t.Fatal(err)
}
assert.NotNil(t, FindPassword("u000000000000010"))
p.InitPassword("hutfdt")
m := FindPassword("u000000000000010")