Skip to content

Commit

Permalink
Fix nested resources for non-curried urlParams and update tests to de…
Browse files Browse the repository at this point in the history
…monstrate their use
  • Loading branch information
rattrayalex-stripe committed May 10, 2019
1 parent 963228e commit 65b2df2
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 80 deletions.
6 changes: 4 additions & 2 deletions lib/StripeMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ const makeAutoPaginationMethods = require('./autoPagination')
* @param [spec.host] Hostname for the request.
*/
function stripeMethod(spec) {
spec.urlParams = utils.extractUrlParams(spec.path || '');

return function() {
const self = this;
const args = [].slice.call(arguments);

const callback = typeof args[args.length - 1] == 'function' && args.pop();

spec.urlParams = utils.extractUrlParams(
self.createResourcePathWithSymbols(spec.path || '')
);

const requestPromise = utils.callbackifyPromiseWithTimeout(
makeRequest(self, args, spec, {}),
callback
Expand Down
12 changes: 0 additions & 12 deletions lib/resources/ApplicationFeeRefunds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@

const StripeResource = require('../StripeResource');

/**
* ApplicationFeeRefunds is a unique resource in that, upon instantiation,
* requires an application fee id , and therefore each of its methods only
* require the refundId argument.
*
* This streamlines the API specifically for the case of accessing refunds
* on a returned application fee object.
*
* E.g. applicationFeeObject.refunds.retrieve(refundId)
* (As opposed to the also-supported stripe.applicationFees.retrieveRefund(chargeId,
* refundId))
*/
module.exports = StripeResource.extend({
path: 'application_fees/{feeId}/refunds',

Expand Down
15 changes: 3 additions & 12 deletions test/resources/ApplicationFeeRefunds.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
'use strict';

const resources = require('../../lib/stripe').resources;
const stripe = require('../../testUtils').getSpyableStripe();
const expect = require('chai').expect;

const APPFEE_TEST_ID = 'appFeeIdTest999';
const REFUND_TEST_ID = 'refundIdTest999';

// Create new CustomerCard instance with pre-filled customerId:
const appFeeRefund = new resources.ApplicationFeeRefunds(stripe, {
feeId: APPFEE_TEST_ID,
});

// Use spy from existing resource:
appFeeRefund._request = stripe.customers._request;

describe('ApplicationFeeRefund Resource', () => {
describe('retrieve', () => {
it('Sends the correct request', () => {
appFeeRefund.retrieve(REFUND_TEST_ID);
stripe.applicationFeeRefunds.retrieve(APPFEE_TEST_ID, REFUND_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/application_fees/${APPFEE_TEST_ID}/refunds/${REFUND_TEST_ID}`,
Expand All @@ -30,7 +21,7 @@ describe('ApplicationFeeRefund Resource', () => {

describe('update', () => {
it('Sends the correct request', () => {
appFeeRefund.update(REFUND_TEST_ID, {
stripe.applicationFeeRefunds.update(APPFEE_TEST_ID, REFUND_TEST_ID, {
metadata: {key: 'value'},
});
expect(stripe.LAST_REQUEST).to.deep.equal({
Expand All @@ -44,7 +35,7 @@ describe('ApplicationFeeRefund Resource', () => {

describe('list', () => {
it('Sends the correct request', () => {
appFeeRefund.list();
stripe.applicationFeeRefunds.list(APPFEE_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/application_fees/${APPFEE_TEST_ID}/refunds`,
Expand Down
9 changes: 1 addition & 8 deletions test/resources/LoginLinks.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
'use strict';

const resources = require('../../lib/stripe').resources;
const stripe = require('../../testUtils').getSpyableStripe();
const expect = require('chai').expect;

const ACCOUNT_ID = 'acct_EXPRESS';

// Create new LoginLink instance with pre-filled accountId:
const loginLink = new resources.LoginLinks(stripe, {accountId: ACCOUNT_ID});

// Use spy from existing resource:
loginLink._request = stripe.customers._request;

describe('LoginLink Resource', () => {
describe('create', () => {
it('Sends the correct request', () => {
loginLink.create();
stripe.loginLinks.create(ACCOUNT_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'POST',
url: `/v1/accounts/${ACCOUNT_ID}/login_links`,
Expand Down
17 changes: 5 additions & 12 deletions test/resources/Persons.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
'use strict';

const resources = require('../../lib/stripe').resources;
const stripe = require('../../testUtils').getSpyableStripe();
const expect = require('chai').expect;

const ACCOUNT_TEST_ID = 'acct_123';
const PERSON_TEST_ID = 'person_123';

// Create new Person instance with pre-filled accountId:
const person = new resources.Persons(stripe, {accountId: ACCOUNT_TEST_ID});

// Use spy from existing resource:
person._request = stripe.customers._request;

describe('Person Resource', () => {
describe('create', () => {
it('Sends the correct request', () => {
person.create({
stripe.persons.create(ACCOUNT_TEST_ID, {
first_name: 'John',
});
expect(stripe.LAST_REQUEST).to.deep.equal({
Expand All @@ -30,7 +23,7 @@ describe('Person Resource', () => {

describe('delete', () => {
it('Sends the correct request', () => {
person.del(PERSON_TEST_ID);
stripe.persons.del(ACCOUNT_TEST_ID, PERSON_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'DELETE',
url: `/v1/accounts/${ACCOUNT_TEST_ID}/persons/${PERSON_TEST_ID}`,
Expand All @@ -42,7 +35,7 @@ describe('Person Resource', () => {

describe('list', () => {
it('Sends the correct request', () => {
person.list();
stripe.persons.list(ACCOUNT_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/accounts/${ACCOUNT_TEST_ID}/persons`,
Expand All @@ -54,7 +47,7 @@ describe('Person Resource', () => {

describe('retrieve', () => {
it('Sends the correct request', () => {
person.retrieve(PERSON_TEST_ID);
stripe.persons.retrieve(ACCOUNT_TEST_ID, PERSON_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/accounts/${ACCOUNT_TEST_ID}/persons/${PERSON_TEST_ID}`,
Expand All @@ -66,7 +59,7 @@ describe('Person Resource', () => {

describe('update', () => {
it('Sends the correct request', () => {
person.update(PERSON_TEST_ID, {
stripe.persons.update(ACCOUNT_TEST_ID, PERSON_TEST_ID, {
first_name: 'John',
});
expect(stripe.LAST_REQUEST).to.deep.equal({
Expand Down
16 changes: 5 additions & 11 deletions test/resources/SubscriptionScheduleRevision.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
'use strict';

const resources = require('../../lib/stripe').resources;
const stripe = require('../../testUtils').getSpyableStripe();
const expect = require('chai').expect;

const SCHEDULE_TEST_ID = 'sub_sched_123';
const REVISION_TEST_ID = 'sub_sched_rev_123';

// Create new SubscriptionScheduleRevision instance with pre-filled scheduleId:
const revision = new resources.SubscriptionScheduleRevisions(stripe, {
scheduleId: SCHEDULE_TEST_ID,
});

// Use spy from existing resource:
revision._request = stripe.customers._request;

describe('SubscriptionScheduleRevision Resource', () => {
describe('list', () => {
it('Sends the correct request', () => {
revision.list();
stripe.subscriptionScheduleRevisions.list(SCHEDULE_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/subscription_schedules/${SCHEDULE_TEST_ID}/revisions`,
Expand All @@ -30,7 +21,10 @@ describe('SubscriptionScheduleRevision Resource', () => {

describe('retrieve', () => {
it('Sends the correct request', () => {
revision.retrieve(REVISION_TEST_ID);
stripe.subscriptionScheduleRevisions.retrieve(
SCHEDULE_TEST_ID,
REVISION_TEST_ID
);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/subscription_schedules/${SCHEDULE_TEST_ID}/revisions/${REVISION_TEST_ID}`,
Expand Down
14 changes: 4 additions & 10 deletions test/resources/TaxIds.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
'use strict';

const resources = require('../../lib/stripe').resources;
const stripe = require('../../testUtils').getSpyableStripe();
const expect = require('chai').expect;

const CUSTOMER_TEST_ID = 'cus_123';
const TAX_ID_TEST_ID = 'txi_123';

const taxId = new resources.TaxIds(stripe, {customerId: CUSTOMER_TEST_ID});

// Use spy from existing resource:
taxId._request = stripe.customers._request;

describe('TaxId Resource', () => {
describe('create', () => {
it('Sends the correct request', () => {
const data = {
type: 'eu_vat',
value: '11111',
};
taxId.create(data);
stripe.taxIds.create(CUSTOMER_TEST_ID, data);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'POST',
url: `/v1/customers/${CUSTOMER_TEST_ID}/tax_ids`,
Expand All @@ -31,7 +25,7 @@ describe('TaxId Resource', () => {

describe('delete', () => {
it('Sends the correct request', () => {
taxId.del(TAX_ID_TEST_ID);
stripe.taxIds.del(CUSTOMER_TEST_ID, TAX_ID_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'DELETE',
url: `/v1/customers/${CUSTOMER_TEST_ID}/tax_ids/${TAX_ID_TEST_ID}`,
Expand All @@ -43,7 +37,7 @@ describe('TaxId Resource', () => {

describe('list', () => {
it('Sends the correct request', () => {
taxId.list();
stripe.taxIds.list(CUSTOMER_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/customers/${CUSTOMER_TEST_ID}/tax_ids`,
Expand All @@ -55,7 +49,7 @@ describe('TaxId Resource', () => {

describe('retrieve', () => {
it('Sends the correct request', () => {
taxId.retrieve(TAX_ID_TEST_ID);
stripe.taxIds.retrieve(CUSTOMER_TEST_ID, TAX_ID_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/customers/${CUSTOMER_TEST_ID}/tax_ids/${TAX_ID_TEST_ID}`,
Expand Down
17 changes: 4 additions & 13 deletions test/resources/TransferReversals.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
'use strict';

const resources = require('../../lib/stripe').resources;
const stripe = require('../../testUtils').getSpyableStripe();
const expect = require('chai').expect;

const TRANSFER_TEST_ID = 'transferIdTest999';
const REVERSAL_TEST_ID = 'reversalIdTest999';

// Create new CustomerCard instance with pre-filled customerId:
const transferReversal = new resources.TransferReversals(stripe, {
transferId: TRANSFER_TEST_ID,
});

// Use spy from existing resource:
transferReversal._request = stripe.customers._request;

describe('TransferReversal Resource', () => {
describe('retrieve', () => {
it('Sends the correct request', () => {
transferReversal.retrieve(REVERSAL_TEST_ID);
stripe.transferReversals.retrieve(TRANSFER_TEST_ID, REVERSAL_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/transfers/${TRANSFER_TEST_ID}/reversals/${REVERSAL_TEST_ID}`,
Expand All @@ -30,7 +21,7 @@ describe('TransferReversal Resource', () => {

describe('create', () => {
it('Sends the correct request', () => {
transferReversal.create({
stripe.transferReversals.create(TRANSFER_TEST_ID, {
amount: 100,
});
expect(stripe.LAST_REQUEST).to.deep.equal({
Expand All @@ -44,7 +35,7 @@ describe('TransferReversal Resource', () => {

describe('update', () => {
it('Sends the correct request', () => {
transferReversal.update(REVERSAL_TEST_ID, {
stripe.transferReversals.update(TRANSFER_TEST_ID, REVERSAL_TEST_ID, {
metadata: {key: 'value'},
});
expect(stripe.LAST_REQUEST).to.deep.equal({
Expand All @@ -58,7 +49,7 @@ describe('TransferReversal Resource', () => {

describe('list', () => {
it('Sends the correct request', () => {
transferReversal.list();
stripe.transferReversals.list(TRANSFER_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: `/v1/transfers/${TRANSFER_TEST_ID}/reversals`,
Expand Down

0 comments on commit 65b2df2

Please sign in to comment.