AuthenticationControllerTests.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath =
  4. '../../../../app/src/Features/Authentication/AuthenticationController.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. const tk = require('timekeeper')
  7. const MockRequest = require('../helpers/MockRequest')
  8. const MockResponse = require('../helpers/MockResponse')
  9. const { ObjectId } = require('mongodb')
  10. const AuthenticationErrors = require('../../../../app/src/Features/Authentication/AuthenticationErrors')
  11. describe('AuthenticationController', function () {
  12. beforeEach(function () {
  13. tk.freeze(Date.now())
  14. this.UserModel = { findOne: sinon.stub() }
  15. this.httpAuthUsers = {
  16. 'valid-test-user': Math.random().toString(16).slice(2),
  17. }
  18. this.user = {
  19. _id: new ObjectId(),
  20. email: (this.email = 'USER@example.com'),
  21. first_name: 'bob',
  22. last_name: 'brown',
  23. referal_id: 1234,
  24. isAdmin: false,
  25. }
  26. this.password = 'banana'
  27. this.req = new MockRequest()
  28. this.res = new MockResponse()
  29. this.callback = sinon.stub()
  30. this.next = sinon.stub()
  31. this.req.session.analyticsId = 'abc-123'
  32. this.AuthenticationController = SandboxedModule.require(modulePath, {
  33. requires: {
  34. '../Helpers/AdminAuthorizationHelper': (this.AdminAuthorizationHelper =
  35. {
  36. hasAdminAccess: sinon.stub().returns(false),
  37. }),
  38. './AuthenticationErrors': AuthenticationErrors,
  39. '../User/UserAuditLogHandler': (this.UserAuditLogHandler = {
  40. addEntry: sinon.stub().yields(null),
  41. }),
  42. '../Helpers/AsyncFormHelper': (this.AsyncFormHelper = {
  43. redirect: sinon.stub(),
  44. }),
  45. '../../infrastructure/RequestContentTypeDetection': {
  46. acceptsJson: (this.acceptsJson = sinon.stub().returns(false)),
  47. },
  48. './AuthenticationManager': (this.AuthenticationManager = {}),
  49. '../User/UserUpdater': (this.UserUpdater = {
  50. updateUser: sinon.stub(),
  51. }),
  52. '@overleaf/metrics': (this.Metrics = { inc: sinon.stub() }),
  53. '../Security/LoginRateLimiter': (this.LoginRateLimiter = {
  54. processLoginRequest: sinon.stub(),
  55. recordSuccessfulLogin: sinon.stub(),
  56. }),
  57. '../User/UserHandler': (this.UserHandler = {
  58. setupLoginData: sinon.stub(),
  59. }),
  60. '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
  61. recordEventForUser: sinon.stub(),
  62. identifyUser: sinon.stub(),
  63. getIdsFromSession: sinon.stub().returns({ userId: this.user._id }),
  64. }),
  65. '../../infrastructure/SessionStoreManager': (this.SessionStoreManager =
  66. {}),
  67. '@overleaf/settings': (this.Settings = {
  68. siteUrl: 'http://www.foo.bar',
  69. httpAuthUsers: this.httpAuthUsers,
  70. elevateAccountSecurityAfterFailedLogin: 90 * 24 * 60 * 60 * 1000,
  71. }),
  72. passport: (this.passport = {
  73. authenticate: sinon.stub().returns(sinon.stub()),
  74. }),
  75. '../User/UserSessionsManager': (this.UserSessionsManager = {
  76. trackSession: sinon.stub(),
  77. untrackSession: sinon.stub(),
  78. revokeAllUserSessions: sinon.stub().yields(null),
  79. }),
  80. '../../infrastructure/Modules': (this.Modules = {
  81. hooks: { fire: sinon.stub().yields(null, []) },
  82. }),
  83. '../Notifications/NotificationsBuilder': (this.NotificationsBuilder = {
  84. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() }),
  85. }),
  86. '../../models/User': { User: this.UserModel },
  87. '../../../../modules/oauth2-server/app/src/Oauth2Server':
  88. (this.Oauth2Server = {
  89. Request: sinon.stub(),
  90. Response: sinon.stub(),
  91. server: {
  92. authenticate: sinon.stub(),
  93. },
  94. }),
  95. '../Helpers/UrlHelper': (this.UrlHelper = {
  96. getSafeRedirectPath: sinon.stub(),
  97. }),
  98. './SessionManager': (this.SessionManager = {
  99. isUserLoggedIn: sinon.stub().returns(true),
  100. getSessionUser: sinon.stub().returns(this.user),
  101. }),
  102. },
  103. })
  104. this.UrlHelper.getSafeRedirectPath
  105. .withArgs('https://evil.com')
  106. .returns(undefined)
  107. this.UrlHelper.getSafeRedirectPath.returnsArg(0)
  108. })
  109. afterEach(function () {
  110. tk.reset()
  111. })
  112. describe('validateAdmin', function () {
  113. beforeEach(function () {
  114. this.Settings.adminDomains = ['good.example.com']
  115. this.goodAdmin = {
  116. email: 'alice@good.example.com',
  117. isAdmin: true,
  118. }
  119. this.badAdmin = {
  120. email: 'beatrice@bad.example.com',
  121. isAdmin: true,
  122. }
  123. this.normalUser = {
  124. email: 'claire@whatever.example.com',
  125. isAdmin: false,
  126. }
  127. })
  128. it('should skip when adminDomains are not configured', function (done) {
  129. this.Settings.adminDomains = []
  130. this.SessionManager.getSessionUser = sinon.stub().returns(this.normalUser)
  131. this.AuthenticationController.validateAdmin(this.req, this.res, err => {
  132. this.SessionManager.getSessionUser.called.should.equal(false)
  133. expect(err).to.not.exist
  134. done()
  135. })
  136. })
  137. it('should skip non-admin user', function (done) {
  138. this.SessionManager.getSessionUser = sinon.stub().returns(this.normalUser)
  139. this.AuthenticationController.validateAdmin(this.req, this.res, err => {
  140. this.SessionManager.getSessionUser.called.should.equal(true)
  141. expect(err).to.not.exist
  142. done()
  143. })
  144. })
  145. it('should permit an admin with the right doman', function (done) {
  146. this.SessionManager.getSessionUser = sinon.stub().returns(this.goodAdmin)
  147. this.AuthenticationController.validateAdmin(this.req, this.res, err => {
  148. this.SessionManager.getSessionUser.called.should.equal(true)
  149. expect(err).to.not.exist
  150. done()
  151. })
  152. })
  153. it('should block an admin with a missing email', function (done) {
  154. this.SessionManager.getSessionUser = sinon
  155. .stub()
  156. .returns({ isAdmin: true })
  157. this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
  158. this.AuthenticationController.validateAdmin(this.req, this.res, err => {
  159. this.SessionManager.getSessionUser.called.should.equal(true)
  160. expect(err).to.exist
  161. done()
  162. })
  163. })
  164. it('should block an admin with a bad domain', function (done) {
  165. this.SessionManager.getSessionUser = sinon.stub().returns(this.badAdmin)
  166. this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
  167. this.AuthenticationController.validateAdmin(this.req, this.res, err => {
  168. this.SessionManager.getSessionUser.called.should.equal(true)
  169. expect(err).to.exist
  170. done()
  171. })
  172. })
  173. })
  174. describe('passportLogin', function () {
  175. beforeEach(function () {
  176. this.info = null
  177. this.req.login = sinon.stub().yields(null)
  178. this.res.json = sinon.stub()
  179. this.req.session = {
  180. passport: { user: this.user },
  181. postLoginRedirect: '/path/to/redir/to',
  182. }
  183. this.req.session.destroy = sinon.stub().yields(null)
  184. this.req.session.save = sinon.stub().yields(null)
  185. this.req.sessionStore = { generate: sinon.stub() }
  186. this.AuthenticationController.finishLogin = sinon.stub()
  187. this.passport.authenticate.yields(null, this.user, this.info)
  188. this.err = new Error('woops')
  189. })
  190. it('should call passport.authenticate', function () {
  191. this.AuthenticationController.passportLogin(this.req, this.res, this.next)
  192. this.passport.authenticate.callCount.should.equal(1)
  193. })
  194. describe('when authenticate produces an error', function () {
  195. beforeEach(function () {
  196. this.passport.authenticate.yields(this.err)
  197. })
  198. it('should return next with an error', function () {
  199. this.AuthenticationController.passportLogin(
  200. this.req,
  201. this.res,
  202. this.next
  203. )
  204. this.next.calledWith(this.err).should.equal(true)
  205. })
  206. })
  207. describe('when authenticate produces a user', function () {
  208. beforeEach(function () {
  209. this.req.session.postLoginRedirect = 'some_redirect'
  210. this.passport.authenticate.yields(null, this.user, this.info)
  211. })
  212. afterEach(function () {
  213. delete this.req.session.postLoginRedirect
  214. })
  215. it('should call finishLogin', function () {
  216. this.AuthenticationController.passportLogin(
  217. this.req,
  218. this.res,
  219. this.next
  220. )
  221. this.AuthenticationController.finishLogin.callCount.should.equal(1)
  222. this.AuthenticationController.finishLogin
  223. .calledWith(this.user)
  224. .should.equal(true)
  225. })
  226. })
  227. describe('when authenticate does not produce a user', function () {
  228. beforeEach(function () {
  229. this.info = { text: 'a', type: 'b' }
  230. this.passport.authenticate.yields(null, false, this.info)
  231. })
  232. it('should not call finishLogin', function () {
  233. this.AuthenticationController.passportLogin(
  234. this.req,
  235. this.res,
  236. this.next
  237. )
  238. this.AuthenticationController.finishLogin.callCount.should.equal(0)
  239. })
  240. it('should not send a json response with redirect', function () {
  241. this.AuthenticationController.passportLogin(
  242. this.req,
  243. this.res,
  244. this.next
  245. )
  246. this.res.json.callCount.should.equal(1)
  247. this.res.json.should.have.been.calledWith({ message: this.info })
  248. expect(this.res.json.lastCall.args[0].redir != null).to.equal(false)
  249. })
  250. })
  251. })
  252. describe('doPassportLogin', function () {
  253. beforeEach(function () {
  254. this.AuthenticationController._recordFailedLogin = sinon.stub()
  255. this.AuthenticationController._recordSuccessfulLogin = sinon.stub()
  256. this.Modules.hooks.fire = sinon.stub().yields(null, [])
  257. this.req.body = {
  258. email: this.email,
  259. password: this.password,
  260. session: {
  261. postLoginRedirect: '/path/to/redir/to',
  262. },
  263. }
  264. this.req.__authAuditInfo = { captcha: 'disabled' }
  265. this.cb = sinon.stub()
  266. })
  267. describe('when the preDoPassportLogin hooks produce an info object', function () {
  268. beforeEach(function () {
  269. this.Modules.hooks.fire = sinon
  270. .stub()
  271. .yields(null, [null, { redir: '/somewhere' }, null])
  272. })
  273. it('should stop early and call done with this info object', function (done) {
  274. this.AuthenticationController.doPassportLogin(
  275. this.req,
  276. this.req.body.email,
  277. this.req.body.password,
  278. this.cb
  279. )
  280. this.cb.callCount.should.equal(1)
  281. this.cb
  282. .calledWith(null, false, { redir: '/somewhere' })
  283. .should.equal(true)
  284. this.LoginRateLimiter.processLoginRequest.callCount.should.equal(0)
  285. done()
  286. })
  287. })
  288. describe('when the users rate limit', function () {
  289. beforeEach(function () {
  290. this.LoginRateLimiter.processLoginRequest.yields(null, false)
  291. })
  292. it('should block the request if the limit has been exceeded', function (done) {
  293. this.AuthenticationController.doPassportLogin(
  294. this.req,
  295. this.req.body.email,
  296. this.req.body.password,
  297. this.cb
  298. )
  299. this.cb.callCount.should.equal(1)
  300. this.cb.calledWith(null, null).should.equal(true)
  301. done()
  302. })
  303. })
  304. describe('when the user is authenticated', function () {
  305. beforeEach(function () {
  306. this.cb = sinon.stub()
  307. this.LoginRateLimiter.processLoginRequest.yields(null, true)
  308. this.AuthenticationManager.authenticate = sinon
  309. .stub()
  310. .yields(null, this.user)
  311. this.req.sessionID = Math.random()
  312. })
  313. describe('happy path', function () {
  314. beforeEach(function () {
  315. this.AuthenticationController.doPassportLogin(
  316. this.req,
  317. this.req.body.email,
  318. this.req.body.password,
  319. this.cb
  320. )
  321. })
  322. it('should attempt to authorise the user', function () {
  323. this.AuthenticationManager.authenticate
  324. .calledWith({ email: this.email.toLowerCase() }, this.password)
  325. .should.equal(true)
  326. })
  327. it("should establish the user's session", function () {
  328. this.cb.calledWith(null, this.user).should.equal(true)
  329. })
  330. })
  331. describe('when authenticate flags a parallel login', function () {
  332. beforeEach(function () {
  333. this.AuthenticationManager.authenticate = sinon
  334. .stub()
  335. .yields(new AuthenticationErrors.ParallelLoginError())
  336. this.AuthenticationController.doPassportLogin(
  337. this.req,
  338. this.req.body.email,
  339. this.req.body.password,
  340. this.cb
  341. )
  342. })
  343. it('should send a 429', function () {
  344. this.cb.should.have.been.calledWith(null, false, { status: 429 })
  345. })
  346. })
  347. describe('with a user having a recent failed login ', function () {
  348. beforeEach(function () {
  349. this.user.lastFailedLogin = new Date()
  350. })
  351. describe('with captcha disabled', function () {
  352. beforeEach(function () {
  353. this.req.__authAuditInfo.captcha = 'disabled'
  354. this.AuthenticationController.doPassportLogin(
  355. this.req,
  356. this.req.body.email,
  357. this.req.body.password,
  358. this.cb
  359. )
  360. })
  361. it('should let the user log in', function () {
  362. this.cb.should.have.been.calledWith(null, this.user)
  363. })
  364. })
  365. describe('with a solved captcha', function () {
  366. beforeEach(function () {
  367. this.req.__authAuditInfo.captcha = 'solved'
  368. this.AuthenticationController.doPassportLogin(
  369. this.req,
  370. this.req.body.email,
  371. this.req.body.password,
  372. this.cb
  373. )
  374. })
  375. it('should let the user log in', function () {
  376. this.cb.should.have.been.calledWith(null, this.user)
  377. })
  378. })
  379. describe('with a skipped captcha', function () {
  380. beforeEach(function () {
  381. this.req.__authAuditInfo.captcha = 'skipped'
  382. this.AuthenticationController.doPassportLogin(
  383. this.req,
  384. this.req.body.email,
  385. this.req.body.password,
  386. this.cb
  387. )
  388. })
  389. it('should request a captcha', function () {
  390. this.cb.should.have.been.calledWith(null, false, {
  391. text: 'cannot_verify_user_not_robot',
  392. type: 'error',
  393. errorReason: 'cannot_verify_user_not_robot',
  394. status: 400,
  395. })
  396. })
  397. })
  398. })
  399. })
  400. describe('when the user is not authenticated', function () {
  401. beforeEach(function () {
  402. this.LoginRateLimiter.processLoginRequest.yields(null, true)
  403. this.AuthenticationManager.authenticate = sinon
  404. .stub()
  405. .yields(null, null)
  406. this.cb = sinon.stub()
  407. this.AuthenticationController.doPassportLogin(
  408. this.req,
  409. this.req.body.email,
  410. this.req.body.password,
  411. this.cb
  412. )
  413. })
  414. it('should not establish the login', function () {
  415. this.cb.callCount.should.equal(1)
  416. this.cb.calledWith(null, false)
  417. expect(this.cb.lastCall.args[2]).to.deep.equal({
  418. type: 'error',
  419. key: 'invalid-password-retry-or-reset',
  420. status: 401,
  421. })
  422. })
  423. it('should not setup the user data in the background', function () {
  424. this.UserHandler.setupLoginData.called.should.equal(false)
  425. })
  426. it('should record a failed login', function () {
  427. this.AuthenticationController._recordFailedLogin.called.should.equal(
  428. true
  429. )
  430. })
  431. it('should log the failed login', function () {
  432. this.logger.debug
  433. .calledWith({ email: this.email.toLowerCase() }, 'failed log in')
  434. .should.equal(true)
  435. })
  436. })
  437. })
  438. describe('requireLogin', function () {
  439. beforeEach(function () {
  440. this.user = {
  441. _id: 'user-id-123',
  442. email: 'user@overleaf.com',
  443. }
  444. this.middleware = this.AuthenticationController.requireLogin()
  445. })
  446. describe('when the user is logged in', function () {
  447. beforeEach(function () {
  448. this.req.session = {
  449. user: (this.user = {
  450. _id: 'user-id-123',
  451. email: 'user@overleaf.com',
  452. }),
  453. }
  454. this.middleware(this.req, this.res, this.next)
  455. })
  456. it('should call the next method in the chain', function () {
  457. this.next.called.should.equal(true)
  458. })
  459. })
  460. describe('when the user is not logged in', function () {
  461. beforeEach(function () {
  462. this.req.session = {}
  463. this.AuthenticationController._redirectToLoginOrRegisterPage =
  464. sinon.stub()
  465. this.req.query = {}
  466. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  467. this.middleware(this.req, this.res, this.next)
  468. })
  469. it('should redirect to the register or login page', function () {
  470. this.AuthenticationController._redirectToLoginOrRegisterPage
  471. .calledWith(this.req, this.res)
  472. .should.equal(true)
  473. })
  474. })
  475. })
  476. describe('validateUserSession', function () {
  477. beforeEach(function () {
  478. this.user = {
  479. _id: 'user-id-123',
  480. email: 'user@overleaf.com',
  481. }
  482. this.middleware = this.AuthenticationController.validateUserSession()
  483. })
  484. describe('when the user has a session token', function () {
  485. beforeEach(function () {
  486. this.req.user = this.user
  487. this.SessionStoreManager.hasValidationToken = sinon.stub().returns(true)
  488. this.middleware(this.req, this.res, this.next)
  489. })
  490. it('should call the next method in the chain', function () {
  491. this.next.called.should.equal(true)
  492. })
  493. })
  494. describe('when the user does not have a session token', function () {
  495. beforeEach(function () {
  496. this.req.session = {
  497. user: this.user,
  498. regenerate: sinon.stub().yields(),
  499. }
  500. this.req.user = this.user
  501. this.AuthenticationController._redirectToLoginOrRegisterPage =
  502. sinon.stub()
  503. this.req.query = {}
  504. this.SessionStoreManager.hasValidationToken = sinon
  505. .stub()
  506. .returns(false)
  507. this.middleware(this.req, this.res, this.next)
  508. })
  509. it('should destroy the current session', function () {
  510. this.req.session.regenerate.called.should.equal(true)
  511. })
  512. it('should redirect to the register or login page', function () {
  513. this.AuthenticationController._redirectToLoginOrRegisterPage
  514. .calledWith(this.req, this.res)
  515. .should.equal(true)
  516. })
  517. })
  518. })
  519. describe('requireOauth', function () {
  520. beforeEach(function () {
  521. this.res.json = sinon.stub()
  522. this.res.status = sinon.stub().returns(this.res)
  523. this.res.sendStatus = sinon.stub()
  524. this.middleware = this.AuthenticationController.requireOauth('scope')
  525. })
  526. describe('when Oauth2Server authenticates', function () {
  527. beforeEach(function () {
  528. this.token = {
  529. accessToken: 'token',
  530. user: 'user',
  531. }
  532. this.Oauth2Server.server.authenticate.yields(null, this.token)
  533. this.middleware(this.req, this.res, this.next)
  534. })
  535. it('should set oauth_token on request', function () {
  536. this.req.oauth_token.should.equal(this.token)
  537. })
  538. it('should set oauth on request', function () {
  539. this.req.oauth.access_token.should.equal(this.token.accessToken)
  540. })
  541. it('should set oauth_user on request', function () {
  542. this.req.oauth_user.should.equal('user')
  543. })
  544. it('should call next', function () {
  545. this.next.should.have.been.calledOnce
  546. })
  547. })
  548. describe('when Oauth2Server returns 401 error', function () {
  549. beforeEach(function () {
  550. this.Oauth2Server.server.authenticate.yields({ code: 401 })
  551. this.middleware(this.req, this.res, this.next)
  552. })
  553. it('should return 401 error', function () {
  554. this.res.status.should.have.been.calledWith(401)
  555. })
  556. it('should not call next', function () {
  557. this.next.should.have.not.been.calledOnce
  558. })
  559. })
  560. })
  561. describe('requireGlobalLogin', function () {
  562. beforeEach(function () {
  563. this.req.headers = {}
  564. this.middleware = sinon.stub()
  565. this.AuthenticationController.requirePrivateApiAuth = sinon
  566. .stub()
  567. .returns(this.middleware)
  568. this.setRedirect = sinon.spy(
  569. this.AuthenticationController,
  570. 'setRedirectInSession'
  571. )
  572. })
  573. afterEach(function () {
  574. this.setRedirect.restore()
  575. })
  576. describe('with white listed url', function () {
  577. beforeEach(function () {
  578. this.AuthenticationController.addEndpointToLoginWhitelist('/login')
  579. this.req._parsedUrl.pathname = '/login'
  580. this.AuthenticationController.requireGlobalLogin(
  581. this.req,
  582. this.res,
  583. this.next
  584. )
  585. })
  586. it('should call next() directly', function () {
  587. this.next.called.should.equal(true)
  588. })
  589. })
  590. describe('with white listed url and a query string', function () {
  591. beforeEach(function () {
  592. this.AuthenticationController.addEndpointToLoginWhitelist('/login')
  593. this.req._parsedUrl.pathname = '/login'
  594. this.req.url = '/login?query=something'
  595. this.AuthenticationController.requireGlobalLogin(
  596. this.req,
  597. this.res,
  598. this.next
  599. )
  600. })
  601. it('should call next() directly', function () {
  602. this.next.called.should.equal(true)
  603. })
  604. })
  605. describe('with http auth', function () {
  606. beforeEach(function () {
  607. this.req.headers.authorization = 'Mock Basic Auth'
  608. this.AuthenticationController.requireGlobalLogin(
  609. this.req,
  610. this.res,
  611. this.next
  612. )
  613. })
  614. it('should pass the request onto requirePrivateApiAuth middleware', function () {
  615. this.middleware
  616. .calledWith(this.req, this.res, this.next)
  617. .should.equal(true)
  618. })
  619. })
  620. describe('with a user session', function () {
  621. beforeEach(function () {
  622. this.req.session = { user: { mock: 'user', _id: 'some_id' } }
  623. this.AuthenticationController.requireGlobalLogin(
  624. this.req,
  625. this.res,
  626. this.next
  627. )
  628. })
  629. it('should call next() directly', function () {
  630. this.next.called.should.equal(true)
  631. })
  632. })
  633. describe('with no login credentials', function () {
  634. beforeEach(function () {
  635. this.req.session = {}
  636. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  637. this.AuthenticationController.requireGlobalLogin(
  638. this.req,
  639. this.res,
  640. this.next
  641. )
  642. })
  643. it('should have called setRedirectInSession', function () {
  644. this.setRedirect.callCount.should.equal(1)
  645. })
  646. it('should redirect to the /login page', function () {
  647. this.res.redirectedTo.should.equal('/login')
  648. })
  649. })
  650. })
  651. describe('requireBasicAuth', function () {
  652. beforeEach(function () {
  653. this.basicAuthUsers = {
  654. 'basic-auth-user': 'basic-auth-password',
  655. }
  656. this.middleware = this.AuthenticationController.requireBasicAuth(
  657. this.basicAuthUsers
  658. )
  659. })
  660. describe('with http auth', function () {
  661. it('should error with incorrect user', function (done) {
  662. this.req.headers = {
  663. authorization: `Basic ${Buffer.from('user:nope').toString('base64')}`,
  664. }
  665. this.req.sendStatus = status => {
  666. expect(status).to.equal(401)
  667. done()
  668. }
  669. this.middleware(this.req, this.req)
  670. })
  671. it('should error with incorrect password', function (done) {
  672. this.req.headers = {
  673. authorization: `Basic ${Buffer.from('basic-auth-user:nope').toString(
  674. 'base64'
  675. )}`,
  676. }
  677. this.req.sendStatus = status => {
  678. expect(status).to.equal(401)
  679. done()
  680. }
  681. this.middleware(this.req, this.req)
  682. })
  683. it('should fail with empty pass', function (done) {
  684. this.req.headers = {
  685. authorization: `Basic ${Buffer.from(`basic-auth-user:`).toString(
  686. 'base64'
  687. )}`,
  688. }
  689. this.req.sendStatus = status => {
  690. expect(status).to.equal(401)
  691. done()
  692. }
  693. this.middleware(this.req, this.req)
  694. })
  695. it('should fail with empty user and password of "undefined"', function (done) {
  696. this.req.headers = {
  697. authorization: `Basic ${Buffer.from(`:undefined`).toString(
  698. 'base64'
  699. )}`,
  700. }
  701. this.req.sendStatus = status => {
  702. expect(status).to.equal(401)
  703. done()
  704. }
  705. this.middleware(this.req, this.req)
  706. })
  707. it('should fail with empty user and empty password', function (done) {
  708. this.req.headers = {
  709. authorization: `Basic ${Buffer.from(`:`).toString('base64')}`,
  710. }
  711. this.req.sendStatus = status => {
  712. expect(status).to.equal(401)
  713. done()
  714. }
  715. this.middleware(this.req, this.req)
  716. })
  717. it('should fail with a user that is not a valid property', function (done) {
  718. this.req.headers = {
  719. authorization: `Basic ${Buffer.from(
  720. `constructor:[Function ]`
  721. ).toString('base64')}`,
  722. }
  723. this.req.sendStatus = status => {
  724. expect(status).to.equal(401)
  725. done()
  726. }
  727. this.middleware(this.req, this.req)
  728. })
  729. it('should succeed with correct user/pass', function (done) {
  730. this.req.headers = {
  731. authorization: `Basic ${Buffer.from(
  732. `basic-auth-user:${this.basicAuthUsers['basic-auth-user']}`
  733. ).toString('base64')}`,
  734. }
  735. this.middleware(this.req, this.res, done)
  736. })
  737. })
  738. })
  739. describe('requirePrivateApiAuth', function () {
  740. beforeEach(function () {
  741. this.AuthenticationController.requireBasicAuth = sinon.stub()
  742. })
  743. it('should call requireBasicAuth with the private API user details', function () {
  744. this.AuthenticationController.requirePrivateApiAuth()
  745. this.AuthenticationController.requireBasicAuth
  746. .calledWith(this.httpAuthUsers)
  747. .should.equal(true)
  748. })
  749. })
  750. describe('_redirectToLoginOrRegisterPage', function () {
  751. beforeEach(function () {
  752. this.middleware = this.AuthenticationController.requireLogin(
  753. (this.options = { load_from_db: false })
  754. )
  755. this.req.session = {}
  756. this.AuthenticationController._redirectToRegisterPage = sinon.stub()
  757. this.AuthenticationController._redirectToLoginPage = sinon.stub()
  758. this.req.query = {}
  759. })
  760. describe('they have come directly to the url', function () {
  761. beforeEach(function () {
  762. this.req.query = {}
  763. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  764. this.middleware(this.req, this.res, this.next)
  765. })
  766. it('should redirect to the login page', function () {
  767. this.AuthenticationController._redirectToRegisterPage
  768. .calledWith(this.req, this.res)
  769. .should.equal(false)
  770. this.AuthenticationController._redirectToLoginPage
  771. .calledWith(this.req, this.res)
  772. .should.equal(true)
  773. })
  774. })
  775. describe('they have come via a templates link', function () {
  776. beforeEach(function () {
  777. this.req.query.zipUrl = 'something'
  778. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  779. this.middleware(this.req, this.res, this.next)
  780. })
  781. it('should redirect to the register page', function () {
  782. this.AuthenticationController._redirectToRegisterPage
  783. .calledWith(this.req, this.res)
  784. .should.equal(true)
  785. this.AuthenticationController._redirectToLoginPage
  786. .calledWith(this.req, this.res)
  787. .should.equal(false)
  788. })
  789. })
  790. describe('they have been invited to a project', function () {
  791. beforeEach(function () {
  792. this.req.query.project_name = 'something'
  793. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  794. this.middleware(this.req, this.res, this.next)
  795. })
  796. it('should redirect to the register page', function () {
  797. this.AuthenticationController._redirectToRegisterPage
  798. .calledWith(this.req, this.res)
  799. .should.equal(true)
  800. this.AuthenticationController._redirectToLoginPage
  801. .calledWith(this.req, this.res)
  802. .should.equal(false)
  803. })
  804. })
  805. })
  806. describe('_redirectToRegisterPage', function () {
  807. beforeEach(function () {
  808. this.req.path = '/target/url'
  809. this.req.query = { extra_query: 'foo' }
  810. this.AuthenticationController._redirectToRegisterPage(this.req, this.res)
  811. })
  812. it('should redirect to the register page with a query string attached', function () {
  813. this.req.session.postLoginRedirect.should.equal(
  814. '/target/url?extra_query=foo'
  815. )
  816. this.res.redirectedTo.should.equal('/register?extra_query=foo')
  817. })
  818. it('should log out a message', function () {
  819. this.logger.debug
  820. .calledWith(
  821. { url: this.url },
  822. 'user not logged in so redirecting to register page'
  823. )
  824. .should.equal(true)
  825. })
  826. })
  827. describe('_redirectToLoginPage', function () {
  828. beforeEach(function () {
  829. this.req.path = '/target/url'
  830. this.req.query = { extra_query: 'foo' }
  831. this.AuthenticationController._redirectToLoginPage(this.req, this.res)
  832. })
  833. it('should redirect to the register page with a query string attached', function () {
  834. this.req.session.postLoginRedirect.should.equal(
  835. '/target/url?extra_query=foo'
  836. )
  837. this.res.redirectedTo.should.equal('/login?extra_query=foo')
  838. })
  839. })
  840. describe('_recordSuccessfulLogin', function () {
  841. beforeEach(function () {
  842. this.UserUpdater.updateUser = sinon.stub().yields()
  843. this.AuthenticationController._recordSuccessfulLogin(
  844. this.user._id,
  845. this.callback
  846. )
  847. })
  848. it('should increment the user.login.success metric', function () {
  849. this.Metrics.inc.calledWith('user.login.success').should.equal(true)
  850. })
  851. it("should update the user's login count and last logged in date", function () {
  852. this.UserUpdater.updateUser.args[0][1].$set.lastLoggedIn.should.not.equal(
  853. undefined
  854. )
  855. this.UserUpdater.updateUser.args[0][1].$inc.loginCount.should.equal(1)
  856. })
  857. it('should call the callback', function () {
  858. this.callback.called.should.equal(true)
  859. })
  860. })
  861. describe('_recordFailedLogin', function () {
  862. beforeEach(function () {
  863. this.AuthenticationController._recordFailedLogin(this.callback)
  864. })
  865. it('should increment the user.login.failed metric', function () {
  866. this.Metrics.inc.calledWith('user.login.failed').should.equal(true)
  867. })
  868. it('should call the callback', function () {
  869. this.callback.called.should.equal(true)
  870. })
  871. })
  872. describe('setRedirectInSession', function () {
  873. beforeEach(function () {
  874. this.req = { session: {} }
  875. this.req.path = '/somewhere'
  876. this.req.query = { one: '1' }
  877. })
  878. it('should set redirect property on session', function () {
  879. this.AuthenticationController.setRedirectInSession(this.req)
  880. expect(this.req.session.postLoginRedirect).to.equal('/somewhere?one=1')
  881. })
  882. it('should set the supplied value', function () {
  883. this.AuthenticationController.setRedirectInSession(
  884. this.req,
  885. '/somewhere/specific'
  886. )
  887. expect(this.req.session.postLoginRedirect).to.equal('/somewhere/specific')
  888. })
  889. it('should not allow open redirects', function () {
  890. this.AuthenticationController.setRedirectInSession(
  891. this.req,
  892. 'https://evil.com'
  893. )
  894. expect(this.req.session.postLoginRedirect).to.be.undefined
  895. })
  896. describe('with a png', function () {
  897. beforeEach(function () {
  898. this.req = { session: {} }
  899. })
  900. it('should not set the redirect', function () {
  901. this.AuthenticationController.setRedirectInSession(
  902. this.req,
  903. '/something.png'
  904. )
  905. expect(this.req.session.postLoginRedirect).to.equal(undefined)
  906. })
  907. })
  908. describe('with a js path', function () {
  909. beforeEach(function () {
  910. this.req = { session: {} }
  911. })
  912. it('should not set the redirect', function () {
  913. this.AuthenticationController.setRedirectInSession(
  914. this.req,
  915. '/js/something.js'
  916. )
  917. expect(this.req.session.postLoginRedirect).to.equal(undefined)
  918. })
  919. })
  920. })
  921. describe('_getRedirectFromSession', function () {
  922. it('should get redirect property from session', function () {
  923. this.req = { session: { postLoginRedirect: '/a?b=c' } }
  924. expect(
  925. this.AuthenticationController._getRedirectFromSession(this.req)
  926. ).to.equal('/a?b=c')
  927. })
  928. it('should not allow open redirects', function () {
  929. this.req = { session: { postLoginRedirect: 'https://evil.com' } }
  930. expect(this.AuthenticationController._getRedirectFromSession(this.req)).to
  931. .be.null
  932. })
  933. it('handle null values', function () {
  934. this.req = { session: {} }
  935. expect(this.AuthenticationController._getRedirectFromSession(this.req)).to
  936. .be.null
  937. })
  938. })
  939. describe('_clearRedirectFromSession', function () {
  940. beforeEach(function () {
  941. this.req = { session: { postLoginRedirect: '/a?b=c' } }
  942. })
  943. it('should remove the redirect property from session', function () {
  944. this.AuthenticationController._clearRedirectFromSession(this.req)
  945. expect(this.req.session.postLoginRedirect).to.equal(undefined)
  946. })
  947. })
  948. describe('finishLogin', function () {
  949. // - get redirect
  950. // - async handlers
  951. // - afterLoginSessionSetup
  952. // - clear redirect
  953. // - issue redir, two ways
  954. beforeEach(function () {
  955. this.AuthenticationController._getRedirectFromSession = sinon
  956. .stub()
  957. .returns('/some/page')
  958. this.req.sessionID = 'thisisacryptographicallysecurerandomid'
  959. this.req.session = {
  960. passport: { user: { _id: 'one' } },
  961. }
  962. this.req.session.destroy = sinon.stub().yields(null)
  963. this.req.session.save = sinon.stub().yields(null)
  964. this.req.sessionStore = { generate: sinon.stub() }
  965. this.req.login = sinon.stub().yields(null)
  966. this.AuthenticationController._clearRedirectFromSession = sinon.stub()
  967. this.AuthenticationController._redirectToReconfirmPage = sinon.stub()
  968. this.UserSessionsManager.trackSession = sinon.stub()
  969. this.UserHandler.setupLoginData = sinon.stub()
  970. this.LoginRateLimiter.recordSuccessfulLogin = sinon.stub()
  971. this.AuthenticationController._recordSuccessfulLogin = sinon.stub()
  972. this.AnalyticsManager.recordEvent = sinon.stub()
  973. this.AnalyticsManager.identifyUser = sinon.stub()
  974. this.acceptsJson.returns(true)
  975. this.res.json = sinon.stub()
  976. this.res.redirect = sinon.stub()
  977. })
  978. it('should extract the redirect from the session', function () {
  979. this.AuthenticationController.finishLogin(
  980. this.user,
  981. this.req,
  982. this.res,
  983. this.next
  984. )
  985. expect(
  986. this.AuthenticationController._getRedirectFromSession.callCount
  987. ).to.equal(1)
  988. expect(
  989. this.AuthenticationController._getRedirectFromSession.calledWith(
  990. this.req
  991. )
  992. ).to.equal(true)
  993. })
  994. it('should clear redirect from session', function () {
  995. this.AuthenticationController.finishLogin(
  996. this.user,
  997. this.req,
  998. this.res,
  999. this.next
  1000. )
  1001. expect(
  1002. this.AuthenticationController._clearRedirectFromSession.callCount
  1003. ).to.equal(1)
  1004. expect(
  1005. this.AuthenticationController._clearRedirectFromSession.calledWith(
  1006. this.req
  1007. )
  1008. ).to.equal(true)
  1009. })
  1010. it('should issue a json response with a redirect', function () {
  1011. this.AuthenticationController.finishLogin(
  1012. this.user,
  1013. this.req,
  1014. this.res,
  1015. this.next
  1016. )
  1017. expect(
  1018. this.AsyncFormHelper.redirect.calledWith(
  1019. this.req,
  1020. this.res,
  1021. '/some/page'
  1022. )
  1023. ).to.equal(true)
  1024. })
  1025. describe('with a non-json request', function () {
  1026. beforeEach(function () {
  1027. this.acceptsJson.returns(false)
  1028. this.res.json = sinon.stub()
  1029. this.res.redirect = sinon.stub()
  1030. })
  1031. it('should issue a plain redirect', function () {
  1032. this.AuthenticationController.finishLogin(
  1033. this.user,
  1034. this.req,
  1035. this.res,
  1036. this.next
  1037. )
  1038. expect(
  1039. this.AsyncFormHelper.redirect.calledWith(
  1040. this.req,
  1041. this.res,
  1042. '/some/page'
  1043. )
  1044. ).to.equal(true)
  1045. })
  1046. })
  1047. describe('when user is flagged to reconfirm', function () {
  1048. beforeEach(function () {
  1049. this.req.session = {}
  1050. this.user.must_reconfirm = true
  1051. })
  1052. it('should redirect to reconfirm page', function () {
  1053. this.AuthenticationController.finishLogin(
  1054. this.user,
  1055. this.req,
  1056. this.res,
  1057. this.next
  1058. )
  1059. expect(
  1060. this.AuthenticationController._redirectToReconfirmPage.calledWith(
  1061. this.req
  1062. )
  1063. ).to.equal(true)
  1064. })
  1065. })
  1066. describe('preFinishLogin hook', function () {
  1067. it('call hook and proceed', function () {
  1068. this.Modules.hooks.fire = sinon.stub().yields(null, [])
  1069. this.AuthenticationController.finishLogin(
  1070. this.user,
  1071. this.req,
  1072. this.res,
  1073. this.next
  1074. )
  1075. sinon.assert.calledWith(
  1076. this.Modules.hooks.fire,
  1077. 'preFinishLogin',
  1078. this.req,
  1079. this.res,
  1080. this.user
  1081. )
  1082. expect(this.AsyncFormHelper.redirect.called).to.equal(true)
  1083. })
  1084. it('stop if hook has redirected', function (done) {
  1085. this.Modules.hooks.fire = sinon
  1086. .stub()
  1087. .yields(null, [{ doNotFinish: true }])
  1088. this.AuthenticationController.finishLogin(
  1089. this.user,
  1090. this.req,
  1091. this.res,
  1092. this.next
  1093. )
  1094. expect(this.next.callCount).to.equal(0)
  1095. expect(this.res.json.callCount).to.equal(0)
  1096. done()
  1097. })
  1098. it('call next with hook errors', function (done) {
  1099. this.Modules.hooks.fire = sinon.stub().yields(new Error())
  1100. this.AuthenticationController.finishLogin(
  1101. this.user,
  1102. this.req,
  1103. this.res,
  1104. error => {
  1105. expect(error).to.exist
  1106. expect(this.res.json.callCount).to.equal(0)
  1107. done()
  1108. }
  1109. )
  1110. })
  1111. })
  1112. describe('UserAuditLog', function () {
  1113. it('should add an audit log entry', function () {
  1114. this.AuthenticationController.finishLogin(
  1115. this.user,
  1116. this.req,
  1117. this.res,
  1118. this.next
  1119. )
  1120. expect(this.UserAuditLogHandler.addEntry).to.have.been.calledWith(
  1121. this.user._id,
  1122. 'login',
  1123. this.user._id,
  1124. '42.42.42.42'
  1125. )
  1126. })
  1127. it('should add an audit log entry before logging the user in', function () {
  1128. this.AuthenticationController.finishLogin(
  1129. this.user,
  1130. this.req,
  1131. this.res,
  1132. this.next
  1133. )
  1134. expect(this.UserAuditLogHandler.addEntry).to.have.been.calledBefore(
  1135. this.req.login
  1136. )
  1137. })
  1138. it('should not log the user in without an audit log entry', function () {
  1139. const theError = new Error()
  1140. this.UserAuditLogHandler.addEntry.yields(theError)
  1141. this.AuthenticationController.finishLogin(
  1142. this.user,
  1143. this.req,
  1144. this.res,
  1145. this.next
  1146. )
  1147. expect(this.next).to.have.been.calledWith(theError)
  1148. expect(this.req.login).to.not.have.been.called
  1149. })
  1150. it('should pass along auditInfo when present', function () {
  1151. this.AuthenticationController.setAuditInfo(this.req, {
  1152. method: 'Login',
  1153. })
  1154. this.AuthenticationController.finishLogin(
  1155. this.user,
  1156. this.req,
  1157. this.res,
  1158. this.next
  1159. )
  1160. expect(this.UserAuditLogHandler.addEntry).to.have.been.calledWith(
  1161. this.user._id,
  1162. 'login',
  1163. this.user._id,
  1164. '42.42.42.42',
  1165. { method: 'Login' }
  1166. )
  1167. })
  1168. })
  1169. describe('_afterLoginSessionSetup', function () {
  1170. beforeEach(function () {})
  1171. it('should call req.login', function () {
  1172. this.AuthenticationController.finishLogin(
  1173. this.user,
  1174. this.req,
  1175. this.res,
  1176. this.next
  1177. )
  1178. this.req.login.callCount.should.equal(1)
  1179. })
  1180. it('should erase the CSRF secret', function () {
  1181. this.AuthenticationController.finishLogin(
  1182. this.user,
  1183. this.req,
  1184. this.res,
  1185. this.next
  1186. )
  1187. expect(this.req.session.csrfSecret).to.not.exist
  1188. })
  1189. it('should call req.session.save', function () {
  1190. this.AuthenticationController.finishLogin(
  1191. this.user,
  1192. this.req,
  1193. this.res,
  1194. this.next
  1195. )
  1196. this.req.session.save.callCount.should.equal(1)
  1197. })
  1198. it('should call UserSessionsManager.trackSession', function () {
  1199. this.AuthenticationController.finishLogin(
  1200. this.user,
  1201. this.req,
  1202. this.res,
  1203. this.next
  1204. )
  1205. this.UserSessionsManager.trackSession.callCount.should.equal(1)
  1206. })
  1207. describe('when req.session.save produces an error', function () {
  1208. beforeEach(function () {
  1209. this.req.session.save = sinon.stub().yields(new Error('woops'))
  1210. })
  1211. it('should produce an error', function (done) {
  1212. this.AuthenticationController.finishLogin(
  1213. this.user,
  1214. this.req,
  1215. this.res,
  1216. err => {
  1217. expect(err).to.not.be.oneOf([null, undefined])
  1218. expect(err).to.be.instanceof(Error)
  1219. done()
  1220. }
  1221. )
  1222. })
  1223. it('should not call UserSessionsManager.trackSession', function (done) {
  1224. this.AuthenticationController.finishLogin(
  1225. this.user,
  1226. this.req,
  1227. this.res,
  1228. err => {
  1229. expect(err).to.exist
  1230. this.UserSessionsManager.trackSession.callCount.should.equal(0)
  1231. done()
  1232. }
  1233. )
  1234. })
  1235. })
  1236. })
  1237. describe('_loginAsyncHandlers', function () {
  1238. beforeEach(function () {
  1239. this.AuthenticationController.finishLogin(
  1240. this.user,
  1241. this.req,
  1242. this.res,
  1243. this.next
  1244. )
  1245. })
  1246. it('should call identifyUser', function () {
  1247. sinon.assert.calledWith(
  1248. this.AnalyticsManager.identifyUser,
  1249. this.user._id,
  1250. this.req.session.analyticsId
  1251. )
  1252. })
  1253. it('should setup the user data in the background', function () {
  1254. this.UserHandler.setupLoginData.calledWith(this.user).should.equal(true)
  1255. })
  1256. it('should set res.session.justLoggedIn', function () {
  1257. this.req.session.justLoggedIn.should.equal(true)
  1258. })
  1259. it('should record the successful login', function () {
  1260. this.AuthenticationController._recordSuccessfulLogin
  1261. .calledWith(this.user._id)
  1262. .should.equal(true)
  1263. })
  1264. it('should tell the rate limiter that there was a success for that email', function () {
  1265. this.LoginRateLimiter.recordSuccessfulLogin
  1266. .calledWith(this.user.email)
  1267. .should.equal(true)
  1268. })
  1269. it('should log the successful login', function () {
  1270. this.logger.debug
  1271. .calledWith(
  1272. { email: this.user.email, userId: this.user._id.toString() },
  1273. 'successful log in'
  1274. )
  1275. .should.equal(true)
  1276. })
  1277. it('should track the login event', function () {
  1278. sinon.assert.calledWith(
  1279. this.AnalyticsManager.recordEventForUser,
  1280. this.user._id,
  1281. 'user-logged-in'
  1282. )
  1283. })
  1284. })
  1285. })
  1286. })