Skip to content

Commit

Permalink
[TECH] Ajouter une route /api/healthcheck/forwarded-origin (PIX-16368)
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Feb 5, 2025
2 parents 075ee46 + 1ad2cf0 commit 2f1f0fb
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { config } from '../../../../src/shared/config.js';
import * as network from '../../../identity-access-management/infrastructure/utils/network.js';
import { featureToggles } from '../../infrastructure/feature-toggles/index.js';
import * as serializer from '../../infrastructure/serializers/jsonapi/feature-toggle-serializer.js';

Expand All @@ -11,11 +10,6 @@ const getActiveFeatures = async function () {
return serializer.serialize({ ...newFeatureToggles, ...legacyFeatureToggles });
};

// TODO: Test route to be removed soon
const getForwardedOrigin = function (request, h) {
return h.response(network.getForwardedOrigin(request.headers)).code(200);
};

const featureToggleController = { getActiveFeatures, getForwardedOrigin };
const featureToggleController = { getActiveFeatures };

export { featureToggleController, getForwardedOrigin };
export { featureToggleController };
15 changes: 0 additions & 15 deletions api/src/shared/application/feature-toggles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ const register = async function (server) {
cache: false,
},
},
// TODO: Test route to be removed soon
{
method: 'GET',
path: '/api/test-origin-soon-to-be-remove',
config: {
auth: false,
handler: featureToggleController.getForwardedOrigin,
notes: [
'- **Route ponctuelle à des fins de test**\n' +
'- **Cette route est publique**\n' +
'- Récupération de l’origin HTTP de l’application appelante\n',
],
tags: ['identity-access-management', 'api', 'user'],
},
},
]);
};

Expand Down
12 changes: 11 additions & 1 deletion api/src/shared/application/healthcheck/healthcheck-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Boom from '@hapi/boom';

import { knex } from '../../../../db/knex-database-connection.js';
import packageJSON from '../../../../package.json' with { type: 'json' };
import * as network from '../../../identity-access-management/infrastructure/utils/network.js';
import { config } from '../../config.js';
import { redisMonitor } from '../../infrastructure/utils/redis-monitor.js';

Expand Down Expand Up @@ -37,6 +38,15 @@ const checkRedisStatus = async function () {
}
};

const healthcheckController = { get, checkDbStatus, checkRedisStatus };
const checkForwardedOriginStatus = async function (request, h) {
const forwardedOrigin = network.getForwardedOrigin(request.headers);
if (!forwardedOrigin) {
return h.response('Obtaining Forwarded Origin failed').code(500);
}

return h.response(forwardedOrigin).code(200);
};

const healthcheckController = { get, checkDbStatus, checkRedisStatus, checkForwardedOriginStatus };

export { healthcheckController };
10 changes: 10 additions & 0 deletions api/src/shared/application/healthcheck/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ const register = async function (server) {
tags: ['api'],
},
},
{
method: 'GET',
path: '/api/healthcheck/forwarded-origin',
config: {
auth: false,
handler: healthcheckController.checkForwardedOriginStatus,
notes: ['- **Cette route est publique**\n' + '- Récupération de l’origine HTTP de l’application appelante\n'],
tags: ['api', 'healthcheck'],
},
},
]);
};

Expand Down
23 changes: 23 additions & 0 deletions api/tests/integration/application/healthcheck/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,27 @@ describe('Integration | Application | Route | healthcheckRouter', function () {
expect(healthCheckController.checkRedisStatus).to.have.been.calledOnce;
});
});

describe('GET /api/healthcheck/forwarded-origin', function () {
context('when forwarded origin is not obtained', function () {
it('returns an HTTP status code 500', async function () {
// given
const options = {
method: 'GET',
url: '/api/healthcheck/forwarded-origin',
headers: {
'x-forwarded-proto': 'https',
'x-forwarded-host-is-missing': 'there is a problem with the nginx configuration',
},
};

// when
const response = await httpTestServer.requestObject(options);

// then
expect(response.statusCode).to.equal(500);
expect(response.result).to.equal('Obtaining Forwarded Origin failed');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,4 @@ describe('Acceptance | Shared | Application | Controller | feature-toggle', func
expect(response.result).to.deep.equal(expectedData);
});
});

describe('GET /api/test-origin-soon-to-be-remove. Test route soon to be removed.', function () {
const options = {
method: 'GET',
url: '/api/test-origin-soon-to-be-remove',
headers: { 'x-forwarded-proto': 'http', 'x-forwarded-host': 'test.pix.org' },
};

it('returns an empty string because the calling application HTTP Origin', async function () {
// given
const expectedData = 'http://test.pix.org';

// when
const response = await server.inject(options);

// then
expect(response.statusCode).to.equal(200);
expect(response.result).to.deep.equal(expectedData);
});
});
});
27 changes: 27 additions & 0 deletions api/tests/shared/acceptance/application/healthcheck.route.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createServer, expect } from '../../../test-helper.js';

describe('Acceptance | Shared | Application | Route | healthcheck', function () {
let server;

beforeEach(async function () {
server = await createServer();
});

describe('GET /api/healthcheck/forwarded-origin', function () {
it('returns an HTTP status code 200 with the forwarded origin', async function () {
// given
const options = {
method: 'GET',
url: '/api/healthcheck/forwarded-origin',
headers: { 'x-forwarded-proto': 'https', 'x-forwarded-host': 'app.pix.org' },
};

// when
const response = await server.inject(options);

// then
expect(response.statusCode).to.equal(200);
expect(response.result).to.equal('https://app.pix.org');
});
});
});

0 comments on commit 2f1f0fb

Please sign in to comment.