diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/components/auth/controller.go | 2 | ||||
-rw-r--r-- | pkg/components/auth/controller_test.go | 47 | ||||
-rw-r--r-- | pkg/components/user/model.go | 2 | ||||
-rw-r--r-- | pkg/database/sql/user.go | 6 | ||||
-rw-r--r-- | pkg/database/sql/user_test.go | 12 | ||||
-rw-r--r-- | pkg/ext/middleware.go | 3 | ||||
-rw-r--r-- | pkg/worker/file_scanner.go | 4 |
7 files changed, 48 insertions, 28 deletions
diff --git a/pkg/components/auth/controller.go b/pkg/components/auth/controller.go index 2f30fb5..a33d9b3 100644 --- a/pkg/components/auth/controller.go +++ b/pkg/components/auth/controller.go @@ -67,7 +67,7 @@ func (c *Controller) InitialRegister(ctx context.Context, username, password []b return err } - err = c.userRepository.Create(ctx, &user.CreateUser{ + _, err = c.userRepository.Create(ctx, &user.CreateUser{ Username: string(username), Password: hash, Path: string(path), diff --git a/pkg/components/auth/controller_test.go b/pkg/components/auth/controller_test.go index 6b4e3cd..50bf69b 100644 --- a/pkg/components/auth/controller_test.go +++ b/pkg/components/auth/controller_test.go @@ -17,7 +17,7 @@ import ( type ( scene struct { ctx context.Context - mockRepository *MockUserRepository + mockRepository *MockAuthRepository controller Controller } @@ -27,6 +27,12 @@ type ( password []byte } + MockAuthRepository struct { + index uint + users []*mockUser + err error + } + MockUserRepository struct { index uint users []*mockUser @@ -35,16 +41,17 @@ type ( ) var ( - _ Repository = &MockUserRepository{} + _ Repository = &MockAuthRepository{} key = []byte("6368616e676520746869732070617373") ) func setUp() *scene { + mockAuthRepository := &MockAuthRepository{} mockUserRepository := &MockUserRepository{} return &scene{ ctx: context.Background(), - mockRepository: mockUserRepository, - controller: *NewController(mockUserRepository, nil, key), + mockRepository: mockAuthRepository, + controller: *NewController(mockAuthRepository, mockUserRepository, key), } } @@ -93,11 +100,11 @@ func toUser(m *mockUser, _ int) *user.User { } } -func (m *MockUserRepository) GetLastId() uint { +func (m *MockAuthRepository) GetLastId() uint { return m.index } -func (m *MockUserRepository) List(ctx context.Context) ([]*user.User, error) { +func (m *MockAuthRepository) List(ctx context.Context) ([]*user.User, error) { if m.err != nil { return nil, m.err } @@ -105,7 +112,7 @@ func (m *MockUserRepository) List(ctx context.Context) ([]*user.User, error) { return lo.Map(m.users, toUser), nil } -func (m *MockUserRepository) Get(ctx context.Context, id uint) (*user.User, error) { +func (m *MockAuthRepository) Get(ctx context.Context, id uint) (*user.User, error) { if m.err != nil { return nil, m.err } @@ -118,7 +125,7 @@ func (m *MockUserRepository) Get(ctx context.Context, id uint) (*user.User, erro return nil, errors.New("Item not found") } -func (m *MockUserRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) { +func (m *MockAuthRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) { if m.err != nil { return 0, m.err } @@ -131,7 +138,7 @@ func (m *MockUserRepository) GetIDByUsername(ctx context.Context, username strin return 0, errors.New("Item not found") } -func (m *MockUserRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) { +func (m *MockAuthRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) { if m.err != nil { return nil, m.err } @@ -144,7 +151,7 @@ func (m *MockUserRepository) GetPassword(ctx context.Context, id uint) ([]byte, return nil, errors.New("Item not found") } -func (m *MockUserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) { +func (m *MockAuthRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) { if m.err != nil { return 0, m.err } @@ -160,7 +167,7 @@ func (m *MockUserRepository) Create(ctx context.Context, createUser *user.Create return m.index, nil } -func (m *MockUserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error { +func (m *MockAuthRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error { if m.err != nil { return m.err } @@ -177,7 +184,7 @@ func remove[T any](slice []T, s int) []T { return append(slice[:s], slice[s+1:]...) } -func (r *MockUserRepository) Delete(ctx context.Context, id uint) error { +func (r *MockAuthRepository) Delete(ctx context.Context, id uint) error { if r.err != nil { return r.err } @@ -189,3 +196,19 @@ func (r *MockUserRepository) Delete(ctx context.Context, id uint) error { } 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 +} diff --git a/pkg/components/user/model.go b/pkg/components/user/model.go index ce1b3a5..0ff6d0a 100644 --- a/pkg/components/user/model.go +++ b/pkg/components/user/model.go @@ -27,7 +27,7 @@ type ( Repository interface { List(ctx context.Context) ([]*User, error) - Create(ctx context.Context, createUser *CreateUser) error + Create(ctx context.Context, createUser *CreateUser) (uint, error) Update(ctx context.Context, id uint, updateUser *UpdateUser) error Any(ctx context.Context) (bool, error) } diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go index a02b67b..a0884f4 100644 --- a/pkg/database/sql/user.go +++ b/pkg/database/sql/user.go @@ -137,7 +137,7 @@ func (self *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, e return userPassword.Password, nil } -func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) error { +func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) { user := &User{ Username: createUser.Username, Name: createUser.Name, @@ -148,10 +148,10 @@ func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateU WithContext(ctx). Create(user) if result.Error != nil { - return result.Error + return 0, result.Error } - return nil + return user.Model.ID, nil } func (self *UserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error { diff --git a/pkg/database/sql/user_test.go b/pkg/database/sql/user_test.go index 473ce03..f0d89ad 100644 --- a/pkg/database/sql/user_test.go +++ b/pkg/database/sql/user_test.go @@ -48,7 +48,7 @@ func TestCreate(t *testing.T) { repository := NewUserRepository(db) - err := repository.Create(context.Background(), &user.CreateUser{ + id, err := repository.Create(context.Background(), &user.CreateUser{ Username: "new_username", Name: "new_name", }) @@ -56,12 +56,12 @@ func TestCreate(t *testing.T) { t.Fatalf("Error creating: %s", err.Error()) } - got, err := repository.Get(context.Background(), 1) + got, err := repository.Get(context.Background(), id) if err != nil { t.Fatalf("Error getting: %s", err.Error()) } want := &user.User{ - ID: 1, + ID: id, Username: "new_username", Name: "new_name", } @@ -78,7 +78,7 @@ func TestUpdate(t *testing.T) { repository := NewUserRepository(db) - err := repository.Create(context.Background(), &user.CreateUser{ + id, err := repository.Create(context.Background(), &user.CreateUser{ Username: "username", Name: "name", }) @@ -86,7 +86,7 @@ func TestUpdate(t *testing.T) { t.Fatalf("Error creating user: %s", err.Error()) } - err = repository.Update(context.Background(), 1, &user.UpdateUser{ + err = repository.Update(context.Background(), id, &user.UpdateUser{ Username: "new_username", Name: "new_name", }) @@ -99,7 +99,7 @@ func TestUpdate(t *testing.T) { t.Fatalf("Error getting user: %s", err.Error()) } want := &user.User{ - ID: 1, + ID: id, Username: "new_username", Name: "new_name", } diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go index 649272e..bc23b90 100644 --- a/pkg/ext/middleware.go +++ b/pkg/ext/middleware.go @@ -4,9 +4,10 @@ import ( "encoding/base64" "time" - "git.sr.ht/~gabrielgio/img/pkg/components/user" "github.com/sirupsen/logrus" "github.com/valyala/fasthttp" + + "git.sr.ht/~gabrielgio/img/pkg/components/user" ) func HTML(next fasthttp.RequestHandler) fasthttp.RequestHandler { diff --git a/pkg/worker/file_scanner.go b/pkg/worker/file_scanner.go index fda869c..a51f60b 100644 --- a/pkg/worker/file_scanner.go +++ b/pkg/worker/file_scanner.go @@ -77,10 +77,6 @@ func (f *FileScanner) Process(ctx context.Context, path string) error { return nil } - if errResp != nil { - return errResp - } - return f.repository.Create(ctx, &media.CreateMedia{ Name: name, Path: path, |