2018-05-17 11:58:58 -04:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
modulePath = path.join __dirname, '../../../../app/js/Features/Subscription/V1SubscriptionManager'
|
|
|
|
sinon = require("sinon")
|
|
|
|
expect = require("chai").expect
|
|
|
|
|
|
|
|
|
|
|
|
describe 'V1SubscriptionManager', ->
|
|
|
|
beforeEach ->
|
|
|
|
@V1SubscriptionManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"../User/UserGetter": @UserGetter = {}
|
|
|
|
"logger-sharelatex":
|
|
|
|
log: sinon.stub()
|
|
|
|
err: sinon.stub()
|
|
|
|
warn: sinon.stub()
|
|
|
|
"settings-sharelatex":
|
2018-05-29 12:21:42 -04:00
|
|
|
apis:
|
|
|
|
v1:
|
|
|
|
host: @host = "http://overleaf.example.com"
|
2018-05-17 11:58:58 -04:00
|
|
|
"request": @request = sinon.stub()
|
|
|
|
@userId = 'abcd'
|
|
|
|
@v1UserId = 42
|
|
|
|
@user =
|
|
|
|
_id: @userId
|
|
|
|
email: 'user@example.com'
|
|
|
|
overleaf:
|
|
|
|
id: @v1UserId
|
|
|
|
|
|
|
|
describe 'getPlanCodeFromV1', ->
|
|
|
|
beforeEach ->
|
|
|
|
@responseBody =
|
|
|
|
id: 32,
|
|
|
|
plan_name: 'pro'
|
2018-05-29 12:21:42 -04:00
|
|
|
@V1SubscriptionManager._v1Request = sinon.stub()
|
2018-05-17 11:58:58 -04:00
|
|
|
.yields(null, @responseBody)
|
|
|
|
@call = (cb) =>
|
|
|
|
@V1SubscriptionManager.getPlanCodeFromV1 @userId, cb
|
|
|
|
|
|
|
|
describe 'when all goes well', ->
|
2018-05-29 12:21:42 -04:00
|
|
|
it 'should call _v1Request', (done) ->
|
2018-05-17 11:58:58 -04:00
|
|
|
@call (err, planCode) =>
|
|
|
|
expect(
|
2018-05-29 12:21:42 -04:00
|
|
|
@V1SubscriptionManager._v1Request.callCount
|
2018-05-17 11:58:58 -04:00
|
|
|
).to.equal 1
|
|
|
|
expect(
|
2018-05-29 12:21:42 -04:00
|
|
|
@V1SubscriptionManager._v1Request.calledWith(
|
|
|
|
@userId
|
2018-05-17 11:58:58 -04:00
|
|
|
)
|
|
|
|
).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should produce a plan-code without error', (done) ->
|
|
|
|
@call (err, planCode) =>
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(planCode).to.equal 'v1_pro'
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when the plan_name from v1 is null', ->
|
|
|
|
beforeEach ->
|
|
|
|
@responseBody.plan_name = null
|
|
|
|
|
|
|
|
it 'should produce a null plan-code without error', (done) ->
|
|
|
|
@call (err, planCode) =>
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(planCode).to.equal null
|
|
|
|
done()
|
|
|
|
|
2018-05-29 12:21:42 -04:00
|
|
|
describe '_v1Request', ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserGetter.getUser = sinon.stub()
|
|
|
|
.yields(null, @user)
|
|
|
|
|
2018-05-17 11:58:58 -04:00
|
|
|
describe 'when getUser produces an error', ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserGetter.getUser = sinon.stub()
|
|
|
|
.yields(new Error('woops'))
|
2018-05-29 12:21:42 -04:00
|
|
|
@call = (cb) =>
|
|
|
|
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
|
2018-05-17 11:58:58 -04:00
|
|
|
|
2018-05-29 12:21:42 -04:00
|
|
|
it 'should not call request', (done) ->
|
2018-05-17 11:58:58 -04:00
|
|
|
@call (err, planCode) =>
|
|
|
|
expect(
|
2018-05-29 12:21:42 -04:00
|
|
|
@request.callCount
|
2018-05-17 11:58:58 -04:00
|
|
|
).to.equal 0
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should produce an error', (done) ->
|
|
|
|
@call (err, planCode) =>
|
|
|
|
expect(err).to.exist
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when getUser does not find a user', ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserGetter.getUser = sinon.stub()
|
|
|
|
.yields(null, null)
|
2018-05-29 12:21:42 -04:00
|
|
|
@call = (cb) =>
|
|
|
|
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
|
2018-05-17 11:58:58 -04:00
|
|
|
|
2018-05-29 12:21:42 -04:00
|
|
|
it 'should not call request', (done) ->
|
2018-05-17 11:58:58 -04:00
|
|
|
@call (err, planCode) =>
|
|
|
|
expect(
|
2018-05-29 12:21:42 -04:00
|
|
|
@request.callCount
|
2018-05-17 11:58:58 -04:00
|
|
|
).to.equal 0
|
|
|
|
done()
|
|
|
|
|
2018-05-29 12:21:42 -04:00
|
|
|
it 'should not error', (done) ->
|
|
|
|
@call (err) =>
|
2018-05-17 11:58:58 -04:00
|
|
|
expect(err).to.not.exist
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when the request to v1 fails', ->
|
|
|
|
beforeEach ->
|
2018-05-29 12:21:42 -04:00
|
|
|
@request.yields(new Error('woops'))
|
|
|
|
@call = (cb) =>
|
|
|
|
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
|
2018-05-17 11:58:58 -04:00
|
|
|
|
|
|
|
it 'should produce an error', (done) ->
|
2018-05-29 12:21:42 -04:00
|
|
|
@call (err) =>
|
2018-05-17 11:58:58 -04:00
|
|
|
expect(err).to.exist
|
|
|
|
done()
|