From 4d930c0c8cb585979798fac2bb254f991faa62fb Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Mon, 26 Jun 2023 22:26:10 +0200 Subject: feat: Add initial user setup --- pkg/components/auth/controller.go | 44 ++++++++++++++++++++++++++++++---- pkg/components/auth/controller_test.go | 19 ++++++++------- 2 files changed, 49 insertions(+), 14 deletions(-) (limited to 'pkg/components/auth') diff --git a/pkg/components/auth/controller.go b/pkg/components/auth/controller.go index a81a1c0..2f30fb5 100644 --- a/pkg/components/auth/controller.go +++ b/pkg/components/auth/controller.go @@ -5,18 +5,26 @@ import ( "golang.org/x/crypto/bcrypt" + "git.sr.ht/~gabrielgio/img/pkg/components" + "git.sr.ht/~gabrielgio/img/pkg/components/user" "git.sr.ht/~gabrielgio/img/pkg/ext" ) type Controller struct { - repository Repository - key []byte + repository Repository + userRepository user.Repository + key []byte } -func NewController(repository Repository, key []byte) *Controller { +func NewController( + repository Repository, + userRepository user.Repository, + key []byte, +) *Controller { return &Controller{ - repository: repository, - key: key, + repository: repository, + userRepository: userRepository, + key: key, } } @@ -41,3 +49,29 @@ func (c *Controller) Login(ctx context.Context, username, password []byte) ([]by } return ext.WriteToken(token, c.key) } + +// InitialRegister register a initial user, it will validate if there is another +// user stored already. If so an error `InvlidaInput` will be returned +func (c *Controller) InitialRegister(ctx context.Context, username, password []byte, path []byte) error { + exist, err := c.userRepository.Any(ctx) + if err != nil { + return err + } + + if exist { + return components.InvlidaInput + } + + hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) + if err != nil { + return err + } + + err = c.userRepository.Create(ctx, &user.CreateUser{ + Username: string(username), + Password: hash, + Path: string(path), + }) + + return err +} diff --git a/pkg/components/auth/controller_test.go b/pkg/components/auth/controller_test.go index 33aa901..6b4e3cd 100644 --- a/pkg/components/auth/controller_test.go +++ b/pkg/components/auth/controller_test.go @@ -9,6 +9,7 @@ import ( "github.com/samber/lo" + "git.sr.ht/~gabrielgio/img/pkg/components/user" "git.sr.ht/~gabrielgio/img/pkg/ext" "git.sr.ht/~gabrielgio/img/pkg/testkit" ) @@ -43,11 +44,11 @@ func setUp() *scene { return &scene{ ctx: context.Background(), mockRepository: mockUserRepository, - controller: *NewController(mockUserRepository, key), + controller: *NewController(mockUserRepository, nil, key), } } -func TestRegisterAndLogin(t *testing.T) { +func TestInitialRegisterAndLogin(t *testing.T) { testCases := []struct { name string username string @@ -64,7 +65,7 @@ func TestRegisterAndLogin(t *testing.T) { t.Run(tc.name, func(t *testing.T) { scene := setUp() - err := scene.controller.Register(scene.ctx, []byte(tc.username), tc.password) + err := scene.controller.InitialRegister(scene.ctx, []byte(tc.username), tc.password, []byte("/")) testkit.TestFatalError(t, "Register", err) userID := scene.mockRepository.GetLastId() @@ -85,8 +86,8 @@ func TestRegisterAndLogin(t *testing.T) { } } -func toUser(m *mockUser, _ int) *User { - return &User{ +func toUser(m *mockUser, _ int) *user.User { + return &user.User{ ID: m.id, Username: m.username, } @@ -96,7 +97,7 @@ func (m *MockUserRepository) GetLastId() uint { return m.index } -func (m *MockUserRepository) List(ctx context.Context) ([]*User, error) { +func (m *MockUserRepository) List(ctx context.Context) ([]*user.User, error) { if m.err != nil { return nil, m.err } @@ -104,7 +105,7 @@ func (m *MockUserRepository) List(ctx context.Context) ([]*User, error) { return lo.Map(m.users, toUser), nil } -func (m *MockUserRepository) Get(ctx context.Context, id uint) (*User, error) { +func (m *MockUserRepository) Get(ctx context.Context, id uint) (*user.User, error) { if m.err != nil { return nil, m.err } @@ -143,7 +144,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 *CreateUser) (uint, error) { +func (m *MockUserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) { if m.err != nil { return 0, m.err } @@ -159,7 +160,7 @@ func (m *MockUserRepository) Create(ctx context.Context, createUser *CreateUser) return m.index, nil } -func (m *MockUserRepository) Update(ctx context.Context, id uint, update *UpdateUser) error { +func (m *MockUserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error { if m.err != nil { return m.err } -- cgit v1.2.3