def session( url: str, username: str, password: str, mock_session_factory: Callable[[], requests.Session], ): """Create a LimeSurvey Session fixture.""" session = Session(url, username, password, mock_session_factory) yield session session.close()
def test_interface_off( url: str, username: str, password: str, off_session_factory: Callable[[], requests.Session], ): """Test effect of JSON RPC not enabled.""" with pytest.raises(LimeSurveyError, match="JSON RPC interface is not enabled"): Session(url, username, password, off_session_factory)
def test_session_context( url: str, username: str, password: str, mock_session_factory: Callable[[], requests.Session], ): """Test context creates a session key.""" with Session(url, username, password, mock_session_factory) as session: assert not session.closed assert session.key == LimeSurveyMockAdapter.session_key assert session.closed assert session.key is None with pytest.raises(AttributeError, match="can't set attribute"): session.key = "123456" with pytest.raises(AttributeError, match="can't set attribute"): session.closed = False
def test_api_error(session: Session): """Test non-null error raises LimeSurveyApiError.""" with pytest.raises(LimeSurveyApiError, match="API Error!"): session.__api_error()
def test_empty_response(session: Session): """Test empty response.""" with pytest.raises(LimeSurveyError, match="RPC interface not enabled"): session.__disabled()
def test_http_error(session: Session): """Test HTTP errors.""" with pytest.raises(requests.HTTPError): session.__http_error()
def test_json_rpc(session: Session): """Test JSON RPC response structure.""" result = session.__ok() assert result == "OK"
def test_mismatching_request_id(session: Session): """Test result with status key raises LimeSurveyStatusError.""" with pytest.raises(LimeSurveyError, match="response does not match the one in the request"): session.__bad_id()
def test_status_ok(session: Session): """Test result with OK status does not raise errors.""" result = session.__status_ok() assert result["status"] == "OK"
def test_status_error(session: Session): """Test result with status key raises LimeSurveyStatusError.""" with pytest.raises(LimeSurveyStatusError, match="Status Error!"): session.__status_error()