Skip to content

Commit

Permalink
Issue 81 (#82): Removed file chooser
Browse files Browse the repository at this point in the history
* removed file chooser and update System calls

* Fixed up Status and colors

* fixed tests

* fix broken tests

* added UI linting to the CI
  • Loading branch information
dorschs57 committed Apr 14, 2021
1 parent 4a7a7a3 commit ddb39fd
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 111 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/main.yml
Expand Up @@ -74,6 +74,25 @@ jobs:
name: fapolicy-analyzer-package
path: ${{ steps.build.outputs.virt_env }}/lib/python3.8/site-packages/fapolicy_analyzer*.egg/

ui_lint:
name: UI Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
id: install_deps
run: |
python -m pip install --upgrade pip
pip install flake8
- name: Lint with flake8
run: |
cd python
python -m flake8 ./fapolicy_analyzer
ui_test:
name: UI Test Suite
needs: py_build
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,6 +14,7 @@ dist

.vscode/

\#*.glade\#
*.glade~
.coverage
.env
Expand Down
3 changes: 3 additions & 0 deletions python/.flake8
@@ -0,0 +1,3 @@
[flake8]
ignore = E402,W503
max-line-length = 120
1 change: 1 addition & 0 deletions python/Pipfile
Expand Up @@ -27,4 +27,5 @@ allow_prereleases = true

[scripts]
test = "pipenv run xvfb-run -a pytest -s --cov-report term-missing --cov=ui fapolicy_analyzer/"
lint = "pipenv run flake8 fapolicy_analyzer/"
watch-test = "pipenv run xvfb-run -a pytest-watch fapolicy_analyzer/ -- -s"
2 changes: 1 addition & 1 deletion python/fapolicy_analyzer/__init__.py
@@ -1 +1 @@
from .rust import *
from .rust import * # noqa: F401,F403
7 changes: 7 additions & 0 deletions python/fapolicy_analyzer/tests/conftest.py
@@ -0,0 +1,7 @@
import pytest
from mocks import mock_System


@pytest.fixture(autouse=True)
def widget(mocker):
mocker.patch("ui.database_admin_page.System", return_value=mock_System())
Expand Up @@ -5,13 +5,14 @@
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from unittest.mock import MagicMock
from helpers import delayed_gui_action
from mocks import mock_System
from ui.ancillary_trust_database_admin import AncillaryTrustDatabaseAdmin
from ui.configs import Colors


@pytest.fixture
def widget():
return AncillaryTrustDatabaseAdmin()
return AncillaryTrustDatabaseAdmin(mock_System())


def test_creates_widget(widget):
Expand All @@ -20,16 +21,16 @@ def test_creates_widget(widget):

def test_status_markup(widget):
assert widget._AncillaryTrustDatabaseAdmin__status_markup("T") == (
"<b><u>T</u></b>/U",
"light green",
"<b><u>T</u></b>/D/U",
Colors.LIGHT_GREEN,
)
assert widget._AncillaryTrustDatabaseAdmin__status_markup("U") == (
"T/<b><u>U</u></b>",
"gold",
assert widget._AncillaryTrustDatabaseAdmin__status_markup("D") == (
"T/<b><u>D</u></b>/U",
Colors.LIGHT_RED,
)
assert widget._AncillaryTrustDatabaseAdmin__status_markup("foo") == (
"T/U",
"light red",
"T/D/<b><u>U</u></b>",
Colors.LIGHT_YELLOW,
)


Expand Down
2 changes: 1 addition & 1 deletion python/fapolicy_analyzer/tests/test_confirmation_dialog.py
Expand Up @@ -22,6 +22,6 @@ def test_trust_database_admin_selection():
dialog = ConfirmDialog("foo").get_content()
for expected in [Gtk.ResponseType.YES, Gtk.ResponseType.NO]:
button = dialog.get_widget_for_response(expected)
delayed_gui_action(button.clicked)
delayed_gui_action(button.clicked, delay=1)
response = dialog.run()
assert response == expected
9 changes: 7 additions & 2 deletions python/fapolicy_analyzer/tests/test_database_admin_page.py
Expand Up @@ -4,12 +4,17 @@

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from mocks import mock_System
from helpers import refresh_gui
from ui.database_admin_page import DatabaseAdminPage


@pytest.fixture
def widget():
return DatabaseAdminPage()
def widget(mocker):
mocker.patch("ui.database_admin_page.System", return_value=mock_System())
widget = DatabaseAdminPage()
refresh_gui()
return widget


def test_creates_widget(widget):
Expand Down
Expand Up @@ -6,16 +6,13 @@
from gi.repository import Gtk
from unittest.mock import MagicMock
from mocks import mock_System
from helpers import refresh_gui
from ui.system_trust_database_admin import SystemTrustDatabaseAdmin
from ui.configs import Colors


@pytest.fixture
def widget(mocker):
mocker.patch("ui.system_trust_database_admin.System", return_value=mock_System())
widget = SystemTrustDatabaseAdmin()
refresh_gui()
return widget
def widget():
return SystemTrustDatabaseAdmin(mock_System())


def test_creates_widget(widget):
Expand All @@ -24,10 +21,13 @@ def test_creates_widget(widget):

def test_status_markup(widget):
assert widget._SystemTrustDatabaseAdmin__status_markup("T") == (
"<b><u>T</u></b>",
"light green",
"<b><u>T</u></b>/D",
Colors.LIGHT_GREEN,
)
assert widget._SystemTrustDatabaseAdmin__status_markup("foo") == (
"T/<b><u>D</u></b>",
Colors.LIGHT_RED,
)
assert widget._SystemTrustDatabaseAdmin__status_markup("foo") == ("T", "light red")


def test_updates_trust_details(widget, mocker):
Expand Down
42 changes: 11 additions & 31 deletions python/fapolicy_analyzer/tests/test_trust_file_list.py
Expand Up @@ -4,17 +4,11 @@

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from helpers import refresh_gui
from mocks import mock_System
from unittest.mock import MagicMock
from helpers import refresh_gui
from ui.trust_file_list import TrustFileList


@pytest.fixture
def patch(mocker):
mocker.patch("ui.trust_file_list.System", return_value=mock_System())


@pytest.fixture
def trust_func():
return MagicMock(
Expand All @@ -27,55 +21,41 @@ def trust_func():

@pytest.fixture
def widget(trust_func):
widget = TrustFileList(defaultLocation="/foo.db", trust_func=trust_func)
refresh_gui()
widget = TrustFileList(trust_func=trust_func)
refresh_gui(0.2)
return widget


def test_creates_widget(widget):
assert type(widget.get_content()) is Gtk.Box


def test_sets_defaultLocation(patch):
widget = TrustFileList(
locationAction=Gtk.FileChooserAction.SELECT_FOLDER, defaultLocation="/tmp/foo"
)
assert (
widget.databaseFileChooser.get_action() == Gtk.FileChooserAction.SELECT_FOLDER
)
assert widget.databaseFileChooser.get_filename() == "/tmp/foo"


def test_uses_custom_trust_func(patch, trust_func):
widget = TrustFileList(trust_func=trust_func)
widget.databaseFileChooser.set_filename("/foo.db")
refresh_gui()
trust_func.assert_called_with("/foo.db")
def test_uses_custom_trust_func(widget, trust_func):
trust_func.assert_called()


def test_uses_custom_markup_func(patch):
def test_uses_custom_markup_func(trust_func):
markup_func = MagicMock(return_value="t")
widget = TrustFileList(markup_func=markup_func)
widget.databaseFileChooser.set_filename("/foo")
TrustFileList(trust_func=trust_func, markup_func=markup_func)
refresh_gui(0.2)
markup_func.assert_called_with("trusted")
markup_func.assert_called_with("t")


def test_sorting_path(patch, widget):
def test_sorting_path(widget):
trustView = widget.trustView
assert ["/tmp/foo", "/tmp/baz"] == [x[1] for x in trustView.get_model()]
trustView.get_column(1).clicked()
assert ["/tmp/baz", "/tmp/foo"] == [x[1] for x in trustView.get_model()]


def test_sorting_status(patch, widget):
def test_sorting_status(widget):
trustView = widget.trustView
assert ["u", "t"] == [x[0] for x in trustView.get_model()]
trustView.get_column(0).clicked()
assert ["t", "u"] == [x[0] for x in trustView.get_model()]


def test_filtering(patch, widget):
def test_filtering(widget):
trustView = widget.trustView
trustViewFilter = widget.builder.get_object("trustViewSearch")
trustViewFilter.set_text("foo")
Expand Down
15 changes: 9 additions & 6 deletions python/fapolicy_analyzer/ui/ancillary_trust_database_admin.py
Expand Up @@ -8,14 +8,17 @@
from .trust_file_details import TrustFileDetails
from .confirmation_dialog import ConfirmDialog
from .deploy_confirm_dialog import DeployConfirmDialog
from .configs import Colors


class AncillaryTrustDatabaseAdmin(UIWidget):
def __init__(self):
def __init__(self, system):
super().__init__()
self.content = self.builder.get_object("ancillaryTrustDatabaseAdmin")

self.trustFileList = TrustFileList(markup_func=self.__status_markup)
self.trustFileList = TrustFileList(
trust_func=system.ancillary_trust, markup_func=self.__status_markup
)
self.trustFileList.on_file_selection_change += self.on_file_selection_change
self.builder.get_object("leftBox").pack_start(
self.trustFileList.get_content(), True, True, 0
Expand All @@ -29,11 +32,11 @@ def __init__(self):
def __status_markup(self, status):
s = status.lower()
return (
("<b><u>T</u></b>/U", "light green")
("<b><u>T</u></b>/D/U", Colors.LIGHT_GREEN)
if s == "t"
else ("T/<b><u>U</u></b>", "gold")
if s == "u"
else ("T/U", "light red")
else ("T/<b><u>D</u></b>/U", Colors.LIGHT_RED)
if s == "d"
else ("T/D/<b><u>U</u></b>", Colors.LIGHT_YELLOW)
)

def get_content(self):
Expand Down
4 changes: 4 additions & 0 deletions python/fapolicy_analyzer/ui/configs.py
@@ -0,0 +1,4 @@
class Colors:
LIGHT_RED = "#FF3333"
LIGHT_YELLOW = "#FFFF33"
LIGHT_GREEN = "light green"
6 changes: 4 additions & 2 deletions python/fapolicy_analyzer/ui/database_admin_page.py
Expand Up @@ -2,19 +2,21 @@

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from fapolicy_analyzer import System
from .ancillary_trust_database_admin import AncillaryTrustDatabaseAdmin
from .system_trust_database_admin import SystemTrustDatabaseAdmin


class DatabaseAdminPage:
def __init__(self):
system = System()
self.notebook = Gtk.Notebook()
self.notebook.append_page(
SystemTrustDatabaseAdmin().get_content(),
SystemTrustDatabaseAdmin(system).get_content(),
Gtk.Label(label="System Trust Database"),
)
self.notebook.append_page(
AncillaryTrustDatabaseAdmin().get_content(),
AncillaryTrustDatabaseAdmin(system).get_content(),
Gtk.Label(label="Ancillary Trust Database"),
)
self.notebook.show_all()
Expand Down
18 changes: 5 additions & 13 deletions python/fapolicy_analyzer/ui/system_trust_database_admin.py
@@ -1,24 +1,16 @@
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from fapolicy_analyzer import System
from fapolicy_analyzer.util import fs
from .trust_file_list import TrustFileList
from .trust_file_details import TrustFileDetails
from .ui_widget import UIWidget

systemDb = "/var/lib/rpm"
from .configs import Colors


class SystemTrustDatabaseAdmin(UIWidget):
def __init__(self):
def __init__(self, system):
super().__init__()

self.trustFileList = TrustFileList(
locationAction=Gtk.FileChooserAction.SELECT_FOLDER,
defaultLocation=systemDb,
trust_func=lambda x: System(None, x, None).system_trust(),
trust_func=system.system_trust,
markup_func=self.__status_markup,
)
self.trustFileList.on_file_selection_change += self.on_file_selection_change
Expand All @@ -33,9 +25,9 @@ def __init__(self):

def __status_markup(self, status):
return (
("<b><u>T</u></b>", "light green")
("<b><u>T</u></b>/D", Colors.LIGHT_GREEN)
if status.lower() == "t"
else ("T", "light red")
else ("T/<b><u>D</u></b>", Colors.LIGHT_RED)
)

def get_content(self):
Expand Down

0 comments on commit ddb39fd

Please sign in to comment.