Skip to content

Commit

Permalink
Code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongruige committed Sep 7, 2021
1 parent 1ce5f7a commit 095a743
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 194 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"docker:down": "npx wc-e2e docker:down",
"docker:ssh": "npx wc-e2e docker:ssh",
"docker:up": "npx wc-e2e docker:up",
"test:api": "cd ./tests/e2e/api-core-tests && jest --group=api",
"test:e2e": "npx wc-e2e test:e2e",
"test:e2e-debug": "npx wc-e2e test:e2e-debug",
"test:e2e-dev": "npx wc-e2e test:e2e-dev",
Expand All @@ -47,6 +48,7 @@
"@typescript-eslint/experimental-utils": "3.10.1",
"@typescript-eslint/parser": "3.10.1",
"@woocommerce/api": "file:tests/e2e/api",
"@woocommerce/api-core-tests": "file:tests/e2e/api-core-tests",
"@woocommerce/e2e-core-tests": "file:tests/e2e/core-tests",
"@woocommerce/e2e-environment": "file:tests/e2e/env",
"@woocommerce/e2e-utils": "file:tests/e2e/utils",
Expand Down Expand Up @@ -132,6 +134,7 @@
"no-e2e": {
"exclude": [
"@woocommerce/api",
"@woocommerce/api-core-tests",
"@woocommerce/e2e-core-tests",
"@woocommerce/e2e-environment",
"@woocommerce/e2e-utils",
Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/api-core-tests/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
BASE_URL=""

# The admin user's username or generated consumer key
# USERNAME="admin"
# USERNAME="ck_1234"
USERNAME=""
# USER_KEY="admin"
# USER_KEY="ck_1234"
USER_KEY=""

# The admin user's password or generated consumer secret
# PASSWORD="password"
# PASSWORD="cs_1234"
PASSWORD=""
# USER_SECRET="password"
# USER_SECRET="cs_1234"
USER_SECRET=""

# Optional setting to output verbose logs from Jest
# VERBOSE=true
Expand Down
19 changes: 0 additions & 19 deletions tests/e2e/api-core-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
# Editors
project.xml
project.properties
/nbproject/private/
.buildpath
.project
.settings*
.idea
.vscode
*.sublime-project
*.sublime-workspace
.sublimelinterrc

# OS X metadata
.DS_Store

# Windows thumbnail cache
Thumbs.db

# Node modules
node_modules/

Expand Down
10 changes: 8 additions & 2 deletions tests/e2e/api-core-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ Before running the tests, the following environment variables need to be configu
BASE_URL="https://mysite.com"
# The admin user's username or generated consumer key
USERNAME=""
USER_KEY=""
# The admin user's password or generated consumer secret
PASSWORD=""
USER_SECRET=""
```

For local setup, create a `.env` file in this folder with the three required values described above.

Alternatively, these values can be passed in via the command line. For example:

```shell
BASE_URL=http://localhost:8084 USER_KEY=admin USER_SECRET=password npm run test:api
```

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.

For more information about authentication with the WooCommerce API, please see the [Authentication](https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#authentication) section in the WooCommerce REST API documentation.
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/api-core-tests/data/coupon.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const coupon = {
exclude_sale_items: false,
minimum_amount: '100.00',
maximum_amount: '',
email_restrictions: []
}
email_restrictions: [],
};

module.exports = {
coupon
}
coupon,
};
6 changes: 3 additions & 3 deletions tests/e2e/api-core-tests/data/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const order = require('./order');
const coupon = require('./coupon');
const { order } = require('./order');
const { coupon } = require('./coupon');
const shared = require('./shared');

module.exports = {
order,
coupon,
shared,
}
};
57 changes: 24 additions & 33 deletions tests/e2e/api-core-tests/data/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ const { customerBilling, customerShipping } = require('./shared');
*
* https://woocommerce.github.io/woocommerce-rest-api-docs/#order-properties
*/
const order = {
payment_method: '',
payment_method_title: '',
status: 'pending',
set_paid: false,
currency: 'USD',
customer_note: '',
customer_id: 0,
billing: customerBilling,
shipping: customerShipping,
line_items: [],
shipping_lines: [],
fee_lines: [],
coupon_lines: [],
};

const productLineItems = {
name: '',
Expand All @@ -16,53 +31,29 @@ const productLineItems = {
tax_class: '',
subtotal: '',
total: '',
}
};

const shippingLines = {
method_title: '',
method_id: '',
method_id: 'flat_rate',
total: '',
}
};

const feeLines = {
name: '',
name: 'Fee',
tax_class: '',
tax_status: '',
tax_status: 'none',
total: '',
}
};

const couponLines = {
code: ''
}

const order = {
payment_method: '',
payment_method_title: '',
status: 'pending',
set_paid: false,
currency: 'USD',
customer_note: '',
customer_id: 0,
billing: customerBilling,
shipping: customerShipping,
line_items: [
productLineItems
],
shipping_lines: [
shippingLines
],
fee_lines: [
feeLines
],
coupon_lines: [
couponLines
]
}
code: '10off',
};

module.exports = {
order,
productLineItems,
shippingLines,
feeLines,
couponLines,
}
};
95 changes: 27 additions & 68 deletions tests/e2e/api-core-tests/data/shared/batch-update.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,46 @@
/**
* Shared model for batch updates for a resource.
*
* Note that by default the update is limited to 100 objects to be created, updated, or deleted.
*/
const batchUpdatePayload = {
create: [],
update: [],
delete: [],
}

/**
* Batch create a resource.
* Note that by default the update endpoint is limited to 100 objects to be created, updated, or deleted.
*
* @param {Array} resourcesToCreate A list of resource objects to create.
* @returns
* @param {string} action Batch action. Must be one of: create, update, or delete.
* @param {Array} resources A list of resource objects. For the delete action, this will be a list of IDs.
* @param {Object} payload The batch payload object. Defaults to an empty object.
* @returns {Object} The payload to send to the batch endpoint.
*/
const batchCreate = ( resourcesToCreate = [] ) => {
if ( resourcesToCreate.length === 0 ) {
const batch = ( action, resources = [], payload = {} ) => {
if ( ! [ 'create', 'update', 'delete' ].includes( action ) ) {
return;
}

// Build array of resources to create
const createArray = [];
resourcesToCreate.forEach( ( resource ) => {
createArray.push( resource );
});

batchUpdatePayload.create = createArray

return createArray;
}

/**
* Batch update resources.
*
* @param {Array} resourcesToUpdate A list of resource objects to update.
* @returns
*/
const batchUpdate = ( resourcesToUpdate = [] ) => {
if ( resourcesToUpdate.length === 0 ) {
return
if ( resources.length === 0 ) {
return;
}

// Build array of resources to update
const updateArray = [];
resourcesToUpdate.forEach( ( resource ) => {
updateArray.push( resource );
});

return updateArray;
}
if ( action === 'create' ) {
payload.create = [ ...resources ];
}

/**
* Batch delete resources.
*
* @param {Array} resourceIds A list of IDs of resource objects to delete.
* @returns
*/
const batchDelete = ( resourceIds = [] ) => {
if ( resourceIds.length === 0 ) {
return;
if ( action === 'update' ) {
payload.update = [ ...resources ];
}

// Build array of resources to delete
const deleteArray = [];
resourceIds.forEach( ( id ) => {
deleteArray.push( id );
});
if ( action === 'delete' ) {
payload.delete = [ ...resources ];
}

return deleteArray;
}
return payload;
};

const getBatchPayloadExample = ( resource ) => {
batchUpdatePayload.create = batchCreate( [ resource ] );
batchUpdatePayload.update = batchUpdate( [ resource ] );
batchUpdatePayload.delete = batchDelete( [ 1, 2, 3 ] );
let batchUpdatePayload = {};
batchUpdatePayload = batch( 'create', [ resource ], batchUpdatePayload );
batchUpdatePayload = batch( 'update', [ resource ], batchUpdatePayload );
batchUpdatePayload = batch( 'delete', [ 1, 2, 3 ], batchUpdatePayload );
return batchUpdatePayload;
}
};

module.exports = {
batchUpdatePayload,
batchCreate,
batchUpdate,
batchDelete,
getBatchPayloadExample
}
batch,
getBatchPayloadExample,
};
8 changes: 4 additions & 4 deletions tests/e2e/api-core-tests/data/shared/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const customerBilling = {
postcode: '94107',
phone: '123456789',
email: 'john.doe@example.com',
}
};

/**
* Customer shipping object.
Expand All @@ -37,9 +37,9 @@ const customerShipping = {
state: 'NY',
postcode: '14201',
phone: '123456789',
}
};

module.exports = {
customerBilling,
customerShipping
}
customerShipping,
};
6 changes: 4 additions & 2 deletions tests/e2e/api-core-tests/data/shared/error-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ const errorResponse = {
message: '',
data: {
status: 400
}
}
},
};

module.exports = { errorResponse };
16 changes: 5 additions & 11 deletions tests/e2e/api-core-tests/data/shared/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
const { customerBilling, customerShipping } = require('./customer');
const {
batchUpdatePayload,
batchCreate,
batchUpdate,
batchDelete,
batch,
getBatchPayloadExample
} = require('./batch-update');
const errorRessponse = require('./customer');
const { errorResponse } = require('./error-response');

module.exports = {
customerBilling,
customerShipping,
batchUpdatePayload,
batchCreate,
batchUpdate,
batchDelete,
batch,
getBatchPayloadExample,
errorRessponse
}
errorResponse,
};
Loading

0 comments on commit 095a743

Please sign in to comment.