Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Avoir l'envoi multiple activé par défaut à la création d'une organisation (PIX-16684) #11551

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ class OrganizationForAdmin {
active: showNPS,
params: showNPS ? { formNPSUrl: formNPSUrl } : null,
};
if (this.features[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key] === undefined) {
this.features[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key] = {
active: true,
params: null,
};
}
if (this.type === 'SCO' && this.isManagingStudents) {
this.features[ORGANIZATION_FEATURE.COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY.key] = {
active: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
databaseBuilder,
expect,
generateAuthenticatedUserRequestHeaders,
insertMultipleSendingFeatureForNewOrganization,
insertUserWithRoleSuperAdmin,
knex,
sinon,
Expand All @@ -24,6 +25,7 @@ describe('Acceptance | Application | organization-controller', function () {
beforeEach(async function () {
server = await createServer();
await insertUserWithRoleSuperAdmin();
await insertMultipleSendingFeatureForNewOrganization();
});

describe('POST /api/admin/organizations', function () {
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 @@ -19,12 +19,18 @@ import { Membership } from '../../../../src/shared/domain/models/Membership.js';
import * as organizationRepository from '../../../../src/shared/infrastructure/repositories/organization-repository.js';
import { organizationInvitationService } from '../../../../src/team/domain/services/organization-invitation.service.js';
import { organizationInvitationRepository } from '../../../../src/team/infrastructure/repositories/organization-invitation.repository.js';
import { catchErr, databaseBuilder, expect, knex } from '../../../test-helper.js';
import {
catchErr,
databaseBuilder,
expect,
insertMultipleSendingFeatureForNewOrganization,
knex,
} from '../../../test-helper.js';

const { omit } = lodash;

describe('Integration | UseCases | create-organizations-with-tags-and-target-profiles', function () {
let missionFeature, oralizationFeature, importStudentsFeature, ondeImportFormat, userId;
let missionFeature, oralizationFeature, importStudentsFeature, ondeImportFormat, userId, byDefaultFeatureId;

beforeEach(async function () {
databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY);
Expand All @@ -34,6 +40,7 @@ describe('Integration | UseCases | create-organizations-with-tags-and-target-pro
ondeImportFormat = databaseBuilder.factory.buildOrganizationLearnerImportFormat({
name: ORGANIZATION_FEATURE.LEARNER_IMPORT.FORMAT.ONDE,
});
byDefaultFeatureId = await insertMultipleSendingFeatureForNewOrganization();

userId = databaseBuilder.factory.buildUser().id;
await databaseBuilder.commit();
Expand Down Expand Up @@ -739,7 +746,7 @@ describe('Integration | UseCases | create-organizations-with-tags-and-target-pro
});

// then
const savedOrganizationFeatures = await knex('organization-features');
const savedOrganizationFeatures = await knex('organization-features').whereNot({ featureId: byDefaultFeatureId });
expect(savedOrganizationFeatures).to.have.lengthOf(3);
const organizationId = createdOrganizations[0].id;
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
databaseBuilder,
expect,
generateAuthenticatedUserRequestHeaders,
insertMultipleSendingFeatureForNewOrganization,
insertUserWithRoleSuperAdmin,
knex,
} from '../../../../test-helper.js';
Expand All @@ -19,6 +20,7 @@ describe('Acceptance | Organizational Entities | Application | Route | Admin | O

beforeEach(async function () {
admin = await insertUserWithRoleSuperAdmin();
await insertMultipleSendingFeatureForNewOrganization();
await databaseBuilder.commit();

server = await createServer();
Expand Down Expand Up @@ -65,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 });
databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT);
await databaseBuilder.commit();

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import { organizationForAdminRepository } from '../../../../../src/organizationa
import { ORGANIZATION_FEATURE } from '../../../../../src/shared/domain/constants.js';
import { MissingAttributesError, NotFoundError } from '../../../../../src/shared/domain/errors.js';
import { OrganizationInvitation } from '../../../../../src/team/domain/models/OrganizationInvitation.js';
import { catchErr, databaseBuilder, domainBuilder, expect, knex, sinon } from '../../../../test-helper.js';
import {
catchErr,
databaseBuilder,
domainBuilder,
expect,
insertMultipleSendingFeatureForNewOrganization,
knex,
sinon,
} from '../../../../test-helper.js';

describe('Integration | Organizational Entities | Infrastructure | Repository | organization-for-admin', function () {
let clock;
let clock, byDefaultFeatureId;
const now = new Date('2022-02-02');

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

afterEach(function () {
Expand Down Expand Up @@ -424,6 +433,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
creatorLastName: 'Encieux',
identityProviderForCampaigns: 'genericOidcProviderCode',
features: {
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: true, params: null },
[ORGANIZATION_FEATURE.LEARNER_IMPORT.key]: { active: false, params: null },
},
parentOrganizationId: null,
Expand Down Expand Up @@ -485,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 @@ -522,9 +531,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
creatorFirstName: superAdminUser.firstName,
creatorLastName: superAdminUser.lastName,
identityProviderForCampaigns: null,
features: {
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: false, params: null },
},
features: {},
parentOrganizationId: null,
parentOrganizationName: null,
});
Expand All @@ -537,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 @@ -568,6 +577,7 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
const organizationLearnerImportOndeFormat = databaseBuilder.factory.buildOrganizationLearnerImportFormat({
name: 'ONDE',
});
byDefaultFeatureId = await insertMultipleSendingFeatureForNewOrganization();

await databaseBuilder.commit();

Expand All @@ -579,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 @@ -603,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 @@ -627,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 @@ -637,13 +652,15 @@ 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 });
const enabledFeatures = await knex('organization-features')
.where({ organizationId: organization.id, featureId })
.whereNot({ featureId: byDefaultFeatureId });
expect(enabledFeatures).to.have.lengthOf(1);
expect(enabledFeatures[0].featureId).to.equal(featureId);
});
Expand All @@ -656,7 +673,8 @@ 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 });
await databaseBuilder.commit();

Expand All @@ -665,14 +683,16 @@ describe('Integration | Organizational Entities | Infrastructure | Repository |
id: organization.id,
documentationUrl: 'https://pix.fr/',
features: {
[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 });
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 @@ -690,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 @@ -701,17 +721,42 @@ 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);
});

it('should disable the "by default" feature for a given organization', async function () {
// given
const userId = databaseBuilder.factory.buildUser({ firstName: 'Anne', lastName: 'Héantie' }).id;
const organization = databaseBuilder.factory.buildOrganization({
name: 'super orga',
createdBy: userId,
});

await databaseBuilder.commit();

// when
const organizationToUpdate = new OrganizationForAdmin({
id: organization.id,
documentationUrl: 'https://pix.fr/',
features: {
[ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT.key]: { active: false },
},
});
await organizationForAdminRepository.update(organizationToUpdate);

//then
const enabledFeatures = await knex('organization-features');
expect(enabledFeatures).to.have.lengthOf(0);
});

it('should create data protection officer', async function () {
// given
const userId = databaseBuilder.factory.buildUser({ firstName: 'Spider', lastName: 'Man' }).id;
Expand Down Expand Up @@ -750,6 +795,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 @@ -785,6 +831,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
10 changes: 10 additions & 0 deletions api/tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { PIX_ADMIN } from '../src/authorization/domain/constants.js';
import * as tutorialRepository from '../src/devcomp/infrastructure/repositories/tutorial-repository.js';
import * as missionRepository from '../src/school/infrastructure/repositories/mission-repository.js';
import { config } from '../src/shared/config.js';
import { ORGANIZATION_FEATURE } from '../src/shared/domain/constants.js';
import { Membership } from '../src/shared/domain/models/index.js';
import * as tokenService from '../src/shared/domain/services/token-service.js';
import { featureToggles } from '../src/shared/infrastructure/feature-toggles/index.js';
Expand Down Expand Up @@ -192,6 +193,14 @@ async function insertOrganizationUserWithRoleAdmin() {
return { adminUser, organization };
}

// 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 feature = databaseBuilder.factory.buildFeature(ORGANIZATION_FEATURE.MULTIPLE_SENDING_ASSESSMENT);
await databaseBuilder.commit();
return feature.id;
}

// Hapi
const hFake = {
response(source) {
Expand Down Expand Up @@ -348,6 +357,7 @@ export {
generateValidRequestAuthorizationHeaderForApplication,
hFake,
HttpTestServer,
insertMultipleSendingFeatureForNewOrganization,
insertOrganizationUserWithRoleAdmin,
insertUserWithRoleCertif,
insertUserWithRoleSuperAdmin,
Expand Down