|
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