UNPKG

pagaris

Version:

Pagaris API client for Node

95 lines (82 loc) 3.16 kB
const { Pagaris, Client, errors, assert } = require('./helpers') describe('Client', function() { it('is accessible', function() { assert.doesNotThrow(function() { Client }) assert(Client instanceof Function) }) context('get', function () { it('can result in success and return the body', async function () { let response = await Client.get('orders') assert(response) let orders = response.orders assert(orders) assert(() => { orders.length > 0 }) const orderId = '495bcec5-fc79-4bdd-b826-881e24c145e7' let order = orders.find(o => o.id === orderId) assert(order) assert.equal(order.id, orderId) assert.equal(order.amount, '3000.0') assert.equal(order.status, 'cancelled') }) context('can result in errors', function () { it('returns UnauthorizedError', async function () { Pagaris.applicationId = 'incorrect' Pagaris.privateKey = 'incorrect' await Client.get('orders').catch(err => { assert(() => { err instanceof Errors.UnauthorizedError }) }) }) it('returns NotFoundError', async function () { await Client.get('orders/not-an-id').catch(err => { assert(() => { err instanceof Errors.NotFoundError }) }) }) }) }) context('post', function () { it('can result in success and return the body', async function () { let response = await Client.post('orders', { order: { amount: 5544.33 } }) assert(response) let order = response.order assert(order) assert(order.id) assert.equal(order.amount, '5544.33') }) it('can result in errors', async function () { await Client.post('orders', { order: { metadata: { a: 'b' } } }) .catch(err => { assert(() => { err instanceof Errors.UnprocessableEntityError }) }) }) }) context('put', function () { it('can result in success and return the body', async function () { const confirmableOrderId = 'a818358c-4abc-4947-b5dd-815745f6d24f' let response = await Client.put(`orders/${confirmableOrderId}/confirm`) assert(response) let order = response.order assert(order) assert.equal(order.id, confirmableOrderId) assert.equal(order.status, 'confirmed') const cancellableOrderId = '25d0c74d-27b2-4efa-8a7b-4eaaabd30035' response = await Client.put(`orders/${cancellableOrderId}/cancel`) assert(response) order = response.order assert(order) assert.equal(order.id, cancellableOrderId) assert.equal(order.status, 'cancelled') }) it('can result in errors', async function () { // This Order can't be confirmed or cancelled, since it's cancelled const cancelledOrderId = '77490733-a293-4cd5-bf2c-8438df772399' await Client.put(`orders/${cancelledOrderId}/confirm`).catch(err => { assert(() => { err instanceof Errors.UnauthorizedError }) }) await Client.put(`orders/${cancelledOrderId}/cancel`).catch(err => { assert(() => { err instanceof Errors.UnauthorizedError }) }) }) }) })