42
42
#include " qgsspinbox.h"
43
43
#include " qgsmapcanvas.h"
44
44
#include " qgsauthconfigselect.h"
45
+ #include " qgsauthmanager.h"
45
46
#include " models/qgsprocessingmodelalgorithm.h"
46
47
47
48
class TestParamType : public QgsProcessingParameterDefinition
@@ -153,12 +154,69 @@ class TestProcessingGui : public QObject
153
154
void testNumericWrapperInt ();
154
155
void testDistanceWrapper ();
155
156
void testRangeWrapper ();
157
+
158
+ private:
159
+
160
+ QString mTempDir ;
161
+ const char *mPass = " pass" ;
162
+
163
+ void cleanupTempDir ();
156
164
};
157
165
166
+
158
167
void TestProcessingGui::initTestCase ()
159
168
{
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
160
179
QgsApplication::init ();
161
180
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
+
162
220
QgsApplication::processingRegistry ()->addProvider ( new QgsNativeAlgorithms ( QgsApplication::processingRegistry () ) );
163
221
}
164
222
@@ -766,6 +824,43 @@ void TestProcessingGui::testStringWrapper()
766
824
767
825
void TestProcessingGui::testAuthCfgWrapper ()
768
826
{
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
+
769
864
QgsProcessingParameterAuthConfig param ( QStringLiteral ( " authcfg" ), QStringLiteral ( " authcfg" ) );
770
865
771
866
// standard wrapper
@@ -775,14 +870,16 @@ void TestProcessingGui::testAuthCfgWrapper()
775
870
QWidget *w = wrapper.createWrappedWidget ( context );
776
871
777
872
QSignalSpy spy ( &wrapper, &QgsProcessingAuthConfigWidgetWrapper::widgetValueHasChanged );
778
- wrapper.setWidgetValue ( QStringLiteral ( " xxx " ), context );
873
+ wrapper.setWidgetValue ( authIds. at ( 0 ), context );
779
874
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 );
785
878
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 );
786
883
QVERIFY ( wrapper.widgetValue ().toString ().isEmpty () );
787
884
QVERIFY ( static_cast < QgsAuthConfigSelect * >( wrapper.wrappedWidget () )->configId ().isEmpty () );
788
885
@@ -793,8 +890,8 @@ void TestProcessingGui::testAuthCfgWrapper()
793
890
delete l;
794
891
795
892
// 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 );
798
895
799
896
delete w;
800
897
@@ -803,17 +900,17 @@ void TestProcessingGui::testAuthCfgWrapper()
803
900
804
901
w = wrapperB.createWrappedWidget ( context );
805
902
QSignalSpy spy2 ( &wrapperB, &QgsProcessingAuthConfigWidgetWrapper::widgetValueHasChanged );
806
- wrapperB.setWidgetValue ( QStringLiteral ( " a " ), context );
903
+ wrapperB.setWidgetValue ( authIds. at ( 0 ), context );
807
904
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 ) );
810
907
wrapperB.setWidgetValue ( QString (), context );
811
908
QCOMPARE ( spy2.count (), 2 );
812
909
QVERIFY ( wrapperB.widgetValue ().toString ().isEmpty () );
813
910
QVERIFY ( static_cast < QgsAuthConfigSelect * >( wrapperB.wrappedWidget () )->configId ().isEmpty () );
814
911
815
912
// check signal
816
- static_cast < QgsAuthConfigSelect * >( w )->setConfigId ( QStringLiteral ( " x " ) );
913
+ static_cast < QgsAuthConfigSelect * >( w )->setConfigId ( authIds. at ( 0 ) );
817
914
QCOMPARE ( spy2.count (), 3 );
818
915
819
916
// should be no label in batch mode
@@ -825,17 +922,17 @@ void TestProcessingGui::testAuthCfgWrapper()
825
922
826
923
w = wrapperM.createWrappedWidget ( context );
827
924
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 ) );
830
927
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 ) );
832
929
wrapperM.setWidgetValue ( QString (), context );
833
930
QVERIFY ( wrapperM.widgetValue ().toString ().isEmpty () );
834
931
QCOMPARE ( spy3.count (), 2 );
835
932
QVERIFY ( static_cast < QgsAuthConfigSelect * >( wrapperM.wrappedWidget () )->configId ().isEmpty () );
836
933
837
934
// check signal
838
- static_cast < QgsAuthConfigSelect * >( w )->setConfigId ( QStringLiteral ( " x " ) );
935
+ static_cast < QgsAuthConfigSelect * >( w )->setConfigId ( authIds. at ( 0 ) );
839
936
QCOMPARE ( spy3.count (), 3 );
840
937
841
938
// should be a label in modeler mode
@@ -1551,5 +1648,18 @@ void TestProcessingGui::testRangeWrapper()
1551
1648
testWrapper ( QgsProcessingGui::Modeler );
1552
1649
}
1553
1650
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
+
1554
1664
QGSTEST_MAIN ( TestProcessingGui )
1555
1665
#include " testprocessinggui.moc"
0 commit comments