Add/a2p create customers crud api core tests (#34945)

* add customer crud api-core-tests

* add changelog file

* Update plugins/woocommerce/tests/api-core-tests/tests/customers/customers-crud.test.js

Co-authored-by: Greg <71906536+zhongruige@users.noreply.github.com>

* Update plugins/woocommerce/tests/api-core-tests/tests/customers/customers-crud.test.js

Co-authored-by: Greg <71906536+zhongruige@users.noreply.github.com>

* Update plugins/woocommerce/tests/api-core-tests/tests/customers/customers-crud.test.js

Co-authored-by: Greg <71906536+zhongruige@users.noreply.github.com>

* PR review updates

* Update plugins/woocommerce/tests/api-core-tests/tests/customers/customers-crud.test.js

Co-authored-by: Greg <71906536+zhongruige@users.noreply.github.com>

Co-authored-by: Greg <71906536+zhongruige@users.noreply.github.com>
This commit is contained in:
nigeljamesstevenson 2022-10-05 19:45:26 +01:00 committed by GitHub
parent f8ea46b086
commit a0233d2d62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 511 additions and 4 deletions

1
.gitignore vendored
View File

@ -94,6 +94,7 @@ changes.json
/plugins/woocommerce/tests/e2e-pw/report /plugins/woocommerce/tests/e2e-pw/report
/plugins/woocommerce/tests/e2e-pw/storage /plugins/woocommerce/tests/e2e-pw/storage
/plugins/woocommerce/tests/e2e-pw/test-results.json /plugins/woocommerce/tests/e2e-pw/test-results.json
/plugins/woocommerce/tests/api-core-tests/output
# Turborepo # Turborepo
.turbo .turbo

View File

@ -20,9 +20,6 @@ tests/cli/vendor
/tests/e2e/plugins /tests/e2e/plugins
.phpunit.result.cache .phpunit.result.cache
# API Test Output
/tests/api-core-tests/output/*
# Packages # Packages
/packages/* /packages/*
!/packages/README.md !/packages/README.md

View File

@ -0,0 +1,4 @@
Significance: patch
Type: add
Add playwright api-core-tests for customers crud operations

View File

@ -69,7 +69,7 @@ USER_KEY=""
USER_SECRET="" USER_SECRET=""
``` ```
For local setup, create a `.env` file in this folder with the three required values described above. For local setup, create a `.env` file in the `woocommerce/plugins/woocommerce` folder with the three required values described above.
If any of these variables are configured they will override the values automatically set in the `playwright.config.js` If any of these variables are configured they will override the values automatically set in the `playwright.config.js`
When using a username and password combination instead of a consumer secret and consumer key, make sure to have the [JSON Basic Authentication plugin](https://github.com/WP-API/Basic-Auth) installed and activated on the test site. When using a username and password combination instead of a consumer secret and consumer key, make sure to have the [JSON Basic Authentication plugin](https://github.com/WP-API/Basic-Auth) installed and activated on the test site.

View File

@ -0,0 +1,46 @@
/**
* This file contains objects that can be used as test data for scenarios around creating, retrieivng, updating, and deleting customers.
*
* For more details on the Product properties, see:
*
* https://woocommerce.github.io/woocommerce-rest-api-docs/#customers
*
*/
/**
* A customer
*/
const customer = {
email: "john.doe@example.com",
first_name: "John",
last_name: "Doe",
username: "john.doe",
billing: {
first_name: "John",
last_name: "Doe",
company: "",
address_1: "969 Market",
address_2: "",
city: "San Francisco",
state: "CA",
postcode: "94103",
country: "US",
email: "john.doe@example.com",
phone: "(555) 555-5555"
},
shipping: {
first_name: "John",
last_name: "Doe",
company: "",
address_1: "969 Market",
address_2: "",
city: "San Francisco",
state: "CA",
postcode: "94103",
country: "US"
}
};
module.exports = {
customer,
};

View File

@ -4,6 +4,7 @@ const {
getOrderExampleSearchTest, getOrderExampleSearchTest,
} = require( './order' ); } = require( './order' );
const { coupon } = require( './coupon' ); const { coupon } = require( './coupon' );
const { customer } = require( './customer' );
const { refund } = require( './refund' ); const { refund } = require( './refund' );
const { getTaxRateExamples } = require( './tax-rate' ); const { getTaxRateExamples } = require( './tax-rate' );
const { getVariationExample } = require( './variation' ); const { getVariationExample } = require( './variation' );
@ -20,6 +21,7 @@ const { getShippingMethodExample } = require( './shipping-method' );
const shared = require( './shared' ); const shared = require( './shared' );
module.exports = { module.exports = {
customer,
order, order,
getOrderExample, getOrderExample,
getOrderExampleSearchTest, getOrderExampleSearchTest,

View File

@ -0,0 +1,457 @@
const {
test,
expect
} = require('@playwright/test');
const {
customer
} = require('../../data');
/**
* Tests for the WooCommerce Customers API.
*
* @group api
* @group customers
*
*/
test.describe('Customers API tests: CRUD', () => {
let customerId;
test.describe('Retrieve after env setup', () => {
/** when the environment is created,
* (https://github.com/woocommerce/woocommerce/tree/trunk/plugins/woocommerce/tests/e2e-pw#woocommerce-playwright-end-to-end-tests),
* we have an admin user and a subscriber user that can both be
* accessed through their ids
* admin user will have id 1 and subscriber user will have id 2
* neither of these are returned as part of the get all customers call
* unless the role 'all' is passed as a search param
* but they can be accessed by specific id reference
*/
test('can retrieve admin user', async ({
request
}) => {
// call API to retrieve the previously saved customer
const response = await request.get('/wp-json/wc/v3/customers/1');
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.is_paying_customer).toEqual(false);
expect(responseJSON.role).toEqual('administrator');
expect(responseJSON.username).toEqual('admin');
});
test('can retrieve subscriber user', async ({
request
}) => {
// call API to retrieve the previously saved customer
const response = await request.get('/wp-json/wc/v3/customers/2');
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.is_paying_customer).toEqual(false);
expect(responseJSON.role).toEqual('subscriber');
});
test('retrieve user with id 0 is invalid', async ({
request
}) => {
// call API to retrieve the previously saved customer
const response = await request.get('/wp-json/wc/v3/customers/0');
const responseJSON = await response.json();
expect(response.status()).toEqual(404);
expect(responseJSON.code).toEqual('woocommerce_rest_invalid_id');
expect(responseJSON.message).toEqual('Invalid resource ID.');
});
test('can retrieve customers', async ({
request
}) => {
// call API to retrieve all customers should initially return empty array
const response = await request.get('/wp-json/wc/v3/customers');
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(Array.isArray(responseJSON));
expect(responseJSON.length).toEqual(0);
});
// however, if we pass in the search string for role 'all' then all users are returned
test('can retrieve all customers', async ({
request
}) => {
// call API to retrieve all customers should initially return empty array
// unless the role 'all' is passed as a search string, in which case the admin
// and subscriber users will be returned
const response = await request.get('/wp-json/wc/v3/customers', {
params: {
role: 'all'
}
});
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(Array.isArray(responseJSON));
expect(responseJSON.length).toBeGreaterThanOrEqual(2);
});
});
test.describe('Create a customer', () => {
test('can create a customer', async ({
request,
}) => {
// call API to create a customer
const response = await request.post('/wp-json/wc/v3/customers', {
data: customer,
});
const responseJSON = await response.json();
// Save the customer ID. It will be used by the retrieve, update, and delete tests.
customerId = responseJSON.id;
expect(response.status()).toEqual(201);
expect(typeof responseJSON.id).toEqual('number');
// Verify that the customer role is 'customer'
expect(responseJSON.role).toEqual('customer');
});
});
test.describe('Retrieve after create', () => {
test('can retrieve a customer', async ({
request
}) => {
// call API to retrieve the previously saved customer
const response = await request.get(
`/wp-json/wc/v3/customers/${customerId}`
);
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.id).toEqual(customerId);
expect(responseJSON.is_paying_customer).toEqual(false);
expect(responseJSON.role).toEqual('customer');
});
test('can retrieve all customers', async ({
request
}) => {
// call API to retrieve all customers
const response = await request.get('/wp-json/wc/v3/customers');
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(Array.isArray(responseJSON));
expect(responseJSON.length).toBeGreaterThan(0);
});
});
test.describe('Update a customer', () => {
test(`can update the admin user/customer`, async ({
request,
}) => {
/**
* update customer names (regular, billing and shipping) to admin
* (these were initialised blank when the environment is created,
* (https://github.com/woocommerce/woocommerce/tree/trunk/plugins/woocommerce/tests/e2e-pw#woocommerce-playwright-end-to-end-tests
*/
const response = await request.put(
`/wp-json/wc/v3/customers/1`, {
data: {
first_name: 'admin',
billing: {
first_name: 'admin'
},
shipping: {
first_name: 'admin'
}
}
}
);
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.first_name).toEqual('admin');
expect(responseJSON.billing.first_name).toEqual('admin');
expect(responseJSON.shipping.first_name).toEqual('admin');
});
test('retrieve after update admin', async ({
request
}) => {
// call API to retrieve the admin customer we updated above
const response = await request.get('/wp-json/wc/v3/customers/1');
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.first_name).toEqual('admin');
expect(responseJSON.billing.first_name).toEqual('admin');
expect(responseJSON.shipping.first_name).toEqual('admin');
});
test(`can update the subscriber user/customer`, async ({
request,
}) => {
// update customer names (billing and shipping) to Jane
// (these were initialised blank, only regular first_name was populated)
const response = await request.put(
`/wp-json/wc/v3/customers/2`, {
data: {
billing: {
first_name: 'Jane'
},
shipping: {
first_name: 'Jane'
}
}
}
);
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.first_name).toEqual('Jane');
expect(responseJSON.billing.first_name).toEqual('Jane');
expect(responseJSON.shipping.first_name).toEqual('Jane');
});
test('retrieve after update subscriber', async ({
request
}) => {
// call API to retrieve the subscriber customer we updated above
const response = await request.get('/wp-json/wc/v3/customers/2');
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.first_name).toEqual('Jane');
expect(responseJSON.billing.first_name).toEqual('Jane');
expect(responseJSON.shipping.first_name).toEqual('Jane');
});
test(`can update a customer`, async ({
request,
}) => {
// update customer names (regular, billing and shipping) from John to Jack
const response = await request.put(
`/wp-json/wc/v3/customers/${ customerId }`, {
data: {
first_name: 'Jack',
billing: {
first_name: 'Jack'
},
shipping: {
first_name: 'Jack'
}
}
}
);
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.first_name).toEqual('Jack');
expect(responseJSON.billing.first_name).toEqual('Jack');
expect(responseJSON.shipping.first_name).toEqual('Jack');
});
test('retrieve after update customer', async ({
request
}) => {
// call API to retrieve the updated customer we created above
const response = await request.get(`/wp-json/wc/v3/customers/${ customerId }`);
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
expect(responseJSON.first_name).toEqual('Jack');
expect(responseJSON.billing.first_name).toEqual('Jack');
expect(responseJSON.shipping.first_name).toEqual('Jack');
});
});
test.describe('Delete a customer', () => {
test('can permanently delete an customer', async ({
request
}) => {
// Delete the customer.
const response = await request.delete(
`/wp-json/wc/v3/customers/${ customerId }`, {
data: {
force: true,
},
}
);
expect(response.status()).toEqual(200);
// Verify that the customer can no longer be retrieved.
const getDeletedCustomerResponse = await request.get(
`/wp-json/wc/v3/customers/${ customer }`
);
expect(getDeletedCustomerResponse.status()).toEqual(404);
});
});
test.describe('Batch update customers', () => {
/**
* 2 Customers to be created in one batch.
*/
const expectedCustomers = [{
email: "john.doe2@example.com",
first_name: "John",
last_name: "Doe",
username: "john.doe2",
billing: {
first_name: "John",
last_name: "Doe",
company: "",
address_1: "969 Market",
address_2: "",
city: "San Francisco",
state: "CA",
postcode: "94103",
country: "US",
email: "john.doe2@example.com",
phone: "(555) 555-5555"
},
shipping: {
first_name: "John",
last_name: "Doe",
company: "",
address_1: "969 Market",
address_2: "",
city: "San Francisco",
state: "CA",
postcode: "94103",
country: "US"
}
},
{
email: "joao.silva2@example.com",
first_name: "João",
last_name: "Silva",
username: "joao.silva2",
billing: {
first_name: "João",
last_name: "Silva",
company: "",
address_1: "Av. Brasil, 432",
address_2: "",
city: "Rio de Janeiro",
state: "RJ",
postcode: "12345-000",
country: "BR",
email: "joao.silva2@example.com",
phone: "(55) 5555-5555"
},
shipping: {
first_name: "João",
last_name: "Silva",
company: "",
address_1: "Av. Brasil, 432",
address_2: "",
city: "Rio de Janeiro",
state: "RJ",
postcode: "12345-000",
country: "BR"
}
}
];
// set payload to use batch create: action
const batchCreate2CustomersPayload = {
create: expectedCustomers
};
test('can batch create customers', async ({
request
}) => {
// Batch create 2 new customers.
// call API to batch create customers
const response = await request.post('wp-json/wc/v3/customers/batch', {
data: batchCreate2CustomersPayload,
});
const responseJSON = await response.json();
expect(response.status()).toEqual(200);
// Verify that the 2 new customers were created
const actualCustomers = responseJSON.create;
expect(actualCustomers).toHaveLength(expectedCustomers.length);
for (let i = 0; i < actualCustomers.length; i++) {
const {
id,
first_name
} = actualCustomers[i];
const expectedCustomerName = expectedCustomers[i].first_name;
expect(id).toBeDefined();
expect(first_name).toEqual(expectedCustomerName);
// Save the customer id
expectedCustomers[i].id = id;
}
});
test('can batch update customers', async ({
request
}) => {
// set payload to use batch update: action
const batchUpdatePayload = {
update: [{
id: expectedCustomers[0].id,
email: 'emailupdated@example.com',
},
{
id: expectedCustomers[1].id,
billing: {
address_1: "123 Addressupdate Street"
}
},
],
};
// Call API to update the customers
const response = await request.post(
'wp-json/wc/v3/customers/batch', {
data: batchUpdatePayload,
}
);
const responseJSON = await response.json();
// Verify the response code and the number of customers that were updated.
const updatedCustomers = responseJSON.update;
expect(response.status()).toEqual(200);
expect(updatedCustomers).toHaveLength(2);
// Verify that the 1st customer was updated to have a new email address.
expect(updatedCustomers[0].id).toEqual(expectedCustomers[0].id);
expect(updatedCustomers[0].email).toEqual('emailupdated@example.com');
// Verify that the amount of the 2nd customer was updated to have a new billing address.
expect(updatedCustomers[1].id).toEqual(expectedCustomers[1].id);
expect(updatedCustomers[1].billing.address_1).toEqual('123 Addressupdate Street');
});
test('can batch delete customers', async ({
request
}) => {
// Batch delete the 2 customers.
const customerIdsToDelete = expectedCustomers.map(({
id
}) => id);
const batchDeletePayload = {
delete: customerIdsToDelete,
};
//Call API to batch delete the customers
const response = await request.post(
'wp-json/wc/v3/customers/batch', {
data: batchDeletePayload,
}
);
const responseJSON = await response.json();
// Verify that the response shows the 2 customers.
const deletedCustomerIds = responseJSON.delete.map(
({
id
}) => id);
expect(response.status()).toEqual(200);
expect(deletedCustomerIds).toEqual(customerIdsToDelete);
// Verify that the 2 deleted customers cannot be retrieved.
for (const customerId of customerIdsToDelete) {
//Call the API to attempte to retrieve the customers
const response = await request.get(
`wp-json/wc/v3/customers/${ customerId }`
);
expect(response.status()).toEqual(404);
}
});
});
});