import JohnDoe from '../../../../__fixtures__/user' import Merchant from '../../../../src/models/merchant' import MockCatalogAPI from '../../../../__mocks__/apis/catalog' import MockOrderAPI from '../../../../__mocks__/apis/order' import MockUserAPI from '../../../../__mocks__/apis/user' import Order from '../../../../__fixtures__/orders/aggregated' import Product from '../../../../__fixtures__/catalog/product' import Server from '../../../helpers/server' import UUID from 'uuid/v1' import { merge } from 'ramda' import { orderSession } from '../../../../src/sessions' const options: any = { method: 'PATCH' } const userID = UUID() const sessionID = UUID() describe('`PATCH` /orders/{orderId}/items/itemId to update an item', () => { it('should successfully set an item\'s quantity', async () => { try { const id = UUID() const merchant = await Merchant.create({ id }) const orderId = Order.order.id const itemId = Order.order.items[0].id options.url = `/v2/orders/${orderId}/items/${itemId}` await merchant.sign({ id }) const headers = { 'oyst-authorization': `Oyst ${new Buffer(JSON.stringify({ m: merchant.id, t: merchant.$url.token })).toString('base64')}`, 'oyst-session': sessionID, 'oyst-user': userID } await orderSession.setData(id, { product_reference: 'fake', variation_reference: 'fake', version: 2 }) await new MockCatalogAPI().success().fixture({ product: Product }).findByReferences({ merchant_id: merchant.id, product_reference: 'fake', variation_reference: 'fake' }) await new MockUserAPI().success().fixture({ user: JohnDoe }).find(userID) const quantity = 12 await new MockOrderAPI().success().fixture(Order).updateItem(orderId, itemId, { quantity }) const res = await Server.inject(merge(options, { headers, payload: { quantity } })) const { order } = res.result expect(res.statusCode).toBe(200) expect(order.items[0].quantity).toEqual(quantity) } catch (err) { expect(err).toBeNull() } }) it('should successfully set an item\'s quantity to 1', async () => { try { const id = UUID() const merchant = await Merchant.create({ id }) const orderId = Order.order.id const itemId = Order.order.items[0].id options.url = `/v2/orders/${orderId}/items/${itemId}` await merchant.sign({ id }) const headers = { 'oyst-authorization': `Oyst ${new Buffer(JSON.stringify({ m: merchant.id, t: merchant.$url.token })).toString('base64')}`, 'oyst-session': sessionID, 'oyst-user': userID } await orderSession.setData(id, { product_reference: 'fake', variation_reference: 'fake', version: 2 }) await new MockCatalogAPI().success().fixture({ product: Product }).findByReferences({ merchant_id: merchant.id, product_reference: 'fake', variation_reference: 'fake' }) await new MockUserAPI().success().fixture({ user: JohnDoe }).find(userID) const quantity = 1 await new MockOrderAPI().success().fixture(Order).updateItem(orderId, itemId, { quantity }) const res = await Server.inject(merge(options, { headers, payload: { quantity } })) const { order } = res.result expect(res.statusCode).toBe(200) expect(order.items[0].quantity).toEqual(quantity) } catch (err) { expect(err).toBeNull() } }) it('should fail to set an item\'s quantity to 0', async () => { try { const id = UUID() const merchant = await Merchant.create({ id }) const orderId = Order.order.id const itemId = Order.order.items[0].id options.url = `/v2/orders/${orderId}/items/${itemId}` await merchant.sign({ id }) const headers = { 'oyst-authorization': `Oyst ${new Buffer(JSON.stringify({ m: merchant.id, t: merchant.$url.token })).toString('base64')}`, 'oyst-session': sessionID, 'oyst-user': userID } await orderSession.setData(id, { product_reference: 'fake', variation_reference: 'fake', version: 2 }) await new MockCatalogAPI().success().fixture({ product: Product }).findByReferences({ merchant_id: merchant.id, product_reference: 'fake', variation_reference: 'fake' }) await new MockUserAPI().success().fixture({ user: JohnDoe }).find(userID) const quantity = 0 await new MockOrderAPI().success().fixture(Order).updateItem(orderId, itemId, { quantity }) const res = await Server.inject(merge(options, { headers, payload: { quantity } })) const { order } = res.result } catch (err) { expect(err.code).toEqual(400) expect(err.prefix).toEqual('ORDER') expect(err.error).toEqual({ message: 'quantity-must-be-positive' }) } }) it('should fail because oyst-user header is missing', async () => { try { const id = UUID() const merchant = await Merchant.create({ id }) await merchant.sign({ id }) const headers = { 'oyst-authorization': `Oyst ${new Buffer(JSON.stringify({ m: merchant.id, t: merchant.$url.token })).toString('base64')}`, 'oyst-session': sessionID } await orderSession.setData(id, { product_reference: 'fake', variation_reference: 'fake', version: 2 }) await Server.inject(merge(options, { headers })) } catch (err) { expect(err.statusCode).toBe(400) expect(err).not.toBeNull() } }) })