diff --git a/openedx/core/djangoapps/theming/management/commands/create_sites_and_configurations.py b/openedx/core/djangoapps/theming/management/commands/create_sites_and_configurations.py index 7c8dacb6ae015b53e688688ea9223f93ad3f5609..0189f68edc99496d42c669096d9bbfec25bd3743 100644 --- a/openedx/core/djangoapps/theming/management/commands/create_sites_and_configurations.py +++ b/openedx/core/djangoapps/theming/management/commands/create_sites_and_configurations.py @@ -32,7 +32,12 @@ class Command(BaseCommand): dns_name = None theme_path = None ecommerce_user = None + ecommerce_base_url_fmt = None + ecommerce_oidc_url = None discovery_user = None + discovery_base_url_fmt = None + discovery_oidc_url = None + configuration_filename = None def add_arguments(self, parser): """ @@ -52,6 +57,12 @@ class Command(BaseCommand): required=True ) + parser.add_argument( + "--devstack", + action='store_true', + help="Use devstack config, otherwise sandbox config is assumed", + ) + def _create_oauth2_client(self, url, site_name, is_discovery=True): """ Creates the oauth2 client and add it in trusted clients. @@ -119,25 +130,17 @@ class Command(BaseCommand): def _update_default_clients(self): """ - These two clients is being created by default without service + These two clients are being created by default without service users so we have to associate the service users to them. """ - ecommerce_queryset = Client.objects.filter( - redirect_uri="https://ecommerce-{dns_name}.sandbox.edx.org/complete/edx-oidc/".format( - dns_name=self.dns_name - ) - ) + ecommerce_queryset = Client.objects.filter(redirect_uri=self.ecommerce_oidc_url) if ecommerce_queryset: ecommerce_client = ecommerce_queryset[0] ecommerce_client.user = self.ecommerce_user ecommerce_client.save() - discovery_queryset = Client.objects.filter( - redirect_uri="https://discovery-{dns_name}.sandbox.edx.org/complete/edx-oidc/".format( - dns_name=self.dns_name - ) - ) + discovery_queryset = Client.objects.filter(redirect_uri=self.discovery_oidc_url) if discovery_queryset: discovery_client = discovery_queryset[0] discovery_client.user = self.discovery_user @@ -155,7 +158,7 @@ class Command(BaseCommand): } """ site_data = {} - for config_file in self.find('sandbox_configuration.json', self.theme_path): + for config_file in self.find(self.configuration_filename, self.theme_path): LOG.info("Reading file from {file}".format(file=config_file)) configuration_data = json.loads( json.dumps( @@ -192,10 +195,23 @@ class Command(BaseCommand): return service_user def handle(self, *args, **options): - - self.theme_path = options['theme_path'] self.dns_name = options['dns_name'] + self.theme_path = options['theme_path'] + + if options['devstack']: + configuration_prefix = "devstack" + self.discovery_oidc_url = "http://discovery-{}.e2e.devstack:18381/complete/edx-oidc/".format(self.dns_name) + self.discovery_base_url_fmt = "http://discovery-{site_domain}:18381/" + self.ecommerce_oidc_url = "http://ecommerce-{}.e2e.devstack:18130/complete/edx-oidc/".format(self.dns_name) + self.ecommerce_base_url_fmt = "http://ecommerce-{site_domain}:18130/" + else: + configuration_prefix = "sandbox" + self.discovery_oidc_url = "https://discovery-{}.sandbox.edx.org/complete/edx-oidc/".format(self.dns_name) + self.discovery_base_url_fmt = "https://discovery-{site_domain}/" + self.ecommerce_oidc_url = "https://ecommerce-{}.sandbox.edx.org/complete/edx-oidc/".format(self.dns_name) + self.ecommerce_base_url_fmt = "https://ecommerce-{site_domain}/" + self.configuration_filename = '{}_configuration.json'.format(configuration_prefix) self.discovery_user = self.get_or_create_service_user("lms_catalog_service_user") self.ecommerce_user = self.get_or_create_service_user("ecommerce_worker") @@ -206,8 +222,8 @@ class Command(BaseCommand): for site_name, site_data in all_sites.items(): site_domain = site_data['site_domain'] - discovery_url = "https://discovery-{site_domain}/".format(site_domain=site_domain) - ecommerce_url = "https://ecommerce-{site_domain}/".format(site_domain=site_domain) + discovery_url = self.discovery_base_url_fmt.format(site_domain=site_domain) + ecommerce_url = self.ecommerce_base_url_fmt.format(site_domain=site_domain) LOG.info("Creating '{site_name}' Site".format(site_name=site_name)) self._create_sites(site_domain, site_data['theme_dir_name'], site_data['configuration']) diff --git a/openedx/core/djangoapps/theming/management/commands/tests/test_create_sites_and_configurations.py b/openedx/core/djangoapps/theming/management/commands/tests/test_create_sites_and_configurations.py index 0989bffd4016d4242eb141e10d479d8dbbfc0ba1..1fef82e6d70f0a4b7c784917f7c55e1c4913ecfe 100644 --- a/openedx/core/djangoapps/theming/management/commands/tests/test_create_sites_and_configurations.py +++ b/openedx/core/djangoapps/theming/management/commands/tests/test_create_sites_and_configurations.py @@ -18,23 +18,34 @@ SITES = ["site_a", "site_b"] MANAGEMENT_COMMAND_PATH = "openedx.core.djangoapps.theming.management.commands.create_sites_and_configurations." -def _generate_site_config(dns_name, site_domain): +def _generate_site_config(dns_name, site_domain, devstack=False): """ Generate the site configuration for a given site """ + if devstack: + lms_url_fmt = "{domain}-{dns_name}.e2e.devstack" + else: + lms_url_fmt = "{domain}-{dns_name}.sandbox.edx.org" + return { - "lms_url": "{domain}-{dns_name}.sandbox.edx.org".format(domain=site_domain, dns_name=dns_name), + "lms_url": lms_url_fmt.format(domain=site_domain, dns_name=dns_name), "platform_name": "{domain}-{dns_name}".format(domain=site_domain, dns_name=dns_name) } -def _get_sites(dns_name): +def _get_sites(dns_name, devstack=False): """ Creates the mocked data for management command """ sites = {} + + if devstack: + site_domain_fmt = "{site}-{dns_name}.e2e.devstack" + else: + site_domain_fmt = "{site}-{dns_name}.sandbox.edx.org" + for site in SITES: sites.update({ site: { "theme_dir_name": "{}_dir_name".format(site), "configuration": _generate_site_config(dns_name, site), - "site_domain": "{site}-{dns_name}.sandbox.edx.org".format(site=site, dns_name=dns_name) + "site_domain": site_domain_fmt.format(site=site, dns_name=dns_name) } }) return sites @@ -80,7 +91,7 @@ class TestCreateSiteAndConfiguration(TestCase): self.assertEqual(len(user_profile), 1) return service_user - def _assert_ecommerce_clients_are_valid(self): + def _assert_ecommerce_clients_are_valid(self, devstack=False): """ Checks that all ecommerce clients are valid """ @@ -89,10 +100,15 @@ class TestCreateSiteAndConfiguration(TestCase): clients = Client.objects.filter(user=service_user) self.assertEqual(len(clients), len(SITES)) + if devstack: + ecommerce_url_fmt = u"http://ecommerce-{site_name}-{dns_name}.e2e.devstack:18130/" + else: + ecommerce_url_fmt = u"https://ecommerce-{site_name}-{dns_name}.sandbox.edx.org/" + for client in clients: self.assertEqual(client.user.username, service_user[0].username) site_name = client.name[:6] - ecommerce_url = "https://ecommerce-{site_name}-{dns_name}.sandbox.edx.org/".format( + ecommerce_url = ecommerce_url_fmt.format( site_name=site_name, dns_name=self.dns_name ) @@ -114,22 +130,29 @@ class TestCreateSiteAndConfiguration(TestCase): 1 ) - def _assert_discovery_clients_are_valid(self): + def _assert_discovery_clients_are_valid(self, devstack=False): """ Checks that all discovery clients are valid """ service_user = self._assert_service_user_is_valid("lms_catalog_service_user") clients = Client.objects.filter(user=service_user) + self.assertEqual(len(clients), len(SITES)) + if devstack: + discovery_url_fmt = u"http://discovery-{site_name}-{dns_name}.e2e.devstack:18381/" + else: + discovery_url_fmt = u"https://discovery-{site_name}-{dns_name}.sandbox.edx.org/" + for client in clients: self.assertEqual(client.user.username, service_user[0].username) site_name = client.name[:6] - discovery_url = "https://discovery-{site_name}-{dns_name}.sandbox.edx.org/".format( + discovery_url = discovery_url_fmt.format( site_name=site_name, dns_name=self.dns_name ) + self.assertEqual(client.url, discovery_url) self.assertEqual( client.redirect_uri, @@ -179,3 +202,30 @@ class TestCreateSiteAndConfiguration(TestCase): self._assert_sites_are_valid() self._assert_discovery_clients_are_valid() self._assert_ecommerce_clients_are_valid() + + @mock.patch(MANAGEMENT_COMMAND_PATH + "Command._enable_commerce_configuration") + @mock.patch(MANAGEMENT_COMMAND_PATH + "Command._get_sites_data") + def test_with_devstack_and_dns(self, mock_get_sites, mock_commerce): + """ Test the command with dns_name """ + mock_get_sites.return_value = _get_sites(self.dns_name, devstack=True) + mock_commerce.return_value = None + call_command( + "create_sites_and_configurations", + "--dns-name", self.dns_name, + "--theme-path", self.theme_path, + "--devstack" + ) + self._assert_sites_are_valid() + self._assert_discovery_clients_are_valid(devstack=True) + self._assert_ecommerce_clients_are_valid(devstack=True) + + call_command( + "create_sites_and_configurations", + "--dns-name", self.dns_name, + "--theme-path", self.theme_path, + "--devstack" + ) + # if we run command with same dns then it will not duplicates the sites and oauth2 clients. + self._assert_sites_are_valid() + self._assert_discovery_clients_are_valid(devstack=True) + self._assert_ecommerce_clients_are_valid(devstack=True)