Skip to content

Commit 2f59901

Browse files
authored
[CMS-306] Thomas L - Onboarding (#294)
* Add TestLogin * Add TestLogout Add some helper functions, setUpMockRepositories and newTestUser, to reduce the duplication between TestLogout and its sibling TestLogin.
1 parent e44977f commit 2f59901

File tree

2 files changed

+96
-14
lines changed

2 files changed

+96
-14
lines changed

backend/endpoints/models/user_models.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ func (u *User) IsValidEmail() bool {
3232

3333
// UserExists just checks if a user has a valid password
3434
func (u *User) UserExists(personRepo repositories.PersonRepository) bool {
35-
hashedPassword := u.hashPassword()
35+
hashedPassword := u.HashPassword()
3636

3737
return personRepo.PersonExists(repositories.Person{
3838
Email: u.Email,
3939
Password: hashedPassword,
4040
})
4141
}
4242

43-
// hashPassword hashes a user's password
44-
func (u *User) hashPassword() string {
43+
// HashPassword hashes a user's password
44+
func (u *User) HashPassword() string {
4545
hashedBytes := sha256.Sum256([]byte(u.Password))
4646
return string(hashedBytes[:])
4747
}
Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,93 @@
1-
package tests
2-
3-
import (
4-
"testing"
5-
)
6-
7-
func TestLogin(t *testing.T) {
8-
}
9-
10-
func TestLogout(t *testing.T) {
11-
}
1+
package tests
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/golang/mock/gomock"
9+
"github.com/stretchr/testify/assert"
10+
11+
"cms.csesoc.unsw.edu.au/endpoints"
12+
"cms.csesoc.unsw.edu.au/endpoints/models"
13+
14+
"cms.csesoc.unsw.edu.au/database/repositories"
15+
. "cms.csesoc.unsw.edu.au/database/repositories/mocks"
16+
. "cms.csesoc.unsw.edu.au/endpoints/mocks"
17+
)
18+
19+
const TEST_EMAIL = "[email protected]"
20+
const TEST_PASSWORD = "backendnumberone!"
21+
22+
// Test [endpoints.LoginHandler] succeeds on correct input.
23+
func TestLogin(t *testing.T) {
24+
form := newTestUser()
25+
responseRecorder := httptest.NewRecorder()
26+
request := httptest.NewRequest("POST", "/login", nil)
27+
28+
// Set up mock-test boilerplate.
29+
controller := gomock.NewController(t)
30+
defer controller.Finish()
31+
mockDependencyFactory := setUpMockRepositories(controller)
32+
33+
response := endpoints.LoginHandler(form, responseRecorder, request, mockDependencyFactory)
34+
35+
// The fun happens here!
36+
const statusCode = http.StatusMovedPermanently
37+
assert := assert.New(t)
38+
assert.Equal(statusCode, response.Status)
39+
// The written status should align with the returned status.
40+
assert.Equal(statusCode, responseRecorder.Result().StatusCode)
41+
}
42+
43+
func TestLogout(t *testing.T) {
44+
form := newTestUser()
45+
loginResponseRecorder := httptest.NewRecorder()
46+
loginRequest := httptest.NewRequest("POST", "/login", nil)
47+
48+
// Set up mock-test boilerplate.
49+
controller := gomock.NewController(t)
50+
defer controller.Finish()
51+
mockDependencyFactory := setUpMockRepositories(controller)
52+
53+
// First, we login.
54+
loginResponse := endpoints.LoginHandler(form, loginResponseRecorder, loginRequest, mockDependencyFactory)
55+
// A bit of a hack. Since empty is unexported, it is difficult to get a value of type empty to pass to LogoutHandler.
56+
empty := loginResponse.Response
57+
58+
logoutResponseRecorder := httptest.NewRecorder()
59+
logoutRequest := httptest.NewRequest("POST", "/logout", nil)
60+
61+
// Pass on the cookies from logging in.
62+
cookies := logoutResponseRecorder.Result().Cookies()
63+
for _, cookie := range cookies {
64+
logoutRequest.AddCookie(cookie)
65+
}
66+
67+
logoutResponse := endpoints.LogoutHandler(empty, logoutResponseRecorder, logoutRequest, mockDependencyFactory)
68+
69+
// The fun happens here!
70+
const statusCode = http.StatusOK
71+
assert := assert.New(t)
72+
assert.Equal(statusCode, logoutResponse.Status)
73+
assert.Equal(statusCode, logoutResponseRecorder.Result().StatusCode)
74+
}
75+
76+
// Create a dependency factory that contains the user created by [testUser].
77+
func setUpMockRepositories(controller *gomock.Controller) endpoints.DependencyFactory {
78+
form := newTestUser()
79+
// LoginHandler queries the person repository of the dependency factory it is given for the user details.
80+
mockPersonRepository := NewMockIPersonRepository(controller)
81+
// Fake a repository entry.
82+
person := repositories.Person {Email: form.Email, Password: form.HashPassword()}
83+
mockPersonRepository.EXPECT().PersonExists(person).Return(true)
84+
mockDependencyFactory := NewMockDependencyFactory(controller)
85+
mockDependencyFactory.EXPECT().GetPersonsRepo().Return(mockPersonRepository)
86+
87+
return mockDependencyFactory
88+
}
89+
90+
// Create a [User] with the details in [TEST_EMAIL] and [TEST_PASSWORD].
91+
func newTestUser() models.User {
92+
return models.User {Email: TEST_EMAIL, Password: TEST_PASSWORD}
93+
}

0 commit comments

Comments
 (0)