Skip to content

Commit

Permalink
feat(mon-pix): leverage the new errorMessages service
Browse files Browse the repository at this point in the history
  • Loading branch information
lego-technix committed Jan 9, 2025
1 parent 8ab49e6 commit 51eca4c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 58 deletions.
36 changes: 5 additions & 31 deletions mon-pix/app/components/authentication/login-or-register-oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import isEmailValid from '../../utils/email-validator';

const ERROR_INPUT_MESSAGE_MAP = {
termsOfServiceNotSelected: 'pages.login-or-register-oidc.error.error-message',
unknownError: 'common.error',
expiredAuthenticationKey: 'pages.login-or-register-oidc.error.expired-authentication-key',
invalidEmail: 'pages.login-or-register-oidc.error.invalid-email',
accountConflict: 'pages.login-or-register-oidc.error.account-conflict',
loginUnauthorizedError: 'pages.login-or-register-oidc.error.login-unauthorized-error',
};

export default class LoginOrRegisterOidcComponent extends Component {
Expand All @@ -23,6 +19,7 @@ export default class LoginOrRegisterOidcComponent extends Component {
@service oidcIdentityProviders;
@service store;
@service url;
@service errorMessages;

@tracked isTermsOfServiceValidated = false;
@tracked loginErrorMessage = null;
Expand Down Expand Up @@ -70,15 +67,9 @@ export default class LoginOrRegisterOidcComponent extends Component {

try {
await this.args.onLogin({ enteredEmail: this.email, enteredPassword: this.password });
} catch (error) {
const status = get(error, 'errors[0].status');

const errorsMapping = {
401: this.intl.t(ERROR_INPUT_MESSAGE_MAP['expiredAuthenticationKey']),
404: this.intl.t(ERROR_INPUT_MESSAGE_MAP['loginUnauthorizedError']),
409: this.intl.t(ERROR_INPUT_MESSAGE_MAP['accountConflict']),
};
this.loginErrorMessage = errorsMapping[status] || this.intl.t(ERROR_INPUT_MESSAGE_MAP['unknownError']);
} catch (responseError) {
const error = get(responseError, 'errors[0]');
this.loginErrorMessage = this.errorMessages.getErrorMessage(error);
} finally {
this.isLoginLoading = false;
}
Expand All @@ -102,24 +93,7 @@ export default class LoginOrRegisterOidcComponent extends Component {
});
} catch (responseError) {
const error = get(responseError, 'errors[0]');
switch (error?.code) {
case 'INVALID_LOCALE_FORMAT':
this.registerErrorMessage = this.intl.t('pages.sign-up.errors.invalid-locale-format', {
invalidLocale: error.meta.locale,
});
return;
case 'LOCALE_NOT_SUPPORTED':
this.registerErrorMessage = this.intl.t('pages.sign-up.errors.locale-not-supported', {
localeNotSupported: error.meta.locale,
});
return;
}
if (error.status === '401') {
this.registerErrorMessage = this.intl.t(ERROR_INPUT_MESSAGE_MAP['expiredAuthenticationKey']);
} else {
this.registerErrorMessage =
this.intl.t(ERROR_INPUT_MESSAGE_MAP['unknownError']) + (error.detail ? ` (${error.detail})` : '');
}
this.registerErrorMessage = this.errorMessages.getErrorMessage(error);
} finally {
this.isRegisterLoading = false;
}
Expand Down
10 changes: 3 additions & 7 deletions mon-pix/app/components/authentication/oidc-reconciliation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { action } from '@ember/object';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import get from 'lodash/get';

export default class OidcReconciliationComponent extends Component {
@service intl;
@service oidcIdentityProviders;
@service session;
@service errorMessages;

@tracked reconcileErrorMessage = null;
@tracked isLoading = false;
Expand Down Expand Up @@ -49,12 +49,8 @@ export default class OidcReconciliationComponent extends Component {
identityProviderSlug: this.args.identityProviderSlug,
hostSlug: 'user/reconcile',
});
} catch (error) {
const status = get(error, 'errors[0].status');
const errorsMapping = {
401: this.intl.t('pages.login-or-register-oidc.error.expired-authentication-key'),
};
this.reconcileErrorMessage = errorsMapping[status] || this.intl.t('common.error');
} catch (responseError) {
this.reconcileErrorMessage = this.errorMessages.getErrorMessage(responseError);
} finally {
this.isLoading = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module('Unit | Component | authentication | login-or-register-oidc', function (h
const component = createGlimmerComponent('authentication/login-or-register-oidc');

const sessionService = stubSessionService(this.owner, { isAuthenticated: false });
sessionService.authenticate.rejects({ errors: [{ status: '401' }] });
sessionService.authenticate.rejects({ errors: [{ status: '401', code: 'EXPIRED_AUTHENTICATION_KEY' }] });

component.args.identityProviderSlug = 'super-idp';
component.args.authenticationKey = 'super-key';
Expand Down Expand Up @@ -126,23 +126,6 @@ module('Unit | Component | authentication | login-or-register-oidc', function (h
});
});

test('displays an error message with details', async function (assert) {
// given
const component = createGlimmerComponent('authentication/login-or-register-oidc');
const sessionService = stubSessionService(this.owner, { isAuthenticated: false });
sessionService.authenticate.rejects({ errors: [{ status: '500', detail: 'some detail' }] });

component.args.identityProviderSlug = 'super-idp';
component.args.authenticationKey = 'super-key';
component.isTermsOfServiceValidated = true;

// when
await component.register();

// then
assert.strictEqual(component.registerErrorMessage, `${t('common.error')} (some detail)`);
});

test('displays a default error message', async function (assert) {
// given
const component = createGlimmerComponent('authentication/login-or-register-oidc');
Expand Down Expand Up @@ -277,7 +260,9 @@ module('Unit | Component | authentication | login-or-register-oidc', function (h
test('should display error', async function (assert) {
// given
const component = createGlimmerComponent('authentication/login-or-register-oidc');
component.args.onLogin = sinon.stub().rejects({ errors: [{ status: '401' }] });
component.args.onLogin = sinon
.stub()
.rejects({ errors: [{ status: '401', code: 'EXPIRED_AUTHENTICATION_KEY' }] });
component.email = '[email protected]';
component.password = 'pix123';
const eventStub = { preventDefault: sinon.stub() };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ module('Unit | Component | authentication | oidc-reconciliation', function (hook
// given
const component = createGlimmerComponent('authentication/oidc-reconciliation');
const sessionService = stubSessionService(this.owner, { isAuthenticated: false });
sessionService.authenticate.rejects({ errors: [{ status: '401' }] });
sessionService.authenticate.rejects({ errors: [{ status: '401', code: 'EXPIRED_AUTHENTICATION_KEY' }] });

component.args.identityProviderSlug = 'super-idp';
component.args.authenticationKey = 'super-key';
Expand Down

0 comments on commit 51eca4c

Please sign in to comment.