1 /*!
2  * \brief Unit tests for \ref SelfAuthModel
3  *
4  * \copyright Copyright (c) 2018-2021 Governikus GmbH & Co. KG, Germany
5  */
6 
7 #include "SelfAuthModel.h"
8 
9 #include "context/SelfAuthContext.h"
10 
11 #include "MockCardConnectionWorker.h"
12 #include "TestFileHelper.h"
13 
14 #include <QtTest>
15 
16 
17 using namespace governikus;
18 
19 class test_SelfAuthModel
20 	: public QObject
21 {
22 	Q_OBJECT
23 	QSharedPointer<SelfAuthContext> mContext;
24 	SelfAuthModel* mModel;
25 
26 	private Q_SLOTS:
init()27 		void init()
28 		{
29 			mContext.reset(new SelfAuthContext());
30 			mModel = Env::getSingleton<SelfAuthModel>();
31 		}
32 
33 
cleanup()34 		void cleanup()
35 		{
36 			mContext.clear();
37 			mModel->resetContext();
38 		}
39 
40 
test_StartWorkflow()41 		void test_StartWorkflow()
42 		{
43 			QSignalSpy spy(mModel, &SelfAuthModel::fireStartWorkflow);
44 
45 			mModel->startWorkflow();
46 			QCOMPARE(spy.count(), 1);
47 		}
48 
49 
test_CancelWorkflow()50 		void test_CancelWorkflow()
51 		{
52 			QSignalSpy spy(mContext.data(), &SelfAuthContext::fireCancelWorkflow);
53 
54 			mModel->cancelWorkflow();
55 			QCOMPARE(spy.count(), 0);
56 
57 			mModel->resetContext(mContext);
58 			mModel->cancelWorkflow();
59 			QCOMPARE(spy.count(), 1);
60 		}
61 
62 
test_IsBasicReader()63 		void test_IsBasicReader()
64 		{
65 
66 			QThread workerThread;
67 			workerThread.start();
68 
69 			QVERIFY(mModel->isBasicReader());
70 
71 			const QSharedPointer<MockCardConnectionWorker> worker(new MockCardConnectionWorker());
72 			worker->moveToThread(&workerThread);
73 			const QSharedPointer<CardConnection> connection(new CardConnection(worker));
74 			mContext->setCardConnection(connection);
75 			ReaderInfo info;
76 
77 			info.setBasicReader(true);
78 			Q_EMIT worker->fireReaderInfoChanged(info);
79 			mModel->resetContext(mContext);
80 			QVERIFY(mModel->isBasicReader());
81 
82 			info.setBasicReader(false);
83 			Q_EMIT worker->fireReaderInfoChanged(info);
84 			QVERIFY(!mModel->isBasicReader());
85 
86 			workerThread.quit();
87 			workerThread.wait();
88 		}
89 
90 
test_RoleNames()91 		void test_RoleNames()
92 		{
93 			QVERIFY(mModel->roleNames().keys().contains(SelfAuthModel::DataRoles::NAME));
94 			QVERIFY(mModel->roleNames().keys().contains(SelfAuthModel::DataRoles::VALUE));
95 			QVERIFY(mModel->roleNames().values().contains("name"));
96 			QVERIFY(mModel->roleNames().values().contains("value"));
97 		}
98 
99 
test_OnSelfAuthenticationDataChanged()100 		void test_OnSelfAuthenticationDataChanged()
101 		{
102 			const auto& data = TestFileHelper::readFile(":/self/SelfAuthenticationDataNoAddress.json");
103 			SelfAuthenticationData selfAuthenticationData(data);
104 			mContext->setSelfAuthenticationData(selfAuthenticationData);
105 			mModel->resetContext(mContext);
106 			for (int i = 0; i < selfAuthenticationData.getOrderedSelfData().length(); i++)
107 			{
108 				QModelIndex index = mModel->index(i, 0);
109 				QCOMPARE(mModel->data(index, SelfAuthModel::DataRoles::NAME), selfAuthenticationData.getOrderedSelfData().at(i).first);
110 				QCOMPARE(mModel->data(index, SelfAuthModel::DataRoles::VALUE), selfAuthenticationData.getOrderedSelfData().at(i).second);
111 				QCOMPARE(mModel->data(index, 100), QVariant());
112 			}
113 		}
114 
115 
116 };
117 
118 QTEST_GUILESS_MAIN(test_SelfAuthModel)
119 #include "test_SelfAuthModel.moc"
120