diff --git a/Task1_npx_jest_app_test.PNG b/Task1_npx_jest_app_test.PNG new file mode 100644 index 0000000..33dd0b8 Binary files /dev/null and b/Task1_npx_jest_app_test.PNG differ diff --git a/Task1_npx_test.PNG b/Task1_npx_test.PNG new file mode 100644 index 0000000..56c9f57 Binary files /dev/null and b/Task1_npx_test.PNG differ diff --git a/Task2_npx_mock_test_run.PNG b/Task2_npx_mock_test_run.PNG new file mode 100644 index 0000000..f38d334 Binary files /dev/null and b/Task2_npx_mock_test_run.PNG differ diff --git a/app.mock.test.js b/app.mock.test.js index 79b9449..a5ab9a3 100644 --- a/app.mock.test.js +++ b/app.mock.test.js @@ -1,20 +1,25 @@ +jest.mock('./validation/validateEmail', () => jest.fn()) + const createApp = require('./app') const request = require('supertest') const validateUsername = require('./validation/validateUsername') const validatePassword = require('./validation/validatePassword') +const validateEmail = require('./validation/validateEmail') + +const app = createApp(validateUsername, validatePassword, validateEmail) + +beforeEach(() => { + validateEmail.mockImplementation((email) => { + if (!email || typeof email !== 'string') return false -//Mock validateEmail to isolate tests -jest.mock('./validation/validateEmail', () => { - return jest.fn((email) => { - //Simulate real world simulation - if (!email || typeof email !== 'string') return false; - const re = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/i; - return re.test(email); + const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/i + return emailPattern.test(email) }) }) -const validateEmail = require('./validation/validateEmail') -const app = createApp(validateUsername, validatePassword, validateEmail) +afterEach(() => { + jest.clearAllMocks() +}) describe('given correct username and password', () => { test('return status 200', async () => { @@ -32,13 +37,35 @@ describe('given correct username and password', () => { password: 'Password123', email: 'student@example.com' }) - expect(response.body.userId).toBeDefined(); + expect(response.body.userId).toBeDefined() + }) + + test('returns content type json', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.headers['content-type']).toContain('application/json') }) - // test response content type? - // test response message - // test response user id value - // ... + test('returns success message', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.body.message).toBe('Valid User') + }) + + test('returns user id value 1', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.body.userId).toBe('1') + }) }) describe('given incorrect or missing username and password', () => { @@ -51,9 +78,164 @@ describe('given incorrect or missing username and password', () => { expect(response.statusCode).toBe(400) }) - // test response message - // test that response does NOT have userId - // test incorrect username or password according to requirements - // test missing username or password - // ... + test('returns error message', async () => { + const response = await request(app).post('/users').send({ + username: 'user', + password: 'password', + email: 'not-an-email' + }) + expect(response.body.error).toBe('Invalid User') + }) + + test('does not return userId when user is invalid', async () => { + const response = await request(app).post('/users').send({ + username: 'user', + password: 'password', + email: 'not-an-email' + }) + expect(response.body.userId).toBeUndefined() + }) + + test('returns status 400 when username is too short', async () => { + const response = await request(app).post('/users').send({ + username: 'user1', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when username is too long', async () => { + const response = await request(app).post('/users').send({ + username: 'a'.repeat(31), + password: 'Password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when username contains invalid characters', async () => { + const response = await request(app).post('/users').send({ + username: 'user_name', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is too short', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Pass12', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is missing uppercase letter', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is missing lowercase letter', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'PASSWORD123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is missing number', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password contains special characters', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123!', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email format is invalid', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'not-an-email' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email is missing @ symbol', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'studentexample.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email is missing domain extension', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email has too short extension', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.x' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email is missing text before @', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: '@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when username is empty', async () => { + const response = await request(app).post('/users').send({ + username: '', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is empty', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: '', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email is missing', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123' + }) + expect(response.statusCode).toBe(400) + }) }) \ No newline at end of file diff --git a/app.test.js b/app.test.js index f1b561d..844f9fe 100644 --- a/app.test.js +++ b/app.test.js @@ -22,13 +22,35 @@ describe('given correct username and password', () => { password: 'Password123', email: 'student@example.com' }) - expect(response.body.userId).toBeDefined(); + expect(response.body.userId).toBeDefined() }) - // test response content type? - // test response message - // test response user id value - // ... + test('returns content type json', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.headers['content-type']).toContain('application/json') + }) + + test('returns success message', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.body.message).toBe('Valid User') + }) + + test('returns user id value 1', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.body.userId).toBe('1') + }) }) describe('given incorrect or missing username and password', () => { @@ -41,9 +63,164 @@ describe('given incorrect or missing username and password', () => { expect(response.statusCode).toBe(400) }) - // test response message - // test that response does NOT have userId - // test incorrect username or password according to requirements - // test missing username or password - // ... + test('returns error message', async () => { + const response = await request(app).post('/users').send({ + username: 'user', + password: 'password', + email: 'not-an-email' + }) + expect(response.body.error).toBe('Invalid User') + }) + + test('does not return userId when user is invalid', async () => { + const response = await request(app).post('/users').send({ + username: 'user', + password: 'password', + email: 'not-an-email' + }) + expect(response.body.userId).toBeUndefined() + }) + + test('returns status 400 when username is too short', async () => { + const response = await request(app).post('/users').send({ + username: 'user1', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when username is too long', async () => { + const response = await request(app).post('/users').send({ + username: 'a'.repeat(31), + password: 'Password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when username contains invalid characters', async () => { + const response = await request(app).post('/users').send({ + username: 'user_name', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is too short', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Pass12', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is missing uppercase letter', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is missing lowercase letter', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'PASSWORD123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is missing number', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password contains special characters', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123!', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email format is invalid', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'not-an-email' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email is missing @ symbol', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'studentexample.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email is missing domain extension', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email has too short extension', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.x' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email is missing text before @', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: '@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when username is empty', async () => { + const response = await request(app).post('/users').send({ + username: '', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when password is empty', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: '', + email: 'student@example.com' + }) + expect(response.statusCode).toBe(400) + }) + + test('returns status 400 when email is missing', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123' + }) + expect(response.statusCode).toBe(400) + }) }) \ No newline at end of file