UserPagesControllerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const should = require('chai').should()
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/User/UserPagesController'
  21. )
  22. const { expect } = require('chai')
  23. describe('UserPagesController', function() {
  24. beforeEach(function() {
  25. this.settings = {
  26. apis: {
  27. v1: {
  28. url: 'some.host',
  29. user: 'one',
  30. pass: 'two'
  31. }
  32. }
  33. }
  34. this.user = {
  35. _id: (this.user_id = 'kwjewkl'),
  36. features: {},
  37. email: 'joe@example.com',
  38. thirdPartyIdentifiers: [
  39. {
  40. providerId: 'google',
  41. externalUserId: 'testId'
  42. }
  43. ]
  44. }
  45. this.UserGetter = { getUser: sinon.stub() }
  46. this.UserSessionsManager = { getAllUserSessions: sinon.stub() }
  47. this.dropboxStatus = {}
  48. this.DropboxHandler = {
  49. getUserRegistrationStatus: sinon
  50. .stub()
  51. .callsArgWith(1, null, this.dropboxStatus)
  52. }
  53. this.ErrorController = { notFound: sinon.stub() }
  54. this.AuthenticationController = {
  55. getLoggedInUserId: sinon.stub().returns(this.user._id),
  56. getSessionUser: sinon.stub().returns(this.user),
  57. _getRedirectFromSession: sinon.stub(),
  58. setRedirectInSession: sinon.stub()
  59. }
  60. this.UserPagesController = SandboxedModule.require(modulePath, {
  61. requires: {
  62. 'settings-sharelatex': this.settings,
  63. 'logger-sharelatex': {
  64. log() {},
  65. err() {}
  66. },
  67. './UserGetter': this.UserGetter,
  68. './UserSessionsManager': this.UserSessionsManager,
  69. '../Errors/ErrorController': this.ErrorController,
  70. '../Dropbox/DropboxHandler': this.DropboxHandler,
  71. '../Authentication/AuthenticationController': this
  72. .AuthenticationController,
  73. request: (this.request = sinon.stub())
  74. }
  75. })
  76. this.req = {
  77. query: {},
  78. session: {
  79. user: this.user
  80. }
  81. }
  82. return (this.res = {})
  83. })
  84. describe('registerPage', function() {
  85. it('should render the register page', function(done) {
  86. this.res.render = page => {
  87. page.should.equal('user/register')
  88. return done()
  89. }
  90. return this.UserPagesController.registerPage(this.req, this.res)
  91. })
  92. it('should set sharedProjectData', function(done) {
  93. this.req.query.project_name = 'myProject'
  94. this.req.query.user_first_name = 'user_first_name_here'
  95. this.res.render = (page, opts) => {
  96. opts.sharedProjectData.project_name.should.equal('myProject')
  97. opts.sharedProjectData.user_first_name.should.equal(
  98. 'user_first_name_here'
  99. )
  100. return done()
  101. }
  102. return this.UserPagesController.registerPage(this.req, this.res)
  103. })
  104. it('should set newTemplateData', function(done) {
  105. this.req.session.templateData = { templateName: 'templateName' }
  106. this.res.render = (page, opts) => {
  107. opts.newTemplateData.templateName.should.equal('templateName')
  108. return done()
  109. }
  110. return this.UserPagesController.registerPage(this.req, this.res)
  111. })
  112. return it('should not set the newTemplateData if there is nothing in the session', function(done) {
  113. this.res.render = (page, opts) => {
  114. assert.equal(opts.newTemplateData.templateName, undefined)
  115. return done()
  116. }
  117. return this.UserPagesController.registerPage(this.req, this.res)
  118. })
  119. })
  120. describe('loginForm', function() {
  121. it('should render the login page', function(done) {
  122. this.res.render = page => {
  123. page.should.equal('user/login')
  124. return done()
  125. }
  126. return this.UserPagesController.loginPage(this.req, this.res)
  127. })
  128. return describe('when an explicit redirect is set via query string', function() {
  129. beforeEach(function() {
  130. this.AuthenticationController._getRedirectFromSession = sinon
  131. .stub()
  132. .returns(null)
  133. this.AuthenticationController.setRedirectInSession = sinon.stub()
  134. return (this.req.query.redir = '/somewhere/in/particular')
  135. })
  136. return it('should set a redirect', function(done) {
  137. this.res.render = page => {
  138. this.AuthenticationController.setRedirectInSession.callCount.should.equal(
  139. 1
  140. )
  141. expect(
  142. this.AuthenticationController.setRedirectInSession.lastCall.args[1]
  143. ).to.equal(this.req.query.redir)
  144. return done()
  145. }
  146. return this.UserPagesController.loginPage(this.req, this.res)
  147. })
  148. })
  149. })
  150. describe('sessionsPage', function() {
  151. beforeEach(function() {
  152. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  153. 2,
  154. null,
  155. []
  156. )
  157. })
  158. it('should render user/sessions', function(done) {
  159. this.res.render = function(page) {
  160. page.should.equal('user/sessions')
  161. return done()
  162. }
  163. return this.UserPagesController.sessionsPage(this.req, this.res)
  164. })
  165. it('should have called getAllUserSessions', function(done) {
  166. this.res.render = page => {
  167. this.UserSessionsManager.getAllUserSessions.callCount.should.equal(1)
  168. return done()
  169. }
  170. return this.UserPagesController.sessionsPage(this.req, this.res)
  171. })
  172. return describe('when getAllUserSessions produces an error', function() {
  173. beforeEach(function() {
  174. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  175. 2,
  176. new Error('woops')
  177. )
  178. })
  179. return it('should call next with an error', function(done) {
  180. this.next = err => {
  181. assert(err !== null)
  182. assert(err instanceof Error)
  183. return done()
  184. }
  185. return this.UserPagesController.sessionsPage(
  186. this.req,
  187. this.res,
  188. this.next
  189. )
  190. })
  191. })
  192. })
  193. describe('settingsPage', function() {
  194. beforeEach(function() {
  195. this.request.get = sinon
  196. .stub()
  197. .callsArgWith(1, null, { statusCode: 200 }, { has_password: true })
  198. return (this.UserGetter.getUser = sinon
  199. .stub()
  200. .callsArgWith(1, null, this.user))
  201. })
  202. it('should render user/settings', function(done) {
  203. this.res.render = function(page) {
  204. page.should.equal('user/settings')
  205. return done()
  206. }
  207. return this.UserPagesController.settingsPage(this.req, this.res)
  208. })
  209. it('should send user', function(done) {
  210. this.res.render = (page, opts) => {
  211. opts.user.should.equal(this.user)
  212. return done()
  213. }
  214. return this.UserPagesController.settingsPage(this.req, this.res)
  215. })
  216. it("should set 'shouldAllowEditingDetails' to true", function(done) {
  217. this.res.render = (page, opts) => {
  218. opts.shouldAllowEditingDetails.should.equal(true)
  219. return done()
  220. }
  221. return this.UserPagesController.settingsPage(this.req, this.res)
  222. })
  223. it('should restructure thirdPartyIdentifiers data for template use', function(done) {
  224. const expectedResult = {
  225. google: 'testId'
  226. }
  227. this.res.render = (page, opts) => {
  228. expect(opts.thirdPartyIds).to.include(expectedResult)
  229. return done()
  230. }
  231. return this.UserPagesController.settingsPage(this.req, this.res)
  232. })
  233. describe('when ldap.updateUserDetailsOnLogin is true', function() {
  234. beforeEach(function() {
  235. return (this.settings.ldap = { updateUserDetailsOnLogin: true })
  236. })
  237. afterEach(function() {
  238. return delete this.settings.ldap
  239. })
  240. return it('should set "shouldAllowEditingDetails" to false', function(done) {
  241. this.res.render = (page, opts) => {
  242. opts.shouldAllowEditingDetails.should.equal(false)
  243. return done()
  244. }
  245. return this.UserPagesController.settingsPage(this.req, this.res)
  246. })
  247. })
  248. return describe('when saml.updateUserDetailsOnLogin is true', function() {
  249. beforeEach(function() {
  250. return (this.settings.saml = { updateUserDetailsOnLogin: true })
  251. })
  252. afterEach(function() {
  253. return delete this.settings.saml
  254. })
  255. return it('should set "shouldAllowEditingDetails" to false', function(done) {
  256. this.res.render = (page, opts) => {
  257. opts.shouldAllowEditingDetails.should.equal(false)
  258. return done()
  259. }
  260. return this.UserPagesController.settingsPage(this.req, this.res)
  261. })
  262. })
  263. })
  264. return describe('activateAccountPage', function() {
  265. beforeEach(function() {
  266. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  267. this.req.query.user_id = this.user_id
  268. return (this.req.query.token = this.token = 'mock-token-123')
  269. })
  270. it('should 404 without a user_id', function(done) {
  271. delete this.req.query.user_id
  272. this.ErrorController.notFound = () => done()
  273. return this.UserPagesController.activateAccountPage(this.req, this.res)
  274. })
  275. it('should 404 without a token', function(done) {
  276. delete this.req.query.token
  277. this.ErrorController.notFound = () => done()
  278. return this.UserPagesController.activateAccountPage(this.req, this.res)
  279. })
  280. it('should 404 without a valid user_id', function(done) {
  281. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  282. this.ErrorController.notFound = () => done()
  283. return this.UserPagesController.activateAccountPage(this.req, this.res)
  284. })
  285. it('should redirect activated users to login', function(done) {
  286. this.user.loginCount = 1
  287. this.res.redirect = url => {
  288. this.UserGetter.getUser.calledWith(this.user_id).should.equal(true)
  289. url.should.equal(`/login?email=${encodeURIComponent(this.user.email)}`)
  290. return done()
  291. }
  292. return this.UserPagesController.activateAccountPage(this.req, this.res)
  293. })
  294. return it('render the activation page if the user has not logged in before', function(done) {
  295. this.user.loginCount = 0
  296. this.res.render = (page, opts) => {
  297. page.should.equal('user/activate')
  298. opts.email.should.equal(this.user.email)
  299. opts.token.should.equal(this.token)
  300. return done()
  301. }
  302. return this.UserPagesController.activateAccountPage(this.req, this.res)
  303. })
  304. })
  305. })