''' Tests for /list route ''' from types import SimpleNamespace _PF_TABLE_LIST = b''' 192.0.2.66 Cleared: Sat Jan 7 12:50:36 2023 In/Block: [ Packets: 0 Bytes: 0 ] In/Pass: [ Packets: 0 Bytes: 0 ] Out/Block: [ Packets: 0 Bytes: 0 ] Out/Pass: [ Packets: 0 Bytes: 0 ] 2001:db8::abad:cafe Cleared: Sat Jan 7 05:13:53 2023 In/Block: [ Packets: 4 Bytes: 240 ] In/Pass: [ Packets: 0 Bytes: 0 ] Out/Block: [ Packets: 0 Bytes: 0 ] Out/Pass: [ Packets: 0 Bytes: 0 ] 2001:db8::abad:f00d:cafe Cleared: Sat Jan 7 05:05:16 2023 In/Block: [ Packets: 48 Bytes: 2880 ] In/Pass: [ Packets: 0 Bytes: 0 ] Out/Block: [ Packets: 0 Bytes: 0 ] Out/Pass: [ Packets: 0 Bytes: 0 ]''' _LIST_RESULT = [{'addr': '192.0.2.66', 'date': 'Sat Jan 7 12:50:36 2023', 'in_pckt_block': '0', 'in_bytes_block': '0', 'in_pckt_pass': '0', 'in_bytes_pass': '0', 'out_pckt_block': '0', 'out_bytes_block': '0', 'out_pckt_pass': '0', 'out_bytes_pass': '0'}, {'addr': '2001:db8::abad:cafe', 'date': 'Sat Jan 7 05:13:53 2023', 'in_pckt_block': '4', 'in_bytes_block': '240', 'in_pckt_pass': '0', 'in_bytes_pass': '0', 'out_pckt_block': '0', 'out_bytes_block': '0', 'out_pckt_pass': '0', 'out_bytes_pass': '0'}, {'addr': '2001:db8::abad:f00d:cafe', 'date': 'Sat Jan 7 05:05:16 2023', 'in_pckt_block': '48', 'in_bytes_block': '2880', 'in_pckt_pass': '0', 'in_bytes_pass': '0', 'out_pckt_block': '0', 'out_bytes_block': '0', 'out_pckt_pass': '0', 'out_bytes_pass': '0'}] def test_list_single_table(client, mocker, valid_credentials): ''' List a single pf table using the fail2ban jail name ''' def noop(): pass run_res = SimpleNamespace() run_res.stdout = _PF_TABLE_LIST run_res.stderr = b'No ALTQ support in kernel\nALTQ related functions disabled\n' run_res.returncode = 0 run_res.check_returncode = noop mocker.patch('jail2ban.pfctl.run', return_value=run_res) response = client.get("/list/sshd", headers={"Authorization": "Basic " + valid_credentials}) assert response.json['anchor'] == 'f2b-sshd/test.example.com' assert response.json['result'] == _LIST_RESULT def test_list_nonexistent_table(client, mocker, valid_credentials): ''' Test for nonexistent table. Should result in a 404 not found ''' def noop(): pass run_res = SimpleNamespace() run_res.stdout = b'' run_res.stderr = b'No ALTQ support in kernel\nALTQ related functions disabled\n' run_res.returncode = 255 run_res.check_returncode = noop mocker.patch('jail2ban.pfctl.run', return_value=run_res) response = client.get("/list/nonexistent", headers={"Authorization": "Basic " + valid_credentials}) assert response.status_code == 404 assert response.json['error'] == "'nonexistent' is not " \ "a known fail2ban jail"