From 9f88927f08e373105c297ec4ea31d570a6d27d49 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Tue, 12 Nov 2019 20:49:22 +0100
Subject: [PATCH] matomo: manage secondary instances (#37576)

---
 hobo/matomo/utils.py       |  7 +--
 tests/test_matomo_utils.py | 88 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+), 3 deletions(-)

diff --git a/hobo/matomo/utils.py b/hobo/matomo/utils.py
index b5f208f..47cdc9c 100644
--- a/hobo/matomo/utils.py
+++ b/hobo/matomo/utils.py
@@ -89,11 +89,12 @@ def put_tracking_js(tracking_js):
 def get_tenant_name_and_public_urls():
     """get an alias for our matomo's id and urls to monitor"""
     tenant_name = None
-    services = [x for x in Combo.objects.all() if 'portal-user' in x.template_name]
+    services = [x for x in Combo.objects.all()
+                if 'portal-user' in x.template_name and not x.secondary]
     if services != [] and services[0] != '':
         tenant_name = urlparse.urlparse(services[0].base_url).netloc
-    services += [x for x in Wcs.objects.all()]
-    services += [x for x in Fargo.objects.all()]
+    services += [x for x in Wcs.objects.all() if not x.secondary]
+    services += [x for x in Fargo.objects.all() if not x.secondary]
     site_urls = [x.base_url for x in services if x.base_url != '']
     return tenant_name, site_urls
 
diff --git a/tests/test_matomo_utils.py b/tests/test_matomo_utils.py
index b02c002..cc86a12 100644
--- a/tests/test_matomo_utils.py
+++ b/tests/test_matomo_utils.py
@@ -220,6 +220,94 @@ def test_get_tenant_name_and_public_urls():
                          'https://wcs.dev.publik.love/',
                          'https://fargo.dev.publik.love/']
 
+def test_get_tenant_name_and_public_urls_on_primary():
+    Hobo.objects.create(base_url='https://hobo.ville1.love',
+                        slug='hobo-ville1')
+    Hobo.objects.create(base_url='https://hobo.ville2.love',
+                        slug='hobo-ville2')
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         slug='portal')
+    Combo.objects.create(base_url='https://combo.ville1.love',
+                         template_name='...portal-user...',
+                         slug='_hobo-ville1_portal',
+                         secondary=True)
+    Combo.objects.create(base_url='https://combo.ville2.love',
+                         template_name='...portal-user...',
+                         slug='_hobo-ville2_portal',
+                         secondary=True)
+    Combo.objects.create(base_url='https://agent-combo.agglo.love',
+                         template_name='...portal-agent...',
+                         slug='portal-agent')
+    Combo.objects.create(base_url='https://agent-combo.ville1.love',
+                         template_name='...portal-agent...',
+                         slug='_hobo-ville1_portal-agent',
+                         secondary=True)
+    Combo.objects.create(base_url='https://agent-combo.ville2.love',
+                         template_name='...portal-agent...',
+                         slug='_hobo-ville2_portal-agent',
+                         secondary=True)
+    Wcs.objects.create(base_url='https://wcs.agglo.love',
+                       slug='eservices')
+    Wcs.objects.create(base_url='https://wcs.ville1.love',
+                       slug='_hobo-ville1_eservices',
+                       secondary=True)
+    Wcs.objects.create(base_url='https://wcs.ville2.love',
+                       slug='_hobo-ville2_eservices',
+                       secondary=True)
+    Fargo.objects.create(base_url='https://fargo.agglo.love',
+                         slug='porte-doc')
+    Fargo.objects.create(base_url='https://fargo.ville1.love',
+                         slug='_hobo-ville1_porte-doc',
+                         secondary=True)
+    Fargo.objects.create(base_url='https://fargo.ville2.love',
+                         slug='_hobo-ville2_porte-doc',
+                         secondary=True)
+
+    tenant_name, site_urls = get_tenant_name_and_public_urls()
+    assert tenant_name == 'combo.agglo.love'
+    assert site_urls == ['https://combo.agglo.love/',
+                         'https://wcs.agglo.love/',
+                         'https://fargo.agglo.love/']
+
+def test_get_tenant_name_and_public_urls_on_secondary():
+    Hobo.objects.create(base_url='https://hobo.agglo.love',
+                        slug='_interco_hobo')
+    Hobo.objects.create(base_url='https://hobo.ville1.love',
+                        slug='_interco_hobo-ville1')
+    Hobo.objects.create(base_url='https://hobo.ville2.love',
+                        slug='_interco_hobo-ville2')
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         slug='_interco_portal',
+                         secondary=True)
+    Combo.objects.create(base_url='https://combo.ville2.love',
+                         template_name='...portal-user...',
+                         slug='portal')
+    Combo.objects.create(base_url='https://agent-combo.agglo.love',
+                         template_name='...portal-agent...',
+                         slug='_interco_portal-agent',
+                         secondary=True)
+    Combo.objects.create(base_url='https://agent-combo.ville2.love',
+                         template_name='...portal-agent...',
+                         slug='portal-agent')
+    Wcs.objects.create(base_url='https://wcs.agglo.love',
+                       slug='_interco_eservices',
+                       secondary=True)
+    Wcs.objects.create(base_url='https://wcs.ville2.love',
+                       slug='eservices')
+    Fargo.objects.create(base_url='https://fargo.agglo.love',
+                         slug='_interco_porte-doc',
+                         secondary=True)
+    Fargo.objects.create(base_url='https://fargo.ville2.love',
+                         slug='porte-doc')
+
+    tenant_name, site_urls = get_tenant_name_and_public_urls()
+    assert tenant_name == 'combo.ville2.love'
+    assert site_urls == ['https://combo.ville2.love/',
+                         'https://wcs.ville2.love/',
+                         'https://fargo.ville2.love/']
+
 def test_matomo_constructor():
     """build the matomo webservice object"""
     with override_settings(MATOMO_SERVER=CONFIG):
-- 
2.24.0

