UserPagesControllerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. globals: {
  62. console: console
  63. },
  64. requires: {
  65. 'settings-sharelatex': this.settings,
  66. 'logger-sharelatex': {
  67. log() {},
  68. warn() {},
  69. err() {}
  70. },
  71. './UserGetter': this.UserGetter,
  72. './UserSessionsManager': this.UserSessionsManager,
  73. '../Errors/ErrorController': this.ErrorController,
  74. '../Dropbox/DropboxHandler': this.DropboxHandler,
  75. '../Authentication/AuthenticationController': this
  76. .AuthenticationController,
  77. request: (this.request = sinon.stub())
  78. }
  79. })
  80. this.req = {
  81. query: {},
  82. session: {
  83. user: this.user
  84. }
  85. }
  86. return (this.res = {})
  87. })
  88. describe('registerPage', function() {
  89. it('should render the register page', function(done) {
  90. this.res.render = page => {
  91. page.should.equal('user/register')
  92. return done()
  93. }
  94. return this.UserPagesController.registerPage(this.req, this.res)
  95. })
  96. it('should set sharedProjectData', function(done) {
  97. this.req.query.project_name = 'myProject'
  98. this.req.query.user_first_name = 'user_first_name_here'
  99. this.res.render = (page, opts) => {
  100. opts.sharedProjectData.project_name.should.equal('myProject')
  101. opts.sharedProjectData.user_first_name.should.equal(
  102. 'user_first_name_here'
  103. )
  104. return done()
  105. }
  106. return this.UserPagesController.registerPage(this.req, this.res)
  107. })
  108. it('should set newTemplateData', function(done) {
  109. this.req.session.templateData = { templateName: 'templateName' }
  110. this.res.render = (page, opts) => {
  111. opts.newTemplateData.templateName.should.equal('templateName')
  112. return done()
  113. }
  114. return this.UserPagesController.registerPage(this.req, this.res)
  115. })
  116. it('should not set the newTemplateData if there is nothing in the session', function(done) {
  117. this.res.render = (page, opts) => {
  118. assert.equal(opts.newTemplateData.templateName, undefined)
  119. return done()
  120. }
  121. return this.UserPagesController.registerPage(this.req, this.res)
  122. })
  123. })
  124. describe('loginForm', function() {
  125. it('should render the login page', function(done) {
  126. this.res.render = page => {
  127. page.should.equal('user/login')
  128. return done()
  129. }
  130. return this.UserPagesController.loginPage(this.req, this.res)
  131. })
  132. describe('when an explicit redirect is set via query string', function() {
  133. beforeEach(function() {
  134. this.AuthenticationController._getRedirectFromSession = sinon
  135. .stub()
  136. .returns(null)
  137. this.AuthenticationController.setRedirectInSession = sinon.stub()
  138. return (this.req.query.redir = '/somewhere/in/particular')
  139. })
  140. it('should set a redirect', function(done) {
  141. this.res.render = page => {
  142. this.AuthenticationController.setRedirectInSession.callCount.should.equal(
  143. 1
  144. )
  145. expect(
  146. this.AuthenticationController.setRedirectInSession.lastCall.args[1]
  147. ).to.equal(this.req.query.redir)
  148. return done()
  149. }
  150. return this.UserPagesController.loginPage(this.req, this.res)
  151. })
  152. })
  153. })
  154. describe('sessionsPage', function() {
  155. beforeEach(function() {
  156. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  157. 2,
  158. null,
  159. []
  160. )
  161. })
  162. it('should render user/sessions', function(done) {
  163. this.res.render = function(page) {
  164. page.should.equal('user/sessions')
  165. return done()
  166. }
  167. return this.UserPagesController.sessionsPage(this.req, this.res)
  168. })
  169. it('should have called getAllUserSessions', function(done) {
  170. this.res.render = page => {
  171. this.UserSessionsManager.getAllUserSessions.callCount.should.equal(1)
  172. return done()
  173. }
  174. return this.UserPagesController.sessionsPage(this.req, this.res)
  175. })
  176. describe('when getAllUserSessions produces an error', function() {
  177. beforeEach(function() {
  178. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  179. 2,
  180. new Error('woops')
  181. )
  182. })
  183. it('should call next with an error', function(done) {
  184. this.next = err => {
  185. assert(err !== null)
  186. assert(err instanceof Error)
  187. return done()
  188. }
  189. return this.UserPagesController.sessionsPage(
  190. this.req,
  191. this.res,
  192. this.next
  193. )
  194. })
  195. })
  196. })
  197. describe('settingsPage', function() {
  198. beforeEach(function() {
  199. this.request.get = sinon
  200. .stub()
  201. .callsArgWith(1, null, { statusCode: 200 }, { has_password: true })
  202. return (this.UserGetter.getUser = sinon
  203. .stub()
  204. .callsArgWith(1, null, this.user))
  205. })
  206. it('should render user/settings', function(done) {
  207. this.res.render = function(page) {
  208. page.should.equal('user/settings')
  209. return done()
  210. }
  211. return this.UserPagesController.settingsPage(this.req, this.res)
  212. })
  213. it('should send user', function(done) {
  214. this.res.render = (page, opts) => {
  215. opts.user.should.equal(this.user)
  216. return done()
  217. }
  218. return this.UserPagesController.settingsPage(this.req, this.res)
  219. })
  220. it("should set 'shouldAllowEditingDetails' to true", function(done) {
  221. this.res.render = (page, opts) => {
  222. opts.shouldAllowEditingDetails.should.equal(true)
  223. return done()
  224. }
  225. return this.UserPagesController.settingsPage(this.req, this.res)
  226. })
  227. it('should restructure thirdPartyIdentifiers data for template use', function(done) {
  228. const expectedResult = {
  229. google: 'testId'
  230. }
  231. this.res.render = (page, opts) => {
  232. expect(opts.thirdPartyIds).to.include(expectedResult)
  233. return done()
  234. }
  235. return this.UserPagesController.settingsPage(this.req, this.res)
  236. })
  237. describe('when ldap.updateUserDetailsOnLogin is true', function() {
  238. beforeEach(function() {
  239. return (this.settings.ldap = { updateUserDetailsOnLogin: true })
  240. })
  241. afterEach(function() {
  242. return delete this.settings.ldap
  243. })
  244. it('should set "shouldAllowEditingDetails" to false', function(done) {
  245. this.res.render = (page, opts) => {
  246. opts.shouldAllowEditingDetails.should.equal(false)
  247. return done()
  248. }
  249. return this.UserPagesController.settingsPage(this.req, this.res)
  250. })
  251. })
  252. describe('when saml.updateUserDetailsOnLogin is true', function() {
  253. beforeEach(function() {
  254. return (this.settings.saml = { updateUserDetailsOnLogin: true })
  255. })
  256. afterEach(function() {
  257. return delete this.settings.saml
  258. })
  259. it('should set "shouldAllowEditingDetails" to false', function(done) {
  260. this.res.render = (page, opts) => {
  261. opts.shouldAllowEditingDetails.should.equal(false)
  262. return done()
  263. }
  264. return this.UserPagesController.settingsPage(this.req, this.res)
  265. })
  266. })
  267. })
  268. describe('activateAccountPage', function() {
  269. beforeEach(function() {
  270. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  271. this.req.query.user_id = this.user_id
  272. return (this.req.query.token = this.token = 'mock-token-123')
  273. })
  274. it('should 404 without a user_id', function(done) {
  275. delete this.req.query.user_id
  276. this.ErrorController.notFound = () => done()
  277. return this.UserPagesController.activateAccountPage(this.req, this.res)
  278. })
  279. it('should 404 without a token', function(done) {
  280. delete this.req.query.token
  281. this.ErrorController.notFound = () => done()
  282. return this.UserPagesController.activateAccountPage(this.req, this.res)
  283. })
  284. it('should 404 without a valid user_id', function(done) {
  285. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  286. this.ErrorController.notFound = () => done()
  287. return this.UserPagesController.activateAccountPage(this.req, this.res)
  288. })
  289. it('should redirect activated users to login', function(done) {
  290. this.user.loginCount = 1
  291. this.res.redirect = url => {
  292. this.UserGetter.getUser.calledWith(this.user_id).should.equal(true)
  293. url.should.equal(`/login?email=${encodeURIComponent(this.user.email)}`)
  294. return done()
  295. }
  296. return this.UserPagesController.activateAccountPage(this.req, this.res)
  297. })
  298. it('render the activation page if the user has not logged in before', function(done) {
  299. this.user.loginCount = 0
  300. this.res.render = (page, opts) => {
  301. page.should.equal('user/activate')
  302. opts.email.should.equal(this.user.email)
  303. opts.token.should.equal(this.token)
  304. return done()
  305. }
  306. return this.UserPagesController.activateAccountPage(this.req, this.res)
  307. })
  308. })
  309. })