-
Notifications
You must be signed in to change notification settings - Fork 63
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
Showing
6 changed files
with
122 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
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,66 @@ | ||
package handlers | ||
|
||
import ( | ||
"bytes" | ||
|
||
hashmock "goth/internal/hash/mock" | ||
"goth/internal/store" | ||
storemock "goth/internal/store/mock" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogin(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
email string | ||
password string | ||
expectedStatusCode int | ||
getUserResult *store.User | ||
createSessionResult *store.Session | ||
}{ | ||
{ | ||
name: "success", | ||
email: "test@example.com", | ||
password: "password", | ||
getUserResult: &store.User{ID: 1, Email: "test@example.com", Password: "password"}, | ||
createSessionResult: &store.Session{UserID: 1, SessionID: "sessionId"}, | ||
expectedStatusCode: http.StatusOK, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
userStore := &storemock.UserStoreMock{} | ||
sessionStore := &storemock.SessionStoreMock{} | ||
passwordHash := &hashmock.PasswordHashMock{} | ||
|
||
userStore.On("GetUser", tc.email).Return(tc.getUserResult, nil) | ||
|
||
passwordHash.On("ComparePasswordAndHash", tc.password, tc.getUserResult.Password).Return(true, nil) | ||
|
||
sessionStore.On("CreateSession", &store.Session{UserID: tc.getUserResult.ID}).Return(tc.createSessionResult, nil) | ||
|
||
handler := NewPostLoginHandler(PostLoginHandlerParams{ | ||
UserStore: userStore, | ||
SessionStore: sessionStore, | ||
PasswordHash: passwordHash, | ||
SessionCookieName: "session", | ||
}) | ||
body := bytes.NewBufferString("email=" + tc.email + "&password=" + tc.password) | ||
req, _ := http.NewRequest("POST", "/", body) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
rr := httptest.NewRecorder() | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
assert.Equal(tc.expectedStatusCode, rr.Code) | ||
|
||
}) | ||
} | ||
} |
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,17 @@ | ||
package mock | ||
|
||
import "github.com/stretchr/testify/mock" | ||
|
||
type PasswordHashMock struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *PasswordHashMock) ComparePasswordAndHash(password string, encodedHash string) (bool, error) { | ||
args := m.Called(password, encodedHash) | ||
return args.Bool(0), args.Error(1) | ||
} | ||
|
||
func (m *PasswordHashMock) GenerateFromPassword(password string) (string, error) { | ||
args := m.Called(password) | ||
return args.String(0), args.Error(1) | ||
} |
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,36 @@ | ||
package mock | ||
|
||
import ( | ||
"goth/internal/store" | ||
|
||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type UserStoreMock struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *UserStoreMock) CreateUser(email string, password string) error { | ||
args := m.Called(email, password) | ||
|
||
return args.Error(0) | ||
} | ||
|
||
func (m *UserStoreMock) GetUser(email string) (*store.User, error) { | ||
args := m.Called(email) | ||
return args.Get(0).(*store.User), args.Error(1) | ||
} | ||
|
||
type SessionStoreMock struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *SessionStoreMock) CreateSession(session *store.Session) (*store.Session, error) { | ||
args := m.Called(session) | ||
return args.Get(0).(*store.Session), args.Error(1) | ||
} | ||
|
||
func (m *SessionStoreMock) GetUserFromSession(sessionID string) (*store.User, error) { | ||
args := m.Called(sessionID) | ||
return args.Get(0).(*store.User), args.Error(1) | ||
} |