habitica-self-host/test/api/unit/libs/payments/stripe/oneTimePayments.test.js

317 lines
9.7 KiB
JavaScript
Raw Normal View History

Stripe: upgrade module and API, switch to Checkout (#12785) * upgrade stripe module * switch stripe api to latest version * fix api version in tests * start upgrading client and server * client: switch to redirect * implement checkout session creation for gems, start implementing webhooks * stripe: start refactoring one time payments * working gems and gift payments * start adding support for subscriptions * stripe: migrate subscriptions and fix cancelling sub * allow upgrading group plans * remove console.log statements * group plans: upgrade from static page / create new one * fix #11885, correct group plan modal title * silence more stripe webhooks * fix group plans redirects * implement editing payment method * start cleaning up code * fix(stripe): update in-code docs, fix eslint issues * subscriptions tests * remove and skip old tests * skip integration tests * fix client build * stripe webhooks: throw error if request fails * subscriptions: correctly pass groupId * remove console.log * stripe: add unit tests for one time payments * wip: stripe checkout tests * stripe createCheckoutSession unit tests * stripe createCheckoutSession unit tests * stripe createCheckoutSession unit tests (editing card) * fix existing webhooks tests * add new webhooks tests * add more webhooks tests * fix lint * stripe integration tests * better error handling when retrieving customer from stripe * client: remove unused strings and improve error handling * payments: limit gift message length (server) * payments: limit gift message length (client) * fix redirects when payment is cancelled * add back "subUpdateCard" string * fix redirects when editing a sub card, use proper names for products, check subs when gifting
2020-12-14 14:59:17 +00:00
import apiError from '../../../../../../website/server/libs/apiError';
import common from '../../../../../../website/common';
import {
getOneTimePaymentInfo,
applyGemPayment,
} from '../../../../../../website/server/libs/payments/stripe/oneTimePayments';
import * as subscriptions from '../../../../../../website/server/libs/payments/stripe/subscriptions';
import { model as User } from '../../../../../../website/server/models/user';
import payments from '../../../../../../website/server/libs/payments/payments';
const { i18n } = common;
describe('Stripe - One Time Payments', () => {
describe('getOneTimePaymentInfo', () => {
let user;
beforeEach(() => {
user = new User();
sandbox.stub(subscriptions, 'checkSubData');
});
describe('gemsBlock', () => {
it('returns the gemsBlock and amount', async () => {
const { gemsBlock, amount, subscription } = await getOneTimePaymentInfo('21gems', null, user);
expect(gemsBlock).to.equal(common.content.gems['21gems']);
expect(amount).to.equal(gemsBlock.price);
expect(amount).to.equal(499);
expect(subscription).to.be.null;
expect(subscriptions.checkSubData).to.not.be.called;
});
it('throws if the gemsBlock does not exist', async () => {
await expect(getOneTimePaymentInfo('not existant', null, user))
.to.eventually.be.rejected.and.to.eql({
httpCode: 400,
name: 'BadRequest',
message: apiError('invalidGemsBlock'),
});
});
it('throws if the user cannot receive gems', async () => {
sandbox.stub(user, 'canGetGems').resolves(false);
await expect(getOneTimePaymentInfo('21gems', null, user))
.to.eventually.be.rejected.and.to.eql({
httpCode: 401,
name: 'NotAuthorized',
message: i18n.t('groupPolicyCannotGetGems'),
});
});
});
describe('gift', () => {
it('throws if the receiver does not exist', async () => {
const gift = {
type: 'gems',
uuid: 'invalid',
gems: {
amount: 3,
},
};
await expect(getOneTimePaymentInfo(null, gift, user))
.to.eventually.be.rejected.and.to.eql({
httpCode: 404,
name: 'NotFound',
message: i18n.t('userWithIDNotFound', { userId: 'invalid' }),
});
});
it('throws if the user cannot receive gems', async () => {
const receivingUser = new User();
await receivingUser.save();
sandbox.stub(User.prototype, 'canGetGems').resolves(false);
const gift = {
type: 'gems',
uuid: receivingUser._id,
gems: {
amount: 2,
},
};
await expect(getOneTimePaymentInfo(null, gift, user))
.to.eventually.be.rejected.and.to.eql({
httpCode: 401,
name: 'NotAuthorized',
message: i18n.t('groupPolicyCannotGetGems'),
});
});
it('throws if the amount of gems is <= 0', async () => {
const receivingUser = new User();
await receivingUser.save();
const gift = {
type: 'gems',
uuid: receivingUser._id,
gems: {
amount: 0,
},
};
await expect(getOneTimePaymentInfo(null, gift, user))
.to.eventually.be.rejected.and.to.eql({
httpCode: 400,
name: 'BadRequest',
message: i18n.t('badAmountOfGemsToPurchase'),
});
});
it('throws if the subscription block does not exist', async () => {
const receivingUser = new User();
await receivingUser.save();
const gift = {
type: 'subscription',
uuid: receivingUser._id,
subscription: {
key: 'invalid',
},
};
await expect(getOneTimePaymentInfo(null, gift, user))
.to.eventually.throw;
});
it('returns the amount (gems)', async () => {
const receivingUser = new User();
await receivingUser.save();
const gift = {
type: 'gems',
uuid: receivingUser._id,
gems: {
amount: 4,
},
};
expect(subscriptions.checkSubData).to.not.be.called;
const { gemsBlock, amount, subscription } = await getOneTimePaymentInfo(null, gift, user);
expect(gemsBlock).to.equal(null);
expect(amount).to.equal('100');
expect(subscription).to.be.null;
});
it('returns the amount (subscription)', async () => {
const receivingUser = new User();
await receivingUser.save();
const gift = {
type: 'subscription',
uuid: receivingUser._id,
subscription: {
key: 'basic_3mo',
},
};
const sub = common.content.subscriptionBlocks['basic_3mo']; // eslint-disable-line dot-notation
const { gemsBlock, amount, subscription } = await getOneTimePaymentInfo(null, gift, user);
expect(subscriptions.checkSubData).to.be.calledOnce;
expect(subscriptions.checkSubData).to.be.calledWith(sub, false, null);
expect(gemsBlock).to.equal(null);
expect(amount).to.equal('1500');
expect(Number(amount)).to.equal(sub.price * 100);
expect(subscription).to.equal(sub);
});
});
});
describe('applyGemPayment', () => {
let user;
let customerId;
let subKey;
let userFindByIdStub;
let paymentsCreateSubSpy;
let paymentBuyGemsStub;
beforeEach(async () => {
subKey = 'basic_3mo';
user = new User();
await user.save();
customerId = 'test-id';
paymentsCreateSubSpy = sandbox.stub(payments, 'createSubscription');
paymentsCreateSubSpy.resolves({});
paymentBuyGemsStub = sandbox.stub(payments, 'buyGems');
paymentBuyGemsStub.resolves({});
});
it('throws if the user does not exist', async () => {
const metadata = { userId: 'invalid' };
const session = { metadata, customer: customerId };
await expect(applyGemPayment(session))
.to.eventually.be.rejected.and.to.eql({
httpCode: 404,
name: 'NotFound',
message: i18n.t('userWithIDNotFound', { userId: metadata.userId }),
});
});
it('throws if the receiving user does not exist', async () => {
const metadata = { userId: 'invalid' };
const session = { metadata, customer: customerId };
await expect(applyGemPayment(session))
.to.eventually.be.rejected.and.to.eql({
httpCode: 404,
name: 'NotFound',
message: i18n.t('userWithIDNotFound', { userId: metadata.userId }),
});
});
it('throws if the gems block does not exist', async () => {
const gift = {
type: 'gems',
uuid: 'invalid',
gems: {
amount: 16,
},
};
const metadata = { userId: user._id, gift: JSON.stringify(gift) };
const session = { metadata, customer: customerId };
await expect(applyGemPayment(session))
.to.eventually.be.rejected.and.to.eql({
httpCode: 404,
name: 'NotFound',
message: i18n.t('userWithIDNotFound', { userId: 'invalid' }),
});
});
describe('with existing user', () => {
beforeEach(() => {
const execStub = sandbox.stub().resolves(user);
userFindByIdStub = sandbox.stub(User, 'findById');
userFindByIdStub.withArgs(user._id).returns({ exec: execStub });
});
it('buys gems', async () => {
const metadata = { userId: user._id, gemsBlock: '21gems' };
const session = { metadata, customer: customerId };
await applyGemPayment(session);
expect(paymentBuyGemsStub).to.be.calledOnce;
expect(paymentBuyGemsStub).to.be.calledWith({
user,
customerId,
paymentMethod: 'Stripe',
gift: undefined,
gemsBlock: common.content.gems['21gems'],
});
});
it('gift gems', async () => {
const receivingUser = new User();
const execStub = sandbox.stub().resolves(receivingUser);
userFindByIdStub.withArgs(receivingUser._id).returns({ exec: execStub });
const gift = {
type: 'gems',
uuid: receivingUser._id,
gems: {
amount: 16,
},
};
sandbox.stub(JSON, 'parse').returns(gift);
const metadata = { userId: user._id, gift: JSON.stringify(gift) };
const session = { metadata, customer: customerId };
await applyGemPayment(session);
expect(paymentBuyGemsStub).to.be.calledOnce;
expect(paymentBuyGemsStub).to.be.calledWith({
user,
customerId,
paymentMethod: 'Gift',
gift,
gemsBlock: undefined,
});
});
it('gift sub', async () => {
const receivingUser = new User();
const execStub = sandbox.stub().resolves(receivingUser);
userFindByIdStub.withArgs(receivingUser._id).returns({ exec: execStub });
const gift = {
type: 'subscription',
uuid: receivingUser._id,
subscription: {
key: subKey,
},
};
sandbox.stub(JSON, 'parse').returns(gift);
const metadata = { userId: user._id, gift: JSON.stringify(gift) };
const session = { metadata, customer: customerId };
await applyGemPayment(session);
expect(paymentsCreateSubSpy).to.be.calledOnce;
expect(paymentsCreateSubSpy).to.be.calledWith({
user,
customerId,
paymentMethod: 'Gift',
gift,
gemsBlock: undefined,
});
});
});
});
});