Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daemon state preserved on session load #914

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions fapolicy_analyzer/tests/test_session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
import os
import time
from datetime import datetime as DT
from unittest.mock import MagicMock, call, mock_open
from unittest.mock import MagicMock, mock_open

import pytest
from callee import Attr, EndsWith, InstanceOf, StartsWith
from fapolicy_analyzer.redux import Action
from callee import Attr, EndsWith, StartsWith
from fapolicy_analyzer.ui.actions import (
ADD_NOTIFICATION,
APPLY_CHANGESETS,
CLEAR_CHANGESETS,
RESTORE_SYSTEM_CHECKPOINT,
)
from fapolicy_analyzer.ui.changeset_wrapper import RuleChangeset, TrustChangeset
from fapolicy_analyzer.ui.session_manager import SessionManager
Expand Down Expand Up @@ -105,18 +102,12 @@ def test_open_edit_session(uut, mock_dispatch, mocker):
mocker.patch("builtins.open", mock_open(read_data=test_json))
uut.open_edit_session("foo")

mock_dispatch.assert_has_calls(
[
call(InstanceOf(Action) & Attr(type=RESTORE_SYSTEM_CHECKPOINT)),
call(InstanceOf(Action) & Attr(type=CLEAR_CHANGESETS)),
call(InstanceOf(Action) & Attr(type=APPLY_CHANGESETS)),
]
)
mock_dispatch.assert_called_with(Attr(type=APPLY_CHANGESETS))

# verify changesets
expected = test_changes
# parse changesets to json string to compare
args, _ = mock_dispatch.call_args_list[2]
args, _ = mock_dispatch.call_args
actual = [
{path: action for path, action in c.serialize().items()}
if isinstance(c, TrustChangeset)
Expand Down Expand Up @@ -276,18 +267,12 @@ def test_restore_previous_session(uut_autosave_enabled, autosaved_files, mock_di
# Convert to sets so that ordering is irrelevant
assert set(listTmpFiles) == set(autosaved_files)
assert uut_autosave_enabled.restore_previous_session()
mock_dispatch.assert_has_calls(
[
call(InstanceOf(Action) & Attr(type=RESTORE_SYSTEM_CHECKPOINT)),
call(InstanceOf(Action) & Attr(type=CLEAR_CHANGESETS)),
call(InstanceOf(Action) & Attr(type=APPLY_CHANGESETS)),
]
)
mock_dispatch.assert_called_with(Attr(type=APPLY_CHANGESETS))

# verify changesets
expected = test_changes
# parse changesets to json string to compare
args, _ = mock_dispatch.call_args_list[2]
args, _ = mock_dispatch.call_args
actual = [
{path: action for path, action in c.serialize().items()}
if isinstance(c, TrustChangeset)
Expand Down
8 changes: 2 additions & 6 deletions fapolicy_analyzer/ui/session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
NotificationType,
add_notification,
apply_changesets,
clear_changesets,
restore_system_checkpoint,
)
from fapolicy_analyzer.ui.changeset_wrapper import Changeset
from fapolicy_analyzer.ui.store import dispatch, get_system_feature
Expand Down Expand Up @@ -140,14 +138,12 @@ def open_edit_session(self, strJsonFile: str) -> bool:
)
return False

logging.debug("Loaded dict = ", data)
logging.debug(f"Loaded dict = {data}")
changesets = [Changeset.deserialize(d) for d in data]
logging.debug("SessionManager::open_edit_session():{}".format(changesets))

if changesets:
# Deleting current edit session history prior to replacing it.
dispatch(restore_system_checkpoint())
dispatch(clear_changesets())
# Layer changeset on top of existing system changeset (if any)
dispatch(apply_changesets(*changesets))
return True

Expand Down