From 0f6545428887afd6118811bdf072354f6b9da524 Mon Sep 17 00:00:00 2001 From: Eric Lim Date: Tue, 3 Dec 2024 14:49:03 +0100 Subject: [PATCH] refactor(api): move remove-authentication-method usecase in identity-access-management --- .../application/user/user.admin.controller.js | 3 +-- .../remove-authentication-method.usecase.js} | 15 +++++++++--- .../user/user.admin.controller.test.js | 3 +-- ...ove-authentication-method.usecase.test.js} | 24 +++++++++---------- 4 files changed, 26 insertions(+), 19 deletions(-) rename api/{lib/domain/usecases/remove-authentication-method.js => src/identity-access-management/domain/usecases/remove-authentication-method.usecase.js} (77%) rename api/tests/{unit/domain/usecases/remove-authentication-method_test.js => identity-access-management/unit/domain/usecases/remove-authentication-method.usecase.test.js} (91%) diff --git a/api/src/identity-access-management/application/user/user.admin.controller.js b/api/src/identity-access-management/application/user/user.admin.controller.js index ae8e46f707c..e8b750e66c7 100644 --- a/api/src/identity-access-management/application/user/user.admin.controller.js +++ b/api/src/identity-access-management/application/user/user.admin.controller.js @@ -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'; @@ -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); }; diff --git a/api/lib/domain/usecases/remove-authentication-method.js b/api/src/identity-access-management/domain/usecases/remove-authentication-method.usecase.js similarity index 77% rename from api/lib/domain/usecases/remove-authentication-method.js rename to api/src/identity-access-management/domain/usecases/remove-authentication-method.usecase.js index 5e5f02f343b..0d3446bb49d 100644 --- a/api/lib/domain/usecases/remove-authentication-method.js +++ b/api/src/identity-access-management/domain/usecases/remove-authentication-method.usecase.js @@ -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} + * @throws UserNotAuthorizedToRemoveAuthenticationMethod + */ export const removeAuthenticationMethod = async function ({ userId, authenticationMethodType, diff --git a/api/tests/identity-access-management/unit/application/user/user.admin.controller.test.js b/api/tests/identity-access-management/unit/application/user/user.admin.controller.test.js index e83caea7ce9..3e8d24dd79c 100644 --- a/api/tests/identity-access-management/unit/application/user/user.admin.controller.test.js +++ b/api/tests/identity-access-management/unit/application/user/user.admin.controller.test.js @@ -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'; @@ -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: { diff --git a/api/tests/unit/domain/usecases/remove-authentication-method_test.js b/api/tests/identity-access-management/unit/domain/usecases/remove-authentication-method.usecase.test.js similarity index 91% rename from api/tests/unit/domain/usecases/remove-authentication-method_test.js rename to api/tests/identity-access-management/unit/domain/usecases/remove-authentication-method.usecase.test.js index 7d765c81835..499151e819a 100644 --- a/api/tests/unit/domain/usecases/remove-authentication-method_test.js +++ b/api/tests/identity-access-management/unit/domain/usecases/remove-authentication-method.usecase.test.js @@ -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; @@ -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); @@ -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); @@ -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); @@ -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: 'john.doe@example.net' }); userRepository.get.resolves(user); @@ -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); @@ -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);