UserPagesControllerTests.js 11 KB

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