auth.controller.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { Request, Response } from 'express';
  2. import fs from 'fs';
  3. import * as argon2 from 'argon2';
  4. import config from '../../../config';
  5. import AuthController from '../auth.controller';
  6. let user: any;
  7. jest.mock('fs');
  8. const next = jest.fn();
  9. const MOCK_USER_REGISTERED = () => ({
  10. [`${config.ROOT_FOLDER}/state/users.json`]: `[${user}]`,
  11. });
  12. const MOCK_NO_USER = {
  13. [`${config.ROOT_FOLDER}/state/users.json`]: '[]',
  14. };
  15. beforeAll(async () => {
  16. const hash = await argon2.hash('password');
  17. user = JSON.stringify({
  18. email: 'username',
  19. password: hash,
  20. });
  21. });
  22. describe('Login', () => {
  23. beforeEach(() => {
  24. // @ts-ignore
  25. fs.__createMockFiles(MOCK_USER_REGISTERED());
  26. });
  27. it('Should put cookie in response after login', async () => {
  28. const json = jest.fn();
  29. const res = { cookie: jest.fn(), status: jest.fn(() => ({ json })), json: jest.fn() } as unknown as Response;
  30. const req = { body: { email: 'username', password: 'password' } } as Request;
  31. await AuthController.login(req, res, next);
  32. expect(res.cookie).toHaveBeenCalledWith('tipi_token', expect.any(String), expect.any(Object));
  33. expect(res.status).toHaveBeenCalledWith(200);
  34. expect(json).toHaveBeenCalledWith({ token: expect.any(String) });
  35. expect(next).not.toHaveBeenCalled();
  36. });
  37. it('Should throw if username is not provided in request', async () => {
  38. const res = { cookie: jest.fn(), status: jest.fn(), json: jest.fn() } as unknown as Response;
  39. const req = { body: { password: 'password' } } as Request;
  40. await AuthController.login(req, res, next);
  41. expect(res.cookie).not.toHaveBeenCalled();
  42. expect(next).toHaveBeenCalledWith(expect.any(Error));
  43. });
  44. it('Should throw if password is not provided in request', async () => {
  45. const res = { cookie: jest.fn(), status: jest.fn(), json: jest.fn() } as unknown as Response;
  46. const req = { body: { email: 'username' } } as Request;
  47. await AuthController.login(req, res, next);
  48. expect(res.cookie).not.toHaveBeenCalled();
  49. expect(next).toHaveBeenCalledWith(expect.any(Error));
  50. });
  51. });
  52. describe('Register', () => {
  53. beforeEach(() => {
  54. // @ts-ignore
  55. fs.__createMockFiles(MOCK_NO_USER);
  56. });
  57. it('Should put cookie in response after register', async () => {
  58. const json = jest.fn();
  59. const res = { cookie: jest.fn(), status: jest.fn(() => ({ json })), json: jest.fn() } as unknown as Response;
  60. const req = { body: { email: 'username', password: 'password', name: 'name' } } as Request;
  61. await AuthController.register(req, res, next);
  62. expect(res.cookie).toHaveBeenCalledWith('tipi_token', expect.any(String), expect.any(Object));
  63. expect(res.status).toHaveBeenCalledWith(200);
  64. expect(json).toHaveBeenCalledWith({ token: expect.any(String) });
  65. });
  66. });
  67. describe('Me', () => {
  68. beforeEach(() => {
  69. // @ts-ignore
  70. fs.__createMockFiles(MOCK_USER_REGISTERED());
  71. });
  72. it('Should return user if present in request', async () => {
  73. const json = jest.fn();
  74. const res = { status: jest.fn(() => ({ json })) } as unknown as Response;
  75. const req = { user } as unknown as Request;
  76. await AuthController.me(req, res, next);
  77. expect(res.status).toHaveBeenCalledWith(200);
  78. expect(json).toHaveBeenCalledWith({ user });
  79. });
  80. it('Should return null if user is not present in request', async () => {
  81. const json = jest.fn();
  82. const res = { status: jest.fn(() => ({ json })) } as unknown as Response;
  83. const req = {} as Request;
  84. await AuthController.me(req, res, next);
  85. expect(res.status).toHaveBeenCalledWith(200);
  86. expect(json).toHaveBeenCalledWith({ user: null });
  87. });
  88. });
  89. describe('isConfigured', () => {
  90. beforeEach(() => {
  91. // @ts-ignore
  92. fs.__createMockFiles(MOCK_NO_USER);
  93. });
  94. it('Should return false if no user is registered', async () => {
  95. const json = jest.fn();
  96. const res = { status: jest.fn(() => ({ json })) } as unknown as Response;
  97. const req = {} as Request;
  98. await AuthController.isConfigured(req, res, next);
  99. expect(res.status).toHaveBeenCalledWith(200);
  100. expect(json).toHaveBeenCalledWith({ configured: false });
  101. });
  102. it('Should return true if user is registered', async () => {
  103. // @ts-ignore
  104. fs.__createMockFiles(MOCK_USER_REGISTERED());
  105. const json = jest.fn();
  106. const res = { status: jest.fn(() => ({ json })) } as unknown as Response;
  107. const req = { user } as unknown as Request;
  108. await AuthController.isConfigured(req, res, next);
  109. expect(res.status).toHaveBeenCalledWith(200);
  110. expect(json).toHaveBeenCalledWith({ configured: true });
  111. });
  112. });