AuthenticationTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const { expect } = require('chai')
  2. const { ObjectId } = require('mongodb')
  3. const Settings = require('@overleaf/settings')
  4. const User = require('./helpers/User').promises
  5. describe('Authentication', function () {
  6. let user
  7. beforeEach('init vars', function () {
  8. user = new User()
  9. })
  10. describe('CSRF regeneration on login', function () {
  11. it('should prevent use of csrf token from before login', function (done) {
  12. user.logout(err => {
  13. if (err) {
  14. return done(err)
  15. }
  16. user.getCsrfToken(err => {
  17. if (err) {
  18. return done(err)
  19. }
  20. const oldToken = user.csrfToken
  21. user.login(err => {
  22. if (err) {
  23. return done(err)
  24. }
  25. expect(oldToken === user.csrfToken).to.equal(false)
  26. user.request.post(
  27. {
  28. headers: {
  29. 'x-csrf-token': oldToken,
  30. },
  31. url: '/project/new',
  32. json: { projectName: 'test' },
  33. },
  34. (err, response, body) => {
  35. expect(err).to.not.exist
  36. expect(response.statusCode).to.equal(403)
  37. expect(body).to.equal('Forbidden')
  38. done()
  39. }
  40. )
  41. })
  42. })
  43. })
  44. })
  45. })
  46. describe('login', function () {
  47. beforeEach('doLogin', async function () {
  48. await user.login()
  49. })
  50. it('should log the user in', async function () {
  51. const {
  52. response: { statusCode },
  53. } = await user.doRequest('GET', '/project')
  54. expect(statusCode).to.equal(200)
  55. })
  56. it('should emit an user auditLog entry for the login', async function () {
  57. const auditLog = await user.getAuditLog()
  58. const auditLogEntry = auditLog[0]
  59. expect(auditLogEntry).to.exist
  60. expect(auditLogEntry.timestamp).to.exist
  61. expect(auditLogEntry.initiatorId).to.deep.equal(new ObjectId(user.id))
  62. expect(auditLogEntry.userId).to.deep.equal(new ObjectId(user.id))
  63. expect(auditLogEntry.operation).to.equal('login')
  64. expect(auditLogEntry.info).to.deep.equal({
  65. method: 'Password login',
  66. captcha: 'solved',
  67. })
  68. expect(auditLogEntry.ipAddress).to.equal('127.0.0.1')
  69. })
  70. })
  71. describe('failed login', function () {
  72. beforeEach('fetchCsrfToken', async function () {
  73. await user.login()
  74. await user.logout()
  75. await user.getCsrfToken()
  76. })
  77. it('should return a 401, and add an entry to the audit log', async function () {
  78. const {
  79. response: { statusCode },
  80. } = await user.doRequest('POST', {
  81. url: Settings.enableLegacyLogin ? '/login/legacy' : '/login',
  82. json: {
  83. email: user.email,
  84. password: 'foo-bar-baz',
  85. 'g-recaptcha-response': 'valid',
  86. },
  87. })
  88. expect(statusCode).to.equal(401)
  89. const auditLog = await user.getAuditLog()
  90. const auditLogEntry = auditLog.pop()
  91. expect(auditLogEntry).to.exist
  92. expect(auditLogEntry.timestamp).to.exist
  93. expect(auditLogEntry.initiatorId).to.deep.equal(new ObjectId(user.id))
  94. expect(auditLogEntry.userId).to.deep.equal(new ObjectId(user.id))
  95. expect(auditLogEntry.operation).to.equal('failed-password-match')
  96. expect(auditLogEntry.info).to.deep.equal({
  97. method: 'Password login',
  98. })
  99. expect(auditLogEntry.ipAddress).to.equal('127.0.0.1')
  100. })
  101. })
  102. })