Skip to content

Commit

Permalink
WIP need to fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gilv93 committed Sep 17, 2024
1 parent 2173459 commit 0ea625d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 12 deletions.
18 changes: 9 additions & 9 deletions lib/recurly/risk/risk.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ export class Risk {
* @return {Promise}
*/
static preflight ({ recurly, number, month, year, cvv }) {
return recurly.request.get({ route: Risk.resolveRoute(recurly) })
function resolveRoute (recurly) {
let route = '/risk/preflights'
if (recurly.config.risk.threeDSecure.proactive.enabled) {
route += `?proactive=true&gatewayCode=${recurly.config.risk.threeDSecure.proactive.gateway_code}`
}
return route
}

return recurly.request.get({ route: resolveRoute(recurly) })
.then(({ preflights }) => {
debug('received preflight instructions', preflights);
return ThreeDSecure.preflight({ recurly, number, month, year, cvv, preflights });
Expand All @@ -70,14 +78,6 @@ export class Risk {
}));
}

static resolveRoute (recurly) {
let route = '/risk/preflights'
if (recurly.config.risk.threeDSecure.proactive.enabled) {
route += `?proactive=true&gatewayCode=${recurly.config.risk.threeDSecure.proactive.gateway_code}`
}
return route;
}

constructor ({ recurly }) {
this.recurly = recurly;
this.concerns = [];
Expand Down
1 change: 1 addition & 0 deletions lib/util/readiness-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class ReadinessEmitter extends Emitter {
* @return {[type]} [description]
*/
markReady () {
console.log('look we are ready')
this._ready = true;
this.emit('ready');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "three_d_secure_proactive_action",
"id": "proactive-token-test",
"gateway": {
"code": "1234567890",
"type": "test"
},
"three_d_secure": {
"params": {
"challengeType": "challenge"
}
}
}
13 changes: 13 additions & 0 deletions test/unit/risk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ describe('Risk', function () {
});
});

it('appends proactive data to the preflight request when enabled', function (done) {
const { recurly, sandbox, bin } = this;
recurly.config.risk.threeDSecure.proactive.enabled = true;
recurly.config.risk.threeDSecure.proactive.gateway_code = 'test-gateway-code';
sandbox.spy(recurly.request, 'get');
Risk.preflight({ recurly, bin })
.done(results => {
assert(recurly.request.get.calledOnce);
assert(recurly.request.get.calledWithMatch({ route: '/risk/preflights?proactive=true&gatewayCode=test-gateway-code' }));
done();
});
});

describe('when some results are timeouts', function () {
beforeEach(function () {
this.stubPreflightResults = [
Expand Down
73 changes: 72 additions & 1 deletion test/unit/risk/three-d-secure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('ThreeDSecure', function () {

beforeEach(function (done) {
const actionTokenId = this.actionTokenId = 'action-token-test';
const proactiveTokenId = this.proactiveTokenId = 'proactive-token-test';
const recurly = this.recurly = initRecurly();
const risk = this.risk = { add: sinon.stub(), remove: sinon.stub(),concerns: [], recurly };
const sandbox = this.sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -179,9 +180,17 @@ describe('ThreeDSecure', function () {
new ThreeDSecure({ risk });
}, /Option actionTokenId must be a three_d_secure_action_token_id/);
});

it('does not throw an error if proactiveTokenId is provided instead', function () {
const { risk, proactiveTokenId } = this;

assert.doesNotThrow(() => {
new ThreeDSecure({ risk, proactiveTokenId });
});
});
});

describe('when an actionTokenId is valid', function () {
describe.only('when an actionTokenId is valid', function () {
it('sets the strategy according to the gateway type of the action token', function (done) {
const { risk } = this;
[
Expand Down Expand Up @@ -242,6 +251,68 @@ describe('ThreeDSecure', function () {
});
});

describe.only('when a proactiveTokenId is provided', function () {
it('throws an error if it is not valid', function (done) {
const { risk } = this;
const threeDSecure = new ThreeDSecure({ risk, proactiveTokenId: 'invalid-token-id' });

threeDSecure.on('error', err => {
assert.strictEqual(err.code, 'not-found');
assert.strictEqual(err.message, 'Token not found');
done();
});
});

it('sets the strategy to TestStrategy', function (done) {
const { risk, proactiveTokenId } = this;
const threeDSecure = new ThreeDSecure({ risk, proactiveTokenId });
threeDSecure.whenReady(() => {
assert(threeDSecure.strategy instanceof TestStrategy);
done();
});
});

it('constructs the strategy with the proactive token', function (done) {
const { risk, proactiveTokenId } = this;
const threeDSecure = new ThreeDSecure({ risk, proactiveTokenId });
threeDSecure.whenReady(() => {
const { strategy } = threeDSecure;
assert(strategy instanceof TestStrategy);
assert.strictEqual(strategy.proactiveToken.id, proactiveTokenId);
done();
});
});

it('reports the strategy name', function (done) {
const { risk, proactiveTokenId } = this;

const threeDSecure = new ThreeDSecure({ risk, proactiveTokenId });
risk.recurly.reporter.send.reset();

threeDSecure.whenReady(() => {
assert(risk.recurly.reporter.send.calledOnce);
assert(risk.recurly.reporter.send.calledWithMatch(
'three-d-secure:ready',
{ concernId: threeDSecure.id, strategy: 'test' }
));
done();
});
});

it('calls onStrategyDone when a strategy completes', function (done) {
const { sandbox, threeDSecure } = this;
const example = { arbitrary: 'test-payload' };
sandbox.spy(threeDSecure, 'onStrategyDone');

threeDSecure.whenReady(() => {
threeDSecure.strategy.emit('done', example);
assert(threeDSecure.onStrategyDone.calledOnce);
assert(threeDSecure.onStrategyDone.calledWithMatch(example));
done();
});
});
});

describe('challengeWindowSize', function() {
it('validates', function () {
const challengeWindowSize = 'xx';
Expand Down
4 changes: 2 additions & 2 deletions test/unit/token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Recurly } from '../../lib/recurly';
import { applyFixtures } from './support/fixtures';
import { initRecurly, testBed } from './support/helpers';

describe.only(`Recurly.token`, function () {
describe(`Recurly.token`, function () {
// Some of these tests can take a while to stand up fields and receive reponses
this.timeout(15000);

Expand Down Expand Up @@ -317,7 +317,7 @@ describe.only(`Recurly.token`, function () {
});
});

describe.only('when given an invalid json response', function () {
describe('when given an invalid json response', function () {
prepareExample(Object.assign({}, valid, {
number: '5454545454545454'
}), builder);
Expand Down

0 comments on commit 0ea625d

Please sign in to comment.