@labdigital/commercetools-mock 2.12.0 → 2.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
3
  "author": "Michael van Tellingen",
4
- "version": "2.12.0",
4
+ "version": "2.12.2",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
@@ -0,0 +1,18 @@
1
+ import { CommercetoolsMock } from './index.js'
2
+ import { test } from 'vitest'
3
+
4
+ test('ctMock.authServer', async () => {
5
+ const ctMock = new CommercetoolsMock({
6
+ enableAuthentication: false,
7
+ validateCredentials: false,
8
+ apiHost: 'http://api.localhost',
9
+ })
10
+
11
+ ctMock.authStore().addToken({
12
+ token_type: 'Bearer',
13
+ access_token: 'foobar',
14
+ expires_in: 172800,
15
+ scope: 'my-project',
16
+ refresh_token: 'foobar',
17
+ })
18
+ })
package/src/ctMock.ts CHANGED
@@ -97,6 +97,10 @@ export class CommercetoolsMock {
97
97
  )
98
98
  }
99
99
 
100
+ authStore() {
101
+ return this._oauth2.store
102
+ }
103
+
100
104
  runServer(port = 3000, options?: AppOptions) {
101
105
  const server = this.app.listen(port, () => {
102
106
  console.info(`Mock server listening at http://localhost:${port}`)
@@ -23,6 +23,13 @@ type AuthRequest = Request & {
23
23
  clientSecret: string
24
24
  }
25
25
  }
26
+ export type Token = {
27
+ access_token: string
28
+ token_type: 'Bearer'
29
+ expires_in: number
30
+ scope: string
31
+ refresh_token?: string
32
+ }
26
33
 
27
34
  export class OAuth2Server {
28
35
  store: OAuth2Store
@@ -164,7 +171,8 @@ export class OAuth2Server {
164
171
  )
165
172
  return response.status(200).send(token)
166
173
  } else if (grantType === 'refresh_token') {
167
- const refreshToken = request.query.refresh_token?.toString()
174
+ const refreshToken =
175
+ request.query.refresh_token?.toString() || request.body.refresh_token
168
176
  if (!refreshToken) {
169
177
  return next(
170
178
  new CommercetoolsError<InvalidRequestError>(
@@ -17,6 +17,10 @@ export class OAuth2Store {
17
17
  this.validate = validate
18
18
  }
19
19
 
20
+ addToken(token: Token) {
21
+ this.tokens.push(token)
22
+ }
23
+
20
24
  getClientToken(clientId: string, clientSecret: string, scope?: string) {
21
25
  const token: Token = {
22
26
  access_token: randomBytes(16).toString('base64'),
@@ -25,7 +29,7 @@ export class OAuth2Store {
25
29
  scope: scope || 'todo',
26
30
  refresh_token: `my-project-${randomBytes(16).toString('base64')}`,
27
31
  }
28
- this.tokens.push(token)
32
+ this.addToken(token)
29
33
  return token
30
34
  }
31
35
 
@@ -42,7 +46,7 @@ export class OAuth2Store {
42
46
  : `anonymous_id:${anonymousId}`,
43
47
  refresh_token: `my-project-${randomBytes(16).toString('base64')}`,
44
48
  }
45
- this.tokens.push(token)
49
+ this.addToken(token)
46
50
  return token
47
51
  }
48
52
 
@@ -56,7 +60,7 @@ export class OAuth2Store {
56
60
  : `customer_id:${customerId}`,
57
61
  refresh_token: `my-project-${randomBytes(16).toString('base64')}`,
58
62
  }
59
- this.tokens.push(token)
63
+ this.addToken(token)
60
64
  return token
61
65
  }
62
66
 
@@ -69,7 +73,7 @@ export class OAuth2Store {
69
73
  ...existing,
70
74
  access_token: randomBytes(16).toString('base64'),
71
75
  }
72
- this.tokens.push(token)
76
+ this.addToken(token)
73
77
 
74
78
  // We don't want to return the refresh_token again
75
79
  return {