gpaste/userstore_test.go

52 lines
1.2 KiB
Go
Raw Normal View History

2022-01-19 20:45:53 +00:00
package gpaste_test
import (
"testing"
"git.t-juice.club/torjus/gpaste"
2022-01-20 00:19:32 +00:00
"github.com/google/go-cmp/cmp"
2022-01-19 20:45:53 +00:00
)
func RunUserStoreTest(newFunc func() (func(), gpaste.UserStore), t *testing.T) {
t.Run("Basics", func(t *testing.T) {
cleanup, s := newFunc()
t.Cleanup(cleanup)
2022-01-20 00:19:32 +00:00
userMap := make(map[string]*gpaste.User)
passwordMap := make(map[string]string)
2022-01-19 20:45:53 +00:00
for i := 0; i < 10; i++ {
2022-01-20 00:19:32 +00:00
username := randomString(8)
password := randomString(16)
passwordMap[username] = password
2022-01-19 20:45:53 +00:00
user := &gpaste.User{
2022-01-20 00:19:32 +00:00
Username: username,
Roles: []gpaste.Role{gpaste.RoleAdmin},
2022-01-19 20:45:53 +00:00
}
2022-01-20 00:19:32 +00:00
if err := user.SetPassword(password); err != nil {
2022-01-19 20:45:53 +00:00
t.Fatalf("Error setting password: %s", err)
}
2022-01-20 00:19:32 +00:00
userMap[username] = user
}
for _, user := range userMap {
2022-01-19 20:45:53 +00:00
if err := s.Store(user); err != nil {
t.Fatalf("Error storing user: %s", err)
}
}
2022-01-20 00:19:32 +00:00
for k := range userMap {
2022-01-19 20:45:53 +00:00
user, err := s.Get(k)
if err != nil {
t.Errorf("Error getting user: %s", err)
}
2022-01-20 00:19:32 +00:00
if err := user.ValidatePassword(passwordMap[user.Username]); err != nil {
2022-01-19 20:45:53 +00:00
t.Errorf("Error verifying password: %s", err)
}
2022-01-20 00:19:32 +00:00
if !cmp.Equal(user, userMap[k]) {
t.Errorf("User mismatch: %s", cmp.Diff(user, userMap[k]))
}
2022-01-19 20:45:53 +00:00
}
})
}