From 515b0738343bea1ca066bd012ee7358123faa22b Mon Sep 17 00:00:00 2001 From: Rodel Date: Tue, 19 Oct 2021 20:34:43 +0800 Subject: [PATCH] Added tests for Refunds API --- .../tests/e2e/api-core-tests/data/index.js | 2 + .../tests/e2e/api-core-tests/data/refund.js | 18 +++ .../e2e/api-core-tests/endpoints/index.js | 2 + .../e2e/api-core-tests/endpoints/refunds.js | 60 +++++++++ .../tests/refunds/refunds.test.js | 123 ++++++++++++++++++ 5 files changed, 205 insertions(+) create mode 100644 plugins/woocommerce/tests/e2e/api-core-tests/data/refund.js create mode 100644 plugins/woocommerce/tests/e2e/api-core-tests/endpoints/refunds.js create mode 100644 plugins/woocommerce/tests/e2e/api-core-tests/tests/refunds/refunds.test.js diff --git a/plugins/woocommerce/tests/e2e/api-core-tests/data/index.js b/plugins/woocommerce/tests/e2e/api-core-tests/data/index.js index b944044d8dc..2bfd95b1b2e 100644 --- a/plugins/woocommerce/tests/e2e/api-core-tests/data/index.js +++ b/plugins/woocommerce/tests/e2e/api-core-tests/data/index.js @@ -1,5 +1,6 @@ const { order, getOrderExample } = require('./order'); const { coupon } = require('./coupon'); +const { refund } = require('./refund'); const shared = require('./shared'); module.exports = { @@ -7,4 +8,5 @@ module.exports = { getOrderExample, coupon, shared, + refund, }; diff --git a/plugins/woocommerce/tests/e2e/api-core-tests/data/refund.js b/plugins/woocommerce/tests/e2e/api-core-tests/data/refund.js new file mode 100644 index 00000000000..13a35f0cb58 --- /dev/null +++ b/plugins/woocommerce/tests/e2e/api-core-tests/data/refund.js @@ -0,0 +1,18 @@ +/** + * A basic refund. + * + * For more details on the order refund properties, see: + * + * https://woocommerce.github.io/woocommerce-rest-api-docs/#order-refund-properties + * + */ +const refund = { + api_refund: false, + amount: '1.00', + reason: 'Late delivery refund.', + line_items: [], +}; + +module.exports = { + refund: refund, +}; diff --git a/plugins/woocommerce/tests/e2e/api-core-tests/endpoints/index.js b/plugins/woocommerce/tests/e2e/api-core-tests/endpoints/index.js index 1b36341726a..43953654d8c 100644 --- a/plugins/woocommerce/tests/e2e/api-core-tests/endpoints/index.js +++ b/plugins/woocommerce/tests/e2e/api-core-tests/endpoints/index.js @@ -1,9 +1,11 @@ const { ordersApi } = require('./orders'); const { couponsApi } = require('./coupons'); const { productsApi } = require('./products'); +const { refundsApi } = require('./refunds'); module.exports = { ordersApi, couponsApi, productsApi, + refundsApi, }; diff --git a/plugins/woocommerce/tests/e2e/api-core-tests/endpoints/refunds.js b/plugins/woocommerce/tests/e2e/api-core-tests/endpoints/refunds.js new file mode 100644 index 00000000000..8a958fee3d0 --- /dev/null +++ b/plugins/woocommerce/tests/e2e/api-core-tests/endpoints/refunds.js @@ -0,0 +1,60 @@ +/** + * Internal dependencies + */ +const { + getRequest, + postRequest, + putRequest, + deleteRequest, +} = require( '../utils/request' ); + +/** + * WooCommerce Refunds endpoints. + * + * https://woocommerce.github.io/woocommerce-rest-api-docs/#refunds + */ +const refundsApi = { + name: 'Refunds', + create: { + name: 'Create a refund', + method: 'POST', + path: 'orders//refunds', + responseCode: 201, + refund: async ( orderId, refundDetails ) => + postRequest( `orders/${ orderId }/refunds`, refundDetails ), + }, + retrieve: { + name: 'Retrieve a refund', + method: 'GET', + path: 'orders//refunds/', + responseCode: 200, + refund: async ( orderId, refundId ) => + getRequest( `orders/${ orderId }/refunds/${ refundId }` ), + }, + listAll: { + name: 'List all refunds', + method: 'GET', + path: 'orders//refunds', + responseCode: 200, + refunds: async ( orderId ) => + getRequest( `orders/${ orderId }/refunds` ), + }, + delete: { + name: 'Delete a refund', + method: 'DELETE', + path: 'orders//refunds/', + responseCode: 200, + payload: { + force: false, + }, + refund: async ( orderId, refundId, deletePermanently ) => + deleteRequest( + `orders/${ orderId }/refunds/${ refundId }`, + deletePermanently + ), + }, +}; + +module.exports = { + refundsApi: refundsApi, +}; diff --git a/plugins/woocommerce/tests/e2e/api-core-tests/tests/refunds/refunds.test.js b/plugins/woocommerce/tests/e2e/api-core-tests/tests/refunds/refunds.test.js new file mode 100644 index 00000000000..12f7ff72b3d --- /dev/null +++ b/plugins/woocommerce/tests/e2e/api-core-tests/tests/refunds/refunds.test.js @@ -0,0 +1,123 @@ +const { refundsApi } = require( '../../endpoints/refunds' ); +const { ordersApi } = require( '../../endpoints/orders' ); +const { productsApi } = require( '../../endpoints/products' ); +const { refund } = require( '../../data' ); +const { expect } = require( '@jest/globals' ); + +/** + * Tests for the WooCommerce Refunds API. + * + * @group api + * @group refunds + * + */ +describe( 'Refunds API tests', () => { + let expectedRefund; + let orderId; + let productId; + + beforeAll( async () => { + // Create a product and save its product ID + const product = { + name: 'Simple Product for Refunds API tests', + regular_price: '100', + }; + const createProductResponse = await productsApi.create.product( + product + ); + productId = createProductResponse.body.id; + + // Create an order with a product line item, and save its Order ID + const order = { + status: 'pending', + line_items: [ + { + product_id: productId, + }, + ], + }; + const createOrderResponse = await ordersApi.create.order( order ); + orderId = createOrderResponse.body.id; + + // Setup the expected refund object + expectedRefund = { + ...refund, + line_items: [ + { + product_id: productId, + }, + ], + }; + } ); + + afterAll( async () => { + // Cleanup the created product and order + await productsApi.delete.product( productId, true ); + await ordersApi.delete.order( orderId, true ); + } ); + + it( 'can create a refund', async () => { + const { status, body } = await refundsApi.create.refund( + orderId, + expectedRefund + ); + expect( status ).toEqual( refundsApi.create.responseCode ); + expect( body.id ).toBeDefined(); + + // Save the refund ID + expectedRefund.id = body.id; + + // Verify that the order was refunded. + const getOrderResponse = await ordersApi.retrieve.order( orderId ); + expect( getOrderResponse.body.refunds ).toHaveLength( 1 ); + expect( getOrderResponse.body.refunds[ 0 ].id ).toEqual( + expectedRefund.id + ); + expect( getOrderResponse.body.refunds[ 0 ].reason ).toEqual( + expectedRefund.reason + ); + expect( getOrderResponse.body.refunds[ 0 ].total ).toEqual( + `-${ expectedRefund.amount }` + ); + } ); + + it( 'can retrieve a refund', async () => { + const { status, body } = await refundsApi.retrieve.refund( + orderId, + expectedRefund.id + ); + + expect( status ).toEqual( refundsApi.retrieve.responseCode ); + expect( body.id ).toEqual( expectedRefund.id ); + } ); + + it( 'can list all refunds', async () => { + const { status, body } = await refundsApi.listAll.refunds( orderId ); + + expect( status ).toEqual( refundsApi.listAll.responseCode ); + expect( body ).toHaveLength( 1 ); + expect( body[ 0 ].id ).toEqual( expectedRefund.id ); + } ); + + it( 'can delete a refund', async () => { + const { status, body } = await refundsApi.delete.refund( + orderId, + expectedRefund.id, + true + ); + + expect( status ).toEqual( refundsApi.delete.responseCode ); + expect( body.id ).toEqual( expectedRefund.id ); + + // Verify that the refund cannot be retrieved + const retrieveRefundResponse = await refundsApi.retrieve.refund( + orderId, + expectedRefund.id + ); + expect( retrieveRefundResponse.status ).toEqual( 404 ); + + // Verify that the order no longer has a refund + const retrieveOrderResponse = await ordersApi.retrieve.order( orderId ); + expect( retrieveOrderResponse.body.refunds ).toHaveLength( 0 ); + } ); +} );