exclude-read-only-user.test.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { ErrorV3 } from '@growi/core';
  2. import { excludeReadOnlyUser } from '../../../src/server/middlewares/exclude-read-only-user';
  3. describe('excludeReadOnlyUser', () => {
  4. let req;
  5. let res;
  6. let next;
  7. beforeEach(() => {
  8. req = {
  9. user: {},
  10. };
  11. res = {
  12. apiv3Err: jest.fn(),
  13. };
  14. next = jest.fn();
  15. });
  16. test('should call next if user is not found', () => {
  17. req.user = null;
  18. excludeReadOnlyUser(req, res, next);
  19. expect(next).toBeCalled();
  20. expect(res.apiv3Err).not.toBeCalled();
  21. });
  22. test('should call next if user is not read only', () => {
  23. req.user.readOnly = false;
  24. excludeReadOnlyUser(req, res, next);
  25. expect(next).toBeCalled();
  26. expect(res.apiv3Err).not.toBeCalled();
  27. });
  28. test('should return error response if user is read only', () => {
  29. req.user.readOnly = true;
  30. excludeReadOnlyUser(req, res, next);
  31. expect(next).not.toBeCalled();
  32. expect(res.apiv3Err).toBeCalledWith(
  33. new ErrorV3('This user is read only user', 'validatioin_failed'),
  34. );
  35. });
  36. });