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