login-required.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* eslint-disable arrow-body-style */
  2. const { getInstance } = require('../setup-crowi');
  3. describe('loginRequired', () => {
  4. let crowi;
  5. let loginRequiredStrictly;
  6. let loginRequired;
  7. beforeEach(async(done) => {
  8. crowi = await getInstance();
  9. loginRequiredStrictly = require('@server/middleware/login-required')(crowi);
  10. loginRequired = require('@server/middleware/login-required')(crowi, true);
  11. done();
  12. });
  13. describe('not strict mode', () => {
  14. // setup req/res/next
  15. const req = {
  16. originalUrl: 'original url 1',
  17. session: {},
  18. };
  19. const res = {
  20. redirect: jest.fn().mockReturnValue('redirect'),
  21. };
  22. const next = jest.fn().mockReturnValue('next');
  23. test('pass guest user when aclService.isGuestAllowedToRead() returns true', () => {
  24. // prepare spy for AclService.isGuestAllowedToRead
  25. const isGuestAllowedToReadSpy = jest.spyOn(crowi.aclService, 'isGuestAllowedToRead')
  26. .mockImplementation(() => true);
  27. const result = loginRequired(req, res, next);
  28. expect(isGuestAllowedToReadSpy).toHaveBeenCalledTimes(1);
  29. expect(next).toHaveBeenCalled();
  30. expect(res.redirect).not.toHaveBeenCalled();
  31. expect(result).toBe('next');
  32. });
  33. test('redirect to \'/login\' when aclService.isGuestAllowedToRead() returns false', () => {
  34. // prepare spy for AclService.isGuestAllowedToRead
  35. const isGuestAllowedToReadSpy = jest.spyOn(crowi.aclService, 'isGuestAllowedToRead')
  36. .mockImplementation(() => false);
  37. const result = loginRequired(req, res, next);
  38. expect(isGuestAllowedToReadSpy).toHaveBeenCalled();
  39. expect(next).not.toHaveBeenCalled();
  40. expect(res.redirect).toHaveBeenCalledTimes(1);
  41. expect(res.redirect).toHaveBeenCalledWith('/login');
  42. expect(result).toBe('redirect');
  43. });
  44. });
  45. describe('strict mode', () => {
  46. // setup req/res/next
  47. const req = {
  48. originalUrl: 'original url 1',
  49. session: null,
  50. };
  51. const res = {
  52. redirect: jest.fn().mockReturnValue('redirect'),
  53. sendStatus: jest.fn().mockReturnValue('sendStatus'),
  54. };
  55. const next = jest.fn().mockReturnValue('next');
  56. let isGuestAllowedToReadSpy;
  57. beforeEach(async(done) => {
  58. // reset session object
  59. req.session = {};
  60. // spy for AclService.isGuestAllowedToRead
  61. isGuestAllowedToReadSpy = jest.spyOn(crowi.aclService, 'isGuestAllowedToRead');
  62. done();
  63. });
  64. test('send status 403 when \'req.path\' starts with \'_api\'', () => {
  65. req.path = '/_api/someapi';
  66. const result = loginRequiredStrictly(req, res, next);
  67. expect(isGuestAllowedToReadSpy).not.toHaveBeenCalled();
  68. expect(next).not.toHaveBeenCalled();
  69. expect(res.redirect).not.toHaveBeenCalled();
  70. expect(res.sendStatus).toHaveBeenCalledTimes(1);
  71. expect(res.sendStatus).toHaveBeenCalledWith(403);
  72. expect(result).toBe('sendStatus');
  73. });
  74. test('redirect to \'/login\' when the user does not loggedin', () => {
  75. req.path = '/path/that/requires/loggedin';
  76. const result = loginRequiredStrictly(req, res, next);
  77. expect(isGuestAllowedToReadSpy).not.toHaveBeenCalled();
  78. expect(next).not.toHaveBeenCalled();
  79. expect(res.sendStatus).not.toHaveBeenCalled();
  80. expect(res.redirect).toHaveBeenCalledTimes(1);
  81. expect(res.redirect).toHaveBeenCalledWith('/login');
  82. expect(result).toBe('redirect');
  83. expect(req.session.jumpTo).toBe('original url 1');
  84. });
  85. test('pass user who logged in', () => {
  86. const User = crowi.model('User');
  87. req.user = {
  88. _id: 'user id',
  89. status: User.STATUS_ACTIVE,
  90. };
  91. const result = loginRequiredStrictly(req, res, next);
  92. expect(isGuestAllowedToReadSpy).not.toHaveBeenCalled();
  93. expect(res.sendStatus).not.toHaveBeenCalled();
  94. expect(res.redirect).not.toHaveBeenCalled();
  95. expect(next).toHaveBeenCalledTimes(1);
  96. expect(result).toBe('next');
  97. expect(req.session.jumpTo).toBe(undefined);
  98. });
  99. /* eslint-disable indent */
  100. test.each`
  101. userStatus | expectedPath
  102. ${1} | ${'/login/error/registered'}
  103. ${3} | ${'/login/error/suspended'}
  104. ${5} | ${'/login/invited'}
  105. `('redirect to \'$expectedPath\' when user.status is \'$userStatus\'', ({ userStatus, expectedPath }) => {
  106. req.user = {
  107. _id: 'user id',
  108. status: userStatus,
  109. };
  110. const result = loginRequiredStrictly(req, res, next);
  111. expect(isGuestAllowedToReadSpy).not.toHaveBeenCalled();
  112. expect(next).not.toHaveBeenCalled();
  113. expect(res.sendStatus).not.toHaveBeenCalled();
  114. expect(res.redirect).toHaveBeenCalledTimes(1);
  115. expect(res.redirect).toHaveBeenCalledWith(expectedPath);
  116. expect(result).toBe('redirect');
  117. expect(req.session.jumpTo).toBe(undefined);
  118. });
  119. /* eslint-disable indent */
  120. test('redirect to \'/login\' when user.status is \'STATUS_DELETED\'', () => {
  121. const User = crowi.model('User');
  122. req.path = '/path/that/requires/loggedin';
  123. req.user = {
  124. _id: 'user id',
  125. status: User.STATUS_DELETED,
  126. };
  127. const result = loginRequiredStrictly(req, res, next);
  128. expect(isGuestAllowedToReadSpy).not.toHaveBeenCalled();
  129. expect(next).not.toHaveBeenCalled();
  130. expect(res.sendStatus).not.toHaveBeenCalled();
  131. expect(res.redirect).toHaveBeenCalledTimes(1);
  132. expect(res.redirect).toHaveBeenCalledWith('/login');
  133. expect(result).toBe('redirect');
  134. expect(req.session.jumpTo).toBe('original url 1');
  135. });
  136. });
  137. });