[hebe-python] Add tests

This commit is contained in:
Kuba Szczodrzyński 2020-10-06 20:16:44 +02:00
parent 521fa13ce2
commit 82d5d4189c
4 changed files with 78 additions and 0 deletions

View file

@ -56,6 +56,15 @@ jobs:
path: ~/signer
- run: pip install --user .[testing]
- run: python -m pytest .
python-hebe:
working_directory: ~/signer/python-hebe
docker:
- image: circleci/python:3.8
steps:
- checkout:
path: ~/signer
- run: pip install --user .[testing]
- run: python -m pytest .
dotnet:
working_directory: ~/signer/dotnet
@ -86,5 +95,6 @@ workflows:
- jvm
- jvm-hebe
- python
- python-hebe
- dotnet
- dart-hebe

View file

@ -16,3 +16,9 @@ from datetime import datetime
digest, canonical_url, signature = get_signature_values(fingerprint, private_key, body, full_url, datetime.now())
```
## Tests
```console
$ python -m pytest .
```

View file

@ -30,4 +30,5 @@ setuptools.setup(
],
packages=setuptools.find_packages(),
install_requires=["pyopenssl"],
extras_require={"testing": ["pytest"]},
)

View file

@ -0,0 +1,61 @@
from uonet_request_signer_hebe import get_signature_values
import pytest
from datetime import datetime, timezone
fingerprint = "7EBA57E1DDBA1C249D097A9FF1C9CCDD45351A6A"
private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDCbF5Tt176EpB4cX5U+PZE0XytjJ9ABDZFaBFDkaexbkuNeuLOaARjQEOlUoBmpZQXxAF8HlYqeTvPiTcnSfQIS6EdqpICuQNdwvy6CHFAe2imkbbB0aHPsGep6zH8ZxHbssazkTCnGy0j2ZtGT2/iy1GEvc/p2bOkCVcR1H1GqFp+/XpfaMwi2SRCwc67K8Fu8TjSDKNvcRT9nuenGoPA1CWoOiOCxhQA6gnB8LULPel6TVDxeBVdYor/z2GxFe/m0pa7XAKzveuUDhH8k8NlNG65MjvZhgy9iFs+eBUq7lCZ0nuIsDzjnUrLSl4ciYKj9d94qrUyF8L8D9Rl+0WlAgMBAAECggEAQ6jg3rNmyxIg0rl0ZG/LjEF26RKR7P5KQLcpouESga3HfzHvsjMCq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NBDiAvb5lZmMB/B3CzoiMQOwczuus8Xsx7bEci28DzQ+g2zt0/bC2Xl+992Ass5PP5NtOrP/9QiTNgoFSCrVnZnNzQqpjCrFsjfOD2fiuFLCD6zi6"
body = "{}"
full_url = "/powiatwulkanowy/123456/api/mobile/register/hebe"
timestamp = datetime(2020, 4, 14, 4, 14, 16)
expected_digest = "SHA-256=RBNvo1WzZ4oRRq0W9+hknpT7T8If536DEMBg9hyq/4o="
expected_canonical_url = "api%2fmobile%2fregister%2fhebe"
expected_signature = ','.join([
'keyId="7EBA57E1DDBA1C249D097A9FF1C9CCDD45351A6A"',
'headers="vCanonicalUrl Digest vDate"',
'algorithm="sha256withrsa"',
'signature=Base64(SHA256withRSA(mIVNkthTzTHmmXG1qxv1Jpt3uRlyhbj7VHysbCNpl0zXCCzuwTXsuCrfjexDDXsyJVo/LznQKOyvOaW4tEfrBobxtbtTnp7zYi54bdvAZa3pvM02yvkH4i/DvTLDKRO0R9UDZ1LraGrOTsIe3m3mQ21NOynVqCKadeqod8Y7l4YUlVYEmrtq/7xbCwr0qdne6G67eY4Amj6ffbG3TkVLpUrEETBnAC7oFjGYKhcRyvltAi+lcv6omANz1gwELf+Vmsa8NwFo/YGwY3R23z15athU/1iC1JcrECBLC8nRM1+KlvyIqx2HX6RG5R1cMOwBWVg6pRKUdrhxYbQ+VQ8Cag==))'
])
expected_signature_no_body = ','.join([
'keyId="7EBA57E1DDBA1C249D097A9FF1C9CCDD45351A6A"',
'headers="vCanonicalUrl vDate"',
'algorithm="sha256withrsa"',
'signature=Base64(SHA256withRSA(An1Fkg2OjCNOjGMJRG/YmAGGsqzXT00kkRaLq54U30LS8B9yf6f1wjumg4tCxi3x6aGrnf7pKiuhQgHTAbBAGJzwIBGrVcch24pBTPHfZsibOiNJVNu0s6wP8wCDq97JRCUCti9kyt8hcIZiicOVL+wDgV6NAFYkloAKTdEUBC6tqeFObgktr4Qhxa/khKtD4/hwKTSEJFIQKFtOlAuekXFNafdQwekFFmXtKNd69b1n3p39XVHe0bM5AJVNr0kMvN6M97/8RE4Tz62CXKUVla6XyY7n+osrFMnTAGGtXq15FjS3UI6ObUmx9dHJHREn2GdB1DY3KCCv46O1zaRq4A==))'
])
TEST_DATA = [
( # with a POST body
fingerprint,
private_key,
body,
full_url,
timestamp,
expected_digest,
expected_canonical_url,
expected_signature
),
( # without a POST body
fingerprint,
private_key,
None, # body
full_url,
timestamp,
None, # digest
expected_canonical_url,
expected_signature_no_body
)
]
def test_invalid_url():
with pytest.raises(ValueError):
get_signature_values('', '', '', 'invalid url', datetime.now())
@pytest.mark.parametrize("fingerprint,private_key,body,full_url,timestamp,expected_digest,expected_canonical_url,expected_signature", TEST_DATA)
def test_signature(fingerprint, private_key, body, full_url, timestamp, expected_digest, expected_canonical_url, expected_signature):
digest, canonical_url, signature = get_signature_values(fingerprint, private_key, body, full_url, timestamp)
assert expected_digest == digest
assert expected_canonical_url == canonical_url
assert expected_signature == signature