passerelle/tests/test_qrcode.py

101 lines
3.5 KiB
Python

# passerelle - uniform access to multiple data sources and services
# Copyright (C) 2022 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from datetime import timezone
import pytest
from passerelle.apps.qrcode.models import QRCodeConnector
from tests.utils import generic_endpoint_url, setup_access_rights
@pytest.fixture()
def connector(db):
return setup_access_rights(
QRCodeConnector.objects.create(
slug='test',
key='5e8176e50d45b67e9db875d6006edf3ba805ff4ef4d945327012db4c797be1be',
)
)
def test_save_certificate(app, connector):
endpoint = generic_endpoint_url('qrcode', 'save-certificate', slug=connector.slug)
result = app.post_json(
endpoint,
params={
'data': {
'first_name': 'Georges',
'last_name': 'Abitbol',
},
'validity_start': '2022-01-01 10:00:00+00:00',
'validity_end': '2023-01-01 10:00:00+00:00',
},
)
assert result.json['err'] == 0
certificate_uuid = result.json['data']['uuid']
certificate = connector.certificates.get(uuid=certificate_uuid)
assert certificate.data['first_name'] == 'Georges'
assert certificate.data['last_name'] == 'Abitbol'
assert certificate.validity_start == datetime.datetime(2022, 1, 1, 10, 0, 0, 0, tzinfo=timezone.utc)
assert certificate.validity_end == datetime.datetime(2023, 1, 1, 10, 0, 0, 0, tzinfo=timezone.utc)
result = app.post_json(
f'{endpoint}/{certificate_uuid}',
params={
'data': {
'first_name': 'Robert',
'last_name': 'Redford',
},
'validity_start': '2024-01-01T10:00:00+00:00',
'validity_end': '2025-01-01T10:00:00+00:00',
},
)
certificate.refresh_from_db()
assert certificate.data['first_name'] == 'Robert'
assert certificate.data['last_name'] == 'Redford'
assert certificate.validity_start == datetime.datetime(2024, 1, 1, 10, 0, 0, 0, tzinfo=timezone.utc)
assert certificate.validity_end == datetime.datetime(2025, 1, 1, 10, 0, 0, 0, tzinfo=timezone.utc)
def test_get_certificate(app, connector):
certificate = connector.certificates.create(
data={
'first_name': 'Georges',
'last_name': 'Abitbol',
},
validity_start=datetime.datetime(2022, 1, 1, 10, 0, 0, 0, tzinfo=timezone.utc),
validity_end=datetime.datetime(2023, 1, 1, 10, 0, 0, 0, tzinfo=timezone.utc),
)
endpoint = generic_endpoint_url('qrcode', 'get-certificate', slug=connector.slug)
result = app.get(f'{endpoint}/{certificate.uuid}')
assert result.json == {
'err': 0,
'data': {
'uuid': str(certificate.uuid),
'data': {'first_name': 'Georges', 'last_name': 'Abitbol'},
'validity_start': '2022-01-01T10:00:00+00:00',
'validity_end': '2023-01-01T10:00:00+00:00',
},
}