|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import { jest, describe, test, expect, beforeEach } from '@jest/globals'; |
| 5 | + |
| 6 | +describe('auth.controller signout:', () => { |
| 7 | + beforeEach(() => { |
| 8 | + jest.resetModules(); |
| 9 | + |
| 10 | + jest.unstable_mockModule('../../../lib/services/logger.js', () => ({ |
| 11 | + default: { warn: jest.fn(), error: jest.fn(), info: jest.fn() }, |
| 12 | + })); |
| 13 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 14 | + default: { |
| 15 | + sign: { up: true, in: true }, |
| 16 | + jwt: { secret: 's', expiresIn: 3600 }, |
| 17 | + cookie: { secure: true, sameSite: 'lax' }, |
| 18 | + organizations: { enabled: false }, |
| 19 | + app: { title: 'Test', contact: 'a@b.com' }, |
| 20 | + }, |
| 21 | + })); |
| 22 | + jest.unstable_mockModule('../../../modules/users/services/users.service.js', () => ({ |
| 23 | + default: { create: jest.fn(), getBrut: jest.fn(), update: jest.fn(), remove: jest.fn(), search: jest.fn() }, |
| 24 | + })); |
| 25 | + jest.unstable_mockModule('../../../modules/users/repositories/users.repository.js', () => ({ |
| 26 | + default: { update: jest.fn() }, |
| 27 | + })); |
| 28 | + jest.unstable_mockModule('../../../modules/organizations/services/organizations.service.js', () => ({ |
| 29 | + default: { handleSignupOrganization: jest.fn() }, |
| 30 | + })); |
| 31 | + jest.unstable_mockModule('../../../modules/organizations/services/organizations.crud.service.js', () => ({ |
| 32 | + default: { autoSetCurrentOrganization: jest.fn() }, |
| 33 | + })); |
| 34 | + jest.unstable_mockModule('../../../modules/organizations/services/organizations.membership.service.js', () => ({ |
| 35 | + default: { findByUserAndOrganization: jest.fn(), listPendingByUser: jest.fn().mockResolvedValue([]) }, |
| 36 | + })); |
| 37 | + jest.unstable_mockModule('../../../modules/users/models/users.schema.js', () => ({ |
| 38 | + default: { User: {} }, |
| 39 | + })); |
| 40 | + jest.unstable_mockModule('../../../lib/middlewares/model.js', () => ({ |
| 41 | + default: { getResultFromZod: jest.fn(), checkError: jest.fn() }, |
| 42 | + })); |
| 43 | + jest.unstable_mockModule('../../../lib/middlewares/policy.js', () => ({ |
| 44 | + default: { defineAbilityFor: jest.fn().mockResolvedValue({}) }, |
| 45 | + })); |
| 46 | + jest.unstable_mockModule('../../../lib/helpers/mailer/index.js', () => ({ |
| 47 | + default: { isConfigured: jest.fn().mockReturnValue(false), sendMail: jest.fn() }, |
| 48 | + })); |
| 49 | + jest.unstable_mockModule('../../../lib/helpers/responses.js', () => ({ |
| 50 | + default: { |
| 51 | + success: jest.fn().mockReturnValue(jest.fn()), |
| 52 | + error: jest.fn().mockReturnValue(jest.fn()), |
| 53 | + }, |
| 54 | + })); |
| 55 | + jest.unstable_mockModule('../../../lib/helpers/errors.js', () => ({ |
| 56 | + default: { getMessage: jest.fn().mockReturnValue('error') }, |
| 57 | + })); |
| 58 | + jest.unstable_mockModule('../../../lib/helpers/AppError.js', () => ({ |
| 59 | + default: class AppError extends Error { |
| 60 | + constructor(msg, opts) { |
| 61 | + super(msg); |
| 62 | + this.code = opts?.code; |
| 63 | + this.details = opts?.details; |
| 64 | + } |
| 65 | + }, |
| 66 | + })); |
| 67 | + jest.unstable_mockModule('../../../lib/helpers/abilities.js', () => ({ |
| 68 | + default: jest.fn().mockReturnValue([]), |
| 69 | + })); |
| 70 | + jest.unstable_mockModule('../../../lib/helpers/getBaseUrl.js', () => ({ |
| 71 | + default: jest.fn().mockReturnValue('http://localhost:3000'), |
| 72 | + })); |
| 73 | + jest.unstable_mockModule('../../../lib/services/analytics.js', () => ({ |
| 74 | + default: { identify: jest.fn(), groupIdentify: jest.fn() }, |
| 75 | + })); |
| 76 | + }); |
| 77 | + |
| 78 | + test('clears TOKEN cookie with options mirroring tokenCookieOptions and returns success', async () => { |
| 79 | + const { default: AuthController } = await import('../../../modules/auth/controllers/auth.controller.js'); |
| 80 | + |
| 81 | + const cleared = []; |
| 82 | + const res = { |
| 83 | + clearCookie: (name, opts) => { cleared.push({ name, opts }); return res; }, |
| 84 | + status: jest.fn().mockReturnThis(), |
| 85 | + json: jest.fn().mockReturnThis(), |
| 86 | + }; |
| 87 | + |
| 88 | + AuthController.signout({}, res); |
| 89 | + |
| 90 | + expect(cleared).toEqual([{ name: 'TOKEN', opts: { httpOnly: true, secure: true, sameSite: 'lax' } }]); |
| 91 | + expect(res.status).toHaveBeenCalledWith(200); |
| 92 | + expect(res.json).toHaveBeenCalledWith({ type: 'success', message: 'Signed out' }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments