From 4f1f2298650146dda91ae8f12d451b95b19ee11d Mon Sep 17 00:00:00 2001 From: Rivo Aidla Date: Mon, 27 Apr 2026 21:05:02 +0300 Subject: [PATCH] Implement new tests and fix validation code - Add missing password param check - Add missing username param check - Update app.mock.test.js to match app.test.js tests - Update app.test.js tests to test for business requirements - Clean up comments from test files - Perform coverage tests --- app.mock.test.js | 176 ++++++++++++++++++++++++++++++--- app.test.js | 176 ++++++++++++++++++++++++++++++--- validation/validatePassword.js | 2 + validation/validateUsername.js | 2 + 4 files changed, 328 insertions(+), 28 deletions(-) diff --git a/app.mock.test.js b/app.mock.test.js index 79b9449..e467ce6 100644 --- a/app.mock.test.js +++ b/app.mock.test.js @@ -26,6 +26,24 @@ describe('given correct username and password', () => { expect(response.statusCode).toBe(200) }) + test('return status 200 on email with subdomain', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@mail.example.com' + }) + expect(response.statusCode).toBe(200) + }) + + test('return correct content type', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.headers["content-type"]).toMatch('application/json'); + }) + test('returns userId', async () => { const response = await request(app).post('/users').send({ username: 'Username', @@ -35,25 +53,155 @@ describe('given correct username and password', () => { expect(response.body.userId).toBeDefined(); }) - // test response content type? - // test response message - // test response user id value - // ... + test('returns success message', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(res.body.message).toBe('Valid User') + }) }) describe('given incorrect or missing username and password', () => { - test('return status 400', async () => { - const response = await request(app).post('/users').send({ + test('returns 400 for short username', async () => { + const res = await request(app).post('/users').send({ username: 'user', - password: 'password', - email: 'not-an-email' + password: 'Password123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid char username', async () => { + const res = await request(app).post('/users').send({ + username: 'xXx_U$3RN4M3!', + password: 'Password123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid length username', async () => { + const res = await request(app).post('/users').send({ + username: 'ILoveStressTestingInputfieldsForFun', + password: 'Password123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid length password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'P4rool', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for missing number in password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for special char in password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Passw0rd!', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid UPPERCASE only password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'PASSWORD123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid lowercase only password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'password123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid email', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'wrongmail' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid with no extension email', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid with invalid extension email', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.d' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns error message', async () => { + const res = await request(app).post('/users').send({ + username: 'bad', + password: '123', + email: 'x' + }) + expect(res.body.error).toBe('Invalid User') + }) + + test('does not return userId', async () => { + const res = await request(app).post('/users').send({ + username: 'bad', + password: '123', + email: 'x' + }) + expect(res.body.userId).toBeUndefined() + }) + + test('missing username', async () => { + const res = await request(app).post('/users').send({ + password: 'Password123', + email: 'student@example.com' }) - expect(response.statusCode).toBe(400) + expect(res.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('missing password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('missing email', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123' + }) + expect(res.statusCode).toBe(400) + }) }) \ No newline at end of file diff --git a/app.test.js b/app.test.js index f1b561d..c991c90 100644 --- a/app.test.js +++ b/app.test.js @@ -16,6 +16,24 @@ describe('given correct username and password', () => { expect(response.statusCode).toBe(200) }) + test('return status 200 on email with subdomain', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@mail.example.com' + }) + expect(response.statusCode).toBe(200) + }) + + test('return correct content type', async () => { + const response = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(response.headers["content-type"]).toMatch('application/json'); + }) + test('returns userId', async () => { const response = await request(app).post('/users').send({ username: 'Username', @@ -25,25 +43,155 @@ describe('given correct username and password', () => { expect(response.body.userId).toBeDefined(); }) - // test response content type? - // test response message - // test response user id value - // ... + test('returns success message', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.com' + }) + expect(res.body.message).toBe('Valid User') + }) }) describe('given incorrect or missing username and password', () => { - test('return status 400', async () => { - const response = await request(app).post('/users').send({ + test('returns 400 for short username', async () => { + const res = await request(app).post('/users').send({ username: 'user', - password: 'password', - email: 'not-an-email' + password: 'Password123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid char username', async () => { + const res = await request(app).post('/users').send({ + username: 'xXx_U$3RN4M3!', + password: 'Password123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid length username', async () => { + const res = await request(app).post('/users').send({ + username: 'ILoveStressTestingInputfieldsForFun', + password: 'Password123', + email: 'student@example.com' }) - expect(response.statusCode).toBe(400) + expect(res.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 400 for invalid length password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'P4rool', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for missing number in password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for special char in password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Passw0rd!', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid UPPERCASE only password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'PASSWORD123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid lowercase only password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'password123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid email', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'wrongmail' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid with no extension email', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns 400 for invalid with invalid extension email', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123', + email: 'student@example.d' + }) + expect(res.statusCode).toBe(400) + }) + + test('returns error message', async () => { + const res = await request(app).post('/users').send({ + username: 'bad', + password: '123', + email: 'x' + }) + expect(res.body.error).toBe('Invalid User') + }) + + test('does not return userId', async () => { + const res = await request(app).post('/users').send({ + username: 'bad', + password: '123', + email: 'x' + }) + expect(res.body.userId).toBeUndefined() + }) + + test('missing username', async () => { + const res = await request(app).post('/users').send({ + password: 'Password123', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('missing password', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + email: 'student@example.com' + }) + expect(res.statusCode).toBe(400) + }) + + test('missing email', async () => { + const res = await request(app).post('/users').send({ + username: 'Username', + password: 'Password123' + }) + expect(res.statusCode).toBe(400) + }) }) \ No newline at end of file diff --git a/validation/validatePassword.js b/validation/validatePassword.js index 44d8a8b..e2c213f 100644 --- a/validation/validatePassword.js +++ b/validation/validatePassword.js @@ -1,4 +1,6 @@ function validatePassword(password) { + if(!password || typeof password !== 'string') return false; + const validLength = password.length >= 8; const hasNumber = /[0-9]/g.test(password); const hasUpperCaseLetters = /[A-Z]/g.test(password); diff --git a/validation/validateUsername.js b/validation/validateUsername.js index 30213b1..0553e0b 100644 --- a/validation/validateUsername.js +++ b/validation/validateUsername.js @@ -1,4 +1,6 @@ function validateUsername(username) { + if(!username || typeof username !== 'string') return false; + const validLength = username.length >= 6 && username.length <=30; const allowedcharacters = /^[a-zA-Z0-9.]+$/g.test(username);