gpaste/users/userstore_test.go
Torjus Håkestad 6fdd55def8
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add custom claims
2022-01-20 13:33:11 +01:00

52 lines
1.2 KiB
Go

package users_test
import (
"testing"
"git.t-juice.club/torjus/gpaste/users"
"github.com/google/go-cmp/cmp"
)
func RunUserStoreTest(newFunc func() (func(), users.UserStore), t *testing.T) {
t.Run("Basics", func(t *testing.T) {
cleanup, s := newFunc()
t.Cleanup(cleanup)
userMap := make(map[string]*users.User)
passwordMap := make(map[string]string)
for i := 0; i < 10; i++ {
username := randomString(8)
password := randomString(16)
passwordMap[username] = password
user := &users.User{
Username: username,
Role: users.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]))
}
}
})
}