-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
test_authsettingswidget.py
196 lines (166 loc) · 6.49 KB
/
test_authsettingswidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""
Tests for authentication widget
From build dir, run from test directory:
LC_ALL=en_US.UTF-8 ctest -R PyQgsAuthSettingsWidget -V
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
import os
import random
import string
import tempfile
from shutil import rmtree
from qgis.core import (
QgsApplication,
QgsAuthMethodConfig,
)
from qgis.gui import QgsAuthSettingsWidget
import unittest
from qgis.testing import start_app, QgisTestCase
__author__ = 'Alessandro Pasotti'
__date__ = '27/09/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
QGIS_AUTH_DB_DIR_PATH = tempfile.mkdtemp()
os.environ['QGIS_AUTH_DB_DIR_PATH'] = QGIS_AUTH_DB_DIR_PATH
qgis_app = start_app()
class TestAuthenticationWidget(QgisTestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests:
Creates an auth configuration"""
super().setUpClass()
# Enable auth
# os.environ['QGIS_AUTH_PASSWORD_FILE'] = QGIS_AUTH_PASSWORD_FILE
authm = QgsApplication.authManager()
assert (authm.setMasterPassword('masterpassword', True))
cls.auth_config = QgsAuthMethodConfig('Basic')
cls.auth_config.setName('test_auth_config')
cls.username = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6))
cls.password = cls.username[::-1] # reversed
cls.auth_config.setConfig('username', cls.username)
cls.auth_config.setConfig('password', cls.password)
assert (authm.storeAuthenticationConfig(cls.auth_config)[0])
@classmethod
def tearDownClass(cls):
"""Run after all tests"""
rmtree(QGIS_AUTH_DB_DIR_PATH)
super().tearDownClass()
def setUp(self):
"""Run before each test."""
pass
def tearDown(self):
"""Run after each test."""
pass
def testWidgetNoArgs(self):
"""
Test the widget with no args
"""
w = QgsAuthSettingsWidget()
self.assertEqual(w.username(), '')
self.assertEqual(w.password(), '')
self.assertEqual(w.configId(), '')
self.assertTrue(w.configurationTabIsSelected())
self.assertFalse(w.btnConvertToEncryptedIsEnabled())
def testWidgetConfigId(self):
"""
Test the widget with configId
"""
w = QgsAuthSettingsWidget(None, self.auth_config.id())
self.assertEqual(w.username(), '')
self.assertEqual(w.password(), '')
self.assertEqual(w.configId(), self.auth_config.id())
self.assertTrue(w.configurationTabIsSelected())
self.assertFalse(w.btnConvertToEncryptedIsEnabled())
def testWidgetUsername(self):
"""
Test the widget with username only
"""
w = QgsAuthSettingsWidget(None, None, 'username')
self.assertEqual(w.username(), 'username')
self.assertEqual(w.password(), '')
self.assertEqual(w.configId(), '')
self.assertFalse(w.configurationTabIsSelected())
def testWidgetPassword(self):
"""
Test the widget with password only
"""
w = QgsAuthSettingsWidget(None, None, None, 'password')
self.assertEqual(w.username(), '')
self.assertEqual(w.password(), 'password')
self.assertEqual(w.configId(), '')
self.assertFalse(w.configurationTabIsSelected())
def testWidgetUsernameAndPassword(self):
"""
Test the widget with username and password
"""
w = QgsAuthSettingsWidget(None, None, 'username', 'password')
self.assertEqual(w.username(), 'username')
self.assertEqual(w.password(), 'password')
self.assertEqual(w.configId(), '')
self.assertFalse(w.configurationTabIsSelected())
self.assertTrue(w.btnConvertToEncryptedIsEnabled())
def testConvertToEncrypted(self):
"""
Test the widget to encrypted conversion
"""
w = QgsAuthSettingsWidget(None, None, 'username', 'password')
self.assertEqual(w.username(), 'username')
self.assertEqual(w.password(), 'password')
self.assertEqual(w.configId(), '')
self.assertFalse(w.configurationTabIsSelected())
self.assertTrue(w.btnConvertToEncryptedIsEnabled())
self.assertTrue(w.convertToEncrypted())
self.assertNotEqual(w.configId(), '')
self.assertEqual(w.username(), '')
self.assertEqual(w.password(), '')
self.assertTrue(w.configurationTabIsSelected())
self.assertFalse(w.btnConvertToEncryptedIsEnabled())
def test_setters(self):
"""
Test setters
"""
w = QgsAuthSettingsWidget()
w.setUsername('username')
self.assertFalse(w.configurationTabIsSelected())
self.assertEqual(w.username(), 'username')
w = QgsAuthSettingsWidget()
w.setPassword('password')
self.assertEqual(w.password(), 'password')
self.assertFalse(w.configurationTabIsSelected())
w = QgsAuthSettingsWidget()
w.setConfigId(self.auth_config.id())
self.assertEqual(w.configId(), self.auth_config.id())
self.assertTrue(w.configurationTabIsSelected())
w = QgsAuthSettingsWidget()
w.setUsername('username')
w.setPassword('password')
w.setConfigId(self.auth_config.id())
self.assertEqual(w.configId(), self.auth_config.id())
self.assertTrue(w.configurationTabIsSelected())
w = QgsAuthSettingsWidget()
w.setDataprovider('db2')
self.assertEqual(w.dataprovider(), 'db2')
def test_storeCheckBoxes(self):
"""
Test store cb setters and getters
"""
w = QgsAuthSettingsWidget()
self.assertFalse(w.storePasswordIsChecked())
self.assertFalse(w.storeUsernameIsChecked())
w = QgsAuthSettingsWidget()
w.setStorePasswordChecked(True)
self.assertTrue(w.storePasswordIsChecked())
self.assertFalse(w.storeUsernameIsChecked())
w = QgsAuthSettingsWidget()
w.setStoreUsernameChecked(True)
self.assertFalse(w.storePasswordIsChecked())
self.assertTrue(w.storeUsernameIsChecked())
w = QgsAuthSettingsWidget()
w.setStoreUsernameChecked(True)
w.setStorePasswordChecked(True)
self.assertTrue(w.storePasswordIsChecked())
self.assertTrue(w.storeUsernameIsChecked())
if __name__ == '__main__':
unittest.main()