-
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
4c9f2b6
commit ce64a20
Showing
7 changed files
with
143 additions
and
10 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
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,39 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"golang.org/x/crypto/bcrypt" | ||
"time" | ||
|
||
"ahbcc/cmd/api/user" | ||
"ahbcc/cmd/api/user/session" | ||
"ahbcc/internal/log" | ||
) | ||
|
||
// Login logs the user in | ||
type Login func(ctx context.Context, user user.DTO) (string, time.Time, error) | ||
|
||
// MakeLogin creates a new Login function | ||
func MakeLogin(selectUserByUsername user.SelectByUsername, createSessionToken session.CreateToken) Login { | ||
return func(ctx context.Context, user user.DTO) (string, time.Time, error) { | ||
userDAO, err := selectUserByUsername(ctx, user.Username) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
return "", time.Time{}, FailedToSelectUserByUsername | ||
} | ||
|
||
err = bcrypt.CompareHashAndPassword([]byte(userDAO.PasswordHash), []byte(user.Password)) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
return "", time.Time{}, FailedToLoginDueWrongPassword | ||
} | ||
|
||
token, expiresAt, err := createSessionToken(ctx, userDAO.ID) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
return "", time.Time{}, FailedToCreateUserSession | ||
} | ||
|
||
return token, expiresAt, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package auth_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"ahbcc/cmd/api/auth" | ||
"ahbcc/cmd/api/user" | ||
"ahbcc/cmd/api/user/session" | ||
) | ||
|
||
func TestLogin_success(t *testing.T) { | ||
mockUserDAO := user.MockDAO() | ||
mockSelectUserByUsername := user.MockSelectByUsername(mockUserDAO, nil) | ||
mockToken := "abcd" | ||
mockCreatedAt := time.Date(2006, time.January, 1, 0, 0, 0, 0, time.Local) | ||
mockCreateSessionToken := session.MockCreateToken(mockToken, mockCreatedAt, nil) | ||
mockUserDTO := user.MockDTO() | ||
|
||
login := auth.MakeLogin(mockSelectUserByUsername, mockCreateSessionToken) | ||
|
||
token, createdAt, err := login(context.Background(), mockUserDTO) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, mockToken, token) | ||
assert.Equal(t, mockCreatedAt, createdAt) | ||
} | ||
|
||
func TestLogin_failsWhenSelectUserByUsernameThrowsError(t *testing.T) { | ||
mockUserDAO := user.MockDAO() | ||
mockSelectUserByUsername := user.MockSelectByUsername(mockUserDAO, errors.New("error while executing SelectByUsername")) | ||
mockToken := "abcd" | ||
mockCreatedAt := time.Date(2006, time.January, 1, 0, 0, 0, 0, time.Local) | ||
mockCreateSessionToken := session.MockCreateToken(mockToken, mockCreatedAt, nil) | ||
mockUserDTO := user.MockDTO() | ||
|
||
login := auth.MakeLogin(mockSelectUserByUsername, mockCreateSessionToken) | ||
|
||
want := auth.FailedToSelectUserByUsername | ||
_, _, got := login(context.Background(), mockUserDTO) | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func TestLogin_failsWhenCompareHashAndPasswordThrowsError(t *testing.T) { | ||
mockUserDAO := user.MockDAO() | ||
mockSelectUserByUsername := user.MockSelectByUsername(mockUserDAO, nil) | ||
mockCreatedAt := time.Date(2006, time.January, 1, 0, 0, 0, 0, time.Local) | ||
mockCreateSessionToken := session.MockCreateToken("abcd", mockCreatedAt, nil) | ||
mockUserDTO := user.MockDTO() | ||
mockUserDTO.Password = "wrong password" | ||
|
||
login := auth.MakeLogin(mockSelectUserByUsername, mockCreateSessionToken) | ||
|
||
want := auth.FailedToLoginDueWrongPassword | ||
_, _, got := login(context.Background(), mockUserDTO) | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func TestLogin_failsWhenCreateSessionTokenThrowsError(t *testing.T) { | ||
mockUserDAO := user.MockDAO() | ||
mockSelectUserByUsername := user.MockSelectByUsername(mockUserDAO, nil) | ||
mockCreateSessionToken := session.MockCreateToken("abcd", time.Time{}, errors.New("error while executing CreateSessionToken")) | ||
mockUserDTO := user.MockDTO() | ||
|
||
login := auth.MakeLogin(mockSelectUserByUsername, mockCreateSessionToken) | ||
|
||
want := auth.FailedToCreateUserSession | ||
_, _, got := login(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
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 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