Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Task1_npx_jest_app_test.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Task1_npx_test.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Task2_npx_mock_test_run.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
220 changes: 201 additions & 19 deletions app.mock.test.js
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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', () => {
Expand All @@ -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)
})
})
Loading
Loading