Merge pull request #31237 from woocommerce/fix/api-rest-response-code

Fix retrieving status code for API tests
This commit is contained in:
Ron Rennick 2021-11-17 12:40:24 -04:00 committed by GitHub
commit 66ad8774d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 143 additions and 126 deletions

View File

@ -7,23 +7,19 @@ const { HTTPClientFactory, Coupon } = require( '@woocommerce/api' );
* External dependencies * External dependencies
*/ */
const config = require( 'config' ); const config = require( 'config' );
const { const { it, describe, beforeAll } = require( '@jest/globals' );
it,
describe,
beforeAll,
} = require( '@jest/globals' );
/** /**
* Create the default coupon and tests interactions with it via the API. * Create the default coupon and tests interactions with it via the API.
*/ */
const runCouponApiTest = () => { const runCouponApiTest = () => {
describe('REST API > Coupon', () => { describe( 'REST API > Coupon', () => {
let client; let client;
let percentageCoupon; let percentageCoupon;
let coupon; let coupon;
let repository; let repository;
beforeAll(async () => { beforeAll( async () => {
percentageCoupon = config.get( 'coupons.percentage' ); percentageCoupon = config.get( 'coupons.percentage' );
const admin = config.get( 'users.admin' ); const admin = config.get( 'users.admin' );
const url = config.get( 'url' ); const url = config.get( 'url' );
@ -34,15 +30,17 @@ const runCouponApiTest = () => {
.create(); .create();
} ); } );
it('can create a coupon', async () => { it( 'can create a coupon', async () => {
repository = Coupon.restRepository( client ); repository = Coupon.restRepository( client );
// Check properties of the coupon in the create coupon response. // Check properties of the coupon in the create coupon response.
coupon = await repository.create( percentageCoupon ); coupon = await repository.create( percentageCoupon );
expect( coupon ).toEqual( expect.objectContaining( percentageCoupon ) ); expect( coupon ).toEqual(
}); expect.objectContaining( percentageCoupon )
);
} );
it('can retrieve a coupon', async () => { it( 'can retrieve a coupon', async () => {
const couponProperties = { const couponProperties = {
id: coupon.id, id: coupon.id,
code: percentageCoupon.code, code: percentageCoupon.code,
@ -51,12 +49,16 @@ const runCouponApiTest = () => {
}; };
// Read coupon directly from API to compare. // Read coupon directly from API to compare.
const response = await client.get( `/wc/v3/coupons/${coupon.id}` ); const response = await client.get(
expect( response.status ).toBe( 200 ); `/wc/v3/coupons/${ coupon.id }`
expect( response.data ).toEqual( expect.objectContaining( couponProperties ) ); );
}); expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( couponProperties )
);
} );
it('can update a coupon', async () => { it( 'can update a coupon', async () => {
const updatedCouponProperties = { const updatedCouponProperties = {
amount: '75.00', amount: '75.00',
discount_type: 'fixed_cart', discount_type: 'fixed_cart',
@ -66,19 +68,23 @@ const runCouponApiTest = () => {
await repository.update( coupon.id, updatedCouponProperties ); await repository.update( coupon.id, updatedCouponProperties );
// Check the coupon response for the updated values. // Check the coupon response for the updated values.
const response = await client.get( `/wc/v3/coupons/${coupon.id}` ); const response = await client.get(
expect( response.status ).toBe( 200 ); `/wc/v3/coupons/${ coupon.id }`
expect( response.data ).toEqual( expect.objectContaining( updatedCouponProperties ) ); );
}); expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( updatedCouponProperties )
);
} );
it('can delete a coupon', async () => { it( 'can delete a coupon', async () => {
// Delete the coupon // Delete the coupon
const status = await repository.delete( coupon.id ); const status = await repository.delete( coupon.id );
// If the delete is successful, the response comes back truthy // If the delete is successful, the response comes back truthy
expect( status ).toBeTruthy(); expect( status ).toBeTruthy();
}); } );
}); } );
}; };
module.exports = runCouponApiTest; module.exports = runCouponApiTest;

View File

@ -7,24 +7,20 @@ const { HTTPClientFactory, ExternalProduct } = require( '@woocommerce/api' );
* External dependencies * External dependencies
*/ */
const config = require( 'config' ); const config = require( 'config' );
const { const { it, describe, beforeAll } = require( '@jest/globals' );
it,
describe,
beforeAll,
} = require( '@jest/globals' );
/** /**
* Create an external product and retrieve via the API. * Create an external product and retrieve via the API.
*/ */
const runExternalProductAPITest = () => { const runExternalProductAPITest = () => {
// @todo: add a call to ensure pretty permalinks are enabled once settings api is in use. // @todo: add a call to ensure pretty permalinks are enabled once settings api is in use.
describe('REST API > External Product', () => { describe( 'REST API > External Product', () => {
let client; let client;
let defaultExternalProduct; let defaultExternalProduct;
let product; let product;
let repository; let repository;
beforeAll(async () => { beforeAll( async () => {
defaultExternalProduct = config.get( 'products.external' ); defaultExternalProduct = config.get( 'products.external' );
const admin = config.get( 'users.admin' ); const admin = config.get( 'users.admin' );
const url = config.get( 'url' ); const url = config.get( 'url' );
@ -35,15 +31,17 @@ const runExternalProductAPITest = () => {
.create(); .create();
} ); } );
it('can create an external product', async () => { it( 'can create an external product', async () => {
repository = ExternalProduct.restRepository( client ); repository = ExternalProduct.restRepository( client );
// Check properties of product in the create product response. // Check properties of product in the create product response.
product = await repository.create( defaultExternalProduct ); product = await repository.create( defaultExternalProduct );
expect( product ).toEqual( expect.objectContaining( defaultExternalProduct ) ); expect( product ).toEqual(
}); expect.objectContaining( defaultExternalProduct )
);
} );
it('can retrieve a raw external product', async () => { it( 'can retrieve a raw external product', async () => {
const rawProperties = { const rawProperties = {
id: product.id, id: product.id,
button_text: defaultExternalProduct.buttonText, button_text: defaultExternalProduct.buttonText,
@ -52,12 +50,16 @@ const runExternalProductAPITest = () => {
}; };
// Read product directly from api. // Read product directly from api.
const response = await client.get( `/wc/v3/products/${product.id}` ); const response = await client.get(
expect( response.status ).toBe( 200 ); `/wc/v3/products/${ product.id }`
expect( response.data ).toEqual( expect.objectContaining( rawProperties ) ); );
}); expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( rawProperties )
);
} );
it('can retrieve a transformed external product', async () => { it( 'can retrieve a transformed external product', async () => {
const transformedProperties = { const transformedProperties = {
...defaultExternalProduct, ...defaultExternalProduct,
id: product.id, id: product.id,
@ -66,14 +68,16 @@ const runExternalProductAPITest = () => {
// Read product via the repository. // Read product via the repository.
const transformed = await repository.read( product.id ); const transformed = await repository.read( product.id );
expect( transformed ).toEqual( expect.objectContaining( transformedProperties ) ); expect( transformed ).toEqual(
}); expect.objectContaining( transformedProperties )
);
} );
it('can delete an external product', async () => { it( 'can delete an external product', async () => {
const status = repository.delete( product.id ); const status = repository.delete( product.id );
expect( status ).toBeTruthy(); expect( status ).toBeTruthy();
}); } );
}); } );
}; };
module.exports = runExternalProductAPITest; module.exports = runExternalProductAPITest;

View File

@ -1,32 +1,32 @@
/** /**
* Internal dependencies * Internal dependencies
*/ */
const { HTTPClientFactory, GroupedProduct, SimpleProduct } = require( '@woocommerce/api' ); const {
HTTPClientFactory,
GroupedProduct,
SimpleProduct,
} = require( '@woocommerce/api' );
/** /**
* External dependencies * External dependencies
*/ */
const config = require( 'config' ); const config = require( 'config' );
const { const { it, describe, beforeAll } = require( '@jest/globals' );
it,
describe,
beforeAll,
} = require( '@jest/globals' );
/** /**
* Create an external product and retrieve via the API. * Create an external product and retrieve via the API.
*/ */
const runGroupedProductAPITest = () => { const runGroupedProductAPITest = () => {
// @todo: add a call to ensure pretty permalinks are enabled once settings api is in use. // @todo: add a call to ensure pretty permalinks are enabled once settings api is in use.
describe('REST API > Grouped Product', () => { describe( 'REST API > Grouped Product', () => {
let client; let client;
let defaultGroupedProduct; let defaultGroupedProduct;
let baseGroupedProduct; let baseGroupedProduct;
let product; let product;
let groupedProducts = []; const groupedProducts = [];
let repository; let repository;
beforeAll(async () => { beforeAll( async () => {
defaultGroupedProduct = config.get( 'products.grouped' ); defaultGroupedProduct = config.get( 'products.grouped' );
const admin = config.get( 'users.admin' ); const admin = config.get( 'users.admin' );
const url = config.get( 'url' ); const url = config.get( 'url' );
@ -38,13 +38,19 @@ const runGroupedProductAPITest = () => {
// Create the simple products to be grouped first. // Create the simple products to be grouped first.
repository = SimpleProduct.restRepository( client ); repository = SimpleProduct.restRepository( client );
for ( let c = 0; c < defaultGroupedProduct.groupedProducts.length; c++ ) { for (
product = await repository.create( defaultGroupedProduct.groupedProducts[ c ] ); let c = 0;
c < defaultGroupedProduct.groupedProducts.length;
c++
) {
product = await repository.create(
defaultGroupedProduct.groupedProducts[ c ]
);
groupedProducts.push( product.id ); groupedProducts.push( product.id );
} }
}); } );
it('can create a grouped product', async () => { it( 'can create a grouped product', async () => {
baseGroupedProduct = { baseGroupedProduct = {
...defaultGroupedProduct, ...defaultGroupedProduct,
groupedProducts, groupedProducts,
@ -53,38 +59,46 @@ const runGroupedProductAPITest = () => {
// Check properties of product in the create product response. // Check properties of product in the create product response.
product = await repository.create( baseGroupedProduct ); product = await repository.create( baseGroupedProduct );
expect( product ).toEqual( expect.objectContaining( baseGroupedProduct ) ); expect( product ).toEqual(
}); expect.objectContaining( baseGroupedProduct )
);
} );
it('can retrieve a raw grouped product', async () => { it( 'can retrieve a raw grouped product', async () => {
let rawProperties = { const rawProperties = {
id: product.id, id: product.id,
grouped_products: baseGroupedProduct.groupedProducts, grouped_products: baseGroupedProduct.groupedProducts,
...defaultGroupedProduct, ...defaultGroupedProduct,
}; };
delete rawProperties['groupedProducts']; delete rawProperties.groupedProducts;
// Read product directly from api. // Read product directly from api.
const response = await client.get( `/wc/v3/products/${product.id}` ); const response = await client.get(
expect( response.status ).toBe( 200 ); `/wc/v3/products/${ product.id }`
expect( response.data ).toEqual( expect.objectContaining( rawProperties ) ); );
}); expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual(
expect.objectContaining( rawProperties )
);
} );
it('can retrieve a transformed grouped product', async () => { it( 'can retrieve a transformed grouped product', async () => {
// Read product via the repository. // Read product via the repository.
const transformed = await repository.read( product.id ); const transformed = await repository.read( product.id );
expect( transformed ).toEqual( expect.objectContaining( baseGroupedProduct ) ); expect( transformed ).toEqual(
}); expect.objectContaining( baseGroupedProduct )
);
} );
it('can delete a grouped product', async () => { it( 'can delete a grouped product', async () => {
const status = repository.delete( product.id ); const status = repository.delete( product.id );
expect( status ).toBeTruthy(); expect( status ).toBeTruthy();
// Delete the simple "child" products. // Delete the simple "child" products.
groupedProducts.forEach( ( productId ) => { groupedProducts.forEach( ( productId ) => {
repository.delete( productId ); repository.delete( productId );
}); } );
}); } );
}); } );
}; };
module.exports = runGroupedProductAPITest; module.exports = runGroupedProductAPITest;

View File

@ -1,28 +1,24 @@
/** /**
* Internal dependencies * Internal dependencies
*/ */
const { HTTPClientFactory, Order } = require( '@woocommerce/api' ); const { HTTPClientFactory, Order } = require( '@woocommerce/api' );
/** /**
* External dependencies * External dependencies
*/ */
const config = require( 'config' ); const config = require( 'config' );
const { const { it, describe, beforeAll } = require( '@jest/globals' );
it,
describe,
beforeAll,
} = require( '@jest/globals' );
/** /**
* Creates an order and tests interactions with it via the API. * Creates an order and tests interactions with it via the API.
*/ */
const runOrderApiTest = () => { const runOrderApiTest = () => {
describe('REST API > Order', () => { describe( 'REST API > Order', () => {
let client; let client;
let order; let order;
let repository; let repository;
beforeAll(async () => { beforeAll( async () => {
order = config.get( 'orders.basicPaidOrder' ); order = config.get( 'orders.basicPaidOrder' );
const admin = config.get( 'users.admin' ); const admin = config.get( 'users.admin' );
const url = config.get( 'url' ); const url = config.get( 'url' );
@ -33,15 +29,15 @@ const runOrderApiTest = () => {
.create(); .create();
} ); } );
it('can create an order', async () => { it( 'can create an order', async () => {
repository = Order.restRepository( client ); repository = Order.restRepository( client );
// Check properties of the order in the create order response. // Check properties of the order in the create order response.
order = await repository.create( order ); order = await repository.create( order );
expect( order ).toEqual( expect.objectContaining( order ) ); expect( order ).toEqual( expect.objectContaining( order ) );
}); } );
it('can retrieve an order', async () => { it( 'can retrieve an order', async () => {
const orderProperties = { const orderProperties = {
id: order.id, id: order.id,
payment_method: order.paymentMethod, payment_method: order.paymentMethod,
@ -49,12 +45,14 @@ const runOrderApiTest = () => {
}; };
// Read order directly from API to compare. // Read order directly from API to compare.
const response = await client.get( `/wc/v3/orders/${order.id}` ); const response = await client.get( `/wc/v3/orders/${ order.id }` );
expect( response.status ).toBe( 200 ); expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( orderProperties ) ); expect( response.data ).toEqual(
}); expect.objectContaining( orderProperties )
);
} );
it('can update an order', async () => { it( 'can update an order', async () => {
const updatedOrderProperties = { const updatedOrderProperties = {
payment_method: 'bacs', payment_method: 'bacs',
status: 'completed', status: 'completed',
@ -63,19 +61,21 @@ const runOrderApiTest = () => {
await repository.update( order.id, updatedOrderProperties ); await repository.update( order.id, updatedOrderProperties );
// Check the order response for the updated values. // Check the order response for the updated values.
const response = await client.get( `/wc/v3/orders/${order.id}` ); const response = await client.get( `/wc/v3/orders/${ order.id }` );
expect( response.status ).toBe( 200 ); expect( response.statusCode ).toBe( 200 );
expect( response.data ).toEqual( expect.objectContaining( updatedOrderProperties ) ); expect( response.data ).toEqual(
}); expect.objectContaining( updatedOrderProperties )
);
} );
it('can delete an order', async () => { it( 'can delete an order', async () => {
// Delete the order // Delete the order
const status = await repository.delete( order.id ); const status = await repository.delete( order.id );
// If the delete is successful, the response comes back truthy // If the delete is successful, the response comes back truthy
expect( status ).toBeTruthy(); expect( status ).toBeTruthy();
}); } );
}); } );
}; };
module.exports = runOrderApiTest; module.exports = runOrderApiTest;

View File

@ -7,11 +7,7 @@ const { HTTPClientFactory } = require( '@woocommerce/api' );
* External dependencies * External dependencies
*/ */
const config = require( 'config' ); const config = require( 'config' );
const { const { it, describe, beforeAll } = require( '@jest/globals' );
it,
describe,
beforeAll,
} = require( '@jest/globals' );
/** /**
* Create the default coupon and tests interactions with it via the API. * Create the default coupon and tests interactions with it via the API.
@ -20,7 +16,7 @@ const runTelemetryAPITest = () => {
describe( 'REST API > Telemetry', () => { describe( 'REST API > Telemetry', () => {
let client; let client;
beforeAll(async () => { beforeAll( async () => {
const admin = config.get( 'users.admin' ); const admin = config.get( 'users.admin' );
const url = config.get( 'url' ); const url = config.get( 'url' );
@ -30,29 +26,26 @@ const runTelemetryAPITest = () => {
.create(); .create();
} ); } );
it.each([ it.each( [ null, {}, { platform: 'ios' }, { version: '1.1' } ] )(
null, 'errors for invalid request body - %p',
{}, async ( data ) => {
{ platform: 'ios' },
{ version: '1.1' },
])( 'errors for invalid request body - %p', async data => {
const response = await client const response = await client
.post( `/wc-telemetry/tracker`, data ) .post( `/wc-telemetry/tracker`, data )
.catch( err => { .catch( ( err ) => {
expect( err.response.status ).toBe( 400 ); expect( err.statusCode ).toBe( 400 );
} ); } );
expect( response ).toBeUndefined(); expect( response ).toBeUndefined();
} ); }
);
it( 'returns 200 with correct fields', async () => { it( 'returns 200 with correct fields', async () => {
const response = await client const response = await client.post( `/wc-telemetry/tracker`, {
.post( `/wc-telemetry/tracker`, {
platform: 'ios', platform: 'ios',
version: '1.0', version: '1.0',
}) } );
expect( response.status ).toBe( 200 ); expect( response.statusCode ).toBe( 200 );
} ); } );
} ); } );
}; };