Skip to content

Commit 14df327

Browse files
committed
[auth][ogr] Added test cases for all supported drivers
1 parent d163c01 commit 14df327

File tree

2 files changed

+140
-10
lines changed

2 files changed

+140
-10
lines changed

src/auth/basic/qgsauthbasicmethod.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
134134
// Inject credentials
135135
if ( uri.startsWith( QStringLiteral( "PG:" ) ) )
136136
{
137+
bool chopped = false;
138+
if ( uri.endsWith('"'))
139+
{
140+
uri.chop( 1 );
141+
chopped = true;
142+
}
137143
if ( !username.isEmpty() )
138144
{
139145
uri += QStringLiteral( " user='%1'" ).arg( username );
@@ -146,16 +152,26 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
146152
{
147153
uri += ' ' + caparam;
148154
}
155+
if ( chopped )
156+
uri += '"';
149157
}
150158
else if ( uri.startsWith( QStringLiteral( "SDE:" ) ) )
151159
{
152160
uri = uri.replace( QRegExp( ",$" ), QStringLiteral( ",%1,%2" ).arg( username, password ) );
153161
}
154162
else if ( uri.startsWith( QStringLiteral( "IDB" ) ) )
155163
{
164+
bool chopped = false;
165+
if ( uri.endsWith('"'))
166+
{
167+
uri.chop( 1 );
168+
chopped = true;
169+
}
156170
uri += QStringLiteral( " user=%1" ).arg( username );
157171
if ( !password.isEmpty() )
158172
uri += QStringLiteral( " pass=%1" ).arg( password );
173+
if ( chopped )
174+
uri += '"';
159175
}
160176
else if ( uri.startsWith( QStringLiteral( "@driver=ingres" ) ) )
161177
{
@@ -165,7 +181,7 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
165181
}
166182
else if ( uri.startsWith( QStringLiteral( "MySQL:" ) ) )
167183
{
168-
uri += QStringLiteral( ",userid=%1" ).arg( username );
184+
uri += QStringLiteral( ",user=%1" ).arg( username );
169185
if ( !password.isEmpty() )
170186
uri += QStringLiteral( ",password=%1" ).arg( password );
171187
}
@@ -186,19 +202,13 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
186202
{
187203
if ( password.isEmpty() )
188204
{
189-
uri = uri.replace( QRegExp( "^ODBC:[^@]+" ), "ODBC:" + username + '@' );
205+
uri = uri.replace( QRegExp( "^ODBC:@?" ), "ODBC:" + username + '@' );
190206
}
191207
else
192208
{
193-
uri = uri.replace( QRegExp( "^ODBC:[^@]+" ), "ODBC:" + username + '/' + password + '@' );
209+
uri = uri.replace( QRegExp( "^ODBC:@?" ), "ODBC:" + username + '/' + password + '@' );
194210
}
195211
}
196-
else if ( uri.startsWith( QStringLiteral( "MSSQL:" ) ) )
197-
{
198-
uri += QStringLiteral( ";uid=%1" ).arg( username );
199-
if ( !password.isEmpty() )
200-
uri += QStringLiteral( ";pwd=%1" ).arg( password );
201-
}
202212
else if ( uri.startsWith( QStringLiteral( "couchdb" ) )
203213
|| uri.startsWith( QStringLiteral( "DODS" ) )
204214
|| uri.startsWith( "http://" )
@@ -212,7 +222,7 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
212222
// Handle sub-layers
213223
if ( fullUri.contains( '|' ) )
214224
{
215-
uri += '|' + fullUri.right( fullUri.indexOf( '|' ) );
225+
uri += '|' + fullUri.right( fullUri.length() - fullUri.lastIndexOf( '|' ) - 1);
216226
}
217227
connectionItems.replace( 0, uri );
218228
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Tests for auth manager Basic Auth OGR connection credentials injection
4+
5+
6+
From build dir, run: ctest -R PyQgsAuthManagerOgrTest -V
7+
8+
or, if your PostgreSQL path differs from the default:
9+
10+
QGIS_POSTGRES_EXECUTABLE_PATH=/usr/lib/postgresql/<your_version_goes_here>/bin \
11+
ctest -R PyQgsAuthManagerOgrTest -V
12+
13+
.. note:: This program is free software; you can redistribute it and/or modify
14+
it under the terms of the GNU General Public License as published by
15+
the Free Software Foundation; either version 2 of the License, or
16+
(at your option) any later version.
17+
"""
18+
19+
20+
from qgis.core import (
21+
QgsApplication,
22+
QgsAuthManager,
23+
QgsAuthMethodConfig,
24+
QgsVectorLayer,
25+
QgsDataSourceUri,
26+
QgsWkbTypes,
27+
QgsProviderRegistry,
28+
)
29+
30+
from qgis.testing import (
31+
start_app,
32+
unittest,
33+
)
34+
35+
36+
__author__ = 'Alessandro Pasotti'
37+
__date__ = '14/11/2017'
38+
__copyright__ = 'Copyright 2017, The QGIS Project'
39+
# This will get replaced with a git SHA1 when you do a git archive
40+
__revision__ = '$Format:%H$'
41+
42+
43+
qgis_app = start_app()
44+
45+
# Note: value is checked with "in" because some drivers may need additional arguments,
46+
# like temporary paths with rootcerts for PG
47+
TEST_URIS = {
48+
"http://mysite.com/geojson authcfg='%s'": "http://username:password@mysite.com/geojson",
49+
"PG:\"dbname='databasename' host='addr' port='5432' authcfg='%s'\"": "PG:\"dbname='databasename' host='addr' port='5432' user='username' password='password'",
50+
'SDE:127.0.0.1,12345,dbname, authcfg=\'%s\'': 'SDE:127.0.0.1,12345,dbname,username,password',
51+
'IDB:"server=demo_on user=informix dbname=frames authcfg=\'%s\'"': 'IDB:"server=demo_on user=informix dbname=frames user=username pass=password"',
52+
'@driver=ingres,dbname=test,tables=usa/canada authcfg=\'%s\'': '@driver=ingres,dbname=test,tables=usa/canada,userid=username,password=password',
53+
'MySQL:westholland,port=3306,tables=bedrijven authcfg=\'%s\'': 'MySQL:westholland,port=3306,tables=bedrijven,user=username,password=password',
54+
'MSSQL:server=.\MSSQLSERVER2008;database=dbname;trusted_connection=yes authcfg=\'%s\'': 'MSSQL:server=.\MSSQLSERVER2008;database=dbname;uid=username;pwd=password',
55+
'OCI:/@database_instance:table,table authcfg=\'%s\'': 'OCI:username/password@database_instance:table,table',
56+
'ODBC:database_instance authcfg=\'%s\'': 'ODBC:username/password@database_instance',
57+
'couchdb://myconnection authcfg=\'%s\'': 'couchdb://username:password@myconnection',
58+
'http://www.myconnection.com/geojson authcfg=\'%s\'': 'http://username:password@www.myconnection.com/geojson',
59+
'https://www.myconnection.com/geojson authcfg=\'%s\'': 'https://username:password@www.myconnection.com/geojson',
60+
'ftp://www.myconnection.com/geojson authcfg=\'%s\'': 'ftp://username:password@www.myconnection.com/geojson',
61+
'DODS://www.myconnection.com/geojson authcfg=\'%s\'': 'DODS://username:password@www.myconnection.com/geojson',
62+
}
63+
64+
65+
class TestAuthManager(unittest.TestCase):
66+
67+
@classmethod
68+
def setUpAuth(cls):
69+
"""Run before all tests and set up authentication"""
70+
authm = QgsApplication.authManager()
71+
assert (authm.setMasterPassword('masterpassword', True))
72+
# Client side
73+
cls.auth_config = QgsAuthMethodConfig("Basic")
74+
cls.auth_config.setConfig('username', cls.username)
75+
cls.auth_config.setConfig('password', cls.password)
76+
cls.auth_config.setName('test_basic_auth_config')
77+
assert (authm.storeAuthenticationConfig(cls.auth_config)[0])
78+
assert cls.auth_config.isValid()
79+
cls.authcfg = cls.auth_config.id()
80+
81+
@classmethod
82+
def setUpClass(cls):
83+
"""Run before all tests:
84+
Creates an auth configuration"""
85+
cls.username = 'username'
86+
cls.password = 'password'
87+
cls.dbname = 'test_basic'
88+
cls.hostname = 'localhost'
89+
cls.setUpAuth()
90+
91+
@classmethod
92+
def tearDownClass(cls):
93+
"""Run after all tests"""
94+
pass
95+
96+
def setUp(self):
97+
"""Run before each test."""
98+
pass
99+
100+
def tearDown(self):
101+
"""Run after each test."""
102+
pass
103+
104+
def testConnections(self):
105+
"""
106+
Test credentials injection
107+
"""
108+
pr = QgsProviderRegistry.instance().createProvider('ogr', '')
109+
for uri, expanded in TEST_URIS.items():
110+
pr.setDataSourceUri(uri % self.authcfg)
111+
self.assertTrue(expanded in pr.dataSourceUri(True), "%s != %s" % (expanded, pr.dataSourceUri(True)))
112+
113+
# Test sublayers
114+
for uri, expanded in TEST_URIS.items():
115+
pr.setDataSourceUri((uri + '|sublayer1') % self.authcfg)
116+
self.assertEqual(pr.dataSourceUri(True).split('|')[1], "sublayer1", pr.dataSourceUri(True))
117+
118+
119+
if __name__ == '__main__':
120+
unittest.main()

0 commit comments

Comments
 (0)