2019-05-29 05:21:06 -04:00
|
|
|
const { expect } = require('chai')
|
|
|
|
const async = require('async')
|
2021-03-04 09:22:43 -05:00
|
|
|
const UserHelper = require('./helpers/UserHelper')
|
2019-05-29 05:21:06 -04:00
|
|
|
const { Subscription } = require('../../../app/src/models/Subscription')
|
|
|
|
const { Institution } = require('../../../app/src/models/Institution')
|
|
|
|
const SubscriptionViewModelBuilder = require('../../../app/src/Features/Subscription/SubscriptionViewModelBuilder')
|
2020-02-27 07:46:21 -05:00
|
|
|
const RecurlySubscription = require('./helpers/RecurlySubscription')
|
2021-02-25 07:22:24 -05:00
|
|
|
const MockRecurlyApiClass = require('./mocks/MockRecurlyApi')
|
|
|
|
const MockV1ApiClass = require('./mocks/MockV1Api')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-03-04 09:22:43 -05:00
|
|
|
async function buildUsersSubscriptionViewModelPromise(userId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
|
|
|
|
userId,
|
|
|
|
(error, data) => {
|
|
|
|
if (error) reject(error)
|
|
|
|
resolve(data)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
let MockV1Api, MockRecurlyApi
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
before(function () {
|
2021-02-25 07:22:24 -05:00
|
|
|
MockV1Api = MockV1ApiClass.instance()
|
|
|
|
MockRecurlyApi = MockRecurlyApiClass.instance()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('Subscriptions', function () {
|
|
|
|
describe('dashboard', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
let userHelper
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
userHelper = await UserHelper.createUser()
|
|
|
|
this.user = userHelper.user
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not list personal plan', function () {
|
2020-02-05 11:34:29 -05:00
|
|
|
const plans = SubscriptionViewModelBuilder.buildPlansList()
|
|
|
|
expect(plans.individualMonthlyPlans).to.be.a('Array')
|
|
|
|
const personalMonthlyPlan = plans.individualMonthlyPlans.find(
|
|
|
|
plan => plan.planCode === 'personal'
|
|
|
|
)
|
|
|
|
expect(personalMonthlyPlan).to.be.undefined
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user has no subscription', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-03-04 09:22:43 -05:00
|
|
|
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user,
|
|
|
|
(error, data) => {
|
|
|
|
this.data = data
|
2021-03-04 09:22:43 -05:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return done(error)
|
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return no personalSubscription', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.personalSubscription).to.equal(null)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return no memberGroupSubscriptions', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.memberGroupSubscriptions).to.deep.equal([])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user has a subscription with recurly', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-27 07:46:21 -05:00
|
|
|
this.recurlySubscription = new RecurlySubscription({
|
|
|
|
adminId: this.user._id,
|
|
|
|
planCode: 'collaborator',
|
2019-05-29 05:21:06 -04:00
|
|
|
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',
|
2020-02-27 07:46:21 -05:00
|
|
|
trial_ends_at: new Date(2018, 6, 7),
|
|
|
|
account: {
|
|
|
|
hosted_login_token: 'mock-login-token',
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'mock@email.com',
|
|
|
|
},
|
2020-02-27 07:46:21 -05:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
MockRecurlyApi.coupons = this.coupons = {
|
|
|
|
'test-coupon-1': { description: 'Test Coupon 1' },
|
|
|
|
'test-coupon-2': { description: 'Test Coupon 2' },
|
2021-04-27 03:52:58 -04:00
|
|
|
'test-coupon-3': { name: 'TestCoupon3' },
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-02-27 07:46:21 -05:00
|
|
|
this.recurlySubscription.ensureExists(error => {
|
2021-03-04 09:22:43 -05:00
|
|
|
if (error) {
|
2020-02-27 07:46:21 -05:00
|
|
|
return done(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
|
2020-02-27 07:46:21 -05:00
|
|
|
this.user,
|
|
|
|
(error, data) => {
|
|
|
|
this.data = data
|
2021-03-04 09:22:43 -05:00
|
|
|
if (error) {
|
2020-02-27 07:46:21 -05:00
|
|
|
return done(error)
|
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2020-02-27 07:46:21 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
after(function (done) {
|
2020-02-27 07:46:21 -05:00
|
|
|
MockRecurlyApi.mockSubscriptions = []
|
2019-05-29 05:21:06 -04:00
|
|
|
MockRecurlyApi.coupons = {}
|
|
|
|
MockRecurlyApi.redemptions = {}
|
2020-11-03 04:19:05 -05:00
|
|
|
Subscription.deleteOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
admin_id: this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return a personalSubscription with populated recurly data', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const subscription = this.data.personalSubscription
|
|
|
|
expect(subscription).to.exist
|
|
|
|
expect(subscription.planCode).to.equal('collaborator')
|
|
|
|
expect(subscription.recurly).to.exist
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(subscription.recurly).to.deep.equal({
|
2019-05-29 05:21:06 -04:00
|
|
|
activeCoupons: [],
|
|
|
|
billingDetailsLink:
|
|
|
|
'https://test.recurly.com/account/billing_info/edit?ht=mock-login-token',
|
2019-08-20 08:44:59 -04:00
|
|
|
accountManagementLink:
|
|
|
|
'https://test.recurly.com/account/mock-login-token',
|
2019-05-29 05:21:06 -04:00
|
|
|
currency: 'GBP',
|
|
|
|
nextPaymentDueAt: '5th May 2018',
|
|
|
|
price: '£6.00',
|
|
|
|
state: 'active',
|
|
|
|
tax: 100,
|
|
|
|
taxRate: 0.2,
|
|
|
|
trial_ends_at: new Date(2018, 6, 7),
|
2020-02-20 11:08:30 -05:00
|
|
|
trialEndsAtFormatted: '7th July 2018',
|
|
|
|
account: {
|
2021-03-04 09:22:43 -05:00
|
|
|
account_code: this.user._id.toString(),
|
2020-02-20 11:08:30 -05:00
|
|
|
email: 'mock@email.com',
|
2021-04-27 03:52:58 -04:00
|
|
|
hosted_login_token: 'mock-login-token',
|
2021-02-22 11:36:18 -05:00
|
|
|
},
|
|
|
|
additionalLicenses: 0,
|
2021-04-27 03:52:58 -04:00
|
|
|
totalLicenses: 0,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return no memberGroupSubscriptions', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.memberGroupSubscriptions).to.deep.equal([])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should include redeemed coupons', function (done) {
|
2020-02-27 07:46:21 -05:00
|
|
|
MockRecurlyApi.redemptions[this.user._id] = [
|
2019-05-29 05:21:06 -04:00
|
|
|
{ state: 'active', coupon_code: 'test-coupon-1' },
|
|
|
|
{ state: 'inactive', coupon_code: 'test-coupon-2' },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ state: 'active', coupon_code: 'test-coupon-3' },
|
2019-05-29 05:21:06 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
// rebuild the view model with the redemptions
|
2021-03-04 09:22:43 -05:00
|
|
|
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user,
|
2019-08-07 10:04:04 -04:00
|
|
|
(error, data) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(
|
|
|
|
data.personalSubscription.recurly.activeCoupons
|
|
|
|
).to.deep.equal([
|
|
|
|
{
|
|
|
|
coupon_code: 'test-coupon-1',
|
|
|
|
name: '',
|
2021-04-27 03:52:58 -04:00
|
|
|
description: 'Test Coupon 1',
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
coupon_code: 'test-coupon-3',
|
|
|
|
name: 'TestCoupon3',
|
2021-04-27 03:52:58 -04:00
|
|
|
description: '',
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
])
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-02-20 11:08:30 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return Recurly account email', function () {
|
2020-02-20 11:08:30 -05:00
|
|
|
expect(this.data.personalSubscription.recurly.account.email).to.equal(
|
|
|
|
'mock@email.com'
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user has a subscription without recurly', function () {
|
|
|
|
beforeEach(function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
Subscription.create(
|
|
|
|
{
|
|
|
|
admin_id: this.user._id,
|
|
|
|
manager_ids: [this.user._id],
|
2021-04-27 03:52:58 -04:00
|
|
|
planCode: 'collaborator',
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
error => {
|
2021-03-04 09:22:43 -05:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return done(error)
|
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user,
|
|
|
|
(error, data) => {
|
|
|
|
this.data = data
|
2021-03-04 09:22:43 -05:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return done(error)
|
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
after(function (done) {
|
2020-11-03 04:19:05 -05:00
|
|
|
Subscription.deleteOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
admin_id: this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return a personalSubscription with no recurly data', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const subscription = this.data.personalSubscription
|
|
|
|
expect(subscription).to.exist
|
|
|
|
expect(subscription.planCode).to.equal('collaborator')
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(subscription.recurly).to.not.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return no memberGroupSubscriptions', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.memberGroupSubscriptions).to.deep.equal([])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user is a member of a group subscription', function () {
|
|
|
|
beforeEach(async function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
const userHelperOwner1 = await UserHelper.createUser()
|
|
|
|
const userHelperOwner2 = await UserHelper.createUser()
|
|
|
|
this.owner1 = userHelperOwner1.user
|
|
|
|
this.owner2 = userHelperOwner2.user
|
|
|
|
|
|
|
|
await Subscription.create({
|
|
|
|
admin_id: this.owner1._id,
|
|
|
|
manager_ids: [this.owner1._id],
|
|
|
|
planCode: 'collaborator',
|
|
|
|
groupPlan: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
member_ids: [this.user._id],
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
await Subscription.create({
|
|
|
|
admin_id: this.owner2._id,
|
|
|
|
manager_ids: [this.owner2._id],
|
|
|
|
planCode: 'collaborator',
|
|
|
|
groupPlan: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
member_ids: [this.user._id],
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
this.data = await buildUsersSubscriptionViewModelPromise(this.user._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
after(function (done) {
|
2020-11-03 04:19:05 -05:00
|
|
|
Subscription.deleteOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
admin_id: this.owner1._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
error => {
|
2021-03-04 09:22:43 -05:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return done(error)
|
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
Subscription.deleteOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
admin_id: this.owner2._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
done
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return no personalSubscription', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.personalSubscription).to.equal(null)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return the two memberGroupSubscriptions', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.data.memberGroupSubscriptions.length).to.equal(2)
|
|
|
|
expect(
|
|
|
|
// Mongoose populates the admin_id with the user
|
|
|
|
this.data.memberGroupSubscriptions[0].admin_id._id.toString()
|
2021-03-04 09:22:43 -05:00
|
|
|
).to.equal(this.owner1._id.toString())
|
|
|
|
expect(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.data.memberGroupSubscriptions[1].admin_id._id.toString()
|
2021-03-04 09:22:43 -05:00
|
|
|
).to.equal(this.owner2._id.toString())
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user is a manager of a group subscription', function () {
|
|
|
|
beforeEach(async function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
const userHelperOwner1 = await UserHelper.createUser()
|
|
|
|
const userHelperOwner2 = await UserHelper.createUser()
|
|
|
|
this.owner1 = userHelperOwner1.user
|
|
|
|
this.owner2 = userHelperOwner2.user
|
|
|
|
|
|
|
|
await Subscription.create({
|
|
|
|
admin_id: this.owner1._id,
|
|
|
|
manager_ids: [this.owner1._id, this.user._id],
|
|
|
|
planCode: 'collaborator',
|
2021-04-27 03:52:58 -04:00
|
|
|
groupPlan: true,
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
this.data = await buildUsersSubscriptionViewModelPromise(this.user._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
after(function (done) {
|
2020-11-03 04:19:05 -05:00
|
|
|
Subscription.deleteOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
admin_id: this.owner1._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return no personalSubscription', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.personalSubscription).to.equal(null)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return the managedGroupSubscriptions', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.data.managedGroupSubscriptions.length).to.equal(1)
|
|
|
|
const subscription = this.data.managedGroupSubscriptions[0]
|
|
|
|
expect(
|
|
|
|
// Mongoose populates the admin_id with the user
|
|
|
|
subscription.admin_id._id.toString()
|
2021-03-04 09:22:43 -05:00
|
|
|
).to.equal(this.owner1._id.toString())
|
|
|
|
expect(subscription.groupPlan).to.equal(true)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user is a manager of an institution', function () {
|
|
|
|
beforeEach(function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.v1Id = MockV1Api.nextV1Id()
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
cb => {
|
2021-03-04 09:22:43 -05:00
|
|
|
Institution.create(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
v1Id: this.v1Id,
|
2021-04-27 03:52:58 -04:00
|
|
|
managerIds: [this.user._id],
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
cb
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
],
|
|
|
|
error => {
|
2021-03-04 09:22:43 -05:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return done(error)
|
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user,
|
|
|
|
(error, data) => {
|
|
|
|
this.data = data
|
2021-03-04 09:22:43 -05:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return done(error)
|
|
|
|
}
|
2021-03-04 09:22:43 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
after(function (done) {
|
2020-11-03 04:19:05 -05:00
|
|
|
Institution.deleteOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
v1Id: this.v1Id,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return the managedInstitutions', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.data.managedInstitutions.length).to.equal(1)
|
|
|
|
const institution = this.data.managedInstitutions[0]
|
|
|
|
expect(institution.v1Id).to.equal(this.v1Id)
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(institution.name).to.equal(`Institution ${this.v1Id}`)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user is a member of an affiliation', function () {
|
|
|
|
beforeEach(async function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const v1Id = MockV1Api.nextV1Id()
|
|
|
|
MockV1Api.setUser(v1Id, {
|
|
|
|
subscription: {},
|
2021-04-27 03:52:58 -04:00
|
|
|
subscription_status: {},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2021-03-04 09:22:43 -05:00
|
|
|
await UserHelper.updateUser(this.user._id, {
|
2021-04-27 03:52:58 -04:00
|
|
|
$set: { overleaf: { id: v1Id } },
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
const harvardDomain = 'harvard.example.edu'
|
|
|
|
const mitDomain = 'mit.example.edu'
|
|
|
|
const stanfordDomain = 'stanford.example.edu'
|
|
|
|
const harvardId = MockV1Api.createInstitution({
|
|
|
|
name: 'Harvard',
|
2021-04-27 03:52:58 -04:00
|
|
|
hostname: harvardDomain,
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
const mitId = MockV1Api.createInstitution({
|
|
|
|
name: 'MIT',
|
2021-04-27 03:52:58 -04:00
|
|
|
hostname: mitDomain,
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
const stanfordId = MockV1Api.createInstitution({
|
|
|
|
name: 'Stanford',
|
2021-04-27 03:52:58 -04:00
|
|
|
hostname: stanfordDomain,
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
MockV1Api.updateInstitutionDomain(harvardId, harvardDomain, {
|
2021-04-27 03:52:58 -04:00
|
|
|
confirmed: true,
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
MockV1Api.updateInstitutionDomain(mitId, mitDomain, {
|
2021-04-27 03:52:58 -04:00
|
|
|
confirmed: false,
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
MockV1Api.updateInstitutionDomain(stanfordId, stanfordDomain, {
|
2021-04-27 03:52:58 -04:00
|
|
|
confirmed: true,
|
2021-03-04 09:22:43 -05:00
|
|
|
})
|
|
|
|
this.harvardEmail = `unconfirmed-affiliation-email@${harvardDomain}`
|
|
|
|
this.stanfordEmail = `confirmed-affiliation-email@${stanfordDomain}`
|
|
|
|
const mitEmail = `confirmed-affiliation-email@${mitDomain}`
|
|
|
|
userHelper = await UserHelper.loginUser(
|
|
|
|
userHelper.getDefaultEmailPassword()
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2021-03-04 09:22:43 -05:00
|
|
|
await userHelper.addEmail(this.harvardEmail)
|
|
|
|
await userHelper.addEmailAndConfirm(this.user._id, this.stanfordEmail)
|
|
|
|
await userHelper.addEmailAndConfirm(this.user._id, mitEmail)
|
|
|
|
this.data = await buildUsersSubscriptionViewModelPromise(this.user._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return only the affilations with confirmed institutions, and confirmed emails', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.confirmedMemberAffiliations.length).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.data.confirmedMemberAffiliations[0].institution.name
|
|
|
|
).to.equal('Stanford')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user has a v1 subscription', function () {
|
|
|
|
beforeEach(async function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
let v1Id
|
|
|
|
MockV1Api.setUser((v1Id = MockV1Api.nextV1Id()), {
|
|
|
|
subscription: (this.subscription = {
|
|
|
|
trial: false,
|
|
|
|
has_plan: true,
|
|
|
|
teams: [
|
|
|
|
{
|
|
|
|
id: 56,
|
2021-04-27 03:52:58 -04:00
|
|
|
name: 'Test team',
|
|
|
|
},
|
|
|
|
],
|
2019-05-29 05:21:06 -04:00
|
|
|
}),
|
|
|
|
subscription_status: (this.subscription_status = {
|
|
|
|
product: { mock: 'product' },
|
2021-04-27 03:52:58 -04:00
|
|
|
team: null,
|
|
|
|
}),
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2021-03-04 09:22:43 -05:00
|
|
|
await UserHelper.updateUser(this.user._id, {
|
|
|
|
$set: {
|
|
|
|
overleaf: {
|
2021-04-27 03:52:58 -04:00
|
|
|
id: v1Id,
|
|
|
|
},
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2021-03-04 09:22:43 -05:00
|
|
|
this.data = await buildUsersSubscriptionViewModelPromise(this.user._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return no personalSubscription', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.personalSubscription).to.equal(null)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return no memberGroupSubscriptions', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.memberGroupSubscriptions).to.deep.equal([])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return a v1SubscriptionStatus', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.data.v1SubscriptionStatus).to.deep.equal(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription_status
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('canceling', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
let userHelper, v1Id
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
v1Id = MockV1Api.nextV1Id()
|
|
|
|
userHelper = await UserHelper.createUser({ overleaf: { id: v1Id } })
|
|
|
|
this.user = userHelper.user
|
|
|
|
MockV1Api.setUser(v1Id, (this.v1_user = {}))
|
|
|
|
|
|
|
|
userHelper = await UserHelper.loginUser(
|
|
|
|
userHelper.getDefaultEmailPassword()
|
|
|
|
)
|
|
|
|
this.response = await userHelper.request.post(
|
|
|
|
'/user/subscription/v1/cancel',
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
simple: false,
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should tell v1 to cancel the subscription', function () {
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.v1_user.canceled).to.equal(true)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should redirect to the subscription dashboard', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.response.statusCode).to.equal(302)
|
2021-03-04 09:22:43 -05:00
|
|
|
expect(this.response.headers.location).to.equal('/user/subscription')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|