Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: Assume UTC when no offset it specified #941

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pontos/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from dataclasses import dataclass
from datetime import date, datetime
from datetime import date, datetime, timezone
from enum import Enum
from inspect import isclass
from typing import Any, Dict, Type, Union, get_args, get_origin, get_type_hints
Expand Down Expand Up @@ -93,11 +93,10 @@ def _get_value_from_model_field_cls(
value = dateparser.isoparse(value)
# the iso format may not contain UTC data or a UTC offset
# this means it is considered local time (Python calls this "naive"
# datetime) and can't really be compared to other times. maybe we
# should always assume UTC for these formats.
# This could be done the following:
# if not value.tzinfo:
# value = value.replace(tzinfo=timezone.utc)
# datetime) and can't really be compared to other times.
# Let's UTC in these cases:
if not value.tzinfo:
value = value.replace(tzinfo=timezone.utc)
elif isclass(model_field_cls) and issubclass(model_field_cls, date):
value = date.fromisoformat(value)
elif get_origin(model_field_cls) == list:
Expand Down
13 changes: 10 additions & 3 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ class ExampleModel(Model):
foo: datetime

model = ExampleModel.from_dict({"foo": "1988-10-01T04:00:00.000"})
self.assertEqual(model.foo, datetime(1988, 10, 1, 4))
self.assertEqual(
model.foo, datetime(1988, 10, 1, 4, tzinfo=timezone.utc)
)

model = ExampleModel.from_dict({"foo": "1988-10-01T04:00:00Z"})
self.assertEqual(
Expand All @@ -131,7 +133,10 @@ class ExampleModel(Model):
)

model = ExampleModel.from_dict({"foo": "2021-06-06T11:15:10.213"})
self.assertEqual(model.foo, datetime(2021, 6, 6, 11, 15, 10, 213000))
self.assertEqual(
model.foo,
datetime(2021, 6, 6, 11, 15, 10, 213000, tzinfo=timezone.utc),
)

def test_date(self):
@dataclass
Expand Down Expand Up @@ -190,7 +195,9 @@ class ExampleModel(Model):
)

self.assertEqual(model.foo, "abc")
self.assertEqual(model.bar, datetime(1988, 10, 1, 4))
self.assertEqual(
model.bar, datetime(1988, 10, 1, 4, tzinfo=timezone.utc)
)
self.assertEqual(model.id, 123)
self.assertEqual(model.baz, ["a", "b", "c"])
self.assertIsNotNone(model.ipsum)
Expand Down
10 changes: 7 additions & 3 deletions tests/nvd/cpe/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# pylint: disable=line-too-long, arguments-differ, redefined-builtin
# ruff: noqa: E501

from datetime import datetime
from datetime import datetime, timezone
from itertools import repeat
from typing import Any, Optional
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -132,9 +132,13 @@ async def test_cpe(self):
self.assertEqual(cpe.cpe_name_id, uuid_replace(uuid, 1, 1))
self.assertFalse(cpe.deprecated)
self.assertEqual(
cpe.last_modified, datetime(2022, 12, 9, 18, 15, 16, 973000)
cpe.last_modified,
datetime(2022, 12, 9, 18, 15, 16, 973000, tzinfo=timezone.utc),
)
self.assertEqual(
cpe.created,
datetime(2022, 12, 9, 16, 20, 6, 943000, tzinfo=timezone.utc),
)
self.assertEqual(cpe.created, datetime(2022, 12, 9, 16, 20, 6, 943000))

self.assertEqual(cpe.refs, [])
self.assertEqual(cpe.titles, [])
Expand Down
8 changes: 5 additions & 3 deletions tests/nvd/cve/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# pylint: disable=line-too-long, arguments-differ, redefined-builtin

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

Expand Down Expand Up @@ -111,10 +111,12 @@ async def test_cve(self):
self.assertEqual(cve.id, "CVE-2022-45536")
self.assertEqual(cve.source_identifier, "[email protected]")
self.assertEqual(
cve.published, datetime(2022, 11, 22, 21, 15, 11, 103000)
cve.published,
datetime(2022, 11, 22, 21, 15, 11, 103000, tzinfo=timezone.utc),
)
self.assertEqual(
cve.last_modified, datetime(2022, 11, 23, 16, 2, 7, 367000)
cve.last_modified,
datetime(2022, 11, 23, 16, 2, 7, 367000, tzinfo=timezone.utc),
)
self.assertEqual(len(cve.descriptions), 1)
self.assertEqual(len(cve.references), 2)
Expand Down
3 changes: 2 additions & 1 deletion tests/nvd/cve_changes/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ async def test_cve_changes(self):
)
self.assertEqual(cve_change.source_identifier, "[email protected]")
self.assertEqual(
cve_change.created, datetime(2022, 3, 18, 20, 13, 8, 123000)
cve_change.created,
datetime(2022, 3, 18, 20, 13, 8, 123000, tzinfo=timezone.utc),
)
self.assertEqual(
cve_change.details,
Expand Down
10 changes: 7 additions & 3 deletions tests/nvd/models/test_cpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# ruff: noqa: E501

import unittest
from datetime import datetime
from datetime import datetime, timezone
from uuid import UUID

from pontos.nvd.models.cpe import CPE, ReferenceType
Expand All @@ -37,9 +37,13 @@ def test_required_only(self):
)
self.assertFalse(cpe.deprecated)
self.assertEqual(
cpe.last_modified, datetime(2022, 12, 9, 18, 15, 16, 973000)
cpe.last_modified,
datetime(2022, 12, 9, 18, 15, 16, 973000, tzinfo=timezone.utc),
)
self.assertEqual(
cpe.created,
datetime(2022, 12, 9, 16, 20, 6, 943000, tzinfo=timezone.utc),
)
self.assertEqual(cpe.created, datetime(2022, 12, 9, 16, 20, 6, 943000))

self.assertEqual(cpe.titles, [])
self.assertEqual(cpe.refs, [])
Expand Down
12 changes: 8 additions & 4 deletions tests/nvd/models/test_cve.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# ruff: noqa: E501

import unittest
from datetime import date, datetime
from datetime import date, datetime, timezone

from pontos.nvd.models import cvss_v2, cvss_v3
from pontos.nvd.models.cve import CVE, CVSSType, Operator
Expand All @@ -33,10 +33,12 @@ def test_required_only(self):
self.assertEqual(cve.id, "CVE-2022-45536")
self.assertEqual(cve.source_identifier, "[email protected]")
self.assertEqual(
cve.published, datetime(2022, 11, 22, 21, 15, 11, 103000)
cve.published,
datetime(2022, 11, 22, 21, 15, 11, 103000, tzinfo=timezone.utc),
)
self.assertEqual(
cve.last_modified, datetime(2022, 11, 23, 16, 2, 7, 367000)
cve.last_modified,
datetime(2022, 11, 23, 16, 2, 7, 367000, tzinfo=timezone.utc),
)
self.assertEqual(len(cve.descriptions), 1)
self.assertEqual(len(cve.references), 2)
Expand Down Expand Up @@ -506,7 +508,9 @@ def test_vendor_comments(self):
comment.comment,
"Fixed in Apache HTTP Server 1.3.12:\nhttp://httpd.apache.org/security/vulnerabilities_13.html",
)
self.assertEqual(comment.last_modified, datetime(2008, 7, 2))
self.assertEqual(
comment.last_modified, datetime(2008, 7, 2, tzinfo=timezone.utc)
)

def test_evaluator_comment(self):
cve = CVE.from_dict(
Expand Down
5 changes: 3 additions & 2 deletions tests/nvd/models/test_cve_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# ruff: noqa: E501

import unittest
from datetime import datetime
from datetime import datetime, timezone
from uuid import UUID

from pontos.nvd.models.cve_change import CVEChange, Detail, EventName
Expand All @@ -25,7 +25,8 @@ def test_required_only(self):
)
self.assertEqual(cve_change.source_identifier, "[email protected]")
self.assertEqual(
cve_change.created, datetime(2022, 3, 18, 20, 13, 8, 123000)
cve_change.created,
datetime(2022, 3, 18, 20, 13, 8, 123000, tzinfo=timezone.utc),
)
self.assertEqual(
cve_change.details,
Expand Down