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_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_ensure_organization_raises_for_unknown_org_no_autocreate(self): """ Test that, with organization auto-create DISABLED, ``ensure_organization`` fails when given a non-existent organization (in contrast to how it behaves when organization auto-create is enabled). """ with self.assertRaises(exceptions.InvalidOrganizationException): api.ensure_organization('myorg') # Make sure the organization was not saved to the database. with self.assertRaises(exceptions.InvalidOrganizationException): api.get_organization_by_short_name('myorg')
def test_library_with_unknown_organization_autocreation(self): """ Test that when automatic organization creation is enabled, creating a content library with an unknown organization auto-creates said organization. """ with self.assertRaises(InvalidOrganizationException): get_organization_by_short_name("org_xyz") response = self.client.ajax_post(LIBRARY_REST_URL, { 'org': "org_xyz", 'library': "org_test_lib", 'display_name': "This library's organization doesn't exist... yet.", }) assert response.status_code == 200 assert get_organization_by_short_name("org_xyz")
def test_library_with_unknown_organization_validation_error(self): """ Test that when automatic organization creation is disabled, creating a content library with an unknown organization raises an error. """ with self.assertRaises(InvalidOrganizationException): get_organization_by_short_name("org_xyz") response = self.client.ajax_post(LIBRARY_REST_URL, { 'org': "org_xyz", 'library': "org_test_lib", 'display_name': "This library's organization doesn't exist!", }) assert response.status_code == 400 assert "'org_xyz' is not a valid organization identifier" in parse_json(response)['ErrMsg'] with self.assertRaises(InvalidOrganizationException): get_organization_by_short_name("org_xyz")
def test_course_creation_for_unknown_organization_strict(self, store): """ Tests that when ORGANIZATIONS_AUTOCREATE is False, creating a course-run with an unknown org slug will raise a validation error. """ with modulestore().default_store(store): 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, 400) with self.assertRaises(InvalidOrganizationException): get_organization_by_short_name("orgX") data = parse_json(response) self.assertIn('Organization you selected does not exist in the system', data['error'])
def get_organization_by_short_name(organization_short_name): """ Client API operation adapter/wrapper """ if not organizations_enabled(): return None from organizations import api as organizations_api from organizations.exceptions import InvalidOrganizationException try: return organizations_api.get_organization_by_short_name(organization_short_name) except InvalidOrganizationException: return None
def get_organization_by_short_name(organization_short_name): """ Client API operation adapter/wrapper """ if not organizations_enabled(): return None from organizations import api as organizations_api from organizations.exceptions import InvalidOrganizationException try: return organizations_api.get_organization_by_short_name(organization_short_name) except InvalidOrganizationException: return None
def test_course_creation_for_unknown_organization_relaxed(self, store): """ Tests that when ORGANIZATIONS_AUTOCREATE is True, creating a course-run with an unknown org slug will create an organization and organization-course linkage in the system. """ with self.assertRaises(InvalidOrganizationException): get_organization_by_short_name("orgX") with modulestore().default_store(store): 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) self.assertIsNotNone(get_organization_by_short_name("orgX")) 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_ensure_organization_creates_unknown_org(self, mock_log_info): """ Test that, with organization auto-create enabled, ``ensure_organization`` automatically creates not-yet-existent organizations. """ org_data = api.ensure_organization('myorg') assert org_data['id'] assert org_data['short_name'] == 'myorg' assert org_data['name'] == 'myorg' assert org_data['description'] == '' # auto-created, so no description. # Make sure we logged about the new organization's creation. assert mock_log_info.call_count == 1 # Make sure that the organization has, in fact, been saved to the database # by loading it up again. assert api.get_organization_by_short_name('myorg') == org_data
def test_end_to_end(self, run_type): """ Test the happy path of the backfill command without any mocking. """ # org_A: already existing, with courses and a library. org_a = add_organization({"short_name": "org_A", "name": "Org A"}) course_a1_key = CourseOverviewFactory(org="org_A", run="1").id CourseOverviewFactory(org="org_A", run="2") LibraryFactory(org="org_A") # Write linkage for org_a->course_a1. # (Linkage for org_a->course_a2 is purposefully left out here; # it should be created by the backfill). add_organization_course(org_a, course_a1_key) # org_B: already existing, but has no content. add_organization({"short_name": "org_B", "name": "Org B"}) # org_C: has a few courses; should be created. CourseOverviewFactory(org="org_C", run="1") CourseOverviewFactory(org="org_C", run="2") # Include an Old Mongo Modulestore -style deprecated course key. # This can be safely removed when Old Mongo Modulestore support is # removed. CourseOverviewFactory( id=CourseLocator.from_string("org_C/toy/3"), org="org_C", run="3", ) # org_D: has both a course and a library; should be created. CourseOverviewFactory(org="org_D", run="1") LibraryFactory(org="org_D") # org_E: just has a library; should be created. LibraryFactory(org="org_E") # Confirm starting condition: # Only orgs are org_A and org_B, and only linkage is org_a->course_a1. assert set( org["short_name"] for org in get_organizations() ) == { "org_A", "org_B" } assert len(get_organization_courses(get_organization_by_short_name('org_A'))) == 1 assert len(get_organization_courses(get_organization_by_short_name('org_B'))) == 0 # Run the backfill. call_command("backfill_orgs_and_org_courses", run_type) if run_type == "--dry": # Confirm ending conditions are the same as the starting conditions. assert set( org["short_name"] for org in get_organizations() ) == { "org_A", "org_B" } assert len(get_organization_courses(get_organization_by_short_name('org_A'))) == 1 assert len(get_organization_courses(get_organization_by_short_name('org_B'))) == 0 else: # Confirm ending condition: # All five orgs present. Each org a has expected number of org-course linkages. assert set( org["short_name"] for org in get_organizations() ) == { "org_A", "org_B", "org_C", "org_D", "org_E" } assert len(get_organization_courses(get_organization_by_short_name('org_A'))) == 2 assert len(get_organization_courses(get_organization_by_short_name('org_B'))) == 0 assert len(get_organization_courses(get_organization_by_short_name('org_C'))) == 3 assert len(get_organization_courses(get_organization_by_short_name('org_D'))) == 1 assert len(get_organization_courses(get_organization_by_short_name('org_E'))) == 0
def test_end_to_end(self): """ Test the happy path of the backfill command without any mocking. """ # org_A: already existing, with courses and a library. org_a = add_organization({"short_name": "org_A", "name": "Org A"}) course_a1_key = CourseOverviewFactory(org="org_A", run="1").id CourseOverviewFactory(org="org_A", run="2") LibraryFactory(org="org_A") # Write linkage for org_a->course_a1. # (Linkage for org_a->course_a2 is purposefully left out here; # it should be created by the backfill). add_organization_course(org_a, course_a1_key) # org_B: already existing, but has no content. add_organization({"short_name": "org_B", "name": "Org B"}) # org_C: has a couple courses; should be created. CourseOverviewFactory(org="org_C", run="1") CourseOverviewFactory(org="org_C", run="2") # org_D: has both a course and a library; should be created. CourseOverviewFactory(org="org_D", run="1") LibraryFactory(org="org_D") # org_E: just has a library; should be created. LibraryFactory(org="org_E") # Confirm starting condition: # Only orgs are org_A and org_B, and only linkage is org_a->course_a1. assert set(org["short_name"] for org in get_organizations()) == {"org_A", "org_B"} assert len( get_organization_courses( get_organization_by_short_name('org_A'))) == 1 assert len( get_organization_courses( get_organization_by_short_name('org_B'))) == 0 # Run the backfill. call_command("backfill_orgs_and_org_courses", "--apply") # Confirm ending condition: # All five orgs present. Each org a has expected number of org-course linkages. assert set(org["short_name"] for org in get_organizations()) == { "org_A", "org_B", "org_C", "org_D", "org_E" } assert len( get_organization_courses( get_organization_by_short_name('org_A'))) == 2 assert len( get_organization_courses( get_organization_by_short_name('org_B'))) == 0 assert len( get_organization_courses( get_organization_by_short_name('org_C'))) == 2 assert len( get_organization_courses( get_organization_by_short_name('org_D'))) == 1 assert len( get_organization_courses( get_organization_by_short_name('org_E'))) == 0