|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Tests for auth manager Basic configuration update proxy |
| 4 | +
|
| 5 | +From build dir, run from test directory: |
| 6 | +LC_ALL=en_US.UTF-8 ctest -R PyQgsAuthManagerProxy -V |
| 7 | +
|
| 8 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation; either version 2 of the License, or |
| 11 | +(at your option) any later version. |
| 12 | +""" |
| 13 | +import os |
| 14 | +import re |
| 15 | +import string |
| 16 | +import sys |
| 17 | +from functools import partial |
| 18 | +from shutil import rmtree |
| 19 | +import tempfile |
| 20 | +import random |
| 21 | + |
| 22 | +from qgis.core import QgsAuthManager, QgsAuthMethodConfig, QgsNetworkAccessManager, QgsSettings |
| 23 | +from qgis.testing import start_app, unittest |
| 24 | + |
| 25 | +from utilities import unitTestDataPath, waitServer |
| 26 | + |
| 27 | +__author__ = 'Alessandro Pasotti' |
| 28 | +__date__ = '27/09/2017' |
| 29 | +__copyright__ = 'Copyright 2017, The QGIS Project' |
| 30 | +# This will get replaced with a git SHA1 when you do a git archive |
| 31 | +__revision__ = '$Format:%H$' |
| 32 | + |
| 33 | + |
| 34 | +QGIS_AUTH_DB_DIR_PATH = tempfile.mkdtemp() |
| 35 | + |
| 36 | +os.environ['QGIS_AUTH_DB_DIR_PATH'] = QGIS_AUTH_DB_DIR_PATH |
| 37 | + |
| 38 | +qgis_app = start_app() |
| 39 | + |
| 40 | + |
| 41 | +class TestAuthManager(unittest.TestCase): |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def setUpClass(cls): |
| 45 | + """Run before all tests: |
| 46 | + Creates an auth configuration""" |
| 47 | + cls.testdata_path = unitTestDataPath('qgis_server') + '/' |
| 48 | + # Enable auth |
| 49 | + # os.environ['QGIS_AUTH_PASSWORD_FILE'] = QGIS_AUTH_PASSWORD_FILE |
| 50 | + authm = QgsAuthManager.instance() |
| 51 | + assert (authm.setMasterPassword('masterpassword', True)) |
| 52 | + cls.auth_config = QgsAuthMethodConfig('Basic') |
| 53 | + cls.auth_config.setName('test_auth_config') |
| 54 | + cls.username = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6)) |
| 55 | + cls.password = cls.username[::-1] # reversed |
| 56 | + cls.auth_config.setConfig('username', cls.username) |
| 57 | + cls.auth_config.setConfig('password', cls.password) |
| 58 | + assert (authm.storeAuthenticationConfig(cls.auth_config)[0]) |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def tearDownClass(cls): |
| 62 | + """Run after all tests""" |
| 63 | + rmtree(QGIS_AUTH_DB_DIR_PATH) |
| 64 | + |
| 65 | + def setUp(self): |
| 66 | + """Run before each test.""" |
| 67 | + pass |
| 68 | + |
| 69 | + def tearDown(self): |
| 70 | + """Run after each test.""" |
| 71 | + pass |
| 72 | + |
| 73 | + def testProxyIsUpdated(self): |
| 74 | + """ |
| 75 | + Test that proxy is updated |
| 76 | + """ |
| 77 | + authm = QgsAuthManager.instance() |
| 78 | + nam = QgsNetworkAccessManager.instance() |
| 79 | + proxy = nam.proxy() |
| 80 | + self.assertEqual(proxy.password(), '') |
| 81 | + self.assertEqual(proxy.user(), '') |
| 82 | + self.assertTrue(authm.updateNetworkProxy(proxy, self.auth_config.id())) |
| 83 | + self.assertEqual(proxy.user(), self.username) |
| 84 | + self.assertEqual(proxy.password(), self.password) |
| 85 | + |
| 86 | + def testProxyIsUpdatedByUserSettings(self): |
| 87 | + """ |
| 88 | + Test that proxy is updated |
| 89 | + """ |
| 90 | + nam = QgsNetworkAccessManager.instance() |
| 91 | + nam.setupDefaultProxyAndCache() |
| 92 | + proxy = nam.proxy() |
| 93 | + self.assertEqual(proxy.password(), '') |
| 94 | + self.assertEqual(proxy.user(), '') |
| 95 | + settings = QgsSettings() |
| 96 | + settings.setValue("proxy/authcfg", self.auth_config.id()) |
| 97 | + settings.setValue("proxy/proxyEnabled", True) |
| 98 | + del(settings) |
| 99 | + nam.setupDefaultProxyAndCache() |
| 100 | + proxy = nam.fallbackProxy() |
| 101 | + self.assertEqual(proxy.password(), self.password) |
| 102 | + self.assertEqual(proxy.user(), self.username) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + unittest.main() |
0 commit comments