Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire in assertion that the appropriate product is created in Stripe #90

Merged
merged 2 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Get these from https://dashboard.stripe.com/test/apikeys
STRIPE_SECRET_KEY=""
STRIPE_PUBLISHABLE_KEY=""
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules
package-lock.json

# Probably don't want tmp files to persist between dev machines.
tmp
tmp
.env
2 changes: 1 addition & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ gem install --conservative rbhint -v 0.87.1.rc1

# We use cucumber-js for system and integration testing
# We use `uuid` to generate random strings
npm install --save-dev cucumber uuid child-process-promise
npm install --save-dev cucumber uuid child-process-promise dotenv stripe
18 changes: 14 additions & 4 deletions features/client-sandbox.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const fs = require("fs");
require('dotenv').config();
const { execSync } = require("child_process");
const { v4: uuidv4 } = require("uuid");
const PaymentGateway = require("./support/PaymentGateway");

/*
* A Sandbox lets us create and destroy our testing environment
* programmatically. At present, Compensated is a command-line
* program, so the sandbox is going to be a local directory on
* the filesystem that we can dump temporary files in.
*/
module.exports = class ClientSandbox {
constructor(paymentGateway) {
this.paymentGateway = paymentGateway;

module.exports = class ClientSandbox {
constructor() {
this.runId = uuidv4();

this.createTempDirectory();
Expand All @@ -23,7 +25,7 @@ module.exports = class ClientSandbox {
* The location where the Sandbox stores any files useful at runtime
*/
get temporaryDirectory() {
return `${sandboxDir}/${this.paymentGateway}-${this.runId}`;
return `${sandboxDir}/${this.runId}`;
}

/*
Expand All @@ -45,6 +47,14 @@ module.exports = class ClientSandbox {
fs.mkdirSync(this.temporaryDirectory);
}
}

productsWhere(type, filter) {
return new PaymentGateway({ type, secretKey: process.env.STRIPE_SECRET_KEY })
.products()
.then((products) =>
products.filter((product) => product.name === filter.name)
);
}
};

// Location to store sandbox files
Expand Down
7 changes: 5 additions & 2 deletions features/parameter_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ defineParameterType({
defineParameterType({
name: "paymentGateway",
regexp: /Stripe/,
transformer: (pg) => pg,
});
transformer: (type) => type
})




/*
* A Custom Parameter Type for our Compensated Packages
Expand Down
9 changes: 5 additions & 4 deletions features/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ Then(
}
);

Then("a {string} Product is created in {paymentGateway}", function (
string,
Then("a {string} Product is created in {paymentGateway}", async function (
name,
paymentGateway
) {
// Write code here that turns the phrase above into concrete actions
return "pending";
return this.clientSandbox.productsWhere(paymentGateway, { name }).then(
(products) => assert(products[0], `Product "${name}" does not exist`)
)
});

Then("all the commands passed", function () {
Expand Down
20 changes: 20 additions & 0 deletions features/support/PaymentGateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Stripe = require('stripe')
const http = require('http')

module.exports = class PaymentGateway {
constructor({ type, secretKey }) {
this.type = type;
if(this.type === 'Stripe') {
this.stripe = Stripe(secretKey);
}
}

products() {
return new Promise((resolve, reject) => {
this.stripe.products.list({limit: 100}, (err, products) => {
Copy link
Contributor

@user512 user512 Jul 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this limit: 100 from Stripe?

Nvm, it's like we do ?limit=100 and the default is 10.
https://stripe.com/docs/api/products/list#list_products-limit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I figured 100 was a better default.

if(err) { return reject(err) }
resolve(products.data)
})
})
}
}