Merge pull request #2611 from overleaf/ta-cmg-recurly-email-update-test-plus

Improve Recurly API Acceptance Test

GitOrigin-RevId: 89f4707c9baaa20e59f64821daa36551ad700118
This commit is contained in:
Eric Mc Sween 2020-02-27 07:46:21 -05:00 committed by Copybot
parent 174af14d46
commit 17969c50ce
5 changed files with 68 additions and 67 deletions

View file

@ -34,8 +34,9 @@ describe('Subscriptions', function() {
return done(error)
}
expect(statusCode).to.equal(200)
// the actual email update is not tested as the mocked Recurly API
// doesn't handle it
expect(this.recurlyUser.email).to.equal(
this.recurlySubscription.account.email
)
done()
})
})

View file

@ -16,6 +16,7 @@ const User = require('./helpers/User')
const { Subscription } = require('../../../app/src/models/Subscription')
const { Institution } = require('../../../app/src/models/Institution')
const SubscriptionViewModelBuilder = require('../../../app/src/Features/Subscription/SubscriptionViewModelBuilder')
const RecurlySubscription = require('./helpers/RecurlySubscription')
const MockRecurlyApi = require('./helpers/MockRecurlyApi')
const MockV1Api = require('./helpers/MockV1Api')
@ -61,56 +62,45 @@ describe('Subscriptions', function() {
describe('when the user has a subscription with recurly', function() {
beforeEach(function(done) {
MockRecurlyApi.accounts['mock-account-id'] = this.accounts = {
hosted_login_token: 'mock-login-token',
email: 'mock@email.com'
}
MockRecurlyApi.subscriptions[
'mock-subscription-id'
] = this.subscription = {
plan_code: 'collaborator',
this.recurlySubscription = new RecurlySubscription({
adminId: this.user._id,
planCode: 'collaborator',
tax_in_cents: 100,
tax_rate: 0.2,
unit_amount_in_cents: 500,
currency: 'GBP',
current_period_ends_at: new Date(2018, 4, 5),
state: 'active',
account_id: 'mock-account-id',
trial_ends_at: new Date(2018, 6, 7)
}
trial_ends_at: new Date(2018, 6, 7),
account: {
hosted_login_token: 'mock-login-token',
email: 'mock@email.com'
}
})
MockRecurlyApi.coupons = this.coupons = {
'test-coupon-1': { description: 'Test Coupon 1' },
'test-coupon-2': { description: 'Test Coupon 2' },
'test-coupon-3': { name: 'TestCoupon3' }
}
Subscription.create(
{
admin_id: this.user._id,
manager_ids: [this.user._id],
recurlySubscription_id: 'mock-subscription-id',
planCode: 'collaborator'
},
error => {
if (error != null) {
return done(error)
}
return SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
this.user,
(error, data) => {
this.data = data
if (error != null) {
return done(error)
}
return done()
}
)
this.recurlySubscription.ensureExists(error => {
if (error != null) {
return done(error)
}
)
return SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
this.user,
(error, data) => {
this.data = data
if (error != null) {
return done(error)
}
return done()
}
)
})
})
after(function(done) {
MockRecurlyApi.accounts = {}
MockRecurlyApi.subscriptions = {}
MockRecurlyApi.mockSubscriptions = []
MockRecurlyApi.coupons = {}
MockRecurlyApi.redemptions = {}
Subscription.remove(
@ -141,7 +131,7 @@ describe('Subscriptions', function() {
trial_ends_at: new Date(2018, 6, 7),
trialEndsAtFormatted: '7th July 2018',
account: {
account_code: 'mock-account-id',
account_code: this.user._id,
email: 'mock@email.com',
hosted_login_token: 'mock-login-token'
}
@ -153,7 +143,7 @@ describe('Subscriptions', function() {
})
it('should include redeemed coupons', function(done) {
MockRecurlyApi.redemptions['mock-account-id'] = [
MockRecurlyApi.redemptions[this.user._id] = [
{ state: 'active', coupon_code: 'test-coupon-1' },
{ state: 'inactive', coupon_code: 'test-coupon-2' },
{ state: 'active', coupon_code: 'test-coupon-3' }

View file

@ -19,25 +19,31 @@ const SubscriptionController = require('../../../../app/src/Features/Subscriptio
app.use(bodyParser.json())
module.exports = MockRecurlyApi = {
subscriptions: {},
accounts: {},
mockSubscriptions: [],
redemptions: {},
coupons: {},
addSubscription(subscription) {
this.subscriptions[subscription.uuid] = subscription
addMockSubscription(recurlySubscription) {
this.mockSubscriptions.push(recurlySubscription)
},
addAccount(account) {
this.accounts[account.id] = account
getMockSubscriptionByAccountId(accountId) {
return this.mockSubscriptions.find(
mockSubscription => mockSubscription.account.id === accountId
)
},
getMockSubscriptionById(uuid) {
return this.mockSubscriptions.find(
mockSubscription => mockSubscription.uuid === uuid
)
},
run() {
app.get('/subscriptions/:id', (req, res, next) => {
const subscription = this.subscriptions[req.params.id]
const subscription = this.getMockSubscriptionById(req.params.id)
if (subscription == null) {
return res.status(404).end()
} else {
@ -54,7 +60,7 @@ module.exports = MockRecurlyApi = {
<unit_amount_in_cents type="integer">${
subscription.unit_amount_in_cents
}</unit_amount_in_cents>
<account href="accounts/${subscription.account_id}" />
<account href="accounts/${subscription.account.id}" />
<trial_ends_at type="datetime">${subscription.trial_ends_at}</trial_ends_at>
</subscription>\
`)
@ -62,15 +68,17 @@ module.exports = MockRecurlyApi = {
})
app.get('/accounts/:id', (req, res, next) => {
const account = this.accounts[req.params.id]
if (account == null) {
const subscription = this.getMockSubscriptionByAccountId(req.params.id)
if (subscription == null) {
return res.status(404).end()
} else {
return res.send(`\
<account>
<account_code>${req.params.id}</account_code>
<hosted_login_token>${account.hosted_login_token}</hosted_login_token>
<email>${account.email}</email>
<hosted_login_token>${
subscription.account.hosted_login_token
}</hosted_login_token>
<email>${subscription.account.email}</email>
</account>\
`)
}
@ -80,14 +88,15 @@ module.exports = MockRecurlyApi = {
'/accounts/:id',
SubscriptionController.recurlyNotificationParser, // required to parse XML requests
(req, res, next) => {
const account = this.accounts[req.params.id]
if (account == null) {
const subscription = this.getMockSubscriptionByAccountId(req.params.id)
if (subscription == null) {
return res.status(404).end()
} else {
Object.assign(subscription.account, req.body.account)
return res.send(`\
<account>
<account_code>${req.params.id}</account_code>
<email>${account.email}</email>
<email>${subscription.account.email}</email>
</account>\
`)
}

View file

@ -5,13 +5,21 @@ const RecurlyWrapper = require('../../../../app/src/Features/Subscription/Recurl
class RecurlySubscription {
constructor(options = {}) {
options.recurlySubscription_id = ObjectId().toString()
this.subscription = new Subscription(options)
this.uuid = ObjectId().toString()
this.accountId = this.subscription.admin_id.toString()
this.uuid = options.recurlySubscription_id
this.state = options.state || 'active'
this.tax_in_cents = 100
this.tax_rate = 0.2
this.unit_amount_in_cents = 500
this.currency = 'GBP'
this.current_period_ends_at = new Date(2018, 4, 5)
this.trial_ends_at = new Date(2018, 6, 7)
this.account = {
email: options.account && options.account.email
id: this.subscription.admin_id.toString(),
email: options.account && options.account.email,
hosted_login_token: options.account && options.account.hosted_login_token
}
}
@ -20,15 +28,7 @@ class RecurlySubscription {
if (error) {
return callback(error)
}
MockRecurlyApi.addSubscription({
uuid: this.uuid,
account_id: this.accountId,
state: this.state
})
MockRecurlyApi.addAccount({
id: this.accountId,
email: this.account.email
})
MockRecurlyApi.addMockSubscription(this)
callback()
})
}

View file

@ -16,6 +16,7 @@ class Subscription {
this.invited_emails = options.invitedEmails || []
this.teamInvites = options.teamInvites || []
this.planCode = options.planCode
this.recurlySubscription_id = options.recurlySubscription_id
}
ensureExists(callback) {