Skip to content

Commit

Permalink
fix: Remove explicit check for enabled RPC interface (#143)
Browse files Browse the repository at this point in the history
* fix: Remove explicit check for enabled RPC interface

* fix: Use new ResponseMismatchError exception
  • Loading branch information
edgarrmondragon committed Jan 28, 2022
1 parent 10d8b05 commit 8effb13
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/citric/exceptions.py
@@ -1,6 +1,10 @@
"""Common exceptions."""


class ResponseMismatchError(Exception):
"""Exception raised when request and response ID don't match."""


class LimeSurveyError(Exception):
"""Basic exception raised by LimeSurvey."""

Expand Down
12 changes: 4 additions & 8 deletions src/citric/session.py
Expand Up @@ -10,6 +10,7 @@
LimeSurveyError,
LimeSurveyStatusError,
LimeSurveyApiError,
ResponseMismatchError,
)
from citric.method import Method

Expand All @@ -29,9 +30,6 @@ class Session:
For example, `AuthLDAP`_. Defaults to using the internal database
(``"Authdb"``).
Raises:
LimeSurveyError: If the JSON RPC interface is not enabled.
.. _requests.Session:
https://docs.python-requests.org/en/latest/api/#requests.Session
.. _plugin: https://manual.limesurvey.org/Authentication_plugins
Expand Down Expand Up @@ -65,9 +63,6 @@ def __init__(
auth_plugin,
)

if self.get_site_settings("RPCInterface") != "json":
raise LimeSurveyError("JSON RPC interface is not enabled.")

self.__closed = False

@property
Expand Down Expand Up @@ -119,7 +114,8 @@ def _invoke(session: requests.Session, url: str, method: str, *params: Any) -> A
LimeSurveyStatusError: The response key from the response payload has
a non-null status.
LimeSurveyApiError: The response payload has a non-null error key.
LimeSurveyError: Request ID does not match the response ID.
LimeSurveyError: If the JSON RPC interface is not enabled (empty response).
ResponseMismatchError: Request ID does not match the response ID.
Returns:
Any: An RPC result.
Expand Down Expand Up @@ -152,7 +148,7 @@ def _invoke(session: requests.Session, url: str, method: str, *params: Any) -> A
raise LimeSurveyApiError(error)

if response_id != request_id:
raise LimeSurveyError(
raise ResponseMismatchError(
f"Response ID {response_id} does not match request ID {request_id}",
)

Expand Down
14 changes: 2 additions & 12 deletions tests/test_rpc.py
Expand Up @@ -8,6 +8,7 @@
LimeSurveyApiError,
LimeSurveyError,
LimeSurveyStatusError,
ResponseMismatchError,
)
from citric.method import Method
from citric.session import Session
Expand Down Expand Up @@ -81,17 +82,6 @@ def test_session_context(
session.closed = False


def test_interface_off(
url: str,
username: str,
password: str,
off_session: requests.Session,
):
"""Test effect of JSON RPC not enabled."""
with pytest.raises(LimeSurveyError, match="JSON RPC interface is not enabled"):
Session(url, username, password, requests_session=off_session)


def test_empty_response(session: Session):
"""Test empty response."""
with pytest.raises(LimeSurveyError, match="RPC interface not enabled"):
Expand Down Expand Up @@ -126,6 +116,6 @@ def randint(a, b):
monkeypatch.setattr(random, "randint", randint)

with pytest.raises(
LimeSurveyError, match="Response ID 2 does not match request ID 123"
ResponseMismatchError, match="Response ID 2 does not match request ID 123"
):
session.__bad_id()

0 comments on commit 8effb13

Please sign in to comment.