Skip to content

Commit

Permalink
refactor(api): move remove-authentication-method usecase
Browse files Browse the repository at this point in the history
in identity-access-management
  • Loading branch information
er-lim authored Dec 4, 2024
1 parent 1e170ac commit 0f65454
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { usecases as libUsecases } from '../../../../lib/domain/usecases/index.js';
import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js';
import { usecases } from '../../domain/usecases/index.js';
import * as userAnonymizedDetailsForAdminSerializer from '../../infrastructure/serializers/jsonapi/user-anonymized-details-for-admin.serializer.js';
Expand Down Expand Up @@ -93,7 +92,7 @@ const anonymizeUser = async function (request, h, dependencies = { userAnonymize
const removeAuthenticationMethod = async function (request, h) {
const userId = request.params.id;
const authenticationMethodType = request.payload.data.attributes.type;
await libUsecases.removeAuthenticationMethod({ userId, authenticationMethodType });
await usecases.removeAuthenticationMethod({ userId, authenticationMethodType });
return h.response().code(204);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { NON_OIDC_IDENTITY_PROVIDERS } from '../../../src/identity-access-management/domain/constants/identity-providers.js';
import * as OidcIdentityProviders from '../../../src/identity-access-management/domain/constants/oidc-identity-providers.js';
import { UserNotAuthorizedToRemoveAuthenticationMethod } from '../../../src/shared/domain/errors.js';
import { UserNotAuthorizedToRemoveAuthenticationMethod } from '../../../shared/domain/errors.js';
import { NON_OIDC_IDENTITY_PROVIDERS } from '../constants/identity-providers.js';
import * as OidcIdentityProviders from '../constants/oidc-identity-providers.js';

/**
* @param{object} params
* @param{string} params.userId
* @param{string} params.authenticationMethodType
* @param{UserRepository} userRepository
* @param{AuthenticationMethodRepository} authenticationMethodRepository
* @returns {Promise<void>}
* @throws UserNotAuthorizedToRemoveAuthenticationMethod
*/
export const removeAuthenticationMethod = async function ({
userId,
authenticationMethodType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { usecases as libUsecases } from '../../../../../lib/domain/usecases/index.js';
import { userAdminController } from '../../../../../src/identity-access-management/application/user/user.admin.controller.js';
import { QUERY_TYPES } from '../../../../../src/identity-access-management/domain/constants/user-query.js';
import { User } from '../../../../../src/identity-access-management/domain/models/User.js';
Expand Down Expand Up @@ -245,7 +244,7 @@ describe('Unit | Identity Access Management | Application | Controller | Admin |
let removeAuthenticationMethodStub, request;

beforeEach(function () {
removeAuthenticationMethodStub = sinon.stub(libUsecases, 'removeAuthenticationMethod');
removeAuthenticationMethodStub = sinon.stub(usecases, 'removeAuthenticationMethod');
request = {
params: { id: 123 },
payload: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { removeAuthenticationMethod } from '../../../../lib/domain/usecases/remove-authentication-method.js';
import { NON_OIDC_IDENTITY_PROVIDERS } from '../../../../src/identity-access-management/domain/constants/identity-providers.js';
import * as OidcIdentityProviders from '../../../../src/identity-access-management/domain/constants/oidc-identity-providers.js';
import { UserNotAuthorizedToRemoveAuthenticationMethod } from '../../../../src/shared/domain/errors.js';
import { catchErr, domainBuilder, expect, sinon } from '../../../test-helper.js';
import { NON_OIDC_IDENTITY_PROVIDERS } from '../../../../../src/identity-access-management/domain/constants/identity-providers.js';
import * as OidcIdentityProviders from '../../../../../src/identity-access-management/domain/constants/oidc-identity-providers.js';
import { removeAuthenticationMethod } from '../../../../../src/identity-access-management/domain/usecases/remove-authentication-method.usecase.js';
import { UserNotAuthorizedToRemoveAuthenticationMethod } from '../../../../../src/shared/domain/errors.js';
import { catchErr, domainBuilder, expect, sinon } from '../../../../test-helper.js';

describe('Unit | UseCase | remove-authentication-method', function () {
describe('Unit | Identity Access Management | Domain | UseCase | remove-authentication-method', function () {
let userRepository;
let authenticationMethodRepository;

Expand Down Expand Up @@ -47,7 +47,7 @@ describe('Unit | UseCase | remove-authentication-method', function () {
context('When authentication method type is EMAIL', function () {
const authenticationMethodType = 'EMAIL';

it('should set the email to null', async function () {
it('sets the email to null', async function () {
// given
const user = domainBuilder.buildUser();
userRepository.get.resolves(user);
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('Unit | UseCase | remove-authentication-method', function () {
});

context('When user has a username', function () {
it('should not remove PIX authentication method', async function () {
it('does not remove PIX authentication method', async function () {
// given
const user = domainBuilder.buildUser({ username: 'john.doe0101' });
userRepository.get.resolves(user);
Expand All @@ -115,7 +115,7 @@ describe('Unit | UseCase | remove-authentication-method', function () {
context('When authentication method type is USERNAME', function () {
const authenticationMethodType = 'USERNAME';

it('should set the username to null', async function () {
it('sets the username to null', async function () {
// given
const user = domainBuilder.buildUser();
userRepository.get.resolves(user);
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('Unit | UseCase | remove-authentication-method', function () {
});

context('When user has an email', function () {
it('should not remove PIX authentication method', async function () {
it('does not remove PIX authentication method', async function () {
// given
const user = domainBuilder.buildUser({ email: '[email protected]' });
userRepository.get.resolves(user);
Expand Down Expand Up @@ -256,7 +256,7 @@ describe('Unit | UseCase | remove-authentication-method', function () {
});
});
context('When there is only one remaining authentication method', function () {
it('should throw a UserNotAuthorizedToRemoveAuthenticationMethod', async function () {
it('throws a UserNotAuthorizedToRemoveAuthenticationMethod', async function () {
// given
const user = domainBuilder.buildUser();
userRepository.get.resolves(user);
Expand All @@ -277,7 +277,7 @@ describe('Unit | UseCase | remove-authentication-method', function () {
expect(error).to.be.an.instanceOf(UserNotAuthorizedToRemoveAuthenticationMethod);
});

it('should not remove the authentication method', async function () {
it('does not remove the authentication method', async function () {
// given
const user = domainBuilder.buildUser();
userRepository.get.resolves(user);
Expand Down

0 comments on commit 0f65454

Please sign in to comment.