UserHelperTests.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const AuthenticationManager = require('../../../app/src/Features/Authentication/AuthenticationManager')
  2. const UserHelper = require('./helpers/UserHelper')
  3. const chai = require('chai')
  4. const chaiAsPromised = require('chai-as-promised')
  5. const Features = require('../../../app/src/infrastructure/Features')
  6. const expect = chai.expect
  7. chai.should()
  8. chai.use(chaiAsPromised)
  9. describe('UserHelper', function() {
  10. describe('UserHelper.createUser', function() {
  11. describe('with no args', function() {
  12. it('should create new user with default username and password', async function() {
  13. const userHelper = await UserHelper.createUser()
  14. userHelper.user.email.should.equal(userHelper.getDefaultEmail())
  15. const authedUser = await AuthenticationManager.promises.authenticate(
  16. { _id: userHelper.user._id },
  17. userHelper.getDefaultPassword()
  18. )
  19. expect(authedUser).to.not.be.null
  20. })
  21. })
  22. describe('with email', function() {
  23. it('should create new user with provided email and default password', async function() {
  24. const userHelper = await UserHelper.createUser({
  25. email: 'foo@test.com'
  26. })
  27. userHelper.user.email.should.equal('foo@test.com')
  28. const authedUser = await AuthenticationManager.promises.authenticate(
  29. { _id: userHelper.user._id },
  30. userHelper.getDefaultPassword()
  31. )
  32. expect(authedUser).to.not.be.null
  33. })
  34. })
  35. describe('with password', function() {
  36. it('should create new user with provided password and default email', async function() {
  37. const userHelper = await UserHelper.createUser({
  38. password: 'foofoofoo'
  39. })
  40. userHelper.user.email.should.equal(userHelper.getDefaultEmail())
  41. const authedUser = await AuthenticationManager.promises.authenticate(
  42. { _id: userHelper.user._id },
  43. 'foofoofoo'
  44. )
  45. expect(authedUser).to.not.be.null
  46. })
  47. })
  48. })
  49. describe('fetching existing user', function() {
  50. let user
  51. beforeEach(async function() {
  52. user = (await UserHelper.createUser()).user
  53. })
  54. describe('with string id', function() {
  55. it('should fetch user', async function() {
  56. const userHelper = await UserHelper.getUser(user._id.toString())
  57. userHelper.user.email.should.equal(user.email)
  58. })
  59. })
  60. describe('with _id', function() {
  61. it('should fetch user', async function() {
  62. const userHelper = await UserHelper.getUser({ _id: user._id })
  63. userHelper.user.email.should.equal(user.email)
  64. })
  65. })
  66. })
  67. describe('getCsrfToken', function() {
  68. describe('when the csrfToken is not cached', function() {
  69. it('should fetch csrfToken', async function() {
  70. const userHelper = new UserHelper()
  71. await userHelper.getCsrfToken()
  72. expect(userHelper.csrfToken).to.be.a.string
  73. })
  74. })
  75. describe('when the csrfToken is cached', function() {
  76. it('should fetch csrfToken', async function() {
  77. let userHelper = new UserHelper()
  78. await userHelper.getCsrfToken()
  79. const csrfToken = userHelper._csrfToken
  80. await userHelper.getCsrfToken()
  81. expect(csrfToken).to.equal(userHelper._csrfToken)
  82. })
  83. })
  84. })
  85. describe('registerUser', function() {
  86. describe('with no args', function() {
  87. before(function() {
  88. if (!Features.hasFeature('public-registration')) {
  89. this.skip()
  90. }
  91. })
  92. it('should create new user with default username and password', async function() {
  93. const userHelper = await UserHelper.registerUser()
  94. userHelper.user.email.should.equal(userHelper.getDefaultEmail())
  95. const authedUser = await AuthenticationManager.promises.authenticate(
  96. { _id: userHelper.user._id },
  97. userHelper.getDefaultPassword()
  98. )
  99. expect(authedUser).to.not.be.null
  100. })
  101. })
  102. describe('with email', function() {
  103. before(function() {
  104. if (!Features.hasFeature('public-registration')) {
  105. this.skip()
  106. }
  107. })
  108. it('should create new user with provided email and default password', async function() {
  109. const userHelper = await UserHelper.registerUser({
  110. email: 'foo2@test.com'
  111. })
  112. userHelper.user.email.should.equal('foo2@test.com')
  113. const authedUser = await AuthenticationManager.promises.authenticate(
  114. { _id: userHelper.user._id },
  115. userHelper.getDefaultPassword()
  116. )
  117. expect(authedUser).to.not.be.null
  118. })
  119. })
  120. describe('with password', function() {
  121. before(function() {
  122. if (!Features.hasFeature('public-registration')) {
  123. this.skip()
  124. }
  125. })
  126. it('should create new user with provided password and default email', async function() {
  127. const userHelper = await UserHelper.registerUser({
  128. password: 'foofoofoo'
  129. })
  130. userHelper.user.email.should.equal(userHelper.getDefaultEmail())
  131. const authedUser = await AuthenticationManager.promises.authenticate(
  132. { _id: userHelper.user._id },
  133. 'foofoofoo'
  134. )
  135. expect(authedUser).to.not.be.null
  136. })
  137. })
  138. })
  139. describe('after logout', function() {
  140. let userHelper, oldCsrfToken
  141. before(function() {
  142. if (!Features.hasFeature('public-registration')) {
  143. this.skip()
  144. }
  145. })
  146. beforeEach(async function() {
  147. userHelper = await UserHelper.registerUser()
  148. oldCsrfToken = userHelper.csrfToken
  149. })
  150. it('refreshes csrf token after logout', async function() {
  151. await userHelper.logout()
  152. expect(userHelper._csrfToken).to.equal('')
  153. await userHelper.getCsrfToken()
  154. expect(userHelper._csrfToken).to.not.equal('')
  155. expect(userHelper._csrfToken).to.not.equal(oldCsrfToken)
  156. })
  157. })
  158. })