UserPagesControllerTests.js 12 KB

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