Merge branch 'master' into sk-upgrade-metrics
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
should = require('chai').should()
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
assert = require('assert')
|
||||
path = require('path')
|
||||
modulePath = path.join __dirname, '../../../../app/js/Features/Analytics/AnalyticsController'
|
||||
sinon = require("sinon")
|
||||
expect = require("chai").expect
|
||||
|
||||
|
||||
describe 'AnalyticsController', ->
|
||||
|
||||
beforeEach ->
|
||||
@AuthenticationController =
|
||||
getLoggedInUserId: sinon.stub()
|
||||
|
||||
@AnalyticsManager =
|
||||
recordEvent: sinon.stub().callsArgWith(3)
|
||||
|
||||
@req =
|
||||
params:
|
||||
event:"i_did_something"
|
||||
body:"stuff"
|
||||
sessionID: "sessionIDHere"
|
||||
|
||||
@res =
|
||||
send:->
|
||||
@controller = SandboxedModule.require modulePath, requires:
|
||||
"./AnalyticsManager":@AnalyticsManager
|
||||
"../Authentication/AuthenticationController":@AuthenticationController
|
||||
"logger-sharelatex":
|
||||
log:->
|
||||
|
||||
describe "recordEvent", ->
|
||||
|
||||
it "should use the user_id", (done)->
|
||||
@AuthenticationController.getLoggedInUserId.returns("1234")
|
||||
@controller.recordEvent @req, @res
|
||||
@AnalyticsManager.recordEvent.calledWith("1234", @req.params["event"], @req.body).should.equal true
|
||||
done()
|
||||
|
||||
it "should use the session id", (done)->
|
||||
@controller.recordEvent @req, @res
|
||||
@AnalyticsManager.recordEvent.calledWith(@req.sessionID, @req.params["event"], @req.body).should.equal true
|
||||
done()
|
||||
@@ -254,6 +254,8 @@ describe "AuthenticationController", ->
|
||||
@cb = sinon.stub()
|
||||
@LoginRateLimiter.processLoginRequest.callsArgWith(1, null, true)
|
||||
@AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, null, @user)
|
||||
@req.sessionID = Math.random()
|
||||
@AnalyticsManager.identifyUser = sinon.stub()
|
||||
@AuthenticationController.doPassportLogin(@req, @req.body.email, @req.body.password, @cb)
|
||||
|
||||
it "should attempt to authorise the user", ->
|
||||
@@ -261,6 +263,9 @@ describe "AuthenticationController", ->
|
||||
.calledWith(email: @email.toLowerCase(), @password)
|
||||
.should.equal true
|
||||
|
||||
it "should call identifyUser", ->
|
||||
@AnalyticsManager.identifyUser.calledWith(@user._id, @req.sessionID).should.equal true
|
||||
|
||||
it "should setup the user data in the background", ->
|
||||
@UserHandler.setupLoginData.calledWith(@user).should.equal true
|
||||
|
||||
|
||||
@@ -136,7 +136,20 @@ describe "AuthorizationManager", ->
|
||||
it "should return a NotFoundError", ->
|
||||
@AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, (error) ->
|
||||
error.should.be.instanceof Errors.NotFoundError
|
||||
|
||||
|
||||
describe "when the project id is not valid", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
|
||||
@CollaboratorsHandler.getMemberIdPrivilegeLevel
|
||||
.withArgs(@user_id, @project_id)
|
||||
.yields(null, "readOnly")
|
||||
|
||||
it "should return a error", (done)->
|
||||
@AuthorizationManager.getPrivilegeLevelForProject undefined, "not project id", (err) =>
|
||||
@Project.findOne.called.should.equal false
|
||||
expect(err).to.exist
|
||||
done()
|
||||
|
||||
describe "canUserReadProject", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
|
||||
|
||||
@@ -51,17 +51,19 @@ describe "EmailSender", ->
|
||||
it "should set the properties on the email to send", (done)->
|
||||
@sesClient.sendMail.callsArgWith(1)
|
||||
|
||||
@sender.sendEmail @opts, =>
|
||||
@sender.sendEmail @opts, (err) =>
|
||||
expect(err).to.not.exist
|
||||
args = @sesClient.sendMail.args[0][0]
|
||||
args.html.should.equal @opts.html
|
||||
args.to.should.equal @opts.to
|
||||
args.subject.should.equal @opts.subject
|
||||
done()
|
||||
|
||||
it "should return the error", (done)->
|
||||
it "should return a non-specific error", (done)->
|
||||
@sesClient.sendMail.callsArgWith(1, "error")
|
||||
@sender.sendEmail {}, (err)=>
|
||||
err.should.equal "error"
|
||||
err.should.exist
|
||||
err.toString().should.equal 'Error: Cannot send email'
|
||||
done()
|
||||
|
||||
|
||||
|
||||
@@ -96,6 +96,13 @@ describe "FileStoreHandler", ->
|
||||
@fs.createReadStream.called.should.equal false
|
||||
done()
|
||||
|
||||
describe "symlink", ->
|
||||
it "should not read file stat returns nothing", (done)->
|
||||
@fs.lstat = sinon.stub().callsArgWith(1, null, null)
|
||||
@handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
|
||||
@fs.createReadStream.called.should.equal false
|
||||
done()
|
||||
|
||||
describe "when upload fails", ->
|
||||
beforeEach ->
|
||||
@writeStream.on = (type, cb) ->
|
||||
|
||||
@@ -20,6 +20,7 @@ describe 'Project api controller', ->
|
||||
session:
|
||||
destroy:sinon.stub()
|
||||
@res = {}
|
||||
@next = sinon.stub()
|
||||
@projDetails = {name:"something"}
|
||||
|
||||
|
||||
@@ -34,9 +35,7 @@ describe 'Project api controller', ->
|
||||
@controller.getProjectDetails @req, @res
|
||||
|
||||
|
||||
it "should send a 500 if there is an error", (done)->
|
||||
it "should send a 500 if there is an error", ()->
|
||||
@ProjectDetailsHandler.getDetails.callsArgWith(1, "error")
|
||||
@res.sendStatus = (resCode)=>
|
||||
resCode.should.equal 500
|
||||
done()
|
||||
@controller.getProjectDetails @req, @res
|
||||
@controller.getProjectDetails @req, @res, @next
|
||||
@next.calledWith("error").should.equal true
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
should = require('chai').should()
|
||||
modulePath = "../../../../app/js/Features/Project/ProjectDetailsHandler"
|
||||
Errors = require "../../../../app/js/Features/Errors/Errors"
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
sinon = require('sinon')
|
||||
assert = require("chai").assert
|
||||
@@ -48,6 +49,13 @@ describe 'ProjectDetailsHandler', ->
|
||||
assert.equal(details.something, undefined)
|
||||
done()
|
||||
|
||||
it "should return an error for a non-existent project", (done)->
|
||||
@ProjectGetter.getProject.callsArg(2, null, null)
|
||||
err = new Errors.NotFoundError("project not found")
|
||||
@handler.getDetails "0123456789012345678901234", (error, details) =>
|
||||
err.should.eql error
|
||||
done()
|
||||
|
||||
it "should return the error", (done)->
|
||||
error = "some error"
|
||||
@ProjectGetter.getProject.callsArgWith(2, error)
|
||||
|
||||
@@ -9,7 +9,7 @@ describe 'ProjectDuplicator', ->
|
||||
@level2folder =
|
||||
name: "level2folderName"
|
||||
_id:"level2folderId"
|
||||
docs:[@doc2 = {_id: "doc2_id", name:"level2folderDocName"}]
|
||||
docs:[@doc2 = {_id: "doc2_id", name:"level2folderDocName"}, undefined]
|
||||
folders:[]
|
||||
fileRefs:[{name:"file2", _id:"file2"}]
|
||||
@level1folder =
|
||||
@@ -17,12 +17,12 @@ describe 'ProjectDuplicator', ->
|
||||
_id:"level1folderId"
|
||||
docs:[@doc1 = {_id: "doc1_id", name:"level1folderDocName"}]
|
||||
folders:[@level2folder]
|
||||
fileRefs:[{name:"file1", _id:"file1"}]
|
||||
fileRefs:[{name:"file1", _id:"file1"}, null]
|
||||
@rootFolder =
|
||||
name:"rootFolder"
|
||||
_id:"rootFolderId"
|
||||
docs:[@doc0 = {_id: "doc0_id", name:"rootDocHere"}]
|
||||
folders:[@level1folder]
|
||||
folders:[@level1folder, {}]
|
||||
fileRefs:[{name:"file0", _id:"file0"}]
|
||||
@project =
|
||||
_id: @old_project_id = "this_is_the_old_project_id"
|
||||
@@ -117,7 +117,7 @@ describe 'ProjectDuplicator', ->
|
||||
@projectOptionsHandler.setCompiler.calledWith(@stubbedNewProject._id, @project.compiler).should.equal true
|
||||
done()
|
||||
|
||||
it 'should use the same root docccccccc', (done)->
|
||||
it 'should use the same root doc', (done)->
|
||||
@entityHandler.addDocWithProject.callsArgWith(4, null, @rootFolder.docs[0])
|
||||
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
|
||||
@entityHandler.setRootDoc.calledWith(@stubbedNewProject._id, @rootFolder.docs[0]._id).should.equal true
|
||||
|
||||
@@ -17,7 +17,7 @@ file1 = name:"file1", _id:"dsa9lkdsad"
|
||||
subSubFile = name:"subSubFile", _id:"d1d2dk"
|
||||
subSubDoc = name:"subdoc.txt", _id:"321dmdwi"
|
||||
secondSubFolder = name:"secondSubFolder", _id:"dsa3e23", docs:[subSubDoc], fileRefs:[subSubFile], folders:[]
|
||||
subFolder = name:"subFolder", _id:"dsadsa93", folders:[secondSubFolder], docs:[], fileRefs:[]
|
||||
subFolder = name:"subFolder", _id:"dsadsa93", folders:[secondSubFolder, null], docs:[], fileRefs:[]
|
||||
subFolder1 = name:"subFolder1", _id:"123asdjoij"
|
||||
|
||||
rootFolder =
|
||||
|
||||
@@ -1030,3 +1030,35 @@ describe "RecurlyWrapper", ->
|
||||
@call (err, result) =>
|
||||
expect(err).to.be.instanceof Error
|
||||
done()
|
||||
|
||||
describe "listAccountActiveSubscriptions", ->
|
||||
beforeEach ->
|
||||
@user_id = "mock-user-id"
|
||||
@callback = sinon.stub()
|
||||
@RecurlyWrapper.apiRequest = sinon.stub().yields(null, @response = {"mock": "response"}, @body = "<mock body/>")
|
||||
@RecurlyWrapper._parseSubscriptionsXml = sinon.stub().yields(null, @subscriptions = ["mock", "subscriptions"])
|
||||
|
||||
describe "with an account", ->
|
||||
beforeEach ->
|
||||
@RecurlyWrapper.listAccountActiveSubscriptions @user_id, @callback
|
||||
|
||||
it "should send a request to Recurly", ->
|
||||
@RecurlyWrapper.apiRequest
|
||||
.calledWith({
|
||||
url: "accounts/#{@user_id}/subscriptions"
|
||||
qs:
|
||||
state: "active"
|
||||
expect404: true
|
||||
})
|
||||
.should.equal true
|
||||
|
||||
it "should return the subscriptions", ->
|
||||
@callback.calledWith(null, @subscriptions).should.equal true
|
||||
|
||||
describe "without an account", ->
|
||||
beforeEach ->
|
||||
@response.statusCode = 404
|
||||
@RecurlyWrapper.listAccountActiveSubscriptions @user_id, @callback
|
||||
|
||||
it "should return an empty array of subscriptions", ->
|
||||
@callback.calledWith(null, []).should.equal true
|
||||
@@ -17,8 +17,7 @@ mockSubscriptions =
|
||||
account:
|
||||
account_code: "user-123"
|
||||
|
||||
describe "SubscriptionController sanboxed", ->
|
||||
|
||||
describe "SubscriptionController", ->
|
||||
beforeEach ->
|
||||
@user = {email:"tom@yahoo.com", _id: 'one', signUpDate: new Date('2000-10-01')}
|
||||
@activeRecurlySubscription = mockSubscriptions["subscription-123-active"]
|
||||
@@ -150,6 +149,7 @@ describe "SubscriptionController sanboxed", ->
|
||||
describe "paymentPage", ->
|
||||
beforeEach ->
|
||||
@req.headers = {}
|
||||
@SubscriptionHandler.validateNoSubscriptionInRecurly = sinon.stub().yields(null, true)
|
||||
@GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
|
||||
|
||||
describe "with a user without a subscription", ->
|
||||
@@ -209,6 +209,16 @@ describe "SubscriptionController sanboxed", ->
|
||||
opts.currency.should.equal @stubbedCurrencyCode
|
||||
done()
|
||||
@SubscriptionController.paymentPage @req, @res
|
||||
|
||||
describe "with a recurly subscription already", ->
|
||||
it "should redirect to the subscription dashboard", (done)->
|
||||
@LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
|
||||
@SubscriptionHandler.validateNoSubscriptionInRecurly = sinon.stub().yields(null, false)
|
||||
@res.redirect = (url)=>
|
||||
url.should.equal "/user/subscription"
|
||||
done()
|
||||
@SubscriptionController.paymentPage(@req, @res)
|
||||
|
||||
|
||||
describe "successful_subscription", ->
|
||||
beforeEach (done) ->
|
||||
|
||||
@@ -16,7 +16,7 @@ mockRecurlySubscriptions =
|
||||
account:
|
||||
account_code: "user-123"
|
||||
|
||||
describe "Subscription Handler sanboxed", ->
|
||||
describe "SubscriptionHandler", ->
|
||||
|
||||
beforeEach ->
|
||||
@Settings =
|
||||
@@ -33,7 +33,7 @@ describe "Subscription Handler sanboxed", ->
|
||||
@activeRecurlySubscription = mockRecurlySubscriptions["subscription-123-active"]
|
||||
@User = {}
|
||||
@user =
|
||||
_id: "user_id_here_"
|
||||
_id: @user_id = "user_id_here_"
|
||||
@subscription =
|
||||
recurlySubscription_id: @activeRecurlySubscription.uuid
|
||||
@RecurlyWrapper =
|
||||
@@ -77,23 +77,33 @@ describe "Subscription Handler sanboxed", ->
|
||||
|
||||
|
||||
describe "createSubscription", ->
|
||||
beforeEach (done) ->
|
||||
beforeEach ->
|
||||
@callback = sinon.stub()
|
||||
@subscriptionDetails =
|
||||
cvv:"123"
|
||||
number:"12345"
|
||||
@recurly_token_id = "45555666"
|
||||
@SubscriptionHandler.createSubscription(@user, @subscriptionDetails, @recurly_token_id, done)
|
||||
@SubscriptionHandler.validateNoSubscriptionInRecurly = sinon.stub().yields(null, true)
|
||||
|
||||
it "should create the subscription with the wrapper", (done)->
|
||||
@RecurlyWrapper.createSubscription.calledWith(@user, @subscriptionDetails, @recurly_token_id).should.equal true
|
||||
done()
|
||||
describe "successfully", ->
|
||||
beforeEach ->
|
||||
@SubscriptionHandler.createSubscription(@user, @subscriptionDetails, @recurly_token_id, @callback)
|
||||
|
||||
it "should sync the subscription to the user", (done)->
|
||||
@SubscriptionUpdater.syncSubscription.calledOnce.should.equal true
|
||||
@SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal @activeRecurlySubscription
|
||||
@SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal @user._id
|
||||
done()
|
||||
it "should create the subscription with the wrapper", ->
|
||||
@RecurlyWrapper.createSubscription.calledWith(@user, @subscriptionDetails, @recurly_token_id).should.equal true
|
||||
|
||||
it "should sync the subscription to the user", ->
|
||||
@SubscriptionUpdater.syncSubscription.calledOnce.should.equal true
|
||||
@SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal @activeRecurlySubscription
|
||||
@SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal @user._id
|
||||
|
||||
describe "when there is already a subscription in Recurly", ->
|
||||
beforeEach ->
|
||||
@SubscriptionHandler.validateNoSubscriptionInRecurly = sinon.stub().yields(null, false)
|
||||
@SubscriptionHandler.createSubscription(@user, @subscriptionDetails, @recurly_token_id, @callback)
|
||||
|
||||
it "should return an error", ->
|
||||
@callback.calledWith(new Error("user already has subscription in recurly"))
|
||||
|
||||
describe "updateSubscription", ->
|
||||
describe "with a user with a subscription", ->
|
||||
@@ -145,8 +155,6 @@ describe "Subscription Handler sanboxed", ->
|
||||
updateOptions = @RecurlyWrapper.updateSubscription.args[0][1]
|
||||
updateOptions.plan_code.should.equal @plan_code
|
||||
|
||||
|
||||
|
||||
describe "cancelSubscription", ->
|
||||
describe "with a user without a subscription", ->
|
||||
beforeEach (done) ->
|
||||
@@ -210,5 +218,39 @@ describe "Subscription Handler sanboxed", ->
|
||||
@SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal @activeRecurlySubscription
|
||||
@SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal @user._id
|
||||
|
||||
describe "validateNoSubscriptionInRecurly", ->
|
||||
beforeEach ->
|
||||
@subscriptions = []
|
||||
@RecurlyWrapper.listAccountActiveSubscriptions = sinon.stub().yields(null, @subscriptions)
|
||||
@SubscriptionUpdater.syncSubscription = sinon.stub().yields()
|
||||
@callback = sinon.stub()
|
||||
|
||||
describe "with no subscription in recurly", ->
|
||||
beforeEach ->
|
||||
@subscriptions.push @subscription = { "mock": "subscription" }
|
||||
@SubscriptionHandler.validateNoSubscriptionInRecurly @user_id, @callback
|
||||
|
||||
it "should call RecurlyWrapper.listAccountActiveSubscriptions with the user id", ->
|
||||
@RecurlyWrapper.listAccountActiveSubscriptions
|
||||
.calledWith(@user_id)
|
||||
.should.equal true
|
||||
|
||||
it "should sync the subscription", ->
|
||||
@SubscriptionUpdater.syncSubscription
|
||||
.calledWith(@subscription, @user_id)
|
||||
.should.equal true
|
||||
|
||||
it "should call the callback with valid == false", ->
|
||||
@callback.calledWith(null, false).should.equal true
|
||||
|
||||
describe "with a subscription in recurly", ->
|
||||
beforeEach ->
|
||||
@SubscriptionHandler.validateNoSubscriptionInRecurly @user_id, @callback
|
||||
|
||||
it "should not sync the subscription", ->
|
||||
@SubscriptionUpdater.syncSubscription
|
||||
.called
|
||||
.should.equal false
|
||||
|
||||
it "should call the callback with valid == true", ->
|
||||
@callback.calledWith(null, true).should.equal true
|
||||
|
||||
Reference in New Issue
Block a user