Commit df5010ee by Awais Committed by Awais Qureshi
parent ae66ce1b
import logging import logging
from django.contrib.auth.models import Group
from course_discovery.apps.publisher.assign_permissions import assign_permissions
from course_discovery.apps.publisher.choices import CourseRunStateChoices, CourseStateChoices, PublisherUserRole from course_discovery.apps.publisher.choices import CourseRunStateChoices, CourseStateChoices, PublisherUserRole
from course_discovery.apps.publisher.models import (Course, CourseRun, CourseRunState, CourseState, CourseUserRole, from course_discovery.apps.publisher.models import Course, CourseRun, CourseRunState, CourseState, Seat
OrganizationExtension, Seat)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -57,9 +53,6 @@ def create_or_update_course(meta_data_course, available_organization): ...@@ -57,9 +53,6 @@ def create_or_update_course(meta_data_course, available_organization):
if available_organization: if available_organization:
publisher_course.organizations.add(available_organization) publisher_course.organizations.add(available_organization)
# assign course-user-roles
assign_course_user_roles(publisher_course, available_organization)
# marked course as approved with related fields. # marked course as approved with related fields.
state, created = CourseState.objects.get_or_create(course=publisher_course) state, created = CourseState.objects.get_or_create(course=publisher_course)
if created: if created:
...@@ -154,13 +147,6 @@ def create_seats(metadata_course_run, publisher_course_run): ...@@ -154,13 +147,6 @@ def create_seats(metadata_course_run, publisher_course_run):
) )
def assign_course_user_roles(course, organization):
# Assign the course user roles against each organization users.
for user_role in organization.organization_user_roles.all():
CourseUserRole.add_course_roles(course, user_role.role, user_role.user)
def organizations_requirements(organizations, meta_data_course): def organizations_requirements(organizations, meta_data_course):
""" Before adding course make sure organization exists and has OrganizationExtension """ Before adding course make sure organization exists and has OrganizationExtension
object also. object also.
...@@ -179,25 +165,4 @@ def organizations_requirements(organizations, meta_data_course): ...@@ -179,25 +165,4 @@ def organizations_requirements(organizations, meta_data_course):
) )
return None return None
if available_organization.key.strip() == "": return available_organization
logger.warning(
'Organization key has empty value [%s].', available_organization.uuid
)
return None
group, __ = Group.objects.get_or_create(
name__iexact=available_organization.key, defaults={'name': available_organization.key}
)
organization_extension = OrganizationExtension.objects.filter(organization=available_organization)
if not organization_extension.exists():
organization_extension, __ = OrganizationExtension.objects.get_or_create(
organization=available_organization,
group=group
)
else:
organization_extension = organization_extension.first()
assign_permissions(organization_extension)
return organization_extension.organization
import ddt import ddt
import mock import mock
from django.contrib.auth.models import Group
from django.core.management import CommandError, call_command from django.core.management import CommandError, call_command
from django.test import TestCase from django.test import TestCase
from guardian.shortcuts import get_group_perms
from testfixtures import LogCapture from testfixtures import LogCapture
from course_discovery.apps.course_metadata.tests.factories import (CourseFactory, CourseRunFactory, OrganizationFactory, from course_discovery.apps.course_metadata.tests.factories import (CourseFactory, CourseRunFactory, OrganizationFactory,
PersonFactory, SeatFactory, SubjectFactory) PersonFactory, SeatFactory)
from course_discovery.apps.ietf_language_tags.models import LanguageTag from course_discovery.apps.ietf_language_tags.models import LanguageTag
from course_discovery.apps.publisher.choices import PublisherUserRole
from course_discovery.apps.publisher.constants import REVIEWER_GROUP_NAME
from course_discovery.apps.publisher.dataloader.create_courses import logger as dataloader_logger from course_discovery.apps.publisher.dataloader.create_courses import logger as dataloader_logger
from course_discovery.apps.publisher.models import Course as Publisher_Course from course_discovery.apps.publisher.models import Course as Publisher_Course
from course_discovery.apps.publisher.models import CourseRun as Publisher_CourseRun from course_discovery.apps.publisher.models import CourseRun as Publisher_CourseRun
from course_discovery.apps.publisher.models import OrganizationExtension
from course_discovery.apps.publisher.tests import factories from course_discovery.apps.publisher.tests import factories
from course_discovery.apps.publisher.tests.factories import GroupFactory
@ddt.ddt @ddt.ddt
...@@ -144,7 +138,8 @@ class CreateCoursesTests(TestCase): ...@@ -144,7 +138,8 @@ class CreateCoursesTests(TestCase):
self.course.save() self.course.save()
# create org and assign to the course-metadata # create org and assign to the course-metadata
self.organization = OrganizationFactory() self.forganization_extension = factories.OrganizationExtensionFactory()
self.organization = self.forganization_extension.organization
self.course.authoring_organizations.add(self.organization) self.course.authoring_organizations.add(self.organization)
def test_course_create_successfully(self): def test_course_create_successfully(self):
...@@ -159,30 +154,6 @@ class CreateCoursesTests(TestCase): ...@@ -159,30 +154,6 @@ class CreateCoursesTests(TestCase):
self.assertFalse(course.course_user_roles.all()) self.assertFalse(course.course_user_roles.all())
self.assertFalse(self.course.subjects.all()) self.assertFalse(self.course.subjects.all())
def test_course_create_successfully_with_roles(self):
""" Verify that publisher course with default roles and subjects."""
for role, __ in PublisherUserRole.choices:
factories.OrganizationUserRoleFactory(role=role, organization=self.organization)
subjects = SubjectFactory.create_batch(3)
self.course.subjects.add(*subjects)
call_command(self.command_name, *self.command_args)
publisher_course = Publisher_Course.objects.all().first()
self._assert_course(publisher_course)
self._assert_course_run(publisher_course.course_runs.first(), self.course.canonical_course_run)
self._assert_seats(publisher_course.course_runs.first(), self.course.canonical_course_run)
roles = publisher_course.course_user_roles.all()
for role in PublisherUserRole.choices:
self.assertEqual(roles.get(role=role[0]).role, role[0])
subjects = self.course.subjects.all()
self.assertEqual(subjects[0], publisher_course.primary_subject)
self.assertEqual(subjects[1], publisher_course.secondary_subject)
self.assertEqual(subjects[2], publisher_course.tertiary_subject)
def test_course_does_not_create_twice(self): def test_course_does_not_create_twice(self):
""" Verify that course does not create two course with same title and number. """ Verify that course does not create two course with same title and number.
Just update. Just update.
...@@ -254,55 +225,6 @@ class CreateCoursesTests(TestCase): ...@@ -254,55 +225,6 @@ class CreateCoursesTests(TestCase):
), ),
) )
def test_course_create_with_org_extension(self):
""" Verify that publisher course having organization-ext."""
factories.OrganizationExtensionFactory(
organization=self.course.authoring_organizations.all().first()
)
call_command(self.command_name, *self.command_args)
course = Publisher_Course.objects.all().first()
self._assert_course(course)
def test_course_create_with_empty_org_key(self):
""" Verify that publisher course having organization-ext."""
org = self.course.authoring_organizations.all().first()
org.key = ''
org.save()
with LogCapture(dataloader_logger.name) as log_capture:
call_command(self.command_name, *self.command_args)
log_capture.check(
(
dataloader_logger.name,
'WARNING',
'Organization key has empty value [{}].'.format(org.uuid)
),
)
def test_organization_ext_with_duplicate_organization(self):
""" Verify that if organization-ext already exists but with different group it will raise error
if it comes again with different org."""
# make org and create its org-ext object.
organization = OrganizationFactory(name='test-org', key='testing')
factories.OrganizationExtensionFactory(
organization=organization, group=GroupFactory(name='testing name')
)
# update org key to a same group name so that it will raise exception.
course_organization = self.course.authoring_organizations.all().first()
course_organization.key = 'testing name'
course_organization.save()
with LogCapture(dataloader_logger.name) as log_capture:
call_command(self.command_name, *self.command_args)
log_capture.check(
(
dataloader_logger.name,
'ERROR',
'Exception appear for course-id [{}].'.format(self.course.uuid)
),
)
def _assert_course(self, publisher_course): def _assert_course(self, publisher_course):
""" Verify that publisher course and metadata course has correct values.""" """ Verify that publisher course and metadata course has correct values."""
...@@ -352,35 +274,3 @@ class CreateCoursesTests(TestCase): ...@@ -352,35 +274,3 @@ class CreateCoursesTests(TestCase):
sorted([(seat.type, seat.price, seat.credit_provider, seat.currency) for seat in metadata_seats]), sorted([(seat.type, seat.price, seat.credit_provider, seat.currency) for seat in metadata_seats]),
sorted([(seat.type, seat.price, seat.credit_provider, seat.currency) for seat in publisher_seats]) sorted([(seat.type, seat.price, seat.credit_provider, seat.currency) for seat in publisher_seats])
) )
def test_organization_extension_permission(self):
"""
Verify that required permissions assigned to OrganizationExtension object.
"""
call_command(self.command_name, *self.command_args)
course = Publisher_Course.objects.all().first()
organization_extension = course.organizations.first().organization_extension
course_team_permissions = [
OrganizationExtension.VIEW_COURSE,
OrganizationExtension.EDIT_COURSE,
OrganizationExtension.VIEW_COURSE_RUN,
OrganizationExtension.EDIT_COURSE_RUN
]
self._assert_permissions(
organization_extension, organization_extension.group, course_team_permissions
)
marketing_permissions = [
OrganizationExtension.VIEW_COURSE,
OrganizationExtension.EDIT_COURSE,
OrganizationExtension.VIEW_COURSE_RUN
]
self._assert_permissions(
organization_extension, Group.objects.get(name=REVIEWER_GROUP_NAME), marketing_permissions
)
def _assert_permissions(self, organization_extension, group, expected_permissions):
permissions = get_group_perms(group, organization_extension)
self.assertEqual(sorted(permissions), sorted(expected_permissions))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment