Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
Release 0.0.7
Browse files Browse the repository at this point in the history
- Added unofficial source token `tok_forget`.
  • Loading branch information
jeff committed Sep 20, 2019
1 parent 8321026 commit add454b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 40 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ This server supports a few bonus parameters to test scenarios not testable again

### Source token `tok_429`

Use the charge source token `tok_429` to get a 429 response from the server.
Use the charge source token `tok_429` to get a 429 (rate limit) response from the server.

### Source token `tok_500`

Use the charge source token `tok_500` to get a 500 response from the server.
Use the charge source token `tok_500` to get a 500 (server error) response from the server.

### Source token `tok_forget`

Use the charge source token `tok_forget` to get a normal charge response from the server that is not saved.

### Source token chains

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stripe-stateful-mock",
"version": "0.0.6",
"version": "0.0.7",
"description": "A half-baked, stateful Stripe mock server",
"main": "dist/index.js",
"scripts": {
Expand Down
15 changes: 12 additions & 3 deletions src/api/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace cards {
export function createFromSource(token: string): stripe.cards.ICard {
log.debug("cards.createFromSource", token);

let saveCard = true;
const cardId = `card_${generateId(24)}`;
const now = new Date();
const card: stripe.cards.ICard = {
Expand Down Expand Up @@ -117,13 +118,21 @@ namespace cards {
card.brand = "Visa";
card.last4 = "1976";
break;
case "tok_forget":
// Unofficial token.
card.brand = "Visa";
card.last4 = "1982";
saveCard = false;
break;
default:
throw new Error(`Unhandled source token '${token}'`);
}

cardExtras[card.id] = {
sourceToken: token
};
if (saveCard) {
cardExtras[card.id] = {
sourceToken: token
};
}

return card;
}
Expand Down
5 changes: 4 additions & 1 deletion src/api/charges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ namespace charges {

const card = cards.createFromSource(sourceToken);
charge = getChargeFromCard(params, card);
accountCharges.put(accountId, charge);
if (params.source !== "tok_forget") {
accountCharges.put(accountId, charge);
}
handleSpecialChargeTokens(accountId, charge, sourceToken);
} else {
throw new StripeError(400, {
Expand Down Expand Up @@ -640,6 +642,7 @@ namespace charges {
const dispute = disputes.createFromSource(accountId, sourceToken, charge);
charge.dispute = dispute.id;
});
break;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/api/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ namespace customers {
createCard(accountId, customer, {source: params.source});
}

accountCustomers.put(accountId, customer);
if (params.source !== "tok_forget") {
accountCustomers.put(accountId, customer);
}

return customer;
}
Expand Down
81 changes: 53 additions & 28 deletions test/charges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ chai.use(chaiExclude);

describe("charges", () => {

const localStripeClient = getLocalStripeClient();

const buildChargeParityTest = (params: stripe.charges.IChargeCreationOptions) =>
buildStripeParityTest(
async stripeClient => {
Expand Down Expand Up @@ -131,16 +133,16 @@ describe("charges", () => {
));

it("supports tok_createDisputeProductNotReceived", buildStripeParityTest(
async stripeClient => {
async (stripeClient, mode) => {
const charge = await stripeClient.charges.create({
amount: 5000,
currency: "usd",
source: "tok_createDisputeProductNotReceived"
});
chai.assert.isNull(charge.dispute);
chai.assert.isNull(charge.dispute, mode);

const chargeGet = await stripeClient.charges.retrieve(charge.id);
chai.assert.isString(chargeGet.dispute);
chai.assert.isString(chargeGet.dispute, mode);
return [charge, chargeGet];
}
));
Expand Down Expand Up @@ -171,15 +173,15 @@ describe("charges", () => {
}));

it("supports capture=false", buildStripeParityTest(
async stripeClient => {
async (stripeClient, mode) => {
const charge = await stripeClient.charges.create({
amount: 3500,
currency: "usd",
source: "tok_visa",
capture: false
});
chai.assert.isFalse(charge.captured);
chai.assert.isNull(charge.balance_transaction);
chai.assert.isFalse(charge.captured, mode);
chai.assert.isNull(charge.balance_transaction, mode);
return [charge];
}
));
Expand Down Expand Up @@ -270,12 +272,12 @@ describe("charges", () => {
}
));

describe("bonus secret tokens!", () => {
describe("unofficial token support", () => {
describe("tok_429", () => {
it("throws a 429 error", async () => {
let error: any;
try {
await getLocalStripeClient().charges.create({
await localStripeClient.charges.create({
amount: 5000,
currency: "usd",
source: "tok_429"
Expand All @@ -295,7 +297,7 @@ describe("charges", () => {
it("throws a 500 error", async () => {
let error: any;
try {
await getLocalStripeClient().charges.create({
await localStripeClient.charges.create({
amount: 5000,
currency: "usd",
source: "tok_500"
Expand All @@ -310,6 +312,29 @@ describe("charges", () => {
chai.assert.equal(error.type, "StripeAPIError");
});
});

describe("tok_forget", () => {
it("creates a successful charge that is not saved", async () => {
const charge = await localStripeClient.charges.create(
{
amount: 2000,
currency: "usd",
source: "tok_forget"
}
);
chai.assert.isString(charge.id);

let getChargeError: any;
try {
await localStripeClient.charges.retrieve(charge.id);
} catch (err) {
getChargeError = err;
}

chai.assert.isDefined(getChargeError);
chai.assert.equal(getChargeError.statusCode, 404);
});
});

describe("source token chains", async () => {
it("supports test case tok_chargeDeclinedInsufficientFunds|tok_visa", async () => {
Expand All @@ -321,14 +346,14 @@ describe("charges", () => {

let nsfError: any;
try {
await getLocalStripeClient().charges.create(chargeParams);
await localStripeClient.charges.create(chargeParams);
} catch (err) {
nsfError = err;
}
chai.assert.isDefined(nsfError);
chai.assert.equal(nsfError.type, "StripeCardError");

const charge = await getLocalStripeClient().charges.create(chargeParams);
const charge = await localStripeClient.charges.create(chargeParams);
chai.assert.equal(charge.amount, chargeParams.amount);
});

Expand All @@ -341,7 +366,7 @@ describe("charges", () => {

let error1: any;
try {
await getLocalStripeClient().charges.create(chargeParams);
await localStripeClient.charges.create(chargeParams);
} catch (err) {
error1 = err;
}
Expand All @@ -351,15 +376,15 @@ describe("charges", () => {

let error2: any;
try {
await getLocalStripeClient().charges.create(chargeParams);
await localStripeClient.charges.create(chargeParams);
} catch (err) {
error2 = err;
}
chai.assert.isDefined(error2);
chai.assert.equal(error2.statusCode, 500);
chai.assert.equal(error2.type, "StripeAPIError");

const charge = await getLocalStripeClient().charges.create(chargeParams);
const charge = await localStripeClient.charges.create(chargeParams);
chai.assert.equal(charge.amount, chargeParams.amount);
});

Expand All @@ -377,7 +402,7 @@ describe("charges", () => {

let error1: any;
try {
await getLocalStripeClient().charges.create(chargeParams1);
await localStripeClient.charges.create(chargeParams1);
} catch (err) {
error1 = err;
}
Expand All @@ -387,18 +412,18 @@ describe("charges", () => {

let error2: any;
try {
await getLocalStripeClient().charges.create(chargeParams2);
await localStripeClient.charges.create(chargeParams2);
} catch (err) {
error2 = err;
}
chai.assert.isDefined(error2);
chai.assert.equal(error2.statusCode, 500);
chai.assert.equal(error2.type, "StripeAPIError");

const charge1 = await getLocalStripeClient().charges.create(chargeParams1);
const charge1 = await localStripeClient.charges.create(chargeParams1);
chai.assert.equal(charge1.amount, chargeParams1.amount);

const charge2 = await getLocalStripeClient().charges.create(chargeParams2);
const charge2 = await localStripeClient.charges.create(chargeParams2);
chai.assert.equal(charge2.amount, chargeParams2.amount);
});
});
Expand All @@ -412,8 +437,8 @@ describe("charges", () => {
currency: "usd",
source: "tok_visa"
};
const originalCharge = await getLocalStripeClient().charges.create(params, {idempotency_key: idempotencyKey});
const repeatCharge = await getLocalStripeClient().charges.create(params, {idempotency_key: idempotencyKey});
const originalCharge = await localStripeClient.charges.create(params, {idempotency_key: idempotencyKey});
const repeatCharge = await localStripeClient.charges.create(params, {idempotency_key: idempotencyKey});

chai.assert.deepEqual(repeatCharge, originalCharge);
});
Expand All @@ -428,15 +453,15 @@ describe("charges", () => {

let originalError: any;
try {
await getLocalStripeClient().charges.create(params, {idempotency_key: idempotencyKey});
await localStripeClient.charges.create(params, {idempotency_key: idempotencyKey});
} catch (err) {
originalError = err;
}
chai.assert.isDefined(originalError);

let repeatError: any;
try {
await getLocalStripeClient().charges.create(params, {idempotency_key: idempotencyKey});
await localStripeClient.charges.create(params, {idempotency_key: idempotencyKey});
} catch (err) {
repeatError = err;
}
Expand All @@ -457,7 +482,7 @@ describe("charges", () => {

let originalError: any;
try {
await getLocalStripeClient().charges.create(params, {
await localStripeClient.charges.create(params, {
idempotency_key: idempotencyKey
});
} catch (err) {
Expand All @@ -468,7 +493,7 @@ describe("charges", () => {

let repeatError: any;
try {
await getLocalStripeClient().charges.create(params, {
await localStripeClient.charges.create(params, {
idempotency_key: idempotencyKey
});
} catch (err) {
Expand All @@ -482,7 +507,7 @@ describe("charges", () => {

it("sends the right error on mismatched idempotent bodies", async () => {
const idempotencyKey = generateId();
await getLocalStripeClient().charges.create({
await localStripeClient.charges.create({
amount: 1000,
currency: "usd",
source: "tok_visa"
Expand All @@ -492,7 +517,7 @@ describe("charges", () => {

let repeatError: any;
try {
await getLocalStripeClient().charges.create({
await localStripeClient.charges.create({
amount: 2000,
currency: "usd",
source: "tok_visa"
Expand All @@ -512,7 +537,7 @@ describe("charges", () => {
it("does not confused two Connected accounts", async () => {
const idempotencyKey = generateId();

await getLocalStripeClient().charges.create({
await localStripeClient.charges.create({
amount: 1000,
currency: "usd",
source: "tok_visa"
Expand All @@ -521,7 +546,7 @@ describe("charges", () => {
stripe_account: "acct_uno"
});

await getLocalStripeClient().charges.create({
await localStripeClient.charges.create({
amount: 2000,
currency: "usd",
source: "tok_visa"
Expand Down
24 changes: 24 additions & 0 deletions test/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import * as chai from "chai";
import * as stripe from "stripe";
import {generateId} from "../src/api/utils";
import {buildStripeParityTest} from "./buildStripeParityTest";
import {getLocalStripeClient} from "./stripeUtils";

describe("customers", () => {

const localStripeClient = getLocalStripeClient();

it("supports basic creation with no params", buildStripeParityTest(
async (stripeClient) => {
const customer = await stripeClient.customers.create({});
Expand Down Expand Up @@ -199,6 +202,27 @@ describe("customers", () => {
}
));

describe("unofficial token support", () => {
describe("tok_forget", () => {
it("forgets the customer when specified on customer create", async () => {
const customer = await localStripeClient.customers.create({
source: "tok_forget"
});
chai.assert.isString(customer.id);

let getCustomerError: any;
try {
await localStripeClient.customers.retrieve(customer.id);
} catch (err) {
getCustomerError = err;
}

chai.assert.isDefined(getCustomerError);
chai.assert.equal(getCustomerError.statusCode, 404);
});
});
});

describe("deleting", () => {
it("supports deleting the only source", buildStripeParityTest(
async (stripeClient) => {
Expand Down
Loading

0 comments on commit add454b

Please sign in to comment.