-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
403754b
commit 69b4062
Showing
6 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.