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
129 changes: 109 additions & 20 deletions app.mock.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
jest.mock('./validation/validateEmail', () => jest.fn())

const createApp = require('./app')
const request = require('supertest')
const validateUsername = require('./validation/validateUsername')
const validatePassword = require('./validation/validatePassword')

//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 validateEmail = require('./validation/validateEmail')

const app = createApp(validateUsername, validatePassword, validateEmail)

beforeEach(() => {
validateEmail.mockReset()
})

describe('given correct username and password', () => {
test('return status 200', async () => {
validateEmail.mockReturnValue(true)

const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
Expand All @@ -27,6 +25,8 @@ describe('given correct username and password', () => {
})

test('returns userId', async () => {
validateEmail.mockReturnValue(true)

const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
Expand All @@ -35,25 +35,114 @@ 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 correct message', async () => {
validateEmail.mockReturnValue(true)

const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body.message).toBe("Valid User")
})

test('content-type is json', async () => {
validateEmail.mockReturnValue(true)

const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.headers['content-type']).toMatch(/json/)
})

test('userId has correct value', async () => {
validateEmail.mockReturnValue(true)

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', () => {
test('return status 400', async () => {
validateEmail.mockReturnValue(false)

const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
})
expect(response.statusCode).toBe(400)
})

test('returns error message', async () => {
validateEmail.mockReturnValue(false)

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', async () => {
validateEmail.mockReturnValue(false)

const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
})
expect(response.body.userId).toBeUndefined()
})

test('missing username returns 400', async () => {
validateEmail.mockReturnValue(true)

const response = await request(app).post('/users').send({
password: 'Password123',
email: 'student@example.com'
})
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('missing password returns 400', async () => {
validateEmail.mockReturnValue(true)

const response = await request(app).post('/users').send({
username: 'Username',
email: 'student@example.com'
})
expect(response.statusCode).toBe(400)
})

test('missing email returns 400 (coverage for validateEmail)', async () => {
validateEmail.mockReturnValue(false)

const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123'
})

expect(response.statusCode).toBe(400)
})

test('email is not a string returns 400', async () => {
validateEmail.mockReturnValue(false)

const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 12345
})

expect(response.statusCode).toBe(400)
})
})
87 changes: 78 additions & 9 deletions app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,32 @@ 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 correct 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('content-type is json', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.headers['content-type']).toMatch(/json/)
})

test('userId has correct value', 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 @@ -41,9 +63,56 @@ 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', async () => {
const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
})
expect(response.body.userId).toBeUndefined()
})

test('missing username returns 400', async () => {
const response = await request(app).post('/users').send({
password: 'Password123',
email: 'student@example.com'
})
expect(response.statusCode).toBe(400)
})

test('missing password returns 400', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
email: 'student@example.com'
})
expect(response.statusCode).toBe(400)
})

test('missing email returns 400 (coverage for validateEmail)', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123'
})

expect(response.statusCode).toBe(400)
})

test('email is not a string returns 400', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 12345
})

expect(response.statusCode).toBe(400)
})
})
1 change: 1 addition & 0 deletions validation/validatePassword.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function validatePassword(password) {
if (!password) return false;
const validLength = password.length >= 8;
const hasNumber = /[0-9]/g.test(password);
const hasUpperCaseLetters = /[A-Z]/g.test(password);
Expand Down
1 change: 1 addition & 0 deletions validation/validateUsername.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function validateUsername(username) {
if (!username) return false;
const validLength = username.length >= 6 && username.length <=30;
const allowedcharacters = /^[a-zA-Z0-9.]+$/g.test(username);

Expand Down
Loading