Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

feat: handle nonslug program keys from discovery #596

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion registrar/apps/core/management/commands/manage_programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# key fromd disco is not guarenteed to be a valid slugfield
# key from disco is not guaranteed 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')
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading