def test_get_organization_by_short_name(self): """ Unit Test: get_organization_by_short_name""" api.add_organization({ "name": "local_organization_1ßßß", "short_name": "Orgx1", "description": "Local Organization 1 Descriptionßßß", }) api.add_organization({ "name": "local_organization_2", "short_name": "Orgx2", "description": "Local Organization 2", }) with self.assertNumQueries(1): organization = api.get_organization_by_short_name("Orgx2") self.assertEqual(organization["name"], "local_organization_2") self.assertEqual(organization["description"], "Local Organization 2") with self.assertNumQueries(0): with self.assertRaises(exceptions.InvalidOrganizationException): api.get_organization_by_short_name(None) with self.assertNumQueries(1): with self.assertRaises(exceptions.InvalidOrganizationException): api.get_organization_by_short_name("not_existing")
def test_get_course_organization_multi_linked_orgs(self): """ Test that when a course is linked to multiple organizations, ``get_course_organization`` and ``get_course_organization_id`` return the first-linked one. """ # Use non-alphabetically-ordered org names to test that the # returned org was the first *linked*, not just the first *alphabetically*. api.add_organization_course( api.add_organization({ 'short_name': 'orgW', 'name': 'Org West' }), self.test_course_key, ) api.add_organization_course( api.add_organization({ 'short_name': 'orgN', 'name': 'Org North' }), self.test_course_key, ) api.add_organization_course( api.add_organization({ 'short_name': 'orgS', 'name': 'Org South' }), self.test_course_key, ) org_result = api.get_course_organization(self.test_course_key) assert org_result['short_name'] == 'orgW' org_id_result = api.get_course_organization_id(self.test_course_key) assert org_id_result assert org_id_result == org_result['id']
def test_course_creation_for_known_organization(self, organizations_autocreate): """ Tests course creation workflow when course organization exist in system. """ add_organization({ 'name': 'Test Organization', 'short_name': 'orgX', 'description': 'Testing Organization Description', }) with override_settings( ORGANIZATIONS_AUTOCREATE=organizations_autocreate): response = self.client.ajax_post( self.course_create_rerun_url, { 'org': 'orgX', 'number': 'CS101', 'display_name': 'Course with web certs enabled', 'run': '2015_T2' }) self.assertEqual(response.status_code, 200) data = parse_json(response) new_course_key = CourseKey.from_string(data['course_key']) course_orgs = get_course_organizations(new_course_key) self.assertEqual(len(course_orgs), 1) self.assertEqual(course_orgs[0]['short_name'], 'orgX')
def test_get_organization_by_short_name(self): """ Unit Test: get_organization_by_short_name""" api.add_organization({ 'name': 'local_organization_1ßßß', 'short_name': 'Orgx1', 'description': 'Local Organization 1 Descriptionßßß' }) api.add_organization({ 'name': 'local_organization_2', 'short_name': 'Orgx2', 'description': 'Local Organization 2' }) with self.assertNumQueries(1): organization = api.get_organization_by_short_name('Orgx2') self.assertEqual(organization['name'], 'local_organization_2') self.assertEqual(organization['description'], 'Local Organization 2') with self.assertNumQueries(0): with self.assertRaises(exceptions.InvalidOrganizationException): api.get_organization_by_short_name(None) with self.assertNumQueries(1): with self.assertRaises(exceptions.InvalidOrganizationException): api.get_organization_by_short_name('not_existing')
def test_course_creation_without_permission_for_specific_organization(self, store): """ Tests course creation with user not having permission to create course for specific organization. """ add_organization({ 'name': 'Test Organization', 'short_name': self.source_course_key.org, 'description': 'Testing Organization Description', }) add_organization({ 'name': 'DC', 'short_name': 'DC', 'description': 'DC Comics', }) self.course_creator_entry.all_organizations = False self.course_creator_entry.state = CourseCreator.GRANTED self.creator_admin.save_model(self.request, self.course_creator_entry, None, True) # User has been given the permission to create course under `DC` organization. # When the user tries to create course under `Test Organization` it throws a 403. dc_org_object = Organization.objects.get(name='DC') self.course_creator_entry.organizations.add(dc_org_object) with modulestore().default_store(store): response = self.client.ajax_post(self.course_create_rerun_url, { 'org': self.source_course_key.org, 'number': 'CS101', 'display_name': 'Course with web certs enabled', 'run': '2021_T1' }) self.assertEqual(response.status_code, 403)
def test_rerun(self): """ Just testing the functionality the view handler adds over the tasks tested in test_clone_course """ add_organization({ 'name': 'Test Organization', 'short_name': self.source_course_key.org, 'description': 'Testing Organization Description', }) response = self.client.ajax_post( self.course_create_rerun_url, { 'source_course_key': six.text_type(self.source_course_key), 'org': self.source_course_key.org, 'course': self.source_course_key.course, 'run': 'copy', 'display_name': 'not the same old name', }) self.assertEqual(response.status_code, 200) data = parse_json(response) dest_course_key = CourseKey.from_string(data['destination_course_key']) self.assertEqual(dest_course_key.run, 'copy') source_course = self.store.get_course(self.source_course_key) dest_course = self.store.get_course(dest_course_key) self.assertEqual(dest_course.start, CourseFields.start.default) self.assertEqual(dest_course.end, source_course.end) self.assertEqual(dest_course.enrollment_start, None) self.assertEqual(dest_course.enrollment_end, None) course_orgs = get_course_organizations(dest_course_key) self.assertEqual(len(course_orgs), 1) self.assertEqual(course_orgs[0]['short_name'], self.source_course_key.org)
def test_add_several_organizations(self): """ Test that the query_count of bulk_add_organizations does not increase when given more organizations. """ existing_org = api.add_organization( self.make_organization_data("existing_org")) api.remove_organization( api.add_organization( self.make_organization_data("org_to_reactivate"))["id"]) # 1 query to load list of existing orgs, # 1 query to filter for only inactive existing orgs, # 1 query for activate-existing, and 1 query for create-new. with self.assertNumQueries(4): api.bulk_add_organizations([ existing_org, existing_org, existing_org, self.make_organization_data("org_to_reactivate"), self.make_organization_data("new_org_1"), self.make_organization_data("new_org_2"), self.make_organization_data("new_org_3"), self.make_organization_data("new_org_4"), self.make_organization_data("new_org_5"), self.make_organization_data("new_org_6"), self.make_organization_data("new_org_7"), self.make_organization_data("new_org_8"), self.make_organization_data("new_org_9"), self.make_organization_data("new_org_9"), # Redundant. self.make_organization_data("new_org_9"), # Redundant. ]) assert len(api.get_organizations()) == 11
def test_add_organization_active_exists(self): """ Unit Test: test_add_organization_active_exists""" organization_data = { 'name': 'local_organizationßßß', 'description': 'Local Organization Descriptionßßß' } organization = api.add_organization(organization_data) self.assertGreater(organization['id'], 0) with self.assertNumQueries(1): organization = api.add_organization(organization_data)
def test_add_organization_active_exists(self): """ Unit Test: test_add_organization_active_exists""" organization_data = { "name": "local_organizationßßß", "description": "Local Organization Descriptionßßß", } organization = api.add_organization(organization_data) self.assertGreater(organization["id"], 0) with self.assertNumQueries(1): organization = api.add_organization(organization_data)
def test_add_organization_inactive_organization_with_relationships(self): """ Unit Test: test_add_organization_inactive_organization_with_relationships""" organization_data = { 'name': 'local_organizationßßß', 'description': 'Local Organization Descriptionßßß' } organization = api.add_organization(organization_data) api.add_organization_course(organization, self.test_course_key) with self.assertNumQueries(1): organization = api.add_organization(organization_data)
def test_add_organization_inactive_to_active(self): """ Unit Test: test_add_organization_inactive_organization_present""" organization_data = { 'name': 'local_organizationßßß', 'description': 'Local Organization Descriptionßßß' } organization = api.add_organization(organization_data) self.assertGreater(organization['id'], 0) api.remove_organization(organization['id']) with self.assertNumQueries(3): organization = api.add_organization(organization_data)
def test_add_organization_inactive_to_active(self): """ Unit Test: test_add_organization_inactive_organization_present""" organization_data = { "name": "local_organizationßßß", "description": "Local Organization Descriptionßßß", } organization = api.add_organization(organization_data) self.assertGreater(organization["id"], 0) api.remove_organization(organization["id"]) with self.assertNumQueries(3): organization = api.add_organization(organization_data)
def setUp(self): super(TestOrganizationListing, self).setUp() self.staff = UserFactory(is_staff=True) self.client.login(username=self.staff.username, password='******') self.org_names_listing_url = reverse('organizations') self.org_short_names = ["alphaX", "betaX", "orgX"] for index, short_name in enumerate(self.org_short_names): add_organization(organization_data={ 'name': u'Test Organization %s' % index, 'short_name': short_name, 'description': u'Testing Organization %s Description' % index, })
def test_rerun(self, pacing_type, expected_self_paced_value, number): original_course_run = ToyCourseFactory() add_organization({ 'name': 'Test Organization', 'short_name': original_course_run.id.org, # lint-amnesty, pylint: disable=no-member 'description': 'Testing Organization Description', }) start = datetime.datetime.now(pytz.UTC).replace(microsecond=0) end = start + datetime.timedelta(days=30) user = UserFactory() role = 'instructor' run = '3T2017' url = reverse('api:v1:course_run-rerun', kwargs={'pk': str(original_course_run.id)}) # lint-amnesty, pylint: disable=no-member data = { 'run': run, 'schedule': { 'start': serialize_datetime(start), 'end': serialize_datetime(end), }, 'team': [ { 'user': user.username, 'role': role, } ], 'pacing_type': pacing_type, } # If number is supplied, this should become the course number used in the course run key # If not, it should default to the original course run number that the rerun is based on. if number: data.update({'number': number}) response = self.client.post(url, data, format='json') assert response.status_code == 201 course_run_key = CourseKey.from_string(response.data['id']) course_run = modulestore().get_course(course_run_key) assert course_run.id.run == run assert course_run.self_paced is expected_self_paced_value if number: assert course_run.id.course == number assert course_run.id.course != original_course_run.id.course # lint-amnesty, pylint: disable=no-member else: assert course_run.id.course == original_course_run.id.course # lint-amnesty, pylint: disable=no-member self.assert_course_run_schedule(course_run, start, end) self.assert_access_role(course_run, user, role) self.assert_course_access_role_count(course_run, 1) course_orgs = get_course_organizations(course_run_key) self.assertEqual(len(course_orgs), 1) self.assertEqual(course_orgs[0]['short_name'], original_course_run.id.org) # lint-amnesty, pylint: disable=no-member
def test_get_organizations(self): """ Unit Test: test_get_organizations """ api.add_organization({ 'name': 'local_organization_1ßßß', 'description': 'Local Organization 1 Descriptionßßß' }) api.add_organization({ 'name': 'local_organization_2ßßß', 'description': 'Local Organization 2 Descriptionßßß' }) with self.assertNumQueries(1): organizations = api.get_organizations() self.assertEqual(len(organizations), 3) # One from SetUp, two from local
def test_add_organization_inactive_organization_with_relationships(self): """ Unit Test: test_add_organization_inactive_organization_with_relationships""" organization_data = { 'name': 'local_organizationßßß', 'description': 'Local Organization Descriptionßßß' } organization = api.add_organization(organization_data) api.add_organization_course( organization, self.test_course_key ) with self.assertNumQueries(1): organization = api.add_organization(organization_data)
def test_add_organization_invalid_data_throws_exceptions(self): """ Unit Test: test_add_organization_invalid_namespaces_throw_exceptions""" with self.assertNumQueries(0): with self.assertRaises(exceptions.InvalidOrganizationException): api.add_organization({ 'name': '', # Empty name should throw an exception on create 'description': 'Local Organization Description 2ßßß' }) with self.assertNumQueries(0): with self.assertRaises(exceptions.InvalidOrganizationException): api.add_organization({ # Missing name should throw exception 'description': 'Local Organization Description 2ßßß' })
def test_ensure_organization_retrieves_known_org_no_autocreate(self): """ Test that, with organization auto-create DISABLED, ``ensure_organization`` returns the org data of an existing organization (just like it does when organization auto-create is enabled). """ api.add_organization({ 'short_name': 'myorg', 'name': 'My Org', 'description': 'this is my org' }) org_data = api.ensure_organization('myorg') assert org_data['id'] assert org_data['short_name'] == 'myorg' assert org_data['name'] == 'My Org' assert org_data['description'] == 'this is my org'
def add_organization(organization_data): """ Client API operation adapter/wrapper """ if not organizations_enabled(): return None from organizations import api as organizations_api return organizations_api.add_organization(organization_data=organization_data)
def add_organization(organization_data): """ Client API operation adapter/wrapper """ if not settings.FEATURES.get('ORGANIZATIONS_APP', False): return None from organizations import api as organizations_api return organizations_api.add_organization(organization_data=organization_data)
def test_add_organization_inactive_to_active(self): """ Unit Test: test_add_organization_inactive_to_active""" organization_data = { 'name': 'local_organizationßßß', 'description': 'Local Organization Descriptionßßß' } organization = api.add_organization(organization_data) self.assertGreater(organization['id'], 0) api.remove_organization(organization['id']) with self.assertNumQueries(5): organization = api.add_organization(organization_data) # Assert that the organization is active by making sure we can load it. loaded_organization = api.get_organization(organization['id']) self.assertEqual(loaded_organization['name'], organization_data['name'])
def test_add_organization(self): """ Unit Test: test_add_organization""" with self.assertNumQueries(2): organization = api.add_organization({ 'name': 'local_organizationßßß', 'description': 'Local Organization Descriptionßßß' }) self.assertGreater(organization['id'], 0)
def test_course_creation_when_user_in_org_with_creator_role(self, store): """ Tests course creation with user having the organization content creation role. """ add_organization({ 'name': 'Test Organization', 'short_name': self.source_course_key.org, 'description': 'Testing Organization Description', }) update_org_role(self.global_admin, OrgContentCreatorRole, self.user, [self.source_course_key.org]) with modulestore().default_store(store): response = self.client.ajax_post(self.course_create_rerun_url, { 'org': self.source_course_key.org, 'number': 'CS101', 'display_name': 'Course with web certs enabled', 'run': '2021_T1' }) self.assertEqual(response.status_code, 200)
def add_organization(organization_data): """ Client API operation adapter/wrapper """ if not settings.FEATURES.get('ORGANIZATIONS_APP', False): return None from organizations import api as organizations_api return organizations_api.add_organization( organization_data=organization_data)
def setUp(self): """ Organizations API Test Case scaffolding """ super(OrganizationsApiTestCase, self).setUp() self.test_organization = api.add_organization({ 'name': unicode('test_organizationßßß', 'utf-8'), 'description': unicode('Test Organization Descriptionßßß', 'utf-8'), })
def test_add_organization(self): """ Unit Test: test_add_organization""" with self.assertNumQueries(2): organization = api.add_organization({ "name": "local_organizationßßß", "description": "Local Organization Descriptionßßß", }) self.assertGreater(organization["id"], 0)
def test_ensure_organization_retrieves_known_org(self, mock_log_info): """ Test that, with oranization auto-create enabled, ``ensure_organization`` returns the org data of an existing organization. """ api.add_organization({ 'short_name': 'myorg', 'name': 'My Org', 'description': 'this is my org' }) org_data = api.ensure_organization('myorg') assert org_data['id'] assert org_data['short_name'] == 'myorg' assert org_data['name'] == 'My Org' assert org_data['description'] == 'this is my org' # We expect a single logging message in test_ensure_organization_creates_unknown_org, # so check for 0 log messages here as a control. assert mock_log_info.call_count == 0
def test_add_several_organization_courses(self): """ Test that the query_count of bulk_add_organization_courses does not increase when given more organization-course linkages to add. """ org_a = api.add_organization(self.make_organization_data("org_a")) org_b = api.add_organization(self.make_organization_data("org_b")) org_c = api.add_organization(self.make_organization_data("org_c")) course_key_x = CourseKey.from_string("course-v1:x+x+x") course_key_y = CourseKey.from_string("course-v1:y+y+y") course_key_z = CourseKey.from_string("course-v1:z+z+z") # Add linkage A->X. api.add_organization_course(org_a, course_key_x) # Add linkage A->Y, and then remove (actually: deactivate). api.add_organization_course(org_a, course_key_y) api.remove_organization_course(org_a, course_key_y) # 1 query to load list of existing linkages, # 1 query to fetch organizations for existing linkages, # 1 query to ensure all existing linkages active, # 1 query to get organiations for new linkages, # 1 query to create new linkages. with self.assertNumQueries(5): api.bulk_add_organization_courses([ (org_a, course_key_x), # Already existing. (org_a, course_key_x), # Already existing. (org_a, course_key_y), # Reactivation. (org_a, course_key_z), # The rest are new. (org_b, course_key_x), (org_b, course_key_y), (org_b, course_key_z), (org_c, course_key_x), (org_c, course_key_y), (org_c, course_key_z), (org_c, course_key_z), # Redundant. (org_c, course_key_z), # Redundant. ]) assert len(api.get_organization_courses(org_a)) == 3 assert len(api.get_organization_courses(org_b)) == 3 assert len(api.get_organization_courses(org_c)) == 3
def setUp(self): """ Organizations API Test Case scaffolding """ super(OrganizationsApiTestCase, self).setUp() self.test_organization = api.add_organization({ 'name': 'test_organizationßßß', 'description': 'Test Organization Descriptionßßß' })
def setUp(self): """ Organizations API Test Case scaffolding """ super(OrganizationsApiTestCase, self).setUp() self.test_organization = api.add_organization({ "name": str("test_organizationßßß", "utf-8"), "description": str("Test Organization Descriptionßßß", "utf-8"), })
def test_course_creation_with_all_org_checked(self, store): """ Tests course creation with user having permission to create course for all organization. """ add_organization({ 'name': 'Test Organization', 'short_name': self.source_course_key.org, 'description': 'Testing Organization Description', }) self.course_creator_entry.all_organizations = True self.course_creator_entry.state = CourseCreator.GRANTED self.creator_admin.save_model(self.request, self.course_creator_entry, None, True) with modulestore().default_store(store): response = self.client.ajax_post(self.course_create_rerun_url, { 'org': self.source_course_key.org, 'number': 'CS101', 'display_name': 'Course with web certs enabled', 'run': '2021_T1' }) self.assertEqual(response.status_code, 200)
def test_dry_run(self, mock_log_info): """ Test that `bulk_add_organization_courses` does nothing when `dry_run` is specified (except logging). """ org_a = api.add_organization(self.make_organization_data("org_a")) course_key_x = CourseKey.from_string("course-v1:x+x+x") api.bulk_add_organization_courses([(org_a, course_key_x)], dry_run=True) assert api.get_organization_courses(org_a) == [] # One for reactivations, one for creations. assert mock_log_info.call_count == 2
def handle(self, *args, **options): org_options = { key: options[key] for key in {'short_name', 'name'} } if len(org_options['short_name']) > len(org_options['name']): fmt = ( "The provided short_name ({short_name}) " "is longer than the provided name ({name}). " "You probably want to switch the order of the arguments." ) raise CommandError(fmt.format(**org_options)) created_org = api.add_organization(org_options) log_fmt = ( "Created or activated organization: id={id}, " "short_name={short_name}, name='{name}'" ) logger.info(log_fmt.format(**created_org))