8
8
9
9
import assert from 'node:assert' ;
10
10
11
- import { ApplicationType } from '@logto/schemas' ;
11
+ import { ApplicationType , token } from '@logto/schemas' ;
12
12
import { noop , removeUndefinedKeys } from '@silverhand/essentials' ;
13
13
import { HTTPError } from 'ky' ;
14
14
@@ -18,6 +18,7 @@ import {
18
18
createApplicationSecret ,
19
19
deleteApplication ,
20
20
} from '#src/api/application.js' ;
21
+ import { getAuditLogs } from '#src/api/index.js' ;
21
22
import { createResource } from '#src/api/resource.js' ;
22
23
import { devFeatureTest , randomString , waitFor } from '#src/utils.js' ;
23
24
@@ -33,6 +34,23 @@ const [application, resource] = await Promise.all([
33
34
createResource ( ) ,
34
35
] ) ;
35
36
37
+ const getLogs = async ( ) =>
38
+ getAuditLogs (
39
+ new URLSearchParams ( {
40
+ logKey : `${ token . Type . ExchangeTokenBy } .${ token . ExchangeByType . ClientCredentials } ` ,
41
+ } )
42
+ ) ;
43
+
44
+ const expectLog = ( applicationId : string , secretName : string ) =>
45
+ expect . objectContaining ( {
46
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
47
+ payload : expect . objectContaining ( {
48
+ applicationId,
49
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
50
+ applicationSecret : expect . objectContaining ( { name : secretName } ) ,
51
+ } ) ,
52
+ } ) ;
53
+
36
54
afterAll ( async ( ) => {
37
55
await deleteApplication ( application . id ) . catch ( noop ) ;
38
56
} ) ;
@@ -155,11 +173,14 @@ devFeatureTest.describe('client authentication', () => {
155
173
} ) ;
156
174
157
175
it ( 'should pass when client credentials are valid in authorization header' , async ( ) => {
176
+ const application = await createApplication ( 'application' , ApplicationType . MachineToMachine ) ;
158
177
const secret = await createApplicationSecret ( {
159
178
applicationId : application . id ,
160
179
name : randomString ( ) ,
161
180
} ) ;
181
+ const beforeLogs = await getLogs ( ) ;
162
182
183
+ expect ( beforeLogs ) . not . toContainEqual ( expectLog ( application . id , secret . name ) ) ;
163
184
await expect (
164
185
post ( {
165
186
authorization : `Basic ${ Buffer . from ( `${ application . id } :${ secret . value } ` ) . toString (
@@ -170,14 +191,21 @@ devFeatureTest.describe('client authentication', () => {
170
191
) . resolves . toMatchObject ( {
171
192
token_type : 'Bearer' ,
172
193
} ) ;
194
+
195
+ const logs = await getLogs ( ) ;
196
+ expect ( logs ) . toContainEqual ( expectLog ( application . id , secret . name ) ) ;
197
+ await deleteApplication ( application . id ) ;
173
198
} ) ;
174
199
175
200
it ( 'should pass when client credentials are valid in body' , async ( ) => {
201
+ const application = await createApplication ( 'application' , ApplicationType . MachineToMachine ) ;
176
202
const secret = await createApplicationSecret ( {
177
203
applicationId : application . id ,
178
204
name : randomString ( ) ,
179
205
} ) ;
206
+ const beforeLogs = await getLogs ( ) ;
180
207
208
+ expect ( beforeLogs ) . not . toContainEqual ( expectLog ( application . id , secret . name ) ) ;
181
209
await expect (
182
210
post ( {
183
211
body : {
@@ -206,5 +234,9 @@ devFeatureTest.describe('client authentication', () => {
206
234
token_type : 'Bearer' ,
207
235
} ) ;
208
236
}
237
+
238
+ const logs = await getLogs ( ) ;
239
+ expect ( logs ) . toContainEqual ( expectLog ( application . id , secret . name ) ) ;
240
+ await deleteApplication ( application . id ) ;
209
241
} ) ;
210
242
} ) ;
0 commit comments