Skip to content

Commit

Permalink
Fix formatting for new CPE match API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timopollmeier committed Dec 13, 2024
1 parent 42827ac commit 49754ba
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 40 deletions.
132 changes: 96 additions & 36 deletions tests/nvd/cpe_match/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def create_cpe_match_response(
"cpe_name": generate_cpe_name(iteration, i),
"cpe_name_id": f"{uuid_replace_str(cpe_name_id, iteration, i)}",
}
]
],
}
)
}
Expand All @@ -70,8 +70,12 @@ def create_cpe_match_response(
response.json.return_value = data
return response


def create_cpe_match_responses(
match_criteria_id: UUID, cpe_name_id: UUID, responses: int = 2, results_per_response: int = 1
match_criteria_id: UUID,
cpe_name_id: UUID,
responses: int = 2,
results_per_response: int = 1,
) -> list[MagicMock]:
return [
create_cpe_match_response(
Expand Down Expand Up @@ -114,7 +118,9 @@ async def test_cpe_match(self):
match_criteria_id = uuid_replace(uuid4(), 1, 1)
cpe_name_id = uuid_replace(uuid4(), 1, 1)

self.http_client.get.return_value = create_cpe_match_response(match_criteria_id, cpe_name_id)
self.http_client.get.return_value = create_cpe_match_response(
match_criteria_id, cpe_name_id
)

cpe_match_string = await self.api.cpe_match(match_criteria_id)

Expand All @@ -129,32 +135,38 @@ async def test_cpe_match(self):
cpe_match_string.match_criteria_id,
)
self.assertEqual(
generate_cpe_name(1,1),
generate_cpe_name(1, 1),
cpe_match_string.criteria,
)
self.assertEqual(
cpe_name_id,
cpe_match_string.matches[0].cpe_name_id,
)
self.assertEqual(
generate_cpe_name(1,1),
generate_cpe_name(1, 1),
cpe_match_string.matches[0].cpe_name,
)

@patch("pontos.nvd.cpe_match.api.now", spec=now)
async def test_cpe_matches_last_modified_start_date(self, now_mock: MagicMock):
async def test_cpe_matches_last_modified_start_date(
self, now_mock: MagicMock
):
match_criteria_id = uuid4()
cpe_name_id = uuid4()

now_mock.return_value = datetime(2019, 8, 30)
self.http_client.get.side_effect = create_cpe_match_responses(match_criteria_id, cpe_name_id)
self.http_client.get.side_effect = create_cpe_match_responses(
match_criteria_id, cpe_name_id
)

it = aiter(
self.api.cpe_matches(last_modified_start_date=datetime(2019, 6, 1))
)
cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -170,7 +182,9 @@ async def test_cpe_matches_last_modified_start_date(self, now_mock: MagicMock):

cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -186,19 +200,25 @@ async def test_cpe_matches_last_modified_start_date(self, now_mock: MagicMock):
cpe_match = await anext(it)

@patch("pontos.nvd.cpe_match.api.now", spec=now)
async def test_cpe_matches_last_modified_end_date(self, now_mock: MagicMock):
async def test_cpe_matches_last_modified_end_date(
self, now_mock: MagicMock
):
match_criteria_id = uuid4()
cpe_name_id = uuid4()

now_mock.return_value = datetime(2019, 8, 30)
self.http_client.get.side_effect = create_cpe_match_responses(match_criteria_id, cpe_name_id)
self.http_client.get.side_effect = create_cpe_match_responses(
match_criteria_id, cpe_name_id
)

it = aiter(
self.api.cpe_matches(last_modified_end_date=datetime(2019, 8, 1))
)
cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -213,7 +233,9 @@ async def test_cpe_matches_last_modified_end_date(self, now_mock: MagicMock):

cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -231,14 +253,16 @@ async def test_cpe_matches_cve_id(self):
match_criteria_id = uuid4()
cpe_name_id = uuid4()

self.http_client.get.side_effect = create_cpe_match_responses(match_criteria_id, cpe_name_id)

it = aiter(
self.api.cpe_matches(cve_id="CVE-2010-3574")
self.http_client.get.side_effect = create_cpe_match_responses(
match_criteria_id, cpe_name_id
)

it = aiter(self.api.cpe_matches(cve_id="CVE-2010-3574"))
cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -253,7 +277,9 @@ async def test_cpe_matches_cve_id(self):

cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -271,14 +297,20 @@ async def test_cpe_matches_match_string_search(self):
match_criteria_id = uuid4()
cpe_name_id = uuid4()

self.http_client.get.side_effect = create_cpe_match_responses(match_criteria_id, cpe_name_id)
self.http_client.get.side_effect = create_cpe_match_responses(
match_criteria_id, cpe_name_id
)

it = aiter(
self.api.cpe_matches(match_string_search="cpe:2.3:a:sun:jre:*:*:*:*:*:*:*:*")
self.api.cpe_matches(
match_string_search="cpe:2.3:a:sun:jre:*:*:*:*:*:*:*:*"
)
)
cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -293,7 +325,9 @@ async def test_cpe_matches_match_string_search(self):

cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -311,14 +345,20 @@ async def test_cpe_matches_match_string_search(self):
match_criteria_id = uuid4()
cpe_name_id = uuid4()

self.http_client.get.side_effect = create_cpe_match_responses(match_criteria_id, cpe_name_id)
self.http_client.get.side_effect = create_cpe_match_responses(
match_criteria_id, cpe_name_id
)

it = aiter(
self.api.cpe_matches(match_string_search="cpe:2.3:a:sun:jre:*:*:*:*:*:*:*:*")
self.api.cpe_matches(
match_string_search="cpe:2.3:a:sun:jre:*:*:*:*:*:*:*:*"
)
)
cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -333,7 +373,9 @@ async def test_cpe_matches_match_string_search(self):

cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -360,7 +402,9 @@ async def test_cpe_matches_request_results(self):
it = aiter(self.api.cpe_matches(request_results=10))
cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -372,14 +416,18 @@ async def test_cpe_matches_request_results(self):

self.http_client.get.reset_mock()
cpe_match = await anext(it)
self.assertEqual(uuid_replace(match_criteria_id, 1, 2), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 2), cpe_match.match_criteria_id
)
self.http_client.get.assert_not_called()

self.http_client.get.reset_mock()

cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={},
Expand All @@ -391,7 +439,9 @@ async def test_cpe_matches_request_results(self):

self.http_client.get.reset_mock()
cpe_match = await anext(it)
self.assertEqual(uuid_replace(match_criteria_id, 2, 2), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 2), cpe_match.match_criteria_id
)
self.http_client.get.assert_not_called()

with self.assertRaises(Exception):
Expand Down Expand Up @@ -427,7 +477,9 @@ async def test_cpe_matches_request_results_with_token(self):
it = aiter(self.api.cpe_matches(request_results=10))
cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={"apiKey": "token123"},
Expand All @@ -439,14 +491,18 @@ async def test_cpe_matches_request_results_with_token(self):

self.http_client.get.reset_mock()
cpe_match = await anext(it)
self.assertEqual(uuid_replace(match_criteria_id, 1, 2), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 1, 2), cpe_match.match_criteria_id
)
self.http_client.get.assert_not_called()

self.http_client.get.reset_mock()

cpe_match = await anext(it)

self.assertEqual(uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 1), cpe_match.match_criteria_id
)
self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cpematch/2.0",
headers={"apiKey": "token123"},
Expand All @@ -458,7 +514,9 @@ async def test_cpe_matches_request_results_with_token(self):

self.http_client.get.reset_mock()
cpe_match = await anext(it)
self.assertEqual(uuid_replace(match_criteria_id, 2, 2), cpe_match.match_criteria_id)
self.assertEqual(
uuid_replace(match_criteria_id, 2, 2), cpe_match.match_criteria_id
)
self.http_client.get.assert_not_called()

with self.assertRaises(Exception):
Expand All @@ -473,7 +531,9 @@ async def test_rate_limit_with_token(
):
match_criteria_id = uuid4()
cpe_name_id = uuid4()
self.http_client.get.side_effect = create_cpe_match_responses(match_criteria_id, cpe_name_id,8)
self.http_client.get.side_effect = create_cpe_match_responses(
match_criteria_id, cpe_name_id, 8
)
monotonic_mock.side_effect = [10, 11]

it = aiter(self.api.cpe_matches())
Expand Down
8 changes: 4 additions & 4 deletions tests/nvd/models/test_cpe_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ def test_matches(self):
self.assertEqual(
CPEMatch(
"cpe:2.3:a:sun:jre:1.3.0:update3:*:*:*:*:*:*",
UUID("2d284534-da21-43d5-9d89-07f19ae400ea")
UUID("2d284534-da21-43d5-9d89-07f19ae400ea"),
),
cpe_match_string.matches[0]
cpe_match_string.matches[0],
)

self.assertEqual(
CPEMatch(
"cpe:2.3:a:sun:jre:1.6.0:update3:*:*:*:*:*:*",
UUID("c518a954-369e-453e-8e17-2af639150115")
UUID("c518a954-369e-453e-8e17-2af639150115"),
),
cpe_match_string.matches[-1]
cpe_match_string.matches[-1],
)

def test_including_version_limits(self):
Expand Down

0 comments on commit 49754ba

Please sign in to comment.