def test_list(self): """ Verify the endpoint returns a list of all organizations. """ OrganizationFactory.create_batch(3, partner=self.partner) with self.assertNumQueries(7): response = self.client.get(self.list_path) assert response.status_code == 200 self.assert_response_data_valid(response, Organization.objects.all())
def test_list(self): """ Verify the endpoint returns a list of all organizations. """ OrganizationFactory.create_batch(3) with self.assertNumQueries(6): response = self.client.get(self.list_path) self.assertEqual(response.status_code, 200) self.assert_response_data_valid(response, Organization.objects.all())
def test_update_using_api(self): """ Verify endpoint successfully updates a program. """ program_data = self._program_data() response = self.client.post(self.list_path, program_data, format='json') assert response.status_code == 201 program = Program.objects.last() assert program.courses.count() == 3 assert program.authoring_organizations.count() == 3 assert program.credit_backing_organizations.count() == 3 program_detail_url = reverse('api:v1:program-detail', kwargs={'uuid': str(program.uuid)}) program.title = '{orignal_title} Test Update'.format(orignal_title=program_data['title']) program_data['status'] = 'unpublished' course_runs = CourseRunFactory.create_batch(2) course_runs = [course_run.key for course_run in course_runs] program_data['course_runs'] = program_data['course_runs'] + course_runs organizations = OrganizationFactory.create_batch(3) organizations = [organization.key for organization in organizations] program_data['authoring_organizations'] = program_data['authoring_organizations'] + organizations program_data['credit_backing_organizations'] = program_data['credit_backing_organizations'] + organizations data = json.dumps(program_data) response = self.client.patch(program_detail_url, data, content_type='application/json') assert response.status_code == 200 program = Program.objects.last() assert program.title == response.data['title'] assert program.status == response.data['status'] assert program.courses.count() == 5 assert program.authoring_organizations.count() == 6 assert program.credit_backing_organizations.count() == 6 course_runs = CourseRunFactory.create_batch(2) course_runs = [course_run.key for course_run in course_runs] course_runs.append(program_data['course_runs'][0]) program_data['course_runs'] = course_runs organizations = OrganizationFactory.create_batch(3) organizations = [organization.key for organization in organizations] organizations.append(program_data['authoring_organizations'][0]) program_data['authoring_organizations'] = organizations program_data['credit_backing_organizations'] = organizations data = json.dumps(program_data) response = self.client.patch(program_detail_url, data, content_type='application/json') assert response.status_code == 200 program = Program.objects.last() assert program.courses.count() == 3 assert program.authoring_organizations.count() == 4 assert program.credit_backing_organizations.count() == 4
def setUpClass(cls): super().setUpClass() cls.user = UserFactory(is_staff=True) first_instructor = PersonFactory(given_name="First", family_name="Instructor") second_instructor = PersonFactory(given_name="Second", family_name="Instructor") cls.instructors = [first_instructor, second_instructor] cls.organizations = OrganizationFactory.create_batch(3) cls.organization_extensions = [] for instructor in cls.instructors: PositionFactory(organization=cls.organizations[0], title="professor", person=instructor) for organization in cls.organizations: cls.organization_extensions.append( OrganizationExtensionFactory(organization=organization)) disco_course = CourseFactory( authoring_organizations=[cls.organizations[0]]) disco_course2 = CourseFactory( authoring_organizations=[cls.organizations[1]]) CourseRunFactory(course=disco_course, staff=[first_instructor]) CourseRunFactory(course=disco_course2, staff=[second_instructor]) cls.user.groups.add(cls.organization_extensions[0].group)
def setUp(self): super(AutoCompletePersonTests, self).setUp() self.user = UserFactory(is_staff=True) self.client.login(username=self.user.username, password=USER_PASSWORD) first_instructor = PersonFactory(given_name="First", family_name="Instructor") second_instructor = PersonFactory(given_name="Second", family_name="Instructor") self.instructors = [first_instructor, second_instructor] self.organizations = OrganizationFactory.create_batch(3) self.organization_extensions = [] for instructor in self.instructors: PositionFactory(organization=self.organizations[0], title="professor", person=instructor) for organization in self.organizations: org_ex = publisher_factories.OrganizationExtensionFactory( organization=organization) self.organization_extensions.append(org_ex) disco_course = CourseFactory( authoring_organizations=[self.organizations[0]]) disco_course2 = CourseFactory( authoring_organizations=[self.organizations[1]]) CourseRunFactory(course=disco_course, staff=[first_instructor]) CourseRunFactory(course=disco_course2, staff=[second_instructor]) self.user.groups.add(self.organization_extensions[0].group)
def setUp(self): super(AutoCompletePersonTests, self).setUp() self.user = UserFactory(is_staff=True) self.client.login(username=self.user.username, password=USER_PASSWORD) self.courses = factories.CourseFactory.create_batch( 3, title='Some random course title') for course in self.courses: factories.CourseRunFactory(course=course) self.organizations = OrganizationFactory.create_batch(3) self.organization_extensions = [] for organization in self.organizations: self.organization_extensions.append( factories.OrganizationExtensionFactory( organization=organization)) self.user.groups.add(self.organization_extensions[0].group) first_instructor = PersonFactory(given_name="First", family_name="Instructor") second_instructor = PersonFactory(given_name="Second", family_name="Instructor") self.instructors = [first_instructor, second_instructor] for instructor in self.instructors: PositionFactory(organization=self.organizations[0], title="professor", person=instructor)
def test_list_uuid_filter(self): """ Verify the endpoint returns a list of organizations filtered by UUID. """ organizations = OrganizationFactory.create_batch(3) # Test with a single UUID self.assert_list_uuid_filter([organizations[0]]) # Test with multiple UUIDs self.assert_list_uuid_filter(organizations)
def test_list_uuid_filter(self): """ Verify the endpoint returns a list of organizations filtered by UUID. """ organizations = OrganizationFactory.create_batch(3) # Test with a single UUID self.assert_list_uuid_filter([organizations[0]]) # Test with multiple UUIDs self.assert_list_uuid_filter(organizations)
def setUp(self): super(AutocompleteTests, self).setUp() self.user = UserFactory(is_staff=True) self.client.login(username=self.user.username, password=USER_PASSWORD) self.courses = CourseFactory.create_batch(3, title='Some random course title') for course in self.courses: CourseRunFactory(course=course) self.organizations = OrganizationFactory.create_batch(3) first_instructor = PersonFactory(given_name="First Instructor") second_instructor = PersonFactory(given_name="Second Instructor") self.instructors = [first_instructor, second_instructor]
def test_data(self): authoring_organization, crediting_organization = OrganizationFactory.create_batch(2) program = ProgramFactory(authoring_organizations=[authoring_organization], credit_backing_organizations=[crediting_organization]) # NOTE: This serializer expects SearchQuerySet results, so we run a search on the newly-created object # to generate such a result. result = SearchQuerySet().models(Program).filter(uuid=program.uuid)[0] serializer = ProgramSearchSerializer(result) expected = self._create_expected_data(program) self.assertDictEqual(serializer.data, expected)
def test_organization_autocomplete(self, admin_client): """ Verify Organization autocomplete returns the data. """ organizations = OrganizationFactory.create_batch(3) path = reverse('admin_metadata:organisation-autocomplete') response = admin_client.get(path) data = json.loads(response.content.decode('utf-8')) assert response.status_code == 200 assert len(data['results']) == 3 # Search for substrings of organization keys and names organization = organizations[0] self.assert_valid_query_result(admin_client, path, organization.key[:3], organization) self.assert_valid_query_result(admin_client, path, organization.name[:5], organization)
def test_typeahead_authoring_organizations_partial_search(self): """ Test typeahead response with partial organization matching. """ authoring_organizations = OrganizationFactory.create_batch(3) course_run = CourseRunFactory(authoring_organizations=authoring_organizations, course__partner=self.partner) program = ProgramFactory(authoring_organizations=authoring_organizations, partner=self.partner) partial_key = authoring_organizations[0].key[0:5] response = self.get_typeahead_response(partial_key) self.assertEqual(response.status_code, 200) expected = { 'course_runs': [self.serialize_course_run(course_run)], 'programs': [self.serialize_program(program)] } self.assertDictEqual(response.data, expected)
def test_data(self): authoring_organization, crediting_organization = OrganizationFactory.create_batch( 2) program = ProgramFactory( authoring_organizations=[authoring_organization], credit_backing_organizations=[crediting_organization]) # NOTE: This serializer expects SearchQuerySet results, so we run a search on the newly-created object # to generate such a result. result = SearchQuerySet().models(Program).filter(uuid=program.uuid)[0] serializer = ProgramSearchSerializer(result) expected = self._create_expected_data(program) self.assertDictEqual(serializer.data, expected)
def _program_data(self): course_runs = CourseRunFactory.create_batch(3) organizations = OrganizationFactory.create_batch(3) return { "title": "Test Program", "type": "XSeries", "status": "active", "marketing_slug": "edX-test-program", "course_runs": [course_run.key for course_run in course_runs], "min_hours_effort_per_week": 10, "max_hours_effort_per_week": 20, "authoring_organizations": [organization.key for organization in organizations], "credit_backing_organizations": [organization.key for organization in organizations], }
def test_typeahead_authoring_organizations_partial_search(self): """ Test typeahead response with partial organization matching. """ authoring_organizations = OrganizationFactory.create_batch(3) course_run = CourseRunFactory(authoring_organizations=authoring_organizations, course__partner=self.partner) program = ProgramFactory(authoring_organizations=authoring_organizations, partner=self.partner) partial_key = authoring_organizations[0].key[0:5] response = self.get_response({'q': partial_key}) self.assertEqual(response.status_code, 200) expected = { 'course_runs': [self.serialize_course_run_search(course_run)], 'programs': [self.serialize_program_search(program)] } self.assertDictEqual(response.data, expected)
def test_typeahead_multiple_authoring_organizations(self): """ Test typeahead response with multiple authoring organizations. """ title = "Design" authoring_organizations = OrganizationFactory.create_batch(3) course_run = CourseRunFactory( title=title, authoring_organizations=authoring_organizations, course__partner=self.partner ) program = ProgramFactory( title=title, authoring_organizations=authoring_organizations, status=ProgramStatus.Active, partner=self.partner ) response = self.get_typeahead_response(title) self.assertEqual(response.status_code, 200) response_data = response.json() self.assertDictEqual(response_data, {'course_runs': [self.serialize_course_run(course_run)], 'programs': [self.serialize_program(program)]})
def test_list_tag_filter(self): """ Verify the endpoint returns a list of organizations filtered by tag. """ tag = 'test-org' organizations = OrganizationFactory.create_batch(2) # If no organizations have been tagged, the endpoint should not return any data self.assert_list_tag_filter([], [tag], expected_query_count=4) # Tagged organizations should be returned organizations[0].tags.add(tag) self.assert_list_tag_filter([organizations[0]], [tag]) # The endpoint should support filtering by multiple tags. The filter should be an OR filter, meaning the results # include any organization containing at least one of the given tags. tag2 = 'another-tag' organizations[1].tags.add(tag) self.assert_list_tag_filter(Organization.objects.all(), [tag, tag2])
def test_typeahead_multiple_authoring_organizations(self): """ Test typeahead response with multiple authoring organizations. """ title = "Design" authoring_organizations = OrganizationFactory.create_batch(3) course_run = CourseRunFactory( title=title, authoring_organizations=authoring_organizations, course__partner=self.partner ) program = ProgramFactory( title=title, authoring_organizations=authoring_organizations, status=ProgramStatus.Active, partner=self.partner ) response = self.get_response({'q': title}) self.assertEqual(response.status_code, 200) response_data = response.json() self.assertDictEqual(response_data, {'course_runs': [self.serialize_course_run_search(course_run)], 'programs': [self.serialize_program_search(program)]})
def test_list_tag_filter(self): """ Verify the endpoint returns a list of organizations filtered by tag. """ tag = 'test-org' organizations = OrganizationFactory.create_batch(2) # If no organizations have been tagged, the endpoint should not return any data self.assert_list_tag_filter([], [tag], expected_query_count=3) # Tagged organizations should be returned organizations[0].tags.add(tag) self.assert_list_tag_filter([organizations[0]], [tag]) # The endpoint should support filtering by multiple tags. The filter should be an OR filter, meaning the results # include any organization containing at least one of the given tags. tag2 = 'another-tag' organizations[1].tags.add(tag) self.assert_list_tag_filter(Organization.objects.all(), [tag, tag2])
def create_program(self): organizations = OrganizationFactory.create_batch(2) person = PersonFactory() courses = CourseFactory.create_batch(3) for course in courses: CourseRunFactory.create_batch(2, course=course, staff=[person], start=datetime.now()) return ProgramFactory( courses=courses, authoring_organizations=organizations, credit_backing_organizations=organizations, corporate_endorsements=CorporateEndorsementFactory.create_batch(1), individual_endorsements=EndorsementFactory.create_batch(1), expected_learning_items=ExpectedLearningItemFactory.create_batch(1), job_outlook_items=JobOutlookItemFactory.create_batch(1), banner_image=make_image_file('test_banner.jpg'), video=VideoFactory(), order_courses_by_start_date=False, )
def test_typeahead_authoring_organizations_partial_search(self): """ Test typeahead response with partial organization matching. """ authoring_organizations = OrganizationFactory.create_batch(3) course_run = CourseRunFactory.create(course__partner=self.partner) program = ProgramFactory.create(partner=self.partner) for authoring_organization in authoring_organizations: course_run.authoring_organizations.add(authoring_organization) program.authoring_organizations.add(authoring_organization) course_run.save() program.save() partial_key = authoring_organizations[0].key[0:5] response = self.get_response({'q': partial_key}) self.assertEqual(response.status_code, 200) # This call is flaky in Travis. It is reliable locally, but occasionally in our CI environment, # this call won't contain the data for course_runs and programs. Instead of relying on the factories # we now explicitly add the authoring organizations to a course_run and program and call .save() # in order to update the search indexes. expected = { 'course_runs': [self.serialize_course_run_search(course_run)], 'programs': [self.serialize_program_search(program)] } self.assertDictEqual(response.data, expected)
def create_program(self): organizations = OrganizationFactory.create_batch(2) person = PersonFactory() courses = CourseFactory.create_batch(3) for course in courses: CourseRunFactory.create_batch(2, course=course, staff=[person], start=datetime.now()) return ProgramFactory( courses=courses, authoring_organizations=organizations, credit_backing_organizations=organizations, corporate_endorsements=CorporateEndorsementFactory.create_batch(1), individual_endorsements=EndorsementFactory.create_batch(1), expected_learning_items=ExpectedLearningItemFactory.create_batch( 1), job_outlook_items=JobOutlookItemFactory.create_batch(1), banner_image=make_image_file('test_banner.jpg'), video=VideoFactory(), order_courses_by_start_date=False, )
def test_data_multiple_authoring_organizations(self): authoring_organizations = OrganizationFactory.create_batch(3) program = ProgramFactory(authoring_organizations=authoring_organizations) serialized_program = self.serialize_program(program) expected = [org.key for org in authoring_organizations] self.assertEqual(serialized_program.data['orgs'], expected)