|
15 | 15 | from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order |
16 | 16 | from xmodule.modulestore.tests.factories import ToyCourseFactory # lint-amnesty, pylint: disable=wrong-import-order |
17 | 17 |
|
18 | | -from .. import cohorts |
19 | | -from .helpers import CohortFactory |
| 18 | +from openedx.core.djangoapps.course_groups import cohorts |
| 19 | +from openedx.core.djangoapps.course_groups.views import link_cohort_to_partition_group |
| 20 | +from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory |
20 | 21 |
|
21 | 22 | USERNAME = 'honor' |
22 | 23 | USER_MAIL = 'honor@example.com' |
@@ -458,3 +459,151 @@ def test_add_users_csv(self, is_staff, payload, status): |
458 | 459 | response = self.client.post(path=path, |
459 | 460 | data={'uploaded-file': file_pointer}) |
460 | 461 | assert response.status_code == status |
| 462 | + |
| 463 | + def test_post_cohort_with_group_id(self): |
| 464 | + """ |
| 465 | + Test creating a cohort with group_id and user_partition_id. |
| 466 | + """ |
| 467 | + path = reverse('api_cohorts:cohort_handler', kwargs={'course_key_string': self.course_str}) |
| 468 | + self.client.login(username=self.staff_user.username, password=self.password) |
| 469 | + |
| 470 | + payload = json.dumps({ |
| 471 | + 'name': 'TestCohort', |
| 472 | + 'assignment_type': 'manual', |
| 473 | + 'group_id': 1, |
| 474 | + 'user_partition_id': 50 |
| 475 | + }) |
| 476 | + response = self.client.post(path=path, data=payload, content_type='application/json') |
| 477 | + assert response.status_code == 200 |
| 478 | + |
| 479 | + data = json.loads(response.content.decode('utf-8')) |
| 480 | + assert data['name'] == 'TestCohort' |
| 481 | + assert data['assignment_type'] == 'manual' |
| 482 | + assert data['group_id'] == 1 |
| 483 | + assert data['user_partition_id'] == 50 |
| 484 | + assert data['user_count'] == 0 |
| 485 | + assert 'id' in data |
| 486 | + |
| 487 | + def test_post_cohort_with_group_id_missing_partition_id(self): |
| 488 | + """ |
| 489 | + Test that creating a cohort with group_id but without user_partition_id returns an error. |
| 490 | + """ |
| 491 | + path = reverse('api_cohorts:cohort_handler', kwargs={'course_key_string': self.course_str}) |
| 492 | + self.client.login(username=self.staff_user.username, password=self.password) |
| 493 | + |
| 494 | + payload = json.dumps({ |
| 495 | + 'name': 'TestCohort', |
| 496 | + 'assignment_type': 'manual', |
| 497 | + 'group_id': 1 |
| 498 | + }) |
| 499 | + response = self.client.post(path=path, data=payload, content_type='application/json') |
| 500 | + assert response.status_code == 400 |
| 501 | + |
| 502 | + data = json.loads(response.content.decode('utf-8')) |
| 503 | + assert data['developer_message'] == 'If group_id is specified, user_partition_id must also be specified.' |
| 504 | + assert data['error_code'] == 'missing-user-partition-id' |
| 505 | + |
| 506 | + def test_patch_cohort_set_group_id(self): |
| 507 | + """ |
| 508 | + Test updating a cohort to set group_id and user_partition_id. |
| 509 | + """ |
| 510 | + cohort = cohorts.add_cohort(self.course_key, "TestCohort", "manual") |
| 511 | + path = reverse( |
| 512 | + 'api_cohorts:cohort_handler', |
| 513 | + kwargs={'course_key_string': self.course_str, 'cohort_id': cohort.id} |
| 514 | + ) |
| 515 | + self.client.login(username=self.staff_user.username, password=self.password) |
| 516 | + |
| 517 | + payload = json.dumps({ |
| 518 | + 'group_id': 2, |
| 519 | + 'user_partition_id': 50 |
| 520 | + }) |
| 521 | + response = self.client.patch(path=path, data=payload, content_type='application/json') |
| 522 | + assert response.status_code == 204 |
| 523 | + |
| 524 | + # Verify by fetching the cohort |
| 525 | + response = self.client.get(path=path) |
| 526 | + data = json.loads(response.content.decode('utf-8')) |
| 527 | + assert data['id'] == cohort.id |
| 528 | + assert data['name'] == 'TestCohort' |
| 529 | + assert data['assignment_type'] == 'manual' |
| 530 | + assert data['group_id'] == 2 |
| 531 | + assert data['user_partition_id'] == 50 |
| 532 | + |
| 533 | + def test_patch_cohort_remove_group_id(self): |
| 534 | + """ |
| 535 | + Test updating a cohort to remove the group_id association by setting it to null. |
| 536 | + """ |
| 537 | + cohort = cohorts.add_cohort(self.course_key, "TestCohort", "manual") |
| 538 | + link_cohort_to_partition_group(cohort, 50, 1) |
| 539 | + |
| 540 | + path = reverse( |
| 541 | + 'api_cohorts:cohort_handler', |
| 542 | + kwargs={'course_key_string': self.course_str, 'cohort_id': cohort.id} |
| 543 | + ) |
| 544 | + self.client.login(username=self.staff_user.username, password=self.password) |
| 545 | + |
| 546 | + # Verify the cohort has a group_id |
| 547 | + response = self.client.get(path=path) |
| 548 | + data = json.loads(response.content.decode('utf-8')) |
| 549 | + assert data['id'] == cohort.id |
| 550 | + assert data['name'] == 'TestCohort' |
| 551 | + assert data['group_id'] == 1 |
| 552 | + assert data['user_partition_id'] == 50 |
| 553 | + |
| 554 | + # Remove the group_id by setting it to null |
| 555 | + payload = json.dumps({'group_id': None}) |
| 556 | + response = self.client.patch(path=path, data=payload, content_type='application/json') |
| 557 | + assert response.status_code == 204 |
| 558 | + |
| 559 | + # Verify the group_id was removed but other fields unchanged |
| 560 | + response = self.client.get(path=path) |
| 561 | + data = json.loads(response.content.decode('utf-8')) |
| 562 | + assert data['id'] == cohort.id |
| 563 | + assert data['name'] == 'TestCohort' |
| 564 | + assert data['assignment_type'] == 'manual' |
| 565 | + assert data['group_id'] is None |
| 566 | + assert data['user_partition_id'] is None |
| 567 | + |
| 568 | + def test_patch_cohort_with_group_id_missing_partition_id(self): |
| 569 | + """ |
| 570 | + Test that updating a cohort with group_id but without user_partition_id returns an error. |
| 571 | + """ |
| 572 | + cohort = cohorts.add_cohort(self.course_key, "TestCohort", "manual") |
| 573 | + path = reverse( |
| 574 | + 'api_cohorts:cohort_handler', |
| 575 | + kwargs={'course_key_string': self.course_str, 'cohort_id': cohort.id} |
| 576 | + ) |
| 577 | + self.client.login(username=self.staff_user.username, password=self.password) |
| 578 | + |
| 579 | + payload = json.dumps({'group_id': 2}) |
| 580 | + response = self.client.patch(path=path, data=payload, content_type='application/json') |
| 581 | + assert response.status_code == 400 |
| 582 | + |
| 583 | + data = json.loads(response.content.decode('utf-8')) |
| 584 | + assert data['developer_message'] == 'If group_id is specified, user_partition_id must also be specified.' |
| 585 | + assert data['error_code'] == 'missing-user-partition-id' |
| 586 | + |
| 587 | + def test_patch_cohort_with_name_only(self): |
| 588 | + """ |
| 589 | + Test that PATCH with only name is now valid (previously required assignment_type too). |
| 590 | + """ |
| 591 | + cohort = cohorts.add_cohort(self.course_key, "OldName", "manual") |
| 592 | + path = reverse( |
| 593 | + 'api_cohorts:cohort_handler', |
| 594 | + kwargs={'course_key_string': self.course_str, 'cohort_id': cohort.id} |
| 595 | + ) |
| 596 | + self.client.login(username=self.staff_user.username, password=self.password) |
| 597 | + |
| 598 | + payload = json.dumps({'name': 'NewName'}) |
| 599 | + response = self.client.patch(path=path, data=payload, content_type='application/json') |
| 600 | + assert response.status_code == 204 |
| 601 | + |
| 602 | + # Verify the name was updated and other fields unchanged |
| 603 | + response = self.client.get(path=path) |
| 604 | + data = json.loads(response.content.decode('utf-8')) |
| 605 | + assert data['id'] == cohort.id |
| 606 | + assert data['name'] == 'NewName' |
| 607 | + assert data['assignment_type'] == 'manual' |
| 608 | + assert data['group_id'] is None |
| 609 | + assert data['user_partition_id'] is None |
0 commit comments