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

chore(users): ajoute une contrainte d'unicité sur l'email et supprime le mot de passe #86

Open
wants to merge 1 commit into
base: main
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
9 changes: 9 additions & 0 deletions database/migrations/024-users-email-unique.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Knex } from 'knex';

exports.up = async (knex: Knex) => {
await knex.raw('create unique index users_email_index on users (email)')
await knex.raw('alter table users drop column password')
};

exports.down = async (_knex: Knex) => {
};
7 changes: 0 additions & 7 deletions database/seeds/dummy/001-users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { fakerFR } from '@faker-js/faker';
import bcrypt from 'bcryptjs';
import { Knex } from 'knex';
import { v4 as uuidv4 } from 'uuid';
import { setKnexInstance } from '../../../server/repositories/db';
Expand All @@ -12,23 +11,20 @@ exports.seed = async function (knex: Knex) {
{
id: uuidv4(),
email: '[email protected]',
password: bcrypt.hashSync('Test2024'),
firstName: fakerFR.person.firstName(),
lastName: fakerFR.person.lastName(),
roles: ['Administrator']
},
{
id: uuidv4(),
email: '[email protected]',
password: bcrypt.hashSync('Test2024'),
firstName: fakerFR.person.firstName(),
lastName: fakerFR.person.lastName(),
roles: ['NationalCoordinator']
},
{
id: uuidv4(),
email: '[email protected]',
password: bcrypt.hashSync('Test2024'),
firstName: fakerFR.person.firstName(),
lastName: fakerFR.person.lastName(),
roles: ['RegionalCoordinator'],
Expand All @@ -37,7 +33,6 @@ exports.seed = async function (knex: Knex) {
{
id: uuidv4(),
email: '[email protected]',
password: bcrypt.hashSync('Test2024'),
firstName: fakerFR.person.firstName(),
lastName: fakerFR.person.lastName(),
roles: ['RegionalCoordinator'],
Expand All @@ -46,7 +41,6 @@ exports.seed = async function (knex: Knex) {
{
id: uuidv4(),
email: '[email protected]',
password: bcrypt.hashSync('Test2024'),
firstName: fakerFR.person.firstName(),
lastName: fakerFR.person.lastName(),
roles: ['Sampler'],
Expand All @@ -55,7 +49,6 @@ exports.seed = async function (knex: Knex) {
{
id: uuidv4(),
email: '[email protected]',
password: bcrypt.hashSync('Test2024'),
firstName: fakerFR.person.firstName(),
lastName: fakerFR.person.lastName(),
roles: ['Sampler'],
Expand Down
15 changes: 0 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@faker-js/faker": "^8.2.0",
"@getbrevo/brevo": "^2.1.1",
"@sentry/node": "^7.118.0",
"bcryptjs": "^2.4.3",
"compose-middleware": "^5.0.1",
"convict": "^6.2.4",
"convict-format-with-validator": "^6.2.0",
Expand Down Expand Up @@ -72,7 +71,6 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/convict": "^6.1.1",
"@types/convict-format-with-validator": "^6.0.2",
"@types/cors": "^2.8.12",
Expand Down
1 change: 0 additions & 1 deletion server/repositories/kysely.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ export interface Users {
firstName: string;
id: Generated<string>;
lastName: string;
password: string;
region: Region | null;
roles: UserRole[];
}
Expand Down
28 changes: 28 additions & 0 deletions server/repositories/userRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { kysely } from './kysely';
import { expect, test } from 'vitest';

test("impossible d'avoir 2 utilisateurs avec le même email", async () => {
const email = '[email protected]'
await kysely.insertInto('users').values({
region: '01',
email,
roles: ['Sampler'],
firstName: 'firstName',
lastName: 'lastName'
}).execute()
await kysely.insertInto('users').values({
region: '02',
email: '[email protected]',
roles: ['Sampler'],
firstName: 'firstName2',
lastName: 'lastName2'
}).execute()
expect(async () =>
await kysely.insertInto('users').values({
region: '03',
email,
roles: ['Sampler'],
firstName: 'firstName3',
lastName: 'lastName3'
}).execute()).rejects.toThrowErrorMatchingInlineSnapshot(`[error: duplicate key value violates unique constraint "users_email_index"]`)
})
5 changes: 1 addition & 4 deletions server/routers/test/auth.router.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import bcrypt from 'bcryptjs';
import { constants } from 'http2';
import request from 'supertest';
import { genUser } from '../../../shared/test/userFixtures';
Expand All @@ -22,10 +21,8 @@ describe('Auth routes', () => {
const user = genUser();

beforeAll(async () => {
const hash = await bcrypt.hash(user.password, 10);
await Users().insert({
...user,
password: hash
...user
});
});

Expand Down
11 changes: 5 additions & 6 deletions server/routers/test/user.router.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { constants } from 'http2';
import { describe, test, expect } from 'vitest';
import fp from 'lodash';
import randomstring from 'randomstring';
import request from 'supertest';
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -90,8 +89,8 @@ describe('User router', () => {
.expect(constants.HTTP_STATUS_OK);

expect(res.body).toEqual([
fp.omit(Sampler1Fixture, 'password'),
fp.omit(RegionalCoordinator, 'password')
Sampler1Fixture,
RegionalCoordinator,
]);
});

Expand All @@ -102,9 +101,9 @@ describe('User router', () => {
.expect(constants.HTTP_STATUS_OK);

expect(res.body).toEqual([
fp.omit(Sampler1Fixture, 'password'),
fp.omit(Sampler2Fixture, 'password'),
fp.omit(SamplerDromFixture, 'password')
Sampler1Fixture,
Sampler2Fixture,
SamplerDromFixture,
]);
});
});
Expand Down
1 change: 0 additions & 1 deletion shared/schema/User/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { UserRole, UserRolePermissions } from './UserRole';
export const User = z.object({
id: z.string().uuid(),
email: z.string().email(),
password: z.string(),
firstName: z.string(),
lastName: z.string(),
roles: z.array(UserRole),
Expand Down
2 changes: 0 additions & 2 deletions shared/test/userFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import { User } from '../schema/User/User';
import { UserRoleList } from '../schema/User/UserRole';
import { oneOf } from './testFixtures';

export const genValidPassword = () => '123Valid';

export const genUser = (data?: Partial<User>): User => {
const roles = data?.roles ?? [oneOf(UserRoleList)];
return {
id: uuidv4(),
email: fakerFR.internet.email(),
password: randomstring.generate(),
firstName: fakerFR.person.firstName(),
lastName: fakerFR.person.lastName(),
roles,
Expand Down
5 changes: 3 additions & 2 deletions vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { defineWorkspace } from 'vitest/config'

const integrationTestFiles = ['**/*.router.test.ts', '**/repositories/*.test.ts']
export default defineWorkspace([
{
test: {
name: 'unit',
setupFiles: [],
include: ['shared/**/*.test.ts', 'server/**/*.test.ts'],
exclude: ['**/*.router.test.ts'],
exclude: integrationTestFiles,
},
},
{
Expand All @@ -15,7 +16,7 @@ export default defineWorkspace([
hookTimeout: 45000,
root: 'server',
setupFiles: ['./server/test/setupTests.ts'],
include: ['**/*.router.test.ts'],
include: integrationTestFiles,
},
},
])
Loading