UserPagesControllerTests.js 14 KB

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