-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "The concours de la mairie de Paris has ended a long time ago !"
Acces team is still using this script This reverts commit cd33298.
- Loading branch information
1 parent
43e24ee
commit 982db8f
Showing
3 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
113
api/tests/acceptance/scripts/create-users-accounts-for-contest_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
api/tests/unit/scripts/create-users-accounts-for-contest_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
]); | ||
}); | ||
}); | ||
}); |