WL-1395 | Added User profile for service users.
This commit is contained in:
@@ -14,8 +14,10 @@ from django.contrib.auth.models import User
|
||||
from django.contrib.sites.models import Site
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from lms.djangoapps.commerce.models import CommerceConfiguration
|
||||
from openedx.core.djangoapps.theming.models import SiteTheme
|
||||
from openedx.core.djangoapps.site_configuration.models import SiteConfiguration
|
||||
from student.models import UserProfile
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
@@ -107,6 +109,15 @@ class Command(BaseCommand):
|
||||
result.append(os.path.join(root, name))
|
||||
return result
|
||||
|
||||
def _enable_commerce_configuration(self):
|
||||
"""
|
||||
Enable the commerce configuration.
|
||||
"""
|
||||
CommerceConfiguration.objects.get_or_create(
|
||||
enabled=True,
|
||||
checkout_on_ecommerce_service=True
|
||||
)
|
||||
|
||||
def _get_sites_data(self):
|
||||
"""
|
||||
Reads the json files from theme directory and returns the site data in JSON format.
|
||||
@@ -140,21 +151,28 @@ class Command(BaseCommand):
|
||||
"""
|
||||
Creates the service user for ecommerce and discovery.
|
||||
"""
|
||||
return User.objects.get_or_create(
|
||||
username=username,
|
||||
service_user, _ = User.objects.get_or_create(username=username)
|
||||
service_user.is_active = True
|
||||
service_user.is_staff = True
|
||||
service_user.is_superuser = True
|
||||
service_user.save()
|
||||
|
||||
# Without User profile we cannot publish the course from ecommerce to LMS.
|
||||
UserProfile.objects.get_or_create(
|
||||
user=service_user,
|
||||
defaults={
|
||||
"is_staff": True,
|
||||
"is_superuser": True
|
||||
"name": username
|
||||
}
|
||||
)
|
||||
return service_user
|
||||
|
||||
def handle(self, *args, **options):
|
||||
|
||||
self.theme_path = options['theme_path']
|
||||
self.dns_name = options['dns_name']
|
||||
|
||||
self.discovery_user, _ = self.get_or_create_service_user("lms_catalog_service_user")
|
||||
self.ecommerce_user, _ = self.get_or_create_service_user("ecommerce_worker")
|
||||
self.discovery_user = self.get_or_create_service_user("lms_catalog_service_user")
|
||||
self.ecommerce_user = self.get_or_create_service_user("ecommerce_worker")
|
||||
|
||||
all_sites = self._get_sites_data()
|
||||
|
||||
@@ -173,3 +191,5 @@ class Command(BaseCommand):
|
||||
|
||||
LOG.info("Creating ecommerce oauth2 client for '{site_name}' site".format(site_name=site_name))
|
||||
self._create_oauth2_client(ecommerce_url, site_name, is_discovery=False)
|
||||
|
||||
self._enable_commerce_configuration()
|
||||
|
||||
@@ -12,8 +12,10 @@ from django.core.management import call_command, CommandError
|
||||
from provider.oauth2.models import Client
|
||||
from edx_oauth2_provider.models import TrustedClient
|
||||
from openedx.core.djangoapps.theming.models import SiteTheme
|
||||
from student.models import UserProfile
|
||||
|
||||
SITES = ['site_a', 'site_b']
|
||||
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):
|
||||
@@ -67,13 +69,22 @@ class TestCreateSiteAndConfiguration(TestCase):
|
||||
_generate_site_config(self.dns_name, site.name)
|
||||
)
|
||||
|
||||
def _assert_service_user_is_valid(self, username):
|
||||
service_user = User.objects.filter(username=username)
|
||||
self.assertEqual(len(service_user), 1)
|
||||
self.assertTrue(service_user[0].is_active)
|
||||
self.assertTrue(service_user[0].is_staff)
|
||||
self.assertTrue(service_user[0].is_superuser)
|
||||
|
||||
user_profile = UserProfile.objects.filter(user=service_user)
|
||||
self.assertEqual(len(user_profile), 1)
|
||||
return service_user
|
||||
|
||||
def _assert_ecommerce_clients_are_valid(self):
|
||||
"""
|
||||
Checks that all ecommerce clients are valid
|
||||
"""
|
||||
service_user = User.objects.filter(username="ecommerce_worker")
|
||||
self.assertEqual(len(service_user), 1)
|
||||
self.assertTrue(service_user[0].is_staff)
|
||||
service_user = self._assert_service_user_is_valid("ecommerce_worker")
|
||||
|
||||
clients = Client.objects.filter(user=service_user)
|
||||
self.assertEqual(len(clients), len(SITES))
|
||||
@@ -99,9 +110,7 @@ class TestCreateSiteAndConfiguration(TestCase):
|
||||
"""
|
||||
Checks that all discovery clients are valid
|
||||
"""
|
||||
service_user = User.objects.filter(username="lms_catalog_service_user")
|
||||
self.assertEqual(len(service_user), 1)
|
||||
self.assertTrue(service_user[0].is_staff)
|
||||
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))
|
||||
@@ -130,12 +139,12 @@ class TestCreateSiteAndConfiguration(TestCase):
|
||||
"create_sites_and_configurations"
|
||||
)
|
||||
|
||||
@mock.patch(
|
||||
'openedx.core.djangoapps.theming.management.commands.create_sites_and_configurations.Command._get_sites_data'
|
||||
)
|
||||
def test_with_dns(self, mock_get_sites):
|
||||
@mock.patch(MANAGEMENT_COMMAND_PATH + "Command._enable_commerce_configuration")
|
||||
@mock.patch(MANAGEMENT_COMMAND_PATH + "Command._get_sites_data")
|
||||
def test_with_dns(self, mock_get_sites, mock_commerce):
|
||||
""" Test the command with dns_name """
|
||||
mock_get_sites.return_value = _get_sites(self.dns_name)
|
||||
mock_commerce.return_value = None
|
||||
call_command(
|
||||
"create_sites_and_configurations",
|
||||
"--dns-name", self.dns_name,
|
||||
|
||||
Reference in New Issue
Block a user