2017-07-03 09:30:10 +02:00
|
|
|
/*!
|
|
|
|
* \brief Unit tests for \ref MsgHandlerAccessRights
|
|
|
|
*
|
2019-05-22 10:08:38 +02:00
|
|
|
* \copyright Copyright (c) 2016-2019 Governikus GmbH & Co. KG, Germany
|
2017-07-03 09:30:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "messages/MsgHandlerAccessRights.h"
|
|
|
|
|
|
|
|
#include "InternalActivationContext.h"
|
|
|
|
#include "MessageDispatcher.h"
|
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
#include "TestAuthContext.h"
|
2017-07-03 09:30:10 +02:00
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
using namespace governikus;
|
|
|
|
|
|
|
|
class test_MsgHandlerAccessRights
|
|
|
|
: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
QSharedPointer<CHAT> getChat(const std::initializer_list<AccessRight>& pList)
|
|
|
|
{
|
|
|
|
auto chat = newObject<CHAT>();
|
|
|
|
chat->setAccessRights(QSet<AccessRight>(pList));
|
|
|
|
return chat;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
QSharedPointer<TestAuthContext> getContextWithChat()
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
2017-07-03 09:33:28 +02:00
|
|
|
QSharedPointer<TestAuthContext> context(new TestAuthContext(new InternalActivationContext(QUrl("http://dummy")), ":/paos/DIDAuthenticateEAC1.xml"));
|
2017-09-15 10:23:30 +02:00
|
|
|
context->setRequiredAccessRights({AccessRight::READ_DG01, AccessRight::READ_DG04, AccessRight::READ_DG17});
|
2017-07-03 09:33:28 +02:00
|
|
|
context->setOptionalAccessRights({AccessRight::AGE_VERIFICATION, AccessRight::READ_DG05});
|
2017-07-03 09:30:10 +02:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray getAux() const
|
|
|
|
{
|
|
|
|
const QDate today = QDateTime::currentDateTimeUtc().date();
|
|
|
|
const QDate ageVerificationDate = QDate(1992, 12, 6);
|
|
|
|
|
|
|
|
const QByteArray age =
|
|
|
|
(today.month() > ageVerificationDate.month() || (today.month() == ageVerificationDate.month() && today.day() >= ageVerificationDate.day()))
|
|
|
|
? QByteArray::number(today.year() - ageVerificationDate.year())
|
|
|
|
: QByteArray::number(today.year() - ageVerificationDate.year() - 1);
|
|
|
|
|
|
|
|
QByteArray aux(R"({"aux":{"ageVerificationDate":"%1","communityId":"02760400110000","requiredAge":"%2","validityDate":"2013-12-06"},)");
|
|
|
|
aux.replace(QByteArrayLiteral("%1"), ageVerificationDate.toString(Qt::ISODate).toLatin1());
|
|
|
|
aux.replace(QByteArrayLiteral("%2"), age);
|
|
|
|
return aux;
|
|
|
|
}
|
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
|
2017-07-03 09:30:10 +02:00
|
|
|
private Q_SLOTS:
|
2017-09-15 10:23:30 +02:00
|
|
|
void nonExistingTransactionInfo()
|
|
|
|
{
|
|
|
|
QSharedPointer<TestAuthContext> context(new TestAuthContext(new InternalActivationContext(QUrl("http://dummy")), ":/paos/DIDAuthenticateEAC1_2.xml"));
|
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
dispatcher.init(context);
|
|
|
|
|
|
|
|
QCOMPARE(dispatcher.processStateChange("StateEditAccessRights"),
|
|
|
|
QByteArray(R"({"chat":{"effective":["ResidencePermitI","Address","BirthName","Nationality","PlaceOfBirth","DateOfBirth","DoctoralDegree","ArtisticName","FamilyName","GivenNames","ValidUntil","IssuingCountry","DocumentType","Pseudonym"],"optional":["ResidencePermitI","Address","BirthName","Nationality","PlaceOfBirth","DateOfBirth","DoctoralDegree","ArtisticName","FamilyName","GivenNames","ValidUntil","IssuingCountry","DocumentType","Pseudonym"],"required":[]},"msg":"ACCESS_RIGHTS"})"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 09:30:10 +02:00
|
|
|
void state()
|
|
|
|
{
|
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
dispatcher.init(getContextWithChat());
|
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processStateChange("StateEditAccessRights"),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void getAccessRightsBadState()
|
|
|
|
{
|
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
const auto& msg = QByteArray(R"( {"cmd": "GET_ACCESS_RIGHTS"} )");
|
|
|
|
QCOMPARE(dispatcher.processCommand(msg), QByteArray(R"({"error":"GET_ACCESS_RIGHTS","msg":"BAD_STATE"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
|
|
|
|
dispatcher.init(getContextWithChat());
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processCommand(msg), QByteArray(R"({"error":"GET_ACCESS_RIGHTS","msg":"BAD_STATE"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void acceptAccessRights()
|
|
|
|
{
|
|
|
|
const auto& context = getContextWithChat();
|
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
dispatcher.init(context);
|
|
|
|
|
|
|
|
QVERIFY(!context->isStateApproved());
|
|
|
|
QVERIFY(!dispatcher.processStateChange("StateEditAccessRights").isEmpty());
|
|
|
|
|
|
|
|
QVERIFY(!context->isStateApproved());
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "GET_ACCESS_RIGHTS"} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "ACCEPT"} )")), QByteArray());
|
2017-07-03 09:30:10 +02:00
|
|
|
QVERIFY(context->isStateApproved());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void getAccessRights()
|
|
|
|
{
|
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
dispatcher.init(getContextWithChat());
|
|
|
|
|
|
|
|
QVERIFY(!dispatcher.processStateChange("StateEditAccessRights").isEmpty());
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "GET_ACCESS_RIGHTS"} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-09-15 10:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setAccessRights_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QByteArray>("cmd");
|
|
|
|
QTest::addColumn<QByteArray>("msg");
|
|
|
|
|
|
|
|
QTest::newRow("chat_invalid") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["8",11]} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"error":"Entry in 'chat' data needs to be string","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-09-15 10:23:30 +02:00
|
|
|
|
|
|
|
QTest::newRow("chat_needs_be_string") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": [8,"11"]} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"error":"Entry in 'chat' data is invalid","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-09-15 10:23:30 +02:00
|
|
|
|
|
|
|
QTest::newRow("chat_unknown_id") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["y", "123"]} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"error":"Entry in 'chat' data is invalid","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-09-15 10:23:30 +02:00
|
|
|
|
|
|
|
QTest::newRow("chat_set_optional") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["FamilyName"]} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-09-15 10:23:30 +02:00
|
|
|
|
|
|
|
QTest::newRow("chat_mixed_valid_and_required") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["AgeVerification", "GivenNames"]} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"error":"Entry in 'chat' data is not available","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-09-15 10:23:30 +02:00
|
|
|
|
|
|
|
QTest::newRow("chat_both_optional") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["AgeVerification", "FamilyName"]} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-09-15 10:23:30 +02:00
|
|
|
|
|
|
|
QTest::newRow("chat_single_optional") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["AgeVerification"]} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-09-15 10:23:30 +02:00
|
|
|
|
|
|
|
QTest::newRow("chat_disable_optional") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": []} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","GivenNames","DocumentType"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setAccessRights()
|
|
|
|
{
|
2017-09-15 10:23:30 +02:00
|
|
|
QFETCH(QByteArray, cmd);
|
|
|
|
QFETCH(QByteArray, msg);
|
|
|
|
|
2017-07-03 09:30:10 +02:00
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
dispatcher.init(getContextWithChat());
|
|
|
|
QVERIFY(!dispatcher.processStateChange("StateEditAccessRights").isEmpty());
|
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
// check original state
|
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "GET_ACCESS_RIGHTS"} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
// check cmd
|
|
|
|
QCOMPARE(dispatcher.processCommand(cmd), msg);
|
|
|
|
}
|
2017-07-03 09:30:10 +02:00
|
|
|
|
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
void setAccessRightsWithMultipleCmds()
|
|
|
|
{
|
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
dispatcher.init(getContextWithChat());
|
|
|
|
QVERIFY(!dispatcher.processStateChange("StateEditAccessRights").isEmpty());
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
// check original state
|
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "GET_ACCESS_RIGHTS"} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
// check cmds
|
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["FamilyName"]} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
// 11 is not valid, 0 is valid ... we do not accept partial valid values!
|
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["AgeVerification", "GivenNames"]} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"error":"Entry in 'chat' data is not available","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setAccessRightsWithoutChat()
|
|
|
|
{
|
2017-07-03 09:33:28 +02:00
|
|
|
const auto& context = getContextWithChat();
|
|
|
|
context->setOptionalAccessRights({});
|
2017-07-03 09:30:10 +02:00
|
|
|
|
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
dispatcher.init(context);
|
|
|
|
QVERIFY(!dispatcher.processStateChange("StateEditAccessRights").isEmpty());
|
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["AgeVerification"]} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","GivenNames","DocumentType"],"optional":[],"required":["Address","GivenNames","DocumentType"]},"error":"No optional access rights available","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
context->setOptionalAccessRights({AccessRight::AGE_VERIFICATION});
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["AgeVerification"]} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","GivenNames","DocumentType","AgeVerification"],"optional":["AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
context->setRequiredAccessRights({AccessRight::AGE_VERIFICATION, AccessRight::READ_DG17});
|
2017-07-03 09:33:28 +02:00
|
|
|
context->setOptionalAccessRights({});
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processCommand(QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": ["AgeVerification"]} )")),
|
2017-12-20 14:54:05 +01:00
|
|
|
getAux() + QByteArray(R"("chat":{"effective":["Address","AgeVerification"],"optional":[],"required":["Address","AgeVerification"]},"error":"No optional access rights available","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})"));
|
2017-09-15 10:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void crap_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QByteArray>("cmd");
|
|
|
|
QTest::addColumn<QByteArray>("msg");
|
|
|
|
|
|
|
|
QTest::newRow("chat_null") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "chat": null} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"error":"Invalid 'chat' data","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-09-15 10:23:30 +02:00
|
|
|
|
|
|
|
QTest::newRow("CHAT") << QByteArray(R"( {"cmd": "SET_ACCESS_RIGHTS", "CHAT": []} )")
|
2017-12-20 14:54:05 +01:00
|
|
|
<< getAux() + QByteArray(R"("chat":{"effective":["Address","FamilyName","GivenNames","DocumentType","AgeVerification"],"optional":["FamilyName","AgeVerification"],"required":["Address","GivenNames","DocumentType"]},"error":"'chat' cannot be undefined","msg":"ACCESS_RIGHTS","transactionInfo":"this is a test for TransactionInfo"})");
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void crap()
|
|
|
|
{
|
2017-09-15 10:23:30 +02:00
|
|
|
QFETCH(QByteArray, cmd);
|
|
|
|
QFETCH(QByteArray, msg);
|
|
|
|
|
2017-07-03 09:30:10 +02:00
|
|
|
MessageDispatcher dispatcher;
|
|
|
|
dispatcher.init(getContextWithChat());
|
|
|
|
QVERIFY(!dispatcher.processStateChange("StateEditAccessRights").isEmpty());
|
|
|
|
|
2017-09-15 10:23:30 +02:00
|
|
|
QCOMPARE(dispatcher.processCommand(cmd), msg);
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(test_MsgHandlerAccessRights)
|
|
|
|
#include "test_MsgHandlerAccessRights.moc"
|