From 6ce9b4d962b6e3ff8234ded3d84a66f0433a4943 Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Mon, 19 Sep 2022 15:43:24 +0200
Subject: [PATCH] profile: remove redundant mobile phone field (#69228)

---
 hobo/profile/migrations/0002_add_data.py |  1 -
 tests/test_environment.py                | 18 +++++++-------
 tests/test_settings_loaders.py           | 15 +++++-------
 tests_authentic/test_hobo_deploy.py      | 31 ++++++++----------------
 tests_schemas/example_env.json           | 13 ----------
 5 files changed, 25 insertions(+), 53 deletions(-)

diff --git a/hobo/profile/migrations/0002_add_data.py b/hobo/profile/migrations/0002_add_data.py
index cffc1d52..2a06cfcb 100644
--- a/hobo/profile/migrations/0002_add_data.py
+++ b/hobo/profile/migrations/0002_add_data.py
@@ -18,7 +18,6 @@ def add_initial_data(apps, schema_editor):
         {'label': u'Pays', 'name': 'country', 'disabled': True},
         {'label': u'Date de naissance', 'name': 'birthdate', 'kind': 'birthdate', 'disabled': True},
         {'label': u'Téléphone', 'name': 'phone', 'kind': 'phone_number'},
-        {'label': u'Mobile', 'name': 'mobile', 'kind': 'phone_number'},
     ]
 
     for i, attribute_dict in enumerate(attributes):
diff --git a/tests/test_environment.py b/tests/test_environment.py
index b2d7aee1..05627438 100644
--- a/tests/test_environment.py
+++ b/tests/test_environment.py
@@ -373,12 +373,12 @@ def test_export_import_view(app, admin_user):
     Variable.objects.create(name='foo3', value='bar3').save()
     AttributeDefinition.objects.create(name='prefered_color', label='not empty').save()
     assert Variable.objects.count() == 3
-    assert AttributeDefinition.objects.count() == 12
+    assert AttributeDefinition.objects.count() == 11
     assert Variable.objects.get(name='foo').label == ''
     assert AttributeDefinition.objects.get(name='title').description == ''
     assert AttributeDefinition.objects.get(name='title').order == 1
     assert AttributeDefinition.objects.get(name='last_name').order == 3
-    assert AttributeDefinition.objects.get(name='prefered_color').order == 12
+    assert AttributeDefinition.objects.get(name='prefered_color').order == 11
 
     # import valid content
     resp = app.get('/', status=200)
@@ -386,13 +386,13 @@ def test_export_import_view(app, admin_user):
     resp.form['parameters_json'] = Upload('export.json', export_json.encode('utf-8'), 'application/json')
     resp = resp.form.submit()
     assert Variable.objects.count() == 3
-    assert AttributeDefinition.objects.count() == 12
+    assert AttributeDefinition.objects.count() == 11
     assert Variable.objects.get(name='foo').label == 'bar'
     assert AttributeDefinition.objects.get(name='title').description == 'genre'
     assert AttributeDefinition.objects.get(name='title').order == 1
     assert AttributeDefinition.objects.get(name='last_name').label == 'Nom de naissance'
     assert AttributeDefinition.objects.get(name='last_name').order == 3
-    assert AttributeDefinition.objects.get(name='prefered_color').order == 12
+    assert AttributeDefinition.objects.get(name='prefered_color').order == 11
 
     # import empty json
     resp = app.get('/', status=200)
@@ -400,13 +400,13 @@ def test_export_import_view(app, admin_user):
     resp.form['parameters_json'] = Upload('export.json', b'{}', 'application/json')
     resp = resp.form.submit()
     assert Variable.objects.count() == 3
-    assert AttributeDefinition.objects.count() == 12
+    assert AttributeDefinition.objects.count() == 11
     assert Variable.objects.get(name='foo').label == 'bar'
     assert AttributeDefinition.objects.get(name='title').description == 'genre'
     assert AttributeDefinition.objects.get(name='title').order == 1
     assert AttributeDefinition.objects.get(name='last_name').label == 'Nom de naissance'
     assert AttributeDefinition.objects.get(name='last_name').order == 3
-    assert AttributeDefinition.objects.get(name='prefered_color').order == 12
+    assert AttributeDefinition.objects.get(name='prefered_color').order == 11
 
     # import from scratch
     Variable.objects.all().delete()
@@ -420,7 +420,7 @@ def test_export_import_view(app, admin_user):
     resp.form['parameters_json'] = Upload('export.json', export_json.encode('utf-8'), 'application/json')
     resp = resp.form.submit()
     assert Variable.objects.count() == 2
-    assert AttributeDefinition.objects.count() == 12
+    assert AttributeDefinition.objects.count() == 11
     assert Variable.objects.get(name='foo').label == 'bar'
     assert AttributeDefinition.objects.get(name='title').order == 4
     assert AttributeDefinition.objects.get(name='last_name').order == 2
@@ -432,7 +432,7 @@ def test_export_import_view(app, admin_user):
     resp.form['parameters_json'] = Upload('export.json', b'garbage', 'application/json')
     resp = resp.form.submit()
     assert Variable.objects.count() == 2
-    assert AttributeDefinition.objects.count() == 12
+    assert AttributeDefinition.objects.count() == 11
 
     # import corrupted json
     export['variables'][0]['label'] = 'foofoo'
@@ -446,7 +446,7 @@ def test_export_import_view(app, admin_user):
     with pytest.raises(IntegrityError):
         resp = resp.form.submit()
     assert Variable.objects.count() == 2
-    assert AttributeDefinition.objects.count() == 12
+    assert AttributeDefinition.objects.count() == 11
     assert Variable.objects.get(name='foo').label == 'bar'
 
 
diff --git a/tests/test_settings_loaders.py b/tests/test_settings_loaders.py
index c7ceeafb..232fc5fc 100644
--- a/tests/test_settings_loaders.py
+++ b/tests/test_settings_loaders.py
@@ -31,20 +31,19 @@ def test_authentic_update_settings_from_path(tmpdir):
         u'country',
         u'birthdate',
         u'phone',
-        u'mobile',
     ]
 
-    # swap title and mobile fields
+    # swap title and phone fields
     title = AttributeDefinition.objects.get(name='title')
-    mobile = AttributeDefinition.objects.get(name='mobile')
-    (title.order, mobile.order) = (mobile.order, title.order)
+    phone = AttributeDefinition.objects.get(name='phone')
+    (title.order, phone.order) = (phone.order, title.order)
     title.save()
-    mobile.save()
+    phone.save()
 
     env = get_hobo_json()
     fields = env['profile']['fields']
     assert [x['name'] for x in fields] == [
-        u'mobile',
+        u'phone',
         u'first_name',
         u'last_name',
         u'email',
@@ -53,21 +52,19 @@ def test_authentic_update_settings_from_path(tmpdir):
         u'city',
         u'country',
         u'birthdate',
-        u'phone',
         u'title',
     ]
 
     assert [x['name'] for x in fields if x['disabled']] == ['country', 'birthdate', 'title']
     profile_fields = [x['name'] for x in fields if not x['disabled']]
     assert profile_fields == [
-        u'mobile',
+        u'phone',
         u'first_name',
         u'last_name',
         u'email',
         u'address',
         u'zipcode',
         u'city',
-        u'phone',
     ]
 
     # serialize hobo.json
diff --git a/tests_authentic/test_hobo_deploy.py b/tests_authentic/test_hobo_deploy.py
index 6697507e..41fa7c65 100644
--- a/tests_authentic/test_hobo_deploy.py
+++ b/tests_authentic/test_hobo_deploy.py
@@ -245,17 +245,6 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
                     'asked_on_registration': False,
                     'name': 'fr_phone',
                 },
-                {
-                    'kind': 'string',
-                    'description': '',
-                    'required': False,
-                    'user_visible': True,
-                    'label': 'Mobile',
-                    'disabled': False,
-                    'user_editable': True,
-                    'asked_on_registration': False,
-                    'name': 'mobile',
-                },
                 {
                     'kind': 'string',
                     'description': '',
@@ -404,7 +393,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
         assert user.is_staff is True
         from authentic2.models import Attribute
 
-        assert Attribute.all_objects.count() == 12
+        assert Attribute.all_objects.count() == 11
         for field in env['profile']['fields']:
             if field['name'] != 'email':
                 at = Attribute.all_objects.get(name=field['name'])
@@ -437,10 +426,10 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
         assert policy.authn_request_signed is False
         assert policy.accepted_name_id_format == ['uuid']
         assert policy.default_name_id_format == 'uuid'
-        assert policy.attributes.count() == 16
-        assert policy.attributes.filter(enabled=True).count() == 13
+        assert policy.attributes.count() == 15
+        assert policy.attributes.filter(enabled=True).count() == 12
         assert policy.attributes.filter(enabled=False).count() == 3
-        assert policy.attributes.filter(name_format='basic').count() == 16
+        assert policy.attributes.filter(name_format='basic').count() == 15
         assert (
             policy.attributes.filter(name='is_superuser', attribute_name='django_user_is_superuser').count()
             == 1
@@ -536,18 +525,18 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
 
     # test attribute kind update
     with tenant_context(tenant):
-        assert Attribute.objects.filter(name='mobile', kind='string').count() == 1
-    field = env['profile']['fields'][9]
-    assert field['name'] == 'mobile'
-    field['kind'] = 'phone_number'
+        assert Attribute.objects.filter(name='phone', kind='phone_number').count() == 1
+    field = env['profile']['fields'][7]
+    assert field['name'] == 'phone'
+    field['kind'] = 'string'
     side_effect_iter = iter([meta1, meta2, RequestException(), meta4, meta3])
     with mock.patch('hobo.agent.authentic2.provisionning.notify_agents') as mock_notify, mock.patch(
         'hobo.agent.authentic2.management.commands.hobo_deploy.sleep', wraps=time.sleep
     ) as sleep_mock:
         call_command('hobo_deploy', 'http://sso.example.net', hobo_json(), ignore_timestamp=True)
     with tenant_context(tenant):
-        assert Attribute.objects.filter(name='mobile', kind='string').count() == 0
-        assert Attribute.objects.filter(name='mobile', kind='phone_number').count() == 1
+        assert Attribute.objects.filter(name='phone', kind='phone_number').count() == 0
+        assert Attribute.objects.filter(name='phone', kind='string').count() == 1
 
 
 def test_import_template(db, tenant_base):
diff --git a/tests_schemas/example_env.json b/tests_schemas/example_env.json
index 085676cd..36231dd0 100644
--- a/tests_schemas/example_env.json
+++ b/tests_schemas/example_env.json
@@ -130,19 +130,6 @@
                 "searchable": false,
                 "user_editable": true,
                 "user_visible": true
-            },
-            {
-                "asked_on_registration": false,
-                "description": "",
-                "disabled": false,
-                "kind": "phone_number",
-                "label": "Mobile",
-                "name": "mobile",
-                "required": false,
-                "required_on_login": false,
-                "searchable": false,
-                "user_editable": true,
-                "user_visible": true
             }
         ]
     },
-- 
2.37.2

