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
176 changes: 162 additions & 14 deletions app.mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
})
})
176 changes: 162 additions & 14 deletions app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
})
})
2 changes: 2 additions & 0 deletions validation/validatePassword.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Loading
Loading