|
| 1 | +import { monitorPreHandlers } from '../../../../src/identity-access-management/application/monitor-pre-handlers.js'; |
| 2 | +import { generateHash } from '../../../../src/identity-access-management/infrastructure/utils/crypto.js'; |
| 3 | +import { expect, hFake, sinon } from '../../../test-helper.js'; |
| 4 | + |
| 5 | +describe('Unit | Identity Access Management | Application | monitor-pre-handlers', function () { |
| 6 | + describe('#monitorApiTokenRoute', function () { |
| 7 | + it('logs authentication attempt with grant type password', function () { |
| 8 | + // given |
| 9 | + const username = 'test@email.com'; |
| 10 | + const grant_type = 'password'; |
| 11 | + const scope = 'pix-app'; |
| 12 | + const hash = generateHash(username); |
| 13 | + const logger = { warn: sinon.stub() }; |
| 14 | + const request = { payload: { grant_type, username, scope } }; |
| 15 | + |
| 16 | + // when |
| 17 | + monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger }); |
| 18 | + |
| 19 | + // then |
| 20 | + expect(logger.warn).to.have.been.calledWith({ hash, grant_type, scope }, 'Authentication attempt'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('logs authentication attempt with grant type refresh token', async function () { |
| 24 | + // given |
| 25 | + const refresh_token = '123'; |
| 26 | + const grant_type = 'refresh_token'; |
| 27 | + const scope = 'pix-app'; |
| 28 | + const hash = generateHash(refresh_token); |
| 29 | + const logger = { warn: sinon.stub() }; |
| 30 | + const request = { payload: { grant_type, refresh_token, scope } }; |
| 31 | + |
| 32 | + // when |
| 33 | + monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger }); |
| 34 | + |
| 35 | + // then |
| 36 | + expect(logger.warn).to.have.been.calledWith({ hash, grant_type, scope }, 'Authentication attempt'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('logs authentication attempt with grant type unknown', async function () { |
| 40 | + // given |
| 41 | + const grant_type = 'unknown'; |
| 42 | + const logger = { warn: sinon.stub() }; |
| 43 | + const request = { payload: { foo: 'bar', grant_type } }; |
| 44 | + |
| 45 | + // when |
| 46 | + monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger }); |
| 47 | + |
| 48 | + // then |
| 49 | + expect(logger.warn).to.have.been.calledWith(request.payload, 'Authentication attempt with unknown method'); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments