diff --git a/registrar/apps/core/management/commands/manage_programs.py b/registrar/apps/core/management/commands/manage_programs.py index 10e50cf9..44cf7d25 100644 --- a/registrar/apps/core/management/commands/manage_programs.py +++ b/registrar/apps/core/management/commands/manage_programs.py @@ -3,6 +3,7 @@ from django.core.management.base import BaseCommand, CommandError from django.db import transaction +from slugify import slugify from registrar.apps.core.discovery_cache import ProgramDetails from registrar.apps.core.models import Organization, Program @@ -89,7 +90,7 @@ def create_or_modify_program(self, org, program_details, program_uuid, program_k discovery_uuid=program_uuid, defaults={ 'managing_organization': org, - 'key': program_key or program_details.get('marketing_slug'), + 'key': program_key or slugify(program_details.get('marketing_slug')), }, ) if (not created) and program_key and (program.key != program_key): diff --git a/registrar/apps/core/management/commands/sync_with_discovery.py b/registrar/apps/core/management/commands/sync_with_discovery.py index d399def0..f57bfcf6 100644 --- a/registrar/apps/core/management/commands/sync_with_discovery.py +++ b/registrar/apps/core/management/commands/sync_with_discovery.py @@ -3,6 +3,7 @@ from django.core.management.base import BaseCommand from django.db import transaction +from slugify import slugify from registrar.apps.core.api_client import DiscoveryServiceClient from registrar.apps.core.models import ( @@ -127,14 +128,16 @@ 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')) programs_to_create.append(Program( discovery_uuid=discovery_program.get('uuid'), managing_organization=auth_org, - key=discovery_program.get('marketing_slug'), + key=program_key, )) logger.info( 'Creating %s with uuid %s', - discovery_program.get('marketing_slug'), + program_key, discovery_authoring_org.get('uuid') ) diff --git a/registrar/apps/core/management/commands/test/test_manage_programs.py b/registrar/apps/core/management/commands/test/test_manage_programs.py index 17b781c4..16ef22f3 100644 --- a/registrar/apps/core/management/commands/test/test_manage_programs.py +++ b/registrar/apps/core/management/commands/test/test_manage_programs.py @@ -86,6 +86,23 @@ def test_create_program(self): ) self.assert_program(self.arabic_uuid, 'masters-in-arabic', self.org) + def test_create_program_discovery_nonslug_key(self): + """ + Discovery program with a key that is not a valid SlugField + should be slugified on create + """ + self.mock_get_discovery_program.return_value = self.discovery_dict( + self.org.key, + self.arabic_uuid, + 'nonslug/marketing/key-format', + ) + self.assert_program_nonexistant(self.arabic_uuid) + call_command( + self.command, + self.arabic_uuid, + ) + self.assert_program(self.arabic_uuid, 'nonslug-marketing-key-format', self.org) + def test_create_program_no_key(self): self.mock_get_discovery_program.return_value = self.arabic_discovery_program self.assert_program_nonexistant(self.arabic_uuid) 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 7568b5ab..47583d8a 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 @@ -299,6 +299,20 @@ def test_sync_programs_create_with_different_slugs(self): self.assert_programs(programs_to_sync, 42) + def test_sync_programs_create_with_non_slug_marketing_slug(self): + """ Discovery keys that are not valid slugs should be slugified """ + discovery_program = self.discovery_program_dict( + self.org.discovery_uuid, + '77777777-2222-3333-4444-555555555555', + 'marketing/path/non-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') + def test_sync_programs_with_different_slugs(self): updated_discovery_program = self.discovery_program_dict( self.german_program.managing_organization.discovery_uuid,