From ac6f3ec82a814bd5da3cc8d76203fc2024b345f2 Mon Sep 17 00:00:00 2001 From: Alexander J Sheehan Date: Tue, 26 Sep 2023 16:52:58 +0000 Subject: [PATCH] chore: adding display name to the sso orchestrator table --- CHANGELOG.rst | 4 ++ enterprise/__init__.py | 2 +- .../migrations/0187_auto_20230926_1627.py | 23 +++++++++++ enterprise/models.py | 9 ++++ enterprise/signals.py | 13 ++++++ tests/test_models.py | 41 +++++++++++++++++++ 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 enterprise/migrations/0187_auto_20230926_1627.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6d8cd94043..7d683b5db1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,10 @@ Change Log Unreleased ---------- +[4.3.3] +------- +chore: adding display name to the sso orchestrator table + [4.3.2] ------- fix: enterprise sso orchestrator api cleanup diff --git a/enterprise/__init__.py b/enterprise/__init__.py index fdf652a73f..d28160d589 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,4 +2,4 @@ Your project description goes here. """ -__version__ = "4.3.2" +__version__ = "4.3.3" diff --git a/enterprise/migrations/0187_auto_20230926_1627.py b/enterprise/migrations/0187_auto_20230926_1627.py new file mode 100644 index 0000000000..5a43d18b3d --- /dev/null +++ b/enterprise/migrations/0187_auto_20230926_1627.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.20 on 2023-09-26 16:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('enterprise', '0186_auto_20230921_1828'), + ] + + operations = [ + migrations.AddField( + model_name='enterprisecustomerssoconfiguration', + name='display_name', + field=models.CharField(blank=True, help_text='The display name of the SSO configuration.', max_length=255, null=True), + ), + migrations.AddField( + model_name='historicalenterprisecustomerssoconfiguration', + name='display_name', + field=models.CharField(blank=True, help_text='The display name of the SSO configuration.', max_length=255, null=True), + ), + ] diff --git a/enterprise/models.py b/enterprise/models.py index 6df6ebe7a1..fe204313f4 100644 --- a/enterprise/models.py +++ b/enterprise/models.py @@ -3765,6 +3765,15 @@ class Meta: # ---------------------------- base configurations ---------------------------- # + display_name = models.CharField( + blank=True, + null=True, + max_length=255, + help_text=_( + "The display name of the SSO configuration." + ), + ) + uuid = models.UUIDField(primary_key=True, default=uuid4, editable=False) enterprise_customer = models.ForeignKey( diff --git a/enterprise/signals.py b/enterprise/signals.py index f3e80ca8c7..87af0ac401 100644 --- a/enterprise/signals.py +++ b/enterprise/signals.py @@ -394,6 +394,19 @@ def create_enterprise_enrollment_receiver(sender, instance, **kwargs): # pyl ) +@receiver(pre_save, sender=models.EnterpriseCustomerSsoConfiguration) +def generate_default_orchestration_record_display_name(sender, instance, **kwargs): # pylint: disable=unused-argument + """ + Ensure that the display_name field is populated with a default value if it is not provided while creating. + """ + if not models.EnterpriseCustomerSsoConfiguration.objects.filter(pk=instance.pk).exists(): + if instance.display_name is None: + num_records_for_customer = models.EnterpriseCustomerSsoConfiguration.objects.filter( + enterprise_customer=instance.enterprise_customer, + ).count() + instance.display_name = f'SSO-config-{instance.identity_provider}-{num_records_for_customer + 1}' + + # Don't connect this receiver if we dont have access to CourseEnrollment model if CourseEnrollment is not None: post_save.connect(create_enterprise_enrollment_receiver, sender=CourseEnrollment) diff --git a/tests/test_models.py b/tests/test_models.py index e5f7000962..674668c24e 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -2710,3 +2710,44 @@ def test_submitting_already_submitted_sso_config(self): ) with raises(ValidationError): sso_configuration.submit_for_configuration() + + def test_sso_config_default_display_name(self): + """ + Test the default naming functionality of the SSO orchestration config table + """ + customer = factories.EnterpriseCustomerFactory() + # Confirm display name is generated correctly on creation if absent from the kwargs + sso_configuration, _ = EnterpriseCustomerSsoConfiguration.objects.get_or_create( + enterprise_customer=customer, + identity_provider='foobar', + ) + assert sso_configuration.display_name == 'SSO-config-foobar-1' + + # Confirm display name is not generated or effected by updating a record + sso_configuration.display_name = None + sso_configuration.save() + sso_configuration.metadata_url = 'ayylmao' + sso_configuration.save() + assert sso_configuration.display_name is None + + # Confirm the display name is kept if specified during creation + second_sso_configuration, _ = EnterpriseCustomerSsoConfiguration.objects.get_or_create( + enterprise_customer=customer, + identity_provider='SAP', + display_name='im-a-display-name', + ) + assert second_sso_configuration.display_name == 'im-a-display-name' + + # Confirm the display name is generated based on how many records exist for the customer + third_sso_configuration, _ = EnterpriseCustomerSsoConfiguration.objects.get_or_create( + enterprise_customer=customer, + identity_provider='ayylmao', + ) + assert third_sso_configuration.display_name == 'SSO-config-ayylmao-3' + + # Confirm the display name default value is not effected by other customers' records + different_customer_sso_configuration, _ = EnterpriseCustomerSsoConfiguration.objects.get_or_create( + enterprise_customer=factories.EnterpriseCustomerFactory(), + identity_provider='SAP', + ) + assert different_customer_sso_configuration.display_name == 'SSO-config-SAP-1'