|
| 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