AuthenticationControllerTests.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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: 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().callsArgWith(1, 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().callsArgWith(1, 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().callsArgWith(0, null)
  184. this.req.session.save = sinon.stub().callsArgWith(0, null)
  185. this.req.sessionStore = { generate: sinon.stub() }
  186. this.AuthenticationController.finishLogin = sinon.stub()
  187. this.passport.authenticate.callsArgWith(1, 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.callsArgWith(1, 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.callsArgWith(1, 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.callsArgWith(1, 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().callsArgWith(3, null, [])
  257. // @AuthenticationController.establishUserSession = sinon.stub().callsArg(2)
  258. this.req.body = {
  259. email: this.email,
  260. password: this.password,
  261. session: {
  262. postLoginRedirect: '/path/to/redir/to',
  263. },
  264. }
  265. this.req.__authAuditInfo = { captcha: 'disabled' }
  266. this.cb = sinon.stub()
  267. })
  268. describe('when the preDoPassportLogin hooks produce an info object', function () {
  269. beforeEach(function () {
  270. this.Modules.hooks.fire = sinon
  271. .stub()
  272. .callsArgWith(3, null, [null, { redir: '/somewhere' }, null])
  273. })
  274. it('should stop early and call done with this info object', function (done) {
  275. this.AuthenticationController.doPassportLogin(
  276. this.req,
  277. this.req.body.email,
  278. this.req.body.password,
  279. this.cb
  280. )
  281. this.cb.callCount.should.equal(1)
  282. this.cb
  283. .calledWith(null, false, { redir: '/somewhere' })
  284. .should.equal(true)
  285. this.LoginRateLimiter.processLoginRequest.callCount.should.equal(0)
  286. done()
  287. })
  288. })
  289. describe('when the users rate limit', function () {
  290. beforeEach(function () {
  291. this.LoginRateLimiter.processLoginRequest.callsArgWith(1, null, false)
  292. })
  293. it('should block the request if the limit has been exceeded', function (done) {
  294. this.AuthenticationController.doPassportLogin(
  295. this.req,
  296. this.req.body.email,
  297. this.req.body.password,
  298. this.cb
  299. )
  300. this.cb.callCount.should.equal(1)
  301. this.cb.calledWith(null, null).should.equal(true)
  302. done()
  303. })
  304. })
  305. describe('when the user is authenticated', function () {
  306. beforeEach(function () {
  307. this.cb = sinon.stub()
  308. this.LoginRateLimiter.processLoginRequest.callsArgWith(1, null, true)
  309. this.AuthenticationManager.authenticate = sinon
  310. .stub()
  311. .callsArgWith(2, null, this.user)
  312. this.req.sessionID = Math.random()
  313. })
  314. describe('happy path', function () {
  315. beforeEach(function () {
  316. this.AuthenticationController.doPassportLogin(
  317. this.req,
  318. this.req.body.email,
  319. this.req.body.password,
  320. this.cb
  321. )
  322. })
  323. it('should attempt to authorise the user', function () {
  324. this.AuthenticationManager.authenticate
  325. .calledWith({ email: this.email.toLowerCase() }, this.password)
  326. .should.equal(true)
  327. })
  328. it("should establish the user's session", function () {
  329. this.cb.calledWith(null, this.user).should.equal(true)
  330. })
  331. })
  332. describe('when authenticate flags a parallel login', function () {
  333. beforeEach(function () {
  334. this.AuthenticationManager.authenticate = sinon
  335. .stub()
  336. .callsArgWith(2, new AuthenticationErrors.ParallelLoginError())
  337. this.AuthenticationController.doPassportLogin(
  338. this.req,
  339. this.req.body.email,
  340. this.req.body.password,
  341. this.cb
  342. )
  343. })
  344. it('should send a 429', function () {
  345. this.cb.should.have.been.calledWith(null, false, { status: 429 })
  346. })
  347. })
  348. describe('with a user having a recent failed login ', function () {
  349. beforeEach(function () {
  350. this.user.lastFailedLogin = new Date()
  351. })
  352. describe('with captcha disabled', function () {
  353. beforeEach(function () {
  354. this.req.__authAuditInfo.captcha = 'disabled'
  355. this.AuthenticationController.doPassportLogin(
  356. this.req,
  357. this.req.body.email,
  358. this.req.body.password,
  359. this.cb
  360. )
  361. })
  362. it('should let the user log in', function () {
  363. this.cb.should.have.been.calledWith(null, this.user)
  364. })
  365. })
  366. describe('with a solved captcha', function () {
  367. beforeEach(function () {
  368. this.req.__authAuditInfo.captcha = 'solved'
  369. this.AuthenticationController.doPassportLogin(
  370. this.req,
  371. this.req.body.email,
  372. this.req.body.password,
  373. this.cb
  374. )
  375. })
  376. it('should let the user log in', function () {
  377. this.cb.should.have.been.calledWith(null, this.user)
  378. })
  379. })
  380. describe('with a skipped captcha', function () {
  381. beforeEach(function () {
  382. this.req.__authAuditInfo.captcha = 'skipped'
  383. this.AuthenticationController.doPassportLogin(
  384. this.req,
  385. this.req.body.email,
  386. this.req.body.password,
  387. this.cb
  388. )
  389. })
  390. it('should request a captcha', function () {
  391. this.cb.should.have.been.calledWith(null, false, {
  392. text: 'cannot_verify_user_not_robot',
  393. type: 'error',
  394. errorReason: 'cannot_verify_user_not_robot',
  395. status: 400,
  396. })
  397. })
  398. })
  399. })
  400. })
  401. describe('when the user is not authenticated', function () {
  402. beforeEach(function () {
  403. this.LoginRateLimiter.processLoginRequest.callsArgWith(1, null, true)
  404. this.AuthenticationManager.authenticate = sinon
  405. .stub()
  406. .callsArgWith(2, null, null)
  407. this.cb = sinon.stub()
  408. this.AuthenticationController.doPassportLogin(
  409. this.req,
  410. this.req.body.email,
  411. this.req.body.password,
  412. this.cb
  413. )
  414. })
  415. it('should not establish the login', function () {
  416. this.cb.callCount.should.equal(1)
  417. this.cb.calledWith(null, false)
  418. // @res.body.should.exist
  419. expect(this.cb.lastCall.args[2]).to.contain.all.keys(['text', 'type'])
  420. })
  421. // message:
  422. // text: 'Your email or password were incorrect. Please try again',
  423. // type: 'error'
  424. it('should not setup the user data in the background', function () {
  425. this.UserHandler.setupLoginData.called.should.equal(false)
  426. })
  427. it('should record a failed login', function () {
  428. this.AuthenticationController._recordFailedLogin.called.should.equal(
  429. true
  430. )
  431. })
  432. it('should log the failed login', function () {
  433. this.logger.log
  434. .calledWith({ email: this.email.toLowerCase() }, 'failed log in')
  435. .should.equal(true)
  436. })
  437. })
  438. })
  439. describe('requireLogin', function () {
  440. beforeEach(function () {
  441. this.user = {
  442. _id: 'user-id-123',
  443. email: 'user@sharelatex.com',
  444. }
  445. this.middleware = this.AuthenticationController.requireLogin()
  446. })
  447. describe('when the user is logged in', function () {
  448. beforeEach(function () {
  449. this.req.session = {
  450. user: (this.user = {
  451. _id: 'user-id-123',
  452. email: 'user@sharelatex.com',
  453. }),
  454. }
  455. this.middleware(this.req, this.res, this.next)
  456. })
  457. it('should call the next method in the chain', function () {
  458. this.next.called.should.equal(true)
  459. })
  460. })
  461. describe('when the user is not logged in', function () {
  462. beforeEach(function () {
  463. this.req.session = {}
  464. this.AuthenticationController._redirectToLoginOrRegisterPage =
  465. sinon.stub()
  466. this.req.query = {}
  467. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  468. this.middleware(this.req, this.res, this.next)
  469. })
  470. it('should redirect to the register or login page', function () {
  471. this.AuthenticationController._redirectToLoginOrRegisterPage
  472. .calledWith(this.req, this.res)
  473. .should.equal(true)
  474. })
  475. })
  476. })
  477. describe('validateUserSession', function () {
  478. beforeEach(function () {
  479. this.user = {
  480. _id: 'user-id-123',
  481. email: 'user@sharelatex.com',
  482. }
  483. this.middleware = this.AuthenticationController.validateUserSession()
  484. })
  485. describe('when the user has a session token', function () {
  486. beforeEach(function () {
  487. this.req.user = this.user
  488. this.SessionStoreManager.hasValidationToken = sinon.stub().returns(true)
  489. this.middleware(this.req, this.res, this.next)
  490. })
  491. it('should call the next method in the chain', function () {
  492. this.next.called.should.equal(true)
  493. })
  494. })
  495. describe('when the user does not have a session token', function () {
  496. beforeEach(function () {
  497. this.req.session = {
  498. user: this.user,
  499. regenerate: sinon.stub().yields(),
  500. }
  501. this.req.user = this.user
  502. this.AuthenticationController._redirectToLoginOrRegisterPage =
  503. sinon.stub()
  504. this.req.query = {}
  505. this.SessionStoreManager.hasValidationToken = sinon
  506. .stub()
  507. .returns(false)
  508. this.middleware(this.req, this.res, this.next)
  509. })
  510. it('should destroy the current session', function () {
  511. this.req.session.regenerate.called.should.equal(true)
  512. })
  513. it('should redirect to the register or login page', function () {
  514. this.AuthenticationController._redirectToLoginOrRegisterPage
  515. .calledWith(this.req, this.res)
  516. .should.equal(true)
  517. })
  518. })
  519. })
  520. describe('requireOauth', function () {
  521. beforeEach(function () {
  522. this.res.json = sinon.stub()
  523. this.res.status = sinon.stub().returns(this.res)
  524. this.res.sendStatus = sinon.stub()
  525. this.middleware = this.AuthenticationController.requireOauth()
  526. })
  527. describe('when Oauth2Server authenticates', function () {
  528. beforeEach(function () {
  529. this.token = {
  530. accessToken: 'token',
  531. user: 'user',
  532. }
  533. this.Oauth2Server.server.authenticate.yields(null, this.token)
  534. this.middleware(this.req, this.res, this.next)
  535. })
  536. it('should set oauth_token on request', function () {
  537. this.req.oauth_token.should.equal(this.token)
  538. })
  539. it('should set oauth on request', function () {
  540. this.req.oauth.access_token.should.equal(this.token.accessToken)
  541. })
  542. it('should set oauth_user on request', function () {
  543. this.req.oauth_user.should.equal('user')
  544. })
  545. it('should call next', function () {
  546. this.next.should.have.been.calledOnce
  547. })
  548. })
  549. describe('when Oauth2Server returns 401 error', function () {
  550. beforeEach(function () {
  551. this.Oauth2Server.server.authenticate.yields({ code: 401 })
  552. this.middleware(this.req, this.res, this.next)
  553. })
  554. it('should return 401 error', function () {
  555. this.res.status.should.have.been.calledWith(401)
  556. })
  557. it('should not call next', function () {
  558. this.next.should.have.not.been.calledOnce
  559. })
  560. })
  561. })
  562. describe('requireGlobalLogin', function () {
  563. beforeEach(function () {
  564. this.req.headers = {}
  565. this.middleware = sinon.stub()
  566. this.AuthenticationController.requirePrivateApiAuth = sinon
  567. .stub()
  568. .returns(this.middleware)
  569. this.setRedirect = sinon.spy(
  570. this.AuthenticationController,
  571. 'setRedirectInSession'
  572. )
  573. })
  574. afterEach(function () {
  575. this.setRedirect.restore()
  576. })
  577. describe('with white listed url', function () {
  578. beforeEach(function () {
  579. this.AuthenticationController.addEndpointToLoginWhitelist('/login')
  580. this.req._parsedUrl.pathname = '/login'
  581. this.AuthenticationController.requireGlobalLogin(
  582. this.req,
  583. this.res,
  584. this.next
  585. )
  586. })
  587. it('should call next() directly', function () {
  588. this.next.called.should.equal(true)
  589. })
  590. })
  591. describe('with white listed url and a query string', function () {
  592. beforeEach(function () {
  593. this.AuthenticationController.addEndpointToLoginWhitelist('/login')
  594. this.req._parsedUrl.pathname = '/login'
  595. this.req.url = '/login?query=something'
  596. this.AuthenticationController.requireGlobalLogin(
  597. this.req,
  598. this.res,
  599. this.next
  600. )
  601. })
  602. it('should call next() directly', function () {
  603. this.next.called.should.equal(true)
  604. })
  605. })
  606. describe('with http auth', function () {
  607. beforeEach(function () {
  608. this.req.headers.authorization = 'Mock Basic Auth'
  609. this.AuthenticationController.requireGlobalLogin(
  610. this.req,
  611. this.res,
  612. this.next
  613. )
  614. })
  615. it('should pass the request onto requirePrivateApiAuth middleware', function () {
  616. this.middleware
  617. .calledWith(this.req, this.res, this.next)
  618. .should.equal(true)
  619. })
  620. })
  621. describe('with a user session', function () {
  622. beforeEach(function () {
  623. this.req.session = { user: { mock: 'user', _id: 'some_id' } }
  624. this.AuthenticationController.requireGlobalLogin(
  625. this.req,
  626. this.res,
  627. this.next
  628. )
  629. })
  630. it('should call next() directly', function () {
  631. this.next.called.should.equal(true)
  632. })
  633. })
  634. describe('with no login credentials', function () {
  635. beforeEach(function () {
  636. this.req.session = {}
  637. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  638. this.AuthenticationController.requireGlobalLogin(
  639. this.req,
  640. this.res,
  641. this.next
  642. )
  643. })
  644. it('should have called setRedirectInSession', function () {
  645. this.setRedirect.callCount.should.equal(1)
  646. })
  647. it('should redirect to the /login page', function () {
  648. this.res.redirectedTo.should.equal('/login')
  649. })
  650. })
  651. })
  652. describe('requireBasicAuth', function () {
  653. beforeEach(function () {
  654. this.basicAuthUsers = {
  655. 'basic-auth-user': 'basic-auth-password',
  656. }
  657. this.middleware = this.AuthenticationController.requireBasicAuth(
  658. this.basicAuthUsers
  659. )
  660. })
  661. describe('with http auth', function () {
  662. it('should error with incorrect user', function (done) {
  663. this.req.headers = {
  664. authorization: `Basic ${Buffer.from('user:nope').toString('base64')}`,
  665. }
  666. this.req.sendStatus = status => {
  667. expect(status).to.equal(401)
  668. done()
  669. }
  670. this.middleware(this.req, this.req)
  671. })
  672. it('should error with incorrect password', function (done) {
  673. this.req.headers = {
  674. authorization: `Basic ${Buffer.from('basic-auth-user:nope').toString(
  675. 'base64'
  676. )}`,
  677. }
  678. this.req.sendStatus = status => {
  679. expect(status).to.equal(401)
  680. done()
  681. }
  682. this.middleware(this.req, this.req)
  683. })
  684. it('should fail with empty pass', function (done) {
  685. this.req.headers = {
  686. authorization: `Basic ${Buffer.from(`basic-auth-user:`).toString(
  687. 'base64'
  688. )}`,
  689. }
  690. this.req.sendStatus = status => {
  691. expect(status).to.equal(401)
  692. done()
  693. }
  694. this.middleware(this.req, this.req)
  695. })
  696. it('should fail with empty user and password of "undefined"', function (done) {
  697. this.req.headers = {
  698. authorization: `Basic ${Buffer.from(`:undefined`).toString(
  699. 'base64'
  700. )}`,
  701. }
  702. this.req.sendStatus = status => {
  703. expect(status).to.equal(401)
  704. done()
  705. }
  706. this.middleware(this.req, this.req)
  707. })
  708. it('should fail with empty user and empty password', function (done) {
  709. this.req.headers = {
  710. authorization: `Basic ${Buffer.from(`:`).toString('base64')}`,
  711. }
  712. this.req.sendStatus = status => {
  713. expect(status).to.equal(401)
  714. done()
  715. }
  716. this.middleware(this.req, this.req)
  717. })
  718. it('should fail with a user that is not a valid property', function (done) {
  719. this.req.headers = {
  720. authorization: `Basic ${Buffer.from(
  721. `constructor:[Function ]`
  722. ).toString('base64')}`,
  723. }
  724. this.req.sendStatus = status => {
  725. expect(status).to.equal(401)
  726. done()
  727. }
  728. this.middleware(this.req, this.req)
  729. })
  730. it('should succeed with correct user/pass', function (done) {
  731. this.req.headers = {
  732. authorization: `Basic ${Buffer.from(
  733. `basic-auth-user:${this.basicAuthUsers['basic-auth-user']}`
  734. ).toString('base64')}`,
  735. }
  736. this.middleware(this.req, this.res, done)
  737. })
  738. })
  739. })
  740. describe('requirePrivateApiAuth', function () {
  741. beforeEach(function () {
  742. this.AuthenticationController.requireBasicAuth = sinon.stub()
  743. })
  744. it('should call requireBasicAuth with the private API user details', function () {
  745. this.AuthenticationController.requirePrivateApiAuth()
  746. this.AuthenticationController.requireBasicAuth
  747. .calledWith(this.httpAuthUsers)
  748. .should.equal(true)
  749. })
  750. })
  751. describe('_redirectToLoginOrRegisterPage', function () {
  752. beforeEach(function () {
  753. this.middleware = this.AuthenticationController.requireLogin(
  754. (this.options = { load_from_db: false })
  755. )
  756. this.req.session = {}
  757. this.AuthenticationController._redirectToRegisterPage = sinon.stub()
  758. this.AuthenticationController._redirectToLoginPage = sinon.stub()
  759. this.req.query = {}
  760. })
  761. describe('they have come directly to the url', function () {
  762. beforeEach(function () {
  763. this.req.query = {}
  764. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  765. this.middleware(this.req, this.res, this.next)
  766. })
  767. it('should redirect to the login page', function () {
  768. this.AuthenticationController._redirectToRegisterPage
  769. .calledWith(this.req, this.res)
  770. .should.equal(false)
  771. this.AuthenticationController._redirectToLoginPage
  772. .calledWith(this.req, this.res)
  773. .should.equal(true)
  774. })
  775. })
  776. describe('they have come via a templates link', function () {
  777. beforeEach(function () {
  778. this.req.query.zipUrl = 'something'
  779. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  780. this.middleware(this.req, this.res, this.next)
  781. })
  782. it('should redirect to the register page', function () {
  783. this.AuthenticationController._redirectToRegisterPage
  784. .calledWith(this.req, this.res)
  785. .should.equal(true)
  786. this.AuthenticationController._redirectToLoginPage
  787. .calledWith(this.req, this.res)
  788. .should.equal(false)
  789. })
  790. })
  791. describe('they have been invited to a project', function () {
  792. beforeEach(function () {
  793. this.req.query.project_name = 'something'
  794. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  795. this.middleware(this.req, this.res, this.next)
  796. })
  797. it('should redirect to the register page', function () {
  798. this.AuthenticationController._redirectToRegisterPage
  799. .calledWith(this.req, this.res)
  800. .should.equal(true)
  801. this.AuthenticationController._redirectToLoginPage
  802. .calledWith(this.req, this.res)
  803. .should.equal(false)
  804. })
  805. })
  806. })
  807. describe('_redirectToRegisterPage', function () {
  808. beforeEach(function () {
  809. this.req.path = '/target/url'
  810. this.req.query = { extra_query: 'foo' }
  811. this.AuthenticationController._redirectToRegisterPage(this.req, this.res)
  812. })
  813. it('should redirect to the register page with a query string attached', function () {
  814. this.req.session.postLoginRedirect.should.equal(
  815. '/target/url?extra_query=foo'
  816. )
  817. this.res.redirectedTo.should.equal('/register?extra_query=foo')
  818. })
  819. it('should log out a message', function () {
  820. this.logger.log
  821. .calledWith(
  822. { url: this.url },
  823. 'user not logged in so redirecting to register page'
  824. )
  825. .should.equal(true)
  826. })
  827. })
  828. describe('_redirectToLoginPage', function () {
  829. beforeEach(function () {
  830. this.req.path = '/target/url'
  831. this.req.query = { extra_query: 'foo' }
  832. this.AuthenticationController._redirectToLoginPage(this.req, this.res)
  833. })
  834. it('should redirect to the register page with a query string attached', function () {
  835. this.req.session.postLoginRedirect.should.equal(
  836. '/target/url?extra_query=foo'
  837. )
  838. this.res.redirectedTo.should.equal('/login?extra_query=foo')
  839. })
  840. })
  841. describe('_recordSuccessfulLogin', function () {
  842. beforeEach(function () {
  843. this.UserUpdater.updateUser = sinon.stub().callsArg(2)
  844. this.AuthenticationController._recordSuccessfulLogin(
  845. this.user._id,
  846. this.callback
  847. )
  848. })
  849. it('should increment the user.login.success metric', function () {
  850. this.Metrics.inc.calledWith('user.login.success').should.equal(true)
  851. })
  852. it("should update the user's login count and last logged in date", function () {
  853. this.UserUpdater.updateUser.args[0][1].$set.lastLoggedIn.should.not.equal(
  854. undefined
  855. )
  856. this.UserUpdater.updateUser.args[0][1].$inc.loginCount.should.equal(1)
  857. })
  858. it('should call the callback', function () {
  859. this.callback.called.should.equal(true)
  860. })
  861. })
  862. describe('_recordFailedLogin', function () {
  863. beforeEach(function () {
  864. this.AuthenticationController._recordFailedLogin(this.callback)
  865. })
  866. it('should increment the user.login.failed metric', function () {
  867. this.Metrics.inc.calledWith('user.login.failed').should.equal(true)
  868. })
  869. it('should call the callback', function () {
  870. this.callback.called.should.equal(true)
  871. })
  872. })
  873. describe('setRedirectInSession', function () {
  874. beforeEach(function () {
  875. this.req = { session: {} }
  876. this.req.path = '/somewhere'
  877. this.req.query = { one: '1' }
  878. })
  879. it('should set redirect property on session', function () {
  880. this.AuthenticationController.setRedirectInSession(this.req)
  881. expect(this.req.session.postLoginRedirect).to.equal('/somewhere?one=1')
  882. })
  883. it('should set the supplied value', function () {
  884. this.AuthenticationController.setRedirectInSession(
  885. this.req,
  886. '/somewhere/specific'
  887. )
  888. expect(this.req.session.postLoginRedirect).to.equal('/somewhere/specific')
  889. })
  890. it('should not allow open redirects', function () {
  891. this.AuthenticationController.setRedirectInSession(
  892. this.req,
  893. 'https://evil.com'
  894. )
  895. expect(this.req.session.postLoginRedirect).to.be.undefined
  896. })
  897. describe('with a png', function () {
  898. beforeEach(function () {
  899. this.req = { session: {} }
  900. })
  901. it('should not set the redirect', function () {
  902. this.AuthenticationController.setRedirectInSession(
  903. this.req,
  904. '/something.png'
  905. )
  906. expect(this.req.session.postLoginRedirect).to.equal(undefined)
  907. })
  908. })
  909. describe('with a js path', function () {
  910. beforeEach(function () {
  911. this.req = { session: {} }
  912. })
  913. it('should not set the redirect', function () {
  914. this.AuthenticationController.setRedirectInSession(
  915. this.req,
  916. '/js/something.js'
  917. )
  918. expect(this.req.session.postLoginRedirect).to.equal(undefined)
  919. })
  920. })
  921. })
  922. describe('_getRedirectFromSession', function () {
  923. it('should get redirect property from session', function () {
  924. this.req = { session: { postLoginRedirect: '/a?b=c' } }
  925. expect(
  926. this.AuthenticationController._getRedirectFromSession(this.req)
  927. ).to.equal('/a?b=c')
  928. })
  929. it('should not allow open redirects', function () {
  930. this.req = { session: { postLoginRedirect: 'https://evil.com' } }
  931. expect(this.AuthenticationController._getRedirectFromSession(this.req)).to
  932. .be.null
  933. })
  934. it('handle null values', function () {
  935. this.req = { session: {} }
  936. expect(this.AuthenticationController._getRedirectFromSession(this.req)).to
  937. .be.null
  938. })
  939. })
  940. describe('_clearRedirectFromSession', function () {
  941. beforeEach(function () {
  942. this.req = { session: { postLoginRedirect: '/a?b=c' } }
  943. })
  944. it('should remove the redirect property from session', function () {
  945. this.AuthenticationController._clearRedirectFromSession(this.req)
  946. expect(this.req.session.postLoginRedirect).to.equal(undefined)
  947. })
  948. })
  949. describe('finishLogin', function () {
  950. // - get redirect
  951. // - async handlers
  952. // - afterLoginSessionSetup
  953. // - clear redirect
  954. // - issue redir, two ways
  955. beforeEach(function () {
  956. this.AuthenticationController._getRedirectFromSession = sinon
  957. .stub()
  958. .returns('/some/page')
  959. this.req.sessionID = 'thisisacryptographicallysecurerandomid'
  960. this.req.session = {
  961. passport: { user: { _id: 'one' } },
  962. }
  963. this.req.session.destroy = sinon.stub().callsArgWith(0, null)
  964. this.req.session.save = sinon.stub().callsArgWith(0, null)
  965. this.req.sessionStore = { generate: sinon.stub() }
  966. this.req.login = sinon.stub().yields(null)
  967. this.AuthenticationController._clearRedirectFromSession = sinon.stub()
  968. this.AuthenticationController._redirectToReconfirmPage = sinon.stub()
  969. this.UserSessionsManager.trackSession = sinon.stub()
  970. this.UserHandler.setupLoginData = sinon.stub()
  971. this.LoginRateLimiter.recordSuccessfulLogin = sinon.stub()
  972. this.AuthenticationController._recordSuccessfulLogin = sinon.stub()
  973. this.AnalyticsManager.recordEvent = sinon.stub()
  974. this.AnalyticsManager.identifyUser = sinon.stub()
  975. this.acceptsJson.returns(true)
  976. this.res.json = sinon.stub()
  977. this.res.redirect = sinon.stub()
  978. })
  979. it('should extract the redirect from the session', function () {
  980. this.AuthenticationController.finishLogin(
  981. this.user,
  982. this.req,
  983. this.res,
  984. this.next
  985. )
  986. expect(
  987. this.AuthenticationController._getRedirectFromSession.callCount
  988. ).to.equal(1)
  989. expect(
  990. this.AuthenticationController._getRedirectFromSession.calledWith(
  991. this.req
  992. )
  993. ).to.equal(true)
  994. })
  995. it('should clear redirect from session', function () {
  996. this.AuthenticationController.finishLogin(
  997. this.user,
  998. this.req,
  999. this.res,
  1000. this.next
  1001. )
  1002. expect(
  1003. this.AuthenticationController._clearRedirectFromSession.callCount
  1004. ).to.equal(1)
  1005. expect(
  1006. this.AuthenticationController._clearRedirectFromSession.calledWith(
  1007. this.req
  1008. )
  1009. ).to.equal(true)
  1010. })
  1011. it('should issue a json response with a redirect', function () {
  1012. this.AuthenticationController.finishLogin(
  1013. this.user,
  1014. this.req,
  1015. this.res,
  1016. this.next
  1017. )
  1018. expect(
  1019. this.AsyncFormHelper.redirect.calledWith(
  1020. this.req,
  1021. this.res,
  1022. '/some/page'
  1023. )
  1024. ).to.equal(true)
  1025. })
  1026. describe('with a non-json request', function () {
  1027. beforeEach(function () {
  1028. this.acceptsJson.returns(false)
  1029. this.res.json = sinon.stub()
  1030. this.res.redirect = sinon.stub()
  1031. })
  1032. it('should issue a plain redirect', function () {
  1033. this.AuthenticationController.finishLogin(
  1034. this.user,
  1035. this.req,
  1036. this.res,
  1037. this.next
  1038. )
  1039. expect(
  1040. this.AsyncFormHelper.redirect.calledWith(
  1041. this.req,
  1042. this.res,
  1043. '/some/page'
  1044. )
  1045. ).to.equal(true)
  1046. })
  1047. })
  1048. describe('when user is flagged to reconfirm', function () {
  1049. beforeEach(function () {
  1050. this.req.session = {}
  1051. this.user.must_reconfirm = true
  1052. })
  1053. it('should redirect to reconfirm page', function () {
  1054. this.AuthenticationController.finishLogin(
  1055. this.user,
  1056. this.req,
  1057. this.res,
  1058. this.next
  1059. )
  1060. expect(
  1061. this.AuthenticationController._redirectToReconfirmPage.calledWith(
  1062. this.req
  1063. )
  1064. ).to.equal(true)
  1065. })
  1066. })
  1067. describe('preFinishLogin hook', function () {
  1068. it('call hook and proceed', function () {
  1069. this.Modules.hooks.fire = sinon.stub().yields(null, [])
  1070. this.AuthenticationController.finishLogin(
  1071. this.user,
  1072. this.req,
  1073. this.res,
  1074. this.next
  1075. )
  1076. sinon.assert.calledWith(
  1077. this.Modules.hooks.fire,
  1078. 'preFinishLogin',
  1079. this.req,
  1080. this.res,
  1081. this.user
  1082. )
  1083. expect(this.AsyncFormHelper.redirect.called).to.equal(true)
  1084. })
  1085. it('stop if hook has redirected', function (done) {
  1086. this.Modules.hooks.fire = sinon
  1087. .stub()
  1088. .yields(null, [{ doNotFinish: true }])
  1089. this.AuthenticationController.finishLogin(
  1090. this.user,
  1091. this.req,
  1092. this.res,
  1093. this.next
  1094. )
  1095. expect(this.next.callCount).to.equal(0)
  1096. expect(this.res.json.callCount).to.equal(0)
  1097. done()
  1098. })
  1099. it('call next with hook errors', function (done) {
  1100. this.Modules.hooks.fire = sinon.stub().yields(new Error())
  1101. this.AuthenticationController.finishLogin(
  1102. this.user,
  1103. this.req,
  1104. this.res,
  1105. error => {
  1106. expect(error).to.exist
  1107. expect(this.res.json.callCount).to.equal(0)
  1108. done()
  1109. }
  1110. )
  1111. })
  1112. })
  1113. describe('UserAuditLog', function () {
  1114. it('should add an audit log entry', function () {
  1115. this.AuthenticationController.finishLogin(
  1116. this.user,
  1117. this.req,
  1118. this.res,
  1119. this.next
  1120. )
  1121. expect(this.UserAuditLogHandler.addEntry).to.have.been.calledWith(
  1122. this.user._id,
  1123. 'login',
  1124. this.user._id,
  1125. '42.42.42.42'
  1126. )
  1127. })
  1128. it('should add an audit log entry before logging the user in', function () {
  1129. this.AuthenticationController.finishLogin(
  1130. this.user,
  1131. this.req,
  1132. this.res,
  1133. this.next
  1134. )
  1135. expect(this.UserAuditLogHandler.addEntry).to.have.been.calledBefore(
  1136. this.req.login
  1137. )
  1138. })
  1139. it('should not log the user in without an audit log entry', function () {
  1140. const theError = new Error()
  1141. this.UserAuditLogHandler.addEntry.yields(theError)
  1142. this.AuthenticationController.finishLogin(
  1143. this.user,
  1144. this.req,
  1145. this.res,
  1146. this.next
  1147. )
  1148. expect(this.next).to.have.been.calledWith(theError)
  1149. expect(this.req.login).to.not.have.been.called
  1150. })
  1151. it('should pass along auditInfo when present', function () {
  1152. this.AuthenticationController.setAuditInfo(this.req, {
  1153. method: 'Login',
  1154. })
  1155. this.AuthenticationController.finishLogin(
  1156. this.user,
  1157. this.req,
  1158. this.res,
  1159. this.next
  1160. )
  1161. expect(this.UserAuditLogHandler.addEntry).to.have.been.calledWith(
  1162. this.user._id,
  1163. 'login',
  1164. this.user._id,
  1165. '42.42.42.42',
  1166. { method: 'Login' }
  1167. )
  1168. })
  1169. })
  1170. describe('_afterLoginSessionSetup', function () {
  1171. beforeEach(function () {})
  1172. it('should call req.login', function () {
  1173. this.AuthenticationController.finishLogin(
  1174. this.user,
  1175. this.req,
  1176. this.res,
  1177. this.next
  1178. )
  1179. this.req.login.callCount.should.equal(1)
  1180. })
  1181. it('should erase the CSRF secret', function () {
  1182. this.AuthenticationController.finishLogin(
  1183. this.user,
  1184. this.req,
  1185. this.res,
  1186. this.next
  1187. )
  1188. expect(this.req.session.csrfSecret).to.not.exist
  1189. })
  1190. it('should call req.session.save', function () {
  1191. this.AuthenticationController.finishLogin(
  1192. this.user,
  1193. this.req,
  1194. this.res,
  1195. this.next
  1196. )
  1197. this.req.session.save.callCount.should.equal(1)
  1198. })
  1199. it('should call UserSessionsManager.trackSession', function () {
  1200. this.AuthenticationController.finishLogin(
  1201. this.user,
  1202. this.req,
  1203. this.res,
  1204. this.next
  1205. )
  1206. this.UserSessionsManager.trackSession.callCount.should.equal(1)
  1207. })
  1208. describe('when req.session.save produces an error', function () {
  1209. beforeEach(function () {
  1210. this.req.session.save = sinon
  1211. .stub()
  1212. .callsArgWith(0, new Error('woops'))
  1213. })
  1214. it('should produce an error', function (done) {
  1215. this.AuthenticationController.finishLogin(
  1216. this.user,
  1217. this.req,
  1218. this.res,
  1219. err => {
  1220. expect(err).to.not.be.oneOf([null, undefined])
  1221. expect(err).to.be.instanceof(Error)
  1222. done()
  1223. }
  1224. )
  1225. })
  1226. it('should not call UserSessionsManager.trackSession', function (done) {
  1227. this.AuthenticationController.finishLogin(
  1228. this.user,
  1229. this.req,
  1230. this.res,
  1231. err => {
  1232. expect(err).to.exist
  1233. this.UserSessionsManager.trackSession.callCount.should.equal(0)
  1234. done()
  1235. }
  1236. )
  1237. })
  1238. })
  1239. })
  1240. describe('_loginAsyncHandlers', function () {
  1241. beforeEach(function () {
  1242. this.AuthenticationController.finishLogin(
  1243. this.user,
  1244. this.req,
  1245. this.res,
  1246. this.next
  1247. )
  1248. })
  1249. it('should call identifyUser', function () {
  1250. sinon.assert.calledWith(
  1251. this.AnalyticsManager.identifyUser,
  1252. this.user._id,
  1253. this.req.session.analyticsId
  1254. )
  1255. })
  1256. it('should setup the user data in the background', function () {
  1257. this.UserHandler.setupLoginData.calledWith(this.user).should.equal(true)
  1258. })
  1259. it('should set res.session.justLoggedIn', function () {
  1260. this.req.session.justLoggedIn.should.equal(true)
  1261. })
  1262. it('should record the successful login', function () {
  1263. this.AuthenticationController._recordSuccessfulLogin
  1264. .calledWith(this.user._id)
  1265. .should.equal(true)
  1266. })
  1267. it('should tell the rate limiter that there was a success for that email', function () {
  1268. this.LoginRateLimiter.recordSuccessfulLogin
  1269. .calledWith(this.user.email)
  1270. .should.equal(true)
  1271. })
  1272. it('should log the successful login', function () {
  1273. this.logger.log
  1274. .calledWith(
  1275. { email: this.user.email, user_id: this.user._id.toString() },
  1276. 'successful log in'
  1277. )
  1278. .should.equal(true)
  1279. })
  1280. it('should track the login event', function () {
  1281. sinon.assert.calledWith(
  1282. this.AnalyticsManager.recordEventForUser,
  1283. this.user._id,
  1284. 'user-logged-in'
  1285. )
  1286. })
  1287. })
  1288. })
  1289. })