test: improve coverage — domain, protocol, references, integration DB tests

- Domain: DeviceId, Command, DeviceParameter, DeviceProperty, Mode
- Protocol: SOAP parsing, CWMPMessageBuilder (all methods)
- References: config_loader, WassupProvider (timeout, errors)
- Integration: MongoDB tests (CRUD, pagination, references)
- Fix: Home.vue IP lookup for Device.WANDevice path
- Total: 384 tests passing
This commit is contained in:
Kevin L'Hereec committed 2026-06-02 16:29:19 +02:00
1 parent e857b4fbe7
commit 003ef50a8e
14 files changed
+972 -1

No files matched your search

@@ -0,0 +1,100 @@
import { describe, it, expect } from 'vitest'
import { CWMPMessageBuilder } from '../../../app/protocol/common/cwmp_message_builder.js'
describe('CWMPMessageBuilder', () => {
it('informResponse builds valid response', () => {
const xml = CWMPMessageBuilder.informResponse('id-1')
expect(xml).toContain('InformResponse')
expect(xml).toContain('MaxEnvelopes')
expect(xml).toContain('id-1')
})
it('getParameterNames builds request', () => {
const xml = CWMPMessageBuilder.getParameterNames('id-2', 'Device.', true)
expect(xml).toContain('GetParameterNames')
expect(xml).toContain('Device.')
expect(xml).toContain('<NextLevel>1</NextLevel>')
})
it('getParameterNames with nextLevel=false', () => {
const xml = CWMPMessageBuilder.getParameterNames('id-3', 'Device.', false)
expect(xml).toContain('<NextLevel>0</NextLevel>')
})
it('getParameterValues builds request with names', () => {
const xml = CWMPMessageBuilder.getParameterValues('id-4', ['Device.Info.SW', 'Device.Info.HW'])
expect(xml).toContain('GetParameterValues')
expect(xml).toContain('Device.Info.SW')
expect(xml).toContain('Device.Info.HW')
expect(xml).toContain('arrayType="xsd:string[2]"')
})
it('setParameterValues builds request with params', () => {
const xml = CWMPMessageBuilder.setParameterValues('id-5', [
{ name: 'Device.X', value: '42', type: 'xsd:int' },
])
expect(xml).toContain('SetParameterValues')
expect(xml).toContain('Device.X')
expect(xml).toContain('42')
expect(xml).toContain('xsd:int')
expect(xml).toContain('ParameterKey')
})
it('download builds request with all params', () => {
const xml = CWMPMessageBuilder.download('id-6', {
url: 'http://example.com/fw.bin',
commandKey: 'fw-upgrade',
fileType: '1 Firmware Upgrade Image',
fileSize: 1024,
})
expect(xml).toContain('Download')
expect(xml).toContain('http://example.com/fw.bin')
expect(xml).toContain('fw-upgrade')
expect(xml).toContain('1024')
})
it('upload builds request', () => {
const xml = CWMPMessageBuilder.upload('id-7', { url: 'http://upload.com/cfg', commandKey: 'cfg-bk' })
expect(xml).toContain('Upload')
expect(xml).toContain('http://upload.com/cfg')
expect(xml).toContain('cfg-bk')
})
it('reboot builds request', () => {
const xml = CWMPMessageBuilder.reboot('id-8', 'reboot-key')
expect(xml).toContain('Reboot')
expect(xml).toContain('reboot-key')
})
it('factoryReset builds request', () => {
const xml = CWMPMessageBuilder.factoryReset('id-9')
expect(xml).toContain('FactoryReset')
})
it('scheduleInform builds request', () => {
const xml = CWMPMessageBuilder.scheduleInform('id-10', 300, 'sched-key')
expect(xml).toContain('ScheduleInform')
expect(xml).toContain('300')
expect(xml).toContain('sched-key')
})
it('getParameterAttributes builds request', () => {
const xml = CWMPMessageBuilder.getParameterAttributes('id-11', ['Device.Info.'])
expect(xml).toContain('GetParameterAttributes')
expect(xml).toContain('Device.Info.')
})
it('setParameterAttributes builds request', () => {
const xml = CWMPMessageBuilder.setParameterAttributes('id-12', [
{ name: 'Device.X', notificationChange: true, notification: 2, accessListChange: false, accessList: [] },
])
expect(xml).toContain('SetParameterAttributes')
expect(xml).toContain('Device.X')
expect(xml).toContain('<NotificationChange>1</NotificationChange>')
expect(xml).toContain('<Notification>2</Notification>')
})
it('emptyResponse returns empty string', () => {
expect(CWMPMessageBuilder.emptyResponse()).toBe('')
})
})
+77
View File
@@ -0,0 +1,77 @@
import { describe, it, expect } from 'vitest'
import { parseSoapXml, extractCwmpMessage, buildSoapEnvelope } from '../../../app/protocol/soap.js'
describe('parseSoapXml', () => {
it('parses a basic Inform SOAP envelope', () => {
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0">
<soap-env:Header>
<cwmp:ID soap-env:mustUnderstand="1">0001</cwmp:ID>
</soap-env:Header>
<soap-env:Body>
<cwmp:Inform>
<DeviceId><Manufacturer>Test</Manufacturer></DeviceId>
</cwmp:Inform>
</soap-env:Body>
</soap-env:Envelope>`
const result = parseSoapXml(xml)
expect(result.header.id).toBe('0001')
expect(result.body).toBeDefined()
expect(result.cwmpVersion).toBe('1.0')
})
it('parses soapenv prefix variant', () => {
const xml = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0">
<soapenv:Header><cwmp:ID soapenv:mustUnderstand="1">42</cwmp:ID></soapenv:Header>
<soapenv:Body><cwmp:InformResponse><MaxEnvelopes>1</MaxEnvelopes></cwmp:InformResponse></soapenv:Body>
</soapenv:Envelope>`
const result = parseSoapXml(xml)
expect(String(result.header.id)).toBe('42')
})
it('handles missing header gracefully', () => {
const xml = `<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Body><cwmp:GetParameterValuesResponse xmlns:cwmp="urn:dslforum-org:cwmp-1-0"></cwmp:GetParameterValuesResponse></soap-env:Body>
</soap-env:Envelope>`
const result = parseSoapXml(xml)
expect(result.header.id).toBeUndefined()
})
})
describe('extractCwmpMessage', () => {
it('extracts Inform message', () => {
const body = { 'cwmp:Inform': { DeviceId: { Manufacturer: 'Test' } } }
const msg = extractCwmpMessage(body)
expect(msg).not.toBeNull()
expect(msg!.type).toBe('Inform')
expect(msg!.data.DeviceId.Manufacturer).toBe('Test')
})
it('extracts GetParameterValuesResponse', () => {
const body = { 'cwmp:GetParameterValuesResponse': { ParameterList: [] } }
const msg = extractCwmpMessage(body)
expect(msg!.type).toBe('GetParameterValuesResponse')
})
it('returns null for empty body', () => {
expect(extractCwmpMessage({})).toBeNull()
})
it('returns null for non-cwmp keys', () => {
expect(extractCwmpMessage({ 'other:Thing': {} })).toBeNull()
})
})
describe('buildSoapEnvelope', () => {
it('builds valid XML envelope', () => {
const xml = buildSoapEnvelope('id-123', '<cwmp:Test/>')
expect(xml).toContain('soap-env:Envelope')
expect(xml).toContain('id-123')
expect(xml).toContain('<cwmp:Test/>')
})
it('builds envelope without id', () => {
const xml = buildSoapEnvelope(undefined, '<cwmp:Reboot/>')
expect(xml).toContain('<cwmp:Reboot/>')
})
})