2021-01-19 13:49:18 +00:00
|
|
|
import {
|
2020-08-05 18:01:43 +00:00
|
|
|
clearLocalStorage,
|
2021-01-19 13:49:18 +00:00
|
|
|
setBrowserViewport,
|
2021-05-20 20:55:14 +00:00
|
|
|
withRestApi,
|
|
|
|
WP_ADMIN_LOGIN
|
2021-01-19 13:49:18 +00:00
|
|
|
} from '@woocommerce/e2e-utils';
|
2020-08-04 22:55:13 +00:00
|
|
|
|
2021-05-20 20:55:14 +00:00
|
|
|
const config = require('config');
|
|
|
|
const { HTTPClientFactory } = require('@woocommerce/api');
|
2021-05-26 11:20:08 +00:00
|
|
|
const { addConsoleSuppression } = require( '@woocommerce/e2e-environment' );
|
|
|
|
|
|
|
|
// @todo: remove this once https://github.com/woocommerce/woocommerce-admin/issues/6992 has been addressed
|
|
|
|
addConsoleSuppression( 'woocommerce_shared_settings' );
|
2021-01-19 15:02:08 +00:00
|
|
|
|
2020-08-04 22:55:13 +00:00
|
|
|
/**
|
2021-05-20 20:55:14 +00:00
|
|
|
* Uses the WordPress API to delete all existing posts
|
2020-08-04 22:55:13 +00:00
|
|
|
*/
|
|
|
|
async function trashExistingPosts() {
|
2021-05-20 20:55:14 +00:00
|
|
|
const apiUrl = config.get('url');
|
|
|
|
const wpPostsEndpoint = '/wp/v2/posts';
|
|
|
|
const adminUsername = config.get('users.admin.username');
|
|
|
|
const adminPassword = config.get('users.admin.password');
|
|
|
|
const client = HTTPClientFactory.build(apiUrl)
|
|
|
|
.withBasicAuth(adminUsername, adminPassword)
|
|
|
|
.create();
|
2020-08-04 22:55:13 +00:00
|
|
|
|
2021-05-20 20:55:14 +00:00
|
|
|
// List all existing posts
|
|
|
|
const response = await client.get(wpPostsEndpoint);
|
|
|
|
const posts = response.data;
|
|
|
|
|
|
|
|
// Delete each post
|
2021-05-21 11:27:50 +00:00
|
|
|
for (const post of posts) {
|
2021-05-20 20:55:14 +00:00
|
|
|
await client.delete(`${wpPostsEndpoint}/${post.id}`);
|
2020-08-04 22:55:13 +00:00
|
|
|
}
|
2021-06-16 21:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the WordPress API to update the Ready page's status.
|
|
|
|
*
|
|
|
|
* @param {string} status | Status to update the page to. One of: publish, future, draft, pending, private
|
|
|
|
*/
|
|
|
|
async function updateReadyPageStatus( status ) {
|
|
|
|
const apiUrl = config.get('url');
|
|
|
|
const wpPagesEndpoint = '/wp/v2/pages';
|
|
|
|
const adminUsername = config.get('users.admin.username');
|
|
|
|
const adminPassword = config.get('users.admin.password');
|
|
|
|
const client = HTTPClientFactory.build(apiUrl)
|
|
|
|
.withBasicAuth(adminUsername, adminPassword)
|
|
|
|
.create();
|
2021-05-20 17:18:46 +00:00
|
|
|
|
2021-06-16 21:28:04 +00:00
|
|
|
// As the default status filter in the API is `publish`, we need to
|
|
|
|
// filter based on the supplied status otherwise no results are returned.
|
|
|
|
let statusFilter = 'publish';
|
|
|
|
if ( 'publish' === status ) {
|
|
|
|
// The page will be in a draft state, so we need to filter on that status
|
|
|
|
statusFilter = 'draft';
|
|
|
|
}
|
|
|
|
const getPostsResponse = await client.get(`${wpPagesEndpoint}?search=ready&status=${statusFilter}`);
|
|
|
|
const pageId = getPostsResponse.data[0].id;
|
|
|
|
|
|
|
|
// Update the page to the new status
|
|
|
|
await client.post(`${wpPagesEndpoint}/${pageId}`, { 'status': status });
|
2020-08-04 22:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Before every test suite run, delete all content created by the test. This ensures
|
|
|
|
// other posts/comments/etc. aren't dirtying tests and tests don't depend on
|
|
|
|
// each other's side-effects.
|
2021-05-21 11:27:50 +00:00
|
|
|
beforeAll(async () => {
|
2021-06-16 21:28:04 +00:00
|
|
|
// Update the ready page to prevent concurrent test runs
|
|
|
|
await updateReadyPageStatus('draft');
|
|
|
|
|
2020-08-04 22:55:13 +00:00
|
|
|
await trashExistingPosts();
|
2021-04-08 01:54:25 +00:00
|
|
|
await withRestApi.deleteAllProducts();
|
|
|
|
await withRestApi.deleteAllCoupons();
|
2021-05-20 20:55:14 +00:00
|
|
|
await page.goto(WP_ADMIN_LOGIN);
|
2020-08-05 18:01:43 +00:00
|
|
|
await clearLocalStorage();
|
2021-05-21 11:27:50 +00:00
|
|
|
await setBrowserViewport('large');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Clear browser cookies and cache using DevTools.
|
|
|
|
// This is to ensure that each test ends with no user logged in.
|
|
|
|
afterAll(async () => {
|
2021-06-16 21:28:04 +00:00
|
|
|
// Reset the ready page to published to allow future test runs
|
|
|
|
await updateReadyPageStatus('publish');
|
|
|
|
|
2021-05-21 11:27:50 +00:00
|
|
|
const client = await page.target().createCDPSession();
|
|
|
|
await client.send('Network.clearBrowserCookies');
|
|
|
|
await client.send('Network.clearBrowserCache');
|
|
|
|
});
|