Skip to content

Commit

Permalink
Merge pull request #28 from lotrekagency/feature/exemptions
Browse files Browse the repository at this point in the history
Added exemptions
  • Loading branch information
b0tero authored Feb 1, 2022
2 parents d9e74fd + 94a90eb commit ab6cb81
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
50 changes: 50 additions & 0 deletions tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,53 @@ def test_certificates_rules():
False,
verifier.Codes.NOT_VALID,
)

# Verify exemption rules
exemption_certificate = dcc_without_vaccinations
del exemption_certificate._payload["v"]
exemption_certificate._payload["e"] = [
{
"df": "2021-02-15",
"du": "2021-12-15",
"co": "IT",
"ci": "TESTIDFAKEEXEMPTION#2",
"is": "",
"tg": "",
}
]

traveller = time_machine.travel(dt.datetime(2021, 5, 22))
traveller.start()
verify_rules_from_certificate(
exemption_certificate,
True,
verifier.Codes.VALID,
r"^Exemption is valid \[2021-02-15 - 2021-12-15\]$",
)

verify_rules_from_certificate(
exemption_certificate,
False,
verifier.Codes.TEST_NEEDED,
"^Test needed$",
verifier.Mode.BOOSTER_DGP,
)
traveller.stop()
traveller = time_machine.travel(dt.datetime(2022, 5, 23))
traveller.start()
verify_rules_from_certificate(
exemption_certificate,
False,
verifier.Codes.NOT_VALID,
"^Exemption is expired at: 2021-12-15$",
)
traveller.stop()
traveller = time_machine.travel(dt.datetime(2020, 5, 22))
traveller.start()
verify_rules_from_certificate(
exemption_certificate,
False,
verifier.Codes.NOT_VALID_YET,
"^Exemption is not valid yet, starts at: 2021-02-15$",
)
traveller.stop()
50 changes: 46 additions & 4 deletions verificac19/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _check_vaccination(self, dcc: dcc.DCC, mode: Mode):
return Result(
self.Codes.NOT_EU_DCC,
False,
"No vaccination, test or recovery statement found in payload",
"No vaccination, test, recovery or exemption statement found in payload",
)
last = payload["v"][-1]
vaccine_type = last["mp"]
Expand Down Expand Up @@ -205,7 +205,7 @@ def _check_test(self, dcc: dcc.DCC, mode: Mode):
return Result(
self.Codes.NOT_EU_DCC,
False,
"No vaccination, test or recovery statement found in payload",
"No vaccination, test, recovery or exemption statement found in payload",
)
if mode != self.Mode.NORMAL_DGP:
return Result(
Expand Down Expand Up @@ -262,7 +262,7 @@ def _check_recovery(self, dcc: dcc.DCC, mode: Mode):
return Result(
self.Codes.NOT_EU_DCC,
False,
"No vaccination, test or recovery statement found in payload",
"No vaccination, test, recovery or exemption statement found in payload",
)
if mode == self.Mode.BOOSTER_DGP:
return Result(
Expand Down Expand Up @@ -312,6 +312,46 @@ def _check_recovery(self, dcc: dcc.DCC, mode: Mode):
"Recovery statement is valid",
)

def _check_exemption(self, dcc: dcc.DCC, mode: Mode):
payload = dcc.payload
if len(payload["e"]) == 0:
return Result(
self.Codes.NOT_EU_DCC,
False,
"No vaccination, test, recovery, or exemption statement found in payload",
)
if mode == self.Mode.BOOSTER_DGP:
return Result(
self.Codes.TEST_NEEDED,
False,
"Test needed",
)

last = payload["e"][-1]

start_date = datetime.strptime(last["df"], "%Y-%m-%d")
end_date = datetime.strptime(last["du"], "%Y-%m-%d")
now = datetime.now()

if start_date > now:
return Result(
self.Codes.NOT_VALID_YET,
False,
f"Exemption is not valid yet, starts at: {start_date.strftime('%Y-%m-%d')}",
)
if now > end_date:
return Result(
self.Codes.NOT_VALID,
False,
f"Exemption is expired at: {end_date.strftime('%Y-%m-%d')}",
)

return Result(
self.Codes.VALID,
True,
f"Exemption is valid [{start_date.strftime('%Y-%m-%d')} - {end_date.strftime('%Y-%m-%d')}]",
)

def _format_dsc(self, dsc: str) -> str:
return ("-----BEGIN CERTIFICATE-----\n{}\n-----END CERTIFICATE-----").format(
dsc
Expand Down Expand Up @@ -354,14 +394,16 @@ def _verify_rules(self, dcc_obj: dcc.DCC, mode: Mode):
result = self._check_test(dcc_obj, mode)
elif "r" in payload:
result = self._check_recovery(dcc_obj, mode)
elif "e" in payload:
result = self._check_exemption(dcc_obj, mode)
else:
result = Result(
self.Codes.NOT_EU_DCC,
False,
"No vaccination, test or recovery statement found in payload",
)
if result._result and not any(
self._verify_uvci(payload, t) for t in ["v", "t", "r"]
self._verify_uvci(payload, t) for t in ["v", "t", "r", "e"]
):
result = Result(
self.Codes.NOT_VALID,
Expand Down

0 comments on commit ab6cb81

Please sign in to comment.