2014-04-09 07:17:50 -04:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = path.join __dirname, "../../../../app/js/Features/User/UserPagesController"
|
|
|
|
expect = require("chai").expect
|
|
|
|
|
|
|
|
describe "UserPagesController", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
|
|
|
|
@settings = {}
|
2016-09-07 11:40:49 -04:00
|
|
|
@user =
|
2015-12-11 06:30:06 -05:00
|
|
|
_id: @user_id = "kwjewkl"
|
2014-04-09 10:04:47 -04:00
|
|
|
features:{}
|
2015-12-11 06:30:06 -05:00
|
|
|
email: "joe@example.com"
|
2014-04-09 10:41:19 -04:00
|
|
|
|
2014-04-09 10:04:47 -04:00
|
|
|
@UserLocator =
|
|
|
|
findById: sinon.stub().callsArgWith(1, null, @user)
|
2015-12-11 06:30:06 -05:00
|
|
|
@UserGetter =
|
|
|
|
getUser: sinon.stub().callsArgWith(2, null, @user)
|
2016-10-07 05:52:58 -04:00
|
|
|
@UserSessionsManager =
|
|
|
|
getAllUserSessions: sinon.stub()
|
2014-04-09 10:04:47 -04:00
|
|
|
@dropboxStatus = {}
|
|
|
|
@DropboxHandler =
|
|
|
|
getUserRegistrationStatus : sinon.stub().callsArgWith(1, null, @dropboxStatus)
|
2015-12-11 06:30:06 -05:00
|
|
|
@ErrorController =
|
|
|
|
notFound: sinon.stub()
|
2016-09-07 11:40:49 -04:00
|
|
|
@AuthenticationController =
|
|
|
|
getLoggedInUserId: sinon.stub().returns(@user._id)
|
2016-10-07 05:52:58 -04:00
|
|
|
getSessionUser: sinon.stub().returns(@user)
|
2014-04-09 07:17:50 -04:00
|
|
|
@UserPagesController = SandboxedModule.require modulePath, requires:
|
|
|
|
"settings-sharelatex":@settings
|
2016-10-07 05:52:58 -04:00
|
|
|
"logger-sharelatex":
|
|
|
|
log:->
|
|
|
|
err:->
|
2014-04-09 10:04:47 -04:00
|
|
|
"./UserLocator": @UserLocator
|
2015-12-11 06:30:06 -05:00
|
|
|
"./UserGetter": @UserGetter
|
2016-10-07 05:52:58 -04:00
|
|
|
"./UserSessionsManager": @UserSessionsManager
|
2015-12-11 06:30:06 -05:00
|
|
|
"../Errors/ErrorController": @ErrorController
|
2014-04-09 10:04:47 -04:00
|
|
|
'../Dropbox/DropboxHandler': @DropboxHandler
|
2016-09-07 11:40:49 -04:00
|
|
|
'../Authentication/AuthenticationController': @AuthenticationController
|
|
|
|
@req =
|
2014-04-09 07:17:50 -04:00
|
|
|
query:{}
|
2014-04-09 10:04:47 -04:00
|
|
|
session:
|
|
|
|
user:@user
|
2014-04-09 07:17:50 -04:00
|
|
|
@res = {}
|
|
|
|
|
|
|
|
|
|
|
|
describe "registerPage", ->
|
|
|
|
|
|
|
|
it "should render the register page", (done)->
|
|
|
|
@res.render = (page)=>
|
|
|
|
page.should.equal "user/register"
|
|
|
|
done()
|
|
|
|
@UserPagesController.registerPage @req, @res
|
|
|
|
|
|
|
|
it "should set the redirect", (done)->
|
|
|
|
redirect = "/go/here/please"
|
|
|
|
@req.query.redir = redirect
|
|
|
|
@res.render = (page, opts)=>
|
|
|
|
opts.redir.should.equal redirect
|
|
|
|
done()
|
|
|
|
@UserPagesController.registerPage @req, @res
|
|
|
|
|
|
|
|
it "should set sharedProjectData", (done)->
|
|
|
|
@req.query.project_name = "myProject"
|
|
|
|
@req.query.user_first_name = "user_first_name_here"
|
|
|
|
|
|
|
|
@res.render = (page, opts)=>
|
|
|
|
opts.sharedProjectData.project_name.should.equal "myProject"
|
|
|
|
opts.sharedProjectData.user_first_name.should.equal "user_first_name_here"
|
|
|
|
done()
|
|
|
|
@UserPagesController.registerPage @req, @res
|
|
|
|
|
|
|
|
it "should set newTemplateData", (done)->
|
|
|
|
@req.session.templateData =
|
|
|
|
templateName : "templateName"
|
|
|
|
|
|
|
|
@res.render = (page, opts)=>
|
|
|
|
opts.newTemplateData.templateName.should.equal "templateName"
|
|
|
|
done()
|
|
|
|
@UserPagesController.registerPage @req, @res
|
|
|
|
|
|
|
|
it "should not set the newTemplateData if there is nothing in the session", (done)->
|
|
|
|
@res.render = (page, opts)=>
|
|
|
|
assert.equal opts.newTemplateData.templateName, undefined
|
|
|
|
done()
|
|
|
|
@UserPagesController.registerPage @req, @res
|
|
|
|
|
|
|
|
|
2014-04-09 07:22:57 -04:00
|
|
|
describe "loginForm", ->
|
2014-04-09 07:17:50 -04:00
|
|
|
|
2014-04-09 07:22:57 -04:00
|
|
|
it "should render the login page", (done)->
|
|
|
|
@res.render = (page)=>
|
|
|
|
page.should.equal "user/login"
|
|
|
|
done()
|
|
|
|
@UserPagesController.loginPage @req, @res
|
2014-04-09 07:17:50 -04:00
|
|
|
|
2014-04-09 07:22:57 -04:00
|
|
|
it "should set the redirect", (done)->
|
|
|
|
redirect = "/go/here/please"
|
|
|
|
@req.query.redir = redirect
|
|
|
|
@res.render = (page, opts)=>
|
|
|
|
opts.redir.should.equal redirect
|
|
|
|
done()
|
|
|
|
@UserPagesController.loginPage @req, @res
|
2014-04-09 07:26:59 -04:00
|
|
|
|
2016-10-07 05:52:58 -04:00
|
|
|
describe 'sessionsPage', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@UserSessionsManager.getAllUserSessions.callsArgWith(2, null, [])
|
|
|
|
|
|
|
|
it 'should render user/sessions', (done) ->
|
|
|
|
@res.render = (page)->
|
|
|
|
page.should.equal "user/sessions"
|
|
|
|
done()
|
|
|
|
@UserPagesController.sessionsPage @req, @res
|
|
|
|
|
|
|
|
it 'should have called getAllUserSessions', (done) ->
|
|
|
|
@res.render = (page) =>
|
|
|
|
@UserSessionsManager.getAllUserSessions.callCount.should.equal 1
|
|
|
|
done()
|
|
|
|
@UserPagesController.sessionsPage @req, @res
|
|
|
|
|
|
|
|
describe 'when getAllUserSessions produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@UserSessionsManager.getAllUserSessions.callsArgWith(2, new Error('woops'))
|
|
|
|
|
|
|
|
it 'should call next with an error', (done) ->
|
|
|
|
@next = (err) =>
|
|
|
|
assert(err != null)
|
|
|
|
assert(err instanceof Error)
|
|
|
|
done()
|
|
|
|
@UserPagesController.sessionsPage @req, @res, @next
|
2014-04-09 07:26:59 -04:00
|
|
|
|
2014-04-09 10:04:47 -04:00
|
|
|
describe "settingsPage", ->
|
2014-04-09 09:43:08 -04:00
|
|
|
|
2014-04-09 10:04:47 -04:00
|
|
|
it "should render user/settings", (done)->
|
|
|
|
@res.render = (page)->
|
|
|
|
page.should.equal "user/settings"
|
|
|
|
done()
|
|
|
|
@UserPagesController.settingsPage @req, @res
|
2014-04-09 09:43:08 -04:00
|
|
|
|
2014-04-09 10:04:47 -04:00
|
|
|
it "should send user", (done)->
|
|
|
|
@res.render = (page, opts)=>
|
|
|
|
opts.user.should.equal @user
|
|
|
|
done()
|
2015-12-11 06:30:06 -05:00
|
|
|
@UserPagesController.settingsPage @req, @res
|
2016-09-07 11:40:49 -04:00
|
|
|
|
2015-12-11 06:30:06 -05:00
|
|
|
describe "activateAccountPage", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.query.user_id = @user_id
|
|
|
|
@req.query.token = @token = "mock-token-123"
|
2016-09-07 11:40:49 -04:00
|
|
|
|
2015-12-11 06:30:06 -05:00
|
|
|
it "should 404 without a user_id", (done) ->
|
|
|
|
delete @req.query.user_id
|
|
|
|
@ErrorController.notFound = () ->
|
|
|
|
done()
|
|
|
|
@UserPagesController.activateAccountPage @req, @res
|
2016-09-07 11:40:49 -04:00
|
|
|
|
2015-12-11 06:30:06 -05:00
|
|
|
it "should 404 without a token", (done) ->
|
|
|
|
delete @req.query.token
|
|
|
|
@ErrorController.notFound = () ->
|
|
|
|
done()
|
|
|
|
@UserPagesController.activateAccountPage @req, @res
|
2016-09-07 11:40:49 -04:00
|
|
|
|
2015-12-11 06:30:06 -05:00
|
|
|
it "should 404 without a valid user_id", (done) ->
|
|
|
|
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
|
|
|
@ErrorController.notFound = () ->
|
|
|
|
done()
|
|
|
|
@UserPagesController.activateAccountPage @req, @res
|
|
|
|
|
|
|
|
it "should redirect activated users to login", (done) ->
|
|
|
|
@user.loginCount = 1
|
|
|
|
@res.redirect = (url) =>
|
|
|
|
@UserGetter.getUser.calledWith(@user_id).should.equal true
|
|
|
|
url.should.equal "/login?email=#{encodeURIComponent(@user.email)}"
|
|
|
|
done()
|
|
|
|
@UserPagesController.activateAccountPage @req, @res
|
2016-09-07 11:40:49 -04:00
|
|
|
|
2015-12-11 06:30:06 -05:00
|
|
|
it "render the activation page if the user has not logged in before", (done) ->
|
|
|
|
@user.loginCount = 0
|
|
|
|
@res.render = (page, opts) =>
|
|
|
|
page.should.equal "user/activate"
|
|
|
|
opts.email.should.equal @user.email
|
|
|
|
opts.token.should.equal @token
|
|
|
|
done()
|
2016-09-07 11:40:49 -04:00
|
|
|
@UserPagesController.activateAccountPage @req, @res
|