|
| 1 | +"""Unit tests for user-facing error wording.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from py_moodle.auth import LoginError, MoodleAuth |
| 6 | +from py_moodle.course import MoodleCourseError, list_courses |
| 7 | +from py_moodle.session import MoodleSession, MoodleSessionError |
| 8 | +from py_moodle.settings import Settings |
| 9 | + |
| 10 | + |
| 11 | +class FakeResponse: |
| 12 | + """Minimal HTTP response object for error-message tests.""" |
| 13 | + |
| 14 | + def __init__(self, *, text="", url="https://moodle.example.test/", json_data=None): |
| 15 | + self.text = text |
| 16 | + self.url = url |
| 17 | + self.json_data = json_data |
| 18 | + self.status_code = 200 |
| 19 | + |
| 20 | + def json(self): |
| 21 | + """Return the configured JSON payload.""" |
| 22 | + return self.json_data |
| 23 | + |
| 24 | + def raise_for_status(self): |
| 25 | + """Mirror the requests.Response API for successful responses.""" |
| 26 | + return None |
| 27 | + |
| 28 | + |
| 29 | +class FakeSession: |
| 30 | + """Minimal session object for deterministic unit tests.""" |
| 31 | + |
| 32 | + def __init__(self, *, get_response=None, post_response=None): |
| 33 | + self.get_response = get_response or FakeResponse() |
| 34 | + self.post_response = post_response or FakeResponse(json_data={}) |
| 35 | + self.sesskey = None |
| 36 | + self.webservice_token = None |
| 37 | + |
| 38 | + def get(self, url, **kwargs): |
| 39 | + """Return the canned GET response.""" |
| 40 | + return self.get_response |
| 41 | + |
| 42 | + def post(self, url, **kwargs): |
| 43 | + """Return the canned POST response.""" |
| 44 | + return self.post_response |
| 45 | + |
| 46 | + |
| 47 | +class FakeCompatibility: |
| 48 | + """Minimal compatibility helper that never finds a sesskey.""" |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def extract_sesskey(text): |
| 52 | + """Return no sesskey for the provided HTML payload.""" |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def build_settings(): |
| 57 | + """Create a minimal settings object for session tests.""" |
| 58 | + return Settings( |
| 59 | + env_name="local", |
| 60 | + url="https://moodle.example.test", |
| 61 | + username="user", |
| 62 | + password="secret", |
| 63 | + use_cas=False, |
| 64 | + cas_url=None, |
| 65 | + webservice_token=None, |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def test_standard_login_error_mentions_credentials_and_cas(): |
| 70 | + """Invalid login errors should point users to credentials and CAS settings.""" |
| 71 | + auth = MoodleAuth( |
| 72 | + base_url="https://moodle.example.test", |
| 73 | + username="user", |
| 74 | + password="wrong", |
| 75 | + ) |
| 76 | + auth.compatibility = type( |
| 77 | + "Compat", |
| 78 | + (), |
| 79 | + {"extract_login_token": staticmethod(lambda text: "token")}, |
| 80 | + )() |
| 81 | + auth.session = FakeSession( |
| 82 | + get_response=FakeResponse(text="<input name='logintoken' value='token'>"), |
| 83 | + post_response=FakeResponse( |
| 84 | + text="Invalid login", |
| 85 | + url="https://moodle.example.test/login/index.php", |
| 86 | + ), |
| 87 | + ) |
| 88 | + |
| 89 | + with pytest.raises(LoginError) as excinfo: |
| 90 | + auth._standard_login() |
| 91 | + |
| 92 | + message = str(excinfo.value) |
| 93 | + assert "invalid username or password" in message |
| 94 | + assert "MOODLE_USERNAME" in message |
| 95 | + assert "CAS" in message |
| 96 | + |
| 97 | + |
| 98 | +def test_session_login_error_mentions_webservice_and_cas(monkeypatch): |
| 99 | + """Missing token and sesskey errors should point to the likely fixes.""" |
| 100 | + fake_session = FakeSession(get_response=FakeResponse(text="<html></html>")) |
| 101 | + monkeypatch.setattr("py_moodle.session.login", lambda *args, **kwargs: fake_session) |
| 102 | + monkeypatch.setattr( |
| 103 | + "py_moodle.session.get_session_compatibility", |
| 104 | + lambda session: FakeCompatibility(), |
| 105 | + ) |
| 106 | + |
| 107 | + moodle_session = MoodleSession(build_settings()) |
| 108 | + |
| 109 | + with pytest.raises(MoodleSessionError) as excinfo: |
| 110 | + moodle_session._login() |
| 111 | + |
| 112 | + message = str(excinfo.value) |
| 113 | + assert "no webservice token or sesskey was available" in message |
| 114 | + assert "Moodle mobile web service" in message |
| 115 | + assert "CAS/session configuration" in message |
| 116 | + |
| 117 | + |
| 118 | +def test_session_call_without_token_mentions_wsfunction(): |
| 119 | + """Missing-token errors should explain how to restore webservice access.""" |
| 120 | + moodle_session = MoodleSession(build_settings()) |
| 121 | + moodle_session._session = FakeSession() |
| 122 | + |
| 123 | + with pytest.raises(LoginError) as excinfo: |
| 124 | + moodle_session.call("core_webservice_get_site_info") |
| 125 | + |
| 126 | + message = str(excinfo.value) |
| 127 | + assert "core_webservice_get_site_info" in message |
| 128 | + assert "pre-configured token" in message |
| 129 | + assert "Moodle mobile web service" in message |
| 130 | + |
| 131 | + |
| 132 | +def test_session_call_api_error_mentions_wsfunction(): |
| 133 | + """API errors should include the failing Moodle webservice function name.""" |
| 134 | + moodle_session = MoodleSession(build_settings()) |
| 135 | + moodle_session._session = FakeSession( |
| 136 | + post_response=FakeResponse( |
| 137 | + json_data={ |
| 138 | + "message": "Access control exception", |
| 139 | + "errorcode": "accessexception", |
| 140 | + "exception": "required_capability_exception", |
| 141 | + } |
| 142 | + ) |
| 143 | + ) |
| 144 | + moodle_session._token = "token" |
| 145 | + |
| 146 | + with pytest.raises(MoodleSessionError) as excinfo: |
| 147 | + moodle_session.call("core_course_get_courses") |
| 148 | + |
| 149 | + message = str(excinfo.value) |
| 150 | + assert "core_course_get_courses" in message |
| 151 | + assert "Access control exception" in message |
| 152 | + assert "accessexception" in message |
| 153 | + |
| 154 | + |
| 155 | +def test_list_courses_error_mentions_token_or_sesskey(): |
| 156 | + """Course-listing errors should explain which session credentials are missing.""" |
| 157 | + with pytest.raises(MoodleCourseError) as excinfo: |
| 158 | + list_courses(object(), "https://moodle.example.test") |
| 159 | + |
| 160 | + message = str(excinfo.value) |
| 161 | + assert "valid webservice token or sesskey" in message |
| 162 | + assert "Moodle mobile web service" in message |
0 commit comments