def test_program_page_serializer(self): """ Test program page serializer """ page = ProgramPageFactory.create() courses = CourseFactory.create_batch(6, program=page.program) faculty = FacultyFactory.create_batch(3, program_page=page) elective_set = ElectivesSet.objects.create(program=page.program, required_number=2, title="Elective") # Add 3 courses to elective set - For required 2 of 3 scenario for elective_course in courses[:3]: ElectiveCourse.objects.create(electives_set=elective_set, course=elective_course) data = ProgramPageSerializer(page).data data['faculty'] = sorted(data['faculty'], key=lambda member: member['name']) assert data == { "id": page.program.id, "title": page.title, "slug": ProgramPageSerializer().get_slug(page), "faculty": FacultySerializer(sorted(faculty, key=lambda member: member.name), many=True).data, # Only core courses (Not associated with electives) "courses": CourseSerializer(courses[3:], many=True).data, "electives_sets": ElectivesSetSerializer([elective_set], many=True).data, }
def get_program_page_context(programpage, request): """ Get context for the program page""" from cms.serializers import ProgramPageSerializer courses_query = (programpage.program.course_set.all()) js_settings = { "gaTrackingID": settings.GA_TRACKING_ID, "host": webpack_dev_server_host(request), "environment": settings.ENVIRONMENT, "sentry_dsn": settings.SENTRY_DSN, "release_version": settings.VERSION, "user": serialize_maybe_user(request.user), "program": ProgramPageSerializer(programpage).data, } username = get_social_username(request.user) context = super(ProgramPage, programpage).get_context(request) context["is_staff"] = has_role(request.user, [Staff.ROLE_ID, Instructor.ROLE_ID]) context["is_public"] = True context["has_zendesk_widget"] = True context["google_maps_api"] = False context["authenticated"] = not request.user.is_anonymous context["username"] = username context["js_settings_json"] = json.dumps(js_settings) context["title"] = programpage.title context["courses"] = courses_query context["ga_tracking_id"] = programpage.program.ga_tracking_id return context
def test_program_page_serializer(self): """ Test program page serializer """ page = ProgramPageFactory.create() courses = CourseFactory.create_batch(3, program=page.program) faculty = FacultyFactory.create_batch(3, program_page=page) data = ProgramPageSerializer(page).data data['faculty'] = sorted(data['faculty'], key=lambda member: member['name']) assert data == { "id": page.program.id, "title": page.title, "slug": ProgramPageSerializer().get_slug(page), "faculty": FacultySerializer(sorted(faculty, key=lambda member: member.name), many=True).data, "courses": CourseSerializer(courses, many=True).data, }
def test_context(self): """ Assert context values for logged in user """ with mute_signals(post_save): profile = ProfileFactory.create() self.client.force_login(profile.user) # ProgramFaculty and ProgramCourse are asserted via ProgramPageSerializer below FacultyFactory.create_batch(3, program_page=self.program_page) courses = self.program_page.program.course_set.all() ProgramCourseFactory.create_batch(len(courses), program_page=self.program_page, course=Iterator(courses)) ga_tracking_id = FuzzyText().fuzz() with self.settings( GA_TRACKING_ID=ga_tracking_id, ENVIRONMENT='environment', VERSION='version', ): response = self.client.get(self.program_page.url) assert response.context['authenticated'] is True assert response.context['username'] is None assert response.context['title'] == self.program_page.title assert response.context['is_public'] is True assert response.context['has_zendesk_widget'] is True assert response.context['is_staff'] is False self.assertContains(response, 'Share this page') js_settings = json.loads(response.context['js_settings_json']) assert js_settings == { 'gaTrackingID': ga_tracking_id, 'environment': 'environment', 'sentry_dsn': None, 'host': 'testserver', 'release_version': 'version', 'user': serialize_maybe_user(profile.user), 'program': ProgramPageSerializer(self.program_page).data, }