|
6 | 6 | import random |
7 | 7 |
|
8 | 8 | import pytest |
| 9 | +from django.contrib.auth.models import AnonymousUser |
9 | 10 | from django.db import connection |
| 11 | +from django.test.client import RequestFactory |
10 | 12 | from django.urls import reverse |
11 | 13 | from rest_framework import status |
| 14 | +from rest_framework.request import Request |
12 | 15 |
|
13 | | -from courses.factories import DepartmentFactory |
| 16 | +from b2b.api import create_contract_run |
| 17 | +from b2b.factories import ContractPageFactory, OrganizationPageFactory |
| 18 | +from courses.factories import CourseFactory, CourseRunFactory, DepartmentFactory |
14 | 19 | from courses.models import Course, Program |
15 | 20 | from courses.serializers.v2.courses import CourseWithCourseRunsSerializer |
16 | 21 | from courses.serializers.v2.departments import ( |
|
22 | 27 | num_queries_from_department, |
23 | 28 | num_queries_from_programs, |
24 | 29 | ) |
25 | | -from courses.views.v2 import Pagination |
| 30 | +from courses.views.v2 import CourseFilterSet, Pagination |
26 | 31 | from main.test_utils import assert_drf_json_equal, duplicate_queries_check |
27 | 32 |
|
28 | 33 | pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures("raise_nplusone")] |
@@ -251,3 +256,59 @@ def test_get_course( |
251 | 256 | CourseWithCourseRunsSerializer(instance=course, context=mock_context).data |
252 | 257 | ) |
253 | 258 | assert_drf_json_equal(course_data, course_from_fixture, ignore_order=True) |
| 259 | + |
| 260 | + |
| 261 | +@pytest.mark.django_db |
| 262 | +def test_filter_with_org_id_returns_contracted_course(user_drf_client): |
| 263 | + org = OrganizationPageFactory(name="Test Org") |
| 264 | + contract = ContractPageFactory(organization=org, active=True) |
| 265 | + course = CourseFactory(title="Contracted Course") |
| 266 | + create_contract_run(contract, course) |
| 267 | + |
| 268 | + unrelated_course = Course.objects.create(title="Other Course") |
| 269 | + CourseRunFactory(course=unrelated_course) |
| 270 | + |
| 271 | + url = reverse("v2:courses_api-list") |
| 272 | + response = user_drf_client.get(url, {"org_id": org.id}) |
| 273 | + |
| 274 | + titles = [result["title"] for result in response.data["results"]] |
| 275 | + assert course.title in titles |
| 276 | + assert unrelated_course.title not in titles |
| 277 | + |
| 278 | + |
| 279 | +@pytest.mark.django_db |
| 280 | +def test_filter_without_org_id_authenticated_user(user_drf_client): |
| 281 | + course_with_contract = CourseFactory(title="Contract Course") |
| 282 | + contract = ContractPageFactory(active=True) |
| 283 | + CourseRunFactory(course=course_with_contract, b2b_contract=contract) |
| 284 | + |
| 285 | + course_no_contract = CourseFactory(title="No Contract Course") |
| 286 | + CourseRunFactory(course=course_no_contract, b2b_contract=None) |
| 287 | + |
| 288 | + url = reverse("v2:courses_api-list") |
| 289 | + response = user_drf_client.get(url) |
| 290 | + |
| 291 | + titles = [result["title"] for result in response.data["results"]] |
| 292 | + |
| 293 | + assert course_no_contract.title in titles |
| 294 | + assert course_with_contract.title in titles |
| 295 | + |
| 296 | + |
| 297 | +def test_filter_anonymous_user_sees_no_contracted_runs(): |
| 298 | + course_with_contract = CourseFactory(title="Hidden Course") |
| 299 | + contract = ContractPageFactory(active=True) |
| 300 | + CourseRunFactory(course=course_with_contract, b2b_contract=contract) |
| 301 | + |
| 302 | + course_no_contract = CourseFactory(title="Visible Course") |
| 303 | + CourseRunFactory(course=course_no_contract) |
| 304 | + rf = RequestFactory() |
| 305 | + request = rf.get(reverse("v2:courses_api-list")) |
| 306 | + request.user = AnonymousUser() |
| 307 | + drf_request = Request(request) |
| 308 | + queryset = Course.objects.all() |
| 309 | + filtered = CourseFilterSet( |
| 310 | + data=drf_request.query_params, request=drf_request, queryset=queryset |
| 311 | + ).qs |
| 312 | + |
| 313 | + assert course_no_contract in filtered |
| 314 | + assert course_with_contract not in filtered |
0 commit comments