UserPagesControllerTests.js 11 KB

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