Skip to content
Permalink
Browse files

[auth] Migrate more manager unit tests; add test html reporter

  • Loading branch information
dakcarto committed Oct 18, 2015
1 parent 4d44a96 commit b5261619b2ff9806ce85c82d515c4657cc16490e
Showing with 244 additions and 1 deletion.
  1. +244 −1 tests/src/core/testqgsauthmanager.cpp
@@ -17,6 +17,7 @@
#include <QtTest/QtTest>
#include <QtTest/QSignalSpy>
#include <QObject>
#include <QDesktopServices>
#include <QDir>
#include <QFile>
#include <QString>
@@ -40,15 +41,24 @@ class TestQgsAuthManager: public QObject
private slots:
void initTestCase();
void cleanupTestCase();
void init() {}
void init();
void cleanup() {}

void testMasterPassword();
void testAuthConfigs();
void testAuthMethods();

private:
void cleanupTempDir();
QList<QgsAuthMethodConfig> registerAuthConfigs();

void reportRow( const QString& msg );
void reportHeader( const QString& msg );

QString mPkiData;
QString mTempDir;
const char* mPass;
QString mReport;
};


@@ -61,6 +71,10 @@ TestQgsAuthManager::TestQgsAuthManager()

void TestQgsAuthManager::initTestCase()
{
cleanupTempDir();

mReport += "<h1>QgsAuthManager Tests</h1>\n";

// make QGIS_AUTH_DB_DIR_PATH temp dir for qgis-auth.db and master password file
QDir tmpDir = QDir::temp();
QVERIFY2( tmpDir.mkpath( mTempDir ), "Couldn't make temp directory" );
@@ -72,6 +86,10 @@ void TestQgsAuthManager::initTestCase()
QVERIFY2( !QgsAuthManager::instance()->isDisabled(),
"Authentication system is DISABLED" );

QString mySettings = QgsApplication::showSettings();
mySettings = mySettings.replace( "\n", "<br />\n" );
mReport += "<p>" + mySettings + "</p>\n";

// verify QGIS_AUTH_DB_DIR_PATH (temp auth db path) worked
QString db1( QFileInfo( QgsAuthManager::instance()->authenticationDbPath() ).canonicalFilePath() );
QString db2( QFileInfo( mTempDir + "/qgis-auth.db" ).canonicalFilePath() );
@@ -111,9 +129,48 @@ void TestQgsAuthManager::initTestCase()
// all tests should now have a valid qgis-auth.db and stored/set master password
}

void TestQgsAuthManager::cleanupTempDir()
{
QDir tmpDir = QDir( mTempDir );
if ( tmpDir.exists() )
{
Q_FOREACH ( const QString &tf, tmpDir.entryList( QDir::NoDotAndDotDot | QDir::Files ) )
{
QVERIFY( tmpDir.remove( mTempDir + "/" + tf ) );
}
QVERIFY( tmpDir.rmdir( mTempDir ) );
}
}

void TestQgsAuthManager::cleanupTestCase()
{
cleanupTempDir();
QgsApplication::exitQgis();

QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
// QDesktopServices::openUrl( "file:///" + myReportFile );
}
}

void TestQgsAuthManager::init()
{
mReport += "<h2>" + QString( QTest::currentTestFunction() ) + "</h2>\n";
}

void TestQgsAuthManager::reportRow( const QString& msg )
{
mReport += msg + "<br>\n";
}

void TestQgsAuthManager::reportHeader( const QString& msg )
{
mReport += "<h3>" + msg + "</h3>\n";
}

void TestQgsAuthManager::testMasterPassword()
@@ -159,5 +216,191 @@ void TestQgsAuthManager::testMasterPassword()
QVERIFY( spyargs.at( 0 ).toBool() == true );
}

void TestQgsAuthManager::testAuthConfigs()
{
QList<QgsAuthMethodConfig> configs( registerAuthConfigs() );
QVERIFY( !configs.isEmpty() );

QgsAuthManager *authm = QgsAuthManager::instance();

// test storing/loading/updating
Q_FOREACH ( QgsAuthMethodConfig config, configs )
{
QVERIFY( config.isValid() );

QVERIFY( authm->storeAuthenticationConfig( config ) );

// config should now have a valid, unique ID
QString configid( config.id() );
QVERIFY( !configid.isEmpty() );
QVERIFY( !authm->configIdUnique( configid ) ); // uniqueness registered, so can't be overridden
QVERIFY( authm->configIds().contains( configid ) );

// config -> method map should have been updated and return the right method key
QCOMPARE( authm->configAuthMethodKey( configid ), config.method() );

// loading into new base config should return same as original stored
QgsAuthMethodConfig config2;
QVERIFY( authm->loadAuthenticationConfig( configid, config2, false ) );
QVERIFY( config2.isValid() );
QVERIFY( config2.configMap().isEmpty() ); // has no sensitive data

QVERIFY( authm->loadAuthenticationConfig( configid, config2, true ) );
QVERIFY( config2.isValid() );
QVERIFY( !config2.configMap().isEmpty() ); // has sensitive data

QVERIFY( config == config2 );

// values haven't been changed, but db update should take place and values should roundtrip
QVERIFY( authm->updateAuthenticationConfig( config2 ) );
QVERIFY( authm->loadAuthenticationConfig( configid, config2, true ) );
QVERIFY( config2.isValid() );
QVERIFY( !config2.configMap().isEmpty() ); // has sensitive data
QVERIFY( config == config2 );

// changed config should update then correctly roundtrip
Q_FOREACH ( const QString &key, config2.configMap().keys() )
{
config2.setConfig( key, config2.configMap().value( key ) + "changed" );
}
config2.setName( config2.name() + "changed" );
config2.setUri( config2.uri() + "changed" );
QVERIFY( authm->updateAuthenticationConfig( config2 ) );

QgsAuthMethodConfig config3;
QVERIFY( authm->loadAuthenticationConfig( configid, config3, true ) );
QVERIFY( config3.isValid() );
QVERIFY( !config3.configMap().isEmpty() );
QVERIFY( config != config3 );
QVERIFY( config2 == config3 );

// config can be deleted
QVERIFY( authm->removeAuthenticationConfig( configid ) );
QVERIFY( !authm->configIds().contains( configid ) );
QVERIFY( authm->configIds().isEmpty() );

// config with custom id can be stored
QString customid( authm->uniqueConfigId() );
config2.setId( customid );

QVERIFY( authm->storeAuthenticationConfig( config2 ) );
QCOMPARE( config2.id(), customid );
QVERIFY( config2 != config3 ); // id should be different

// custom configid can be deleted
QVERIFY( authm->removeAuthenticationConfig( customid ) );
QVERIFY( !authm->configIds().contains( customid ) );
QVERIFY( authm->configIds().isEmpty() );
}

// verify cleanup of test configs
QVERIFY( authm->configIds().isEmpty() );
QVERIFY( authm->availableAuthMethodConfigs().isEmpty() );

// test bulk operations
configs.clear();
configs = registerAuthConfigs();
QVERIFY( !configs.isEmpty() );

// test storing, then retrieving configid -> config map
QgsAuthMethodConfigsMap idcfgmap;
Q_FOREACH ( QgsAuthMethodConfig config, configs )
{
QVERIFY( authm->storeAuthenticationConfig( config ) );
idcfgmap.insert( config.id(), config );
}
QgsAuthMethodConfigsMap authmap( authm->availableAuthMethodConfigs() );
QCOMPARE( authmap.size(), 3 );

QgsAuthMethodConfigsMap::iterator it = authmap.begin();
for ( it = authmap.begin(); it != authmap.end(); ++it )
{
QString cfgid = it.key();
if ( !idcfgmap.contains( cfgid ) )
continue;

QgsAuthMethodConfig cfg = it.value();
QgsAuthMethodConfig origcfg = idcfgmap.take( cfgid );
QCOMPARE( origcfg.id(), cfg.id() );
QCOMPARE( origcfg.name(), cfg.name() );
QCOMPARE( origcfg.method(), cfg.method() );
QCOMPARE( origcfg.uri(), cfg.uri() );
}
QCOMPARE( idcfgmap.size(), 0 );

QVERIFY( authm->removeAllAuthenticationConfigs() );
}

void TestQgsAuthManager::testAuthMethods()
{
QList<QgsAuthMethodConfig> configs( registerAuthConfigs() );
QVERIFY( !configs.isEmpty() );

QgsAuthManager *authm = QgsAuthManager::instance();

Q_FOREACH ( QgsAuthMethodConfig config, configs )
{
QVERIFY( config.isValid() );
QVERIFY( authm->storeAuthenticationConfig( config ) );
// config should now have a valid, unique ID
// (see testAuthConfigs for further config testing)
QString configid( config.id() );

// correct method, loaded from core auth method plugin registry, should be returned
QgsAuthMethod *authmethod = authm->configAuthMethod( configid );
QVERIFY( authmethod );
QCOMPARE( authmethod->key(), config.method() );


}
QVERIFY( authm->removeAllAuthenticationConfigs() );
}

QList<QgsAuthMethodConfig> TestQgsAuthManager::registerAuthConfigs()
{
QList<QgsAuthMethodConfig> configs;

// Basic
QgsAuthMethodConfig b_config;
b_config.setName( "Basic" );
b_config.setMethod( "Basic" );
b_config.setUri( "http://example.com" );
b_config.setConfig( "username", "username" );
b_config.setConfig( "password", "password" );
b_config.setConfig( "realm", "Realm" );
if ( !b_config.isValid() )
{
return configs;
}

// PKI-Paths
QgsAuthMethodConfig p_config;
p_config.setName( "PKI-Paths" );
p_config.setMethod( "PKI-Paths" );
p_config.setUri( "http://example.com" );
p_config.setConfig( "certpath", mPkiData + "/gerardus_cert.pem" );
p_config.setConfig( "keypath", mPkiData + "gerardus_key_w-pass.pem" );
if ( !p_config.isValid() )
{
return configs;
}

// PKI-PKCS#12
QgsAuthMethodConfig k_config;
k_config.setName( "PKI-PKCS#12" );
k_config.setMethod( "PKI-PKCS#12" );
k_config.setUri( "http://example.com" );
k_config.setConfig( "bundlepath", mPkiData + "/gerardus.p12" );
k_config.setConfig( "bundlepass", "password" );
if ( !k_config.isValid() )
{
return configs;
}

// do this last, so we are assured to have all core configs
configs << b_config << p_config << k_config;
return configs;
}

QTEST_MAIN( TestQgsAuthManager )
#include "testqgsauthmanager.moc"

0 comments on commit b526161

Please sign in to comment.
You can’t perform that action at this time.