aboutsummaryrefslogtreecommitdiff
path: root/pkg/components/auth/controller_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/components/auth/controller_test.go')
-rw-r--r--pkg/components/auth/controller_test.go162
1 files changed, 13 insertions, 149 deletions
diff --git a/pkg/components/auth/controller_test.go b/pkg/components/auth/controller_test.go
index 50bf69b..b1ca065 100644
--- a/pkg/components/auth/controller_test.go
+++ b/pkg/components/auth/controller_test.go
@@ -4,12 +4,9 @@ package auth
import (
"context"
- "errors"
"testing"
- "github.com/samber/lo"
-
- "git.sr.ht/~gabrielgio/img/pkg/components/user"
+ "git.sr.ht/~gabrielgio/img/pkg/database/repository"
"git.sr.ht/~gabrielgio/img/pkg/ext"
"git.sr.ht/~gabrielgio/img/pkg/testkit"
)
@@ -17,41 +14,23 @@ import (
type (
scene struct {
ctx context.Context
- mockRepository *MockAuthRepository
- controller Controller
- }
-
- mockUser struct {
- id uint
- username string
- password []byte
- }
-
- MockAuthRepository struct {
- index uint
- users []*mockUser
- err error
- }
-
- MockUserRepository struct {
- index uint
- users []*mockUser
- err error
+ authRepository repository.AuthRepository
+ userRepository repository.UserRepository
+ controller *Controller
}
)
var (
- _ Repository = &MockAuthRepository{}
- key = []byte("6368616e676520746869732070617373")
+ key = []byte("6368616e676520746869732070617373")
)
func setUp() *scene {
- mockAuthRepository := &MockAuthRepository{}
- mockUserRepository := &MockUserRepository{}
+ userRepository := NewUserRepository()
return &scene{
ctx: context.Background(),
- mockRepository: mockAuthRepository,
- controller: *NewController(mockAuthRepository, mockUserRepository, key),
+ authRepository: userRepository,
+ userRepository: userRepository,
+ controller: NewController(userRepository, userRepository, key),
}
}
@@ -64,7 +43,7 @@ func TestInitialRegisterAndLogin(t *testing.T) {
{
name: "Normal register",
username: "username",
- password: []byte("password"),
+ password: []byte("this is an password"),
},
}
@@ -75,9 +54,10 @@ func TestInitialRegisterAndLogin(t *testing.T) {
err := scene.controller.InitialRegister(scene.ctx, []byte(tc.username), tc.password, []byte("/"))
testkit.TestFatalError(t, "Register", err)
- userID := scene.mockRepository.GetLastId()
+ users, err := scene.userRepository.List(scene.ctx)
+ userID := users[0].ID
- user, err := scene.mockRepository.Get(scene.ctx, userID)
+ user, err := scene.userRepository.Get(scene.ctx, userID)
testkit.TestFatalError(t, "Get", err)
testkit.TestValue(t, "Register", tc.username, user.Username)
@@ -93,122 +73,6 @@ func TestInitialRegisterAndLogin(t *testing.T) {
}
}
-func toUser(m *mockUser, _ int) *user.User {
- return &user.User{
- ID: m.id,
- Username: m.username,
- }
-}
-
-func (m *MockAuthRepository) GetLastId() uint {
- return m.index
-}
-
-func (m *MockAuthRepository) List(ctx context.Context) ([]*user.User, error) {
- if m.err != nil {
- return nil, m.err
- }
-
- return lo.Map(m.users, toUser), nil
-}
-
-func (m *MockAuthRepository) Get(ctx context.Context, id uint) (*user.User, error) {
- if m.err != nil {
- return nil, m.err
- }
-
- for _, m := range m.users {
- if m.id == id {
- return toUser(m, 0), nil
- }
- }
- return nil, errors.New("Item not found")
-}
-
-func (m *MockAuthRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) {
- if m.err != nil {
- return 0, m.err
- }
-
- for _, m := range m.users {
- if m.username == username {
- return m.id, nil
- }
- }
- return 0, errors.New("Item not found")
-}
-
-func (m *MockAuthRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) {
- if m.err != nil {
- return nil, m.err
- }
-
- for _, m := range m.users {
- if m.id == id {
- return m.password, nil
- }
- }
- return nil, errors.New("Item not found")
-}
-
-func (m *MockAuthRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) {
- if m.err != nil {
- return 0, m.err
- }
-
- m.index++
-
- m.users = append(m.users, &mockUser{
- id: m.index,
- username: createUser.Username,
- password: createUser.Password,
- })
-
- return m.index, nil
-}
-
-func (m *MockAuthRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error {
- if m.err != nil {
- return m.err
- }
-
- for _, m := range m.users {
- if m.id == id {
- m.username = update.Username
- }
- }
- return nil
-}
-
func remove[T any](slice []T, s int) []T {
return append(slice[:s], slice[s+1:]...)
}
-
-func (r *MockAuthRepository) Delete(ctx context.Context, id uint) error {
- if r.err != nil {
- return r.err
- }
-
- for i, m := range r.users {
- if m.id == id {
- r.users = remove(r.users, i)
- }
- }
- return nil
-}
-
-func (m *MockUserRepository) List(ctx context.Context) ([]*user.User, error) {
- panic("not implemented") // TODO: Implement
-}
-
-func (m *MockUserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) {
- panic("not implemented") // TODO: Implement
-}
-
-func (m *MockUserRepository) Update(ctx context.Context, id uint, updateUser *user.UpdateUser) error {
- panic("not implemented") // TODO: Implement
-}
-
-func (m *MockUserRepository) Any(ctx context.Context) (bool, error) {
- panic("not implemented") // TODO: Implement
-}