|
12 | 12 | from django.urls import reverse |
13 | 13 | from rest_framework import status |
14 | 14 | from rest_framework.request import Request |
| 15 | +from rest_framework.test import APIClient |
15 | 16 |
|
16 | 17 | from b2b.api import create_contract_run |
17 | 18 | from b2b.factories import ContractPageFactory, OrganizationPageFactory |
|
29 | 30 | ) |
30 | 31 | from courses.views.v2 import CourseFilterSet, Pagination |
31 | 32 | from main.test_utils import assert_drf_json_equal, duplicate_queries_check |
| 33 | +from users.factories import UserFactory |
32 | 34 |
|
33 | 35 | pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures("raise_nplusone")] |
34 | 36 |
|
@@ -259,23 +261,50 @@ def test_get_course( |
259 | 261 |
|
260 | 262 |
|
261 | 263 | @pytest.mark.django_db |
262 | | -def test_filter_with_org_id_returns_contracted_course(user_drf_client): |
| 264 | +def test_filter_with_org_id_returns_contracted_course(): |
263 | 265 | org = OrganizationPageFactory(name="Test Org") |
264 | 266 | contract = ContractPageFactory(organization=org, active=True) |
| 267 | + user = UserFactory() |
| 268 | + user.b2b_contracts.add(contract) |
265 | 269 | course = CourseFactory(title="Contracted Course") |
266 | 270 | create_contract_run(contract, course) |
267 | 271 |
|
| 272 | + client = APIClient() |
| 273 | + client.force_authenticate(user=user) |
| 274 | + |
268 | 275 | unrelated_course = Course.objects.create(title="Other Course") |
269 | 276 | CourseRunFactory(course=unrelated_course) |
270 | 277 |
|
271 | 278 | url = reverse("v2:courses_api-list") |
272 | | - response = user_drf_client.get(url, {"org_id": org.id}) |
| 279 | + response = client.get(url, {"org_id": org.id}) |
273 | 280 |
|
274 | 281 | titles = [result["title"] for result in response.data["results"]] |
275 | 282 | assert course.title in titles |
276 | 283 | assert unrelated_course.title not in titles |
277 | 284 |
|
278 | 285 |
|
| 286 | +@pytest.mark.django_db |
| 287 | +def test_filter_with_org_id_user_not_associated_with_org_returns_no_courses(): |
| 288 | + org = OrganizationPageFactory(name="Test Org") |
| 289 | + user = UserFactory() |
| 290 | + contract = ContractPageFactory(organization=org, active=True) |
| 291 | + course = CourseFactory(title="Contracted Course") |
| 292 | + create_contract_run(contract, course) |
| 293 | + |
| 294 | + client = APIClient() |
| 295 | + client.force_authenticate(user=user) |
| 296 | + |
| 297 | + unrelated_course = Course.objects.create(title="Other Course") |
| 298 | + CourseRunFactory(course=unrelated_course) |
| 299 | + |
| 300 | + url = reverse("v2:courses_api-list") |
| 301 | + response = client.get(url, {"org_id": org.id}) |
| 302 | + |
| 303 | + titles = [result["title"] for result in response.data["results"]] |
| 304 | + assert course.title not in titles |
| 305 | + assert unrelated_course.title not in titles |
| 306 | + |
| 307 | + |
279 | 308 | @pytest.mark.django_db |
280 | 309 | def test_filter_without_org_id_authenticated_user(user_drf_client): |
281 | 310 | course_with_contract = CourseFactory(title="Contract Course") |
|
0 commit comments