digaws/tests/test_get_aws_ip_ranges_json.py

87 lines
2.4 KiB
Python
Raw Normal View History

2020-12-25 20:36:41 +00:00
import json
import os
import pytest
import requests
2024-06-08 11:43:41 +00:00
import digaws.digaws as digaws
2020-12-25 20:36:41 +00:00
import tests
class MockGetResponse:
text = tests.AWS_IP_RANGES
status_code = 200
@staticmethod
def json():
return json.loads(tests.AWS_IP_RANGES)
def mock_get(*args, **kwargs):
return MockGetResponse()
@pytest.fixture
def create_cache_dir(fs):
digaws.CACHE_DIR.mkdir(parents=True)
2024-06-08 11:43:41 +00:00
@pytest.mark.parametrize("fs", [[None, [digaws]]], indirect=True)
2020-12-25 20:36:41 +00:00
def test_get_aws_ip_ranges_cached_valid_file(mocker, fs, create_cache_dir) -> None:
2024-06-08 11:43:41 +00:00
with open(digaws.CACHE_FILE, "w") as out:
2020-12-25 20:36:41 +00:00
out.write(tests.AWS_IP_RANGES)
response = requests.Response
response.status_code = 304
2024-06-08 11:43:41 +00:00
mocker.patch("requests.get", return_value=response)
2020-12-25 20:36:41 +00:00
result = digaws.get_aws_ip_ranges()
2024-06-08 11:43:41 +00:00
assert result["syncToken"] == "1608245058"
2020-12-25 20:36:41 +00:00
2024-06-08 11:43:41 +00:00
@pytest.mark.parametrize("fs", [[None, [digaws]]], indirect=True)
2020-12-25 20:36:41 +00:00
def test_get_aws_ip_ranges_cached_invalid_file(mocker, fs, create_cache_dir) -> None:
2024-06-08 11:43:41 +00:00
with open(digaws.CACHE_FILE, "w"):
2020-12-25 20:36:41 +00:00
pass
response = requests.Response
response.status_code = 304
2024-06-08 11:43:41 +00:00
mocker.patch("requests.get", return_value=response)
2020-12-25 20:36:41 +00:00
with pytest.raises(digaws.CachedFileException):
digaws.get_aws_ip_ranges()
2024-06-08 11:43:41 +00:00
@pytest.mark.parametrize("fs", [[None, [digaws]]], indirect=True)
2020-12-25 20:36:41 +00:00
def test_get_aws_ip_ranges_cached_deprecated_file(monkeypatch, fs, create_cache_dir) -> None:
2024-06-08 11:43:41 +00:00
with open(digaws.CACHE_FILE, "w"):
2020-12-25 20:36:41 +00:00
pass
digaws.CACHE_FILE.touch()
os.utime(digaws.CACHE_FILE, times=(0, 0))
2024-06-08 11:43:41 +00:00
monkeypatch.setattr(requests, "get", mock_get)
2020-12-25 20:36:41 +00:00
result = digaws.get_aws_ip_ranges()
2024-06-08 11:43:41 +00:00
assert result["syncToken"] == "1608245058"
2020-12-25 20:36:41 +00:00
2024-06-08 11:43:41 +00:00
@pytest.mark.parametrize("fs", [[None, [digaws]]], indirect=True)
2020-12-25 20:36:41 +00:00
def test_get_aws_ip_ranges_no_file(monkeypatch, fs, create_cache_dir) -> None:
2024-06-08 11:43:41 +00:00
monkeypatch.setattr(requests, "get", mock_get)
2020-12-25 20:36:41 +00:00
result = digaws.get_aws_ip_ranges()
2024-06-08 11:43:41 +00:00
assert result["syncToken"] == "1608245058"
2020-12-25 20:36:41 +00:00
2024-06-08 11:43:41 +00:00
@pytest.mark.parametrize("fs", [[None, [digaws]]], indirect=True)
2020-12-25 20:36:41 +00:00
def test_get_aws_ip_ranges_invalid_status(mocker, fs, create_cache_dir) -> None:
response = requests.Response
response.status_code = 301
2024-06-08 11:43:41 +00:00
mocker.patch("requests.get", return_value=response)
2020-12-25 20:36:41 +00:00
with pytest.raises(digaws.UnexpectedRequestException) as e:
digaws.get_aws_ip_ranges()
2021-02-19 16:45:07 +00:00
2024-06-08 13:53:50 +00:00
assert str(e.value).startswith("Unexpected response from")