Skip to content

Commit

Permalink
Revert "The concours de la mairie de Paris has ended a long time ago !"
Browse files Browse the repository at this point in the history
Acces team is still using this script
This reverts commit cd33298.
  • Loading branch information
Steph0 authored and laura-bergoens committed Oct 2, 2024
1 parent 43e24ee commit 982db8f
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 0 deletions.
79 changes: 79 additions & 0 deletions api/scripts/create-users-accounts-for-contest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as url from 'node:url';

import { disconnect } from '../db/knex-database-connection.js';
import * as authenticationMethodRepository from '../src/identity-access-management/infrastructure/repositories/authentication-method.repository.js';
import { userToCreateRepository } from '../src/identity-access-management/infrastructure/repositories/user-to-create.repository.js';
import { cryptoService } from '../src/shared/domain/services/crypto-service.js';
import * as userService from '../src/shared/domain/services/user-service.js';
import { parseCsvWithHeader } from './helpers/csvHelpers.js';

function prepareDataForInsert(rawUsers) {
return rawUsers.map(({ firstName, lastName, email, password }) => {
return {
firstName: firstName.trim(),
lastName: lastName.trim(),
email: email.trim().toLowerCase(),
password: password.trim(),
};
});
}

async function createUsers({ usersInRaw }) {
const now = new Date();

for (const userDTO of usersInRaw) {
const userToCreate = {
firstName: userDTO.firstName,
lastName: userDTO.lastName,
email: userDTO.email,
createAt: now,
updatedAt: now,
cgu: true,
lang: 'fr',
};
const hashedPassword = await cryptoService.hashPassword(userDTO.password);

await userService.createUserWithPassword({
user: userToCreate,
hashedPassword,
userToCreateRepository,
authenticationMethodRepository,
});
}
}

const modulePath = url.fileURLToPath(import.meta.url);
const isLaunchedFromCommandLine = process.argv[1] === modulePath;

async function main() {
console.log('Starting creating users accounts for contest.');

const filePath = process.argv[2];

console.log('Reading and parsing csv data file... ');
const csvData = await parseCsvWithHeader(filePath);
console.log('ok');

console.log('Preparing data... ');
const usersInRaw = prepareDataForInsert(csvData);
console.log('ok');

console.log('Creating users...');
await createUsers({ usersInRaw });
console.log('\nDone.');
}

(async () => {
if (isLaunchedFromCommandLine) {
try {
await main();
} catch (error) {
console.error(error);
process.exitCode = 1;
} finally {
await disconnect();
}
}
})();

export { createUsers, prepareDataForInsert };
113 changes: 113 additions & 0 deletions api/tests/acceptance/scripts/create-users-accounts-for-contest_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { createUsers } from '../../../scripts/create-users-accounts-for-contest.js';
import { expect, knex, sinon } from '../../test-helper.js';

describe('Acceptance | Scripts | create-users-accounts-for-contest', function () {
describe('#createUsers', function () {
const now = new Date();
let clock;

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

afterEach(async function () {
clock.restore();
});

it('should insert users', async function () {
// given
const usersInRaw = [
{
firstName: 'Sandy',
lastName: 'Kilo',
email: '[email protected]',
password: 'pix123',
},
{
firstName: 'Tom',
lastName: 'Desavoie',
email: '[email protected]',
password: 'pixou123',
},
];

// when
await createUsers({ usersInRaw });

// then
const firstUserFound = await knex('users').where({ lastName: 'Kilo' }).first();
expect(firstUserFound).to.contains({
firstName: 'Sandy',
lastName: 'Kilo',
email: '[email protected]',
cgu: true,
pixOrgaTermsOfServiceAccepted: false,
pixCertifTermsOfServiceAccepted: false,
hasSeenAssessmentInstructions: false,
username: null,
mustValidateTermsOfService: false,
lastTermsOfServiceValidatedAt: null,
lang: 'fr',
hasSeenNewDashboardInfo: false,
isAnonymous: false,
emailConfirmedAt: null,
hasSeenFocusedChallengeTooltip: false,
hasSeenOtherChallengesTooltip: false,
lastPixOrgaTermsOfServiceValidatedAt: null,
lastPixCertifTermsOfServiceValidatedAt: null,
});
expect(firstUserFound.createdAt).to.deep.equal(now);
expect(firstUserFound.updatedAt).to.deep.equal(now);

const secondUserFound = await knex('users').where({ lastName: 'Desavoie' }).first();
expect(secondUserFound).to.contains({
firstName: 'Tom',
lastName: 'Desavoie',
});
});

it("should create users's authentication methods", async function () {
// given
const usersInRaw = [
{
firstName: 'Sandy',
lastName: 'Kilo',
email: '[email protected]',
password: 'pix123',
},
{
firstName: 'Tom',
lastName: 'Desavoie',
email: '[email protected]',
password: 'pixou123',
},
];

// when
await createUsers({ usersInRaw });

// then
const usersInDatabases = await knex('authentication-methods');
expect(usersInDatabases.length).to.equal(2);

const firstUserFound = await knex('users').where({ lastName: 'Kilo' }).first();
const firstAuthenticationMethodFound = await knex('authentication-methods')
.where({ userId: firstUserFound.id })
.first();
expect(firstAuthenticationMethodFound.identityProvider).to.equal('PIX');
expect(firstAuthenticationMethodFound.authenticationComplement.password).to.exist;
expect(firstAuthenticationMethodFound.authenticationComplement.shouldChangePassword).to.be.false;
expect(firstAuthenticationMethodFound.createdAt).to.be.not.null;
expect(firstAuthenticationMethodFound.updatedAt).to.be.not.null;

const secondUserFound = await knex('users').where({ lastName: 'Desavoie' }).first();
const secondAuthenticationMethodFound = await knex('authentication-methods')
.where({ userId: secondUserFound.id })
.first();
expect(secondAuthenticationMethodFound.authenticationComplement.password).to.exist;
});
});
});
33 changes: 33 additions & 0 deletions api/tests/unit/scripts/create-users-accounts-for-contest_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { prepareDataForInsert } from '../../../scripts/create-users-accounts-for-contest.js';
import { expect } from '../../test-helper.js';

describe('Unit | Scripts | create-users-accounts-for-contest.js', function () {
describe('#prepareDataForInsert', function () {
it('should trim firstName, lastName, email and password', function () {
// given
const data = [
{
firstName: ' Salim ',
lastName: ' Bienléongle ',
email: ' [email protected] ',
password: ' pix123 ',
},
{ firstName: ' Samantha ', lastName: ' Lo ', email: ' [email protected] ', password: ' pixou123 ' },
];

// when
const result = prepareDataForInsert(data);

// then
expect(result).to.deep.equal([
{
firstName: 'Salim',
lastName: 'Bienléongle',
email: '[email protected]',
password: 'pix123',
},
{ firstName: 'Samantha', lastName: 'Lo', email: '[email protected]', password: 'pixou123' },
]);
});
});
});

0 comments on commit 982db8f

Please sign in to comment.