Skip to content

Commit

Permalink
add some success conditions for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
laura-bergoens authored Nov 13, 2024
1 parent 3390fdb commit 6734f7b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 19 deletions.
5 changes: 2 additions & 3 deletions api/lib/application/whitelisted-urls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ export async function register(server) {
if (!whitelistedUrlToUpdate) {
throw new NotFoundWhitelistedUrlError(`L'URL whitelistée d'id ${whitelistedUrlId} n'existe pas`);
}
const existingWhitelistedUrls = await whitelistedUrlReadRepository.list();
const existingWhitelistedUrlsExceptUpdatedOne = existingWhitelistedUrls.filter((whitelistedUrl) => whitelistedUrl.id !== whitelistedUrlId);
whitelistedUrlToUpdate.canUpdate(updateCommand, authenticatedUser, existingWhitelistedUrlsExceptUpdatedOne);
const existingWhitelistedUrls = await whitelistedUrlRepository.list();
whitelistedUrlToUpdate.canUpdate(updateCommand, authenticatedUser, existingWhitelistedUrls);
whitelistedUrlToUpdate.update(updateCommand, authenticatedUser);
await whitelistedUrlRepository.save(whitelistedUrlToUpdate);
const updatedWhitelistedUrl = await whitelistedUrlReadRepository.find(whitelistedUrlId);
Expand Down
14 changes: 10 additions & 4 deletions api/lib/domain/models/WhitelistedUrl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
NotFoundWhitelistedUrlError,
CommandWhitelistedUrlConflictError,
CommandWhitelistedUrlError,
CommandWhitelistedUrlForbiddenError,
NotFoundWhitelistedUrlError,
} from '../errors.js';

export class WhitelistedUrl {
Expand Down Expand Up @@ -39,13 +39,18 @@ export class WhitelistedUrl {
};
}

get isActive() {
return this.deletedAt === null;
}

static canCreate(creationCommand, user, existingWhitelistedUrls) {
const activeExistingWhitelistedUrls = existingWhitelistedUrls.filter((whitelistedUrl) => whitelistedUrl.isActive);
if (!user.isAdmin) throw new CommandWhitelistedUrlForbiddenError('L\'utilisateur n\'a pas les droits pour créer une URL whitelistée');
if (!isUrlValid(creationCommand.url)) throw new CommandWhitelistedUrlError({ message: 'URL invalide', attribute: 'url' });
if (!isRelatedSkillNamesValid(creationCommand.relatedSkillNames)) throw new CommandWhitelistedUrlError({ message: 'Liste d\'acquis invalide. Doit être une suite d\'acquis séparés par des virgules ou vide', attribute: 'relatedSkillNames' });
if (!isCommentValid(creationCommand.comment)) throw new CommandWhitelistedUrlError({ message: 'Commentaire invalide. Doit être un texte ou vide', attribute: 'comment' });
if (!isCheckTypeValid(creationCommand.checkType)) throw new CommandWhitelistedUrlError({ message: `Type de check invalide. Valeurs parmi : ${Object.values(WhitelistedUrl.CHECK_TYPES).join(', ')}`, attribute: 'checkType' });
if (!isUrlUnique(creationCommand.url, existingWhitelistedUrls)) throw new CommandWhitelistedUrlConflictError('URL déjà whitelistée');
if (!isUrlUnique(creationCommand.url, activeExistingWhitelistedUrls)) throw new CommandWhitelistedUrlConflictError('URL déjà whitelistée');
}

static create(creationCommand, user) {
Expand Down Expand Up @@ -78,14 +83,15 @@ export class WhitelistedUrl {
this.deletedAt = operationDate;
}

canUpdate(updateCommand, user, existingReadWhitelistedUrls) {
canUpdate(updateCommand, user, existingWhitelistedUrls) {
const activeOtherExistingWhitelistedUrls = existingWhitelistedUrls.filter((whitelistedUrl) => whitelistedUrl.isActive && whitelistedUrl.id !== this.id);
if (!user.isAdmin) throw new CommandWhitelistedUrlForbiddenError('L\'utilisateur n\'a pas les droits pour mettre à jour cette URL whitelistée');
if (this.deletedAt) throw new NotFoundWhitelistedUrlError('L\'URL whitelistée n\'existe pas');
if (!isUrlValid(updateCommand.url)) throw new CommandWhitelistedUrlError({ message: 'URL invalide', attribute: 'url' });
if (!isRelatedSkillNamesValid(updateCommand.relatedSkillNames)) throw new CommandWhitelistedUrlError({ message: 'Liste d\'acquis invalide. Doit être une suite d\'acquis séparés par des virgules ou vide', attribute: 'relatedSkillNames' });
if (!isCommentValid(updateCommand.comment)) throw new CommandWhitelistedUrlError({ message: 'Commentaire invalide. Doit être un texte ou vide', attribute: 'comment' });
if (!isCheckTypeValid(updateCommand.checkType)) throw new CommandWhitelistedUrlError({ message: `Type de check invalide. Valeurs parmi : ${Object.values(WhitelistedUrl.CHECK_TYPES).join(', ')}`, attribute: 'checkType' });
if (!isUrlUnique(updateCommand.url, existingReadWhitelistedUrls)) throw new CommandWhitelistedUrlConflictError('URL déjà whitelistée');
if (!isUrlUnique(updateCommand.url, activeOtherExistingWhitelistedUrls)) throw new CommandWhitelistedUrlConflictError('URL déjà whitelistée');
}

update(updateCommand, user) {
Expand Down
82 changes: 70 additions & 12 deletions api/tests/unit/domain/models/WhitelistedUrl_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { domainBuilder } from '../../../test-helper.js';
import { User, WhitelistedUrl } from '../../../../lib/domain/models/index.js';
import {
NotFoundWhitelistedUrlError,
CommandWhitelistedUrlConflictError,
CommandWhitelistedUrlError,
CommandWhitelistedUrlForbiddenError,
NotFoundWhitelistedUrlError,
} from '../../../../lib/domain/errors.js';

describe('Unit | Domain | WhitelistedUrl', () => {
Expand All @@ -22,6 +22,34 @@ describe('Unit | Domain | WhitelistedUrl', () => {
vi.useRealTimers();
});

describe('#get isActive', function() {
it('should return true when WhitelistedUrl is not deleted', function() {
// given
const activeWhitelistedUrl = domainBuilder.buildWhitelistedUrl({
deletedAt: null,
});

// when
const isActive = activeWhitelistedUrl.isActive;

// then
expect(isActive).toStrictEqual(true);
});

it('should return false when WhitelistedUrl is deleted', function() {
// given
const activeWhitelistedUrl = domainBuilder.buildWhitelistedUrl({
deletedAt: new Date('2020-01-01'),
});

// when
const isActive = activeWhitelistedUrl.isActive;

// then
expect(isActive).toStrictEqual(false);
});
});

describe('#canDelete', () => {
describe('can', function() {
it('should not throw when all conditions are reunited', function() {
Expand Down Expand Up @@ -121,9 +149,18 @@ describe('Unit | Domain | WhitelistedUrl', () => {
it('should not throw when all conditions are reunited', function() {
// given
const user = domainBuilder.buildUser({ access: User.ROLES.ADMIN });
const existingWhitelistedUrls = [domainBuilder.buildWhitelistedUrl({
url: 'https://www.painperdu.com',
})];
const existingWhitelistedUrls = [
domainBuilder.buildWhitelistedUrl({
id: 123,
url: 'https://www.painperdu.com',
deletedAt: null,
}),
domainBuilder.buildWhitelistedUrl({
id: 456,
url: 'https://www.brioche.com',
deletedAt: new Date('2020-01-01'),
}),
];
const creationCommand1 = {
url: 'https://www.brioche.com',
relatedSkillNames: null,
Expand Down Expand Up @@ -379,9 +416,25 @@ describe('Unit | Domain | WhitelistedUrl', () => {
it('should not throw when all conditions are reunited', function() {
// given
const user = domainBuilder.buildUser({ access: User.ROLES.ADMIN });
const existingWhitelistedUrls = [domainBuilder.buildWhitelistedUrlRead({
url: 'https://www.painperdu.com',
})];
const whitelistedUrlToUpdate = domainBuilder.buildWhitelistedUrl({
id: 123,
url: 'https://www.url-origine.com',
deletedBy: null,
deletedAt: null,
});
const existingWhitelistedUrls = [
domainBuilder.buildWhitelistedUrl({
id: 456,
url: 'https://www.painperdu.com',
deletedAt: null,
}),
domainBuilder.buildWhitelistedUrl({
id: 789,
url: 'https://www.brioche.com',
deletedAt: new Date('2020-01-01'),
}),
whitelistedUrlToUpdate,
];
const updateCommand1 = {
url: 'https://www.brioche.com',
relatedSkillNames: null,
Expand All @@ -394,14 +447,17 @@ describe('Unit | Domain | WhitelistedUrl', () => {
comment: 'COucou',
checkType: WhitelistedUrl.CHECK_TYPES.STARTS_WITH,
};
const whitelistedUrlToUpdate = domainBuilder.buildWhitelistedUrl({
deletedBy: null,
deletedAt: null,
});
const updateCommand3 = {
url: 'https://www.url-origine.com',
relatedSkillNames: '@fruit8',
comment: 'Update avec la même url mais je vérifie que je me conflicte pas avec moi-même',
checkType: WhitelistedUrl.CHECK_TYPES.EXACT_MATCH,
};

// when
whitelistedUrlToUpdate.canUpdate(updateCommand1, user, existingWhitelistedUrls);
whitelistedUrlToUpdate.canUpdate(updateCommand2, user, []);
whitelistedUrlToUpdate.canUpdate(updateCommand3, user, existingWhitelistedUrls);

// then
expect(true).to.be.true;
Expand Down Expand Up @@ -602,7 +658,8 @@ describe('Unit | Domain | WhitelistedUrl', () => {
it('should throw a CommandWhitelistedUrlConflictError when url has already been whitelisted (case sensitive, exact match)', function() {
// given
const user = domainBuilder.buildUser({ access: User.ROLES.ADMIN });
const existingWhitelistedUrls = [domainBuilder.buildWhitelistedUrlRead({
const existingWhitelistedUrls = [domainBuilder.buildWhitelistedUrl({
id: 123,
url: 'https://www.brioche.com',
})];
const updateCommand = {
Expand All @@ -612,6 +669,7 @@ describe('Unit | Domain | WhitelistedUrl', () => {
checkType: WhitelistedUrl.CHECK_TYPES.STARTS_WITH,
};
const whitelistedUrlToUpdate = domainBuilder.buildWhitelistedUrl({
id: 456,
deletedBy: null,
deletedAt: null,
});
Expand Down

0 comments on commit 6734f7b

Please sign in to comment.