Skip to content

Commit

Permalink
Merge pull request #158 from ewjoachim/fix-timestamp-order
Browse files Browse the repository at this point in the history
Fix order of timestamps in DateRestriction
  • Loading branch information
ewjoachim committed Jan 29, 2023
2 parents 52ca301 + 2b8328d commit 6ca06e9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions docs/discussions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ The legacy caveat are represented by classes prefixed by ``Legacy``.

The types of caveats are:

- ``[0, <timestamp: int>, <timestamp: int>]`` is met if we try uploading the
project between ``nbf`` (included) and ``exp`` (excluded). It's represented
by the class `DateRestriction`. Legacy format is ``"{"nbf": <timestamp: int>,
"exp": <timestamp: int>}"`` and the corresponding class is
- ``[0, <exp: int>, <nbf: int>]`` is met if we try uploading the project
between timestamps ``nbf`` (included) and ``exp`` (excluded). It's
represented by the class `DateRestriction`. Legacy format is ``"{"nbf":
<timestamp: int>, "exp": <timestamp: int>}"`` and the corresponding class is
`LegacyDateRestriction`.

- ``[1, [<project_name: str>, ...]]`` is met if the project we upload is among
Expand Down
4 changes: 2 additions & 2 deletions docs/howto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ without a token. In this case, you can use the methods on the `Restriction` clas

import pypitoken
restriction = pypitoken.Restriction.load_json(
'[0, 1234567890, 1234567891]'
'[0, 1234567891, 1234567890]'
)
# or
restriction = pypitoken.Restriction.load(
[0, 1234567890, 1234567891]
[0, 1234567891, 1234567890]
)
# DateRestriction()

Expand Down
6 changes: 3 additions & 3 deletions pypitoken/restrictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ def _get_schema(cls) -> dict:
@classmethod
def _extract_kwargs(cls, value: list) -> dict:
return {
"not_before": value[1],
"not_after": value[2],
"not_before": value[2], # Yes, the official order is "reversed"
"not_after": value[1],
}

def dump(self) -> list:
return [self.tag, self.not_before, self.not_after]
return [self.tag, self.not_after, self.not_before]

def check(self, context: Context) -> None:
super().check(context=context)
Expand Down
14 changes: 7 additions & 7 deletions tests/test_restrictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,20 @@ def test__Restriction__restrictions_from_parameters():

def test__DateRestriction__load_value__pass():
assert restrictions.DateRestriction._load_value(
value=[0, 1_234_567_890, 1_234_567_900]
value=[0, 1_234_567_900, 1_234_567_890]
) == restrictions.DateRestriction(not_before=1_234_567_890, not_after=1_234_567_900)


@pytest.mark.parametrize(
"value",
[
[],
[1, 1_234_567_890, 1_234_567_900],
[1, 1_234_567_900, 1_234_567_890],
[0],
[0, 1_234_567_890],
[0, 1_234_567_890, 1_234_567_900, 1_234_567_999],
[0, "1_234_567_890", "1_234_567_900"],
[0, "2000-01-01 00:00:00", "2000-01-02 00:00:00"],
[0, 1_234_567_890, 1_234_567_999, 1_234_567_900],
[0, "1_234_567_900", "1_234_567_890"],
[0, "2000-01-02 00:00:00", "2000-01-01 00:00:00"],
],
)
def test__DateRestriction__load_value__fail(value):
Expand All @@ -389,7 +389,7 @@ def test__DateRestriction__load_value__fail(value):


def test__DateRestriction__extract_kwargs():
value = [0, 1_234_567_890, 1_234_567_900]
value = [0, 1_234_567_900, 1_234_567_890]
kwargs = restrictions.DateRestriction._extract_kwargs(value=value)
assert kwargs == {"not_before": 1_234_567_890, "not_after": 1_234_567_900}

Expand Down Expand Up @@ -420,7 +420,7 @@ def test__DateRestriction__dump():
restriction = restrictions.DateRestriction(
not_before=1_234_567_890, not_after=1_234_567_900
)
assert restriction.dump() == [0, 1_234_567_890, 1_234_567_900]
assert restriction.dump() == [0, 1_234_567_900, 1_234_567_890]


def test__DateRestriction__from_parameters__empty():
Expand Down

0 comments on commit 6ca06e9

Please sign in to comment.