gpaste/users/userstore_test.go

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