Skip to content

Commit

Permalink
fix(mon-pix): use request manager to request the password reset demand
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Nov 6, 2024
1 parent 0a19983 commit fecac63
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import PasswordResetDemandReceivedInfo from './password-reset-demand-received-in

export default class PasswordResetDemandForm extends Component {
@service errors;
@service requestManager;

@tracked globalError = this.errors.hasErrors && this.errors.shift();
@tracked isLoading = false;
Expand Down Expand Up @@ -48,22 +49,21 @@ export default class PasswordResetDemandForm extends Component {
this.isPasswordResetDemandReceived = false;

try {
const response = await window.fetch(`${ENV.APP.API_HOST}/api/password-reset-demands`, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
const { response } = await this.requestManager.request({
url: `${ENV.APP.API_HOST}/api/password-reset-demands`,
method: 'POST',
body: JSON.stringify({ email }),
body: { email },
});

if (!response.ok && response.status != 404) {
throw new Error(`Response status: ${response.status}`);
}
if (!response.ok) throw new Error(`Response status: ${response.status}`);

this.isPasswordResetDemandReceived = true;
} catch (error) {
this.globalError = 'common.api-error-messages.internal-server-error';
if (error.status === 404) {
this.isPasswordResetDemandReceived = true;
} else {
this.globalError = 'common.api-error-messages.internal-server-error';
}
} finally {
this.isLoading = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ const I18N_KEYS = {
module('Integration | Component | Authentication | PasswordResetDemand | password-reset-demand-form', function (hooks) {
setupIntlRenderingTest(hooks);

let requestManagerService;

hooks.beforeEach(function () {
requestManagerService = this.owner.lookup('service:requestManager');
sinon.stub(requestManagerService, 'request');
});

test('it displays all elements of component successfully', async function (assert) {
// given
const screen = await render(<template><PasswordResetDemandForm /></template>);
Expand Down Expand Up @@ -78,11 +85,7 @@ module('Integration | Component | Authentication | PasswordResetDemand | passwor
module('when the password-reset-demand is successful', function () {
test('it displays a "password reset demand received" info (without any error message)', async function (assert) {
// given
window.fetch.resolves(
fetchMock({
status: 201,
}),
);
requestManagerService.request.resolves({ response: { ok: true, status: 201 } });

const email = '[email protected]';
const locale = ENGLISH_INTERNATIONAL_LOCALE;
Expand Down Expand Up @@ -127,14 +130,7 @@ module('Integration | Component | Authentication | PasswordResetDemand | passwor
module('when there is no corresponding user account', function () {
test('it displays a "password reset demand received" info (without any error message to avoid email enumeration)', async function (assert) {
// given
window.fetch.resolves(
fetchMock({
status: 404,
body: {
errors: [{ title: 'Not Found' }],
},
}),
);
requestManagerService.request.rejects({ status: 404 });

const email = '[email protected]';
const screen = await render(<template><PasswordResetDemandForm /></template>);
Expand All @@ -157,11 +153,7 @@ module('Integration | Component | Authentication | PasswordResetDemand | passwor
module('when there is an unknown error', function () {
test('it displays an "unknown error" error message', async function (assert) {
// given
window.fetch.resolves(
fetchMock({
status: 500,
}),
);
requestManagerService.request.rejects({ status: 500 });

const email = '[email protected]';
const screen = await render(<template><PasswordResetDemandForm /></template>);
Expand Down Expand Up @@ -195,12 +187,3 @@ module('Integration | Component | Authentication | PasswordResetDemand | passwor
});
});
});

function fetchMock({ body, status }) {
return new window.Response(JSON.stringify(body), {
status,
headers: {
'Content-type': 'application/json',
},
});
}

0 comments on commit fecac63

Please sign in to comment.