Newer
Older
"""Test AuthenticationBoardModule."""
import unittest
import string
from PySide2 import QtGui
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import Qt, QTimer
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
from PySide2.QtTest import QTest
from soitool.modules.module_authentication_board import (
AuthenticationBoardModule,
START_NO_OF_CODES as START_NO_OF_CODES_AUTHENTICATION,
HEADLINE_TEXT as HEADLINE_TEXT_AUTHENTICATION,
ROW_IDENTIFIERS as ROW_IDENTIFIERS_AUTHENTICATION,
CODE_LENGTH as CODE_LENGTH_AUTHENTICATION,
)
from soitool.modules.module_subtractorcodes import (
SubtractorcodesModule,
START_NO_OF_CODES as START_NO_OF_CODES_SUBTRACTOR,
HEADLINE_TEXT as HEADLINE_TEXT_SUBTRACTOR,
ROW_IDENTIFIERS as ROW_IDENTIFIERS_SUBTRACTOR,
CODE_LENGTH as CODE_LENGTH_SUBTRACTOR,
)
from soitool.soi import SOI
if isinstance(QtGui.qApp, type(None)):
app = QApplication([])
else:
app = QtGui.qApp
class TestDefaultAuthenticationBoardAndSubtractorcodesModule(
unittest.TestCase
):
"""TestCase for AuthenticationBoardModule and SubtractorcodesModule.
Both modules inherit from CodeTableBase. Therefore, only
AuthenticationBoardModule is used to test mutual functions.
Module-specific functionality-tests are, of course, executed.
"""
def setUp(self):
"""Create new AuthenticationBoardModule."""
def press_enter_on_active_dialog():
active_widget = app.activeModalWidget()
QTest.keyClick(active_widget, Qt.Key_Enter)
QTimer.singleShot(0, press_enter_on_active_dialog)
self.authentication = AuthenticationBoardModule()
QTimer.singleShot(0, press_enter_on_active_dialog)
self.subtractor = SubtractorcodesModule()
def test_default_module(self):
"""Test that module is initialized properly."""
# Assert correct headline

Anders H. Rebner
committed
self.assertTrue(
HEADLINE_TEXT_AUTHENTICATION
in self.authentication.item(0, 0).text()
)
self.assertEqual(
self.subtractor.item(0, 0).text(), HEADLINE_TEXT_SUBTRACTOR
)
# Assert correct number of rows including header
self.assertEqual(
self.authentication.rowCount(),
START_NO_OF_CODES_AUTHENTICATION + 1,
)
self.assertEqual(
self.subtractor.rowCount(), START_NO_OF_CODES_SUBTRACTOR + 1,
)
# Assert correct number of columns
self.assertEqual(self.authentication.columnCount(), 3)
self.assertEqual(self.subtractor.columnCount(), 3)
# Assert cell content in first and second column is correct
# From 1 to skip headline-row
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
self.assertEqual(
self.authentication.item(i, 0).text(),
ROW_IDENTIFIERS_AUTHENTICATION[i - 1],
)
self.assertEqual(
self.authentication.item(i, 1).text(), str(i - 1),
)
self.assertEqual(
self.subtractor.item(i, 0).text(),
ROW_IDENTIFIERS_SUBTRACTOR[i - 1],
)
# Assert cell content in third column is correct
code = self.authentication.item(1, 2).text()
self.assertTrue(len(code) >= CODE_LENGTH_AUTHENTICATION)
# Assert all code characters are taken from list code_characters
for _, character in enumerate(code):
if character != " ":
self.assertTrue(
character in self.authentication.code_characters
)
code = self.subtractor.item(1, 2).text()
self.assertTrue(len(code) >= CODE_LENGTH_SUBTRACTOR)
# Assert all code characters are digits
for _, character in enumerate(code):
if character != " ":
self.assertTrue(character in string.digits)
# Assert all codes are unique
code_list = self.authentication.get_codes()
code_set = set(code_list)
self.assertEqual(len(code_list), len(code_set))
def test_generate_unique_authentication_code(self):
"""Test function generate_unique_authentication_module."""
code_list = self.authentication.get_codes()
generated_code = self.authentication.generate_unique_code()
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
# Assert code length is equal to an existing code
self.assertEqual(len(generated_code), len(code_list[0]))
# Assert generated code is not equal to any existing codes
self.assertFalse(generated_code in code_list)
def test_get_codes(self):
"""Test function get_codes."""
# Get codes
code_list = self.authentication.get_codes()
# Assert codes are correct
for i, code in enumerate(code_list):
self.assertEqual(code, self.authentication.item(i + 1, 2).text())
def test_gui_add_row(self):
"""Test adding rows with shortcuts."""
# Widget must be shown for shortcuts to work
self.authentication.show()
QTest.qWaitForWindowExposed(self.authentication)
# From experience it can happen that the widget is not fully
# initialized before the QTest.keyClicks below. By calling
# processEvents here we give Qt the opportunity to catch up with us
app.processEvents()
old_row_count = self.authentication.rowCount()
# Use shortcut 'Ctrl + +'
QTest.keyClicks(self.authentication, "+", Qt.ControlModifier)
# Assert a new row is added
new_row_count = self.authentication.rowCount()
self.assertEqual(old_row_count + 1, new_row_count)
# Assert new row has correct content
row_index = new_row_count - 1
self.assertEqual(
self.authentication.item(row_index, 0).text(),
ROW_IDENTIFIERS_AUTHENTICATION[row_index - 1],
)
self.assertEqual(self.authentication.item(row_index, 1).text(), "")
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
197
198
199
200
201
202
new_code = self.authentication.item(row_index, 2).text()
existing_code = self.authentication.item(1, 2).text()
self.assertEqual(len(new_code), len(existing_code))
def test_gui_remove_row(self):
"""Test removing rows with shortcuts."""
# Widget must be shown for shortcuts to work
self.authentication.show()
QTest.qWaitForWindowExposed(self.authentication)
# From experience it can happen that the widget is not fully
# initialized before the QTest.keyClicks below. By calling
# processEvents here we give Qt the opportunity to catch up with us
app.processEvents()
old_row_count = self.authentication.rowCount()
# User should not be able to delete headline-row with shortcut
# Locate headline-item, move mouse and and click (select) item
item_center = self.authentication.visualItemRect(
self.authentication.item(0, 0)
).center
viewport = self.authentication.viewport()
QTest.mouseMove(viewport, item_center())
QTest.mouseClick(viewport, Qt.LeftButton, Qt.NoModifier, item_center())
# Try to delete row with shortcut
QTest.keyClicks(self.authentication, "_", Qt.ControlModifier)
# Assert row was not removed
new_row_count = self.authentication.rowCount()
self.assertEqual(old_row_count, new_row_count)
# Move to first row (below headline), then use shortcut to delete it
QTest.keyClick(self.authentication, Qt.Key_Down)
value_to_delete = self.authentication.item(1, 1).text()
QTest.keyClicks(self.authentication, "_", Qt.ControlModifier)
# Assert row was removed and replaced by the row below
new_row_count = self.authentication.rowCount()
self.assertEqual(old_row_count - 1, new_row_count)
self.assertEqual(
self.authentication.item(1, 1).text(), value_to_delete
)
# Remove rows until only headline-row and two code-rows exist
for _ in range(1, self.authentication.rowCount()):
QTest.keyClick(self.authentication, Qt.Key_Down)
QTest.keyClicks(self.authentication, "_", Qt.ControlModifier)
self.assertTrue(self.authentication.rowCount() == 3)
# Try to remove final code-row, should not work
QTest.keyClick(self.authentication, Qt.Key_Down)
QTest.keyClicks(self.authentication, "_", Qt.ControlModifier)
# Assert row was not removed
self.assertTrue(self.authentication.rowCount() == 3)
def test_add_to_soi_smoke_test(self):
"""Test that module can be added to SOI."""
soi = SOI()
authentication_name = "Test name"
soi.add_module(authentication_name, self.authentication)
self.assertTrue(soi.module_name_taken(authentication_name))
class TestAuthenticationBoardModuleFromData(unittest.TestCase):
"""TestCase for initializing AuthenticationBoardModule from data."""
def test_create_from_data(self):
"""Test creating AuthenticationBoardModule from data."""

Anders H. Rebner
committed
test_data = {
"cells": [
"Headline text",
["A", "0", "TEST CODE ONE"],
["B", "1", "TEST CODE TWO"],
],
"code_length": 11,
"space_interval": 4,
"space_amount": 1,
"code_character_type": "ascii",
}
test_size = {"width": 100, "height": 100}
module = AuthenticationBoardModule(size=test_size, data=test_data)
# Assert module contains expected data
self.assertEqual(module.item(0, 0).text(), "Headline text")
self.assertEqual(module.item(1, 0).text(), "A")
self.assertEqual(module.item(1, 1).text(), "0")
self.assertEqual(module.item(1, 2).text(), "TEST CODE ONE")
self.assertEqual(module.item(2, 0).text(), "B")
self.assertEqual(module.item(2, 1).text(), "1")
self.assertEqual(module.item(2, 2).text(), "TEST CODE TWO")