Skip to content

Commit

Permalink
Merge pull request #21 from icapps/bugfix/SIAP-127
Browse files Browse the repository at this point in the history
bugfix/SIAP-127
  • Loading branch information
knor-el-snor authored Apr 26, 2018
2 parents 43cfc1e + a1b3491 commit 56fe4d0
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions db/migrations/20180320162959_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports.up = async (knex) => {
table.text('lastName').notNullable();
table.text('email').notNullable();
table.text('role').notNullable();
table.boolean('registrationCompleted').defaultTo(false);

// Nullable
table.text('password').nullable();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"redis": "~2.8.0",
"tree-house": "~3.2.0",
"tree-house-authentication": "~2.0.5",
"tree-house-errors": "^1.0.3",
"tree-house-errors": "~1.0.3",
"uuid": "~3.2.1",
"winston": "~2.4.2"
},
Expand Down
2 changes: 2 additions & 0 deletions src/models/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface User {
lastName: string;
password: string;
hasAccess: boolean;
registrationCompleted: boolean;
role: string; // Code of role
refreshToken?: string;
resetPwToken?: string;
Expand Down Expand Up @@ -35,6 +36,7 @@ export interface PartialUserUpdate {
firstName?: string;
lastName?: string;
hasAccess?: boolean;
registrationCompleted?: boolean;
role?: string; // Code of role
password?: string;
resetPwToken?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { tableNames, defaultFilters } from '../constants';
import { User, UserUpdate, UserCreate, PartialUserUpdate } from '../models/user.model';
import { getHashedPassword } from 'tree-house-authentication';
const defaultReturnValues = ['id', 'email', 'password', 'firstName', 'lastName',
'hasAccess', 'role', 'refreshToken', 'resetPwToken', 'createdAt', 'updatedAt'];
'hasAccess', 'registrationCompleted', 'role', 'refreshToken', 'resetPwToken', 'createdAt', 'updatedAt'];

/**
* Create new user
Expand Down
1 change: 1 addition & 0 deletions src/serializers/user.serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const userSerializer = new Serializer('users', {
'firstName',
'lastName',
'hasAccess',
'registrationCompleted',
'role',
'createdAt',
'updatedAt',
Expand Down
2 changes: 1 addition & 1 deletion src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function partialUpdate(userId: string, values: PartialUserUpdate):
export async function updatePassword(userId: string, password: string): Promise<{}> {
try {
const hashedPw = await getHashedPassword(password, settings.saltCount);
return await partialUpdate(userId, { password: hashedPw });
return await partialUpdate(userId, { password: hashedPw, registrationCompleted: true });
} catch (error) {
logger.error(`An error occured updating a user's password: ${error}`);
throw error;
Expand Down
1 change: 1 addition & 0 deletions tests/_helpers/payload-schemes/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const userSchema = Joi.object({
firstName: Joi.string().required(),
lastName: Joi.string().required(),
hasAccess: Joi.boolean().required(),
registrationCompleted: Joi.boolean().required(),
role: Joi.string().required().valid(roleNames),
createdAt: Joi.date().iso().raw(),
updatedAt: Joi.date().iso().raw(),
Expand Down
23 changes: 22 additions & 1 deletion tests/integration/user.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ describe('/users', () => {
if (err) throw err;
if (!value) throw new Error('no value to check schema');
});

const createdUser = await findById(body.data.id);
expect(createdUser.resetPwToken).toEqual(expect.any(String));
expect(createdUser.registrationCompleted).toEqual(false);
});


Expand Down Expand Up @@ -441,6 +441,25 @@ describe('/users', () => {
expect(body.errors[0].title).toEqual(errors.INVALID_INPUT.message);
});

it('Should throw an error when trying to manually update registrationCompleted status', async () => {
const { body, status } = await request(app)
.put(`${prefix}/users/${user.id}`)
.set('Authorization', `Bearer ${adminToken}`)
.send({
email: 'test@unknown2.com',
firstName: 'Test',
lastName: 'Unknown',
hasAccess: false,
role: roles.ADMIN.code,
registrationCompleted: true,
});

expect(status).toEqual(httpStatus.BAD_REQUEST);
expect(body.errors[0].code).toEqual(errors.INVALID_INPUT.code);
expect(body.errors[0].title).toEqual(errors.INVALID_INPUT.message);
expect(body.errors[0].detail[0].messages[0]).toMatch(/"registrationCompleted" is not allowed/);
});

it('Should throw an error when user has no admin rights', async () => {
const { body, status } = await request(app)
.put(`${prefix}/users/${user.id}`)
Expand Down Expand Up @@ -478,6 +497,8 @@ describe('/users', () => {
password: 'myNewPw',
});
expect(status).toEqual(httpStatus.OK);
const updatedUser = await findById(user.id);
expect(updatedUser.registrationCompleted).toEqual(true);

const { status: status2 } = await request(app)
.post(`${prefix}/auth/login`)
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5681,7 +5681,7 @@ tree-house-authentication@~2.0.5:
bcrypt "~2.0.0"
jsonwebtoken "~8.2.0"

tree-house-errors@^1.0.3:
tree-house-errors@~1.0.3:
version "1.0.3"
resolved "https://registry.npmjs.org/tree-house-errors/-/tree-house-errors-1.0.3.tgz#976438f1435a1a11d2d5a9ceb506db4d467bd577"
dependencies:
Expand Down

0 comments on commit 56fe4d0

Please sign in to comment.