Skip to content

Commit

Permalink
⚡️ Added SignUp service
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbelfanti committed Jan 11, 2025
1 parent 403754b commit 69b4062
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 7 deletions.
10 changes: 10 additions & 0 deletions cmd/api/auth/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package auth

import "errors"

var (
FailedToRetrieveIfTheUserExists = errors.New("failed to retrieve user exists")
FailedToSignUpBecauseTheUserAlreadyExists = errors.New("user already exists")
FailedToGenerateHashFromPassword = errors.New("failed to generate hash from password")
FailedToInsertUserIntoDatabase = errors.New("failed to insert user into database")
)
77 changes: 77 additions & 0 deletions cmd/api/auth/signup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package auth_test

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"

"ahbcc/cmd/api/auth"
"ahbcc/cmd/api/users"
)

func TestSignUp_success(t *testing.T) {
mockUserExists := users.MockUserExists(false, nil)
mockInsertUser := users.MockInsert(nil)
mockUserDTO := users.MockUserDTO()

signUp := auth.MakeSignUp(mockUserExists, mockInsertUser)

got := signUp(context.Background(), mockUserDTO)

assert.Nil(t, got)
}

func TestSignUp_failsWhenUserExistsThrowsError(t *testing.T) {
mockUserExists := users.MockUserExists(false, errors.New("failed to execute UserExists"))
mockInsertUser := users.MockInsert(nil)
mockUserDTO := users.MockUserDTO()

signUp := auth.MakeSignUp(mockUserExists, mockInsertUser)

want := auth.FailedToRetrieveIfTheUserExists
got := signUp(context.Background(), mockUserDTO)

assert.Equal(t, want, got)
}

func TestSignUp_failsWhenUserAlreadyExists(t *testing.T) {
mockUserExists := users.MockUserExists(true, nil)
mockInsertUser := users.MockInsert(nil)
mockUserDTO := users.MockUserDTO()

signUp := auth.MakeSignUp(mockUserExists, mockInsertUser)

want := auth.FailedToSignUpBecauseTheUserAlreadyExists
got := signUp(context.Background(), mockUserDTO)

assert.Equal(t, want, got)
}

func TestSignUp_failsWhenGenerateFromPasswordThrowsError(t *testing.T) {
mockUserExists := users.MockUserExists(false, nil)
mockInsertUser := users.MockInsert(nil)
mockUserDTO := users.MockUserDTO()
mockUserDTO.Password = "verylongpassword1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"

signUp := auth.MakeSignUp(mockUserExists, mockInsertUser)

want := auth.FailedToGenerateHashFromPassword
got := signUp(context.Background(), mockUserDTO)

assert.Equal(t, want, got)
}

func TestSignUp_failsWhenInsertThrowsError(t *testing.T) {
mockUserExists := users.MockUserExists(false, nil)
mockInsertUser := users.MockInsert(errors.New("failed to insert user"))
mockUserDTO := users.MockUserDTO()

signUp := auth.MakeSignUp(mockUserExists, mockInsertUser)

want := auth.FailedToInsertUserIntoDatabase
got := signUp(context.Background(), mockUserDTO)

assert.Equal(t, want, got)
}
44 changes: 44 additions & 0 deletions cmd/api/auth/singup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package auth

import (
"context"
"golang.org/x/crypto/bcrypt"

"ahbcc/cmd/api/users"
"ahbcc/internal/log"
)

// SignUp registers a new user in the system
type SignUp func(ctx context.Context, user users.UserDTO) error

// MakeSignUp creates a new SignUp
func MakeSignUp(userExists users.UserExists, insertUser users.Insert) SignUp {
return func(ctx context.Context, user users.UserDTO) error {
exists, err := userExists(ctx, user.Username)
if err != nil {
log.Error(ctx, err.Error())
return FailedToRetrieveIfTheUserExists
}

if exists {
log.Error(ctx, FailedToSignUpBecauseTheUserAlreadyExists.Error())
return FailedToSignUpBecauseTheUserAlreadyExists
}

hash, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
log.Error(ctx, err.Error())
return FailedToGenerateHashFromPassword
}

user.Password = string(hash)

err = insertUser(ctx, user)
if err != nil {
log.Error(ctx, err.Error())
return FailedToInsertUserIntoDatabase
}

return nil
}
}
2 changes: 2 additions & 0 deletions cmd/api/users/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"ahbcc/internal/database"
"ahbcc/internal/log"
)

// Insert inserts a new UserDTO into 'users' table
Expand All @@ -20,6 +21,7 @@ func MakeInsert(db database.Connection) Insert {
return func(ctx context.Context, user UserDTO) error {
_, err := db.Exec(ctx, query, user.Username, user.Password)
if err != nil {
log.Error(ctx, err.Error())
return FailedToInsertUser
}

Expand Down
16 changes: 16 additions & 0 deletions cmd/api/users/mocks.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package users

import "context"

// MockUserExists mocks UserExists function
func MockUserExists(userExists bool, err error) UserExists {
return func(ctx context.Context, username string) (bool, error) {
return userExists, err
}
}

// MockInsert mocks Insert function
func MockInsert(err error) Insert {
return func(ctx context.Context, user UserDTO) error {
return err
}
}

// MockUserDTO mocks UserDTO
func MockUserDTO() UserDTO {
return UserDTO{
Expand Down
7 changes: 0 additions & 7 deletions internal/database/errors.go

This file was deleted.

0 comments on commit 69b4062

Please sign in to comment.