Commit 818f57e0 authored by Volker Krause's avatar Volker Krause
Browse files

Route all health certificate imports through ApplicationController

This means we are now using the unified import code there for all content.
parent c53c7e92
......@@ -7,6 +7,8 @@
#include <config-itinerary.h>
#include <applicationcontroller.h>
#include <healthcertificatemanager.h>
#include <pkpassmanager.h>
#include <reservationmanager.h>
#include <QAbstractItemModelTester>
#include <QSignalSpy>
......@@ -31,7 +33,12 @@ private Q_SLOTS:
void testManager()
{
PkPassManager passMgr;
ReservationManager resMgr;
ApplicationController ctrl;
ctrl.setPkPassManager(&passMgr);
ctrl.setReservationManager(&resMgr);
QSignalSpy infoSpy(&ctrl, &ApplicationController::infoMessage);
{
HealthCertificateManager mgr;
......@@ -46,39 +53,51 @@ private Q_SLOTS:
HealthCertificateManager mgr;
QAbstractItemModelTester modelTester(&mgr);
QSignalSpy insertSpy(&mgr, &QAbstractItemModel::rowsInserted);
ctrl.setHealthCertificateManager(&mgr);
QCOMPARE(mgr.rowCount(), 0);
const auto rawData = readFile(QLatin1String(SOURCE_DIR "/data/health-certificates/full-vaccination.txt"));
#if HAVE_KHEALTHCERTIFICATE
QVERIFY(mgr.importCertificate(rawData));
ctrl.importData(rawData);
QCOMPARE(mgr.rowCount(), 1);
QCOMPARE(insertSpy.size(), 1);
QVERIFY(!mgr.data(mgr.index(0, 0), Qt::DisplayRole).toString().isEmpty());
QVERIFY(!mgr.data(mgr.index(0, 0), HealthCertificateManager::CertificateRole).isNull());
QCOMPARE(mgr.data(mgr.index(0, 0), HealthCertificateManager::RawDataRole).toByteArray(), rawData);
QVERIFY(!mgr.data(mgr.index(0, 0), HealthCertificateManager::StorageIdRole).toString().isEmpty());
QCOMPARE(infoSpy.size(), 1);
infoSpy.clear();
#endif
}
{
HealthCertificateManager mgr;
QAbstractItemModelTester modelTester(&mgr);
ctrl.setHealthCertificateManager(&mgr);
#if HAVE_KHEALTHCERTIFICATE
QCOMPARE(mgr.rowCount(), 1);
const auto rawData = readFile(QLatin1String(SOURCE_DIR "/data/health-certificates/full-vaccination.txt"));
// no duplicates
QVERIFY(mgr.importCertificate(rawData));
ctrl.importFromUrl(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/health-certificates/full-vaccination.txt")));
QCOMPARE(mgr.rowCount(), 1);
QCOMPARE(infoSpy.size(), 1);
infoSpy.clear();
#endif
}
{
HealthCertificateManager mgr;
QAbstractItemModelTester modelTester(&mgr);
ctrl.setHealthCertificateManager(&mgr);
#if HAVE_KHEALTHCERTIFICATE
QCOMPARE(mgr.rowCount(), 1);
// garbage is rejected
QVERIFY(!mgr.importCertificate("not a vaccination certificate"));
ctrl.importData("not a vaccination certificate");
QCOMPARE(mgr.rowCount(), 1);
QCOMPARE(infoSpy.size(), 1);
infoSpy.clear();
#endif
}
......@@ -86,16 +105,20 @@ private Q_SLOTS:
HealthCertificateManager mgr;
QAbstractItemModelTester modelTester(&mgr);
QSignalSpy insertSpy(&mgr, &QAbstractItemModel::rowsInserted);
ctrl.setHealthCertificateManager(&mgr);
#if HAVE_KHEALTHCERTIFICATE
QCOMPARE(mgr.rowCount(), 1);
const auto rawData = readFile(QLatin1String(SOURCE_DIR "/data/health-certificates/partial-vaccination.divoc"));
QVERIFY(mgr.importCertificate(rawData));
ctrl.importFromUrl(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/health-certificates/partial-vaccination.divoc")));
QCOMPARE(mgr.rowCount(), 2);
QCOMPARE(insertSpy.size(), 1);
QVERIFY(!mgr.data(mgr.index(0, 0), Qt::DisplayRole).toString().isEmpty());
QVERIFY(!mgr.data(mgr.index(0, 0), HealthCertificateManager::CertificateRole).isNull());
QCOMPARE(mgr.data(mgr.index(0, 0), HealthCertificateManager::RawDataRole).toByteArray(), rawData);
QVERIFY(!mgr.data(mgr.index(0, 0), HealthCertificateManager::StorageIdRole).toString().isEmpty());
QCOMPARE(infoSpy.size(), 1);
infoSpy.clear();
#endif
}
......@@ -109,6 +132,9 @@ private Q_SLOTS:
ctrl.importFromUrl(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/health-certificates/negative-pcr-test-fr.pdf")));
QCOMPARE(mgr.rowCount(), 3);
QCOMPARE(insertSpy.size(), 1);
QCOMPARE(infoSpy.size(), 1);
infoSpy.clear();
#endif
}
......
......@@ -330,18 +330,18 @@ void ApplicationController::importLocalFile(const QUrl &url)
return;
}
// deal with things we can import more efficiently from a file directly
const auto head = f.peek(4);
if (url.fileName().endsWith(QLatin1String(".pkpass"), Qt::CaseInsensitive)) {
m_pkPassMgr->importPass(url);
} else if (url.fileName().endsWith(QLatin1String(".itinerary"), Qt::CaseInsensitive)) {
importBundle(url);
} else if (FileHelper::hasZipHeader(head)) {
if (m_pkPassMgr->importPass(url).isEmpty()) {
importBundle(url);
if (FileHelper::hasZipHeader(head)) {
if (url.fileName().endsWith(QLatin1String(".pkpass"), Qt::CaseInsensitive) && !m_pkPassMgr->importPass(url).isEmpty()) {
return;
}
if (url.fileName().endsWith(QLatin1String(".itinerary"), Qt::CaseInsensitive) && importBundle(url)) {
return;
}
} else {
importData(f.readAll(), f.fileName());
}
importData(f.readAll(), f.fileName());
}
void ApplicationController::importData(const QByteArray &data, const QString &fileName)
......@@ -350,42 +350,53 @@ void ApplicationController::importData(const QByteArray &data, const QString &fi
if (data.size() < 4) {
return;
}
if (FileHelper::hasZipHeader(data)) {
if (m_pkPassMgr->importPassFromData(data).isEmpty()) {
importBundle(data);
if (!m_pkPassMgr->importPassFromData(data).isEmpty()) {
return;
}
} else {
using namespace KItinerary;
ExtractorEngine engine;
engine.setContextDate(QDateTime(QDate::currentDate(), QTime(0, 0)));
engine.setData(data, fileName);
const auto resIds = m_resMgr->importReservations(JsonLdDocument::fromJson(engine.extract()));
if (!resIds.isEmpty()) {
// check if there is a document we want to attach here
QMimeDatabase db;
const auto mt = db.mimeTypeForFileNameAndData(fileName, data);
if (mt.name() == QLatin1String("application/pdf")) { // TODO support more file types (however we certainly want to exclude pkpass and json here)
DigitalDocument docInfo;
docInfo.setName(fileName);
docInfo.setEncodingFormat(mt.name());
const auto docId = DocumentUtil::idForContent(data);
m_docMgr->addDocument(docId, docInfo, data);
for (const auto &resId : resIds) {
auto res = m_resMgr->reservation(resId);
if (DocumentUtil::addDocumentId(res, docId)) {
m_resMgr->updateReservation(resId, res);
}
if (importBundle(data)) {
return;
}
}
using namespace KItinerary;
ExtractorEngine engine;
engine.setContextDate(QDateTime(QDate::currentDate(), QTime(0, 0)));
engine.setData(data, fileName);
const auto resIds = m_resMgr->importReservations(JsonLdDocument::fromJson(engine.extract()));
if (!resIds.isEmpty()) {
// check if there is a document we want to attach here
QMimeDatabase db;
const auto mt = db.mimeTypeForFileNameAndData(fileName, data);
if (mt.name() == QLatin1String("application/pdf")) { // TODO support more file types (however we certainly want to exclude pkpass and json here)
DigitalDocument docInfo;
docInfo.setName(fileName);
docInfo.setEncodingFormat(mt.name());
const auto docId = DocumentUtil::idForContent(data);
m_docMgr->addDocument(docId, docInfo, data);
for (const auto &resId : resIds) {
auto res = m_resMgr->reservation(resId);
if (DocumentUtil::addDocumentId(res, docId)) {
m_resMgr->updateReservation(resId, res);
}
}
Q_EMIT infoMessage(i18np("One reservation imported.", "%1 reservations imported.", resIds.size()));
return;
}
// look for health certificate barcodes instead
importHealthCertificateRecursive(engine.rootDocumentNode());
Q_EMIT infoMessage(i18np("One reservation imported.", "%1 reservations imported.", resIds.size()));
return;
}
// look for health certificate barcodes instead
// if we don't find anything, try to import as health certificate directly
if (importHealthCertificateRecursive(engine.rootDocumentNode()) || m_healthCertMgr->importCertificate(data)) {
Q_EMIT infoMessage(i18n("Health certificate imported."));
return;
}
// nothing found
Q_EMIT infoMessage(i18n("Nothing imported."));
}
void ApplicationController::checkCalendar()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment