Skip to content

Commit

Permalink
tech(api): migrate route
Browse files Browse the repository at this point in the history
  • Loading branch information
xav-car authored Nov 15, 2024
1 parent 9dd1bef commit 5677282
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 146 deletions.
25 changes: 0 additions & 25 deletions api/lib/application/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,31 +192,6 @@ const register = async function (server) {
tags: ['api'],
},
},
{
method: 'GET',
path: '/api/users/{userId}/campaigns/{campaignId}/campaign-participations',
config: {
validate: {
params: Joi.object({
userId: identifiersType.userId,
campaignId: identifiersType.campaignId,
}),
},
pre: [
{
method: securityPreHandlers.checkRequestedUserIsAuthenticatedUser,
assign: 'requestedUserIsAuthenticatedUser',
},
],
handler: userController.getUserCampaignParticipationToCampaign,
notes: [
'- **Cette route est restreinte aux utilisateurs authentifiés**\n' +
'- Récupération de la dernière participation d’un utilisateur (**userId**) à une campagne donnée (**campaignId**)\n' +
'- L’id demandé doit correspondre à celui de l’utilisateur authentifié',
],
tags: ['api', 'user', 'campaign', 'campaign-participations'],
},
},
{
method: 'GET',
path: '/api/users/{id}/trainings',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,31 @@ const register = async function (server) {
tags: ['api'],
},
},
{
method: 'GET',
path: '/api/users/{userId}/campaigns/{campaignId}/campaign-participations',
config: {
validate: {
params: Joi.object({
userId: identifiersType.userId,
campaignId: identifiersType.campaignId,
}),
},
pre: [
{
method: securityPreHandlers.checkRequestedUserIsAuthenticatedUser,
assign: 'requestedUserIsAuthenticatedUser',
},
],
handler: campaignParticipationController.getUserCampaignParticipationToCampaign,
notes: [
'- **Cette route est restreinte aux utilisateurs authentifiés**\n' +
'- Récupération de la dernière participation d’un utilisateur (**userId**) à une campagne donnée (**campaignId**)\n' +
'- L’id demandé doit correspondre à celui de l’utilisateur authentifié',
],
tags: ['api', 'user', 'campaign', 'campaign-participations'],
},
},
]);
};

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -848,4 +848,32 @@ describe('Acceptance | API | Campaign Participations', function () {
expect(participationIds).to.deep.equals([sharableCampaignParticipation.id, startedCampaignParticipation.id]);
});
});

describe('GET /users/{userId}/campaigns/{campaignId}/campaign-participations', function () {
let options;

beforeEach(function () {
databaseBuilder.factory.buildCampaignParticipation({
userId,
campaignId,
status: 'SHARED',
});

options = {
method: 'GET',
url: `/api/users/${userId}/campaigns/${campaignId}/campaign-participations`,
headers: { authorization: generateValidRequestAuthorizationHeader(userId) },
};

return databaseBuilder.commit();
});

it('should return campaign participation with 200 HTTP status code', async function () {
// when
const response = await server.inject(options);

// then
expect(response.statusCode).to.equal(200);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,38 @@ describe('Unit | Application | Router | campaign-participation-router ', functio
expect(securityPreHandlers.checkAuthorizationToAccessCampaign.called).to.be.true;
});
});

describe('GET /api/users/{userId}/campaigns/{campaignId}/campaign-participations', function () {
const method = 'GET';

it('returns 400 when userId is not a number', async function () {
// given
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);

const userId = 'wrongId';
const url = `/api/users/${userId}/campaigns/34/campaign-participations`;

// when
const result = await httpTestServer.request(method, url);

// then
expect(result.statusCode).to.equal(400);
});

it('returns 400 when campaignId is not a number', async function () {
// given
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);

const campaignId = 'wrongId';
const url = `/api/users/12/campaigns/${campaignId}/campaign-participations`;

// when
const result = await httpTestServer.request(method, url);

// then
expect(result.statusCode).to.equal(400);
});
});
});
52 changes: 0 additions & 52 deletions api/tests/unit/application/users/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,6 @@ const CODE_IDENTITY_PROVIDER_POLE_EMPLOI = OidcIdentityProviders.POLE_EMPLOI.cod
const oidcProviderCode = 'genericOidcProviderCode';

describe('Unit | Router | user-router', function () {
describe('GET /api/users/{userId}/campaigns/{campaignId}/campaign-participations', function () {
const method = 'GET';

it('returns 200', async function () {
// given
sinon.stub(userController, 'getUserCampaignParticipationToCampaign').returns('ok');
sinon
.stub(securityPreHandlers, 'checkRequestedUserIsAuthenticatedUser')
.callsFake((request, h) => h.response(true));
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);

const url = '/api/users/12/campaigns/34/campaign-participations';

// when
const result = await httpTestServer.request(method, url);

// then
expect(result.statusCode).to.equal(200);
});

it('returns 400 when userId is not a number', async function () {
// given
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);

const userId = 'wrongId';
const url = `/api/users/${userId}/campaigns/34/campaign-participations`;

// when
const result = await httpTestServer.request(method, url);

// then
expect(result.statusCode).to.equal(400);
});

it('returns 400 when campaignId is not a number', async function () {
// given
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);

const campaignId = 'wrongId';
const url = `/api/users/12/campaigns/${campaignId}/campaign-participations`;

// when
const result = await httpTestServer.request(method, url);

// then
expect(result.statusCode).to.equal(400);
});
});

describe('GET /api/users/{id}/trainings', function () {
const method = 'GET';

Expand Down

0 comments on commit 5677282

Please sign in to comment.