Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelleBonnemay authored and mariannebost committed Jul 5, 2024
1 parent 5390f3b commit b7a3acd
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 105 deletions.
8 changes: 1 addition & 7 deletions api/lib/application/passwords/password-controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import * as userSerializer from '../../../src/shared/infrastructure/serializers/jsonapi/user-serializer.js';
import { usecases } from '../../domain/usecases/index.js';

const checkResetDemand = async function (request, h, dependencies = { userSerializer }) {
const temporaryKey = request.params.temporaryKey;
const user = await usecases.getUserByResetPasswordDemand({ temporaryKey });
return dependencies.userSerializer.serialize(user);
};

const updateExpiredPassword = async function (request, h) {
const passwordResetToken = request.payload.data.attributes['password-reset-token'];
Expand All @@ -24,6 +18,6 @@ const updateExpiredPassword = async function (request, h) {
.created();
};

const passwordController = { checkResetDemand, updateExpiredPassword };
const passwordController = { updateExpiredPassword };

export { passwordController };
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { extractLocaleFromRequest } from '../../../shared/infrastructure/utils/request-response-utils.js';
import { usecases } from '../../domain/usecases/index.js';
import * as resetPasswordSerializer from '../../infrastructure/serializers/jsonapi/reset-password.serializer.js';
import * as userSerializer from '../../../shared/infrastructure/serializers/jsonapi/user-serializer.js';


const checkResetDemand = async function (request, h, dependencies = { userSerializer }) {
const temporaryKey = request.params.temporaryKey;
const user = await usecases.getUserByResetPasswordDemand({ temporaryKey });
return dependencies.userSerializer.serialize(user);
};
const createResetPasswordDemand = async function (request, h, dependencies = { resetPasswordSerializer }) {
const { email } = request.payload.data.attributes;
const locale = extractLocaleFromRequest(request);
Expand All @@ -15,4 +22,4 @@ const createResetPasswordDemand = async function (request, h, dependencies = { r
return h.response(serializedPayload).created();
};

export const passwordController = { createResetPasswordDemand };
export const passwordController = { checkResetDemand, createResetPasswordDemand };
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Joi from 'joi';

import { passwordController as libPasswordController } from '../../../../lib/application/passwords/password-controller.js';
import { passwordController } from './password.controller.js';

export const passwordRoutes = [
Expand Down Expand Up @@ -31,7 +30,7 @@ export const passwordRoutes = [
path: '/api/password-reset-demands/{temporaryKey}',
config: {
auth: false,
handler: (request, h) => libPasswordController.checkResetDemand(request, h),
handler: (request, h) => passwordController.checkResetDemand(request, h),
tags: ['api', 'passwords'],
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { UserNotFoundError } from '../../../../../lib/domain/errors.js';
import { PasswordResetDemandNotFoundError, UserNotFoundError } from '../../../../../lib/domain/errors.js';
import { passwordController } from '../../../../../src/identity-access-management/application/password/password.controller.js';
import { catchErr, databaseBuilder, expect, hFake } from '../../../../test-helper.js';
import { identityAccessManagementRoutes } from '../../../../../src/identity-access-management/application/routes.js';
import { usecases } from '../../../../../src/identity-access-management/domain/usecases/index.js';
import { InvalidTemporaryKeyError } from '../../../../../src/shared/domain/errors.js';
import {
catchErr,
databaseBuilder,
domainBuilder,
expect,
hFake,
HttpTestServer,
sinon,
} from '../../../../test-helper.js';

describe('Integration | Identity Access Management | Application | Controller | password', function () {
describe('#createResetPasswordDemand', function () {
Expand Down Expand Up @@ -52,4 +63,72 @@ describe('Integration | Identity Access Management | Application | Controller |
});
});
});

describe('#checkResetDemand', function () {
let routesUnderTest;
let httpTestServer;
let user;
const method = 'GET';
const url = '/api/password-reset-demands/ABCDEF123';
const email = 'user@example.net';

beforeEach(async function () {
sinon.stub(usecases, 'getUserByResetPasswordDemand');
user = domainBuilder.buildUser({ email });
routesUnderTest = identityAccessManagementRoutes[0];
httpTestServer = new HttpTestServer();
await httpTestServer.register(routesUnderTest);
});

context('Success cases', function () {
it('should return an HTTP response with status code 200', async function () {
// given
usecases.getUserByResetPasswordDemand.resolves(user);

// when
const response = await httpTestServer.request(method, url);

// then
expect(response.statusCode).to.equal(200);
expect(response.result.data.type).to.equal('users');
expect(response.result.data.id).to.equal(user.id.toString());
expect(response.result.data.attributes.email).to.equal(email);
});
});

context('Error cases', function () {
it('should respond an HTTP response with status code 401 when InvalidTemporaryKeyError', async function () {
// given
usecases.getUserByResetPasswordDemand.rejects(new InvalidTemporaryKeyError());

// when
const response = await httpTestServer.request(method, url);

// then
expect(response.statusCode).to.equal(401);
});

it('should respond an HTTP response with status code 404 when PasswordResetDemandNotFoundError', async function () {
// given
usecases.getUserByResetPasswordDemand.rejects(new PasswordResetDemandNotFoundError());

// when
const response = await httpTestServer.request(method, url);

// then
expect(response.statusCode).to.equal(404);
});

it('should respond an HTTP response with status code 404 when UserNotFoundError', async function () {
// given
usecases.getUserByResetPasswordDemand.rejects(new UserNotFoundError());

// when
const response = await httpTestServer.request(method, url);

// then
expect(response.statusCode).to.equal(404);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ import { usecases } from '../../../../../src/identity-access-management/domain/u
import { expect, hFake, sinon } from '../../../../test-helper.js';

describe('Unit | Identity Access Management | Application | Controller | password', function () {
describe('#checkResetDemand', function () {
const email = 'user@example.net';
const temporaryKey = 'ABCDEF123';

const request = {
params: { temporaryKey },
};
let dependencies;

beforeEach(function () {
sinon.stub(usecases, 'getUserByResetPasswordDemand');
const userSerializerStub = {
serialize: sinon.stub(),
};
dependencies = {
userSerializer: userSerializerStub,
};
usecases.getUserByResetPasswordDemand.resolves({ email });
});

it('should return serialized user', async function () {
// when
await passwordController.checkResetDemand(request, hFake, dependencies);

// then
expect(usecases.getUserByResetPasswordDemand).to.have.been.calledWithExactly({ temporaryKey });
expect(dependencies.userSerializer.serialize).to.have.been.calledWithExactly({ email });
});
});
describe('#createResetPasswordDemand', function () {
const email = 'user@example.net';
const locale = 'fr';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as moduleUnderTest from '../../../../lib/application/passwords/index.js';
import { PasswordResetDemandNotFoundError, UserNotFoundError } from '../../../../lib/domain/errors.js';
import { UserNotFoundError } from '../../../../lib/domain/errors.js';
import { usecases } from '../../../../lib/domain/usecases/index.js';
import { ForbiddenAccess, InvalidTemporaryKeyError } from '../../../../src/shared/domain/errors.js';
import { domainBuilder, expect, HttpTestServer, sinon } from '../../../test-helper.js';
import { expect, HttpTestServer, sinon } from '../../../test-helper.js';

describe('Integration | Application | Passwords | password-controller', function () {
let httpTestServer;
Expand All @@ -15,67 +15,6 @@ describe('Integration | Application | Passwords | password-controller', function
await httpTestServer.register(moduleUnderTest);
});

describe('#checkResetDemand', function () {
const method = 'GET';
const url = '/api/password-reset-demands/ABCDEF123';

const email = 'user@example.net';

// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line mocha/no-setup-in-describe
const user = domainBuilder.buildUser({ email });

context('Success cases', function () {
it('should return an HTTP response with status code 200', async function () {
// given
usecases.getUserByResetPasswordDemand.resolves(user);

// when
const response = await httpTestServer.request(method, url);

// then
expect(response.statusCode).to.equal(200);
expect(response.result.data.type).to.equal('users');
expect(response.result.data.id).to.equal(user.id.toString());
expect(response.result.data.attributes.email).to.equal(email);
});
});

context('Error cases', function () {
it('should respond an HTTP response with status code 401 when InvalidTemporaryKeyError', async function () {
// given
usecases.getUserByResetPasswordDemand.rejects(new InvalidTemporaryKeyError());

// when
const response = await httpTestServer.request(method, url);

// then
expect(response.statusCode).to.equal(401);
});

it('should respond an HTTP response with status code 404 when PasswordResetDemandNotFoundError', async function () {
// given
usecases.getUserByResetPasswordDemand.rejects(new PasswordResetDemandNotFoundError());

// when
const response = await httpTestServer.request(method, url);

// then
expect(response.statusCode).to.equal(404);
});

it('should respond an HTTP response with status code 404 when UserNotFoundError', async function () {
// given
usecases.getUserByResetPasswordDemand.rejects(new UserNotFoundError());

// when
const response = await httpTestServer.request(method, url);

// then
expect(response.statusCode).to.equal(404);
});
});
});

describe('#updateExpiredPassword', function () {
const method = 'POST';
Expand Down
30 changes: 0 additions & 30 deletions api/tests/unit/application/passwords/password-controller_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,6 @@ import { usecases } from '../../../../lib/domain/usecases/index.js';
import { expect, hFake, sinon } from '../../../test-helper.js';

describe('Unit | Controller | PasswordController', function () {
describe('#checkResetDemand', function () {
const email = 'user@example.net';
const temporaryKey = 'ABCDEF123';

const request = {
params: { temporaryKey },
};
let dependencies;

beforeEach(function () {
sinon.stub(usecases, 'getUserByResetPasswordDemand');
const userSerializerStub = {
serialize: sinon.stub(),
};
dependencies = {
userSerializer: userSerializerStub,
};
usecases.getUserByResetPasswordDemand.resolves({ email });
});

it('should return serialized user', async function () {
// when
await passwordController.checkResetDemand(request, hFake, dependencies);

// then
expect(usecases.getUserByResetPasswordDemand).to.have.been.calledWithExactly({ temporaryKey });
expect(dependencies.userSerializer.serialize).to.have.been.calledWithExactly({ email });
});
});

describe('#updateExpiredPassword', function () {
it('should return 201 http status code', async function () {
// given
Expand Down

0 comments on commit b7a3acd

Please sign in to comment.