/** * @fileoverview Tests for public registration MCP tools * Tests all public registration endpoints for different user types */ import { describe, test, expect, beforeEach, afterEach } from '@jest/globals'; import { mockFactory } from '../mocks/mockFactory.js'; describe('Public Registration Tools', () => { let mockEnv; let toolGenerator; beforeEach(() => { mockEnv = mockFactory.createMockEnvironment({ authTypes: ['public'], enableHttpMocks: true, enableHealthcareMocks: true }); toolGenerator = mockEnv.toolGenerator; }); afterEach(() => { mockFactory.resetAllMocks(); }); describe('public_create_emrApiproviderRegister', () => { test('should successfully register a new provider', async () => { // Setup const toolName = 'public_create_emrApiproviderRegister'; const parameters = { firstName: 'Dr. John', lastName: 'Smith', username: 'drsmith', emailAddress: 'dr.smith@test.com', textMessageNumber: '555-0123', newUserPassword: 'SecurePass123!', company_name: 'Test Medical Center', on_your_domain: true }; // Mock successful registration mockFactory.httpMocks.mockRequest('POST', '/emr-api/provider-register', { status: 201, data: { success: true, provider: { id: 'provider_123', firstName: 'Dr. John', lastName: 'Smith', emailAddress: 'dr.smith@test.com', username: 'drsmith' }, message: 'Provider registered successfully' } }); // Execute const result = await toolGenerator.executeTool(toolName, parameters); // Assert expect(result.success).toBe(true); expect(result.data.provider.firstName).toBe('Dr. John'); expect(result.data.provider.emailAddress).toBe('dr.smith@test.com'); }); test('should validate required provider registration fields', async () => { const toolName = 'public_create_emrApiproviderRegister'; // Test missing required fields const requiredFields = [ 'firstName', 'lastName', 'username', 'emailAddress', 'textMessageNumber', 'newUserPassword', 'company_name' ]; for (const field of requiredFields) { const incompleteParams = { firstName: 'Dr. John', lastName: 'Smith', username: 'drsmith', emailAddress: 'dr.smith@test.com', textMessageNumber: '555-0123', newUserPassword: 'SecurePass123!', company_name: 'Test Medical Center' }; delete incompleteParams[field]; await expect(toolGenerator.executeTool(toolName, incompleteParams)) .rejects.toThrow(); } }); test('should handle duplicate email registration', async () => { const toolName = 'public_create_emrApiproviderRegister'; const parameters = { firstName: 'Dr. John', lastName: 'Smith', username: 'drsmith', emailAddress: 'existing@test.com', textMessageNumber: '555-0123', newUserPassword: 'SecurePass123!', company_name: 'Test Medical Center' }; // Mock duplicate email error mockFactory.httpMocks.mockRequest('POST', '/emr-api/provider-register', null, true, { response: { status: 409, data: { error: 'Email already exists' } } }); await expect(toolGenerator.executeTool(toolName, parameters)) .rejects.toThrow(); }); }); describe('public_create_registerPatient', () => { test('should successfully register a new patient', async () => { // Setup const toolName = 'public_create_registerPatient'; const parameters = { first_name: 'Jane', last_name: 'Doe', email: 'jane.doe@test.com', phone_no: '555-0456', dob: '1990-01-01', gender: 'Female', provider_id: 123, preferredPhone: '555-0456', password: 'PatientPass123!', username: 'janedoe', isportalAccess: true }; // Mock successful patient registration mockFactory.httpMocks.mockRequest('POST', '/api/register-patients', { status: 201, data: { success: true, patient: { id: 'patient_456', first_name: 'Jane', last_name: 'Doe', email: 'jane.doe@test.com', isportalAccess: true }, message: 'Patient registered successfully' } }); // Execute const result = await toolGenerator.executeTool(toolName, parameters); // Assert expect(result.success).toBe(true); expect(result.data.patient.first_name).toBe('Jane'); expect(result.data.patient.email).toBe('jane.doe@test.com'); expect(result.data.patient.isportalAccess).toBe(true); }); test('should validate patient data format', async () => { const toolName = 'public_create_registerPatient'; // Test invalid email format const invalidEmailParams = { first_name: 'Jane', last_name: 'Doe', email: 'invalid-email', phone_no: '555-0456', dob: '1990-01-01', gender: 'Female', password: 'PatientPass123!' }; await expect(toolGenerator.executeTool(toolName, invalidEmailParams)) .rejects.toThrow(); // Test invalid date format const invalidDateParams = { first_name: 'Jane', last_name: 'Doe', email: 'jane@test.com', phone_no: '555-0456', dob: 'invalid-date', gender: 'Female', password: 'PatientPass123!' }; await expect(toolGenerator.executeTool(toolName, invalidDateParams)) .rejects.toThrow(); }); }); describe('public_create_affiliateRegisterApi', () => { test('should successfully register a new affiliate', async () => { // Setup const toolName = 'public_create_affiliateRegisterApi'; const parameters = { first_name: 'Alice', last_name: 'Johnson', phone_no: '555-0789', email: 'alice.johnson@test.com', dob: '1985-05-15', gender: 'Female', partner_email: 'partner@test.com' }; // Mock successful affiliate registration mockFactory.httpMocks.mockRequest('POST', '/api/affiliate-register-api', { status: 201, data: { success: true, affiliate: { id: 'affiliate_789', first_name: 'Alice', last_name: 'Johnson', email: 'alice.johnson@test.com' }, message: 'Affiliate registered successfully' } }); // Execute const result = await toolGenerator.executeTool(toolName, parameters); // Assert expect(result.success).toBe(true); expect(result.data.affiliate.first_name).toBe('Alice'); expect(result.data.affiliate.email).toBe('alice.johnson@test.com'); }); }); describe('public_create_partnerRegisterApi', () => { test('should successfully register a new partner', async () => { // Setup const toolName = 'public_create_partnerRegisterApi'; const parameters = { first_name: 'Bob', last_name: 'Wilson', phone_no: '555-0321', email: 'bob.wilson@test.com', dob: '1980-12-10', gender: 'Male', password: 'PartnerPass123!' }; // Mock successful partner registration mockFactory.httpMocks.mockRequest('POST', '/api/partner-register-api', { status: 201, data: { success: true, partner: { id: 'partner_321', first_name: 'Bob', last_name: 'Wilson', email: 'bob.wilson@test.com' }, message: 'Partner registered successfully' } }); // Execute const result = await toolGenerator.executeTool(toolName, parameters); // Assert expect(result.success).toBe(true); expect(result.data.partner.first_name).toBe('Bob'); expect(result.data.partner.email).toBe('bob.wilson@test.com'); }); }); describe('public_create_networkregister', () => { test('should successfully register a new network user', async () => { // Setup const toolName = 'public_create_networkregister'; const parameters = { first_name: 'Carol', last_name: 'Davis', phone_no: '555-0654', email: 'carol.davis@test.com', dob: '1992-03-20', gender: 'Female', password: 'NetworkPass123!', partner_id: 'partner_123' }; // Mock successful network registration mockFactory.httpMocks.mockRequest('POST', '/api/network/register', { status: 201, data: { success: true, network_user: { id: 'network_654', first_name: 'Carol', last_name: 'Davis', email: 'carol.davis@test.com' }, message: 'Network user registered successfully' } }); // Execute const result = await toolGenerator.executeTool(toolName, parameters); // Assert expect(result.success).toBe(true); expect(result.data.network_user.first_name).toBe('Carol'); expect(result.data.network_user.email).toBe('carol.davis@test.com'); }); }); describe('Registration Security Tests', () => { test('should validate password strength', async () => { const toolName = 'public_create_emrApiproviderRegister'; const weakPasswordParams = { firstName: 'Dr. John', lastName: 'Smith', username: 'drsmith', emailAddress: 'dr.smith@test.com', textMessageNumber: '555-0123', newUserPassword: '123', // Weak password company_name: 'Test Medical Center' }; await expect(toolGenerator.executeTool(toolName, weakPasswordParams)) .rejects.toThrow(); }); test('should sanitize input data', async () => { const toolName = 'public_create_registerPatient'; const maliciousParams = { first_name: '', last_name: 'Doe', email: 'test@test.com', phone_no: '555-0456', dob: '1990-01-01', gender: 'Female', password: 'ValidPass123!' }; // Mock successful registration (input should be sanitized) mockFactory.httpMocks.mockRequest('POST', '/api/register-patients', { status: 201, data: { success: true, patient: { id: 'patient_123', first_name: 'alert("xss")', // Sanitized email: 'test@test.com' } } }); const result = await toolGenerator.executeTool(toolName, maliciousParams); // Verify XSS attempt was sanitized expect(result.data.patient.first_name).not.toContain('