plugin-voting/__tests__/helpers/testHelper.js
2025-11-03 14:01:07 +02:00

254 lines
6.1 KiB
JavaScript

/**
* Voting Plugin Test Helper
*
* Provides utilities for testing the voting plugin endpoints
* Uses REAL PostgreSQL database - NO mocks
*/
const path = require('path');
const fs = require('fs');
/**
* Create test fixtures for voting plugin testing
*/
class VotingTestHelper {
constructor() {
this.siteId = null;
this.unitId = null;
this.userId = null;
this.personId = null;
}
/**
* Create a test site
*/
async createTestSite() {
const { siteRepository } = require('../../../src/repositories');
const siteData = {
name: `Test Voting Site ${Date.now()}`,
address: '123 Voting Street',
email: `voting-${Date.now()}@example.com`,
owner_name: 'Test Owner',
owner_email: `owner-${Date.now()}@example.com`,
phone: '+1234567890',
plan_id: null,
subscription_type: 'monthly',
is_active: true,
operational_mode: 'dev'
};
const site = await siteRepository.createSite(siteData);
this.siteId = site.id;
return site;
}
/**
* Create a test building
*/
async createTestBuilding(siteId) {
const { buildingRepository } = require('../../../src/repositories');
const buildingData = {
site_id: siteId,
name: 'Test Building',
address: '123 Test Avenue',
total_floors: 5,
total_units: 10
};
const building = await buildingRepository.createBuilding(buildingData);
return building;
}
/**
* Create a test unit
*/
async createTestUnit(siteId, buildingId) {
const { unitsRepository } = require('../../../src/repositories');
const unitData = {
site_id: siteId,
building_id: buildingId,
unit_number: `U${Date.now()}`,
type: 'residential',
size: 100,
monthly_service_fee: 500,
location: 'Floor 3'
};
const unit = await unitsRepository.createUnit(unitData);
this.unitId = unit.id;
return unit;
}
/**
* Create a test user for authentication
*/
async createTestUser(siteId) {
const { userRepository } = require('../../../src/repositories');
const bcrypt = require('bcryptjs');
const passwordHash = await bcrypt.hash('test123', 10);
const userData = {
email: `voting-${Date.now()}@test.com`,
name: 'Test Voting User',
role: 'hoa_admin',
site_id: siteId,
is_active: true,
password_hash: passwordHash
};
const user = await userRepository.createUser(userData);
this.userId = user.id;
return user;
}
/**
* Create a test person (for voting)
*/
async createTestPerson(siteId, unitId, userId = null) {
const { peopleRepository } = require('../../../src/repositories');
const personData = {
site_id: siteId,
name: `Test Person ${Date.now()}`,
email: `person-${Date.now()}@test.com`,
phone: '+1234567890',
role: 'owner',
user_id: userId,
unit_ids: [unitId],
is_active: true
};
const person = await peopleRepository.create(personData);
this.personId = person.id;
return person;
}
/**
* Create a test form
*/
async createTestForm(siteId, formType = 'board_election') {
const { FormRepository } = require('../../repositories');
const formRepo = new FormRepository();
const formData = {
site_id: siteId,
name: 'Test Board Election',
description: 'Test board election form',
form_type: formType,
form_structure: {
questions: [
{
id: 'q1',
type: 'multiple_choice',
label: 'Choose 4 Board Members *',
required: true,
multiple: true,
max_selections: 4,
options: [
{ id: 'opt1', value: 'bod 1' },
{ id: 'opt2', value: 'bod 2' },
{ id: 'opt3', value: 'bod 3' },
{ id: 'opt4', value: 'bod 4' },
{ id: 'opt5', value: 'bod 5' }
]
}
]
},
is_active: true,
created_by: this.userId || 'test-user'
};
return await formRepo.create(formData);
}
/**
* Create a test campaign
*/
async createTestCampaign(siteId, formId, campaignType = 'board_election') {
const { CampaignRepository } = require('../../repositories');
const campaignRepo = new CampaignRepository();
const startDate = new Date();
const endDate = new Date(startDate.getTime() + 7 * 24 * 60 * 60 * 1000); // 7 days later
const campaignData = {
site_id: siteId,
title: 'Test Board Election Campaign',
description: 'Test board election campaign',
campaign_type: campaignType,
form_id: formId,
status: 'active',
overall_sharing_results: 'detailed',
public_access: 'restrict',
start_date: startDate.toISOString(),
end_date: endDate.toISOString(),
recipient_type: 'all_owners',
send_email: true,
send_text: false,
send_owner_app: true,
created_by: this.userId || 'test-user'
};
return await campaignRepo.create(campaignData);
}
/**
* Create a test group
*/
async createTestGroup(siteId) {
const { GroupRepository } = require('../../repositories');
const groupRepo = new GroupRepository();
const groupData = {
site_id: siteId,
name: `Test Group ${Date.now()}`,
description: 'Test voting group',
created_by: this.userId || 'test-user'
};
return await groupRepo.create(groupData);
}
/**
* Cleanup test data
*/
async cleanup() {
// Cleanup will be handled by the schema manager
// This method is here for future use if needed
}
/**
* Get mock request object for testing
*/
getMockRequest(siteId, userId) {
return {
user: {
id: userId,
siteId: siteId,
role: 'hoa_admin'
},
query: {
site_id: siteId
},
body: {}
};
}
/**
* Get mock response object for testing
*/
getMockResponse() {
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
send: jest.fn().mockReturnThis()
};
return res;
}
}
module.exports = VotingTestHelper;