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

Improve _verify_session #70

Merged
merged 4 commits into from
Jul 3, 2024
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
13 changes: 10 additions & 3 deletions src/zinolib/controllers/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,11 @@
if not getattr(self.session, 'request', None):
if quiet:
return False
raise ValueError
raise NotConnectedError("The request socket have not been set up correctly. Reconnect necessary.")
if not self.session.request.connected:
if quiet:
return False

Check warning on line 529 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L529

Added line #L529 was not covered by tests
raise NotConnectedError("Authentication necessary")
return True

@classmethod
Expand All @@ -548,8 +552,11 @@
raise self.ManagerException(e)

def disconnect(self):
self._verify_session()
self.session = self._session_adapter.close_session(self.session)
session_ok = self._verify_session(quiet=True)
if session_ok:
self.session = self._session_adapter.close_session(self.session)

Check warning on line 557 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L555-L557

Added lines #L555 - L557 were not covered by tests
else:
self._session_adapter.close_push_channel(self.session)

Check warning on line 559 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L559

Added line #L559 was not covered by tests

def clear_flapping(self, event_or_id: EventOrId):
"""Clear flapping state of a PortStateEvent
Expand Down
26 changes: 25 additions & 1 deletion tests/test_zinolib_controllers_zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from datetime import datetime, timedelta, timezone

from zinolib.event_types import AdmState, Event, HistoryEntry, LogEntry
from zinolib.controllers.zino1 import EventAdapter, HistoryAdapter, LogAdapter, SessionAdapter, Zino1EventManager, UpdateHandler,RetryError
from zinolib.controllers.zino1 import EventAdapter, HistoryAdapter, LogAdapter, SessionAdapter, Zino1EventManager, UpdateHandler
from zinolib.controllers.zino1 import RetryError, NotConnectedError
from zinolib.ritz import NotifierResponse

raw_event_id = 139110
Expand Down Expand Up @@ -84,6 +85,7 @@ def _setup_config(cls, config):
def _setup_request(session, config):
class FakeSession:
authenticated = True
connected = True

session.request = FakeSession() # needs to be truthy
session.push = True # needs to be truthy
Expand All @@ -106,6 +108,28 @@ def init_manager(self):
zino1 = FakeZino1EventManager.configure(None)
return zino1

def test_verify_session_raises_notconnectederror_on_incorrect_manager(self):
zino1 = FakeZino1EventManager()
with self.assertRaises(NotConnectedError) as e:
zino1._verify_session()
self.assertIn("The request socket have not been set up correctly", e)

def test_verify_session_raises_notconnectederror_if_not_connected(self):
zino1 = FakeZino1EventManager.configure(None)
orig = zino1.session.request.connected
zino1.session.request.connected = False
try:
with self.assertRaises(NotConnectedError) as e:
zino1._verify_session()
self.assertIn("Authentication necessary", e)
finally:
zino1.session.request.connected = orig

def test_verify_session_returns_False_if_quieted_on_incorrect_manager(self):
zino1 = FakeZino1EventManager()
result = zino1._verify_session(quiet=True)
self.assertEqual(result, False)

def test_create_event_from_id_receiving_garbage_admstate_is_safely_handled(self):
global raw_attrlist
zino1 = self.init_manager()
Expand Down
Loading