-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_trust_file_list.py
175 lines (144 loc) · 5.71 KB
/
test_trust_file_list.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
# Copyright Concurrent Technologies Corporation 2021
#
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import context # noqa: F401
import gi
import pytest
gi.require_version("Gtk", "3.0")
from time import localtime, mktime, strftime
from unittest.mock import MagicMock
from gi.repository import Gtk
from helpers import refresh_gui
from fapolicy_analyzer.ui.trust_file_list import TrustFileList, epoch_to_string
_trust = [
MagicMock(status="u", path="/tmp/bar", actual=MagicMock(last_modified=123456789)),
MagicMock(status="t", path="/tmp/foo", actual=MagicMock(last_modified=123456789)),
MagicMock(
status="u", path="/tmp/foobar", actual=MagicMock(last_modified=123456789)
),
]
@pytest.fixture
def widget():
widget = TrustFileList(trust_func=MagicMock())
return widget
def test_creates_widget(widget):
assert type(widget.get_ref()) is Gtk.Box
def test_uses_custom_trust_func():
trust_func = MagicMock()
TrustFileList(trust_func=trust_func)
trust_func.assert_called()
def test_uses_custom_markup_func(mocker):
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.ThreadPoolExecutor",
return_value=MagicMock(submit=lambda x, *args: x(*args)),
)
markup_func = MagicMock(return_value="t")
widget = TrustFileList(trust_func=MagicMock(), markup_func=markup_func)
widget.init_list(3)
widget.append_trust(_trust)
markup_func.assert_called_with("u")
def test_loads_trust_store(widget, mocker):
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.ThreadPoolExecutor",
return_value=MagicMock(submit=lambda x: x()),
)
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.GLib.idle_add",
side_effect=lambda x, args: x(args),
)
widget.init_list(3)
widget.show_trusted = True
widget.append_trust(_trust)
refresh_gui(delay=0.5)
view = widget.get_object("treeView")
assert [t.status for t in _trust] == [x[0] for x in view.get_model()]
assert [t.path for t in _trust] == [x[2] for x in view.get_model()]
def test_cancels_load_trust_store(widget, mocker):
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.ThreadPoolExecutor",
return_value=MagicMock(submit=lambda x: x()),
)
mockIdleAdd = mocker.patch("fapolicy_analyzer.ui.trust_file_list.GLib.idle_add")
widget.init_list(3)
# trust_file_list._executorCanceled = True
widget.on_destroy()
widget.append_trust(_trust)
refresh_gui(delay=0.5)
mockIdleAdd.assert_not_called()
def test_fires_trust_selection_changed(widget):
store = Gtk.ListStore(str, str, str, object, str)
store.append(["f", "o", "o", _trust[0], "baz"])
widget.load_store(1, store)
mockHandler = MagicMock()
widget.trust_selection_changed += mockHandler
view = widget.get_object("treeView")
view.get_selection().select_path(Gtk.TreePath.new_first())
mockHandler.assert_called_with([_trust[0]])
def test_epoch_to_string():
secsEpochCurrent = int(mktime(localtime()))
secsEpochOneDayOld = secsEpochCurrent - (24 * 60 * 60)
# Verify timestamps from today are display in hour:minute:sec format
strExpected = strftime("%H:%M:%S", localtime(secsEpochCurrent))
strFromEpochToString = epoch_to_string(secsEpochCurrent)
assert strExpected == strFromEpochToString
# Verify timestamps older than today are displayed as calendar date
strExpected = strftime("%Y-%m-%d", localtime(secsEpochOneDayOld))
strFromEpochToString = epoch_to_string(secsEpochOneDayOld)
assert strExpected == strFromEpochToString
# Verify a NULL string input outputs "Missing"
assert epoch_to_string(None) == "Missing"
def test_tree_count_full(widget, mocker):
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.ThreadPoolExecutor",
return_value=MagicMock(submit=lambda x: x()),
)
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.GLib.idle_add",
side_effect=lambda x, args: x(args),
)
widget.init_list(3)
widget.append_trust(_trust)
refresh_gui(delay=0.5)
assert widget.treeCount.get_text() == "2 files"
def test_tree_count_empty(widget, mocker):
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.ThreadPoolExecutor",
return_value=MagicMock(submit=lambda x: x()),
)
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.GLib.idle_add",
side_effect=lambda x, args: x(args),
)
widget.init_list(0)
widget.append_trust([])
refresh_gui(delay=0.5)
assert widget.treeCount.get_text() == "0 files"
def test_tree_count_partial(widget, mocker):
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.ThreadPoolExecutor",
return_value=MagicMock(submit=lambda x: x()),
)
mocker.patch(
"fapolicy_analyzer.ui.trust_file_list.GLib.idle_add",
side_effect=lambda x, args: x(args),
)
widget.init_list(3)
widget.append_trust(_trust)
refresh_gui(delay=0.5)
assert widget.treeCount.get_text() == "2 files"
viewFilter = widget.get_object("search")
viewFilter.set_text("foo")
widget.on_search_activate()
refresh_gui(delay=0.3)
assert widget.treeCount.get_text() == "1 / 2 files"