Skip to content

Commit 1285a01

Browse files
committedDec 14, 2018
Correctly test processing auth widget with real authentication setup
1 parent 56bd682 commit 1285a01

File tree

1 file changed

+126
-16
lines changed

1 file changed

+126
-16
lines changed
 

‎tests/src/gui/testprocessinggui.cpp

Lines changed: 126 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "qgsspinbox.h"
4343
#include "qgsmapcanvas.h"
4444
#include "qgsauthconfigselect.h"
45+
#include "qgsauthmanager.h"
4546
#include "models/qgsprocessingmodelalgorithm.h"
4647

4748
class TestParamType : public QgsProcessingParameterDefinition
@@ -153,12 +154,69 @@ class TestProcessingGui : public QObject
153154
void testNumericWrapperInt();
154155
void testDistanceWrapper();
155156
void testRangeWrapper();
157+
158+
private:
159+
160+
QString mTempDir;
161+
const char *mPass = "pass";
162+
163+
void cleanupTempDir();
156164
};
157165

166+
158167
void TestProcessingGui::initTestCase()
159168
{
169+
mTempDir = QDir::tempPath() + "/auth_proc";
170+
// setup a temporary auth db:
171+
cleanupTempDir();
172+
173+
// make QGIS_AUTH_DB_DIR_PATH temp dir for qgis - auth.db and master password file
174+
QDir tmpDir = QDir::temp();
175+
QVERIFY2( tmpDir.mkpath( mTempDir ), "Couldn't make temp directory" );
176+
qputenv( "QGIS_AUTH_DB_DIR_PATH", mTempDir.toAscii() );
177+
178+
// init app and auth manager
160179
QgsApplication::init();
161180
QgsApplication::initQgis();
181+
QVERIFY2( !QgsApplication::authManager()->isDisabled(),
182+
"Authentication system is DISABLED" );
183+
184+
// verify QGIS_AUTH_DB_DIR_PATH (temp auth db path) worked
185+
QString db1( QFileInfo( QgsApplication::authManager()->authenticationDatabasePath() ).canonicalFilePath() );
186+
QString db2( QFileInfo( mTempDir + "/qgis-auth.db" ).canonicalFilePath() );
187+
QVERIFY2( db1 == db2, "Auth db temp path does not match db path of manager" );
188+
189+
// verify master pass can be set manually
190+
// (this also creates a fresh password hash in the new temp database)
191+
QVERIFY2( QgsApplication::authManager()->setMasterPassword( mPass, true ),
192+
"Master password could not be set" );
193+
QVERIFY2( QgsApplication::authManager()->masterPasswordIsSet(),
194+
"Auth master password not set from passed string" );
195+
196+
// create QGIS_AUTH_PASSWORD_FILE file
197+
QString passfilepath = mTempDir + "/passfile";
198+
QFile passfile( passfilepath );
199+
if ( passfile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
200+
{
201+
QTextStream fout( &passfile );
202+
fout << QString( mPass ) << "\r\n";
203+
passfile.close();
204+
qputenv( "QGIS_AUTH_PASSWORD_FILE", passfilepath.toAscii() );
205+
}
206+
// qDebug( "QGIS_AUTH_PASSWORD_FILE=%s", qgetenv( "QGIS_AUTH_PASSWORD_FILE" ).constData() );
207+
208+
// re-init app and auth manager
209+
QgsApplication::quit();
210+
// QTest::qSleep( 3000 );
211+
QgsApplication::init();
212+
QgsApplication::initQgis();
213+
QVERIFY2( !QgsApplication::authManager()->isDisabled(),
214+
"Authentication system is DISABLED" );
215+
216+
// verify QGIS_AUTH_PASSWORD_FILE worked, when compared against hash in db
217+
QVERIFY2( QgsApplication::authManager()->masterPasswordIsSet(),
218+
"Auth master password not set from QGIS_AUTH_PASSWORD_FILE" );
219+
162220
QgsApplication::processingRegistry()->addProvider( new QgsNativeAlgorithms( QgsApplication::processingRegistry() ) );
163221
}
164222

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

767825
void TestProcessingGui::testAuthCfgWrapper()
768826
{
827+
QList<QgsAuthMethodConfig> configs;
828+
829+
// Basic
830+
QgsAuthMethodConfig b_config;
831+
b_config.setId( QStringLiteral( "aaaaaaa" ) );
832+
b_config.setName( QStringLiteral( "Basic" ) );
833+
b_config.setMethod( QStringLiteral( "Basic" ) );
834+
b_config.setUri( QStringLiteral( "http://example.com" ) );
835+
b_config.setConfig( QStringLiteral( "username" ), QStringLiteral( "username" ) );
836+
b_config.setConfig( QStringLiteral( "password" ), QStringLiteral( "password" ) );
837+
b_config.setConfig( QStringLiteral( "realm" ), QStringLiteral( "Realm" ) );
838+
configs << b_config;
839+
840+
QgsAuthMethodConfig b_config2;
841+
b_config2.setId( QStringLiteral( "bbbbbbb" ) );
842+
b_config2.setName( QStringLiteral( "Basic2" ) );
843+
b_config2.setMethod( QStringLiteral( "Basic" ) );
844+
b_config2.setUri( QStringLiteral( "http://example.com" ) );
845+
b_config2.setConfig( QStringLiteral( "username" ), QStringLiteral( "username" ) );
846+
b_config2.setConfig( QStringLiteral( "password" ), QStringLiteral( "password" ) );
847+
b_config2.setConfig( QStringLiteral( "realm" ), QStringLiteral( "Realm" ) );
848+
configs << b_config2;
849+
850+
QgsAuthManager *authm = QgsApplication::authManager();
851+
QStringList authIds;
852+
for ( QgsAuthMethodConfig config : qgis::as_const( configs ) )
853+
{
854+
QVERIFY( config.isValid() );
855+
856+
QVERIFY( authm->storeAuthenticationConfig( config ) );
857+
858+
// config should now have a valid, unique ID
859+
authIds << config.id();
860+
}
861+
862+
QCOMPARE( authIds.count(), 2 );
863+
769864
QgsProcessingParameterAuthConfig param( QStringLiteral( "authcfg" ), QStringLiteral( "authcfg" ) );
770865

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

777872
QSignalSpy spy( &wrapper, &QgsProcessingAuthConfigWidgetWrapper::widgetValueHasChanged );
778-
wrapper.setWidgetValue( QStringLiteral( "xxx" ), context );
873+
wrapper.setWidgetValue( authIds.at( 0 ), context );
779874
QCOMPARE( spy.count(), 1 );
780-
781-
// hard to test these - we don't have a standard test authcfg to set to
782-
// QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "xxx" ) );
783-
// QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->configId(), QStringLiteral( "xxx" ) );
784-
wrapper.setWidgetValue( QString(), context );
875+
QCOMPARE( wrapper.widgetValue().toString(), authIds.at( 0 ) );
876+
QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->configId(), authIds.at( 0 ) );
877+
wrapper.setWidgetValue( authIds.at( 1 ), context );
785878
QCOMPARE( spy.count(), 2 );
879+
QCOMPARE( wrapper.widgetValue().toString(), authIds.at( 1 ) );
880+
QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->configId(), authIds.at( 1 ) );
881+
wrapper.setWidgetValue( QString(), context );
882+
QCOMPARE( spy.count(), 3 );
786883
QVERIFY( wrapper.widgetValue().toString().isEmpty() );
787884
QVERIFY( static_cast< QgsAuthConfigSelect * >( wrapper.wrappedWidget() )->configId().isEmpty() );
788885

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

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

799896
delete w;
800897

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

804901
w = wrapperB.createWrappedWidget( context );
805902
QSignalSpy spy2( &wrapperB, &QgsProcessingAuthConfigWidgetWrapper::widgetValueHasChanged );
806-
wrapperB.setWidgetValue( QStringLiteral( "a" ), context );
903+
wrapperB.setWidgetValue( authIds.at( 0 ), context );
807904
QCOMPARE( spy2.count(), 1 );
808-
//QCOMPARE( wrapperB.widgetValue().toString(), QStringLiteral( "a" ) );
809-
//QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapperB.wrappedWidget() )->configId(), QStringLiteral( "a" ) );
905+
QCOMPARE( wrapperB.widgetValue().toString(), authIds.at( 0 ) );
906+
QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapperB.wrappedWidget() )->configId(), authIds.at( 0 ) );
810907
wrapperB.setWidgetValue( QString(), context );
811908
QCOMPARE( spy2.count(), 2 );
812909
QVERIFY( wrapperB.widgetValue().toString().isEmpty() );
813910
QVERIFY( static_cast< QgsAuthConfigSelect * >( wrapperB.wrappedWidget() )->configId().isEmpty() );
814911

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

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

826923
w = wrapperM.createWrappedWidget( context );
827924
QSignalSpy spy3( &wrapperM, &QgsProcessingAuthConfigWidgetWrapper::widgetValueHasChanged );
828-
wrapperM.setWidgetValue( QStringLiteral( "a" ), context );
829-
//QCOMPARE( wrapperM.widgetValue().toString(), QStringLiteral( "a" ) );
925+
wrapperM.setWidgetValue( authIds.at( 0 ), context );
926+
QCOMPARE( wrapperM.widgetValue().toString(), authIds.at( 0 ) );
830927
QCOMPARE( spy3.count(), 1 );
831-
//QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapperM.wrappedWidget() )->configId(), QStringLiteral( "a" ) );
928+
QCOMPARE( static_cast< QgsAuthConfigSelect * >( wrapperM.wrappedWidget() )->configId(), authIds.at( 0 ) );
832929
wrapperM.setWidgetValue( QString(), context );
833930
QVERIFY( wrapperM.widgetValue().toString().isEmpty() );
834931
QCOMPARE( spy3.count(), 2 );
835932
QVERIFY( static_cast< QgsAuthConfigSelect * >( wrapperM.wrappedWidget() )->configId().isEmpty() );
836933

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

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

1651+
void TestProcessingGui::cleanupTempDir()
1652+
{
1653+
QDir tmpDir = QDir( mTempDir );
1654+
if ( tmpDir.exists() )
1655+
{
1656+
Q_FOREACH ( const QString &tf, tmpDir.entryList( QDir::NoDotAndDotDot | QDir::Files ) )
1657+
{
1658+
QVERIFY2( tmpDir.remove( mTempDir + '/' + tf ), qPrintable( "Could not remove " + mTempDir + '/' + tf ) );
1659+
}
1660+
QVERIFY2( tmpDir.rmdir( mTempDir ), qPrintable( "Could not remove directory " + mTempDir ) );
1661+
}
1662+
}
1663+
15541664
QGSTEST_MAIN( TestProcessingGui )
15551665
#include "testprocessinggui.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.