UserPagesControllerTests.mjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import esmock from 'esmock'
  2. import assert from 'assert'
  3. import sinon from 'sinon'
  4. import { expect } from 'chai'
  5. import MockResponse from '../helpers/MockResponse.js'
  6. import MockRequest from '../helpers/MockRequest.js'
  7. const modulePath = new URL(
  8. '../../../../app/src/Features/User/UserPagesController',
  9. import.meta.url
  10. ).pathname
  11. describe('UserPagesController', function () {
  12. beforeEach(async function () {
  13. this.settings = {
  14. apis: {
  15. v1: {
  16. url: 'some.host',
  17. user: 'one',
  18. pass: 'two',
  19. },
  20. },
  21. }
  22. this.user = {
  23. _id: (this.user_id = 'kwjewkl'),
  24. features: {},
  25. email: 'joe@example.com',
  26. ip_address: '1.1.1.1',
  27. session_created: 'timestamp',
  28. thirdPartyIdentifiers: [
  29. {
  30. providerId: 'google',
  31. externalUserId: 'testId',
  32. },
  33. ],
  34. refProviders: {
  35. mendeley: { encrypted: 'aaaa' },
  36. zotero: { encrypted: 'bbbb' },
  37. papers: { encrypted: 'cccc' },
  38. },
  39. }
  40. this.adminEmail = 'group-admin-email@overleaf.com'
  41. this.subscriptionViewModel = {
  42. memberGroupSubscriptions: [],
  43. }
  44. this.UserGetter = {
  45. getUser: sinon.stub(),
  46. promises: { getUser: sinon.stub() },
  47. }
  48. this.UserSessionsManager = { getAllUserSessions: sinon.stub() }
  49. this.dropboxStatus = {}
  50. this.ErrorController = { notFound: sinon.stub() }
  51. this.SessionManager = {
  52. getLoggedInUserId: sinon.stub().returns(this.user._id),
  53. getSessionUser: sinon.stub().returns(this.user),
  54. }
  55. this.NewsletterManager = {
  56. subscribed: sinon.stub().yields(),
  57. }
  58. this.AuthenticationController = {
  59. getRedirectFromSession: sinon.stub(),
  60. setRedirectInSession: sinon.stub(),
  61. }
  62. this.Features = {
  63. hasFeature: sinon.stub().returns(false),
  64. }
  65. this.PersonalAccessTokenManager = {
  66. listTokens: sinon.stub().returns([]),
  67. }
  68. this.SubscriptionLocator = {
  69. promises: {
  70. getAdminEmail: sinon.stub().returns(this.adminEmail),
  71. getMemberSubscriptions: sinon.stub().resolves(),
  72. },
  73. }
  74. this.SplitTestHandler = {
  75. promises: {
  76. getAssignment: sinon.stub().returns('default'),
  77. },
  78. }
  79. this.Modules = {
  80. promises: {
  81. hooks: {
  82. fire: sinon.stub().resolves(),
  83. },
  84. },
  85. }
  86. this.UserPagesController = await esmock.strict(modulePath, {
  87. '@overleaf/settings': this.settings,
  88. '../../../../app/src/Features/User/UserGetter': this.UserGetter,
  89. '../../../../app/src/Features/User/UserSessionsManager':
  90. this.UserSessionsManager,
  91. '../../../../app/src/Features/Newsletter/NewsletterManager':
  92. this.NewsletterManager,
  93. '../../../../app/src/Features/Errors/ErrorController':
  94. this.ErrorController,
  95. '../../../../app/src/Features/Authentication/AuthenticationController':
  96. this.AuthenticationController,
  97. '../../../../app/src/Features/Subscription/SubscriptionLocator':
  98. this.SubscriptionLocator,
  99. '../../../../app/src/infrastructure/Features': this.Features,
  100. '../../../../modules/oauth2-server/app/src/OAuthPersonalAccessTokenManager':
  101. this.PersonalAccessTokenManager,
  102. '../../../../app/src/Features/Authentication/SessionManager':
  103. this.SessionManager,
  104. '../../../../app/src/Features/SplitTests/SplitTestHandler':
  105. this.SplitTestHandler,
  106. '../../../../app/src/infrastructure/Modules': this.Modules,
  107. request: (this.request = sinon.stub()),
  108. })
  109. this.req = new MockRequest()
  110. this.req.session.user = this.user
  111. this.res = new MockResponse()
  112. })
  113. describe('registerPage', function () {
  114. it('should render the register page', function (done) {
  115. this.res.callback = () => {
  116. this.res.renderedTemplate.should.equal('user/register')
  117. done()
  118. }
  119. this.UserPagesController.registerPage(this.req, this.res, done)
  120. })
  121. it('should set sharedProjectData', function (done) {
  122. this.req.session.sharedProjectData = {
  123. project_name: 'myProject',
  124. user_first_name: 'user_first_name_here',
  125. }
  126. this.res.callback = () => {
  127. this.res.renderedVariables.sharedProjectData.project_name.should.equal(
  128. 'myProject'
  129. )
  130. this.res.renderedVariables.sharedProjectData.user_first_name.should.equal(
  131. 'user_first_name_here'
  132. )
  133. done()
  134. }
  135. this.UserPagesController.registerPage(this.req, this.res, done)
  136. })
  137. it('should set newTemplateData', function (done) {
  138. this.req.session.templateData = { templateName: 'templateName' }
  139. this.res.callback = () => {
  140. this.res.renderedVariables.newTemplateData.templateName.should.equal(
  141. 'templateName'
  142. )
  143. done()
  144. }
  145. this.UserPagesController.registerPage(this.req, this.res, done)
  146. })
  147. it('should not set the newTemplateData if there is nothing in the session', function (done) {
  148. this.res.callback = () => {
  149. assert.equal(
  150. this.res.renderedVariables.newTemplateData.templateName,
  151. undefined
  152. )
  153. done()
  154. }
  155. this.UserPagesController.registerPage(this.req, this.res, done)
  156. })
  157. })
  158. describe('loginForm', function () {
  159. it('should render the login page', function (done) {
  160. this.res.callback = () => {
  161. this.res.renderedTemplate.should.equal('user/login')
  162. done()
  163. }
  164. this.UserPagesController.loginPage(this.req, this.res, done)
  165. })
  166. describe('when an explicit redirect is set via query string', function () {
  167. beforeEach(function () {
  168. this.AuthenticationController.getRedirectFromSession = sinon
  169. .stub()
  170. .returns(null)
  171. this.AuthenticationController.setRedirectInSession = sinon.stub()
  172. this.req.query.redir = '/somewhere/in/particular'
  173. })
  174. it('should set a redirect', function (done) {
  175. this.res.callback = page => {
  176. this.AuthenticationController.setRedirectInSession.callCount.should.equal(
  177. 1
  178. )
  179. expect(
  180. this.AuthenticationController.setRedirectInSession.lastCall.args[1]
  181. ).to.equal(this.req.query.redir)
  182. done()
  183. }
  184. this.UserPagesController.loginPage(this.req, this.res, done)
  185. })
  186. })
  187. })
  188. describe('sessionsPage', function () {
  189. beforeEach(function () {
  190. this.UserSessionsManager.getAllUserSessions.callsArgWith(2, null, [])
  191. })
  192. it('should render user/sessions', function (done) {
  193. this.res.callback = () => {
  194. this.res.renderedTemplate.should.equal('user/sessions')
  195. done()
  196. }
  197. this.UserPagesController.sessionsPage(this.req, this.res, done)
  198. })
  199. it('should include current session data in the view', function (done) {
  200. this.res.callback = () => {
  201. expect(this.res.renderedVariables.currentSession).to.deep.equal({
  202. ip_address: '1.1.1.1',
  203. session_created: 'timestamp',
  204. })
  205. done()
  206. }
  207. this.UserPagesController.sessionsPage(this.req, this.res, done)
  208. })
  209. it('should have called getAllUserSessions', function (done) {
  210. this.res.callback = page => {
  211. this.UserSessionsManager.getAllUserSessions.callCount.should.equal(1)
  212. done()
  213. }
  214. this.UserPagesController.sessionsPage(this.req, this.res, done)
  215. })
  216. describe('when getAllUserSessions produces an error', function () {
  217. beforeEach(function () {
  218. this.UserSessionsManager.getAllUserSessions.callsArgWith(
  219. 2,
  220. new Error('woops')
  221. )
  222. })
  223. it('should call next with an error', function (done) {
  224. this.next = err => {
  225. assert(err !== null)
  226. assert(err instanceof Error)
  227. done()
  228. }
  229. this.UserPagesController.sessionsPage(this.req, this.res, this.next)
  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.callback = () => {
  240. this.res.renderedTemplate.should.equal('user/email-preferences')
  241. this.res.renderedVariables.title.should.equal('newsletter_info_title')
  242. this.res.renderedVariables.subscribed.should.equal(true)
  243. done()
  244. }
  245. this.UserPagesController.emailPreferencesPage(this.req, this.res, done)
  246. })
  247. it('render page with unsubscribed status', function (done) {
  248. this.NewsletterManager.subscribed.yields(null, false)
  249. this.res.callback = () => {
  250. this.res.renderedTemplate.should.equal('user/email-preferences')
  251. this.res.renderedVariables.title.should.equal('newsletter_info_title')
  252. this.res.renderedVariables.subscribed.should.equal(false)
  253. done()
  254. }
  255. this.UserPagesController.emailPreferencesPage(this.req, this.res, done)
  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.callback = () => {
  267. this.res.renderedTemplate.should.equal('user/settings')
  268. done()
  269. }
  270. this.UserPagesController.settingsPage(this.req, this.res, done)
  271. })
  272. it('should send user', function (done) {
  273. this.res.callback = () => {
  274. this.res.renderedVariables.user.id.should.equal(this.user._id)
  275. this.res.renderedVariables.user.email.should.equal(this.user.email)
  276. done()
  277. }
  278. this.UserPagesController.settingsPage(this.req, this.res, done)
  279. })
  280. it("should set 'shouldAllowEditingDetails' to true", function (done) {
  281. this.res.callback = () => {
  282. this.res.renderedVariables.shouldAllowEditingDetails.should.equal(true)
  283. done()
  284. }
  285. this.UserPagesController.settingsPage(this.req, this.res, done)
  286. })
  287. it('should restructure thirdPartyIdentifiers data for template use', function (done) {
  288. const expectedResult = {
  289. google: 'testId',
  290. }
  291. this.res.callback = () => {
  292. expect(this.res.renderedVariables.thirdPartyIds).to.include(
  293. expectedResult
  294. )
  295. done()
  296. }
  297. this.UserPagesController.settingsPage(this.req, this.res, done)
  298. })
  299. it("should set and clear 'projectSyncSuccessMessage'", function (done) {
  300. this.req.session.projectSyncSuccessMessage = 'Some Sync Success'
  301. this.res.callback = () => {
  302. this.res.renderedVariables.projectSyncSuccessMessage.should.equal(
  303. 'Some Sync Success'
  304. )
  305. expect(this.req.session.projectSyncSuccessMessage).to.not.exist
  306. done()
  307. }
  308. this.UserPagesController.settingsPage(this.req, this.res, done)
  309. })
  310. it('should cast refProviders to booleans', function (done) {
  311. this.res.callback = () => {
  312. expect(this.res.renderedVariables.user.refProviders).to.deep.equal({
  313. mendeley: true,
  314. papers: true,
  315. zotero: true,
  316. })
  317. done()
  318. }
  319. this.UserPagesController.settingsPage(this.req, this.res, done)
  320. })
  321. it('should send the correct managed user admin email', function (done) {
  322. this.res.callback = () => {
  323. expect(
  324. this.res.renderedVariables.currentManagedUserAdminEmail
  325. ).to.equal(this.adminEmail)
  326. done()
  327. }
  328. this.UserPagesController.settingsPage(this.req, this.res, done)
  329. })
  330. it('should send info for groups with SSO enabled', function (done) {
  331. this.user.enrollment = {
  332. sso: [
  333. {
  334. groupId: 'abc123abc123',
  335. primary: true,
  336. linkedAt: new Date(),
  337. },
  338. ],
  339. }
  340. const group1 = {
  341. _id: 'abc123abc123',
  342. teamName: 'Group SSO Rulz',
  343. admin_id: {
  344. email: 'admin.email@ssolove.com',
  345. },
  346. linked: true,
  347. }
  348. const group2 = {
  349. _id: 'def456def456',
  350. admin_id: {
  351. email: 'someone.else@noname.co.uk',
  352. },
  353. linked: false,
  354. }
  355. this.Modules.promises.hooks.fire
  356. .withArgs('getUserGroupsSSOEnrollmentStatus')
  357. .resolves([[group1, group2]])
  358. this.res.callback = () => {
  359. expect(
  360. this.res.renderedVariables.memberOfSSOEnabledGroups
  361. ).to.deep.equal([
  362. {
  363. groupId: 'abc123abc123',
  364. groupName: 'Group SSO Rulz',
  365. adminEmail: 'admin.email@ssolove.com',
  366. linked: true,
  367. },
  368. {
  369. groupId: 'def456def456',
  370. groupName: undefined,
  371. adminEmail: 'someone.else@noname.co.uk',
  372. linked: false,
  373. },
  374. ])
  375. done()
  376. }
  377. this.UserPagesController.settingsPage(this.req, this.res, done)
  378. })
  379. describe('when ldap.updateUserDetailsOnLogin is true', function () {
  380. beforeEach(function () {
  381. this.settings.ldap = { updateUserDetailsOnLogin: true }
  382. })
  383. afterEach(function () {
  384. delete this.settings.ldap
  385. })
  386. it('should set "shouldAllowEditingDetails" to false', function (done) {
  387. this.res.callback = () => {
  388. this.res.renderedVariables.shouldAllowEditingDetails.should.equal(
  389. false
  390. )
  391. done()
  392. }
  393. this.UserPagesController.settingsPage(this.req, this.res, done)
  394. })
  395. })
  396. describe('when saml.updateUserDetailsOnLogin is true', function () {
  397. beforeEach(function () {
  398. this.settings.saml = { updateUserDetailsOnLogin: true }
  399. })
  400. afterEach(function () {
  401. delete this.settings.saml
  402. })
  403. it('should set "shouldAllowEditingDetails" to false', function (done) {
  404. this.res.callback = () => {
  405. this.res.renderedVariables.shouldAllowEditingDetails.should.equal(
  406. false
  407. )
  408. done()
  409. }
  410. this.UserPagesController.settingsPage(this.req, this.res, done)
  411. })
  412. })
  413. })
  414. })