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
181 changes: 181 additions & 0 deletions app.mock.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@

const createApp = require('./app');
const request = require('supertest');

jest.mock('./validation/validateEmail', () => {
return jest.fn((email) => {
return email === 'student@example.com';
});
});

const validateUsername = require('./validation/validateUsername');
const validatePassword = require('./validation/validatePassword');
const validateEmail = require('./validation/validateEmail');

const app = createApp(validateUsername, validatePassword, validateEmail);

describe('given correct username and password', () => {
test('return status 200', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
});
expect(response.statusCode).toBe(200);
});

test('returns userId', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
});
expect(response.body.userId).toBeDefined();
});

test('response content type should be 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('response message should be Valid User', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
});
expect(response.body).toEqual({ userId: "1", message: "Valid User" });
});

test('user id value should be valid', 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', () => {
test('return status 400', async () => {
const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
});
expect(response.statusCode).toBe(400);
});

test('response message should be Invalid User', async () => {
const response = await request(app).post('/users').send({
username: 'er',
password: 'Password123',
email: 'student@example.com'
});
expect(response.body).toEqual({ error: "Invalid User" });
});

test('response should not have userId', async () => {
const response = await request(app).post('/users').send({
username: 'er',
password: 'Password123',
email: 'student@example.com'
});
expect(response.body.userId).toBeUndefined();
});

test('username too short', async () => {
const response = await request(app).post('/users').send({
username: 'abc',
password: 'Password1',
email: 'student@example.com'
});
expect(response.body.error).toBe('Invalid User');
});

test('username with invalid characters', async () => {
const response = await request(app).post('/users').send({
username: 'Invalid@Name',
password: 'Password1',
email: 'student@example.com'
});
expect(response.body.error).toBe('Invalid User');
});

test('password too short', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'Pass1',
email: 'student@example.com'
});
expect(response.body.error).toBe('Invalid User');
});

test('password missing uppercase', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'password1',
email: 'student@example.com'
});
expect(response.body.error).toBe('Invalid User');
});

test('password missing lowercase', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'PASSWORD1',
email: 'student@example.com'
});
expect(response.body.error).toBe('Invalid User');
});

test('password contains special characters', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'Password1!',
email: 'student@example.com'
});
expect(response.body.error).toBe('Invalid User');
});

test('email missing @', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'Password1',
email: 'studentexample.com'
});
expect(response.body.error).toBe('Invalid User');
});

test('email invalid domain', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'Password1',
email: 'student@example'
});
expect(response.body.error).toBe('Invalid User');
});

test('password missing', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: '',
email: 'student@example.com'
});
expect(response.body.error).toBe('Invalid User');
});

test('username missing', async () => {
const response = await request(app).post('/users').send({
username: '',
password: 'Password1',
email: 'student@example.com'
});
expect(response.body.error).toBe('Invalid User');
});
});
143 changes: 143 additions & 0 deletions app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const request = require('supertest')
const validateUsername = require('./validation/validateUsername')
const validatePassword = require('./validation/validatePassword')
const validateEmail = require('./validation/validateEmail')
const { response } = require('express')

const app = createApp(validateUsername, validatePassword, validateEmail)

Expand All @@ -26,8 +27,36 @@ describe('given correct username and password', () => {
})

// test response content type?
test('response content type should be 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 response message
test('response message should be Valid User', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body).toEqual({ userId: "1", message: "Valid User" })
})


// test response user id value
test('user id value should be valid', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body.userId).toBe("1")
})
// ...
})

Expand All @@ -42,8 +71,122 @@ describe('given incorrect or missing username and password', () => {
})

// test response message
test('response message should be Invalid User', async () => {
const response = await request(app).post('/users').send({
username: 'er',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body).toEqual({ error: "Invalid User" })
})
// test that response does NOT have userId
test('response should not have userId', async () => {
const response = await request(app).post('/users').send({
username: 'er',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body.userId).toBeUndefined();
})
// test incorrect username or password according to requirements
test('username too short', async () => {
const response = await request(app).post('/users').send({
username: 'abc', // too short (<6)
password: 'Password1',
email: 'student@example.com'
})

expect(response.body.error).toBe('Invalid User')
})

test('username with invalid characters', async () => {
const response = await request(app).post('/users').send({
username: 'Invalid@Name',
password: 'Password1',
email: 'student@example.com'
})

expect(response.body.error).toBe('Invalid User')
})

test('password too short', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'Pass1',
email: 'student@example.com'
})

expect(response.body.error).toBe('Invalid User')
})

test('password missing uppercase', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'password1',
email: 'student@example.com'
})

expect(response.body.error).toBe('Invalid User')
})

test('password missing lowercase', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'PASSWORD1',
email: 'student@example.com'
})

expect(response.body.error).toBe('Invalid User')
})

test('password contains special characters', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'Password1!',
email: 'student@example.com'
})

expect(response.body.error).toBe('Invalid User')
})

test('email missing @', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'Password1',
email: 'studentexample.com'
})

expect(response.body.error).toBe('Invalid User')
})

test('email invalid domain', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: 'Password1',
email: 'student@example'
})

expect(response.body.error).toBe('Invalid User')
})
// test missing username or password
test('password missing', async () => {
const response = await request(app).post('/users').send({
username: 'ValidUsername',
password: '',
email: 'student@example.com'
})

expect(response.body.error).toBe('Invalid User')
})

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

expect(response.body.error).toBe('Invalid User')
})
// ...
})
Loading