From d947bdd5203e939cb729be4a7509c39a2a2a21f0 Mon Sep 17 00:00:00 2001 From: Matt Lott Date: Wed, 9 Sep 2015 09:53:46 -0700 Subject: [PATCH] Reduce server Stripe subscription tests during Travis CI --- .../functional/subscription.spec.coffee | 1185 +++++++++-------- 1 file changed, 593 insertions(+), 592 deletions(-) diff --git a/test/server/functional/subscription.spec.coffee b/test/server/functional/subscription.spec.coffee index d263526cf..f95aeea3f 100644 --- a/test/server/functional/subscription.spec.coffee +++ b/test/server/functional/subscription.spec.coffee @@ -538,44 +538,6 @@ describe 'Subscriptions', -> expect(subscription?.cancel_at_period_end).toEqual(true) done() - it 'Admin subscribes self with valid prepaid', (done) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - expect(err).toBeNull() - expect(user1.isAdmin()).toEqual(true) - createPrepaid 'subscription', 1, (err, res, prepaid) -> - expect(err).toBeNull() - subscribeUser user1, null, prepaid.code, -> - Prepaid.findById prepaid._id, (err, prepaid) -> - expect(err).toBeNull() - expect(prepaid.get('maxRedeemers')).toEqual(1) - expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) - expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) - done() - - it 'Admin subscribes self with valid prepaid twice', (done) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - expect(err).toBeNull() - expect(user1.isAdmin()).toEqual(true) - createPrepaid 'subscription', 2, (err, res, prepaid) -> - expect(err).toBeNull() - Prepaid.findById prepaid._id, (err, prepaid) -> - expect(err).toBeNull() - prepaid.set 'redeemers', [{userID: user1.get('_id'), date: new Date()}] - prepaid.save (err) -> - expect(err).toBeNull() - requestBody = user1.toObject() - requestBody.stripe = - planID: 'basic' - requestBody.stripe.prepaidCode = prepaid.get('code') - request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(403) - done() - it 'User subscribes, deletes themselves, subscription ends', (done) -> stripe.tokens.create { card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } @@ -601,144 +563,6 @@ describe 'Subscriptions', -> expect(res.statusCode).toEqual(200) done() - it 'Admin subscribes self with invalid prepaid', (done) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - expect(err).toBeNull() - expect(user1.isAdmin()).toEqual(true) - requestBody = user1.toObject() - requestBody.stripe = - planID: 'basic' - requestBody.stripe.prepaidCode = 'MattMatt' - request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(404) - done() - - it 'User2 subscribes with used prepaid', (done) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - expect(err).toBeNull() - expect(user1.isAdmin()).toEqual(true) - createPrepaid 'subscription', 1, (err, res, prepaid) -> - expect(err).toBeNull() - subscribeUser user1, null, prepaid.code, -> - loginNewUser (user2) -> - requestBody = user2.toObject() - requestBody.stripe = - planID: 'basic' - requestBody.stripe.prepaidCode = prepaid.code - request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(403) - Prepaid.findById prepaid._id, (err, prepaid) -> - expect(err).toBeNull() - expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) - expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) - done() - - it 'User2 subscribes with same active prepaid', (done) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - expect(err).toBeNull() - expect(user1.isAdmin()).toEqual(true) - createPrepaid 'subscription', 2, (err, res, prepaid) -> - expect(err).toBeNull() - subscribeUser user1, null, prepaid.code, -> - loginNewUser (user2) -> - subscribeUser user2, null, prepaid.code, -> - Prepaid.findById prepaid._id, (err, prepaid) -> - expect(err).toBeNull() - expect(prepaid.get('redeemers').length).toEqual(2) - done() - - it 'Subscribe normally, subscribe with valid prepaid', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - expect(err).toBeNull() - expect(user1.isAdmin()).toEqual(true) - subscribeUser user1, token, null, -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - createPrepaid 'subscription', 1, (err, res, prepaid) -> - expect(err).toBeNull() - subscribeUser user1, null, prepaid.code, -> - Prepaid.findById prepaid._id, (err, prepaid) -> - expect(err).toBeNull() - expect(prepaid.get('maxRedeemers')).toEqual(1) - expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) - expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - customerID = user1.get('stripe').customerID - subscriptionID = user1.get('stripe').subscriptionID - stripe.customers.retrieveSubscription customerID, subscriptionID, (err, subscription) -> - expect(err).toBeNull() - expect(subscription).not.toBeNull() - expect(subscription.discount?.coupon?.id).toEqual('free') - done() - - it 'Subscribe with coupon, subscribe with valid prepaid', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - requestBody = user1.toObject() - requestBody.stripe = - planID: 'basic' - token: token.id - couponID: '20pct' - request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, updatedUser) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(200) - createPrepaid 'subscription', 1, (err, res, prepaid) -> - subscribeUser user1, null, prepaid.code, -> - Prepaid.findById prepaid._id, (err, prepaid) -> - expect(err).toBeNull() - expect(prepaid.get('maxRedeemers')).toEqual(1) - expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) - expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - customerID = user1.get('stripe').customerID - subscriptionID = user1.get('stripe').subscriptionID - stripe.customers.retrieveSubscription customerID, subscriptionID, (err, subscription) -> - expect(err).toBeNull() - expect(subscription).not.toBeNull() - expect(subscription.discount?.coupon?.id).toEqual('free') - done() - - it 'Subscribe with prepaid, then cancel', (done) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - expect(err).toBeNull() - expect(user1.isAdmin()).toEqual(true) - createPrepaid 'subscription', 1, (err, res, prepaid) -> - expect(err).toBeNull() - subscribeUser user1, null, prepaid.code, -> - Prepaid.findById prepaid._id, (err, prepaid) -> - expect(err).toBeNull() - expect(prepaid.get('maxRedeemers')).toEqual(1) - expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) - expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - unsubscribeUser user1, -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - expect(user1.get('stripe').prepaidCode).toEqual(prepaid.get('code')) - done() - it 'Subscribe with prepaid, then delete', (done) -> loginNewUser (user1) -> user1.set('permissions', ['admin']) @@ -776,6 +600,183 @@ describe 'Subscriptions', -> expect(stripeInfo.subscriptionID).toBeUndefined() done() + it 'Subscribe normally, subscribe with valid prepaid', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + expect(err).toBeNull() + expect(user1.isAdmin()).toEqual(true) + subscribeUser user1, token, null, -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + createPrepaid 'subscription', 1, (err, res, prepaid) -> + expect(err).toBeNull() + subscribeUser user1, null, prepaid.code, -> + Prepaid.findById prepaid._id, (err, prepaid) -> + expect(err).toBeNull() + expect(prepaid.get('maxRedeemers')).toEqual(1) + expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) + expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + customerID = user1.get('stripe').customerID + subscriptionID = user1.get('stripe').subscriptionID + stripe.customers.retrieveSubscription customerID, subscriptionID, (err, subscription) -> + expect(err).toBeNull() + expect(subscription).not.toBeNull() + expect(subscription.discount?.coupon?.id).toEqual('free') + done() + + unless TRAVIS + it 'Subscribe with coupon, subscribe with valid prepaid', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + requestBody = user1.toObject() + requestBody.stripe = + planID: 'basic' + token: token.id + couponID: '20pct' + request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, updatedUser) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(200) + createPrepaid 'subscription', 1, (err, res, prepaid) -> + subscribeUser user1, null, prepaid.code, -> + Prepaid.findById prepaid._id, (err, prepaid) -> + expect(err).toBeNull() + expect(prepaid.get('maxRedeemers')).toEqual(1) + expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) + expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + customerID = user1.get('stripe').customerID + subscriptionID = user1.get('stripe').subscriptionID + stripe.customers.retrieveSubscription customerID, subscriptionID, (err, subscription) -> + expect(err).toBeNull() + expect(subscription).not.toBeNull() + expect(subscription.discount?.coupon?.id).toEqual('free') + done() + + it 'Subscribe with prepaid, then cancel', (done) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + expect(err).toBeNull() + expect(user1.isAdmin()).toEqual(true) + createPrepaid 'subscription', 1, (err, res, prepaid) -> + expect(err).toBeNull() + subscribeUser user1, null, prepaid.code, -> + Prepaid.findById prepaid._id, (err, prepaid) -> + expect(err).toBeNull() + expect(prepaid.get('maxRedeemers')).toEqual(1) + expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) + expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + unsubscribeUser user1, -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + expect(user1.get('stripe').prepaidCode).toEqual(prepaid.get('code')) + done() + + it 'User2 subscribes with used prepaid', (done) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + expect(err).toBeNull() + expect(user1.isAdmin()).toEqual(true) + createPrepaid 'subscription', 1, (err, res, prepaid) -> + expect(err).toBeNull() + subscribeUser user1, null, prepaid.code, -> + loginNewUser (user2) -> + requestBody = user2.toObject() + requestBody.stripe = + planID: 'basic' + requestBody.stripe.prepaidCode = prepaid.code + request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(403) + Prepaid.findById prepaid._id, (err, prepaid) -> + expect(err).toBeNull() + expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) + expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) + done() + + it 'User2 subscribes with same active prepaid', (done) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + expect(err).toBeNull() + expect(user1.isAdmin()).toEqual(true) + createPrepaid 'subscription', 2, (err, res, prepaid) -> + expect(err).toBeNull() + subscribeUser user1, null, prepaid.code, -> + loginNewUser (user2) -> + subscribeUser user2, null, prepaid.code, -> + Prepaid.findById prepaid._id, (err, prepaid) -> + expect(err).toBeNull() + expect(prepaid.get('redeemers').length).toEqual(2) + done() + + it 'Admin subscribes self with valid prepaid', (done) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + expect(err).toBeNull() + expect(user1.isAdmin()).toEqual(true) + createPrepaid 'subscription', 1, (err, res, prepaid) -> + expect(err).toBeNull() + subscribeUser user1, null, prepaid.code, -> + Prepaid.findById prepaid._id, (err, prepaid) -> + expect(err).toBeNull() + expect(prepaid.get('maxRedeemers')).toEqual(1) + expect(prepaid.get('redeemers')[0].userID).toEqual(user1.get('_id')) + expect(prepaid.get('redeemers')[0].date).toBeLessThan(new Date()) + done() + + it 'Admin subscribes self with valid prepaid twice', (done) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + expect(err).toBeNull() + expect(user1.isAdmin()).toEqual(true) + createPrepaid 'subscription', 2, (err, res, prepaid) -> + expect(err).toBeNull() + Prepaid.findById prepaid._id, (err, prepaid) -> + expect(err).toBeNull() + prepaid.set 'redeemers', [{userID: user1.get('_id'), date: new Date()}] + prepaid.save (err) -> + expect(err).toBeNull() + requestBody = user1.toObject() + requestBody.stripe = + planID: 'basic' + requestBody.stripe.prepaidCode = prepaid.get('code') + request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(403) + done() + + it 'Admin subscribes self with invalid prepaid', (done) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + expect(err).toBeNull() + expect(user1.isAdmin()).toEqual(true) + requestBody = user1.toObject() + requestBody.stripe = + planID: 'basic' + requestBody.stripe.prepaidCode = 'MattMatt' + request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(404) + done() + describe 'Sponsored', -> it 'Unsubscribed user1 subscribes user2', (done) -> stripe.tokens.create { @@ -786,408 +787,234 @@ describe 'Subscriptions', -> subscribeRecipients user1, [user2], token, (updatedUser) -> verifySponsorship user1.id, user2.id, done - it 'Recipient user delete unsubscribes', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2], token, (updatedUser) -> - customerID = updatedUser.stripe.customerID - subscriptionID = updatedUser.stripe.recipients[0].subscriptionID - loginUser user2, (user2) -> - request.del {uri: "#{userURL}/#{user2.id}"}, (err, res) -> + unless TRAVIS + it 'Recipient user delete unsubscribes', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2], token, (updatedUser) -> + customerID = updatedUser.stripe.customerID + subscriptionID = updatedUser.stripe.recipients[0].subscriptionID + loginUser user2, (user2) -> + request.del {uri: "#{userURL}/#{user2.id}"}, (err, res) -> + expect(err).toBeNull() + stripe.customers.retrieveSubscription customerID, subscriptionID, (err, subscription) -> + expect(err).not.toBeNull() + expect(subscription).toBeNull() + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + expect(_.isEmpty(user1.get('stripe').recipients)) + stripe.customers.retrieveSubscription customerID, user1.get('stripe').sponsorSubscriptionID, (err, subscription) -> + expect(err).toBeNull() + expect(subscription.quantity).toEqual(0) + done() + + it 'Subscribed user1 subscribes user2, one token', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeUser user1, token, null, (updatedUser) -> + User.findById user1.id, (err, user1) -> expect(err).toBeNull() - stripe.customers.retrieveSubscription customerID, subscriptionID, (err, subscription) -> - expect(err).not.toBeNull() - expect(subscription).toBeNull() + subscribeRecipients user1, [user2], null, (updatedUser) -> User.findById user1.id, (err, user1) -> expect(err).toBeNull() - expect(_.isEmpty(user1.get('stripe').recipients)) - stripe.customers.retrieveSubscription customerID, user1.get('stripe').sponsorSubscriptionID, (err, subscription) -> - expect(err).toBeNull() - expect(subscription.quantity).toEqual(0) - done() - - it 'Subscribed user1 subscribes user2, one token', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeUser user1, token, null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - subscribeRecipients user1, [user2], null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - expect(user1.get('stripe').subscriptionID).toBeDefined() - expect(user1.isPremium()).toEqual(true) - verifySponsorship user1.id, user2.id, done - - it 'Clean up sponsorships upon sub cancel after setup sponsor sub fails', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeUser user1, token, null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - subscribeRecipients user1, [user2], null, (updatedUser) -> - - # Delete user1 sponsorSubscriptionID to simulate failed sponsor sub - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - stripeInfo = _.cloneDeep(user1.get('stripe') ? {}) - delete stripeInfo.sponsorSubscriptionID - user1.set 'stripe', stripeInfo - user1.save (err, user1) -> - expect(err).toBeNull() - - User.findById user1.id, (err, user1) -> - unsubscribeRecipient user1, user2, -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - expect(user1.get('stripe').subscriptionID).toBeDefined() - expect(_.isEmpty(user1.get('stripe').recipients)).toEqual(true) - expect(user1.isPremium()).toEqual(true) - User.findById user2.id, (err, user2) -> - verifyNotSponsoring user1.id, user2.id, -> - verifyNotRecipient user2.id, done - - - it 'Unsubscribed user1 unsubscribes user2 and their sub ends', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2], token, (updatedUser) -> - User.findById user1.id, (err, user1) -> - unsubscribeRecipient user1, user2, -> - verifyNotSponsoring user1.id, user2.id, -> - verifyNotRecipient user2.id, done - - it 'Unsubscribed user1 immediately resubscribes user2, one token', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2], token, (updatedUser) -> - User.findById user1.id, (err, user1) -> - unsubscribeRecipient user1, user2, -> - subscribeRecipients user1, [user2], null, (updatedUser) -> - verifySponsorship user1.id, user2.id, done - - it 'Sponsored user2 subscribes their sponsor user1', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2], token, (updatedUser) -> - loginUser user2, (user2) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - subscribeRecipients user2, [user1], token, (updatedUser) -> - verifySponsorship user1.id, user2.id, -> - verifySponsorship user2.id, user1.id, done - - it 'Unsubscribed user1 subscribes user1', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - loginNewUser (user1) -> - - requestBody = user1.toObject() - requestBody.stripe = - subscribeEmails: [user1.get('email')] - token: token.id - request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(200) - - User.findById user1.id, (err, user) -> - expect(err).toBeNull() - stripeInfo = user.get('stripe') - expect(stripeInfo.customerID).toBeDefined() - expect(stripeInfo.planID).toBeUndefined() - expect(stripeInfo.subscriptionID).toBeUndefined() - expect(stripeInfo.recipients.length).toEqual(0) - done() - - it 'Subscribed user1 unsubscribes user2', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeUser user1, token, null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - subscribeRecipients user1, [user2], null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - unsubscribeRecipient user1, user2, -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - expect(user1.get('stripe').subscriptionID).toBeDefined() - expect(user1.isPremium()).toEqual(true) - User.findById user2.id, (err, user2) -> - verifyNotSponsoring user1.id, user2.id, -> - verifyNotRecipient user2.id, done - - it 'Subscribed user1 subscribes user2, unsubscribes themselves', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeUser user1, token, null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - subscribeRecipients user1, [user2], null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - unsubscribeUser user1, -> + expect(user1.get('stripe').subscriptionID).toBeDefined() + expect(user1.isPremium()).toEqual(true) verifySponsorship user1.id, user2.id, done - it 'Sponsored user2 tries to subscribe', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2], token, (updatedUser) -> - loginUser user2, (user2) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - requestBody = user2.toObject() - requestBody.stripe = - token: token.id - planID: 'basic' - request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(403) - done() - - it 'Sponsored user2 tries to subscribe with valid prepaid', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2], token, (updatedUser) -> - loginUser user2, (user2) -> - user2.set('permissions', ['admin']) - user2.save (err, user1) -> + it 'Clean up sponsorships upon sub cancel after setup sponsor sub fails', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeUser user1, token, null, (updatedUser) -> + User.findById user1.id, (err, user1) -> expect(err).toBeNull() - expect(user2.isAdmin()).toEqual(true) - createPrepaid 'subscription', 1, (err, res, prepaid) -> - expect(err).toBeNull() + subscribeRecipients user1, [user2], null, (updatedUser) -> + + # Delete user1 sponsorSubscriptionID to simulate failed sponsor sub + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + stripeInfo = _.cloneDeep(user1.get('stripe') ? {}) + delete stripeInfo.sponsorSubscriptionID + user1.set 'stripe', stripeInfo + user1.save (err, user1) -> + expect(err).toBeNull() + + User.findById user1.id, (err, user1) -> + unsubscribeRecipient user1, user2, -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + expect(user1.get('stripe').subscriptionID).toBeDefined() + expect(_.isEmpty(user1.get('stripe').recipients)).toEqual(true) + expect(user1.isPremium()).toEqual(true) + User.findById user2.id, (err, user2) -> + verifyNotSponsoring user1.id, user2.id, -> + verifyNotRecipient user2.id, done + + + it 'Unsubscribed user1 unsubscribes user2 and their sub ends', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2], token, (updatedUser) -> + User.findById user1.id, (err, user1) -> + unsubscribeRecipient user1, user2, -> + verifyNotSponsoring user1.id, user2.id, -> + verifyNotRecipient user2.id, done + + it 'Unsubscribed user1 immediately resubscribes user2, one token', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2], token, (updatedUser) -> + User.findById user1.id, (err, user1) -> + unsubscribeRecipient user1, user2, -> + subscribeRecipients user1, [user2], null, (updatedUser) -> + verifySponsorship user1.id, user2.id, done + + it 'Sponsored user2 subscribes their sponsor user1', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2], token, (updatedUser) -> + loginUser user2, (user2) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + subscribeRecipients user2, [user1], token, (updatedUser) -> + verifySponsorship user1.id, user2.id, -> + verifySponsorship user2.id, user1.id, done + + it 'Unsubscribed user1 subscribes user1', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + loginNewUser (user1) -> + + requestBody = user1.toObject() + requestBody.stripe = + subscribeEmails: [user1.get('email')] + token: token.id + request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(200) + + User.findById user1.id, (err, user) -> + expect(err).toBeNull() + stripeInfo = user.get('stripe') + expect(stripeInfo.customerID).toBeDefined() + expect(stripeInfo.planID).toBeUndefined() + expect(stripeInfo.subscriptionID).toBeUndefined() + expect(stripeInfo.recipients.length).toEqual(0) + done() + + it 'Subscribed user1 unsubscribes user2', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeUser user1, token, null, (updatedUser) -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + subscribeRecipients user1, [user2], null, (updatedUser) -> + User.findById user1.id, (err, user1) -> + unsubscribeRecipient user1, user2, -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + expect(user1.get('stripe').subscriptionID).toBeDefined() + expect(user1.isPremium()).toEqual(true) + User.findById user2.id, (err, user2) -> + verifyNotSponsoring user1.id, user2.id, -> + verifyNotRecipient user2.id, done + + it 'Subscribed user1 subscribes user2, unsubscribes themselves', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeUser user1, token, null, (updatedUser) -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + subscribeRecipients user1, [user2], null, (updatedUser) -> + User.findById user1.id, (err, user1) -> + unsubscribeUser user1, -> + verifySponsorship user1.id, user2.id, done + + it 'Sponsored user2 tries to subscribe', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2], token, (updatedUser) -> + loginUser user2, (user2) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> requestBody = user2.toObject() requestBody.stripe = + token: token.id planID: 'basic' - prepaidCode: prepaid.code request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> expect(err).toBeNull() expect(res.statusCode).toBe(403) done() - it 'Sponsored user2 tries to unsubscribe', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2], token, (updatedUser) -> - loginUser user2, (user2) -> - requestBody = user2.toObject() - requestBody.stripe = - recipient: user2.id - request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(200) - verifySponsorship user1.id, user2.id, done - - it 'Cancel sponsor subscription with 2 recipient subscriptions, then subscribe 1 old and 1 new', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user3) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2, user3], token, (updatedUser) -> - customerID = updatedUser.stripe.customerID - subscriptionID = updatedUser.stripe.sponsorSubscriptionID - - # Find Stripe sponsor subscription - stripe.customers.retrieveSubscription customerID, subscriptionID, (err, subscription) -> - expect(err).toBeNull() - expect(subscription).not.toBeNull() - - # Cancel Stripe sponsor subscription - stripe.customers.cancelSubscription customerID, subscriptionID, (err) -> - expect(err).toBeNull() - - # Simulate customer.subscription.deleted webhook event for sponsor subscription - event = _.cloneDeep(customerSubscriptionDeletedSampleEvent) - event.data.object = subscription - request.post {uri: webhookURL, json: event}, (err, res, body) -> - expect(err).toBeNull() - - # Should have 2 cancelled recipient subs with cancel_at_period_end = true - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - stripeInfo = user1.get('stripe') - expect(stripeInfo.sponsorSubscriptionID).toBeUndefined() - expect(stripeInfo.recipients).toBeUndefined() - stripe.customers.listSubscriptions stripeInfo.customerID, (err, subscriptions) -> - expect(err).toBeNull() - expect(subscriptions.data.length).toEqual(2) - for sub in subscriptions.data - expect(sub.plan.id).toEqual('basic') - expect(sub.cancel_at_period_end).toEqual(true) - - # Subscribe user3 back - User.findById user1.id, (err, user1) -> - subscribeRecipients user1, [user3], null, (updatedUser) -> - verifySponsorship user1.id, user3.id, -> - - # Subscribe new user4 - createNewUser (user4) -> - loginUser user1, (user1) -> - User.findById user1.id, (err, user1) -> - subscribeRecipients user1, [user4], null, (updatedUser) -> - verifySponsorship user1.id, user4.id, done - - it 'Subscribing two users separately yields proration payment', (done) -> - # TODO: Use test plan with low duration + setTimeout to test delay between 2 subscribes - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user3) -> + it 'Sponsored user2 tries to subscribe with valid prepaid', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> createNewUser (user2) -> loginNewUser (user1) -> subscribeRecipients user1, [user2], token, (updatedUser) -> - User.findById user1.id, (err, user1) -> - subscribeRecipients user1, [user3], null, (updatedUser) -> - # TODO: What do we expect invoices to show here? - stripe.invoices.list {customer: updatedUser.stripe.customerID}, (err, invoices) -> + loginUser user2, (user2) -> + user2.set('permissions', ['admin']) + user2.save (err, user1) -> + expect(err).toBeNull() + expect(user2.isAdmin()).toEqual(true) + createPrepaid 'subscription', 1, (err, res, prepaid) -> expect(err).toBeNull() - - # Verify for proration invoice - foundProratedInvoice = false - for invoice in invoices.data - line = invoice.lines.data[0] - if line.type is 'invoiceitem' and line.proration - totalAmount = utils.getSponsoredSubsAmount(subPrice, 2, false) - expect(invoice.total).toBeLessThan(totalAmount) - expect(invoice.total).toEqual(totalAmount - subPrice) - Payment.findOne "stripe.invoiceID": invoice.id, (err, payment) -> - expect(err).toBeNull() - expect(payment.get('amount')).toEqual(invoice.total) - done() - foundProratedInvoice = true - break - unless foundProratedInvoice - expect(foundProratedInvoice).toEqual(true) + requestBody = user2.toObject() + requestBody.stripe = + planID: 'basic' + prepaidCode: prepaid.code + request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(403) done() - it 'Invalid subscribeEmails', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - loginNewUser (user1) -> - requestBody = user1.toObject() - requestBody.stripe = - subscribeEmails: ['invalid@user.com', 'notemailformat', '', null, undefined] - requestBody.stripe.token = token.id - request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> - expect(err).toBeNull() - expect(res.statusCode).toBe(200) - expect(body.stripe).toBeDefined() - User.findById user1.id, (err, sponsor) -> - expect(err).toBeNull() - expect(sponsor.get('stripe')).toBeDefined() - expect(sponsor.get('stripe').customerID).toBeDefined() - expect(sponsor.get('stripe').sponsorSubscriptionID).toBeDefined() - expect(sponsor.get('stripe').recipients?.length).toEqual(0) - done() - - it 'User1 subscribes user2 then themselves', (done) -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - createNewUser (user2) -> - loginNewUser (user1) -> - subscribeRecipients user1, [user2], token, (updatedUser) -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - verifySponsorship user1.id, user2.id, -> - - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - subscribeUser user1, token, null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() - expect(user1.get('stripe').subscriptionID).toBeDefined() - expect(user1.isPremium()).toEqual(true) - - stripe.customers.listSubscriptions user1.get('stripe').customerID, (err, subscriptions) -> - expect(err).toBeNull() - expect(subscriptions.data.length).toEqual(3) - for sub in subscriptions.data - if sub.plan.id is 'basic' - if sub.discount?.coupon?.id is 'free' - expect(sub.metadata?.id).toEqual(user2.id) - else - expect(sub.metadata?.id).toEqual(user1.id) - else - expect(sub.plan.id).toEqual('incremental') - expect(sub.metadata?.id).toEqual(user1.id) - done() - - it 'Subscribe with prepaid, then get sponsored', (done) -> - loginNewUser (user1) -> - user1.set('permissions', ['admin']) - user1.save (err, user1) -> - expect(err).toBeNull() - expect(user1.isAdmin()).toEqual(true) - createPrepaid 'subscription', 1, (err, res, prepaid) -> - expect(err).toBeNull() - subscribeUser user1, null, prepaid.code, -> - stripe.tokens.create { - card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } - }, (err, token) -> - loginNewUser (user2) -> + it 'Sponsored user2 tries to unsubscribe', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2], token, (updatedUser) -> + loginUser user2, (user2) -> requestBody = user2.toObject() requestBody.stripe = - token: token.id - subscribeEmails: [user1.get('emailLower')] + recipient: user2.id request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> expect(err).toBeNull() expect(res.statusCode).toBe(200) - User.findById user1.id, (err, user) -> - expect(err).toBeNull() - stripeInfo = user.get('stripe') - expect(stripeInfo.customerID).toBeDefined() - expect(stripeInfo.planID).toBeDefined() - expect(stripeInfo.subscriptionID).toBeDefined() - expect(stripeInfo.sponsorID).toBeUndefined() - done() + verifySponsorship user1.id, user2.id, done - - describe 'Bulk discounts', -> - # Bulk discount algorithm (includes personal sub): - # 1 100% - # 2-11 80% - # 12+ 60% - - it 'Unsubscribed user1 subscribes two users', (done) -> + it 'Cancel sponsor subscription with 2 recipient subscriptions, then subscribe 1 old and 1 new', (done) -> stripe.tokens.create { card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } }, (err, token) -> @@ -1195,58 +1022,232 @@ describe 'Subscriptions', -> createNewUser (user2) -> loginNewUser (user1) -> subscribeRecipients user1, [user2, user3], token, (updatedUser) -> + customerID = updatedUser.stripe.customerID + subscriptionID = updatedUser.stripe.sponsorSubscriptionID + + # Find Stripe sponsor subscription + stripe.customers.retrieveSubscription customerID, subscriptionID, (err, subscription) -> + expect(err).toBeNull() + expect(subscription).not.toBeNull() + + # Cancel Stripe sponsor subscription + stripe.customers.cancelSubscription customerID, subscriptionID, (err) -> + expect(err).toBeNull() + + # Simulate customer.subscription.deleted webhook event for sponsor subscription + event = _.cloneDeep(customerSubscriptionDeletedSampleEvent) + event.data.object = subscription + request.post {uri: webhookURL, json: event}, (err, res, body) -> + expect(err).toBeNull() + + # Should have 2 cancelled recipient subs with cancel_at_period_end = true + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + stripeInfo = user1.get('stripe') + expect(stripeInfo.sponsorSubscriptionID).toBeUndefined() + expect(stripeInfo.recipients).toBeUndefined() + stripe.customers.listSubscriptions stripeInfo.customerID, (err, subscriptions) -> + expect(err).toBeNull() + expect(subscriptions.data.length).toEqual(2) + for sub in subscriptions.data + expect(sub.plan.id).toEqual('basic') + expect(sub.cancel_at_period_end).toEqual(true) + + # Subscribe user3 back + User.findById user1.id, (err, user1) -> + subscribeRecipients user1, [user3], null, (updatedUser) -> + verifySponsorship user1.id, user3.id, -> + + # Subscribe new user4 + createNewUser (user4) -> + loginUser user1, (user1) -> + User.findById user1.id, (err, user1) -> + subscribeRecipients user1, [user4], null, (updatedUser) -> + verifySponsorship user1.id, user4.id, done + + it 'Subscribing two users separately yields proration payment', (done) -> + # TODO: Use test plan with low duration + setTimeout to test delay between 2 subscribes + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user3) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2], token, (updatedUser) -> + User.findById user1.id, (err, user1) -> + subscribeRecipients user1, [user3], null, (updatedUser) -> + # TODO: What do we expect invoices to show here? + stripe.invoices.list {customer: updatedUser.stripe.customerID}, (err, invoices) -> + expect(err).toBeNull() + + # Verify for proration invoice + foundProratedInvoice = false + for invoice in invoices.data + line = invoice.lines.data[0] + if line.type is 'invoiceitem' and line.proration + totalAmount = utils.getSponsoredSubsAmount(subPrice, 2, false) + expect(invoice.total).toBeLessThan(totalAmount) + expect(invoice.total).toEqual(totalAmount - subPrice) + Payment.findOne "stripe.invoiceID": invoice.id, (err, payment) -> + expect(err).toBeNull() + expect(payment.get('amount')).toEqual(invoice.total) + done() + foundProratedInvoice = true + break + unless foundProratedInvoice + expect(foundProratedInvoice).toEqual(true) + done() + + it 'Invalid subscribeEmails', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + loginNewUser (user1) -> + requestBody = user1.toObject() + requestBody.stripe = + subscribeEmails: ['invalid@user.com', 'notemailformat', '', null, undefined] + requestBody.stripe.token = token.id + request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(200) + expect(body.stripe).toBeDefined() + User.findById user1.id, (err, sponsor) -> + expect(err).toBeNull() + expect(sponsor.get('stripe')).toBeDefined() + expect(sponsor.get('stripe').customerID).toBeDefined() + expect(sponsor.get('stripe').sponsorSubscriptionID).toBeDefined() + expect(sponsor.get('stripe').recipients?.length).toEqual(0) + done() + + it 'User1 subscribes user2 then themselves', (done) -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2], token, (updatedUser) -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() verifySponsorship user1.id, user2.id, -> - verifySponsorship user1.id, user3.id, done - it 'Subscribed user1 subscribes 2 users, unsubscribes 2', (done) -> - recipientCount = 2 - recipientsToVerify = [0, 1] - recipients = new SubbedRecipients recipientCount, recipientsToVerify + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + subscribeUser user1, token, null, (updatedUser) -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + expect(user1.get('stripe').subscriptionID).toBeDefined() + expect(user1.isPremium()).toEqual(true) - # Create recipients - recipients.createRecipients -> - expect(recipients.length()).toEqual(recipientCount) + stripe.customers.listSubscriptions user1.get('stripe').customerID, (err, subscriptions) -> + expect(err).toBeNull() + expect(subscriptions.data.length).toEqual(3) + for sub in subscriptions.data + if sub.plan.id is 'basic' + if sub.discount?.coupon?.id is 'free' + expect(sub.metadata?.id).toEqual(user2.id) + else + expect(sub.metadata?.id).toEqual(user1.id) + else + expect(sub.plan.id).toEqual('incremental') + expect(sub.metadata?.id).toEqual(user1.id) + done() + it 'Subscribe with prepaid, then get sponsored', (done) -> + loginNewUser (user1) -> + user1.set('permissions', ['admin']) + user1.save (err, user1) -> + expect(err).toBeNull() + expect(user1.isAdmin()).toEqual(true) + createPrepaid 'subscription', 1, (err, res, prepaid) -> + expect(err).toBeNull() + subscribeUser user1, null, prepaid.code, -> + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> + loginNewUser (user2) -> + requestBody = user2.toObject() + requestBody.stripe = + token: token.id + subscribeEmails: [user1.get('emailLower')] + request.put {uri: userURL, json: requestBody, headers: headers }, (err, res, body) -> + expect(err).toBeNull() + expect(res.statusCode).toBe(200) + User.findById user1.id, (err, user) -> + expect(err).toBeNull() + stripeInfo = user.get('stripe') + expect(stripeInfo.customerID).toBeDefined() + expect(stripeInfo.planID).toBeDefined() + expect(stripeInfo.subscriptionID).toBeDefined() + expect(stripeInfo.sponsorID).toBeUndefined() + done() + + + describe 'Bulk discounts', -> + # Bulk discount algorithm (includes personal sub): + # 1 100% + # 2-11 80% + # 12+ 60% + + it 'Unsubscribed user1 subscribes two users', (done) -> stripe.tokens.create { card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } }, (err, token) -> + createNewUser (user3) -> + createNewUser (user2) -> + loginNewUser (user1) -> + subscribeRecipients user1, [user2, user3], token, (updatedUser) -> + verifySponsorship user1.id, user2.id, -> + verifySponsorship user1.id, user3.id, done - # Create sponsor user - loginNewUser (user1) -> - subscribeUser user1, token, null, (updatedUser) -> - User.findById user1.id, (err, user1) -> - expect(err).toBeNull() + it 'Subscribed user1 subscribes 2 users, unsubscribes 2', (done) -> + recipientCount = 2 + recipientsToVerify = [0, 1] + recipients = new SubbedRecipients recipientCount, recipientsToVerify - # Subscribe recipients - recipients.subRecipients user1, null, -> - User.findById user1.id, (err, user1) -> + # Create recipients + recipients.createRecipients -> + expect(recipients.length()).toEqual(recipientCount) - # Unsubscribe recipient0 - unsubscribeRecipient user1, recipients.get(0), -> - User.findById user1.id, (err, user1) -> - stripeInfo = user1.get('stripe') - expect(stripeInfo.recipients.length).toEqual(1) - verifyNotSponsoring user1.id, recipients.get(0).id, -> - verifyNotRecipient recipients.get(0).id, -> - stripe.customers.retrieveSubscription stripeInfo.customerID, stripeInfo.sponsorSubscriptionID, (err, subscription) -> - expect(err).toBeNull() - expect(subscription).not.toBeNull() - expect(subscription.quantity).toEqual(getSubscribedQuantity(1)) + stripe.tokens.create { + card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } + }, (err, token) -> - # Unsubscribe recipient1 - unsubscribeRecipient user1, recipients.get(1), -> - User.findById user1.id, (err, user1) -> - stripeInfo = user1.get('stripe') - expect(stripeInfo.recipients.length).toEqual(0) - verifyNotSponsoring user1.id, recipients.get(1).id, -> - verifyNotRecipient recipients.get(1).id, -> - stripe.customers.retrieveSubscription stripeInfo.customerID, stripeInfo.sponsorSubscriptionID, (err, subscription) -> - expect(err).toBeNull() - expect(subscription).not.toBeNull() - expect(subscription.quantity).toEqual(0) - done() + # Create sponsor user + loginNewUser (user1) -> + subscribeUser user1, token, null, (updatedUser) -> + User.findById user1.id, (err, user1) -> + expect(err).toBeNull() + + # Subscribe recipients + recipients.subRecipients user1, null, -> + User.findById user1.id, (err, user1) -> + + # Unsubscribe recipient0 + unsubscribeRecipient user1, recipients.get(0), -> + User.findById user1.id, (err, user1) -> + stripeInfo = user1.get('stripe') + expect(stripeInfo.recipients.length).toEqual(1) + verifyNotSponsoring user1.id, recipients.get(0).id, -> + verifyNotRecipient recipients.get(0).id, -> + stripe.customers.retrieveSubscription stripeInfo.customerID, stripeInfo.sponsorSubscriptionID, (err, subscription) -> + expect(err).toBeNull() + expect(subscription).not.toBeNull() + expect(subscription.quantity).toEqual(getSubscribedQuantity(1)) + + # Unsubscribe recipient1 + unsubscribeRecipient user1, recipients.get(1), -> + User.findById user1.id, (err, user1) -> + stripeInfo = user1.get('stripe') + expect(stripeInfo.recipients.length).toEqual(0) + verifyNotSponsoring user1.id, recipients.get(1).id, -> + verifyNotRecipient recipients.get(1).id, -> + stripe.customers.retrieveSubscription stripeInfo.customerID, stripeInfo.sponsorSubscriptionID, (err, subscription) -> + expect(err).toBeNull() + expect(subscription).not.toBeNull() + expect(subscription.quantity).toEqual(0) + done() - unless TRAVIS it 'Subscribed user1 subscribes 3 users, unsubscribes 2, themselves, then 1', (done) -> recipientCount = 3 recipientsToVerify = [0, 1, 2]