|
| 1 | +const mockUserFindUnique = jest.fn(); |
| 2 | +const mockMemberFindMany = jest.fn(); |
| 3 | +const mockMemberCount = jest.fn(); |
| 4 | +const mockInvitationFindFirst = jest.fn(); |
| 5 | + |
| 6 | +jest.mock('@db', () => ({ |
| 7 | + db: { |
| 8 | + user: { findUnique: (...a: unknown[]) => mockUserFindUnique(...a) }, |
| 9 | + member: { |
| 10 | + findMany: (...a: unknown[]) => mockMemberFindMany(...a), |
| 11 | + count: (...a: unknown[]) => mockMemberCount(...a), |
| 12 | + }, |
| 13 | + invitation: { findFirst: (...a: unknown[]) => mockInvitationFindFirst(...a) }, |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +// Both guards import ./auth.server, which validates SECRET_KEY and pulls in |
| 18 | +// better-auth/redis at module load. Stub them so importing the controller is |
| 19 | +// hermetic — the test calls the method directly and never runs the guards. |
| 20 | +jest.mock('./hybrid-auth.guard', () => ({ HybridAuthGuard: class {} })); |
| 21 | +jest.mock('./permission.guard', () => ({ |
| 22 | + PermissionGuard: class {}, |
| 23 | + PERMISSIONS_KEY: 'permissions', |
| 24 | +})); |
| 25 | + |
| 26 | +import { AuthController } from './auth.controller'; |
| 27 | +import type { AuthContext } from './types'; |
| 28 | + |
| 29 | +const sessionContext = (): AuthContext => ({ |
| 30 | + organizationId: 'org_1', |
| 31 | + authType: 'session', |
| 32 | + isApiKey: false, |
| 33 | + isPlatformAdmin: false, |
| 34 | + userRoles: null, |
| 35 | + userId: 'user_1', |
| 36 | + userEmail: 'user@example.com', |
| 37 | +}); |
| 38 | + |
| 39 | +describe('AuthController.getMe — hasInactiveMembership (CS-569)', () => { |
| 40 | + const controller = new AuthController(); |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + jest.clearAllMocks(); |
| 44 | + mockUserFindUnique.mockResolvedValue({ |
| 45 | + id: 'user_1', |
| 46 | + email: 'user@example.com', |
| 47 | + name: 'User', |
| 48 | + image: null, |
| 49 | + role: 'user', |
| 50 | + }); |
| 51 | + mockInvitationFindFirst.mockResolvedValue(null); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns hasInactiveMembership: false for a genuinely new user (no memberships at all)', async () => { |
| 55 | + mockMemberFindMany.mockResolvedValue([]); |
| 56 | + mockMemberCount.mockResolvedValue(0); |
| 57 | + |
| 58 | + const res = await controller.getMe(sessionContext()); |
| 59 | + |
| 60 | + expect(res.organizations).toEqual([]); |
| 61 | + expect(res.hasInactiveMembership).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns hasInactiveMembership: true for an offboarded user (no active org, only deactivated memberships)', async () => { |
| 65 | + mockMemberFindMany.mockResolvedValue([]); // no ACTIVE memberships |
| 66 | + mockMemberCount.mockResolvedValue(1); // one deactivated/inactive membership |
| 67 | + |
| 68 | + const res = await controller.getMe(sessionContext()); |
| 69 | + |
| 70 | + expect(res.organizations).toEqual([]); |
| 71 | + expect(res.hasInactiveMembership).toBe(true); |
| 72 | + // The count that distinguishes "offboarded" from "new" must match |
| 73 | + // memberships that are deactivated OR no longer active. |
| 74 | + expect(mockMemberCount).toHaveBeenCalledWith({ |
| 75 | + where: { |
| 76 | + userId: 'user_1', |
| 77 | + OR: [{ deactivated: true }, { isActive: false }], |
| 78 | + }, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns hasInactiveMembership: false for an active member', async () => { |
| 83 | + mockMemberFindMany.mockResolvedValue([ |
| 84 | + { |
| 85 | + id: 'member_1', |
| 86 | + role: 'owner', |
| 87 | + organizationId: 'org_1', |
| 88 | + organization: { |
| 89 | + id: 'org_1', |
| 90 | + name: 'Org', |
| 91 | + logo: null, |
| 92 | + onboardingCompleted: true, |
| 93 | + hasAccess: true, |
| 94 | + createdAt: new Date(), |
| 95 | + }, |
| 96 | + }, |
| 97 | + ]); |
| 98 | + mockMemberCount.mockResolvedValue(0); |
| 99 | + |
| 100 | + const res = await controller.getMe(sessionContext()); |
| 101 | + |
| 102 | + expect(res.organizations).toHaveLength(1); |
| 103 | + expect(res.hasInactiveMembership).toBe(false); |
| 104 | + }); |
| 105 | +}); |
0 commit comments