Add tests for /ban
This commit is contained in:
		
							
								
								
									
										51
									
								
								tests/test_ban.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								tests/test_ban.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,51 @@ | ||||
| import base64 | ||||
| from types import SimpleNamespace | ||||
|  | ||||
| pfctl_stdout_lines = b''' | ||||
| block drop quick proto tcp from <f2b-sendmail-auth> to any port = submission | ||||
| block drop quick proto tcp from <f2b-sendmail-auth> to any port = smtps | ||||
| block drop quick proto tcp from <f2b-sendmail-auth> to any port = smtp | ||||
| block drop quick proto tcp from <f2b-sshd> to any port = ssh | ||||
| block drop quick proto tcp from <f2b-recidive> to any | ||||
| ''' | ||||
|  | ||||
|  | ||||
| def test_ban_ipv6(client, mocker): | ||||
|     def noop(): | ||||
|         pass | ||||
|     run_res = SimpleNamespace() | ||||
|     run_res.stdout = b'' | ||||
|     run_res.stderr = b'1/1 addresses added.\n' | ||||
|     run_res.returncode=0 | ||||
|     run_res.check_returncode = noop | ||||
|  | ||||
|     mocker.patch('jail2ban.pfctl.run', return_value=run_res) | ||||
|  | ||||
|     valid_credentials = base64.b64encode(b"test.example.com:testpassword").decode("utf-8") | ||||
|     json_payload = {"name":"sshd","ip":"2001:db8::abad:cafe"} | ||||
|     response = client.put("/ban", | ||||
|                           json=json_payload, | ||||
|                           headers={"Authorization": "Basic " + valid_credentials}) | ||||
|  | ||||
|  | ||||
|     assert response.json['operation'] == 'add' | ||||
|  | ||||
| def test_ban_ipv4(client, mocker): | ||||
|     def noop(): | ||||
|         pass | ||||
|     run_res = SimpleNamespace() | ||||
|     run_res.stdout = b'' | ||||
|     run_res.stderr = b'1/1 addresses added.\n' | ||||
|     run_res.returncode=0 | ||||
|     run_res.check_returncode = noop | ||||
|  | ||||
|     mocker.patch('jail2ban.pfctl.run', return_value=run_res) | ||||
|  | ||||
|     valid_credentials = base64.b64encode(b"test.example.com:testpassword").decode("utf-8") | ||||
|     json_payload = {"name":"sshd","ip":"192.0.2.42"} | ||||
|     response = client.put("/ban", | ||||
|                           json=json_payload, | ||||
|                           headers={"Authorization": "Basic " + valid_credentials}) | ||||
|  | ||||
|  | ||||
|     assert response.json['operation'] == 'add' | ||||
		Reference in New Issue
	
	Block a user