@labdigital/commercetools-mock 2.10.0 → 2.11.0

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.10.0",
4
+ "version": "2.11.0",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
@@ -0,0 +1,55 @@
1
+ import { describe, it, expect, beforeEach } from 'vitest'
2
+ import express from 'express'
3
+ import supertest from 'supertest'
4
+ import { OAuth2Server } from './server'
5
+
6
+ describe('OAuth2Server', () => {
7
+ let app: express.Express
8
+ let server: OAuth2Server
9
+
10
+ beforeEach(() => {
11
+ server = new OAuth2Server({ enabled: true, validate: false })
12
+ app = express()
13
+ app.use(server.createRouter())
14
+ })
15
+
16
+ describe('POST /token', () => {
17
+ it('should return a token for valid client credentials', async () => {
18
+ const response = await supertest(app)
19
+ .post('/token')
20
+ .auth('validClientId', 'validClientSecret')
21
+ .query({ grant_type: 'client_credentials' })
22
+ .send()
23
+
24
+ const body = await response.body
25
+
26
+ expect(response.status, JSON.stringify(body)).toBe(200)
27
+ expect(body).toHaveProperty('access_token')
28
+ })
29
+ })
30
+
31
+ describe('POST /:projectKey/anonymous/token', () => {
32
+ it('should return a token for anonymous access', async () => {
33
+ const projectKey = 'test-project'
34
+
35
+ const response = await supertest(app)
36
+ .post(`/${projectKey}/anonymous/token`)
37
+ .auth('validClientId', 'validClientSecret')
38
+ .query({ grant_type: 'client_credentials' })
39
+ .send()
40
+
41
+ expect(response.status).toBe(200)
42
+ expect(response.body).toHaveProperty('access_token')
43
+
44
+ const matches = response.body.scope?.match(
45
+ /(customer_id|anonymous_id):([^\s]+)/
46
+ )
47
+ if (matches) {
48
+ expect(matches[1]).toBe('anonymous_id')
49
+ expect(matches[2]).toBeDefined()
50
+ } else {
51
+ expect(response.body.scope).toBe('')
52
+ }
53
+ })
54
+ })
55
+ })
@@ -250,14 +250,27 @@ export class OAuth2Server {
250
250
  response: Response,
251
251
  next: NextFunction
252
252
  ) {
253
- return next(
254
- new CommercetoolsError<InvalidClientError>(
255
- {
256
- code: 'invalid_client',
257
- message: 'Not implemented yet in commercetools-mock',
258
- },
259
- 401
253
+ const grantType = request.query.grant_type || request.body.grant_type
254
+ if (!grantType) {
255
+ return next(
256
+ new CommercetoolsError<InvalidRequestError>(
257
+ {
258
+ code: 'invalid_request',
259
+ message: 'Missing required parameter: grant_type.',
260
+ },
261
+ 400
262
+ )
260
263
  )
261
- )
264
+ }
265
+
266
+ if (grantType === 'client_credentials') {
267
+ const scope =
268
+ request.query.scope?.toString() || request.body.scope?.toString()
269
+
270
+ const anonymous_id = undefined
271
+
272
+ const token = this.store.getAnonymousToken(scope, anonymous_id)
273
+ return response.status(200).send(token)
274
+ }
262
275
  }
263
276
  }
@@ -1,4 +1,5 @@
1
1
  import { randomBytes } from 'crypto'
2
+ import { v4 as uuidv4 } from 'uuid'
2
3
 
3
4
  type Token = {
4
5
  access_token: string
@@ -26,14 +27,30 @@ export class OAuth2Store {
26
27
  return token
27
28
  }
28
29
 
30
+ getAnonymousToken(scope: string, anonymousId: string | undefined) {
31
+ if (!anonymousId) {
32
+ anonymousId = uuidv4()
33
+ }
34
+ const token: Token = {
35
+ access_token: randomBytes(16).toString('base64'),
36
+ token_type: 'Bearer',
37
+ expires_in: 172800,
38
+ scope: scope
39
+ ? `${scope} anonymous_id:${anonymousId}`
40
+ : `anonymous_id:${anonymousId}`,
41
+ }
42
+ this.tokens.push(token)
43
+ return token
44
+ }
45
+
29
46
  getCustomerToken(scope: string, customerId: string) {
30
47
  const token: Token = {
31
48
  access_token: randomBytes(16).toString('base64'),
32
49
  token_type: 'Bearer',
33
50
  expires_in: 172800,
34
51
  scope: scope
35
- ? `${scope} custome_id:${customerId}`
36
- : `customer_id: ${customerId}`,
52
+ ? `${scope} customer_id:${customerId}`
53
+ : `customer_id:${customerId}`,
37
54
  }
38
55
  this.tokens.push(token)
39
56
  return token