UNPKG

n8n-nodes-awx

Version:

n8n node to interact with Ansible AWX/Tower with improved type safety

809 lines 41.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const WorkflowJobTemplateResources_1 = require("../src/resources/WorkflowJobTemplateResources"); const mockApiClient = { get: jest.fn(), post: jest.fn(), put: jest.fn(), delete: jest.fn(), }; describe('WorkflowJobTemplateResources', () => { let resources; const mockWorkflowJobTemplate = { id: 1, type: 'workflow_job_template', url: '/api/v2/workflow_job_templates/1/', name: 'Test Workflow', description: 'A test workflow job template', created: '2023-01-01T00:00:00Z', modified: '2023-01-02T00:00:00Z', extra_vars: '{}', survey_enabled: false, allow_simultaneous: false, summary_fields: { last_job: { id: 100, status: 'successful', finished: '2023-01-03T00:00:00Z', }, organization: { id: 1, name: 'Default', description: 'Default organization', }, created_by: { id: 1, username: 'admin', }, modified_by: { id: 1, username: 'admin', }, user_capabilities: { edit: true, delete: true, start: true, }, labels: { results: [], count: 0, }, }, }; beforeEach(() => { jest.clearAllMocks(); resources = new WorkflowJobTemplateResources_1.WorkflowJobTemplateResources(mockApiClient); }); describe('get', () => { it('should throw an error when ID is not provided', async () => { await expect(resources.get(undefined)) .rejects .toThrow('Workflow Job Template ID is required'); }); it('should call the API with the correct URL', async () => { mockApiClient.get.mockResolvedValueOnce({ data: mockWorkflowJobTemplate }); const result = await resources.get(1); expect(mockApiClient.get).toHaveBeenCalledWith('/api/v2/workflow_job_templates/1/'); expect(result).toEqual(mockWorkflowJobTemplate); }); }); describe('getByName', () => { it('should return null when no workflow is found', async () => { mockApiClient.get.mockResolvedValueOnce({ data: { results: [], count: 0, } }); const result = await resources.getByName('Nonexistent'); expect(result).toBeNull(); expect(mockApiClient.get).toHaveBeenCalledWith('/api/v2/workflow_job_templates/?name=Nonexistent'); }); it('should throw an error when multiple workflows are found', async () => { mockApiClient.get.mockResolvedValueOnce({ data: { results: [ mockWorkflowJobTemplate, { ...mockWorkflowJobTemplate, id: 2 } ], count: 2, }, }); await expect(resources.getByName('Duplicate')) .rejects .toThrow('Multiple workflow job templates found with name: Duplicate'); }); it('should return the workflow when exactly one is found', async () => { mockApiClient.get.mockResolvedValueOnce({ data: { results: [mockWorkflowJobTemplate], count: 1, }, }); const result = await resources.getByName('Test Workflow'); expect(result).toEqual(mockWorkflowJobTemplate); }); }); describe('list', () => { it('should return a list of workflow job templates', async () => { const mockList = [ mockWorkflowJobTemplate, { ...mockWorkflowJobTemplate, id: 2, name: 'Another Workflow' }, ]; mockApiClient.get.mockResolvedValueOnce({ data: { results: mockList, count: mockList.length, }, }); const result = await resources.list(); expect(mockApiClient.get).toHaveBeenCalledWith('/api/v2/workflow_job_templates/'); expect(result).toEqual(mockList); }); }); describe('create', () => { it('should create a new workflow job template', async () => { const newTemplateData = { name: 'New Workflow', description: 'A new workflow job template', organization: 1, inventory: 1, }; const createdTemplate = { ...mockWorkflowJobTemplate, ...newTemplateData, id: 100, }; mockApiClient.post.mockResolvedValueOnce({ data: createdTemplate, }); const result = await resources.create(newTemplateData); expect(mockApiClient.post).toHaveBeenCalledWith('/api/v2/workflow_job_templates/', newTemplateData); expect(result).toEqual(createdTemplate); }); it('should throw an error when required fields are missing', async () => { const invalidData = {}; await expect(resources.create(invalidData)) .rejects .toThrow('Name is required to create a workflow job template'); }); }); describe('update', () => { it('should update an existing workflow job template', async () => { const templateId = 1; const updateData = { name: 'Updated Workflow Name', description: 'Updated description', }; const updatedTemplate = { ...mockWorkflowJobTemplate, ...updateData, }; mockApiClient.put.mockResolvedValueOnce({ data: updatedTemplate, }); const result = await resources.update(templateId, updateData); expect(mockApiClient.put).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/`, updateData); expect(result).toEqual(updatedTemplate); }); it('should throw an error when ID is not provided', async () => { await expect(resources.update(undefined, { name: 'Test' })).rejects.toThrow('Workflow Job Template ID is required'); }); }); describe('delete', () => { it('should delete an existing workflow job template', async () => { const templateId = 1; mockApiClient.delete.mockResolvedValueOnce(undefined); await resources.delete(templateId); expect(mockApiClient.delete).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/`); }); it('should throw an error when ID is not provided', async () => { await expect(resources.delete(undefined)).rejects.toThrow('Workflow Job Template ID is required'); }); }); describe('launch', () => { it('should launch a workflow job template with the provided parameters', async () => { const templateId = 1; const launchParams = { limit: 'webservers', extra_vars: { key: 'value' } }; const mockLaunchResponse = { id: 100, type: 'workflow_job', status: 'pending', workflow_job_template: templateId, extra_vars: launchParams.extra_vars, limit: launchParams.limit }; mockApiClient.post.mockResolvedValueOnce({ data: mockLaunchResponse }); const result = await resources.launch(templateId, launchParams); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/launch/`, launchParams); expect(result).toEqual(mockLaunchResponse); }); it('should launch a workflow job template with empty parameters if none provided', async () => { const templateId = 1; const mockLaunchResponse = { id: 100, type: 'workflow_job', status: 'pending', workflow_job_template: templateId, }; mockApiClient.post.mockResolvedValueOnce({ data: mockLaunchResponse }); const result = await resources.launch(templateId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/launch/`, {}); expect(result).toEqual(mockLaunchResponse); }); it('should throw an error when ID is not provided', async () => { await expect(resources.launch(undefined)).rejects.toThrow('Workflow Job Template ID is required'); }); }); describe('labels', () => { const templateId = 1; const labelId = 10; const mockLabel = { id: labelId, name: 'test-label', organization: 1 }; describe('associateLabel', () => { it('should associate a label with a workflow job template', async () => { var _a, _b; mockApiClient.post.mockResolvedValueOnce({ data: { ...mockWorkflowJobTemplate, summary_fields: { ...mockWorkflowJobTemplate.summary_fields, labels: { results: [mockLabel], count: 1 } } } }); const result = await resources.associateLabel(templateId, labelId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/labels/`, { id: labelId }); expect((_b = (_a = result.summary_fields) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b.results).toContainEqual(mockLabel); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.associateLabel(undefined, labelId)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when label ID is not provided', async () => { await expect(resources.associateLabel(templateId, undefined)).rejects.toThrow('Label ID is required'); }); }); describe('disassociateLabel', () => { it('should disassociate a label from a workflow job template', async () => { mockApiClient.post.mockResolvedValueOnce({ data: { ...mockWorkflowJobTemplate, summary_fields: { ...mockWorkflowJobTemplate.summary_fields, labels: { results: [], count: 0 } } } }); await resources.disassociateLabel(templateId, labelId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/labels/`, { id: labelId, disassociate: true }); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.disassociateLabel(undefined, labelId)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when label ID is not provided', async () => { await expect(resources.disassociateLabel(templateId, undefined)).rejects.toThrow('Label ID is required'); }); }); }); describe('schedules', () => { const templateId = 1; const mockSchedule = { id: 10, type: 'schedule', url: '/api/v2/schedules/10/', name: 'Test Schedule', rrule: 'DTSTART;TZID=UTC:20230101T000000 RRULE:FREQ=DAILY;INTERVAL=1', enabled: true, extra_data: {}, summary_fields: { workflow_job_template: { id: templateId, name: 'Test Workflow' } } }; describe('getSchedules', () => { it('should get all schedules for a workflow job template', async () => { mockApiClient.get.mockResolvedValueOnce({ data: { results: [mockSchedule], count: 1 } }); const result = await resources.getSchedules(templateId); expect(mockApiClient.get).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/schedules/`); expect(result).toEqual([mockSchedule]); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.getSchedules(undefined)).rejects.toThrow('Workflow Job Template ID is required'); }); }); describe('createSchedule', () => { const scheduleData = { name: 'New Schedule', rrule: 'DTSTART;TZID=UTC:20230101T000000 RRULE:FREQ=DAILY;INTERVAL=1', enabled: true }; it('should create a new schedule for a workflow job template', async () => { mockApiClient.post.mockResolvedValueOnce({ data: mockSchedule }); const result = await resources.createSchedule(templateId, scheduleData); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/schedules/`, scheduleData); expect(result).toEqual(mockSchedule); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.createSchedule(undefined, scheduleData)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when schedule data is not provided', async () => { await expect(resources.createSchedule(templateId, undefined)).rejects.toThrow('Schedule data is required'); }); }); }); describe('error notification templates', () => { const templateId = 1; const notificationId = 10; const mockNotification = { id: notificationId, name: 'Test Error Notification', description: 'Test error notification description', notification_type: 'email', notification_configuration: { username: 'test@example.com', password: '$encrypted$', host: 'smtp.example.com', port: 587, use_tls: true, use_ssl: false, timeout: 30, from_email: 'noreply@example.com', recipients: ['admin@example.com'] }, organization: 1, }; describe('getNotificationTemplatesError', () => { it('should get all error notification templates for a workflow job template', async () => { mockApiClient.get.mockResolvedValueOnce({ data: { results: [mockNotification], count: 1 } }); const result = await resources.getNotificationTemplatesError(templateId); expect(mockApiClient.get).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_error/`); expect(result).toEqual([mockNotification]); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.getNotificationTemplatesError(undefined)).rejects.toThrow('Workflow Job Template ID is required'); }); }); describe('associateNotificationTemplateError', () => { it('should associate an error notification template with a workflow job template', async () => { var _a, _b, _c, _d; mockApiClient.post.mockResolvedValueOnce({ data: { ...mockWorkflowJobTemplate, summary_fields: { ...mockWorkflowJobTemplate.summary_fields, notification_templates_error: { results: [{ id: notificationId, name: mockNotification.name, description: mockNotification.description, notification_type: mockNotification.notification_type }], count: 1 } } } }); const result = await resources.associateNotificationTemplateError(templateId, notificationId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_error/`, { id: notificationId }); expect((_b = (_a = result.summary_fields) === null || _a === void 0 ? void 0 : _a.notification_templates_error) === null || _b === void 0 ? void 0 : _b.results).toHaveLength(1); expect((_d = (_c = result.summary_fields) === null || _c === void 0 ? void 0 : _c.notification_templates_error) === null || _d === void 0 ? void 0 : _d.results[0].id).toBe(notificationId); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.associateNotificationTemplateError(undefined, notificationId)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when notification template ID is not provided', async () => { await expect(resources.associateNotificationTemplateError(templateId, undefined)).rejects.toThrow('Notification Template ID is required'); }); }); describe('disassociateNotificationTemplateError', () => { it('should disassociate an error notification template from a workflow job template', async () => { mockApiClient.post.mockResolvedValueOnce({ data: { ...mockWorkflowJobTemplate, summary_fields: { ...mockWorkflowJobTemplate.summary_fields, notification_templates_error: { results: [], count: 0 } } } }); await resources.disassociateNotificationTemplateError(templateId, notificationId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_error/`, { id: notificationId, disassociate: true }); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.disassociateNotificationTemplateError(undefined, notificationId)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when notification template ID is not provided', async () => { await expect(resources.disassociateNotificationTemplateError(templateId, undefined)).rejects.toThrow('Notification Template ID is required'); }); }); }); describe('started notification templates', () => { const templateId = 1; const notificationId = 20; const mockNotification = { id: notificationId, name: 'Test Started Notification', description: 'Test started notification description', notification_type: 'slack', notification_configuration: { channels: ['#general'], token: '$encrypted$', hex_color: '#36a64f', }, organization: 1, }; describe('getNotificationTemplatesStarted', () => { it('should get all started notification templates for a workflow job template', async () => { mockApiClient.get.mockResolvedValueOnce({ data: { results: [mockNotification], count: 1 } }); const result = await resources.getNotificationTemplatesStarted(templateId); expect(mockApiClient.get).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_started/`); expect(result).toEqual([mockNotification]); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.getNotificationTemplatesStarted(undefined)).rejects.toThrow('Workflow Job Template ID is required'); }); }); describe('associateNotificationTemplateStarted', () => { it('should associate a started notification template with a workflow job template', async () => { var _a, _b, _c, _d; mockApiClient.post.mockResolvedValueOnce({ data: { ...mockWorkflowJobTemplate, summary_fields: { ...mockWorkflowJobTemplate.summary_fields, notification_templates_started: { results: [{ id: notificationId, name: mockNotification.name, description: mockNotification.description, notification_type: mockNotification.notification_type }], count: 1 } } } }); const result = await resources.associateNotificationTemplateStarted(templateId, notificationId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_started/`, { id: notificationId }); expect((_b = (_a = result.summary_fields) === null || _a === void 0 ? void 0 : _a.notification_templates_started) === null || _b === void 0 ? void 0 : _b.results).toHaveLength(1); expect((_d = (_c = result.summary_fields) === null || _c === void 0 ? void 0 : _c.notification_templates_started) === null || _d === void 0 ? void 0 : _d.results[0].id).toBe(notificationId); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.associateNotificationTemplateStarted(undefined, notificationId)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when notification template ID is not provided', async () => { await expect(resources.associateNotificationTemplateStarted(templateId, undefined)).rejects.toThrow('Notification Template ID is required'); }); }); describe('disassociateNotificationTemplateStarted', () => { it('should disassociate a started notification template from a workflow job template', async () => { mockApiClient.post.mockResolvedValueOnce({ data: { ...mockWorkflowJobTemplate, summary_fields: { ...mockWorkflowJobTemplate.summary_fields, notification_templates_started: { results: [], count: 0 } } } }); await resources.disassociateNotificationTemplateStarted(templateId, notificationId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_started/`, { id: notificationId, disassociate: true }); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.disassociateNotificationTemplateStarted(undefined, notificationId)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when notification template ID is not provided', async () => { await expect(resources.disassociateNotificationTemplateStarted(templateId, undefined)).rejects.toThrow('Notification Template ID is required'); }); }); }); describe('success notification templates', () => { const templateId = 1; const notificationId = 30; const mockNotification = { id: notificationId, name: 'Test Success Notification', description: 'Test success notification description', notification_type: 'webhook', notification_configuration: { url: 'https://example.com/webhook', http_method: 'POST', headers: { 'Content-Type': 'application/json', }, disable_ssl_verification: false, }, organization: 1, }; describe('getNotificationTemplatesSuccess', () => { it('should get all success notification templates for a workflow job template', async () => { mockApiClient.get.mockResolvedValueOnce({ data: { results: [mockNotification], count: 1 } }); const result = await resources.getNotificationTemplatesSuccess(templateId); expect(mockApiClient.get).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_success/`); expect(result).toEqual([mockNotification]); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.getNotificationTemplatesSuccess(undefined)).rejects.toThrow('Workflow Job Template ID is required'); }); }); describe('associateNotificationTemplateSuccess', () => { it('should associate a success notification template with a workflow job template', async () => { var _a, _b, _c, _d; mockApiClient.post.mockResolvedValueOnce({ data: { ...mockWorkflowJobTemplate, summary_fields: { ...mockWorkflowJobTemplate.summary_fields, notification_templates_success: { results: [{ id: notificationId, name: mockNotification.name, description: mockNotification.description, notification_type: mockNotification.notification_type }], count: 1 } } } }); const result = await resources.associateNotificationTemplateSuccess(templateId, notificationId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_success/`, { id: notificationId }); expect((_b = (_a = result.summary_fields) === null || _a === void 0 ? void 0 : _a.notification_templates_success) === null || _b === void 0 ? void 0 : _b.results).toHaveLength(1); expect((_d = (_c = result.summary_fields) === null || _c === void 0 ? void 0 : _c.notification_templates_success) === null || _d === void 0 ? void 0 : _d.results[0].id).toBe(notificationId); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.associateNotificationTemplateSuccess(undefined, notificationId)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when notification template ID is not provided', async () => { await expect(resources.associateNotificationTemplateSuccess(templateId, undefined)).rejects.toThrow('Notification Template ID is required'); }); }); describe('disassociateNotificationTemplateSuccess', () => { it('should disassociate a success notification template from a workflow job template', async () => { mockApiClient.post.mockResolvedValueOnce({ data: { ...mockWorkflowJobTemplate, summary_fields: { ...mockWorkflowJobTemplate.summary_fields, notification_templates_success: { results: [], count: 0 } } } }); await resources.disassociateNotificationTemplateSuccess(templateId, notificationId); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/notification_templates_success/`, { id: notificationId, disassociate: true }); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.disassociateNotificationTemplateSuccess(undefined, notificationId)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when notification template ID is not provided', async () => { await expect(resources.disassociateNotificationTemplateSuccess(templateId, undefined)).rejects.toThrow('Notification Template ID is required'); }); }); }); const testBuildUrl = (resources, path, params) => { const resourcesWithPrivate = resources; return resourcesWithPrivate.buildUrl(path, params); }; describe('buildUrl', () => { it('should return the path when no params are provided', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/'); expect(result).toBe('/api/v2/workflow_job_templates/'); }); it('should return the path when params is null', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', null); expect(result).toBe('/api/v2/workflow_job_templates/'); }); it('should return the path when params is undefined', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', undefined); expect(result).toBe('/api/v2/workflow_job_templates/'); }); it('should return the path when params is an empty object', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', {}); expect(result).toBe('/api/v2/workflow_job_templates/'); }); it('should append query parameters correctly', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', { name: 'test', page: 1, filter: 'active' }); expect(result).toContain('name=test'); expect(result).toContain('page=1'); expect(result).toContain('filter=active'); expect(result).toMatch(/^\/api\/v2\/workflow_job_templates\/\?.*/); }); it('should handle special characters in query parameters', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', { search: 'test@example.com', filter: 'status=running' }); expect(result).toContain('search=test%40example.com'); expect(result).toContain('filter=status%3Drunning'); }); it('should handle array parameters correctly', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', { id__in: [1, 2, 3], status: ['running', 'successful'] }); expect(result).toContain('id__in=1%2C2%2C3'); expect(result).toContain('status=running%2Csuccessful'); }); it('should filter out undefined and null values but include empty strings', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', { name: 'test', status: undefined, description: null, emptyString: '', zero: 0, falseValue: false }); expect(result).toContain('name=test'); expect(result).toContain('zero=0'); expect(result).toContain('falseValue=false'); expect(result).toContain('emptyString='); expect(result).not.toContain('status='); expect(result).not.toContain('description='); }); it('should include empty strings in the URL', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', { status: undefined, description: null, emptyString: '' }); expect(result).toBe('/api/v2/workflow_job_templates/?emptyString='); }); it('should return base URL when all parameters are null or undefined', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', { status: undefined, description: null }); expect(result).toBe('/api/v2/workflow_job_templates/'); }); it('should handle complex nested paths', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/123/launch/', { extra_vars: '{"key":"value"}' }); expect(result).toContain('extra_vars=%7B%22key%22%3A%22value%22%7D'); expect(result).toBe('/api/v2/workflow_job_templates/123/launch/?extra_vars=%7B%22key%22%3A%22value%22%7D'); }); it('should handle boolean values correctly', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', { active: true, archived: false }); expect(result).toContain('active=true'); expect(result).toContain('archived=false'); }); it('should handle number values correctly', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/', { id: 123, limit: 50, offset: 0 }); expect(result).toContain('id=123'); expect(result).toContain('limit=50'); expect(result).toContain('offset=0'); }); it('should handle special characters in path and parameters', () => { const result = testBuildUrl(resources, '/api/v2/workflow_job_templates/with/special/chars/', { query: 'test & test', filter: 'status=="running"' }); expect(result).toContain('query=test%20%26%20test'); expect(result).toContain('filter=status%3D%3D%22running%22'); }); }); describe('survey spec', () => { const templateId = 1; const mockSurveySpec = { name: 'test_survey', description: 'Test survey description', spec: [ { question_name: 'example_question', question_description: 'Example question description', required: true, type: 'text', variable: 'example_var', min: null, max: null, default: 'default value', choices: '', new_question: true, }, ], }; describe('getSurveySpec', () => { it('should get the survey spec for a workflow job template', async () => { mockApiClient.get.mockResolvedValueOnce({ data: { ...mockSurveySpec, name: 'test_survey', description: 'Test survey description', }, }); const result = await resources.getSurveySpec(templateId); expect(mockApiClient.get).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/survey_spec/`); expect(result).toEqual({ ...mockSurveySpec, name: 'test_survey', description: 'Test survey description', }); }); it('should return null if survey does not exist', async () => { const error = new Error('Not Found'); error.response = { status: 404 }; mockApiClient.get.mockRejectedValueOnce(error); const result = await resources.getSurveySpec(templateId); expect(result).toBeNull(); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.getSurveySpec(undefined)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should re-throw non-404 errors', async () => { const error = new Error('Internal Server Error'); error.response = { status: 500 }; mockApiClient.get.mockRejectedValueOnce(error); await expect(resources.getSurveySpec(templateId)) .rejects .toThrow('Internal Server Error'); expect(mockApiClient.get).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/survey_spec/`); }); it('should re-throw errors without response object', async () => { const error = new Error('Network Error'); mockApiClient.get.mockRejectedValueOnce(error); await expect(resources.getSurveySpec(templateId)) .rejects .toThrow('Network Error'); }); }); describe('createSurveySpec', () => { it('should create a survey spec for a workflow job template', async () => { mockApiClient.post.mockResolvedValueOnce({ data: { ...mockSurveySpec, name: 'test_survey', description: 'Test survey description', }, }); const result = await resources.createSurveySpec(templateId, mockSurveySpec); expect(mockApiClient.post).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/survey_spec/`, mockSurveySpec); expect(result).toEqual({ ...mockSurveySpec, name: 'test_survey', description: 'Test survey description', }); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.createSurveySpec(undefined, mockSurveySpec)).rejects.toThrow('Workflow Job Template ID is required'); }); it('should throw an error when survey spec is not provided', async () => { await expect(resources.createSurveySpec(templateId, undefined)).rejects.toThrow('Survey spec is required'); }); }); describe('deleteSurveySpec', () => { it('should delete the survey spec for a workflow job template', async () => { mockApiClient.delete.mockResolvedValueOnce({}); await resources.deleteSurveySpec(templateId); expect(mockApiClient.delete).toHaveBeenCalledWith(`/api/v2/workflow_job_templates/${templateId}/survey_spec/`); }); it('should throw an error when template ID is not provided', async () => { await expect(resources.deleteSurveySpec(undefined)).rejects.toThrow('Workflow Job Template ID is required'); }); }); }); }); //# sourceMappingURL=WorkflowJobTemplateResources.test.js.map