Merge pull request #30982 from woocommerce/add/api-test-refunds

Add Core API tests for Refunds API
This commit is contained in:
Greg 2021-10-21 11:29:44 -06:00 committed by GitHub
commit 7f904a7d91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 208 additions and 4 deletions

View File

@ -1,7 +1,7 @@
# Unreleased
- Added API tests for the Coupons API.
# 0.1.0
- Initial/beta release
## Added
- Coupons API Tests
- Refunds API Tests

View File

@ -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,
};

View File

@ -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,
};

View File

@ -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,
};

View File

@ -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/<id>/refunds',
responseCode: 201,
refund: async ( orderId, refundDetails ) =>
postRequest( `orders/${ orderId }/refunds`, refundDetails ),
},
retrieve: {
name: 'Retrieve a refund',
method: 'GET',
path: 'orders/<id>/refunds/<refund_id>',
responseCode: 200,
refund: async ( orderId, refundId ) =>
getRequest( `orders/${ orderId }/refunds/${ refundId }` ),
},
listAll: {
name: 'List all refunds',
method: 'GET',
path: 'orders/<id>/refunds',
responseCode: 200,
refunds: async ( orderId ) =>
getRequest( `orders/${ orderId }/refunds` ),
},
delete: {
name: 'Delete a refund',
method: 'DELETE',
path: 'orders/<id>/refunds/<refund_id>',
responseCode: 200,
payload: {
force: false,
},
refund: async ( orderId, refundId, deletePermanently ) =>
deleteRequest(
`orders/${ orderId }/refunds/${ refundId }`,
deletePermanently
),
},
};
module.exports = {
refundsApi: refundsApi,
};

View File

@ -0,0 +1,122 @@
const { refundsApi } = require( '../../endpoints/refunds' );
const { ordersApi } = require( '../../endpoints/orders' );
const { productsApi } = require( '../../endpoints/products' );
const { refund } = require( '../../data' );
/**
* 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 );
} );
} );