Skip to content

Commit

Permalink
feat(api): created feature by default in helper and used it in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alicegoarnisson committed Mar 7, 2025
1 parent 6113a81 commit 1181c9e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
1 change: 1 addition & 0 deletions api/lib/domain/usecases/create-organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const createOrganization = async function ({
}) {
organizationCreationValidator.validate(organization);
const savedOrganization = await organizationForAdminRepository.save(organization);

await dataProtectionOfficerRepository.create({
organizationId: savedOrganization.id,
firstName: organization.dataProtectionOfficer.firstName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { OrganizationForAdmin } from '../../../../src/organizational-entities/do
import * as dataProtectionOfficerRepository from '../../../../src/organizational-entities/infrastructure/repositories/data-protection-officer.repository.js';
import { organizationForAdminRepository } from '../../../../src/organizational-entities/infrastructure/repositories/organization-for-admin.repository.js';
import * as schoolRepository from '../../../../src/school/infrastructure/repositories/school-repository.js';
import { databaseBuilder, expect } from '../../../test-helper.js';
import { databaseBuilder, expect, insertMultipleSendingFeatureForNewOrganization } from '../../../test-helper.js';

describe('Integration | UseCases | create-organization', function () {
it('returns newly created organization', async function () {
// given
const superAdminUserId = databaseBuilder.factory.buildUser().id;
await insertMultipleSendingFeatureForNewOrganization();
await databaseBuilder.commit();

const organization = new OrganizationForAdmin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('Integration | UseCases | create-organizations-with-tags-and-target-pro

beforeEach(async function () {
databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY);
await insertMultipleSendingFeatureForNewOrganization();
missionFeature = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MISSIONS_MANAGEMENT);
oralizationFeature = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.ORALIZATION_MANAGED_BY_PRESCRIBER);
importStudentsFeature = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.LEARNER_IMPORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ describe('Acceptance | Organizational Entities | Application | Route | Admin | O
});
const tag = databaseBuilder.factory.buildTag({ id: 7, name: 'AEFE' });
databaseBuilder.factory.buildOrganizationTag({ tagId: tag.id, organizationId: organization.id });
insertMultipleSendingFeatureForNewOrganization();
await databaseBuilder.commit();

// when
Expand Down Expand Up @@ -106,7 +105,7 @@ describe('Acceptance | Organizational Entities | Application | Route | Admin | O
'creator-full-name': 'Tom Dereck',
'identity-provider-for-campaigns': null,
features: {
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: true, params: null },
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: false, params: null },
[ORGANIZATION_FEATURE.COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY.key]: { active: true, params: null },
[ORGANIZATION_FEATURE.SHOW_SKILLS.key]: { active: false, params: null },
[ORGANIZATION_FEATURE.IS_MANAGING_STUDENTS.key]: { active: true, params: null },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |

beforeEach(async function () {
clock = sinon.useFakeTimers({ now, toFake: ['Date'] });
byDefaultFeatureId = await insertMultipleSendingFeatureForNewOrganization();
await databaseBuilder.commit();
});

Expand Down Expand Up @@ -436,7 +435,6 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
features: {
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: true, params: null },
[ORGANIZATION_FEATURE.LEARNER_IMPORT.key]: { active: false, params: null },
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: true, params: null },
},
parentOrganizationId: null,
parentOrganizationName: null,
Expand Down Expand Up @@ -497,7 +495,6 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
archivedBy: archivist.id,
archivedAt,
});
databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT);

await databaseBuilder.commit();

Expand Down Expand Up @@ -547,6 +544,8 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
it('saves the given organization', async function () {
// given
const superAdminUserId = databaseBuilder.factory.buildUser.withRole().id;
await insertMultipleSendingFeatureForNewOrganization();

await databaseBuilder.commit();

const organization = new OrganizationForAdmin({
Expand Down Expand Up @@ -578,6 +577,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
const organizationLearnerImportOndeFormat = databaseBuilder.factory.buildOrganizationLearnerImportFormat({
name: 'ONDE',
});
byDefaultFeatureId = await insertMultipleSendingFeatureForNewOrganization();

await databaseBuilder.commit();

Expand All @@ -589,9 +589,11 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |

const savedOrganization = await organizationForAdminRepository.save(organization);

const savedOrganizationFeatures = await knex('organization-features').where({
organizationId: savedOrganization.id,
});
const savedOrganizationFeatures = await knex('organization-features')
.where({
organizationId: savedOrganization.id,
})
.whereNot({ featureId: byDefaultFeatureId });

expect(savedOrganizationFeatures).to.have.lengthOf(3);
const savedOrganizationFeatureIds = savedOrganizationFeatures.map(
Expand All @@ -613,6 +615,10 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
});

describe('#update', function () {
beforeEach(async function () {
byDefaultFeatureId = await insertMultipleSendingFeatureForNewOrganization();
});

it('updates organization detail', async function () {
// given
const parentOrganizationId = databaseBuilder.factory.buildOrganization({ name: 'Parent Organization' }).id;
Expand All @@ -637,8 +643,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
name: 'super orga',
createdBy: userId,
});

const featureId = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT).id;
const featureId = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MISSIONS_MANAGEMENT).id;
await databaseBuilder.commit();

// when
Expand All @@ -647,14 +652,14 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
documentationUrl: 'https://pix.fr/',
features: {
[ORGANIZATION_FEATURE.LEARNER_IMPORT.key]: { active: false },
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: true },
[ORGANIZATION_FEATURE.MISSIONS_MANAGEMENT.key]: { active: true },
},
});
await organizationForAdminRepository.update(organizationToUpdate);

// then
const enabledFeatures = await knex('organization-features')
.where({ organizationId: organization.id })
.where({ organizationId: organization.id, featureId })
.whereNot({ featureId: byDefaultFeatureId });
expect(enabledFeatures).to.have.lengthOf(1);
expect(enabledFeatures[0].featureId).to.equal(featureId);
Expand All @@ -669,6 +674,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
});

const featureId = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MISSIONS_MANAGEMENT).id;

databaseBuilder.factory.buildOrganizationFeature({ organizationId: organization.id, featureId });
await databaseBuilder.commit();

Expand All @@ -684,7 +690,9 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
await organizationForAdminRepository.update(organizationToUpdate);

// then
const enabledFeatures = await knex('organization-features').where({ organizationId: organization.id });
const enabledFeatures = await knex('organization-features')
.where({ organizationId: organization.id })
.whereNot({ featureId: byDefaultFeatureId });
expect(enabledFeatures).to.have.lengthOf(1);
expect(enabledFeatures[0].featureId).to.equal(featureId);
});
Expand All @@ -702,7 +710,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
createdBy: userId,
});

const featureId = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT).id;
const featureId = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MISSIONS_MANAGEMENT).id;
databaseBuilder.factory.buildOrganizationFeature({ organizationId: organization.id, featureId });
databaseBuilder.factory.buildOrganizationFeature({ organizationId: otherOrganization.id, featureId });

Expand All @@ -713,13 +721,13 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
id: organization.id,
documentationUrl: 'https://pix.fr/',
features: {
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: false },
[ORGANIZATION_FEATURE.MISSIONS_MANAGEMENT.key]: { active: false },
},
});
await organizationForAdminRepository.update(organizationToUpdate);

//then
const enabledFeatures = await knex('organization-features');
const enabledFeatures = await knex('organization-features').whereNot({ featureId: byDefaultFeatureId });
expect(enabledFeatures).to.have.lengthOf(1);
expect(enabledFeatures[0].organizationId).to.equal(otherOrganization.id);
});
Expand Down Expand Up @@ -762,6 +770,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
name: 'super orga',
createdBy: userId,
});

databaseBuilder.factory.buildDataProtectionOfficer.withOrganizationId({
organizationId: organization.id,
firstName: 'Tony',
Expand Down Expand Up @@ -797,6 +806,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
const organizationId = databaseBuilder.factory.buildOrganization().id;
const tagId = databaseBuilder.factory.buildTag({ name: 'myTag' }).id;
const otherTagId = databaseBuilder.factory.buildTag({ name: 'myOtherTag' }).id;

await databaseBuilder.commit();
const tagsToAdd = [
{ tagId, organizationId },
Expand Down
4 changes: 2 additions & 2 deletions api/tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ async function insertOrganizationUserWithRoleAdmin() {
// We insert a multiple sending feature by default for each new organization created.
// It is under feature for now because we want to be able to deactivate it when asked.
async function insertMultipleSendingFeatureForNewOrganization() {
const featureId = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT).id();
const feature = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT);
await databaseBuilder.commit();
return featureId;
return feature.id;
}

// Hapi
Expand Down

0 comments on commit 1181c9e

Please sign in to comment.