Skip to content

Commit 2e2eeeb

Browse files
wip ajout route organizations
1 parent d56c7de commit 2e2eeeb

File tree

10 files changed

+149
-4
lines changed

10 files changed

+149
-4
lines changed

api/db/database-builder/factory/build-client-application.js

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function buildClientApplication({
1111
clientId = 'client-id',
1212
clientSecret = 'super-secret',
1313
scopes = ['scope1', 'scope2'],
14+
jurisdiction = { rules: [{ name: 'tags', value: ['MEDNUM'] }] },
1415
} = {}) {
1516
return databaseBuffer.pushInsertable({
1617
tableName: 'client_applications',
@@ -20,6 +21,7 @@ export function buildClientApplication({
2021
clientId,
2122
clientSecret: _getHashedSecret(clientSecret),
2223
scopes,
24+
jurisdiction,
2325
},
2426
});
2527
}

api/server.maddo.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { setupErrorHandling } from './config/server-setup-error-handling.js';
66
import { knex } from './db/knex-database-connection.js';
77
import * as authenticationRoutes from './lib/application/authentication/index.js';
88
import { authentication } from './lib/infrastructure/authentication.js';
9+
import * as organizationRoutes from './src/maddo/application/organizations-routes.js';
910
import * as replicationRoutes from './src/maddo/application/replications-routes.js';
1011
import { Metrics } from './src/monitoring/infrastructure/metrics.js';
1112
import * as healthcheckRoutes from './src/shared/application/healthcheck/index.js';
@@ -179,7 +180,7 @@ const setupAuthentication = function (server) {
179180
};
180181

181182
const setupRoutesAndPlugins = async function (server) {
182-
await server.register([...plugins, healthcheckRoutes, authenticationRoutes, replicationRoutes]);
183+
await server.register([...plugins, healthcheckRoutes, authenticationRoutes, replicationRoutes, organizationRoutes]);
183184
};
184185

185186
const setupOpenApiSpecification = async function (server) {
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
export class ClientApplication {
2-
constructor({ id, name, clientId, clientSecret, scopes }) {
2+
constructor({ id, name, clientId, clientSecret, scopes, jurisdiction }) {
33
this.id = id;
44
this.name = name;
55
this.clientId = clientId;
66
this.clientSecret = clientSecret;
77
this.scopes = scopes;
8+
this.jurisdiction = jurisdiction;
89
}
910
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function getOrganizations(request, h, dependencies = {}) {
2+
return h.response().code(200);
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { getOrganizations } from './organizations-controller.js';
2+
3+
const register = async function (server) {
4+
server.route([
5+
{
6+
method: 'GET',
7+
path: '/api/organizations',
8+
config: {
9+
auth: { access: { scope: 'meta' } },
10+
handler: getOrganizations,
11+
notes: ["- Retourne la liste des organizations auxquelles l'application client a droit"],
12+
tags: ['api', 'meta'],
13+
},
14+
},
15+
]);
16+
};
17+
18+
const name = 'maddo-meta-api';
19+
export { name, register };

api/src/maddo/application/replications-routes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ const register = async function (server) {
2525
]);
2626
};
2727

28-
const name = 'admin-campaign-participation-api';
28+
const name = 'maddo-replications-api';
2929
export { name, register };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function findIdsByTagNames() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
createMaddoServer,
3+
databaseBuilder,
4+
expect,
5+
generateValidRequestAuthorizationHeaderForApplication,
6+
} from '../../../test-helper.js';
7+
8+
describe('Acceptance | Maddo | Route | Organizations', function () {
9+
let server;
10+
11+
beforeEach(async function () {
12+
server = await createMaddoServer();
13+
});
14+
15+
describe('GET /api/organizations', function () {
16+
it('returns the list of all organizations of the client jurisdiction with an HTTP status code 200', async function () {
17+
// given
18+
const orgaInJurisdiction = databaseBuilder.factory.buildOrganization({ name: 'orga-in-jurisdiction' });
19+
const orgaAlsoInJurisdiction = databaseBuilder.factory.buildOrganization({ name: 'orga-also-in-jurisdiction' });
20+
databaseBuilder.factory.buildOrganization({ name: 'orga-not-in-jurisdiction' });
21+
22+
const tag = databaseBuilder.factory.buildTag();
23+
databaseBuilder.factory.buildOrganizationTag({ organizationId: orgaInJurisdiction.id, tagId: tag.id });
24+
databaseBuilder.factory.buildOrganizationTag({ organizationId: orgaAlsoInJurisdiction.id, tagId: tag.id });
25+
26+
const clientId = 'client';
27+
databaseBuilder.factory.buildClientApplication({
28+
clientId: 'client',
29+
jurisdiction: { rules: [{ name: 'tags', value: [tag.name] }] },
30+
});
31+
32+
await databaseBuilder.commit();
33+
34+
const options = {
35+
method: 'GET',
36+
url: '/api/organizations',
37+
headers: {
38+
authorization: generateValidRequestAuthorizationHeaderForApplication(clientId, 'pix-client', 'meta'),
39+
},
40+
};
41+
42+
// when
43+
const response = await server.inject(options);
44+
45+
// then
46+
expect(response.statusCode).to.equal(200);
47+
expect(response.result).to.deep.equal();
48+
});
49+
});
50+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { replications } from '../../../../src/maddo/infrastructure/repositories/replication-repository.js';
2+
import {
3+
createMaddoServer,
4+
datamartKnex,
5+
expect,
6+
generateValidRequestAuthorizationHeaderForApplication,
7+
} from '../../../test-helper.js';
8+
9+
describe('Maddo | Application | Acceptance | Replications', function () {
10+
describe('POST /api/replications/{replication}', function () {
11+
let server;
12+
13+
beforeEach(async function () {
14+
const schema = (t) => {
15+
t.string('firstName').notNullable();
16+
t.string('lastName').notNullable();
17+
};
18+
await datamartKnex.schema.dropTableIfExists('to-replicate');
19+
await datamartKnex.schema.createTable('to-replicate', schema);
20+
await datamartKnex.schema.dropTableIfExists('replication');
21+
await datamartKnex.schema.createTable('replication', schema);
22+
server = await createMaddoServer();
23+
});
24+
25+
it('should run given replication', async function () {
26+
// given
27+
const replication = 'my-replication';
28+
await datamartKnex('to-replicate').insert([
29+
{ firstName: 'first1', lastName: 'last1' },
30+
{ firstName: 'first2', lastName: 'last2' },
31+
]);
32+
33+
replications.push({
34+
name: 'my-replication',
35+
from: ({ datawarehouseKnex }) => {
36+
return datawarehouseKnex('to-replicate').select('*');
37+
},
38+
to: ({ datamartKnex }, chunk) => {
39+
return datamartKnex('replication').insert(chunk);
40+
},
41+
});
42+
43+
// when
44+
const response = await server.inject({
45+
method: 'POST',
46+
url: `/api/replications/${replication}?async=false`,
47+
headers: {
48+
authorization: generateValidRequestAuthorizationHeaderForApplication(
49+
'pix-client',
50+
'pix-client',
51+
'replication',
52+
),
53+
},
54+
});
55+
56+
// then
57+
expect(response.statusCode).to.equal(204);
58+
const { count } = await datamartKnex('replication').count().first();
59+
expect(count).to.equal(2);
60+
});
61+
});
62+
});

api/tests/tooling/domain-builder/factory/build-client-application.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export function buildClientApplication({
66
clientId = 'client-id',
77
clientSecret = 'super-secret',
88
scopes = ['scope1', 'scope2'],
9+
jurisdiction = [
10+
{
11+
rule: 'tags',
12+
value: ['MEDNUM'],
13+
},
14+
],
915
} = {}) {
10-
return new ClientApplication({ id, name, clientId, clientSecret, scopes });
16+
return new ClientApplication({ id, name, clientId, clientSecret, scopes, jurisdiction });
1117
}

0 commit comments

Comments
 (0)