Skip to content

Commit bb80a48

Browse files
Added more Moodle exchange cleaning.
1 parent 80bcf15 commit bb80a48

File tree

5 files changed

+63
-12
lines changed

5 files changed

+63
-12
lines changed

lms/backend/moodle/backend_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def child_class_setup(cls) -> None:
3636
'auth_password': DEFAULT_USER,
3737
})
3838

39+
cls.params_to_skip += [
40+
'logintoken',
41+
]
42+
3943
cls.headers_to_skip += [
4044
'edq-lms-moodle-user',
4145
]

lms/testing/serverrunner.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
import lms.model.constants
1212
import lms.util.net
1313

14-
BACKEND_REQUEST_CLEANING_FUNCS: typing.Dict[str, typing.Callable] = {
14+
BACKEND_REQUEST_CLEANING_FUNCS: typing.Dict[typing.Union[str, None], typing.Callable] = {
1515
lms.model.constants.BACKEND_TYPE_BLACKBOARD: lms.util.net.clean_blackboard_response,
1616
lms.model.constants.BACKEND_TYPE_CANVAS: lms.util.net.clean_canvas_response,
1717
lms.model.constants.BACKEND_TYPE_MOODLE: lms.util.net.clean_moodle_response,
1818
}
1919

20+
BACKEND_EXCHANGE_FINALIZING_FUNCS: typing.Dict[typing.Union[str, None], typing.Callable] = {
21+
lms.model.constants.BACKEND_TYPE_MOODLE: lms.util.net.finalize_moodle_exchange,
22+
}
23+
2024
class LMSServerRunner(edq.testing.serverrunner.ServerRunner):
2125
""" A server runner specifically for LMS servers. """
2226

@@ -38,6 +42,12 @@ def __init__(self,
3842
The original value may be changed in start(), and will be reset in stop().
3943
"""
4044

45+
self._old_exchanges_finalize_func: typing.Union[str, None] = None
46+
"""
47+
The value of edq.util.net._exchanges_finalize_func when start() is called.
48+
The original value may be changed in start(), and will be reset in stop().
49+
"""
50+
4151
self._old_set_exchanges_clean_func: bool = False
4252
"""
4353
The value of lms.cli.parser._set_exchanges_clean_func when start() is called.
@@ -57,21 +67,21 @@ def __init__(self,
5767
"""
5868

5969
def start(self) -> None:
60-
super().start()
61-
62-
# Resolve the backend type.
63-
self.backend_type = lms.backend.instance.guess_backend_type(self.server, backend_type = self.backend_type)
64-
if (self.backend_type is None):
65-
raise ValueError(f"Unable to determine backend type for server '{self.server}'.")
66-
6770
# Set configs.
6871

6972
exchange_clean_func = BACKEND_REQUEST_CLEANING_FUNCS.get(self.backend_type, lms.util.net.clean_lms_response)
7073
exchange_clean_func_name = edq.util.reflection.get_qualified_name(exchange_clean_func)
71-
7274
self._old_exchanges_clean_func = edq.util.net._exchanges_clean_func
7375
edq.util.net._exchanges_clean_func = exchange_clean_func_name
7476

77+
self._old_exchanges_finalize_func = edq.util.net._exchanges_finalize_func
78+
exchange_finalize_func = BACKEND_EXCHANGE_FINALIZING_FUNCS.get(self.backend_type, None)
79+
if (exchange_finalize_func is not None):
80+
exchange_finalize_func_name = edq.util.reflection.get_qualified_name(exchange_finalize_func)
81+
edq.util.net._exchanges_finalize_func = exchange_finalize_func_name
82+
else:
83+
edq.util.net._exchanges_finalize_func = None
84+
7585
self._old_set_exchanges_clean_func = lms.cli.parser._set_exchanges_clean_func
7686
lms.cli.parser._set_exchanges_clean_func = False
7787

@@ -88,6 +98,9 @@ def _make_request_callback(exchange: edq.util.net.HTTPExchange) -> None:
8898
self._old_serverrunner_logging_level = logger.level
8999
logger.setLevel(logging.WARNING)
90100

101+
# Start the server.
102+
super().start()
103+
91104
def stop(self) -> bool:
92105
if (self._old_serverrunner_logging_level is not None):
93106
logger = logging.getLogger('edq.testing.serverrunner')

lms/util/net.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import urllib.parse
77

88
import edq.util.json
9+
import edq.util.net
910
import requests
1011

1112
import lms.model.constants
@@ -43,7 +44,26 @@
4344
'vary',
4445
'x-blackboard-xsrf',
4546
}
46-
""" Keys to remove from Blackboard content. """
47+
""" Keys to remove from Blackboard headers. """
48+
49+
MOODLE_CLEAN_REMOVE_HEADERS: typing.Set[str] = {
50+
'accept-ranges',
51+
'content-encoding',
52+
'content-language',
53+
'content-script-type',
54+
'content-style-type',
55+
'expires',
56+
'keep-alive',
57+
'last-modified',
58+
'pragma',
59+
'vary',
60+
}
61+
""" Keys to remove from Moodle headers. """
62+
63+
MOODLE_FINALIZE_REMOVE_PARAMS: typing.Set[str] = {
64+
'logintoken',
65+
}
66+
""" Keys to remove from Moodle headers. """
4767

4868
def clean_lms_response(response: requests.Response, body: str) -> str:
4969
"""
@@ -146,8 +166,22 @@ def clean_moodle_response(response: requests.Response, body: str) -> str:
146166

147167
body = _clean_base_response(response, body)
148168

169+
# Work on both request and response headers.
170+
for headers in [response.headers, response.request.headers]:
171+
for key in list(headers.keys()):
172+
if (key.strip().lower() in MOODLE_CLEAN_REMOVE_HEADERS):
173+
headers.pop(key, None)
174+
149175
return body
150176

177+
def finalize_moodle_exchange(exchange: edq.util.net.HTTPExchange) -> edq.util.net.HTTPExchange:
178+
""" Finalize Moodle exhanges. """
179+
180+
for param in MOODLE_FINALIZE_REMOVE_PARAMS:
181+
exchange.parameters.pop(param, None)
182+
183+
return exchange
184+
151185
def _clean_base_response(response: requests.Response, body: str,
152186
keep_headers: typing.Union[typing.List[str], None] = None) -> str:
153187
"""

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
beautifulsoup4>=4.10.0
2-
edq-utils>=0.2.5
2+
edq-utils>=0.2.7
33
requests>=2.31.0

0 commit comments

Comments
 (0)