|
| 1 | +import request from 'supertest'; |
| 2 | +import { describe, it, expect, beforeAll, beforeEach, vi } from 'vitest'; |
| 3 | +import { createApp } from '../../../src/app'; |
| 4 | +import { Application } from 'express'; |
| 5 | + |
| 6 | +import { buildUser } from '../../factories/users/userFactory.js'; |
| 7 | + |
| 8 | +// 🔥 mocks |
| 9 | +vi.mock('../../../src/models/users.js', () => ({ |
| 10 | + User: { |
| 11 | + findOne: vi.fn(), |
| 12 | + create: vi.fn(), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../../../src/models/authEvents.js', () => ({ |
| 17 | + AuthEvent: { |
| 18 | + create: vi.fn(), |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock('../../../src/lib/token.js', () => ({ |
| 23 | + signEphemeralToken: vi.fn(), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock('../../../src/utils/otp.js', () => ({ |
| 27 | + generatePhoneOTP: vi.fn(), |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock('../../../src/config/getSystemConfig.js', () => ({ |
| 31 | + getSystemConfig: vi.fn(), |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock('../../../src/services/authEventService.js', () => ({ |
| 35 | + AuthEventService: { |
| 36 | + log: vi.fn(), |
| 37 | + notificationSent: vi.fn(), |
| 38 | + }, |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock('../../../src/lib/cookie.js', () => ({ |
| 42 | + setAuthCookies: vi.fn(), |
| 43 | +})); |
| 44 | + |
| 45 | +// imports after mocks |
| 46 | +import { User } from '../../../src/models/users.js'; |
| 47 | +import { signEphemeralToken } from '../../../src/lib/token.js'; |
| 48 | +import { getSystemConfig } from '../../../src/config/getSystemConfig.js'; |
| 49 | +import { buildRegistrationRequest } from '../../factories/requestFactory.js'; |
| 50 | + |
| 51 | +let app: Application; |
| 52 | + |
| 53 | +beforeAll(async () => { |
| 54 | + app = await createApp(); |
| 55 | +}); |
| 56 | + |
| 57 | +beforeEach(() => { |
| 58 | + vi.clearAllMocks(); |
| 59 | + |
| 60 | + (getSystemConfig as any).mockResolvedValue({ |
| 61 | + default_roles: ['user'], |
| 62 | + }); |
| 63 | + |
| 64 | + (signEphemeralToken as any).mockResolvedValue('mock-token'); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('POST /registration/register', () => { |
| 68 | + // ✅ Happy path - new user |
| 69 | + it('creates a new user', async () => { |
| 70 | + (User.findOne as any).mockResolvedValue(null); |
| 71 | + |
| 72 | + const user = buildUser(); |
| 73 | + |
| 74 | + (User.create as any).mockResolvedValue(user); |
| 75 | + |
| 76 | + const res = await request(app).post('/registration/register').send(buildRegistrationRequest()); |
| 77 | + |
| 78 | + expect(res.status).toBe(200); |
| 79 | + expect(res.body.message).toBe('Success'); |
| 80 | + |
| 81 | + expect(User.create).toHaveBeenCalled(); |
| 82 | + expect(signEphemeralToken).toHaveBeenCalledWith(user.id); |
| 83 | + }); |
| 84 | + |
| 85 | + // ✅ Existing user |
| 86 | + it('handles existing user', async () => { |
| 87 | + const user = buildUser(); |
| 88 | + |
| 89 | + (User.findOne as any).mockResolvedValue(user); |
| 90 | + |
| 91 | + const res = await request(app).post('/registration/register').send(buildRegistrationRequest()); |
| 92 | + |
| 93 | + expect(res.status).toBe(200); |
| 94 | + |
| 95 | + expect(User.create).not.toHaveBeenCalled(); |
| 96 | + expect(signEphemeralToken).toHaveBeenCalledWith(user.id); |
| 97 | + }); |
| 98 | + |
| 99 | + // ❌ Missing email |
| 100 | + it('fails without email', async () => { |
| 101 | + const res = await request(app).post('/registration/register').send({ phone: '+15555555555' }); |
| 102 | + |
| 103 | + expect(res.status).toBe(400); |
| 104 | + }); |
| 105 | + |
| 106 | + // ❌ Missing phone |
| 107 | + it('fails without phone', async () => { |
| 108 | + const res = await request(app) |
| 109 | + .post('/registration/register') |
| 110 | + .send({ email: 'test@example.com' }); |
| 111 | + |
| 112 | + expect(res.status).toBe(400); |
| 113 | + }); |
| 114 | + |
| 115 | + // ❌ Invalid email |
| 116 | + it('fails invalid email', async () => { |
| 117 | + const res = await request(app) |
| 118 | + .post('/registration/register') |
| 119 | + .send(buildRegistrationRequest({ email: 'bad' })); |
| 120 | + |
| 121 | + expect(res.status).toBe(400); |
| 122 | + }); |
| 123 | + |
| 124 | + // 💥 Error case |
| 125 | + it('handles unexpected errors', async () => { |
| 126 | + (User.findOne as any).mockRejectedValue(new Error('boom')); |
| 127 | + |
| 128 | + const res = await request(app).post('/registration/register').send(buildRegistrationRequest()); |
| 129 | + |
| 130 | + expect(res.status).toBe(500); |
| 131 | + }); |
| 132 | +}); |
0 commit comments