UserPagesControllerTests.mjs 14 KB

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