mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
V3 payments 6 (#7104)
* payments api: cancelSubscription * some more tests for amazon payments * promisifying amazon payments * somehow payment stub is not working * cleaning up tests * renaming tests in api/v3/integration/payments * improvements * cleanup, lint * fixes as per comments * moment.zone() is back in.
This commit is contained in:
committed by
Matteo Pagliazzi
parent
73e4c719b2
commit
fa21577c46
@@ -0,0 +1,21 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
|
||||
describe('payments : amazon #subscribeCancel', () => {
|
||||
let endpoint = '/payments/amazon/subscribeCancel';
|
||||
let user;
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('verifies subscription', async () => {
|
||||
await expect(user.get(endpoint)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('missingSubscription'),
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import {
|
||||
generateUser,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
|
||||
describe('payments - amazon - #checkout', () => {
|
||||
let endpoint = '/payments/amazon/checkout';
|
||||
let user;
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('verifies credentials', async (done) => {
|
||||
try {
|
||||
await user.post(endpoint);
|
||||
} catch (e) {
|
||||
expect(e.error).to.eql('BadRequest');
|
||||
expect(e.message.type).to.eql('InvalidParameterValue');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import {
|
||||
generateUser,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
|
||||
describe('payments - amazon - #createOrderReferenceId', () => {
|
||||
let endpoint = '/payments/amazon/createOrderReferenceId';
|
||||
let user;
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('verifies billingAgreementId', async (done) => {
|
||||
try {
|
||||
await user.post(endpoint);
|
||||
} catch (e) {
|
||||
// Parameter AWSAccessKeyId cannot be empty.
|
||||
expect(e.error).to.eql('BadRequest');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
|
||||
describe('payments - amazon - #subscribe', () => {
|
||||
let endpoint = '/payments/amazon/subscribe';
|
||||
let user;
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('verifies subscription code', async () => {
|
||||
await expect(user.post(endpoint)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('missingSubscriptionCode'),
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,103 +0,0 @@
|
||||
import * as amzLib from '../../../../../website/src/libs/api-v3/amazonPayments';
|
||||
// import * as amzStub from 'amazon-payments';
|
||||
import amazonPayments from 'amazon-payments';
|
||||
var User = require('mongoose').model('User');
|
||||
|
||||
describe('amazonPayments', () => {
|
||||
beforeEach(() => {
|
||||
});
|
||||
|
||||
describe('#getTokenInfo stubbed', () => {
|
||||
let thisToken = 'this token info';
|
||||
let amzOldConnect;
|
||||
|
||||
beforeEach(() => {
|
||||
amzOldConnect = amazonPayments.connect;
|
||||
amazonPayments.connect = () => {
|
||||
let api = { getTokenInfo: (token, cb) => {
|
||||
return cb(undefined, thisToken);
|
||||
} };
|
||||
return { api };
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
amazonPayments.connect = amzOldConnect;
|
||||
});
|
||||
|
||||
it('returns tokenInfo', async (done) => {
|
||||
let result = await amzLib.getTokenInfo();
|
||||
expect(result).to.eql(thisToken);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getTokenInfo', () => {
|
||||
it('validates access_token parameter', async (done) => {
|
||||
try {
|
||||
await amzLib.getTokenInfo();
|
||||
} catch (e) {
|
||||
expect(e.type).to.eql('invalid_request');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#createOrderReferenceId', () => {
|
||||
it('verifies billingAgreementId', async (done) => {
|
||||
try {
|
||||
let inputSet = {};
|
||||
delete inputSet.Id;
|
||||
await amzLib.createOrderReferenceId(inputSet);
|
||||
} catch (e) {
|
||||
|
||||
/* console.log('error!', e);
|
||||
console.log('error keys!', Object.keys(e));
|
||||
for (var key in e) {
|
||||
console.log(e[key]);
|
||||
} // */
|
||||
|
||||
expect(e.type).to.eql('InvalidParameterValue');
|
||||
expect(e.body.ErrorResponse.Error.Message).to.eql('Parameter AWSAccessKeyId cannot be empty.');
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
xit('succeeds', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#checkout', () => {
|
||||
xit('succeeds');
|
||||
});
|
||||
|
||||
describe('#setOrderReferenceDetails', () => {
|
||||
xit('succeeds');
|
||||
});
|
||||
|
||||
describe('#confirmOrderReference', () => {
|
||||
xit('succeeds');
|
||||
});
|
||||
|
||||
describe('#authorize', () => {
|
||||
xit('succeeds');
|
||||
|
||||
xit('was declined');
|
||||
|
||||
xit('had an error');
|
||||
});
|
||||
|
||||
describe('#closeOrderReference', () => {
|
||||
xit('succeeds');
|
||||
});
|
||||
|
||||
describe.only('#executePayment', () => {
|
||||
it('succeeds not as a gift', () => {
|
||||
});
|
||||
|
||||
it('succeeds as a gift', () => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
75
test/api/v3/unit/libs/payments.test.js
Normal file
75
test/api/v3/unit/libs/payments.test.js
Normal file
@@ -0,0 +1,75 @@
|
||||
import * as sender from '../../../../../website/src/libs/api-v3/email';
|
||||
import * as api from '../../../../../website/src/libs/api-v3/payments';
|
||||
import { model as User } from '../../../../../website/src/models/user';
|
||||
import moment from 'moment';
|
||||
|
||||
describe('payments/index', () => {
|
||||
let fakeSend;
|
||||
let data;
|
||||
let user;
|
||||
|
||||
describe('#createSubscription', () => {
|
||||
beforeEach(async () => {
|
||||
user = new User();
|
||||
});
|
||||
|
||||
it('succeeds', async () => {
|
||||
data = { user, sub: { key: 'basic_3mo' } };
|
||||
expect(user.purchased.plan.planId).to.not.exist;
|
||||
await api.createSubscription(data);
|
||||
expect(user.purchased.plan.planId).to.exist;
|
||||
});
|
||||
});
|
||||
|
||||
describe('#cancelSubscription', () => {
|
||||
beforeEach(() => {
|
||||
fakeSend = sinon.spy(sender, 'sendTxn');
|
||||
data = { user: new User() };
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fakeSend.restore();
|
||||
});
|
||||
|
||||
it('plan.extraMonths is defined', () => {
|
||||
api.cancelSubscription(data);
|
||||
let terminated = data.user.purchased.plan.dateTerminated;
|
||||
data.user.purchased.plan.extraMonths = 2;
|
||||
api.cancelSubscription(data);
|
||||
let difference = Math.abs(moment(terminated).diff(data.user.purchased.plan.dateTerminated, 'days'));
|
||||
expect(difference - 60).to.be.lessThan(3); // the difference is approximately two months, +/- 2 days
|
||||
});
|
||||
|
||||
it('plan.extraMonth is a fraction', () => {
|
||||
api.cancelSubscription(data);
|
||||
let terminated = data.user.purchased.plan.dateTerminated;
|
||||
data.user.purchased.plan.extraMonths = 0.3;
|
||||
api.cancelSubscription(data);
|
||||
let difference = Math.abs(moment(terminated).diff(data.user.purchased.plan.dateTerminated, 'days'));
|
||||
expect(difference - 10).to.be.lessThan(3); // the difference should be 10 days.
|
||||
});
|
||||
|
||||
it('nextBill is defined', () => {
|
||||
api.cancelSubscription(data);
|
||||
let terminated = data.user.purchased.plan.dateTerminated;
|
||||
data.nextBill = moment().add({ days: 25 });
|
||||
api.cancelSubscription(data);
|
||||
let difference = Math.abs(moment(terminated).diff(data.user.purchased.plan.dateTerminated, 'days'));
|
||||
expect(difference - 5).to.be.lessThan(2); // the difference should be 5 days, +/- 1 day
|
||||
});
|
||||
|
||||
it('saves the canceled subscription for the user', () => {
|
||||
expect(data.user.purchased.plan.dateTerminated).to.not.exist;
|
||||
api.cancelSubscription(data);
|
||||
expect(data.user.purchased.plan.dateTerminated).to.exist;
|
||||
});
|
||||
|
||||
it('sends a text', async () => {
|
||||
await api.cancelSubscription(data);
|
||||
sinon.assert.calledOnce(fakeSend);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#buyGems', async () => {
|
||||
});
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
|
||||
describe('payments/index', () => {
|
||||
beforeEach(() => {
|
||||
});
|
||||
|
||||
describe('#createSubscription', async () => {
|
||||
});
|
||||
|
||||
describe('#buyGems', async () => {
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user