Skip to content

Commit

Permalink
Change: Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n-thumann authored and greenbonebot committed Nov 10, 2023
1 parent bc27067 commit 4ee6cc2
Showing 1 changed file with 70 additions and 9 deletions.
79 changes: 70 additions & 9 deletions tests/nvd/cve_change_history/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
# SPDX-License-Identifier: GPL-3.0-or-later


from datetime import datetime
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
from unittest.mock import MagicMock, patch
from uuid import UUID

from httpx import AsyncClient, Response

from pontos.errors import PontosError
from pontos.nvd.api import now
from pontos.nvd.cve_change_history.api import CVEChangeHistoryApi
from pontos.nvd.models.cve_change import Detail, EventName
from tests import AsyncMock, IsolatedAsyncioTestCase, aiter, anext
Expand Down Expand Up @@ -204,16 +205,76 @@ async def test_cve_changes_event_name(self):
)

with self.assertRaises(StopAsyncIteration):
cve_changes = await anext(it)

async def test_cve_changes_no_dates(self):
it = aiter(self.api.cve_changes(change_start_date=datetime(2023, 1, 1)))
with self.assertRaises(PontosError):
await anext(it)

it = aiter(self.api.cve_changes(change_end_date=datetime(2023, 1, 1)))
with self.assertRaises(PontosError):
await anext(it)
@patch("pontos.nvd.cve_change_history.api.now", spec=now)
async def test_cve_changes_calculate_end_date(self, now_mock: MagicMock):
now_mock.return_value = datetime(2023, 1, 2, tzinfo=timezone.utc)
self.http_client.get.side_effect = create_cve_changes_responses()

it = aiter(
self.api.cve_changes(
change_start_date=datetime(2023, 1, 1, tzinfo=timezone.utc)
)
)

await anext(it)

self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cvehistory/2.0",
headers={"apiKey": "token"},
params={
"startIndex": 0,
"changeStartDate": "2023-01-01T00:00:00+00:00",
"changeEndDate": "2023-01-02T00:00:00+00:00",
},
)

@patch("pontos.nvd.cve_change_history.api.now", spec=now)
async def test_cve_changes_calculate_end_date_with_limit(
self, now_mock: MagicMock
):
now_mock.return_value = datetime(2023, 5, 2, tzinfo=timezone.utc)
self.http_client.get.side_effect = create_cve_changes_responses()

it = aiter(
self.api.cve_changes(
change_start_date=datetime(2023, 1, 1, tzinfo=timezone.utc)
)
)

await anext(it)

self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cvehistory/2.0",
headers={"apiKey": "token"},
params={
"startIndex": 0,
"changeStartDate": "2023-01-01T00:00:00+00:00",
"changeEndDate": "2023-05-01T00:00:00+00:00",
},
)

async def test_cve_changes_calculate_start_date(self):
self.http_client.get.side_effect = create_cve_changes_responses()

it = aiter(
self.api.cve_changes(
change_end_date=datetime(2023, 5, 1, tzinfo=timezone.utc)
)
)

await anext(it)

self.http_client.get.assert_awaited_once_with(
"https://services.nvd.nist.gov/rest/json/cvehistory/2.0",
headers={"apiKey": "token"},
params={
"startIndex": 0,
"changeStartDate": "2023-01-01T00:00:00+00:00",
"changeEndDate": "2023-05-01T00:00:00+00:00",
},
)

async def test_cve_changes_range_too_long(self):
it = aiter(
Expand Down

0 comments on commit 4ee6cc2

Please sign in to comment.