From b0190d3d6747fbbf672d80ebc2c7c757d182e01b Mon Sep 17 00:00:00 2001
From: Thomas NOEL <tnoel@entrouvert.com>
Date: Mon, 27 Jan 2020 13:10:39 +0100
Subject: [PATCH] solis: allow dateNaissance in rsa_token payload (#39285)

---
 .../solis/migrations/0007_solisrsalink_dob.py | 20 +++++++++++++++
 passerelle/apps/solis/models.py               | 25 +++++++++++--------
 tests/test_solis.py                           | 17 +++++++++----
 3 files changed, 46 insertions(+), 16 deletions(-)
 create mode 100644 passerelle/apps/solis/migrations/0007_solisrsalink_dob.py

diff --git a/passerelle/apps/solis/migrations/0007_solisrsalink_dob.py b/passerelle/apps/solis/migrations/0007_solisrsalink_dob.py
new file mode 100644
index 00000000..5e248266
--- /dev/null
+++ b/passerelle/apps/solis/migrations/0007_solisrsalink_dob.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.18 on 2020-01-27 12:06
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('solis', '0006_solisrsalink'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='solisrsalink',
+            name='dob',
+            field=models.CharField(blank=True, max_length=64, null=True),
+        ),
+    ]
diff --git a/passerelle/apps/solis/models.py b/passerelle/apps/solis/models.py
index 8f47b309..ab6353d2 100644
--- a/passerelle/apps/solis/models.py
+++ b/passerelle/apps/solis/models.py
@@ -459,11 +459,11 @@ class Solis(BaseResource):
     # RSA endpoints
     #
 
-    def rsa_token(self, user_id, code):
-        response = self.request('referentiels/grsa/token', data={
-            'indexIndividu': user_id,
-            'codeConfidentiel': code,
-        })
+    def rsa_token(self, user_id, code, dob=None):
+        data = {'indexIndividu': user_id, 'codeConfidentiel': code}
+        if dob:
+            data['dateNaissance'] = dob
+        response = self.request('referentiels/grsa/token', data=data)
         return response.get('token')
 
     def rsa_fill_with_link_content(self, link):
@@ -491,7 +491,7 @@ class Solis(BaseResource):
             }
         link['content'] = value
 
-    def rsa_get_information(self, information, user_id=None, code=None, token=None,
+    def rsa_get_information(self, information, user_id=None, code=None, dob=None, token=None,
                             index='search', links=None):
         # simulate "individu" referential: get user details from civi/individu/user_id
         if information == 'individu':
@@ -504,7 +504,7 @@ class Solis(BaseResource):
                                data={'json_content': content})
             return content
         if token is None:
-            token = self.rsa_token(user_id, code)
+            token = self.rsa_token(user_id, code, dob)
         endpoint = 'referentiels/grsa/' + information + '/' + index + '/'
         args = [('token', token)]
         if index.startswith('search'):  # it can be "search/next" in rdvs referential
@@ -530,7 +530,7 @@ class Solis(BaseResource):
 
     @endpoint(name='rsa-link', methods=['post'], perm='can_access',
               description=_('Create link between name_id and '
-                            'Solis RSA. Payload: name_id, user_id, code'))
+                            'Solis RSA. Payload: name_id, user_id, code, dob (optionnal)'))
     def rsa_link(self, request):
         try:
             data = json.loads(request.body)
@@ -543,12 +543,14 @@ class Solis(BaseResource):
         if not data.get('user_id') or not data.get('code'):
             raise APIError('missing user_id/code credentials')
         name_id, user_id, code = data['name_id'], data['user_id'], data['code']
-        self.rsa_token(user_id, code)  # invalid credentials raise APIError here
-        information = self.rsa_get_information('individu', user_id, code)
+        dob = data.get('dob')
+        self.rsa_token(user_id, code, dob)  # invalid credentials raise APIError here
+        information = self.rsa_get_information('individu', user_id, code, dob)
         text = get_template(self.text_template_name_rsa).render(information).strip()
         link, created = SolisRSALink.objects.update_or_create(resource=self, name_id=name_id,
                                                               user_id=user_id,
                                                               defaults={'code': code,
+                                                                        'dob': dob,
                                                                         'text': text})
         return {'data': {'user_id': user_id,
                          'text': text,
@@ -619,7 +621,7 @@ class Solis(BaseResource):
         except SolisRSALink.DoesNotExist:
             raise APIError('unknown link')
         response = self.rsa_get_information(information=information,
-                                            user_id=user_id, code=link.code,
+                                            user_id=user_id, code=link.code, dob=link.dob,
                                             index=index, links=links)
         if information == 'individu':
             text = get_template(self.text_template_name_rsa).render(response).strip()
@@ -651,4 +653,5 @@ class SolisRSALink(models.Model):
     name_id = models.CharField(blank=False, max_length=256)
     user_id = models.CharField(blank=False, max_length=64)
     code = models.CharField(blank=False, max_length=64)
+    dob = models.CharField(blank=True, max_length=64, null=True)
     text = models.CharField(blank=False, max_length=256)
diff --git a/tests/test_solis.py b/tests/test_solis.py
index a64b985e..42a9a909 100644
--- a/tests/test_solis.py
+++ b/tests/test_solis.py
@@ -761,10 +761,11 @@ def test_solis_rsa_link_infos_unlink(app, solis):
             assert SolisRSALink.objects.first().user_id == '4273'
             assert SolisRSALink.objects.first().code == 'foo'
             assert SolisRSALink.objects.first().text == 'MME Prenom NOM'
+            assert SolisRSALink.objects.first().dob is None
 
-            # change code
+            # change code, add dob
             resp = app.post_json(endpoint,
-                                 params={'name_id': NAMEID, 'user_id': '4273', 'code': 'bar'},
+                                 params={'name_id': NAMEID, 'user_id': '4273', 'code': 'bar', 'dob': '01/01/1950'},
                                  status=200)
             assert requests_post.call_count == 3
             assert requests_get.call_count == 2
@@ -777,13 +778,19 @@ def test_solis_rsa_link_infos_unlink(app, solis):
             assert SolisRSALink.objects.first().user_id == '4273'
             assert SolisRSALink.objects.first().code == 'bar'
             assert SolisRSALink.objects.first().text == 'MME Prenom NOM'
+            assert SolisRSALink.objects.first().dob == '01/01/1950'
 
             # second link
+            requests_post.reset_mock()
+            requests_get.reset_mock()
             resp = app.post_json(endpoint,
-                                 params={'name_id': NAMEID, 'user_id': '4242', 'code': 'bar'},
+                                 params={'name_id': NAMEID, 'user_id': '4242', 'code': 'bar', 'dob': '10/10/1960'},
                                  status=200)
-            assert requests_post.call_count == 4
-            assert requests_get.call_count == 3
+            assert requests_post.call_count == 1
+            assert requests_get.call_count == 1
+            assert requests_post.call_args[1]['json'] == {'codeConfidentiel': 'bar',
+                                                          'dateNaissance': '10/10/1960',
+                                                          'indexIndividu': '4242'}
             assert resp.json['err'] == 0
             assert resp.json['data']['user_id'] == '4242'
             assert resp.json['data']['created']
-- 
2.24.1

