def setUp(self): super(OrganizationUserRoleAdminTests, self).setUp() self.user = UserFactory(is_staff=True, is_superuser=True) self.client.login(username=self.user.username, password=USER_PASSWORD) self.admin_page_url = reverse('admin:publisher_organizationuserrole_add') self.organization = OrganizationFactory() self.course1 = CourseFactory(organizations=[self.organization]) self.course2 = CourseFactory(organizations=[self.organization])
def test_save_method_add_course_user_roles(self): """ Verify that save method will not create the duplicate course user roles. """ # for course 3 add course roles user = UserFactory() course3 = CourseFactory() course3.organizations.add(self.organization) factories.CourseUserRoleFactory( course=course3, role=PublisherUserRole.MarketingReviewer, user=user) # for course 4 add course roles project_coordinator = UserFactory() course4 = CourseFactory() course4.organizations.add(self.organization) factories.CourseUserRoleFactory( course=course4, role=PublisherUserRole.ProjectCoordinator, user=project_coordinator) test_user = UserFactory() post_data = { 'organization': self.organization.id, 'user': test_user.id, 'role': PublisherUserRole.MarketingReviewer } self.client.post(self.admin_page_url, data=post_data) # for course-4 course-user-role does not change self.assertTrue( course4.course_user_roles.filter( role=PublisherUserRole.ProjectCoordinator, user=project_coordinator).exists()) # for course-3 course-user-role also changes to test_user self.assertTrue( course3.course_user_roles.filter( role=PublisherUserRole.MarketingReviewer, user=test_user).exists()) self.assertTrue( self.course1.course_user_roles.filter( role=PublisherUserRole.MarketingReviewer, user=test_user).exists()) self.assertTrue( self.course2.course_user_roles.filter( role=PublisherUserRole.MarketingReviewer, user=test_user).exists())
def setUp(self): super(PublisherCustomCourseFormTests, self).setUp() self.course_form = CourseForm() self.organization = OrganizationFactory() self.course = CourseFactory(title='Test', number='a123', organizations=[self.organization])
def test_course_revision_serializer_without_subjects(self): """ Verify that CourseRevisionSerializer serialize the course revision object even if subject fields are not available. """ course = CourseFactory(primary_subject=None, secondary_subject=None, tertiary_subject=None, level_type=None) course.title = 'updated title' course.save() revision = course.history.first() serializer = CourseRevisionSerializer(revision) expected = { 'history_id': revision.history_id, 'title': revision.title, 'number': revision.number, 'short_description': revision.short_description, 'full_description': revision.full_description, 'expected_learnings': revision.expected_learnings, 'prerequisites': revision.prerequisites, 'primary_subject': None, 'secondary_subject': None, 'tertiary_subject': None, 'level_type': None, 'learner_testimonial': revision.learner_testimonial, 'faq': revision.faq, 'video_link': revision.video_link } self.assertDictEqual(serializer.data, expected)
def test_create_course_run_in_studio_with_image_failure(self, __): organization = OrganizationFactory() OrganizationExtensionFactory(organization=organization) partner = organization.partner start = datetime.datetime.utcnow() course_run_key = 'course-v1:TestX+Testing101x+1T2017' body = {'id': course_run_key} studio_url_root = partner.studio_url.strip('/') url = '{}/api/v1/course_runs/'.format(studio_url_root) responses.add(responses.POST, url, json=body, status=200) course = CourseFactory(organizations=[organization]) with mock.patch('course_discovery.apps.api.utils.logger.exception' ) as mock_logger: publisher_course_run = CourseRunFactory( course=course, start=start, lms_course_id=None, ) assert mock_logger.call_count == 1 assert mock_logger.call_args_list[0] == mock.call( 'An error occurred while setting the course run image for [{key}] in studio. All other fields ' 'were successfully saved in Studio.'.format(key=course_run_key)) publisher_course_run.refresh_from_db() assert len(responses.calls) == 2 assert publisher_course_run.lms_course_id == course_run_key
def test_create_course_run_in_studio_with_image_api_failure( self, mock_access_token): organization = OrganizationFactory() partner = organization.partner start = datetime.datetime.utcnow() course_run_key = 'course-v1:TestX+Testing101x+1T2017' body = {'id': course_run_key} studio_url_root = partner.studio_url.strip('/') url = '{}/api/v1/course_runs/'.format(studio_url_root) responses.add(responses.POST, url, json=body, status=200) body = {'error': 'Server error'} url = '{root}/api/v1/course_runs/{course_run_key}/images/'.format( root=studio_url_root, course_run_key=course_run_key) responses.add(responses.POST, url, json=body, status=500) course = CourseFactory(organizations=[organization]) with mock.patch( 'course_discovery.apps.publisher.signals.logger.exception' ) as mock_logger: with pytest.raises(HttpServerError): CourseRunFactory( course=course, start=start, lms_course_id=None, ) assert len(responses.calls) == 2 mock_logger.assert_called_with( 'Failed to create course run [%s] on Studio: %s', course.key, json.dumps(body).encode('utf8'))
def test_create_course_run_in_studio_with_image_failure(self, __, ___): # pylint: disable=unused-argument organization = OrganizationFactory() partner = organization.partner start = datetime.datetime.utcnow() course_run_key = 'course-v1:TestX+Testing101x+1T2017' body = {'id': course_run_key} studio_url_root = partner.studio_url.strip('/') url = '{}/api/v1/course_runs/'.format(studio_url_root) responses.add(responses.POST, url, json=body, status=200) course = CourseFactory(organizations=[organization]) with mock.patch( 'course_discovery.apps.publisher.signals.logger.exception' ) as mock_logger: with pytest.raises(Exception): CourseRunFactory( course=course, start=start, lms_course_id=None, ) assert len(responses.calls) == 1 mock_logger.assert_called_with( 'Failed to create course run [%s] on Studio', course.key)
def setUp(self): super().setUp() self.organization = OrganizationFactory() self.organization_extension = OrganizationExtensionFactory() self.user = UserFactory() self.user.groups.add(self.organization_extension.group) self.course = CourseFactory(title='Test course') assign_perm(OrganizationExtension.VIEW_COURSE, self.organization_extension.group, self.organization_extension)
def test_clean_with_valid_owner_role(self): """ Test that 'clean' does not raise 'ValidationError' if the user role that has been assigned owner does exist """ course = CourseFactory() user = UserFactory() CourseUserRoleFactory(course=course, user=user, role=PublisherUserRole.CourseTeam) course_state_form = CourseStateAdminForm() course_state_form.cleaned_data = { 'owner_role': PublisherUserRole.CourseTeam, 'course': course } self.assertEqual(course_state_form.clean(), course_state_form.cleaned_data)
def setup_course(self, **course_kwargs): """ Creates the course and add organization and admin to this course. Returns: course: a course object course_admin: a user object """ course = CourseFactory(**course_kwargs) course_admin = UserFactory(username='******') organization_extension = OrganizationExtensionFactory() organization = organization_extension.organization course_admin.groups.add(organization_extension.group) course.organizations.add(organization) return course, course_admin
def setup_course(self, **course_kwargs): """ Creates the course and add organization and admin to this course. Returns: course: a course object course_admin: a user object """ organization_extension = OrganizationExtensionFactory() defaults = { 'organizations': [organization_extension.organization], } defaults.update(course_kwargs) course = CourseFactory(**defaults) course_admin = UserFactory() course_admin.groups.add(organization_extension.group) return course, course_admin
def test_course_revision_serializer(self): """ Verify that CourseRevisionSerializer serialize the course revision object. """ course = CourseFactory() course.title = 'updated title' course.save() revision = course.history.first() serializer = CourseRevisionSerializer(revision) expected = { 'history_id': revision.history_id, 'title': revision.title, 'number': revision.number, 'short_description': revision.short_description, 'full_description': revision.full_description, 'expected_learnings': revision.expected_learnings, 'prerequisites': revision.prerequisites, 'primary_subject': revision.primary_subject.name, 'secondary_subject': revision.secondary_subject.name, 'tertiary_subject': revision.tertiary_subject.name, 'level_type': revision.level_type.name } self.assertDictEqual(serializer.data, expected)
def setUp(self): super(PublisherCustomCourseFormTests, self).setUp() self.course_form = CustomCourseForm() self.course = CourseFactory(title="Test", number="a123") self.organization = OrganizationFactory() self.course.organizations.add(self.organization)
def test_calculate_course_run_key_run_value(month, expected): course = CourseFactory() start = datetime.datetime(2017, month, 1) assert StudioAPI.calculate_course_run_key_run_value( course.number, start=start) == expected