-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50e2b57
commit 888eca2
Showing
15 changed files
with
183 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Joi from 'joi'; | ||
|
||
import { securityPreHandlers } from '../../../src/shared/application/security-pre-handlers.js'; | ||
import { identifiersType } from '../../../src/shared/domain/types/identifiers-type.js'; | ||
import { profileController } from './profile-controller.js'; | ||
|
||
const register = async function (server) { | ||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: '/api/users/{id}/profile', | ||
config: { | ||
pre: [ | ||
{ | ||
method: securityPreHandlers.checkRequestedUserIsAuthenticatedUser, | ||
assign: 'requestedUserIsAuthenticatedUser', | ||
}, | ||
], | ||
validate: { | ||
params: Joi.object({ | ||
id: identifiersType.userId, | ||
}), | ||
}, | ||
handler: profileController.getProfile, | ||
notes: [ | ||
'- **Cette route est restreinte aux utilisateurs authentifiés**\n' + | ||
"- Récupération du nombre total de Pix de l'utilisateur\n et de ses scorecards" + | ||
'- L’id demandé doit correspondre à celui de l’utilisateur authentifié', | ||
], | ||
tags: ['api', 'user', 'profile'], | ||
}, | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/api/admin/users/{id}/profile', | ||
config: { | ||
pre: [ | ||
{ | ||
method: (request, h) => | ||
securityPreHandlers.hasAtLeastOneAccessOf([ | ||
securityPreHandlers.checkAdminMemberHasRoleSuperAdmin, | ||
securityPreHandlers.checkAdminMemberHasRoleCertif, | ||
securityPreHandlers.checkAdminMemberHasRoleSupport, | ||
securityPreHandlers.checkAdminMemberHasRoleMetier, | ||
])(request, h), | ||
}, | ||
], | ||
validate: { | ||
params: Joi.object({ | ||
id: identifiersType.userId, | ||
}), | ||
}, | ||
handler: profileController.getProfileForAdmin, | ||
notes: [ | ||
"- Permet à un administrateur de récupérer le nombre total de Pix d'un utilisateur\n et de ses scorecards", | ||
], | ||
tags: ['api', 'user', 'profile'], | ||
}, | ||
}, | ||
]); | ||
}; | ||
|
||
const name = 'profile-api'; | ||
export { name, register }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as requestResponseUtils from '../../../src/shared/infrastructure/utils/request-response-utils.js'; | ||
import { usecases } from '../domain/usecases/index.js'; | ||
import * as profileSerializer from '../infrastructure/serializers/jsonapi/profile-serializer.js'; | ||
|
||
const getProfile = function (request, h, dependencies = { profileSerializer, requestResponseUtils }) { | ||
const authenticatedUserId = request.auth.credentials.userId; | ||
const locale = dependencies.requestResponseUtils.extractLocaleFromRequest(request); | ||
|
||
return usecases | ||
.getUserProfile({ userId: authenticatedUserId, locale }) | ||
.then(dependencies.profileSerializer.serialize); | ||
}; | ||
|
||
const getProfileForAdmin = function (request, h, dependencies = { profileSerializer, requestResponseUtils }) { | ||
const userId = request.params.id; | ||
const locale = dependencies.requestResponseUtils.extractLocaleFromRequest(request); | ||
|
||
return usecases.getUserProfile({ userId, locale }).then(dependencies.profileSerializer.serialize); | ||
}; | ||
|
||
const profileController = { | ||
getProfile, | ||
getProfileForAdmin, | ||
}; | ||
|
||
export { profileController }; |
4 changes: 2 additions & 2 deletions
4
api/lib/domain/usecases/get-user-profile.js → ...ofile/domain/usecases/get-user-profile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as profileRoute from './application/index.js'; | ||
|
||
const profileRoutes = [profileRoute]; | ||
|
||
export { profileRoutes }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as moduleUnderTest from '../../../../src/profile/application/index.js'; | ||
import { profileController } from '../../../../src/profile/application/profile-controller.js'; | ||
import { securityPreHandlers } from '../../../../src/shared/application/security-pre-handlers.js'; | ||
import { HttpTestServer, sinon } from '../../../../tests/test-helper.js'; | ||
|
||
describe('Unit | Router | user-router', function () { | ||
describe('GET /api/users/{id}/profile', function () { | ||
const method = 'GET'; | ||
const url = '/api/users/42/profile'; | ||
|
||
it('exists', async function () { | ||
// given | ||
sinon.stub(profileController, 'getProfile').returns('ok'); | ||
sinon | ||
.stub(securityPreHandlers, 'checkRequestedUserIsAuthenticatedUser') | ||
.callsFake((request, h) => h.response(true)); | ||
const httpTestServer = new HttpTestServer(); | ||
await httpTestServer.register(moduleUnderTest); | ||
|
||
// when | ||
await httpTestServer.request(method, url); | ||
|
||
// then | ||
sinon.assert.calledOnce(profileController.getProfile); | ||
}); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
api/tests/profile/unit/application/profile-controller_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { profileController } from '../../../../src/profile/application/profile-controller.js'; | ||
import { usecases } from '../../../../src/profile/domain/usecases/index.js'; | ||
import * as requestResponseUtils from '../../../../src/shared/infrastructure/utils/request-response-utils.js'; | ||
import { expect, hFake, sinon } from '../../../test-helper.js'; | ||
|
||
describe('Profile | Unit | Controller | profile-controller', function () { | ||
describe('#getProfile', function () { | ||
beforeEach(function () { | ||
sinon.stub(usecases, 'getUserProfile').resolves({ | ||
pixScore: 3, | ||
scorecards: [], | ||
}); | ||
}); | ||
|
||
it('should call the expected usecase', async function () { | ||
// given | ||
const profileSerializer = { serialize: sinon.stub() }; | ||
profileSerializer.serialize.resolves(); | ||
const userId = '12'; | ||
const locale = 'fr'; | ||
|
||
const request = { | ||
auth: { | ||
credentials: { | ||
userId, | ||
}, | ||
}, | ||
params: { | ||
id: userId, | ||
}, | ||
headers: { 'accept-language': locale }, | ||
}; | ||
|
||
// when | ||
await profileController.getProfile(request, hFake, { profileSerializer, requestResponseUtils }); | ||
|
||
// then | ||
expect(usecases.getUserProfile).to.have.been.calledWithExactly({ userId, locale }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...lizers/jsonapi/profile-serializer_test.js → ...lizers/jsonapi/profile-serializer_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.