Skip to content

Commit

Permalink
Add tests for PayPal Complete 3DS
Browse files Browse the repository at this point in the history
  • Loading branch information
douglaslise committed May 23, 2023
1 parent c41c714 commit 4caeaa7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/recurly/risk/three-d-secure/strategy/paypal-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export default class PayPalCompleteStrategy extends ThreeDSecureStrategy {
const { config: { publicKey } } = recurly;
const defaultEventName = 'paypalcomplete-3ds-challenge';

const redirectUri = encodeURIComponent(`?key=${publicKey}&event=${defaultEventName}`);
const approveURLWithRedirect = `${approveURL}&redirect_uri=${recurly.url(`/three_d_secure/finish${redirectUri}`)}`;
const finishURL = recurly.url(`/three_d_secure/finish?key=${publicKey}&event=${defaultEventName}`);
const approveURLWithRedirect = `${approveURL}&redirect_uri=${encodeURIComponent(finishURL)}`;

const payload = {
redirect_url: approveURLWithRedirect,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "three_d_secure_action",
"id": "action-token-test",
"three_d_secure": {
"params": {
"redirect": {
"url": "https://www.sandbox.paypal.com/webapps/helios?action=verify&flow=3ds&cart_id=7G156062VE7988426"
}
}
},
"gateway": {
"type": "paypal_commerce",
"credentials": {}
},
"transaction": {
"amount": 10.0,
"amount_in_cents": 1000
}
}
84 changes: 84 additions & 0 deletions test/unit/risk/three-d-secure/strategy/paypal-complete.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import assert from 'assert';
import { applyFixtures } from '../../../support/fixtures';
import { initRecurly, testBed } from '../../../support/helpers';
import PaypalCompleteStrategy from '../../../../../lib/recurly/risk/three-d-secure/strategy/paypal-complete';
import actionToken from '@recurly/public-api-test-server/fixtures/tokens/action-token-paypal-complete.json';
import { Frame } from '../../../../../lib/recurly/frame';

describe('PaypalCompleteStrategy', function () {
this.ctx.fixture = 'threeDSecure';

applyFixtures();

beforeEach(function (done) {
const recurly = this.recurly = initRecurly();
const risk = recurly.Risk();
const threeDSecure = this.threeDSecure = risk.ThreeDSecure({ actionTokenId: 'action-token-test' });
this.target = testBed().querySelector('#three-d-secure-container');

this.sandbox = sinon.createSandbox();
this.sandbox.spy(recurly, 'Frame');

this.strategy = new PaypalCompleteStrategy({ threeDSecure, actionToken });
this.strategy.whenReady(() => done());
});

afterEach(function () {
this.strategy.remove();
this.sandbox.restore();
});

describe.only('attach', function () {
it('creates a frame and appends finish URL to the redirect URL', function () {
const { strategy, target, recurly } = this;
strategy.attach(target);
assert(recurly.Frame.calledOnce);

const redirectURL = actionToken.three_d_secure.params.redirect.url;
const finishURL = `http://${window.location.host}/api/three_d_secure/finish?key=test&event=paypalcomplete-3ds-challenge`;
const redirectWithFinishURL = `${redirectURL}&redirect_uri=${encodeURIComponent(finishURL)}`;

assert(recurly.Frame.calledWithMatch({
path: '/three_d_secure/start',
container: strategy.container,
type: Frame.TYPES.IFRAME,
payload: {
redirect_url: redirectWithFinishURL,
three_d_secure_action_token_id: 'action-token-test'
},
defaultEventName: 'paypalcomplete-3ds-challenge'
}));
});

it('when liability shift is possible then emits DONE event', function () {
const { strategy, target } = this;
this.sandbox.spy(strategy, 'emit');
strategy.attach(target);

strategy.frame.emit('done', { liability_shift: 'POSSIBLE' });

assert(strategy.emit.calledWithMatch('done', { liability_shift: 'POSSIBLE' }));
});

it('when liability shift is not possible then emits ERROR event', function () {
const { strategy, target } = this;
const { threeDSecure } = strategy;
this.sandbox.spy(threeDSecure, 'error');
strategy.attach(target);

strategy.frame.emit('done', { liability_shift: 'NO' });

assert(threeDSecure.error.calledWithMatch('3ds-auth-error', { cause: 'Liability shift not possible' }));
});
});

describe('remove', function () {
it('destroys any existing frame', function () {
const { strategy, target, sandbox } = this;
strategy.attach(target);
sandbox.spy(strategy.frame, 'destroy');
strategy.remove();
assert(strategy.frame.destroy.calledOnce);
});
});
});

0 comments on commit 4caeaa7

Please sign in to comment.