UserPagesControllerTests.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 SandboxedModule = require('sandboxed-module')
  14. const assert = require('assert')
  15. const path = require('path')
  16. const sinon = require('sinon')
  17. const modulePath = path.join(
  18. __dirname,
  19. '../../../../app/src/Features/User/UserPagesController'
  20. )
  21. const { expect } = require('chai')
  22. describe('UserPagesController', function () {
  23. beforeEach(function () {
  24. this.settings = {
  25. apis: {
  26. v1: {
  27. url: 'some.host',
  28. user: 'one',
  29. pass: 'two',
  30. },
  31. },
  32. }
  33. this.user = {
  34. _id: (this.user_id = 'kwjewkl'),
  35. features: {},
  36. email: 'joe@example.com',
  37. thirdPartyIdentifiers: [
  38. {
  39. providerId: 'google',
  40. externalUserId: 'testId',
  41. },
  42. ],
  43. }
  44. this.UserGetter = { getUser: sinon.stub() }
  45. this.UserSessionsManager = { getAllUserSessions: sinon.stub() }
  46. this.dropboxStatus = {}
  47. this.ErrorController = { notFound: sinon.stub() }
  48. this.SessionManager = {
  49. getLoggedInUserId: sinon.stub().returns(this.user._id),
  50. getSessionUser: sinon.stub().returns(this.user),
  51. }
  52. this.AuthenticationController = {
  53. _getRedirectFromSession: sinon.stub(),
  54. setRedirectInSession: sinon.stub(),
  55. }
  56. this.UserPagesController = SandboxedModule.require(modulePath, {
  57. requires: {
  58. '@overleaf/settings': this.settings,
  59. './UserGetter': this.UserGetter,
  60. './UserSessionsManager': this.UserSessionsManager,
  61. '../Errors/ErrorController': this.ErrorController,
  62. '../Authentication/AuthenticationController': this
  63. .AuthenticationController,
  64. '../Authentication/SessionManager': this.SessionManager,
  65. request: (this.request = sinon.stub()),
  66. },
  67. })
  68. this.req = {
  69. query: {},
  70. session: {
  71. user: this.user,
  72. },
  73. }
  74. return (this.res = {})
  75. })
  76. describe('registerPage', function () {
  77. it('should render the register page', function (done) {
  78. this.res.render = page => {
  79. page.should.equal('user/register')
  80. return done()
  81. }
  82. return this.UserPagesController.registerPage(this.req, this.res)
  83. })
  84. it('should set sharedProjectData', function (done) {
  85. this.req.query.project_name = 'myProject'
  86. this.req.query.user_first_name = 'user_first_name_here'
  87. this.res.render = (page, opts) => {
  88. opts.sharedProjectData.project_name.should.equal('myProject')
  89. opts.sharedProjectData.user_first_name.should.equal(
  90. 'user_first_name_here'
  91. )
  92. return done()
  93. }
  94. return this.UserPagesController.registerPage(this.req, this.res)
  95. })
  96. it('should set newTemplateData', function (done) {
  97. this.req.session.templateData = { templateName: 'templateName' }
  98. this.res.render = (page, opts) => {
  99. opts.newTemplateData.templateName.should.equal('templateName')
  100. return done()
  101. }
  102. return this.UserPagesController.registerPage(this.req, this.res)
  103. })
  104. it('should not set the newTemplateData if there is nothing in the session', function (done) {
  105. this.res.render = (page, opts) => {
  106. assert.equal(opts.newTemplateData.templateName, undefined)
  107. return done()
  108. }
  109. return this.UserPagesController.registerPage(this.req, this.res)
  110. })
  111. })
  112. describe('loginForm', function () {
  113. it('should render the login page', function (done) {
  114. this.res.render = page => {
  115. page.should.equal('user/login')
  116. return done()
  117. }
  118. return this.UserPagesController.loginPage(this.req, this.res)
  119. })
  120. describe('when an explicit redirect is set via query string', function () {
  121. beforeEach(function () {
  122. this.AuthenticationController._getRedirectFromSession = sinon
  123. .stub()
  124. .returns(null)
  125. this.AuthenticationController.setRedirectInSession = sinon.stub()
  126. return (this.req.query.redir = '/somewhere/in/particular')
  127. })
  128. it('should set a redirect', function (done) {
  129. this.res.render = page => {
  130. this.AuthenticationController.setRedirectInSession.callCount.should.equal(
  131. 1
  132. )
  133. expect(
  134. this.AuthenticationController.setRedirectInSession.lastCall.args[1]
  135. ).to.equal(this.req.query.redir)
  136. return done()
  137. }
  138. return this.UserPagesController.loginPage(this.req, this.res)
  139. })
  140. })
  141. })
  142. describe('sessionsPage', function () {
  143. beforeEach(function () {
  144. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  145. 2,
  146. null,
  147. []
  148. )
  149. })
  150. it('should render user/sessions', function (done) {
  151. this.res.render = function (page) {
  152. page.should.equal('user/sessions')
  153. return done()
  154. }
  155. return this.UserPagesController.sessionsPage(this.req, this.res)
  156. })
  157. it('should have called getAllUserSessions', function (done) {
  158. this.res.render = page => {
  159. this.UserSessionsManager.getAllUserSessions.callCount.should.equal(1)
  160. return done()
  161. }
  162. return this.UserPagesController.sessionsPage(this.req, this.res)
  163. })
  164. describe('when getAllUserSessions produces an error', function () {
  165. beforeEach(function () {
  166. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  167. 2,
  168. new Error('woops')
  169. )
  170. })
  171. it('should call next with an error', function (done) {
  172. this.next = err => {
  173. assert(err !== null)
  174. assert(err instanceof Error)
  175. return done()
  176. }
  177. return this.UserPagesController.sessionsPage(
  178. this.req,
  179. this.res,
  180. this.next
  181. )
  182. })
  183. })
  184. })
  185. describe('settingsPage', function () {
  186. beforeEach(function () {
  187. this.request.get = sinon
  188. .stub()
  189. .callsArgWith(1, null, { statusCode: 200 }, { has_password: true })
  190. return (this.UserGetter.getUser = sinon
  191. .stub()
  192. .callsArgWith(1, null, this.user))
  193. })
  194. it('should render user/settings', function (done) {
  195. this.res.render = function (page) {
  196. page.should.equal('user/settings')
  197. return done()
  198. }
  199. return this.UserPagesController.settingsPage(this.req, this.res)
  200. })
  201. it('should send user', function (done) {
  202. this.res.render = (page, opts) => {
  203. opts.user.should.equal(this.user)
  204. return done()
  205. }
  206. return this.UserPagesController.settingsPage(this.req, this.res)
  207. })
  208. it("should set 'shouldAllowEditingDetails' to true", function (done) {
  209. this.res.render = (page, opts) => {
  210. opts.shouldAllowEditingDetails.should.equal(true)
  211. return done()
  212. }
  213. return this.UserPagesController.settingsPage(this.req, this.res)
  214. })
  215. it('should restructure thirdPartyIdentifiers data for template use', function (done) {
  216. const expectedResult = {
  217. google: 'testId',
  218. }
  219. this.res.render = (page, opts) => {
  220. expect(opts.thirdPartyIds).to.include(expectedResult)
  221. return done()
  222. }
  223. return this.UserPagesController.settingsPage(this.req, this.res)
  224. })
  225. describe('when ldap.updateUserDetailsOnLogin is true', function () {
  226. beforeEach(function () {
  227. return (this.settings.ldap = { updateUserDetailsOnLogin: true })
  228. })
  229. afterEach(function () {
  230. return delete this.settings.ldap
  231. })
  232. it('should set "shouldAllowEditingDetails" to false', function (done) {
  233. this.res.render = (page, opts) => {
  234. opts.shouldAllowEditingDetails.should.equal(false)
  235. return done()
  236. }
  237. return this.UserPagesController.settingsPage(this.req, this.res)
  238. })
  239. })
  240. describe('when saml.updateUserDetailsOnLogin is true', function () {
  241. beforeEach(function () {
  242. return (this.settings.saml = { updateUserDetailsOnLogin: true })
  243. })
  244. afterEach(function () {
  245. return delete this.settings.saml
  246. })
  247. it('should set "shouldAllowEditingDetails" to false', function (done) {
  248. this.res.render = (page, opts) => {
  249. opts.shouldAllowEditingDetails.should.equal(false)
  250. return done()
  251. }
  252. return this.UserPagesController.settingsPage(this.req, this.res)
  253. })
  254. })
  255. })
  256. })