UserPagesControllerTests.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. refProviders: {
  46. mendeley: true,
  47. zotero: true,
  48. },
  49. }
  50. this.UserGetter = {
  51. getUser: sinon.stub(),
  52. promises: { getUser: sinon.stub() },
  53. }
  54. this.UserSessionsManager = { getAllUserSessions: sinon.stub() }
  55. this.dropboxStatus = {}
  56. this.ErrorController = { notFound: sinon.stub() }
  57. this.SessionManager = {
  58. getLoggedInUserId: sinon.stub().returns(this.user._id),
  59. getSessionUser: sinon.stub().returns(this.user),
  60. }
  61. this.AuthenticationController = {
  62. _getRedirectFromSession: sinon.stub(),
  63. setRedirectInSession: sinon.stub(),
  64. }
  65. this.SplitTestHandler = {
  66. promises: {
  67. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  68. },
  69. }
  70. this.UserPagesController = SandboxedModule.require(modulePath, {
  71. requires: {
  72. '@overleaf/settings': this.settings,
  73. './UserGetter': this.UserGetter,
  74. './UserSessionsManager': this.UserSessionsManager,
  75. '../Errors/ErrorController': this.ErrorController,
  76. '../Authentication/AuthenticationController':
  77. this.AuthenticationController,
  78. '../Authentication/SessionManager': this.SessionManager,
  79. request: (this.request = sinon.stub()),
  80. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  81. },
  82. })
  83. this.req = {
  84. query: {},
  85. session: {
  86. user: this.user,
  87. },
  88. }
  89. return (this.res = {})
  90. })
  91. describe('registerPage', function () {
  92. it('should render the register page', function (done) {
  93. this.res.render = page => {
  94. page.should.equal('user/register')
  95. return done()
  96. }
  97. return this.UserPagesController.registerPage(this.req, this.res)
  98. })
  99. it('should set sharedProjectData', function (done) {
  100. this.req.query.project_name = 'myProject'
  101. this.req.query.user_first_name = 'user_first_name_here'
  102. this.res.render = (page, opts) => {
  103. opts.sharedProjectData.project_name.should.equal('myProject')
  104. opts.sharedProjectData.user_first_name.should.equal(
  105. 'user_first_name_here'
  106. )
  107. return done()
  108. }
  109. return this.UserPagesController.registerPage(this.req, this.res)
  110. })
  111. it('should set newTemplateData', function (done) {
  112. this.req.session.templateData = { templateName: 'templateName' }
  113. this.res.render = (page, opts) => {
  114. opts.newTemplateData.templateName.should.equal('templateName')
  115. return done()
  116. }
  117. return this.UserPagesController.registerPage(this.req, this.res)
  118. })
  119. it('should not set the newTemplateData if there is nothing in the session', function (done) {
  120. this.res.render = (page, opts) => {
  121. assert.equal(opts.newTemplateData.templateName, undefined)
  122. return done()
  123. }
  124. return this.UserPagesController.registerPage(this.req, this.res)
  125. })
  126. })
  127. describe('loginForm', function () {
  128. it('should render the login page', function (done) {
  129. this.res.render = page => {
  130. page.should.equal('user/login')
  131. return done()
  132. }
  133. return this.UserPagesController.loginPage(this.req, this.res)
  134. })
  135. describe('when an explicit redirect is set via query string', function () {
  136. beforeEach(function () {
  137. this.AuthenticationController._getRedirectFromSession = sinon
  138. .stub()
  139. .returns(null)
  140. this.AuthenticationController.setRedirectInSession = sinon.stub()
  141. return (this.req.query.redir = '/somewhere/in/particular')
  142. })
  143. it('should set a redirect', function (done) {
  144. this.res.render = page => {
  145. this.AuthenticationController.setRedirectInSession.callCount.should.equal(
  146. 1
  147. )
  148. expect(
  149. this.AuthenticationController.setRedirectInSession.lastCall.args[1]
  150. ).to.equal(this.req.query.redir)
  151. return done()
  152. }
  153. return this.UserPagesController.loginPage(this.req, this.res)
  154. })
  155. })
  156. })
  157. describe('sessionsPage', function () {
  158. beforeEach(function () {
  159. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  160. 2,
  161. null,
  162. []
  163. )
  164. })
  165. it('should render user/sessions', function (done) {
  166. this.res.render = function (page) {
  167. page.should.equal('user/sessions')
  168. return done()
  169. }
  170. return this.UserPagesController.sessionsPage(this.req, this.res)
  171. })
  172. it('should include current session data in the view', function (done) {
  173. this.res.render = (page, opts) => {
  174. expect(opts.currentSession).to.deep.equal({
  175. ip_address: '1.1.1.1',
  176. session_created: 'timestamp',
  177. })
  178. return done()
  179. }
  180. return this.UserPagesController.sessionsPage(this.req, this.res)
  181. })
  182. it('should have called getAllUserSessions', function (done) {
  183. this.res.render = page => {
  184. this.UserSessionsManager.getAllUserSessions.callCount.should.equal(1)
  185. return done()
  186. }
  187. return this.UserPagesController.sessionsPage(this.req, this.res)
  188. })
  189. describe('when getAllUserSessions produces an error', function () {
  190. beforeEach(function () {
  191. return this.UserSessionsManager.getAllUserSessions.callsArgWith(
  192. 2,
  193. new Error('woops')
  194. )
  195. })
  196. it('should call next with an error', function (done) {
  197. this.next = err => {
  198. assert(err !== null)
  199. assert(err instanceof Error)
  200. return done()
  201. }
  202. return this.UserPagesController.sessionsPage(
  203. this.req,
  204. this.res,
  205. this.next
  206. )
  207. })
  208. })
  209. })
  210. describe('settingsPage', function () {
  211. beforeEach(function () {
  212. this.request.get = sinon
  213. .stub()
  214. .callsArgWith(1, null, { statusCode: 200 }, { has_password: true })
  215. this.UserGetter.promises.getUser = sinon.stub().resolves(this.user)
  216. })
  217. it('should render user/settings', function (done) {
  218. this.res.render = function (page) {
  219. page.should.equal('user/settings')
  220. return done()
  221. }
  222. return this.UserPagesController.settingsPage(this.req, this.res)
  223. })
  224. it('should render user/settings-react', function (done) {
  225. this.SplitTestHandler.promises.getAssignment = sinon
  226. .stub()
  227. .resolves({ variant: 'react' })
  228. this.res.render = function (page) {
  229. page.should.equal('user/settings-react')
  230. return done()
  231. }
  232. return this.UserPagesController.settingsPage(this.req, this.res)
  233. })
  234. it('should send user', function (done) {
  235. this.res.render = (page, opts) => {
  236. opts.user.should.equal(this.user)
  237. return done()
  238. }
  239. return this.UserPagesController.settingsPage(this.req, this.res)
  240. })
  241. it("should set 'shouldAllowEditingDetails' to true", function (done) {
  242. this.res.render = (page, opts) => {
  243. opts.shouldAllowEditingDetails.should.equal(true)
  244. return done()
  245. }
  246. return this.UserPagesController.settingsPage(this.req, this.res)
  247. })
  248. it('should restructure thirdPartyIdentifiers data for template use', function (done) {
  249. const expectedResult = {
  250. google: 'testId',
  251. }
  252. this.res.render = (page, opts) => {
  253. expect(opts.thirdPartyIds).to.include(expectedResult)
  254. return done()
  255. }
  256. return this.UserPagesController.settingsPage(this.req, this.res)
  257. })
  258. describe('when ldap.updateUserDetailsOnLogin is true', function () {
  259. beforeEach(function () {
  260. return (this.settings.ldap = { updateUserDetailsOnLogin: true })
  261. })
  262. afterEach(function () {
  263. return delete this.settings.ldap
  264. })
  265. it('should set "shouldAllowEditingDetails" to false', function (done) {
  266. this.res.render = (page, opts) => {
  267. opts.shouldAllowEditingDetails.should.equal(false)
  268. return done()
  269. }
  270. return this.UserPagesController.settingsPage(this.req, this.res)
  271. })
  272. })
  273. describe('when saml.updateUserDetailsOnLogin is true', function () {
  274. beforeEach(function () {
  275. return (this.settings.saml = { updateUserDetailsOnLogin: true })
  276. })
  277. afterEach(function () {
  278. return delete this.settings.saml
  279. })
  280. it('should set "shouldAllowEditingDetails" to false', function (done) {
  281. this.res.render = (page, opts) => {
  282. opts.shouldAllowEditingDetails.should.equal(false)
  283. return done()
  284. }
  285. return this.UserPagesController.settingsPage(this.req, this.res)
  286. })
  287. })
  288. })
  289. })