1
1
import { authenticateApplication } from '../../../../lib/domain/usecases/authenticate-application.js' ;
2
+ import { PasswordNotMatching } from '../../../../src/identity-access-management/domain/errors.js' ;
3
+ import { config } from '../../../../src/shared/config.js' ;
2
4
import {
3
5
ApplicationScopeNotAllowedError ,
4
6
ApplicationWithInvalidClientIdError ,
5
7
ApplicationWithInvalidClientSecretError ,
6
8
} from '../../../../src/shared/domain/errors.js' ;
7
- import { catchErr , expect , sinon } from '../../../test-helper.js' ;
9
+ import { catchErr , domainBuilder , expect , sinon } from '../../../test-helper.js' ;
8
10
9
11
describe ( 'Unit | Usecase | authenticate-application' , function ( ) {
10
12
context ( 'when application is not found' , function ( ) {
11
13
it ( 'should throw an error' , async function ( ) {
12
- const client = {
14
+ const payload = {
13
15
clientId : Symbol ( 'id' ) ,
14
16
clientSecret : Symbol ( 'secret' ) ,
15
17
} ;
16
18
17
- const err = await catchErr ( authenticateApplication ) ( client ) ;
19
+ const clientApplicationRepository = {
20
+ findByClientId : sinon . stub ( ) ,
21
+ } ;
22
+ clientApplicationRepository . findByClientId . withArgs ( payload . clientId ) . resolves ( undefined ) ;
23
+
24
+ const err = await catchErr ( authenticateApplication ) ( { ...payload , clientApplicationRepository } ) ;
18
25
19
26
expect ( err ) . to . be . instanceOf ( ApplicationWithInvalidClientIdError ) ;
20
27
} ) ;
@@ -23,48 +30,111 @@ describe('Unit | Usecase | authenticate-application', function () {
23
30
context ( 'when application is found' , function ( ) {
24
31
context ( 'when client secrets are different' , function ( ) {
25
32
it ( 'should throw an error' , async function ( ) {
26
- const client = {
33
+ const payload = {
27
34
clientId : 'test-apimOsmoseClientId' ,
28
- clientSecret : Symbol ( 'toto' ) ,
35
+ clientSecret : 'mauvais-secret' ,
36
+ } ;
37
+
38
+ const clientApplicationRepository = {
39
+ findByClientId : sinon . stub ( ) ,
29
40
} ;
41
+ const application = domainBuilder . buildClientApplication ( {
42
+ name : 'test-apimOsmoseClientId' ,
43
+ clientSecret : 'mon-secret' ,
44
+ scopes : [ ] ,
45
+ } ) ;
46
+ clientApplicationRepository . findByClientId . withArgs ( payload . clientId ) . resolves ( application ) ;
30
47
31
- const err = await catchErr ( authenticateApplication ) ( client ) ;
48
+ const cryptoService = {
49
+ checkPassword : sinon . stub ( ) ,
50
+ } ;
51
+ cryptoService . checkPassword
52
+ . withArgs ( { password : payload . clientSecret , passwordHash : application . clientSecret } )
53
+ . rejects ( new PasswordNotMatching ( ) ) ;
54
+
55
+ const err = await catchErr ( authenticateApplication ) ( { ...payload , clientApplicationRepository, cryptoService } ) ;
32
56
33
57
expect ( err ) . to . be . instanceOf ( ApplicationWithInvalidClientSecretError ) ;
34
58
} ) ;
35
59
} ) ;
36
60
37
61
context ( 'when client scopes are different' , function ( ) {
38
62
it ( 'should throw an error' , async function ( ) {
39
- const client = {
63
+ const payload = {
40
64
clientId : 'test-apimOsmoseClientId' ,
41
- clientSecret : 'test-apimOsmoseClientSecret ' ,
65
+ clientSecret : 'bon-secret ' ,
42
66
scope : 'mauvais-scope' ,
43
67
} ;
44
68
45
- const err = await catchErr ( authenticateApplication ) ( client ) ;
69
+ const clientApplicationRepository = {
70
+ findByClientId : sinon . stub ( ) ,
71
+ } ;
72
+ const application = domainBuilder . buildClientApplication ( {
73
+ name : 'test-apimOsmoseClientId' ,
74
+ clientSecret : 'bon-secret' ,
75
+ scopes : [ 'bon-scope' ] ,
76
+ } ) ;
77
+ clientApplicationRepository . findByClientId . withArgs ( payload . clientId ) . resolves ( application ) ;
78
+
79
+ const cryptoService = {
80
+ checkPassword : sinon . stub ( ) ,
81
+ } ;
82
+ cryptoService . checkPassword
83
+ . withArgs ( { password : payload . clientSecret , passwordHash : application . clientSecret } )
84
+ . resolves ( ) ;
85
+
86
+ const err = await catchErr ( authenticateApplication ) ( { ...payload , clientApplicationRepository, cryptoService } ) ;
46
87
47
88
expect ( err ) . to . be . instanceOf ( ApplicationScopeNotAllowedError ) ;
48
89
} ) ;
49
90
} ) ;
50
91
51
92
context ( 'when given information is correct' , function ( ) {
52
93
it ( 'should return created token' , async function ( ) {
53
- const client = {
94
+ const payload = {
54
95
clientId : 'test-apimOsmoseClientId' ,
55
- clientSecret : 'test-apimOsmoseClientSecret' ,
56
- scope : 'organizations-certifications-result' ,
96
+ clientSecret : 'bon-secret' ,
97
+ scope : 'bon-scope' ,
98
+ } ;
99
+
100
+ const clientApplicationRepository = {
101
+ findByClientId : sinon . stub ( ) ,
102
+ } ;
103
+ const application = domainBuilder . buildClientApplication ( {
104
+ name : 'mon-application' ,
105
+ clientId : 'test-apimOsmoseClientId' ,
106
+ clientSecret : 'bon-secret' ,
107
+ scopes : [ 'bon-scope' ] ,
108
+ } ) ;
109
+ clientApplicationRepository . findByClientId . withArgs ( payload . clientId ) . resolves ( application ) ;
110
+
111
+ const cryptoService = {
112
+ checkPassword : sinon . stub ( ) ,
57
113
} ;
114
+ cryptoService . checkPassword
115
+ . withArgs ( { password : payload . clientSecret , passwordHash : application . clientSecret } )
116
+ . resolves ( ) ;
58
117
59
118
const tokenService = {
60
119
createAccessTokenFromApplication : sinon . stub ( ) ,
61
120
} ;
62
121
const expectedToken = Symbol ( 'Mon Super token' ) ;
63
122
tokenService . createAccessTokenFromApplication
64
- . withArgs ( client . clientId , 'livretScolaire' , client . scope , 'test-secretOsmose' , '4h' )
123
+ . withArgs (
124
+ application . clientId ,
125
+ application . name ,
126
+ payload . scope ,
127
+ config . authentication . secret ,
128
+ config . authentication . accessTokenLifespanMs ,
129
+ )
65
130
. resolves ( expectedToken ) ;
66
131
67
- const token = await authenticateApplication ( { ...client , tokenService } ) ;
132
+ const token = await authenticateApplication ( {
133
+ ...payload ,
134
+ tokenService,
135
+ clientApplicationRepository,
136
+ cryptoService,
137
+ } ) ;
68
138
69
139
expect ( token ) . to . be . equal ( expectedToken ) ;
70
140
} ) ;
0 commit comments