From 07b5029dea0ec924f31871233a30c61ffc5537e5 Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Tue, 16 May 2023 12:51:57 +0530 Subject: [PATCH] fixup! feat: add create_or_update_tenant_config management command --- .../create_or_update_tenant_config.py | 24 +++++++++---------- .../test_create_or_udpate_tenant_config.py | 24 ++++++++++--------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/eox_tenant/management/commands/create_or_update_tenant_config.py b/eox_tenant/management/commands/create_or_update_tenant_config.py index 2398a071..abe44264 100644 --- a/eox_tenant/management/commands/create_or_update_tenant_config.py +++ b/eox_tenant/management/commands/create_or_update_tenant_config.py @@ -6,7 +6,7 @@ import json import logging -from django.core.management import BaseCommand, CommandError +from django.core.management import BaseCommand from jsonfield.fields import JSONField from eox_tenant.models import Route, TenantConfig @@ -24,7 +24,7 @@ def load_json_from_file(filename): class Command(BaseCommand): """ - Management command to create or update SiteConfiguration. + Management command to create or update TenantConfig. """ help = 'Create or update TenantConfig' @@ -60,9 +60,6 @@ def handle(self, *args, **options): configuration = options.get('config') config_file_data = options.get('config_file_data') tenant_configuration_values = configuration or config_file_data - if tenant_configuration_values is None: - raise CommandError("Either --config or --config_file_data needs to be passed in.") - # pylint: disable=no-member,protected-access external_key_length = TenantConfig._meta.get_field("external_key").max_length if external_key: @@ -84,15 +81,16 @@ def handle(self, *args, **options): LOG.info("Found existing tenant for: '%s'", tenant.external_key) # split out lms, studio, theme, meta from configuration json - for field in TenantConfig._meta.get_fields(): - if isinstance(field, JSONField): - name = field.name - value = tenant_configuration_values.get(name) - if value is not None: - setattr(tenant, name, value) + if tenant_configuration_values: + for field in TenantConfig._meta.get_fields(): + if isinstance(field, JSONField): + name = field.name + value = tenant_configuration_values.get(name) + if value is not None: + setattr(tenant, name, value) - tenant.save() - # next add create routes and link them + tenant.save() + # next add routes and link them for route in routes: route, created = Route.objects.update_or_create( domain=route, diff --git a/eox_tenant/test/test_create_or_udpate_tenant_config.py b/eox_tenant/test/test_create_or_udpate_tenant_config.py index 8be33eaf..f6b891ca 100644 --- a/eox_tenant/test/test_create_or_udpate_tenant_config.py +++ b/eox_tenant/test/test_create_or_udpate_tenant_config.py @@ -83,19 +83,21 @@ def test_command_invalid_config(self): self.assertFalse(Route.objects.filter(domain__contains="test.domain").exists()) def test_command_with_no_config(self): - """Tests that command raises if config is not passed""" - self.assertFalse(TenantConfig.objects.filter(external_key="new.key").exists()) - self.assertFalse(Route.objects.filter(domain__contains="test.domain").exists()) - with self.assertRaises(CommandError): - call_command( - "create_or_update_tenant_config", - "--external-key", - "new.key", - "test.domain", - "studio.test.domain" - ) + """ + Tests that command works even if config is not passed, + i.e. it adds/updates an entry with external_key and links given routes. + """ self.assertFalse(TenantConfig.objects.filter(external_key="new.key").exists()) self.assertFalse(Route.objects.filter(domain__contains="test.domain").exists()) + call_command( + "create_or_update_tenant_config", + "--external-key", + "new.key", + "test.domain", + "studio.test.domain" + ) + self.assertTrue(TenantConfig.objects.filter(external_key="new.key").exists()) + self.assertTrue(Route.objects.filter(domain__contains="test.domain").exists()) def test_command_with_long_external_key(self): """Tests that command runs successfully even if external key is longer than limit."""