@labdigital/commercetools-mock 2.9.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/README.md +9 -4
- package/dist/index.cjs +51 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +51 -26
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/oauth/server.test.ts +55 -0
- package/src/oauth/server.ts +21 -8
- package/src/oauth/store.ts +19 -2
- package/src/repositories/cart.ts +9 -0
- package/src/services/cart.test.ts +24 -0
package/package.json
CHANGED
|
@@ -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
|
+
})
|
package/src/oauth/server.ts
CHANGED
|
@@ -250,14 +250,27 @@ export class OAuth2Server {
|
|
|
250
250
|
response: Response,
|
|
251
251
|
next: NextFunction
|
|
252
252
|
) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
}
|
package/src/oauth/store.ts
CHANGED
|
@@ -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}
|
|
36
|
-
: `customer_id
|
|
52
|
+
? `${scope} customer_id:${customerId}`
|
|
53
|
+
: `customer_id:${customerId}`,
|
|
37
54
|
}
|
|
38
55
|
this.tokens.push(token)
|
|
39
56
|
return token
|
package/src/repositories/cart.ts
CHANGED
|
@@ -230,6 +230,15 @@ export class CartRepository extends AbstractResourceRepository<'cart'> {
|
|
|
230
230
|
// Update cart total price
|
|
231
231
|
resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
|
|
232
232
|
},
|
|
233
|
+
recalculate: () => {
|
|
234
|
+
// Dummy action when triggering a recalculation of the cart
|
|
235
|
+
//
|
|
236
|
+
// From commercetools documentation:
|
|
237
|
+
// This update action does not set any Cart field in particular,
|
|
238
|
+
// but it triggers several Cart updates to bring prices and discounts to the latest state.
|
|
239
|
+
// Those can become stale over time when no Cart updates have been performed for a while
|
|
240
|
+
// and prices on related Products have changed in the meanwhile.
|
|
241
|
+
},
|
|
233
242
|
addItemShippingAddress: (
|
|
234
243
|
context: RepositoryContext,
|
|
235
244
|
resource: Writable<Cart>,
|
|
@@ -303,6 +303,30 @@ describe('Cart Update Actions', () => {
|
|
|
303
303
|
expect(response.body.lineItems).toHaveLength(0)
|
|
304
304
|
})
|
|
305
305
|
|
|
306
|
+
test('recalculate', async () => {
|
|
307
|
+
const product = await supertest(ctMock.app)
|
|
308
|
+
.post(`/dummy/products`)
|
|
309
|
+
.send(productDraft)
|
|
310
|
+
.then((x) => x.body)
|
|
311
|
+
|
|
312
|
+
assert(cart, 'cart not created')
|
|
313
|
+
|
|
314
|
+
const response = await supertest(ctMock.app)
|
|
315
|
+
.post(`/dummy/carts/${cart.id}`)
|
|
316
|
+
.send({
|
|
317
|
+
version: 1,
|
|
318
|
+
actions: [
|
|
319
|
+
{
|
|
320
|
+
action: 'recalculate',
|
|
321
|
+
updateProductData: true,
|
|
322
|
+
},
|
|
323
|
+
],
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
expect(response.status).toBe(200)
|
|
327
|
+
expect(response.body.version).toBe(1)
|
|
328
|
+
})
|
|
329
|
+
|
|
306
330
|
test('removeLineItem', async () => {
|
|
307
331
|
const product = await supertest(ctMock.app)
|
|
308
332
|
.post(`/dummy/products`)
|