From 0f54bc2c524e1f9bd345418a2e5c355a9daf76a7 Mon Sep 17 00:00:00 2001 From: Simon Detheridge Date: Tue, 16 Oct 2018 10:15:42 +0100 Subject: [PATCH] Add additional tests for V1SubscriptionManager One call was not returning the v1Id correctly. These tests check for that case. Also added some more generic tests for the v1 API call. bug: overleaf/sharelatex#1014 --- .../Subscription/V1SubscriptionManager.coffee | 2 +- .../V1SusbcriptionManagerTests.coffee | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/services/web/app/coffee/Features/Subscription/V1SubscriptionManager.coffee b/services/web/app/coffee/Features/Subscription/V1SubscriptionManager.coffee index b4f538873f..2d25c8b3e5 100644 --- a/services/web/app/coffee/Features/Subscription/V1SubscriptionManager.coffee +++ b/services/web/app/coffee/Features/Subscription/V1SubscriptionManager.coffee @@ -66,7 +66,7 @@ module.exports = V1SubscriptionManager = V1SubscriptionManager.v1IdForUser userId, (err, v1Id) -> return callback(err) if err? - return callback(null, null) if !v1Id? + return callback(null, null, null) if !v1Id? request { baseUrl: settings.apis.v1.url url: options.url(v1Id) diff --git a/services/web/test/unit/coffee/Subscription/V1SusbcriptionManagerTests.coffee b/services/web/test/unit/coffee/Subscription/V1SusbcriptionManagerTests.coffee index 7f207e93af..7666a418bb 100644 --- a/services/web/test/unit/coffee/Subscription/V1SusbcriptionManagerTests.coffee +++ b/services/web/test/unit/coffee/Subscription/V1SusbcriptionManagerTests.coffee @@ -55,6 +55,11 @@ describe 'V1SubscriptionManager', -> ).to.equal true done() + it 'should return the v1 user id', (done) -> + @call (err, planCode, v1Id) -> + expect(v1Id).to.equal @v1UserId + done() + it 'should produce a plan-code without error', (done) -> @call (err, planCode) => expect(err).to.not.exist @@ -138,6 +143,42 @@ describe 'V1SubscriptionManager', -> expect(err).to.exist done() + describe 'when the call succeeds', -> + beforeEach -> + @V1SubscriptionManager.v1IdForUser = sinon.stub() + .yields(null, @v1UserId) + @request.yields(null, { statusCode: 200 }, "{}") + @call = (cb) => + @V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb + + it 'should not produce an error', (done) -> + @call (err, body, v1Id) => + expect(err).not.to.exist + done() + + it 'should return the v1 user id', (done) -> + @call (err, body, v1Id) => + expect(v1Id).to.equal @v1UserId + done() + + it 'should return the http response body', (done) -> + @call (err, body, v1Id) => + expect(body).to.equal "{}" + done() + + describe 'when the call returns an http error status code', -> + beforeEach -> + @V1SubscriptionManager.v1IdForUser = sinon.stub() + .yields(null, @v1UserId) + @request.yields(null, { statusCode: 500 }, "{}") + @call = (cb) => + @V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb + + it 'should produce an error', (done) -> + @call (err, body, v1Id) => + expect(err).to.exist + done() + describe 'v1IdForUser', -> beforeEach -> @UserGetter.getUser = sinon.stub()