Skip to content

Commit

Permalink
Merge 821823f into 435df90
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-vr committed Jan 30, 2019
2 parents 435df90 + 821823f commit 717e86b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 189 deletions.
188 changes: 2 additions & 186 deletions tests/integration/auth.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { errors } from '../../src/config/errors.config';
import * as mailer from '../../src/lib/mailer';
import { getUserSessionToken } from '../_helpers/mockdata/auth.data';
import { clearAll } from '../_helpers/mockdata/data';
import { adminUser, createUser, findById, regularUser, setResetPwToken, unconfirmedUser, createUsers, removeUser }
from '../_helpers/mockdata/user.data';
// tslint:disable-next-line:max-line-length
import { adminUser, createUser, findById, regularUser, setResetPwToken, unconfirmedUser, createUsers, removeUser } from '../_helpers/mockdata/user.data';
import { loginSchema } from '../_helpers/payload-schemes/auth.schema';

describe('/auth', () => {
Expand Down Expand Up @@ -41,39 +41,6 @@ describe('/auth', () => {
expect(header).toHaveProperty('set-cookie');
});

it('Should succesfully login a user with correct credentials case insensitive', async () => {
const { header, status } = await request(app)
.post(`${prefix}/auth/login`)
.send({
email: regularUser.email.toUpperCase(),
password: regularUser.password,
});

expect(status).toEqual(httpStatus.OK);
expect(header).toHaveProperty('set-cookie');
});

it('Should throw error when no email or password is provided', async () => {
const { status } = await request(app)
.post(`${prefix}/auth/login`)
.send({
email: regularUser.email,
});

expect(status).toEqual(httpStatus.BAD_REQUEST);
});

it('Should throw error when invalid email is provided', async () => {
const { status } = await request(app)
.post(`${prefix}/auth/login`)
.send({
email: 'noValidEmail',
password: 'prutser123',
});

expect(status).toEqual(httpStatus.BAD_REQUEST);
});

it('Should throw error when invalid password is provided', async () => {
const { body, status } = await request(app)
.post(`${prefix}/auth/login`)
Expand All @@ -85,57 +52,6 @@ describe('/auth', () => {
expect(body.errors[0].code).toEqual(errors.USER_INVALID_CREDENTIALS.code);
expect(body.errors[0].detail).toEqual(errors.USER_INVALID_CREDENTIALS.message);
});

it('Should throw error when invalid user is provided', async () => {
const { status } = await request(app)
.post(`${prefix}/auth/login`)
.send({
email: 'fakeuser@icapps.com',
password: 'invalidPw',
});
expect(status).toEqual(httpStatus.BAD_REQUEST);
});

it('Should throw error when unknown email is provided', async () => {
const { body, status } = await request(app)
.post(`${prefix}/auth/login`)
.send({
email: 'unknown@test.com',
password: regularUser.password,
});

expect(status).toEqual(httpStatus.BAD_REQUEST);
expect(body.errors[0].code).toEqual(errors.USER_INVALID_CREDENTIALS.code);
expect(body.errors[0].detail).toEqual(errors.USER_INVALID_CREDENTIALS.message);
});

it('Should throw error when user has not yet confirmed his registration', async () => {
const noAccessUser = await createUser(Object.assign({}, unconfirmedUser, { email: 'newuser98@gmail.com' }), 'active');
const { body, status } = await request(app)
.post(`${prefix}/auth/login`)
.send({
email: noAccessUser.email,
password: 'developer',
});

expect(status).toEqual(httpStatus.UNAUTHORIZED);
expect(body.errors[0].code).toEqual(errors.USER_UNCONFIRMED.code);
expect(body.errors[0].title).toEqual(errors.USER_UNCONFIRMED.message);
});

it('Should throw error when user has been set to inactive', async () => {
const noAccessUser = await createUser(Object.assign({}, regularUser, { email: 'newuser12@gmail.com' }), 'inactive');
const { body, status } = await request(app)
.post(`${prefix}/auth/login`)
.send({
email: noAccessUser.email,
password: 'developer',
});

expect(status).toEqual(httpStatus.UNAUTHORIZED);
expect(body.errors[0].code).toEqual(errors.USER_INACTIVE.code);
expect(body.errors[0].title).toEqual(errors.USER_INACTIVE.message);
});
});

describe('POST /login/jwt', () => {
Expand All @@ -154,42 +70,6 @@ describe('/auth', () => {
});
});

it('Should succesfully login a user with correct credentials case insensitive', async () => {
const { body, status } = await request(app)
.post(`${prefix}/auth/login/jwt`)
.send({
email: regularUser.email.toUpperCase(),
password: regularUser.password,
});

expect(status).toEqual(httpStatus.OK);
Joi.validate(body, loginSchema, (err, value) => {
if (err) throw err;
if (!value) throw new Error('no value to check schema');
});
});

it('Should throw error when no email or password is provided', async () => {
const { status } = await request(app)
.post(`${prefix}/auth/login/jwt`)
.send({
email: regularUser.email,
});

expect(status).toEqual(httpStatus.BAD_REQUEST);
});

it('Should throw error when invalid email is provided', async () => {
const { status } = await request(app)
.post(`${prefix}/auth/login/jwt`)
.send({
email: 'noValidEmail',
password: 'prutser123',
});

expect(status).toEqual(httpStatus.BAD_REQUEST);
});

it('Should throw error when invalid password is provided', async () => {
const { body, status } = await request(app)
.post(`${prefix}/auth/login/jwt`)
Expand All @@ -201,57 +81,6 @@ describe('/auth', () => {
expect(body.errors[0].code).toEqual(errors.USER_INVALID_CREDENTIALS.code);
expect(body.errors[0].detail).toEqual(errors.USER_INVALID_CREDENTIALS.message);
});
it('Should throw error when invalid user is provided', async () => {
const { status } = await request(app)
.post(`${prefix}/auth/login/jwt`)
.send({
email: 'fakeuser@icapps.com',
password: 'invalidPw',
});
expect(status).toEqual(httpStatus.BAD_REQUEST);
});

it('Should throw error when unknown email is provided', async () => {
const { body, status } = await request(app)
.post(`${prefix}/auth/login/jwt`)
.send({
email: 'unknown@test.com',
password: regularUser.password,
});

expect(status).toEqual(httpStatus.BAD_REQUEST);
expect(body.errors[0].code).toEqual(errors.USER_INVALID_CREDENTIALS.code);
expect(body.errors[0].detail).toEqual(errors.USER_INVALID_CREDENTIALS.message);
});

it('Should throw error when user has not yet confirmed his registration', async () => {
const noAccessUser = await createUser(Object.assign({}, unconfirmedUser, { email: 'newuser@gmail.com' }), 'active');
const { body, status } = await request(app)
.post(`${prefix}/auth/login/jwt`)
.send({
email: noAccessUser.email,
password: 'developer',
});

expect(status).toEqual(httpStatus.UNAUTHORIZED);
expect(body.errors[0].code).toEqual(errors.USER_UNCONFIRMED.code);
expect(body.errors[0].title).toEqual(errors.USER_UNCONFIRMED.message);
});

it('Should throw error when user has been set to inactive', async () => {
const noAccessUser = await createUser(Object.assign({}, regularUser, { email: 'newuser2@gmail.com' }), 'inactive');
const { body, status } = await request(app)
.post(`${prefix}/auth/login/jwt`)
.send({
email: noAccessUser.email,
password: 'developer',
});

expect(status).toEqual(httpStatus.UNAUTHORIZED);
expect(body.errors[0].code).toEqual(errors.USER_INACTIVE.code);
expect(body.errors[0].title).toEqual(errors.USER_INACTIVE.message);
});

});

describe('POST /login/admin', () => {
Expand Down Expand Up @@ -367,12 +196,6 @@ describe('/auth', () => {
expect(body.errors[0].code).toEqual(errors.LINK_EXPIRED.code);
expect(body.errors[0].detail).toEqual(errors.LINK_EXPIRED.message);
});

it('Should throw an error when no token is provided', async () => {
const { status } = await request(app)
.get(`${prefix}/forgot-password/verify`);
expect(status).toEqual(httpStatus.BAD_REQUEST);
});
});

describe('PUT /forgot-password/confirm?token=', () => {
Expand Down Expand Up @@ -413,12 +236,5 @@ describe('/auth', () => {

expect(status).toEqual(httpStatus.NOT_FOUND);
});

it('Should throw an error when no password is provided', async () => {
const token = await setResetPwToken(users.regular.id);
const { status } = await request(app)
.put(`${prefix}/forgot-password/confirm?token=${token}`);
expect(status).toEqual(httpStatus.BAD_REQUEST);
});
});
});
88 changes: 86 additions & 2 deletions tests/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,98 @@ describe('lib/utils', () => {
});

it('Should throw an error when no headers are present', () => {
expect.assertions(1);
expect.assertions(2);
try {
const mockRequest = httpMocks.createRequest({
headers: {},
});
utils.extractJwt(mockRequest);
} catch (err) {
expect(err).toEqual(new UnauthorizedError(errors.MISSING_HEADERS));
expect(err).toBeInstanceOf(UnauthorizedError);
expect(err.message).toEqual('Not all required headers are provided');
}
});
});

describe('checkStatus', () => {
it('Should not throw any error when the user is ACTIVE and registration confirmed', () => {
const user: User = {
email: 'ben.vanraemdonck@icapps.com',
firstName: 'Ben',
lastName: 'Van Raemdonck',
password: 'secret',
role: {
name: 'name',
code: 'code',
level: 1,
},
status: {
code: 'ACTIVE',
name: 'status',
},
registrationConfirmed: true,
createdAt: '',
createdBy: '',
};

// TODO: check how to test void function
utils.checkStatus(user);

});

it('Should throw an error when user is INACTIVE', () => {
const user: User = {
email: 'ben.vanraemdonck@icapps.com',
firstName: 'Ben',
lastName: 'Van Raemdonck',
password: 'secret',
role: {
name: 'name',
code: 'code',
level: 1,
},
status: {
code: 'INACTIVE',
name: 'status',
},
registrationConfirmed: true,
createdAt: '',
createdBy: '',
};

try {
utils.checkStatus(user);
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toEqual('Your account is inactive. Please contact your administrator.');
}
});

it('Should throw an error when user registration is not confirmed', () => {
const user: User = {
email: 'ben.vanraemdonck@icapps.com',
firstName: 'Ben',
lastName: 'Van Raemdonck',
password: 'secret',
role: {
name: 'name',
code: 'code',
level: 1,
},
status: {
code: 'ACTIVE',
name: 'status',
},
registrationConfirmed: false,
createdAt: '',
createdBy: '',
};

try {
utils.checkStatus(user);
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toEqual('Your account is not confirmed. Please check your inbox for the confirmation link.');
}
});
});
Expand Down

0 comments on commit 717e86b

Please sign in to comment.