Skip to content
Permalink
Browse files

Correctly test processing auth widget with real authentication setup

  • Loading branch information
nyalldawson committed Dec 14, 2018
1 parent 56bd682 commit 1285a01c64b2e3ed3f78c054dcdf3ea36efddb5c
Showing with 126 additions and 16 deletions.
  1. +126 −16 tests/src/gui/testprocessinggui.cpp
@@ -42,6 +42,7 @@
#include "qgsspinbox.h"
#include "qgsmapcanvas.h"
#include "qgsauthconfigselect.h"
#include "qgsauthmanager.h"
#include "models/qgsprocessingmodelalgorithm.h"

class TestParamType : public QgsProcessingParameterDefinition
@@ -153,12 +154,69 @@ class TestProcessingGui : public QObject
void testNumericWrapperInt();
void testDistanceWrapper();
void testRangeWrapper();

private:

QString mTempDir;
const char *mPass = "pass";

void cleanupTempDir();
};


void TestProcessingGui::initTestCase()
{
mTempDir = QDir::tempPath() + "/auth_proc";
// setup a temporary auth db:
cleanupTempDir();

// 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" );
qputenv( "QGIS_AUTH_DB_DIR_PATH", mTempDir.toAscii() );

// init app and auth manager
QgsApplication::init();
QgsApplication::initQgis();
QVERIFY2( !QgsApplication::authManager()->isDisabled(),
"Authentication system is DISABLED" );

// verify QGIS_AUTH_DB_DIR_PATH (temp auth db path) worked
QString db1( QFileInfo( QgsApplication::authManager()->authenticationDatabasePath() ).canonicalFilePath() );
QString db2( QFileInfo( mTempDir + "/qgis-auth.db" ).canonicalFilePath() );
QVERIFY2( db1 == db2, "Auth db temp path does not match db path of manager" );

// verify master pass can be set manually
// (this also creates a fresh password hash in the new temp database)
QVERIFY2( QgsApplication::authManager()->setMasterPassword( mPass, true ),
"Master password could not be set" );
QVERIFY2( QgsApplication::authManager()->masterPasswordIsSet(),
"Auth master password not set from passed string" );

// create QGIS_AUTH_PASSWORD_FILE file
QString passfilepath = mTempDir + "/passfile";
QFile passfile( passfilepath );
if ( passfile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
QTextStream fout( &passfile );
fout << QString( mPass ) << "\r\n";
passfile.close();
qputenv( "QGIS_AUTH_PASSWORD_FILE", passfilepath.toAscii() );
}
// qDebug( "QGIS_AUTH_PASSWORD_FILE=%s", qgetenv( "QGIS_AUTH_PASSWORD_FILE" ).constData() );

// re-init app and auth manager
QgsApplication::quit();
// QTest::qSleep( 3000 );
QgsApplication::init();
QgsApplication::initQgis();
QVERIFY2( !QgsApplication::authManager()->isDisabled(),
"Authentication system is DISABLED" );

// verify QGIS_AUTH_PASSWORD_FILE worked, when compared against hash in db
QVERIFY2( QgsApplication::authManager()->masterPasswordIsSet(),
"Auth master password not set from QGIS_AUTH_PASSWORD_FILE" );

QgsApplication::processingRegistry()->addProvider( new QgsNativeAlgorithms( QgsApplication::processingRegistry() ) );
}

@@ -766,6 +824,43 @@ void TestProcessingGui::testStringWrapper()

void TestProcessingGui::testAuthCfgWrapper()
{
QList<QgsAuthMethodConfig> configs;

// Basic
QgsAuthMethodConfig b_config;
b_config.setId( QStringLiteral( "aaaaaaa" ) );
b_config.setName( QStringLiteral( "Basic" ) );
b_config.setMethod( QStringLiteral( "Basic" ) );
b_config.setUri( QStringLiteral( "http://example.com" ) );
b_config.setConfig( QStringLiteral( "username" ), QStringLiteral( "username" ) );
b_config.setConfig( QStringLiteral( "password" ), QStringLiteral( "password" ) );
b_config.setConfig( QStringLiteral( "realm" ), QStringLiteral( "Realm" ) );
configs << b_config;

QgsAuthMethodConfig b_config2;
b_config2.setId( QStringLiteral( "bbbbbbb" ) );
b_config2.setName( QStringLiteral( "Basic2" ) );
b_config2.setMethod( QStringLiteral( "Basic" ) );
b_config2.setUri( QStringLiteral( "http://example.com" ) );
b_config2.setConfig( QStringLiteral( "username" ), QStringLiteral( "username" ) );
b_config2.setConfig( QStringLiteral( "password" ), QStringLiteral( "password" ) );
b_config2.setConfig( QStringLiteral( "realm" ), QStringLiteral( "Realm" ) );
configs << b_config2;

QgsAuthManager *authm = QgsApplication::authManager();
QStringList authIds;
for ( QgsAuthMethodConfig config : qgis::as_const( configs ) )
{
QVERIFY( config.isValid() );

QVERIFY( authm->storeAuthenticationConfig( config ) );

// config should now have a valid, unique ID
authIds << config.id();
}

QCOMPARE( authIds.count(), 2 );

QgsProcessingParameterAuthConfig param( QStringLiteral( "authcfg" ), QStringLiteral( "authcfg" ) );

// standard wrapper
@@ -775,14 +870,16 @@ void TestProcessingGui::testAuthCfgWrapper()
QWidget *w = wrapper.createWrappedWidget( context );

QSignalSpy spy( &wrapper, &QgsProcessingAuthConfigWidgetWrapper::widgetValueHasChanged );
wrapper.setWidgetValue( QStringLiteral( "xxx" ), context );
wrapper.setWidgetValue( authIds.at( 0 ), context );
QCOMPARE( spy.count(), 1 );

// hard to test these - we don't have a standard test authcfg to set to
// QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "xxx" ) );
// QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->configId(), QStringLiteral( "xxx" ) );
wrapper.setWidgetValue( QString(), context );
QCOMPARE( wrapper.widgetValue().toString(), authIds.at( 0 ) );
QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->configId(), authIds.at( 0 ) );
wrapper.setWidgetValue( authIds.at( 1 ), context );
QCOMPARE( spy.count(), 2 );
QCOMPARE( wrapper.widgetValue().toString(), authIds.at( 1 ) );
QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->configId(), authIds.at( 1 ) );
wrapper.setWidgetValue( QString(), context );
QCOMPARE( spy.count(), 3 );
QVERIFY( wrapper.widgetValue().toString().isEmpty() );
QVERIFY( static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->configId().isEmpty() );

@@ -793,8 +890,8 @@ void TestProcessingGui::testAuthCfgWrapper()
delete l;

// check signal
static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->setConfigId( QStringLiteral( "b" ) );
QCOMPARE( spy.count(), 3 );
static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->setConfigId( authIds.at( 0 ) );
QCOMPARE( spy.count(), 4 );

delete w;

@@ -803,17 +900,17 @@ void TestProcessingGui::testAuthCfgWrapper()

w = wrapperB.createWrappedWidget( context );
QSignalSpy spy2( &wrapperB, &QgsProcessingAuthConfigWidgetWrapper::widgetValueHasChanged );
wrapperB.setWidgetValue( QStringLiteral( "a" ), context );
wrapperB.setWidgetValue( authIds.at( 0 ), context );
QCOMPARE( spy2.count(), 1 );
//QCOMPARE( wrapperB.widgetValue().toString(), QStringLiteral( "a" ) );
//QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapperB.wrappedWidget() )->configId(), QStringLiteral( "a" ) );
QCOMPARE( wrapperB.widgetValue().toString(), authIds.at( 0 ) );
QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapperB.wrappedWidget() )->configId(), authIds.at( 0 ) );
wrapperB.setWidgetValue( QString(), context );
QCOMPARE( spy2.count(), 2 );
QVERIFY( wrapperB.widgetValue().toString().isEmpty() );
QVERIFY( static_cast< QgsAuthConfigSelect * >( wrapperB.wrappedWidget() )->configId().isEmpty() );

// check signal
static_cast< QgsAuthConfigSelect * >( w )->setConfigId( QStringLiteral( "x" ) );
static_cast< QgsAuthConfigSelect * >( w )->setConfigId( authIds.at( 0 ) );
QCOMPARE( spy2.count(), 3 );

// should be no label in batch mode
@@ -825,17 +922,17 @@ void TestProcessingGui::testAuthCfgWrapper()

w = wrapperM.createWrappedWidget( context );
QSignalSpy spy3( &wrapperM, &QgsProcessingAuthConfigWidgetWrapper::widgetValueHasChanged );
wrapperM.setWidgetValue( QStringLiteral( "a" ), context );
//QCOMPARE( wrapperM.widgetValue().toString(), QStringLiteral( "a" ) );
wrapperM.setWidgetValue( authIds.at( 0 ), context );
QCOMPARE( wrapperM.widgetValue().toString(), authIds.at( 0 ) );
QCOMPARE( spy3.count(), 1 );
//QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapperM.wrappedWidget() )->configId(), QStringLiteral( "a" ) );
QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapperM.wrappedWidget() )->configId(), authIds.at( 0 ) );
wrapperM.setWidgetValue( QString(), context );
QVERIFY( wrapperM.widgetValue().toString().isEmpty() );
QCOMPARE( spy3.count(), 2 );
QVERIFY( static_cast< QgsAuthConfigSelect * >( wrapperM.wrappedWidget() )->configId().isEmpty() );

// check signal
static_cast< QgsAuthConfigSelect * >( w )->setConfigId( QStringLiteral( "x" ) );
static_cast< QgsAuthConfigSelect * >( w )->setConfigId( authIds.at( 0 ) );
QCOMPARE( spy3.count(), 3 );

// should be a label in modeler mode
@@ -1551,5 +1648,18 @@ void TestProcessingGui::testRangeWrapper()
testWrapper( QgsProcessingGui::Modeler );
}

void TestProcessingGui::cleanupTempDir()
{
QDir tmpDir = QDir( mTempDir );
if ( tmpDir.exists() )
{
Q_FOREACH ( const QString &tf, tmpDir.entryList( QDir::NoDotAndDotDot | QDir::Files ) )
{
QVERIFY2( tmpDir.remove( mTempDir + '/' + tf ), qPrintable( "Could not remove " + mTempDir + '/' + tf ) );
}
QVERIFY2( tmpDir.rmdir( mTempDir ), qPrintable( "Could not remove directory " + mTempDir ) );
}
}

QGSTEST_MAIN( TestProcessingGui )
#include "testprocessinggui.moc"

0 comments on commit 1285a01

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