From adceedb8c02fc2a745212d985b2884f2172443d0 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 9 Dec 2022 15:52:52 +0100
Subject: [PATCH] toulouse-maelis: add connector key into referentials (#72244)

---
 .../migrations/0004_auto_20221209_1326.py     | 21 +++++++++++++++++++
 passerelle/contrib/toulouse_maelis/models.py  |  6 +++---
 tests/test_toulouse_maelis.py                 | 17 +++++++++++++++
 3 files changed, 41 insertions(+), 3 deletions(-)
 create mode 100644 passerelle/contrib/toulouse_maelis/migrations/0004_auto_20221209_1326.py

diff --git a/passerelle/contrib/toulouse_maelis/migrations/0004_auto_20221209_1326.py b/passerelle/contrib/toulouse_maelis/migrations/0004_auto_20221209_1326.py
new file mode 100644
index 00000000..16895966
--- /dev/null
+++ b/passerelle/contrib/toulouse_maelis/migrations/0004_auto_20221209_1326.py
@@ -0,0 +1,21 @@
+# Generated by Django 2.2.26 on 2022-12-09 12:26
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('toulouse_maelis', '0003_referential_item_unaccent_text'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='referential',
+            options={'ordering': ('resource', 'referential_name', 'item_text', 'item_id')},
+        ),
+        migrations.AlterUniqueTogether(
+            name='referential',
+            unique_together={('resource', 'referential_name', 'item_id')},
+        ),
+    ]
diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py
index b44e60b9..3018975e 100644
--- a/passerelle/contrib/toulouse_maelis/models.py
+++ b/passerelle/contrib/toulouse_maelis/models.py
@@ -157,17 +157,17 @@ class ToulouseMaelis(BaseResource, HTTPResource):
         if id is not None:
             queryset = self.referential.filter(referential_name=referential_name, item_id=id)
         else:
             queryset = self.referential.filter(referential_name=referential_name).all()
             if q:
                 queryset = queryset.filter(item_unaccent_text__icontains=simplify(q))
 
         if distinct:
-            queryset = queryset.distinct('referential_name', 'item_text')
+            queryset = queryset.distinct('resource', 'referential_name', 'item_text')
 
         if limit:
             try:
                 limit = int(limit)
             except ValueError:
                 pass
             else:
                 queryset = queryset[:limit]
@@ -1401,10 +1401,10 @@ class Referential(models.Model):
     item_data = JSONField('Data')
     created = models.DateTimeField('Created', auto_now_add=True)
     updated = models.DateTimeField('Updated', auto_now=True)
 
     def __repr__(self):
         return '<Referential "%s/%s">' % (self.referential_name, self.item_id)
 
     class Meta:
-        ordering = ('referential_name', 'item_text', 'item_id')
-        unique_together = [['referential_name', 'item_id']]
+        ordering = ('resource', 'referential_name', 'item_text', 'item_id')
+        unique_together = [['resource', 'referential_name', 'item_id']]
diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py
index 773af074..1cb51e9c 100644
--- a/tests/test_toulouse_maelis.py
+++ b/tests/test_toulouse_maelis.py
@@ -241,16 +241,33 @@ def test_update_referential(mocked_get, con):
         mocked_post.return_value = FakedResponse(
             content=get_xml_file('R_read_category_list_2_items.xml'), status_code=200
         )
         con.update_referential('Category')
         assert [
             (x.item_id, x.item_text) for x in Referential.objects.filter(referential_name='Category')
         ] == [('BI', 'BIPARENTALE'), ('ACCEUI', 'Famille acceuillante')]
 
+    # referential are not shared by connectors
+    new_con = setup_access_rights(
+        ToulouseMaelis.objects.get_or_create(
+            slug='test2',
+            zeep_wsse_username='maelis-webservice',
+            zeep_wsse_password='maelis-password',
+        )[0]
+    )
+    assert new_con.referential.filter(referential_name='Category').count() == 0
+    with mock.patch('passerelle.utils.Request.post') as mocked_post:
+        mocked_post.return_value = FakedResponse(
+            content=get_xml_file('R_read_category_list.xml'), status_code=200
+        )
+        new_con.update_referential('Category')
+    assert new_con.referential.filter(referential_name='Category').count() == 3
+    assert con.referential.filter(referential_name='Category').count() == 2
+
 
 def test_cron(db):
     assert Referential.objects.filter(referential_name='Category').count() == 3
     assert sorted(list({x.referential_name for x in Referential.objects.all()})) == [
         'CSP',
         'Category',
         'ChildIndicator',
         'Civility',
-- 
2.30.2

