gpaste/users/userstore_test.go

74 lines
1.6 KiB
Go
Raw Normal View History

2022-01-20 02:35:55 +00:00
package users_test
2022-01-19 20:45:53 +00:00
import (
"testing"
2022-01-20 02:35:55 +00:00
"git.t-juice.club/torjus/gpaste/users"
2022-01-20 00:19:32 +00:00
"github.com/google/go-cmp/cmp"
2022-01-19 20:45:53 +00:00
)
2022-01-20 02:35:55 +00:00
func RunUserStoreTest(newFunc func() (func(), users.UserStore), t *testing.T) {
2022-01-19 20:45:53 +00:00
t.Run("Basics", func(t *testing.T) {
cleanup, s := newFunc()
t.Cleanup(cleanup)
2022-01-20 02:35:55 +00:00
userMap := make(map[string]*users.User)
2022-01-20 00:19:32 +00:00
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-20 02:35:55 +00:00
user := &users.User{
2022-01-20 00:19:32 +00:00
Username: username,
2022-01-20 12:33:11 +00:00
Role: users.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
}
2022-01-21 13:04:41 +00:00
l, err := s.List()
if err != nil {
t.Fatalf("Error listing users: %s", err)
}
if len(l) != len(userMap) {
t.Errorf("List wrong amount of users.")
}
for _, username := range l {
if err := s.Delete(username); err != nil {
t.Fatalf("Error deleting user: %s", err)
}
}
l, err = s.List()
if err != nil {
t.Fatalf("Error listing after delete: %s", err)
}
if len(l) != 0 {
t.Fatalf("List is not empty after deleting all")
}
2022-01-19 20:45:53 +00:00
})
}