1717#include < QtTest/QtTest>
1818#include < QtTest/QSignalSpy>
1919#include < QObject>
20+ #include < QDesktopServices>
2021#include < QDir>
2122#include < QFile>
2223#include < QString>
@@ -40,15 +41,24 @@ class TestQgsAuthManager: public QObject
4041 private slots:
4142 void initTestCase ();
4243 void cleanupTestCase ();
43- void init () {}
44+ void init ();
4445 void cleanup () {}
4546
4647 void testMasterPassword ();
48+ void testAuthConfigs ();
49+ void testAuthMethods ();
4750
4851 private:
52+ void cleanupTempDir ();
53+ QList<QgsAuthMethodConfig> registerAuthConfigs ();
54+
55+ void reportRow ( const QString& msg );
56+ void reportHeader ( const QString& msg );
57+
4958 QString mPkiData ;
5059 QString mTempDir ;
5160 const char * mPass ;
61+ QString mReport ;
5262};
5363
5464
@@ -61,6 +71,10 @@ TestQgsAuthManager::TestQgsAuthManager()
6171
6272void TestQgsAuthManager::initTestCase ()
6373{
74+ cleanupTempDir ();
75+
76+ mReport += " <h1>QgsAuthManager Tests</h1>\n " ;
77+
6478 // make QGIS_AUTH_DB_DIR_PATH temp dir for qgis-auth.db and master password file
6579 QDir tmpDir = QDir::temp ();
6680 QVERIFY2 ( tmpDir.mkpath ( mTempDir ), " Couldn't make temp directory" );
@@ -72,6 +86,10 @@ void TestQgsAuthManager::initTestCase()
7286 QVERIFY2 ( !QgsAuthManager::instance ()->isDisabled (),
7387 " Authentication system is DISABLED" );
7488
89+ QString mySettings = QgsApplication::showSettings ();
90+ mySettings = mySettings.replace ( " \n " , " <br />\n " );
91+ mReport += " <p>" + mySettings + " </p>\n " ;
92+
7593 // verify QGIS_AUTH_DB_DIR_PATH (temp auth db path) worked
7694 QString db1 ( QFileInfo ( QgsAuthManager::instance ()->authenticationDbPath () ).canonicalFilePath () );
7795 QString db2 ( QFileInfo ( mTempDir + " /qgis-auth.db" ).canonicalFilePath () );
@@ -111,9 +129,48 @@ void TestQgsAuthManager::initTestCase()
111129 // all tests should now have a valid qgis-auth.db and stored/set master password
112130}
113131
132+ void TestQgsAuthManager::cleanupTempDir ()
133+ {
134+ QDir tmpDir = QDir ( mTempDir );
135+ if ( tmpDir.exists () )
136+ {
137+ Q_FOREACH ( const QString &tf, tmpDir.entryList ( QDir::NoDotAndDotDot | QDir::Files ) )
138+ {
139+ QVERIFY ( tmpDir.remove ( mTempDir + " /" + tf ) );
140+ }
141+ QVERIFY ( tmpDir.rmdir ( mTempDir ) );
142+ }
143+ }
144+
114145void TestQgsAuthManager::cleanupTestCase ()
115146{
147+ cleanupTempDir ();
116148 QgsApplication::exitQgis ();
149+
150+ QString myReportFile = QDir::tempPath () + " /qgistest.html" ;
151+ QFile myFile ( myReportFile );
152+ if ( myFile.open ( QIODevice::WriteOnly ) )
153+ {
154+ QTextStream myQTextStream ( &myFile );
155+ myQTextStream << mReport ;
156+ myFile.close ();
157+ // QDesktopServices::openUrl( "file:///" + myReportFile );
158+ }
159+ }
160+
161+ void TestQgsAuthManager::init ()
162+ {
163+ mReport += " <h2>" + QString ( QTest::currentTestFunction () ) + " </h2>\n " ;
164+ }
165+
166+ void TestQgsAuthManager::reportRow ( const QString& msg )
167+ {
168+ mReport += msg + " <br>\n " ;
169+ }
170+
171+ void TestQgsAuthManager::reportHeader ( const QString& msg )
172+ {
173+ mReport += " <h3>" + msg + " </h3>\n " ;
117174}
118175
119176void TestQgsAuthManager::testMasterPassword ()
@@ -159,5 +216,191 @@ void TestQgsAuthManager::testMasterPassword()
159216 QVERIFY ( spyargs.at ( 0 ).toBool () == true );
160217}
161218
219+ void TestQgsAuthManager::testAuthConfigs ()
220+ {
221+ QList<QgsAuthMethodConfig> configs ( registerAuthConfigs () );
222+ QVERIFY ( !configs.isEmpty () );
223+
224+ QgsAuthManager *authm = QgsAuthManager::instance ();
225+
226+ // test storing/loading/updating
227+ Q_FOREACH ( QgsAuthMethodConfig config, configs )
228+ {
229+ QVERIFY ( config.isValid () );
230+
231+ QVERIFY ( authm->storeAuthenticationConfig ( config ) );
232+
233+ // config should now have a valid, unique ID
234+ QString configid ( config.id () );
235+ QVERIFY ( !configid.isEmpty () );
236+ QVERIFY ( !authm->configIdUnique ( configid ) ); // uniqueness registered, so can't be overridden
237+ QVERIFY ( authm->configIds ().contains ( configid ) );
238+
239+ // config -> method map should have been updated and return the right method key
240+ QCOMPARE ( authm->configAuthMethodKey ( configid ), config.method () );
241+
242+ // loading into new base config should return same as original stored
243+ QgsAuthMethodConfig config2;
244+ QVERIFY ( authm->loadAuthenticationConfig ( configid, config2, false ) );
245+ QVERIFY ( config2.isValid () );
246+ QVERIFY ( config2.configMap ().isEmpty () ); // has no sensitive data
247+
248+ QVERIFY ( authm->loadAuthenticationConfig ( configid, config2, true ) );
249+ QVERIFY ( config2.isValid () );
250+ QVERIFY ( !config2.configMap ().isEmpty () ); // has sensitive data
251+
252+ QVERIFY ( config == config2 );
253+
254+ // values haven't been changed, but db update should take place and values should roundtrip
255+ QVERIFY ( authm->updateAuthenticationConfig ( config2 ) );
256+ QVERIFY ( authm->loadAuthenticationConfig ( configid, config2, true ) );
257+ QVERIFY ( config2.isValid () );
258+ QVERIFY ( !config2.configMap ().isEmpty () ); // has sensitive data
259+ QVERIFY ( config == config2 );
260+
261+ // changed config should update then correctly roundtrip
262+ Q_FOREACH ( const QString &key, config2.configMap ().keys () )
263+ {
264+ config2.setConfig ( key, config2.configMap ().value ( key ) + " changed" );
265+ }
266+ config2.setName ( config2.name () + " changed" );
267+ config2.setUri ( config2.uri () + " changed" );
268+ QVERIFY ( authm->updateAuthenticationConfig ( config2 ) );
269+
270+ QgsAuthMethodConfig config3;
271+ QVERIFY ( authm->loadAuthenticationConfig ( configid, config3, true ) );
272+ QVERIFY ( config3.isValid () );
273+ QVERIFY ( !config3.configMap ().isEmpty () );
274+ QVERIFY ( config != config3 );
275+ QVERIFY ( config2 == config3 );
276+
277+ // config can be deleted
278+ QVERIFY ( authm->removeAuthenticationConfig ( configid ) );
279+ QVERIFY ( !authm->configIds ().contains ( configid ) );
280+ QVERIFY ( authm->configIds ().isEmpty () );
281+
282+ // config with custom id can be stored
283+ QString customid ( authm->uniqueConfigId () );
284+ config2.setId ( customid );
285+
286+ QVERIFY ( authm->storeAuthenticationConfig ( config2 ) );
287+ QCOMPARE ( config2.id (), customid );
288+ QVERIFY ( config2 != config3 ); // id should be different
289+
290+ // custom configid can be deleted
291+ QVERIFY ( authm->removeAuthenticationConfig ( customid ) );
292+ QVERIFY ( !authm->configIds ().contains ( customid ) );
293+ QVERIFY ( authm->configIds ().isEmpty () );
294+ }
295+
296+ // verify cleanup of test configs
297+ QVERIFY ( authm->configIds ().isEmpty () );
298+ QVERIFY ( authm->availableAuthMethodConfigs ().isEmpty () );
299+
300+ // test bulk operations
301+ configs.clear ();
302+ configs = registerAuthConfigs ();
303+ QVERIFY ( !configs.isEmpty () );
304+
305+ // test storing, then retrieving configid -> config map
306+ QgsAuthMethodConfigsMap idcfgmap;
307+ Q_FOREACH ( QgsAuthMethodConfig config, configs )
308+ {
309+ QVERIFY ( authm->storeAuthenticationConfig ( config ) );
310+ idcfgmap.insert ( config.id (), config );
311+ }
312+ QgsAuthMethodConfigsMap authmap ( authm->availableAuthMethodConfigs () );
313+ QCOMPARE ( authmap.size (), 3 );
314+
315+ QgsAuthMethodConfigsMap::iterator it = authmap.begin ();
316+ for ( it = authmap.begin (); it != authmap.end (); ++it )
317+ {
318+ QString cfgid = it.key ();
319+ if ( !idcfgmap.contains ( cfgid ) )
320+ continue ;
321+
322+ QgsAuthMethodConfig cfg = it.value ();
323+ QgsAuthMethodConfig origcfg = idcfgmap.take ( cfgid );
324+ QCOMPARE ( origcfg.id (), cfg.id () );
325+ QCOMPARE ( origcfg.name (), cfg.name () );
326+ QCOMPARE ( origcfg.method (), cfg.method () );
327+ QCOMPARE ( origcfg.uri (), cfg.uri () );
328+ }
329+ QCOMPARE ( idcfgmap.size (), 0 );
330+
331+ QVERIFY ( authm->removeAllAuthenticationConfigs () );
332+ }
333+
334+ void TestQgsAuthManager::testAuthMethods ()
335+ {
336+ QList<QgsAuthMethodConfig> configs ( registerAuthConfigs () );
337+ QVERIFY ( !configs.isEmpty () );
338+
339+ QgsAuthManager *authm = QgsAuthManager::instance ();
340+
341+ Q_FOREACH ( QgsAuthMethodConfig config, configs )
342+ {
343+ QVERIFY ( config.isValid () );
344+ QVERIFY ( authm->storeAuthenticationConfig ( config ) );
345+ // config should now have a valid, unique ID
346+ // (see testAuthConfigs for further config testing)
347+ QString configid ( config.id () );
348+
349+ // correct method, loaded from core auth method plugin registry, should be returned
350+ QgsAuthMethod *authmethod = authm->configAuthMethod ( configid );
351+ QVERIFY ( authmethod );
352+ QCOMPARE ( authmethod->key (), config.method () );
353+
354+
355+ }
356+ QVERIFY ( authm->removeAllAuthenticationConfigs () );
357+ }
358+
359+ QList<QgsAuthMethodConfig> TestQgsAuthManager::registerAuthConfigs ()
360+ {
361+ QList<QgsAuthMethodConfig> configs;
362+
363+ // Basic
364+ QgsAuthMethodConfig b_config;
365+ b_config.setName ( " Basic" );
366+ b_config.setMethod ( " Basic" );
367+ b_config.setUri ( " http://example.com" );
368+ b_config.setConfig ( " username" , " username" );
369+ b_config.setConfig ( " password" , " password" );
370+ b_config.setConfig ( " realm" , " Realm" );
371+ if ( !b_config.isValid () )
372+ {
373+ return configs;
374+ }
375+
376+ // PKI-Paths
377+ QgsAuthMethodConfig p_config;
378+ p_config.setName ( " PKI-Paths" );
379+ p_config.setMethod ( " PKI-Paths" );
380+ p_config.setUri ( " http://example.com" );
381+ p_config.setConfig ( " certpath" , mPkiData + " /gerardus_cert.pem" );
382+ p_config.setConfig ( " keypath" , mPkiData + " gerardus_key_w-pass.pem" );
383+ if ( !p_config.isValid () )
384+ {
385+ return configs;
386+ }
387+
388+ // PKI-PKCS#12
389+ QgsAuthMethodConfig k_config;
390+ k_config.setName ( " PKI-PKCS#12" );
391+ k_config.setMethod ( " PKI-PKCS#12" );
392+ k_config.setUri ( " http://example.com" );
393+ k_config.setConfig ( " bundlepath" , mPkiData + " /gerardus.p12" );
394+ k_config.setConfig ( " bundlepass" , " password" );
395+ if ( !k_config.isValid () )
396+ {
397+ return configs;
398+ }
399+
400+ // do this last, so we are assured to have all core configs
401+ configs << b_config << p_config << k_config;
402+ return configs;
403+ }
404+
162405QTEST_MAIN ( TestQgsAuthManager )
163406#include " testqgsauthmanager.moc"
0 commit comments