caldav: add EGW SQL log cleaning (#88974)
gitea/passerelle/pipeline/head This commit looks good Details

This commit is contained in:
Yann Weber 2024-04-02 17:08:10 +02:00
parent 71d40f1230
commit 23a59e3f0f
2 changed files with 78 additions and 0 deletions

View File

@ -114,6 +114,22 @@ EVENT_SCHEMA = {
}
def clean_egw_response(response, *args, **kwargs):
'''requests hooks that modify requests's responses deleting
EGW's SQL log lines when there is some
SQL log lines are matched by checking that they :
- startswith "==> SQL =>"
- endswith "<br>"
'''
response._content = b'\n'.join(
line
for line in response.content.split(b'\n')
if not line.startswith(b'==> SQL =>') or not line.endswith(b'<br>')
)
return response
class CalDAV(BaseResource):
dav_url = models.URLField(
blank=False,
@ -137,6 +153,8 @@ class CalDAV(BaseResource):
# Replace DAVClient.session requests.Session instance by our
# own requests session in order to log DAV interactions
client.session = self.requests
# adds EGW response cleaning hook
self.requests.hooks['response'] = clean_egw_response
return client
def check_status(self):

View File

@ -79,6 +79,9 @@ END:VEVENT
END:VCALENDAR
'''
# Sometime EGW will add this line somewhere in its replies
SQL_LOG = '==> SQL => SELECT d.adnum as num, d.adsrc as def from pg_attrdef d, pg_class c where d.adrelid=c.oid and c.relname=\'egw_cal\' order by d.adnum<br>'
SOME_EVENTS = [
{'DTSTART': '2020-02-20T20:02', 'DTEND': '2020-02-22T22:43', 'SUMMARY': 'Test'},
{
@ -171,6 +174,20 @@ def assert_match_vevent(vevent, expt_event):
assert v == vevent.decoded(k)
def add_egw_sql_log(content, lineno=0):
'''Adds the EGW's random SQL log to some content
- lineno : indicate where to insert the line (-1 for last line)
'''
spl = content.split('\n')
if lineno == -1:
spl.append(SQL_LOG)
else:
if lineno < 0:
lineno += 1
spl.insert(lineno, SQL_LOG)
return '\n'.join(spl)
#
# Tests
#
@ -502,3 +519,46 @@ def test_caldav_event_delete_get_event_fail(app, caldav_conn):
assert resp.json['err'] != 0
assert resp.json['err_class'] == APIERROR_CLS
assert resp.json['err_desc'] == 'Unable to get event %r in calendar owned by %r' % (evt_id, username)
@pytest.mark.parametrize('lineno', (0, 10, -1))
@responses.activate
def test_egw_sql_log_propfind(app, caldav_conn, lineno):
response_body = add_egw_sql_log(PROPFIND_REPLY, lineno)
responses.add('PROPFIND', DAV_URL, body=response_body, content_type='text/xml')
caldav_conn.check_status()
assert len(responses.calls) == 1
assert responses.calls[0].request.method == 'PROPFIND'
assert responses.calls[0].request.url == DAV_URL
@pytest.mark.parametrize('lineno', (0, 10, -1))
@responses.activate
def test_egw_sql_log_update(app, caldav_conn, lineno):
event_update = SOME_UPDATES[0]
response_body = add_egw_sql_log(FAKE_ICS, lineno)
url = tests.utils.generic_endpoint_url('caldav', 'event/update')
username = 'toto'
evt_id = '01234567-abcd'
evt_url = DAV_URL + get_event_path(username, evt_id)
qs_params = {'username': username, 'event_id': evt_id}
responses.add(responses.GET, evt_url, body=response_body)
responses.add(responses.PUT, evt_url, status=204, body='')
resp = app_post(app, url, qs_params, event_update)
assert resp.json['err'] == 0
assert resp.json['data']['event_id'] == evt_id
assert len(responses.calls) == 2
assert responses.calls[1].request.method == 'PUT'
calendar = icalendar.Calendar.from_ical(responses.calls[1].request.body)
vevent = calendar.walk('VEVENT')[0]
assert_match_vevent(vevent, event_update)