Skip to content

Commit

Permalink
test: login
Browse files Browse the repository at this point in the history
  • Loading branch information
tomanagle committed Mar 30, 2024
1 parent 602e420 commit d9f5bed
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
Expand Down
66 changes: 66 additions & 0 deletions internal/handlers/login_test.go
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)

})
}
}
7 changes: 0 additions & 7 deletions internal/handlers/postlogin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handlers

import (
"fmt"
"goth/internal/hash"
"goth/internal/store"
"goth/internal/templates"
Expand Down Expand Up @@ -46,14 +45,8 @@ func (h *PostLoginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

fmt.Println("user.Password", user.Password)

passwordIsValid, err := h.passwordhash.ComparePasswordAndHash(password, user.Password)

fmt.Println("passwordIsValid", passwordIsValid)

fmt.Println("err", err)

if err != nil || !passwordIsValid {
w.WriteHeader(http.StatusUnauthorized)
c := templates.LoginError()
Expand Down
17 changes: 17 additions & 0 deletions internal/hash/mock/mock.go
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)
}
36 changes: 36 additions & 0 deletions internal/store/mock/mock.go
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)
}

0 comments on commit d9f5bed

Please sign in to comment.