def test_get_program(user_drf_client, programs): """Test the view that handles a request for single Program""" program = programs[0] resp = user_drf_client.get( reverse("programs_api-detail", kwargs={"pk": program.id})) program_data = resp.json() assert_drf_json_equal(program_data, ProgramSerializer(program).data)
def test_courses_not_live_in_courses_api(client, live): """Courses should be filtered out of the courses API if not live""" course = CourseFactory.create(live=live) resp = client.get(reverse("courses_api-list")) assert resp.status_code == status.HTTP_200_OK assert_drf_json_equal(resp.json(), [CourseSerializer(course).data] if live else [])
def test_get_programs(user_drf_client, programs): """Test the view that handles requests for all Programs""" resp = user_drf_client.get(reverse("programs_api-list")) programs_data = sorted(resp.json(), key=op.itemgetter("id")) assert len(programs_data) == len(programs) for program, program_data in zip(programs, programs_data): assert_drf_json_equal(program_data, ProgramSerializer(program).data)
def test_order_status(client): """ The order status API should provide information about the order based on its unique_id. """ order = B2BOrderFactory.create() resp = client.get( reverse("b2b-order-status", kwargs={"hash": str(order.unique_id)}) ) assert resp.status_code == status.HTTP_200_OK assert_drf_json_equal( resp.json(), { "email": order.email, "num_seats": order.num_seats, "product_version": FullProductVersionSerializer( order.product_version, context={"all_runs": True} ).data, "item_price": str(order.per_item_price), "total_price": str(order.total_price), "status": order.status, "discount": None, "created_on": order.created_on, "reference_number": order.reference_number, "coupon_code": order.coupon.coupon_code if order.coupon else None, "contract_number": order.contract_number, "receipt_data": {"card_type": None, "card_number": None}, }, )
def test_course_runs_not_live_in_courses_api(client, live): """Course runs should be filtered out of the courses API if not live""" run = CourseRunFactory.create(live=live, course__live=True) ProductVersionFactory.create(product=ProductFactory(content_object=run)) resp = client.get(reverse("courses_api-list")) assert resp.status_code == status.HTTP_200_OK assert_drf_json_equal(resp.json()[0]["courseruns"], [CourseRunSerializer(run).data] if live else [])
def test_courses_not_live_in_programs_api(client, live): """Courses should be filtered out of the programs API if not live""" course = CourseFactory.create(live=live, program__live=True) ProductVersionFactory.create(product=ProductFactory( content_object=course.program)) resp = client.get(reverse("programs_api-list")) assert resp.status_code == status.HTTP_200_OK assert_drf_json_equal(resp.json()[0]["courses"], [CourseSerializer(course).data] if live else [])
def test_programs_not_live(client, live): """Programs should be filtered out if live=False""" program = ProgramFactory.create(live=live) ProductVersionFactory.create(product=ProductFactory( content_object=program)) resp = client.get(reverse("programs_api-list")) assert resp.status_code == status.HTTP_200_OK assert_drf_json_equal(resp.json(), [ProgramSerializer(program).data] if live else [])
def test_program_without_product_in_programs_api(client, has_product): """Programs should be filtered out of the programs API if they don't have an associated product""" program = ProgramFactory.create(live=True) if has_product: ProductVersionFactory.create(product=ProductFactory( content_object=program)) resp = client.get(reverse("programs_api-list")) assert resp.status_code == status.HTTP_200_OK assert_drf_json_equal( resp.json(), [ProgramSerializer(program).data] if has_product else [])
def test_course_runs_without_product_in_programs_api(client, has_product): """Regardless of whether course runs have a product, runs should **not** be filtered out of the programs API""" run = CourseRunFactory.create(live=True, course__live=True) ProductVersionFactory.create(product=ProductFactory( content_object=run.course.program)) if has_product: ProductVersionFactory.create(product=ProductFactory( content_object=run)) resp = client.get(reverse("programs_api-list")) assert resp.status_code == status.HTTP_200_OK assert_drf_json_equal(resp.json()[0]["courses"][0]["courseruns"], [CourseRunSerializer(run).data])
def test_course_runs_without_product_in_courses_api(client, has_product): """Course runs should be filtered out of the courses API if they don't have an associated product""" run = CourseRunFactory.create(live=True, course__live=True) if has_product: ProductVersionFactory.create(product=ProductFactory( content_object=run)) resp = client.get(reverse("courses_api-list")) assert resp.status_code == status.HTTP_200_OK assert_drf_json_equal( resp.json()[0]["courseruns"], [CourseRunSerializer(run).data] if has_product else [], )
def test_serialize_course_run(has_product): """Test CourseRun serialization""" faculty_names = ["Emma Jones", "Joe Smith"] course_run = CourseRunFactory.create() FacultyMembersPageFactory.create( parent=course_run.course.page, **{ f"members__{idx}__member__name": name for idx, name in enumerate(faculty_names) }, ) product_id = (ProductVersionFactory.create( product__content_object=course_run).product.id if has_product else None) course_run.refresh_from_db() data = CourseRunSerializer(course_run).data assert_drf_json_equal( data, { "title": course_run.title, "courseware_id": course_run.courseware_id, "run_tag": course_run.run_tag, "courseware_url": course_run.courseware_url, "start_date": drf_datetime(course_run.start_date), "end_date": drf_datetime(course_run.end_date), "enrollment_start": drf_datetime(course_run.enrollment_start), "enrollment_end": drf_datetime(course_run.enrollment_end), "expiration_date": drf_datetime(course_run.expiration_date), "current_price": course_run.current_price, "instructors": course_run.instructors, "id": course_run.id, "product_id": product_id, }, )
def test_serialize_program(mock_context, has_product): """Test Program serialization""" program = ProgramFactory.create() run1 = CourseRunFactory.create(course__program=program) course1 = run1.course run2 = CourseRunFactory.create(course__program=program) course2 = run2.course runs = ([run1, run2] + [CourseRunFactory.create(course=course1) for _ in range(2)] + [CourseRunFactory.create(course=course2) for _ in range(2)]) faculty_names = ["Teacher 1", "Teacher 2"] FacultyMembersPageFactory.create( parent=program.page, **{ f"members__{idx}__member__name": name for idx, name in enumerate(faculty_names) }, ) if has_product: ProductVersionFactory.create(product__content_object=program) topics = [ CourseTopic.objects.create(name=f"topic{num}") for num in range(3) ] course1.topics.set([topics[0], topics[1]]) course2.topics.set([topics[1], topics[2]]) data = ProgramSerializer(instance=program, context=mock_context).data assert_drf_json_equal( data, { "title": program.title, "readable_id": program.readable_id, "id": program.id, "description": program.page.description, "courses": [ CourseSerializer(instance=course, context={ **mock_context, "filter_products": False }).data for course in [course1, course2] ], "thumbnail_url": f"http://localhost:8053{program.page.thumbnail_image.file.url}", "current_price": program.current_price, "start_date": sorted(runs, key=lambda run: run.start_date) [0].start_date.strftime(datetime_format), "end_date": sorted(runs, key=lambda run: run.end_date)[-1].end_date.strftime( datetime_format), "enrollment_start": sorted(runs, key=lambda run: run.enrollment_start) [0].enrollment_start.strftime(datetime_format), "url": f"http://localhost{program.page.get_url()}", "instructors": [{ "name": name } for name in faculty_names], "topics": [{ "name": topic.name } for topic in topics], }, )
def test_serialize_course(mock_context, is_anonymous, all_runs): """Test Course serialization""" now = datetime.now(tz=pytz.UTC) if is_anonymous: mock_context["request"].user = AnonymousUser() if all_runs: mock_context["all_runs"] = True user = mock_context["request"].user course_run = CourseRunFactory.create(course__no_program=True, live=True) course = course_run.course topic = "a course topic" course.topics.set([CourseTopic.objects.create(name=topic)]) # Create expired, enrollment_ended, future, and enrolled course runs CourseRunFactory.create(course=course, end_date=now - timedelta(1), live=True) CourseRunFactory.create(course=course, enrollment_end=now - timedelta(1), live=True) CourseRunFactory.create(course=course, enrollment_start=now + timedelta(1), live=True) enrolled_run = CourseRunFactory.create(course=course, live=True) unexpired_runs = [enrolled_run, course_run] CourseRunEnrollmentFactory.create(run=enrolled_run, **({} if is_anonymous else { "user": user })) # create products for all courses so the serializer shows them for run in CourseRun.objects.all(): ProductVersionFactory.create(product__content_object=run) data = CourseSerializer(instance=course, context=mock_context).data if all_runs or is_anonymous: expected_runs = unexpired_runs else: expected_runs = [course_run] assert_drf_json_equal( data, { "title": course.title, "description": course.page.description, "readable_id": course.readable_id, "id": course.id, "courseruns": [ CourseRunSerializer(run).data for run in sorted(expected_runs, key=lambda run: run.start_date) ], "thumbnail_url": f"http://localhost:8053{course.page.thumbnail_image.file.url}", "next_run_id": course.first_unexpired_run.id, "topics": [{ "name": topic }], }, )
def test_assert_drf_json_equall(): """Asserts that objects are equal in JSON""" assert_drf_json_equal({"a": 1}, {"a": 1}) assert_drf_json_equal(2, 2) assert_drf_json_equal([2], [2])