import * as sinon from 'sinon' import { cardSession, orderSession } from '../../../src/sessions' import Card from '../../../__fixtures__/payments/card' import Faker from 'faker' import JohnDoe from '../../../__fixtures__/user' import Merchant from '../../../src/models/merchant' import MockPaymentAPI from '../../../__mocks__/apis/payment' import MockUserAPI from '../../../__mocks__/apis/user' import { PaymentAPI } from '@oyst/utils/lib/api/payment' import { RequestError } from '@oyst/utils/lib/request' import Server from '../../helpers/server' import UUID from 'uuid/v1' import { UserAPI } from '@oyst/utils/lib/api/user' import { merge } from 'ramda' const options = { method: 'POST', payload: { address: { city: 'Paris', company_name: 'Oyst', country: 'France', first_name: 'John', label: 'work', last_name: 'Doe', postcode: '75002', street: '4 rue du Caire' }, email: Faker.internet.email(), first_name: Faker.name.firstName(), last_name: Faker.name.lastName() }, url: '/users' } const userID = UUID() const sessionID = UUID() describe('`POST` /users', () => { it('should return a successfull user', 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, 'oyst-user': userID } await orderSession.setData(id, { product_reference: 'fake', variation_reference: 'fake' }) await orderSession.setData(id, { phone: '+33628437618' }, 'phone') const paymentCheckCardStub = sinon.stub(PaymentAPI.prototype, 'checkBinCard').resolves({ data: { success: true } }) const userCheckCard = sinon.stub(UserAPI.prototype, 'isCardUsed').resolves({ data: { success: true } }) await cardSession.setData(id, { encrypted_card: 'fake' }) await orderSession.setData(id, { success: true }, 'mfa-validated') await new MockUserAPI().success().fixture({ user: JohnDoe }).create(options.payload) await new MockPaymentAPI().success().fixture({ card: Card }).addCard({}) const res = await Server.inject(merge(options, { headers })) as any expect(res.statusCode).toBe(200) paymentCheckCardStub.restore() userCheckCard.restore() } catch (err) { console.error(err) expect(err).toBeNull() } }) it('Fails because mfa is not validated', 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, 'oyst-user': userID } await orderSession.setData(id, { product_reference: 'fake', variation_reference: 'fake' }) await orderSession.setData(id, { phone: '+33628437618' }, 'phone') await cardSession.setData(id, { encrypted_card: 'fake' }) await new MockUserAPI().success().fixture({ user: JohnDoe }).create(options.payload) await new MockPaymentAPI().success().fixture({ card: Card }).addCard({}) const res = await Server.inject(merge(options, { headers })) as any expect(true).toBeFalsy() } catch (err) { expect(err.statusCode).toBe(403) expect(err).not.toBeNull() } }) it('Fails because oyst-session 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')}` } await orderSession.setData(id, { product_reference: 'fake', variation_reference: 'fake' }) await Server.inject(merge(options, { headers })) } catch (err) { expect(err.statusCode).toBe(400) expect(err).not.toBeNull() } }) })