Skip to content

Commit

Permalink
[TECH] Nouveau contexte Privacy et API (PIX-14907)
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Nov 20, 2024
2 parents 6fcb3fc + d27368e commit d0df70b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
12 changes: 12 additions & 0 deletions api/src/privacy/application/api/users-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { usecases } from '../../domain/usecases/index.js';

/**
* Determines if a user can self-delete their account.
*
* @param {Object} params - The parameters for the function.
* @param {number} params.userId - The ID of the user.
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the user can self-delete their account.
*/
export const canSelfDeleteAccount = async ({ userId }) => {
return usecases.canSelfDeleteAccount({ userId });
};
19 changes: 19 additions & 0 deletions api/src/privacy/domain/usecases/can-self-delete-account.usecase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { config } from '../../../shared/config.js';

/**
* Determines if a user can self-delete their account.
*
* @param {Object} params - The parameters for the use case.
* @param {number} params.userId - The ID of the user.
* @param {Object} [params.featureToggles] - The feature toggles configuration.
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if self-account deletion is enabled.
*/
const canSelfDeleteAccount = async ({ featureToggles = config.featureToggles }) => {
const { isSelfAccountDeletionEnabled } = featureToggles;

if (!isSelfAccountDeletionEnabled) return false;

return true;
};

export { canSelfDeleteAccount };
17 changes: 17 additions & 0 deletions api/src/privacy/domain/usecases/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

import { injectDependencies } from '../../../shared/infrastructure/utils/dependency-injection.js';
import { importNamedExportsFromDirectory } from '../../../shared/infrastructure/utils/import-named-exports-from-directory.js';

const path = dirname(fileURLToPath(import.meta.url));

const usecasesWithoutInjectedDependencies = {
...(await importNamedExportsFromDirectory({ path: join(path, './'), ignoredFileNames: ['index.js'] })),
};

const dependencies = {};

const usecases = injectDependencies(usecasesWithoutInjectedDependencies, dependencies);

export { usecases };
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { usecases } from '../../../../../src/privacy/domain/usecases/index.js';
import { databaseBuilder, expect } from '../../../../test-helper.js';

describe('Integration | Privacy | Domain | UseCase | can-self-delete-account', function () {
context('Feature flag is disabled', function () {
it('returns false', async function () {
// given
const featureToggles = { isSelfAccountDeletionEnabled: false };
const user = databaseBuilder.factory.buildUser();
await databaseBuilder.commit();

// when
const result = await usecases.canSelfDeleteAccount({ userId: user.id, featureToggles });

// then
expect(result).to.be.false;
});
});

context('Feature flag is enabled', function () {
it('returns true', async function () {
// given
const featureToggles = { isSelfAccountDeletionEnabled: true };
const user = databaseBuilder.factory.buildUser();
await databaseBuilder.commit();

// when
const result = await usecases.canSelfDeleteAccount({ userId: user.id, featureToggles });

// then
expect(result).to.be.true;
});
});
});
21 changes: 21 additions & 0 deletions api/tests/privacy/unit/application/api/users-api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { canSelfDeleteAccount } from '../../../../../src/privacy/application/api/users-api.js';
import { usecases } from '../../../../../src/privacy/domain/usecases/index.js';
import { expect, sinon } from '../../../../test-helper.js';

describe('Unit | Privacy | Application | Api | users', function () {
describe('#canSelfDeleteAccount', function () {
it('indicates if a user can self delete an account', async function () {
// given
const userId = Symbol('123');

sinon.stub(usecases, 'canSelfDeleteAccount');
usecases.canSelfDeleteAccount.withArgs({ userId }).resolves(true);

// when
const result = await canSelfDeleteAccount({ userId });

// then
expect(result).to.equal(true);
});
});
});

0 comments on commit d0df70b

Please sign in to comment.