Torjus Håkestad
ce5584ba7e
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package gpaste_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.t-juice.club/torjus/gpaste"
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func RunUserStoreTest(newFunc func() (func(), gpaste.UserStore), t *testing.T) {
|
|
t.Run("Basics", func(t *testing.T) {
|
|
cleanup, s := newFunc()
|
|
t.Cleanup(cleanup)
|
|
|
|
userMap := make(map[string]*gpaste.User)
|
|
passwordMap := make(map[string]string)
|
|
for i := 0; i < 10; i++ {
|
|
username := randomString(8)
|
|
password := randomString(16)
|
|
passwordMap[username] = password
|
|
user := &gpaste.User{
|
|
Username: username,
|
|
Roles: []gpaste.Role{gpaste.RoleAdmin},
|
|
}
|
|
if err := user.SetPassword(password); err != nil {
|
|
t.Fatalf("Error setting password: %s", err)
|
|
}
|
|
userMap[username] = user
|
|
}
|
|
|
|
for _, user := range userMap {
|
|
if err := s.Store(user); err != nil {
|
|
t.Fatalf("Error storing user: %s", err)
|
|
}
|
|
}
|
|
|
|
for k := range userMap {
|
|
user, err := s.Get(k)
|
|
if err != nil {
|
|
t.Errorf("Error getting user: %s", err)
|
|
}
|
|
if err := user.ValidatePassword(passwordMap[user.Username]); err != nil {
|
|
t.Errorf("Error verifying password: %s", err)
|
|
}
|
|
|
|
if !cmp.Equal(user, userMap[k]) {
|
|
t.Errorf("User mismatch: %s", cmp.Diff(user, userMap[k]))
|
|
}
|
|
}
|
|
})
|
|
}
|