From 3cd699ac70e5d4f2958594038e6cdf3042621a5a Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Tue, 12 Nov 2019 21:10:16 +0100
Subject: [PATCH] matomo: add services urls on re-configuration (#37614)

---
 hobo/matomo/utils.py       | 17 +++++++++--
 tests/test_matomo_utils.py | 60 ++++++++++++++++++++++++++++++++------
 tests/test_matomo_views.py | 10 +++++--
 3 files changed, 72 insertions(+), 15 deletions(-)

diff --git a/hobo/matomo/utils.py b/hobo/matomo/utils.py
index b5f208f..306333e 100644
--- a/hobo/matomo/utils.py
+++ b/hobo/matomo/utils.py
@@ -177,9 +177,19 @@ class MatomoWS(object):
             raise MatomoException('get_site_id_from_site_url fails')
         return tag.text
 
-    def add_site(self, site_name, site_urls):
+    def add_site(self, site_name):
         data = {'method': 'SitesManager.addSite',
                 'siteName': site_name}
+        tree = self.call(data)
+        try:
+            tag = tree.xpath('/result')[0]
+        except IndexError:
+            raise MatomoException('add_site fails')
+        return tag.text
+
+    def add_site_alias_urls(self, id_site, site_urls):
+        data = {'method': 'SitesManager.addSiteAliasUrls',
+                'idSite': id_site}
         cpt = 0
         for url in site_urls:
             key = 'urls[%i]' % cpt
@@ -189,7 +199,7 @@ class MatomoWS(object):
         try:
             tag = tree.xpath('/result')[0]
         except IndexError:
-            raise MatomoException('add_site fails')
+            raise MatomoException('add_site_alias_urls fails')
         return tag.text
 
     def add_user(self, user_login, password, initial_id_site):
@@ -246,9 +256,10 @@ def upgrade_site(matomo, tenant_name, site_urls):
         id_site = matomo.get_site_id_from_site_url(tenant_name)
     except MatomoError as exc:
         if str(exc) == 'url not found':
-            id_site = matomo.add_site(tenant_name, site_urls)
+            id_site = matomo.add_site(tenant_name)
         else:
             raise exc
+    matomo.add_site_alias_urls(id_site, site_urls)
     return id_site
 
 def upgrade_user(matomo, user_login, id_site):
diff --git a/tests/test_matomo_utils.py b/tests/test_matomo_utils.py
index b02c002..49bfeae 100644
--- a/tests/test_matomo_utils.py
+++ b/tests/test_matomo_utils.py
@@ -87,6 +87,16 @@ ADD_SITE_BAD_RESPONSE = """<?xml version="1.0" encoding="utf-8" ?>
 <not_result>no result tag</not_result>
 """
 
+ADD_SITE_ALIAS_URLS_SUCCESS = """<?xml version="1.0" encoding="utf-8" ?>
+<result>1</result>
+"""
+
+ADD_SITE_ALIAS_URLS_ERROR = """<?xml version="1.0" encoding="utf-8" ?>
+<result>
+        <error message="Merci de specifier une valeur pour idSite" />
+</result>
+"""
+
 USER_ALREADY_THERE = """<?xml version="1.0" encoding="utf-8" ?>
 <result>
     <error message="Username 'hobo.dev.publik.love' already exists." />
@@ -352,15 +362,13 @@ def test_get_site_from_site_url(mocked_post):
 def test_add_site(mocked_post):
     """webservice to add a new site"""
     mocked_post.return_value.status_code = 200
-    urls = ['https://combo.dev.publik.love',
-            'https://wcs.dev.publik.love']
     with override_settings(MATOMO_SERVER=CONFIG):
         matomo = MatomoWS()
 
         # success
         content = ADD_SITE_SUCCESS
         mocked_post.return_value.content = content
-        site_id = matomo.add_site("hobo.dev.publik.love", urls)
+        site_id = matomo.add_site("hobo.dev.publik.love")
         assert site_id == '42'
 
         # error
@@ -368,13 +376,41 @@ def test_add_site(mocked_post):
         mocked_post.return_value.content = content
         with pytest.raises(MatomoError,
                            match="Please specify a value for 'siteName'."):
-            site_id = matomo.add_site("hobo.dev.publik.love", urls)
+            site_id = matomo.add_site("hobo.dev.publik.love")
 
         # strange message
         content = ADD_SITE_BAD_RESPONSE
         mocked_post.return_value.content = content
         with pytest.raises(MatomoException, match='add_site fails'):
-            site_id = matomo.add_site("hobo.dev.publik.love", urls)
+            site_id = matomo.add_site("hobo.dev.publik.love")
+
+@mock.patch('requests.post')
+def test_add_site_alias_url(mocked_post):
+    """webservice to add a new site"""
+    mocked_post.return_value.status_code = 200
+    urls = ['https://combo.dev.publik.love',
+            'https://wcs.dev.publik.love']
+    with override_settings(MATOMO_SERVER=CONFIG):
+        matomo = MatomoWS()
+
+        # success
+        content = ADD_SITE_ALIAS_URLS_SUCCESS
+        mocked_post.return_value.content = content
+        site_id = matomo.add_site_alias_urls('42', urls)
+        assert site_id == '1'
+
+        # error
+        content = ADD_SITE_ALIAS_URLS_ERROR
+        mocked_post.return_value.content = content
+        with pytest.raises(MatomoError,
+                           match="Merci de specifier une valeur pour idSite"):
+            site_id = matomo.add_site_alias_urls(None, urls)
+
+        # strange message
+        content = ADD_SITE_BAD_RESPONSE
+        mocked_post.return_value.content = content
+        with pytest.raises(MatomoException, match='add_site_alias_urls fails'):
+            site_id = matomo.add_site_alias_urls('42', urls)
 
 @mock.patch('requests.post')
 def test_add_user(mocked_post):
@@ -525,17 +561,23 @@ def test_upgrade_site(mocked_post):
         matomo = MatomoWS()
 
         # site not already here
-        contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS]
+        contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS]
         mocked_post.side_effect = requests_post_mocked_replies(contents)
         site_id = upgrade_site(matomo, "hobo.dev.publik.love", urls)
         assert site_id == '42'
 
         # site already here
-        contents = [GET_SITE_42_FROM_URL]
+        contents = [GET_SITE_42_FROM_URL, ADD_SITE_ALIAS_URLS_SUCCESS]
         mocked_post.side_effect = requests_post_mocked_replies(contents)
         site_id = upgrade_site(matomo, "hobo.dev.publik.love", urls)
         assert site_id == '42'
 
+        # error while updating urls
+        contents = [GET_SITE_42_FROM_URL, ADD_SITE_ALIAS_URLS_ERROR]
+        mocked_post.side_effect = requests_post_mocked_replies(contents)
+        with pytest.raises(MatomoException):
+            upgrade_site(matomo, "hobo.dev.publik.love", urls)
+
         # error while adding new site
         contents = [GET_NO_SITE_FROM_URL, MATOMO_ERROR]
         mocked_post.side_effect = requests_post_mocked_replies(contents)
@@ -663,7 +705,7 @@ def test_auto_configure_matomo(mocked_post):
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
 
     with override_settings(MATOMO_SERVER=CONFIG):
-        contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
+        contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
                     DEL_UNKNOWN_USER, MATOMO_SUCCESS,
                     JAVASCRIPT_TAG, PING_SUCCESS]
         mocked_post.side_effect = requests_post_mocked_replies(contents)
@@ -696,7 +738,7 @@ def test_auto_configure_matomo_error(mocked_post):
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
 
     with override_settings(MATOMO_SERVER=CONFIG):
-        contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
+        contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
                     DEL_UNKNOWN_USER, MATOMO_SUCCESS,
                     JAVASCRIPT_TAG_BAD_RESPONSE]
         mocked_post.side_effect = requests_post_mocked_replies(contents)
diff --git a/tests/test_matomo_views.py b/tests/test_matomo_views.py
index 04fd6f2..12b5686 100644
--- a/tests/test_matomo_views.py
+++ b/tests/test_matomo_views.py
@@ -28,6 +28,10 @@ ADD_SITE_SUCCESS = """<?xml version="1.0" encoding="utf-8" ?>
 <result>42</result>
 """
 
+ADD_SITE_ALIAS_URLS_SUCCESS = """<?xml version="1.0" encoding="utf-8" ?>
+<result>1</result>
+"""
+
 DEL_UNKNOWN_USER = """<?xml version="1.0" encoding="utf-8" ?>
 <result>
         <error message="User 'hobo.dev.publik.love' doesn't exist." />
@@ -161,7 +165,7 @@ def test_enable_auto(mocked_post, admin_user):
     Wcs.objects.create(base_url='https://wcs.dev.publik.love')
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
 
-    contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
+    contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
                 DEL_UNKNOWN_USER, MATOMO_SUCCESS,
                 JAVASCRIPT_TAG, PING_SUCCESS]
 
@@ -185,7 +189,7 @@ def test_enable_auto_warning(mocked_post, admin_user):
     Wcs.objects.create(base_url='https://wcs.dev.publik.love')
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
 
-    contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
+    contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
                 DEL_UNKNOWN_USER, MATOMO_SUCCESS,
                 JAVASCRIPT_TAG, PING_ERROR]
 
@@ -212,7 +216,7 @@ def test_enable_auto_error(mocked_post, admin_user):
     Wcs.objects.create(base_url='https://wcs.dev.publik.love')
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
 
-    contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
+    contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS, ADD_SITE_ALIAS_URLS_SUCCESS,
                 DEL_UNKNOWN_USER, MATOMO_SUCCESS,
                 JAVASCRIPT_TAG_BAD_RESPONSE]
 
-- 
2.24.0

