gpaste/userstore_test.go
Torjus Håkestad ce5584ba7e
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add role to users
2022-01-20 01:19:32 +01:00

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]))
}
}
})
}