From 725467a2fea329c9d46c7d084b7cb2fa954a9d05 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sat, 18 May 2019 08:20:22 +0200
Subject: [PATCH] misc: include URL in sync exceptions errors (#32975)

---
 chrono/agendas/models.py | 5 +++--
 tests/test_agendas.py    | 8 ++++----
 tests/test_manager.py    | 6 +++---
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 3eea87a..b974c63 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -484,9 +484,10 @@ class Desk(models.Model):
             response = requests.get(url, proxies=settings.REQUESTS_PROXIES)
             response.raise_for_status()
         except requests.HTTPError as e:
-            raise ICSError(_('Failed to retrieve remote calendar (HTTP error %s).') % e.response.status_code)
+            raise ICSError(_('Failed to retrieve remote calendar (%s, HTTP error %s).') % (
+                url, e.response.status_code))
         except requests.RequestException as e:
-            raise ICSError(_('Failed to retrieve remote calendar (%s).') % e)
+            raise ICSError(_('Failed to retrieve remote calendar (%s, %s).') % (url, e))
 
         return self.create_timeperiod_exceptions_from_ics(response.text, keep_synced_by_uid=True)
 
diff --git a/tests/test_agendas.py b/tests/test_agendas.py
index d2d053d..c332bda 100644
--- a/tests/test_agendas.py
+++ b/tests/test_agendas.py
@@ -327,7 +327,7 @@ def test_timeperiodexception_creation_from_unreachable_remote_ics(mocked_get):
     mocked_get.side_effect = mocked_requests_connection_error
     with pytest.raises(ICSError) as e:
         exceptions_count = desk.create_timeperiod_exceptions_from_remote_ics('http://example.com/sample.ics')
-    assert str(e.value) == "Failed to retrieve remote calendar (unreachable)."
+    assert str(e.value) == "Failed to retrieve remote calendar (http://example.com/sample.ics, unreachable)."
 
 @mock.patch('chrono.agendas.models.requests.get')
 def test_timeperiodexception_creation_from_forbidden_remote_ics(mocked_get):
@@ -344,13 +344,13 @@ def test_timeperiodexception_creation_from_forbidden_remote_ics(mocked_get):
 
     with pytest.raises(ICSError) as e:
         exceptions_count = desk.create_timeperiod_exceptions_from_remote_ics('http://example.com/sample.ics')
-    assert str(e.value) == "Failed to retrieve remote calendar (HTTP error 403)."
+    assert str(e.value) == "Failed to retrieve remote calendar (http://example.com/sample.ics, HTTP error 403)."
 
 @mock.patch('chrono.agendas.models.requests.get')
 def test_sync_desks_timeperiod_exceptions_from_ics(mocked_get, capsys):
     agenda = Agenda(label=u'Test 11 agenda')
     agenda.save()
-    desk = Desk(label='Test 11 desk', agenda=agenda, timeperiod_exceptions_remote_url='http:example.com/sample.ics')
+    desk = Desk(label='Test 11 desk', agenda=agenda, timeperiod_exceptions_remote_url='http://example.com/sample.ics')
     desk.save()
     mocked_response = mock.Mock()
     mocked_response.status_code = 403
@@ -360,7 +360,7 @@ def test_sync_desks_timeperiod_exceptions_from_ics(mocked_get, capsys):
     mocked_get.side_effect = mocked_requests_http_forbidden_error
     call_command('sync_desks_timeperiod_exceptions')
     out, err = capsys.readouterr()
-    assert err == 'unable to create timeperiod exceptions for "Test 11 desk": Failed to retrieve remote calendar (HTTP error 403).\n'
+    assert err == 'unable to create timeperiod exceptions for "Test 11 desk": Failed to retrieve remote calendar (http://example.com/sample.ics, HTTP error 403).\n'
 
 @mock.patch('chrono.agendas.models.requests.get')
 def test_sync_desks_timeperiod_exceptions_from_changing_ics(mocked_get, caplog):
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 4103153..3ef4bb2 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -1246,7 +1246,7 @@ def test_agenda_import_time_period_exception_from_remote_ics_with_connection_err
         raise requests.exceptions.ConnectionError('unreachable')
     mocked_get.side_effect = mocked_requests_connection_error
     resp = resp.form.submit(status=200)
-    assert 'Failed to retrieve remote calendar (unreachable).' in resp.text
+    assert 'Failed to retrieve remote calendar (http://example.com/foo.ics, unreachable).' in resp.text
 
 @mock.patch('chrono.agendas.models.requests.get')
 def test_agenda_import_time_period_exception_from_forbidden_remote_ics(mocked_get, app, admin_user):
@@ -1272,7 +1272,7 @@ def test_agenda_import_time_period_exception_from_forbidden_remote_ics(mocked_ge
         raise requests.exceptions.HTTPError(response=mocked_response)
     mocked_get.side_effect = mocked_requests_http_forbidden_error
     resp = resp.form.submit(status=200)
-    assert 'Failed to retrieve remote calendar (HTTP error 403).' in resp.text
+    assert 'Failed to retrieve remote calendar (http://example.com/foo.ics, HTTP error 403).' in resp.text
 
 @mock.patch('chrono.agendas.models.requests.get')
 def test_agenda_import_time_period_exception_from_remote_ics_with_ssl_error(mocked_get, app, admin_user):
@@ -1296,7 +1296,7 @@ def test_agenda_import_time_period_exception_from_remote_ics_with_ssl_error(mock
         raise requests.exceptions.SSLError('SSL error')
     mocked_get.side_effect = mocked_requests_http_ssl_error
     resp = resp.form.submit(status=200)
-    assert 'Failed to retrieve remote calendar (SSL error).' in resp.text
+    assert 'Failed to retrieve remote calendar (https://example.com/foo.ics, SSL error).' in resp.text
 
 def test_agenda_day_view(app, admin_user, manager_user, api_user):
     agenda = Agenda.objects.create(label='New Example', kind='meetings')
-- 
2.20.1

