Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanjakl committed May 13, 2024
1 parent 9aa2db8 commit abbfa5b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run: pm2 start --name stripe-mock stripe-mock -- -http-addr 127.0.0.1:12111 -https-addr 127.0.0.1:12112

- name: Display active connections
run: netstat -tuln
run: sleep 5 && netstat -tuln

- name: Run ESLint
run: npm run eslint
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ This module adds a piece module and utility operation to automatically synchroni

## Installation

Use your preferred package manager to install the module. You'll also need to install the [read-only-field](https://github.com/stepanjakl/apostrophe-read-only-field) package alongside it:
Use your preferred package manager to install the module. You'll also need to install the [`read-only-field`](https://github.com/stepanjakl/apostrophe-read-only-field) package alongside it:

```zsh
npm install stripe-products@npm:@stepanjakl/apostrophe-stripe-products
Expand All @@ -61,7 +61,7 @@ npm install read-only-field@npm:@stepanjakl/apostrophe-read-only-field

## Examples

It is highly recommended to explore the [apostrophe-stripe-examples](https://github.com/stepanjakl/apostrophe-stripe-examples) repository, which offers a comprehensive set of examples and full configurations demonstrating how to set up a complete e-commerce store experience.
It is highly recommended to explore the [`apostrophe-stripe-examples`](https://github.com/stepanjakl/apostrophe-stripe-examples) repository, which offers a comprehensive set of examples and full configurations demonstrating how to set up a complete e-commerce store experience.

<br>

Expand Down Expand Up @@ -106,7 +106,22 @@ The `stripe-products` module contains a custom API route (`'/api/v1/stripe-produ

<br>

## Unit Tests

To run tests locally, you'll need to set up the [`stripe/stripe-mock`](https://github.com/stripe/stripe-mock) package:

```zsh
brew install stripe/stripe-mock/stripe-mock

brew services start stripe-mock
```

<br>

Once set up, run tests using `npm run tests` to validate any changes before deploying them.

<br>

## TODOs (Limitations)

- fix disappering `stripeProductObject` and `stripePriceObject` data when moved between `draft` and `published` modes and vice versa
- mention stripe-mock in the `README`
48 changes: 37 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ describe('Apostrophe - Stripe Products Integration Tests', function () {
});

it('should properly instantiate the read-only field module', function () {
assert(apos.readOnlyField);
assert(apos.readOnlyField, 'ReadOnlyField module should be properly instantiated');
});

it('should properly instantiate the products and product piece type modules', function () {
assert(apos.stripeProducts);
assert(apos.stripeProduct);
assert(apos.stripeProducts, 'Stripe Products module should be properly instantiated');
assert(apos.stripeProduct, 'Stripe Product piece type module should be properly instantiated');
});

it('should be able to insert a test admin user', async function() {
assert(apos.user.newInstance);
assert(apos.user.newInstance, 'New instance of User should be available');
const user = apos.user.newInstance();
assert(user);
assert(user, 'User instance should be created successfully');

user.title = 'admin';
user.username = 'admin';
Expand All @@ -56,14 +56,20 @@ describe('Apostrophe - Stripe Products Integration Tests', function () {
user.role = 'admin';

await apos.user.insert(apos.task.getReq(), user);

const insertedUser = await apos.user.find(apos.task.getReq(), { username: 'admin' }).toObject();
assert(insertedUser, 'Admin user should be inserted successfully');
assert.strictEqual(insertedUser.username, 'admin', 'Username of inserted user should match');
assert.strictEqual(insertedUser.email, '[email protected]', 'Email of inserted user should match');
assert.strictEqual(insertedUser.role, 'admin', 'Role of inserted user should be admin');
});

it('should log in as admin and establish a CSRF cookie with a GET request', async function() {
jar = apos.http.jar();

let page = await apos.http.get('/', { jar });

assert(page.match(/logged out/));
assert(page.match(/logged out/), 'Should detect that the user is logged out');

await apos.http.post('/api/v1/@apostrophecms/login/login', {
body: {
Expand All @@ -76,7 +82,7 @@ describe('Apostrophe - Stripe Products Integration Tests', function () {

page = await apos.http.get('/', { jar });

assert(page.match(/logged in/));
assert(page.match(/logged in/), 'Should detect that the user is logged in');
});

it('should connect to Stripe API', async function() {
Expand All @@ -91,7 +97,7 @@ describe('Apostrophe - Stripe Products Integration Tests', function () {

try {
const paymentMethods = await stripe.paymentMethods.list({ limit: 1 });
assert.strictEqual(paymentMethods.data.length > 0, true);
assert.strictEqual(paymentMethods.data.length > 0, true, 'Should connect to Stripe API successfully');
} catch (error) {
console.error('Error connecting to Stripe API:', error);
if (error.detail?.errors?.length > 0) {
Expand All @@ -117,8 +123,28 @@ describe('Apostrophe - Stripe Products Integration Tests', function () {
throw error;
}

assert.strictEqual(Object.keys(response.job)[0] === 'jobId', true);
assert.strictEqual(response.productList.length > 0, true);
assert.strictEqual(Object.keys(response.job)[0] === 'jobId', true, 'Job ID should exist in the response');
assert.strictEqual(response.productList.length > 0, true, 'Product list should not be empty in the response');
});

it('should retrieve both draft and published version of the product from the database', async function () {
const assert = require('assert');

const products = await apos.doc.db.find({ type: /product/i }).toArray();

let hasDraft = false;
let hasPublished = false;

products.forEach(product => {
if (product._id.includes(':draft')) {
hasDraft = true;
} else if (product._id.includes(':published')) {
hasPublished = true;
}
});

assert.strictEqual(hasDraft, true, 'At least one draft product should exist');
assert.strictEqual(hasPublished, true, 'At least one published product should exist');
});

it('should retrieve products via REST API using a GET request', async function () {
Expand All @@ -136,6 +162,6 @@ describe('Apostrophe - Stripe Products Integration Tests', function () {
throw error;
}

assert.strictEqual(response.results.length > 0, true);
assert.strictEqual(response.results.length > 0, true, 'At least one product should be retrieved via REST API');
});
});

0 comments on commit abbfa5b

Please sign in to comment.