UserPagesControllerTests.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. ip_address: '1.1.1.1',
  38. session_created: 'timestamp',
  39. thirdPartyIdentifiers: [
  40. {
  41. providerId: 'google',
  42. externalUserId: 'testId',
  43. },
  44. ],
  45. }
  46. this.UserGetter = { getUser: sinon.stub() }
  47. this.UserSessionsManager = { getAllUserSessions: sinon.stub() }
  48. this.dropboxStatus = {}
  49. this.ErrorController = { notFound: sinon.stub() }
  50. this.SessionManager = {
  51. getLoggedInUserId: sinon.stub().returns(this.user._id),
  52. getSessionUser: sinon.stub().returns(this.user),
  53. }
  54. this.AuthenticationController = {
  55. _getRedirectFromSession: sinon.stub(),
  56. setRedirectInSession: sinon.stub(),
  57. }
  58. this.UserPagesController = SandboxedModule.require(modulePath, {
  59. requires: {
  60. '@overleaf/settings': this.settings,
  61. './UserGetter': this.UserGetter,
  62. './UserSessionsManager': this.UserSessionsManager,
  63. '../Errors/ErrorController': this.ErrorController,
  64. '../Authentication/AuthenticationController': this
  65. .AuthenticationController,
  66. '../Authentication/SessionManager': this.SessionManager,
  67. request: (this.request = sinon.stub()),
  68. },
  69. })
  70. this.req = {
  71. query: {},
  72. session: {
  73. user: this.user,
  74. },
  75. }
  76. return (this.res = {})
  77. })
  78. describe('registerPage', function () {
  79. it('should render the register page', function (done) {
  80. this.res.render = page => {
  81. page.should.equal('user/register')
  82. return done()
  83. }
  84. return this.UserPagesController.registerPage(this.req, this.res)
  85. })
  86. it('should set sharedProjectData', function (done) {
  87. this.req.query.project_name = 'myProject'
  88. this.req.query.user_first_name = 'user_first_name_here'
  89. this.res.render = (page, opts) => {
  90. opts.sharedProjectData.project_name.should.equal('myProject')
  91. opts.sharedProjectData.user_first_name.should.equal(
  92. 'user_first_name_here'
  93. )
  94. return done()
  95. }
  96. return this.UserPagesController.registerPage(this.req, this.res)
  97. })
  98. it('should set newTemplateData', function (done) {
  99. this.req.session.templateData = { templateName: 'templateName' }
  100. this.res.render = (page, opts) => {
  101. opts.newTemplateData.templateName.should.equal('templateName')
  102. return done()
  103. }
  104. return this.UserPagesController.registerPage(this.req, this.res)
  105. })
  106. it('should not set the newTemplateData if there is nothing in the session', function (done) {
  107. this.res.render = (page, opts) => {
  108. assert.equal(opts.newTemplateData.templateName, undefined)
  109. return done()
  110. }
  111. return this.UserPagesController.registerPage(this.req, this.res)
  112. })
  113. })
  114. describe('loginForm', function () {
  115. it('should render the login page', function (done) {
  116. this.res.render = page => {
  117. page.should.equal('user/login')
  118. return done()
  119. }
  120. return this.UserPagesController.loginPage(this.req, this.res)
  121. })
  122. describe('when an explicit redirect is set via query string', function () {
  123. beforeEach(function () {
  124. this.AuthenticationController._getRedirectFromSession = sinon
  125. .stub()
  126. .returns(null)
  127. this.AuthenticationController.setRedirectInSession = sinon.stub()
  128. return (this.req.query.redir = '/somewhere/in/particular')
  129. })
  130. it('should set a redirect', function (done) {
  131. this.res.render = page => {
  132. this.AuthenticationController.setRedirectInSession.callCount.should.equal(
  133. 1
  134. )
  135. expect(
  136. this.AuthenticationController.setRedirectInSession.lastCall.args[1]
  137. ).to.equal(this.req.query.redir)
  138. return done()
  139. }
  140. return this.UserPagesController.loginPage(this.req, this.res)
  141. })
  142. })
  143. })
  144. describe('sessionsPage', function () {
  145. beforeEach(function () {
  146. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  147. 2,
  148. null,
  149. []
  150. )
  151. })
  152. it('should render user/sessions', function (done) {
  153. this.res.render = function (page) {
  154. page.should.equal('user/sessions')
  155. return done()
  156. }
  157. return this.UserPagesController.sessionsPage(this.req, this.res)
  158. })
  159. it('should include current session data in the view', function (done) {
  160. this.res.render = (page, opts) => {
  161. expect(opts.currentSession).to.deep.equal({
  162. ip_address: '1.1.1.1',
  163. session_created: 'timestamp',
  164. })
  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. })