test(coverage): cover all modified lines for 100% patch coverage vs origin/main
- Add domains field to certificate mock to exercise per-domain loop in Dashboard component, covering the previously untested branch - Extend CrowdSec whitelist test suite with backdrop-click close test to cover the dialog dismissal handler - Remove duplicate describe blocks introduced when whitelist API tests were appended to crowdsec.test.ts, resolving ESLint vitest/no-identical-title errors that were blocking pre-commit hooks
This commit is contained in:
@@ -116,6 +116,120 @@ describe('crowdsec API', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('listCrowdsecDecisions', () => {
|
||||
it('should call GET /admin/crowdsec/decisions and return data', async () => {
|
||||
const mockData = {
|
||||
decisions: [
|
||||
{ id: '1', ip: '1.2.3.4', reason: 'bot', duration: '24h', created_at: '2024-01-01T00:00:00Z', source: 'crowdsec' },
|
||||
],
|
||||
}
|
||||
vi.mocked(client.get).mockResolvedValue({ data: mockData })
|
||||
|
||||
const result = await crowdsec.listCrowdsecDecisions()
|
||||
|
||||
expect(client.get).toHaveBeenCalledWith('/admin/crowdsec/decisions')
|
||||
expect(result).toEqual(mockData)
|
||||
})
|
||||
})
|
||||
|
||||
describe('banIP', () => {
|
||||
it('should call POST /admin/crowdsec/ban with ip, duration, and reason', async () => {
|
||||
vi.mocked(client.post).mockResolvedValue({})
|
||||
|
||||
await crowdsec.banIP('1.2.3.4', '24h', 'manual ban')
|
||||
|
||||
expect(client.post).toHaveBeenCalledWith('/admin/crowdsec/ban', {
|
||||
ip: '1.2.3.4',
|
||||
duration: '24h',
|
||||
reason: 'manual ban',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('unbanIP', () => {
|
||||
it('should call DELETE /admin/crowdsec/ban/{encoded ip}', async () => {
|
||||
vi.mocked(client.delete).mockResolvedValue({})
|
||||
|
||||
await crowdsec.unbanIP('1.2.3.4')
|
||||
|
||||
expect(client.delete).toHaveBeenCalledWith('/admin/crowdsec/ban/1.2.3.4')
|
||||
})
|
||||
|
||||
it('should URL-encode special characters in the IP', async () => {
|
||||
vi.mocked(client.delete).mockResolvedValue({})
|
||||
|
||||
await crowdsec.unbanIP('::1')
|
||||
|
||||
expect(client.delete).toHaveBeenCalledWith('/admin/crowdsec/ban/%3A%3A1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getCrowdsecKeyStatus', () => {
|
||||
it('should call GET /admin/crowdsec/key-status and return data', async () => {
|
||||
const mockData = {
|
||||
key_source: 'file' as const,
|
||||
env_key_rejected: false,
|
||||
current_key_preview: 'abc***xyz',
|
||||
message: 'Key loaded from file',
|
||||
}
|
||||
vi.mocked(client.get).mockResolvedValue({ data: mockData })
|
||||
|
||||
const result = await crowdsec.getCrowdsecKeyStatus()
|
||||
|
||||
expect(client.get).toHaveBeenCalledWith('/admin/crowdsec/key-status')
|
||||
expect(result).toEqual(mockData)
|
||||
})
|
||||
})
|
||||
|
||||
describe('listWhitelists', () => {
|
||||
it('should call GET /admin/crowdsec/whitelist and return the whitelist array', async () => {
|
||||
const mockWhitelist = [
|
||||
{
|
||||
uuid: 'uuid-1',
|
||||
ip_or_cidr: '192.168.1.1',
|
||||
reason: 'Home',
|
||||
created_at: '2024-01-01T00:00:00Z',
|
||||
updated_at: '2024-01-01T00:00:00Z',
|
||||
},
|
||||
]
|
||||
vi.mocked(client.get).mockResolvedValue({ data: { whitelist: mockWhitelist } })
|
||||
|
||||
const result = await crowdsec.listWhitelists()
|
||||
|
||||
expect(client.get).toHaveBeenCalledWith('/admin/crowdsec/whitelist')
|
||||
expect(result).toEqual(mockWhitelist)
|
||||
})
|
||||
})
|
||||
|
||||
describe('addWhitelist', () => {
|
||||
it('should call POST /admin/crowdsec/whitelist and return the created entry', async () => {
|
||||
const payload = { ip_or_cidr: '192.168.1.1', reason: 'Home' }
|
||||
const mockEntry = {
|
||||
uuid: 'uuid-1',
|
||||
ip_or_cidr: '192.168.1.1',
|
||||
reason: 'Home',
|
||||
created_at: '2024-01-01T00:00:00Z',
|
||||
updated_at: '2024-01-01T00:00:00Z',
|
||||
}
|
||||
vi.mocked(client.post).mockResolvedValue({ data: mockEntry })
|
||||
|
||||
const result = await crowdsec.addWhitelist(payload)
|
||||
|
||||
expect(client.post).toHaveBeenCalledWith('/admin/crowdsec/whitelist', payload)
|
||||
expect(result).toEqual(mockEntry)
|
||||
})
|
||||
})
|
||||
|
||||
describe('deleteWhitelist', () => {
|
||||
it('should call DELETE /admin/crowdsec/whitelist/{uuid}', async () => {
|
||||
vi.mocked(client.delete).mockResolvedValue({})
|
||||
|
||||
await crowdsec.deleteWhitelist('uuid-1')
|
||||
|
||||
expect(client.delete).toHaveBeenCalledWith('/admin/crowdsec/whitelist/uuid-1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('default export', () => {
|
||||
it('should export all functions', () => {
|
||||
expect(crowdsec.default).toHaveProperty('startCrowdsec')
|
||||
@@ -126,6 +240,14 @@ describe('crowdsec API', () => {
|
||||
expect(crowdsec.default).toHaveProperty('listCrowdsecFiles')
|
||||
expect(crowdsec.default).toHaveProperty('readCrowdsecFile')
|
||||
expect(crowdsec.default).toHaveProperty('writeCrowdsecFile')
|
||||
expect(crowdsec.default).toHaveProperty('listCrowdsecDecisions')
|
||||
expect(crowdsec.default).toHaveProperty('banIP')
|
||||
expect(crowdsec.default).toHaveProperty('unbanIP')
|
||||
expect(crowdsec.default).toHaveProperty('getCrowdsecKeyStatus')
|
||||
expect(crowdsec.default).toHaveProperty('listWhitelists')
|
||||
expect(crowdsec.default).toHaveProperty('addWhitelist')
|
||||
expect(crowdsec.default).toHaveProperty('deleteWhitelist')
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user