UserPagesControllerTests.js 11 KB

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