diff --git a/registrar/apps/core/management/commands/sync_with_discovery.py b/registrar/apps/core/management/commands/sync_with_discovery.py index f57bfcf6..17621a85 100644 --- a/registrar/apps/core/management/commands/sync_with_discovery.py +++ b/registrar/apps/core/management/commands/sync_with_discovery.py @@ -128,8 +128,13 @@ def sync_programs(self, program_types): if discovery_authoring_org: auth_org = existing_org_dictionary.get(discovery_authoring_org.get('uuid')) if auth_org: - # key fromd disco is not guarenteed to be a valid slugfield - program_key = slugify(discovery_program.get('marketing_slug')) + # key from disco is not guaranteed to be a valid slugfield. + # The current key pattern in disco looks something like /masters/program/name-of-the-program, + # so we should only use the piece of the marketing slug that contains the name of the program. + program_key = slugify( + discovery_program.get('marketing_slug').split('/')[-1], + max_length=100, + ) programs_to_create.append(Program( discovery_uuid=discovery_program.get('uuid'), managing_organization=auth_org, diff --git a/registrar/apps/core/management/commands/test/test_sync_with_discovery.py b/registrar/apps/core/management/commands/test/test_sync_with_discovery.py index 47583d8a..5fc4f587 100644 --- a/registrar/apps/core/management/commands/test/test_sync_with_discovery.py +++ b/registrar/apps/core/management/commands/test/test_sync_with_discovery.py @@ -3,6 +3,7 @@ import ddt from django.core.management import call_command +from django.db.utils import IntegrityError from django.test import TestCase from registrar.apps.core.api_client import DiscoveryServiceClient @@ -301,17 +302,47 @@ def test_sync_programs_create_with_different_slugs(self): def test_sync_programs_create_with_non_slug_marketing_slug(self): """ Discovery keys that are not valid slugs should be slugified """ + long_slug = ( + 'masters/online-masters/masters-in-business/' + 'university-of-open-edx-online-masters-in-' + 'business-analytics-statistics-and-computer-science-and-data-science-and-data-anlytics' + ) discovery_program = self.discovery_program_dict( self.org.discovery_uuid, '77777777-2222-3333-4444-555555555555', - 'marketing/path/non-slug', + long_slug, ) programs_to_sync = [ discovery_program, ] self.assert_programs(programs_to_sync, 26) created_program = Program.objects.get(discovery_uuid=discovery_program['uuid']) - self.assertEqual(created_program.key, 'marketing-path-non-slug') + self.assertEqual( + created_program.key, + 'university-of-open-edx-online-masters-in-business-analytics-statistics-and-computer-science-and-data' + ) + + def test_sync_programs_multiple_non_slug_marketing_slug(self): + """ + Discovery keys that are not valid slugs should be slugified, and job should fail if keys are not unique + """ + discovery_program = self.discovery_program_dict( + self.org.discovery_uuid, + '77777777-2222-3333-4444-555555555555', + 'marketing/online-masters/this-is-the-program-name', + ) + another_discovery_program = self.discovery_program_dict( + self.org.discovery_uuid, + '77777777-2222-3333-4444-666666666666', + 'marketing/masters/this-is-the-program-name', + ) + programs_to_sync = [ + discovery_program, + another_discovery_program, + ] + + with self.assertRaises(IntegrityError): + self.assert_programs(programs_to_sync, 26) def test_sync_programs_with_different_slugs(self): updated_discovery_program = self.discovery_program_dict(