Skip to content

Commit

Permalink
feat(structures): Can not add identifier to a structure if a same typ… (
Browse files Browse the repository at this point in the history
#116)

* feat(structures): Can not add identifier to a structure if a same type and value already exist, see #115

* fix(ci/cd): Correct tests about structures identifiers
  • Loading branch information
annelhote authored Oct 3, 2024
1 parent 554f2be commit a3ed85e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
88 changes: 60 additions & 28 deletions src/api/structures/__tests__/identifiers.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { structures as resource, identifiers as subresource } from '../../resources';
import {
structures as resource,
identifiers as subresource,
} from '../../resources';

let authorization;
let id;
let resourceId;

const payload = {
Expand All @@ -25,20 +27,16 @@ beforeAll(async () => {
resourceId = body.id;
});

beforeEach(async () => {
afterEach(async () => {
// Delete all structures identifiers
const { body } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);
id = body.id;
});
.get(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization);
const promises = body.data.map((identifier) => global.superapp
.delete(`/${resource}/${resourceId}/${subresource}/${identifier.id}`)
.set('Authorization', authorization));

afterEach(async () => {
if (id) {
await global.superapp
.delete(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization);
}
await Promise.all(promises);
});

describe('API > structures > identifiers > create', () => {
Expand Down Expand Up @@ -77,17 +75,38 @@ describe('API > structures > identifiers > create', () => {
.send(rest)
.expect(400);
});

it('should return 204 if an identifier with same type and same value already exists', async () => {
const paylod = { type: 'siret', value: '12345678912346' };

await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(paylod)
.expect(201);

await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(paylod)
.expect(204);
});
});

describe('API > structures > identifiers > update', () => {
it('should update an existing identifier', async () => {
const { body: { id } } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);

const type = 'wikidata';
const { body } = await global.superapp
const { body: updatedBody } = await global.superapp
.patch(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
.send({ type })
.expect(200);
expect(body.type).toBe(type);
expect(updatedBody.type).toBe(type);
});

it('should throw bad request if id too short', async () => {
Expand All @@ -107,6 +126,11 @@ describe('API > structures > identifiers > update', () => {
});

it('should throw bad request with badly formatted payload', async () => {
const { body: { id } } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);

await global.superapp
.patch(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
Expand All @@ -115,6 +139,11 @@ describe('API > structures > identifiers > update', () => {
});

it('should accept empty dates', async () => {
const { body: { id } } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);

await global.superapp
.patch(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
Expand All @@ -126,13 +155,19 @@ describe('API > structures > identifiers > update', () => {
describe('API > structures > identifiers > read', () => {
it('should read existing identifier', async () => {
const { body } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);
const { id } = body;

const { body: readBody } = await global.superapp
.get(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
.expect(200);
expect(body.type).toBe(payload.type);
expect(body.value).toBe(payload.value);
expect(body.active).toBeFalsy();
expect(body.createdBy.lastName).toBe('user');
expect(readBody.type).toBe(payload.type);
expect(readBody.value).toBe(payload.value);
expect(readBody.active).toBeFalsy();
expect(readBody.createdBy.lastName).toBe('user');
});

it('should throw bad request if id too short', async () => {
Expand Down Expand Up @@ -166,6 +201,11 @@ describe('API > structures > identifiers > delete', () => {
});

it('should delete existing identifier', async () => {
const { body: { id } } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);

await global.superapp
.delete(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
Expand Down Expand Up @@ -207,14 +247,6 @@ describe('API > structures > identifiers > list', () => {
});
});

beforeEach(async () => {
if (id) {
await global.superapp
.delete(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization);
}
});

it('should list', async () => {
const { body } = await global.superapp
.get(`/${resource}/${resourceId}/${subresource}`)
Expand Down
8 changes: 8 additions & 0 deletions src/api/structures/identifiers/identifiers.middlewares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { identifiersRepository } from '../../commons/repositories';

export const validateStructureIdentifierCreatePayload = async (req, res, next) => {
const { type, value } = req.body;
const { resourceId } = req.params;
const check = await identifiersRepository.findOne({ resourceId, type, value });
return check ? res.status(204).json() : next();
};
4 changes: 3 additions & 1 deletion src/api/structures/identifiers/identifiers.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { saveInElastic, saveInStore } from '../../commons/middlewares/event.midd
import { readQuery, readQueryWithLookup } from '../../commons/queries/identifiers.query';
import elasticQuery from '../../commons/queries/structures.elastic';
import { identifiersRepository as repository, structuresRepository } from '../../commons/repositories';
import { identifiers as subresource, structures as resource } from '../../resources';
import { structures as resource, identifiers as subresource } from '../../resources';
import { validateStructureIdentifierCreatePayload } from './identifiers.middlewares';

const router = new express.Router();

router.route(`/${resource}/:resourceId/${subresource}`)
.get(controllers.list(repository, readQuery))
.post([
createContext,
validateStructureIdentifierCreatePayload,
setGeneratedInternalIdInContext(subresource),
controllers.create(repository, readQueryWithLookup),
saveInStore(subresource),
Expand Down
2 changes: 2 additions & 0 deletions src/openapi/paths/structures/identifiers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ collectionMethods:
application/json:
schema:
$ref: '../../api.yml#/components/schemas/StructureIdentifier'
'204':
description: 'No content'
'400':
$ref: '../../api.yml#/components/responses/BadRequest'
'401':
Expand Down

0 comments on commit a3ed85e

Please sign in to comment.