diff --git a/app.test.js b/app.test.js index f1b561d..64fc38a 100644 --- a/app.test.js +++ b/app.test.js @@ -25,6 +25,17 @@ describe('given correct username and password', () => { expect(response.body.userId).toBeDefined(); }) + test('returns correct message, userId value and content-type', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.body.userId).toBe('1') + expect(response.body.message).toBe('Valid User') + expect(response.headers['content-type']).toMatch(/application\/json/) + }) + // test response content type? // test response message // test response user id value @@ -40,10 +51,72 @@ describe('given incorrect or missing username and password', () => { }) expect(response.statusCode).toBe(400) }) - + test('returns error message and does not include userId', async () => { + const response = await request(app).post('/users').send({ + username: 'user', + password: 'password', + email: 'not-an-email' + }) + expect(response.body.error).toBe('Invalid User') + expect(response.body.userId).toBeUndefined() + expect(response.headers['content-type']).toMatch(/application\/json/) + }) // test response message // test that response does NOT have userId // test incorrect username or password according to requirements // test missing username or password // ... +}) + +describe('edge cases and performance-friendly tests', () => { + test('missing username returns 400 and no userId', async () => { + const validateUsernameMock = (u) => !!u && u.length >= 6 + const validatePasswordMock = (p) => !!p && p.length >= 8 + const validateEmailMock = jest.fn().mockReturnValue(true) + + const appFast = createApp(validateUsernameMock, validatePasswordMock, validateEmailMock) + + const response = await request(appFast).post('/users').send({ + password: 'Password123', + email: 'student@example.com' + }) + + expect(response.statusCode).toBe(400) + expect(response.body.userId).toBeUndefined() + // app validates username, password and email in sequence, so email validator is called + expect(validateEmailMock).toHaveBeenCalled() + }) + + test('missing password returns 400 and no userId', async () => { + const validateUsernameMock = (u) => !!u && u.length >= 6 + const validatePasswordMock = jest.fn().mockReturnValue(false) + const validateEmailMock = jest.fn().mockReturnValue(true) + + const appFast = createApp(validateUsernameMock, validatePasswordMock, validateEmailMock) + + const response = await request(appFast).post('/users').send({ + username: 'Username', + email: 'student@example.com' + }) + + expect(response.statusCode).toBe(400) + expect(response.body.userId).toBeUndefined() + }) + + test('uses mocked email validator so tests stay fast', async () => { + const validateUsernameMock = jest.fn().mockReturnValue(true) + const validatePasswordMock = jest.fn().mockReturnValue(true) + const validateEmailMock = jest.fn().mockReturnValue(true) + + const appFast = createApp(validateUsernameMock, validatePasswordMock, validateEmailMock) + + const response = await request(appFast).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + + expect(response.statusCode).toBe(200) + expect(validateEmailMock).toHaveBeenCalled() + }) }) \ No newline at end of file diff --git a/test-runtime.txt b/test-runtime.txt new file mode 100644 index 0000000..e8e1ed4 --- /dev/null +++ b/test-runtime.txt @@ -0,0 +1,3 @@ +duration_ms:22216 +duration_s:22.22 +exit_code:0