|
166 | 166 | 'bulk_beta_modify_access', |
167 | 167 | 'calculate_grades_csv', |
168 | 168 | 'change_due_date', |
| 169 | + 'instructor_api_v2:change_due_date', |
169 | 170 | 'export_ora2_data', |
170 | 171 | 'export_ora2_submission_files', |
171 | 172 | 'export_ora2_summary', |
@@ -4494,6 +4495,180 @@ def test_show_student_extensions(self): |
4494 | 4495 | 'title': (f'Due date extensions for {self.user1.profile.name} ({self.user1.username})')} |
4495 | 4496 |
|
4496 | 4497 |
|
| 4498 | +class TestChangeDueDateV2(SharedModuleStoreTestCase, LoginEnrollmentTestCase): |
| 4499 | + """ |
| 4500 | + Tests for the V2 due date extension API endpoint. |
| 4501 | + """ |
| 4502 | + MODULESTORE = TEST_DATA_SPLIT_MODULESTORE |
| 4503 | + |
| 4504 | + @classmethod |
| 4505 | + def setUpClass(cls): |
| 4506 | + super().setUpClass() |
| 4507 | + cls.course = CourseFactory.create() |
| 4508 | + cls.due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=UTC) |
| 4509 | + |
| 4510 | + with cls.store.bulk_operations(cls.course.id, emit_signals=False): |
| 4511 | + cls.week1 = BlockFactory.create(due=cls.due) |
| 4512 | + cls.week2 = BlockFactory.create(due=cls.due) |
| 4513 | + cls.week3 = BlockFactory.create() # No due date |
| 4514 | + cls.course.children = [ |
| 4515 | + str(cls.week1.location), |
| 4516 | + str(cls.week2.location), |
| 4517 | + str(cls.week3.location) |
| 4518 | + ] |
| 4519 | + cls.homework = BlockFactory.create( |
| 4520 | + parent_location=cls.week1.location, |
| 4521 | + due=cls.due |
| 4522 | + ) |
| 4523 | + cls.week1.children = [str(cls.homework.location)] |
| 4524 | + |
| 4525 | + def setUp(self): |
| 4526 | + """ |
| 4527 | + Fixtures. |
| 4528 | + """ |
| 4529 | + super().setUp() |
| 4530 | + |
| 4531 | + self.user1 = UserFactory.create() |
| 4532 | + self.user2 = UserFactory.create() |
| 4533 | + |
| 4534 | + # Create StudentModule objects for tracking student progress |
| 4535 | + StudentModule( |
| 4536 | + state='{}', |
| 4537 | + student_id=self.user1.id, |
| 4538 | + course_id=self.course.id, |
| 4539 | + module_state_key=self.week1.location).save() |
| 4540 | + StudentModule( |
| 4541 | + state='{}', |
| 4542 | + student_id=self.user1.id, |
| 4543 | + course_id=self.course.id, |
| 4544 | + module_state_key=self.homework.location).save() |
| 4545 | + StudentModule( |
| 4546 | + state='{}', |
| 4547 | + student_id=self.user2.id, |
| 4548 | + course_id=self.course.id, |
| 4549 | + module_state_key=self.week1.location).save() |
| 4550 | + StudentModule( |
| 4551 | + state='{}', |
| 4552 | + student_id=self.user2.id, |
| 4553 | + course_id=self.course.id, |
| 4554 | + module_state_key=self.homework.location).save() |
| 4555 | + |
| 4556 | + CourseEnrollmentFactory.create(user=self.user1, course_id=self.course.id) |
| 4557 | + CourseEnrollmentFactory.create(user=self.user2, course_id=self.course.id) |
| 4558 | + self.instructor = InstructorFactory(course_key=self.course.id) |
| 4559 | + self.client.login(username=self.instructor.username, password=self.TEST_PASSWORD) |
| 4560 | + extract_dates(None, self.course.id) |
| 4561 | + |
| 4562 | + def test_change_due_date_v2_success(self): |
| 4563 | + """Test successful due date change using V2 API endpoint""" |
| 4564 | + url = reverse('instructor_api_v2:change_due_date', kwargs={'course_id': str(self.course.id)}) |
| 4565 | + due_date = datetime.datetime(2013, 12, 30, tzinfo=UTC) |
| 4566 | + response = self.client.post(url, json.dumps({ |
| 4567 | + 'email_or_username': self.user1.username, |
| 4568 | + 'block_id': str(self.homework.location), |
| 4569 | + 'due_datetime': '12/30/2013 00:00', |
| 4570 | + 'reason': 'Testing V2 API.' |
| 4571 | + }), content_type='application/json') |
| 4572 | + |
| 4573 | + assert response.status_code == 200, response.content |
| 4574 | + response_data = json.loads(response.content.decode('utf-8')) |
| 4575 | + assert 'Successfully changed due date for learner' in response_data['message'] |
| 4576 | + |
| 4577 | + assert get_extended_due(self.course, self.homework, self.user1) == due_date |
| 4578 | + |
| 4579 | + def test_change_due_date_v2_with_email(self): |
| 4580 | + """Test due date change using email instead of username""" |
| 4581 | + url = reverse('instructor_api_v2:change_due_date', kwargs={'course_id': str(self.course.id)}) |
| 4582 | + due_date = datetime.datetime(2013, 12, 30, tzinfo=UTC) |
| 4583 | + response = self.client.post(url, json.dumps({ |
| 4584 | + 'email_or_username': self.user1.email, |
| 4585 | + 'block_id': str(self.homework.location), |
| 4586 | + 'due_datetime': '12/30/2013 00:00', |
| 4587 | + 'reason': 'Testing V2 API with email.' |
| 4588 | + }), content_type='application/json') |
| 4589 | + |
| 4590 | + assert response.status_code == 200, response.content |
| 4591 | + response_data = json.loads(response.content.decode('utf-8')) |
| 4592 | + assert 'Successfully changed due date for learner' in response_data['message'] |
| 4593 | + |
| 4594 | + assert get_extended_due(self.course, self.homework, self.user1) == due_date |
| 4595 | + |
| 4596 | + def test_change_due_date_v2_invalid_user(self): |
| 4597 | + """Test error handling for invalid user""" |
| 4598 | + url = reverse('instructor_api_v2:change_due_date', kwargs={'course_id': str(self.course.id)}) |
| 4599 | + response = self.client.post(url, json.dumps({ |
| 4600 | + 'email_or_username': 'nonexistent@example.com', |
| 4601 | + 'block_id': str(self.homework.location), |
| 4602 | + 'due_datetime': '12/30/2013 00:00' |
| 4603 | + }), content_type='application/json') |
| 4604 | + |
| 4605 | + assert response.status_code == 400, response.content |
| 4606 | + response_data = json.loads(response.content.decode('utf-8')) |
| 4607 | + assert 'Invalid learner identifier' in response_data['error']['email_or_username'][0] |
| 4608 | + |
| 4609 | + def test_change_due_date_v2_invalid_block(self): |
| 4610 | + """Test error handling for invalid block location""" |
| 4611 | + url = reverse('instructor_api_v2:change_due_date', kwargs={'course_id': str(self.course.id)}) |
| 4612 | + # Invalid block location should cause an exception (500 error) |
| 4613 | + with self.assertRaises(Exception): |
| 4614 | + self.client.post(url, json.dumps({ |
| 4615 | + 'email_or_username': self.user1.username, |
| 4616 | + 'block_id': 'i4x://invalid/block/location', |
| 4617 | + 'due_datetime': '12/30/2013 00:00' |
| 4618 | + }), content_type='application/json') |
| 4619 | + |
| 4620 | + def test_change_due_date_v2_invalid_date_format(self): |
| 4621 | + """Test error handling for invalid date format""" |
| 4622 | + url = reverse('instructor_api_v2:change_due_date', kwargs={'course_id': str(self.course.id)}) |
| 4623 | + # V2 API accepts form data as well |
| 4624 | + response = self.client.post(url, { |
| 4625 | + 'email_or_username': self.user1.username, |
| 4626 | + 'block_id': self.homework.location, |
| 4627 | + 'due_datetime': 'invalid-date-format' |
| 4628 | + }) |
| 4629 | + |
| 4630 | + self.assertEqual(response.status_code, 400) |
| 4631 | + |
| 4632 | + def test_change_due_date_v2_missing_fields(self): |
| 4633 | + """Test error handling for missing required fields""" |
| 4634 | + url = reverse('instructor_api_v2:change_due_date', kwargs={'course_id': str(self.course.id)}) |
| 4635 | + # V2 API accepts form data |
| 4636 | + response = self.client.post(url, { |
| 4637 | + 'email_or_username': self.user1.username, |
| 4638 | + # Missing 'block_id' and 'due_datetime' |
| 4639 | + }) |
| 4640 | + |
| 4641 | + self.assertEqual(response.status_code, 400) |
| 4642 | + |
| 4643 | + def test_change_due_date_v2_unenrolled_user(self): |
| 4644 | + """Test error handling for user not enrolled in course""" |
| 4645 | + unenrolled_user = UserFactory.create() |
| 4646 | + url = reverse('instructor_api_v2:change_due_date', kwargs={'course_id': str(self.course.id)}) |
| 4647 | + # V2 API accepts form data |
| 4648 | + response = self.client.post(url, { |
| 4649 | + 'email_or_username': unenrolled_user.username, |
| 4650 | + 'block_id': self.homework.location, |
| 4651 | + 'due_datetime': '12/30/2013 00:00' |
| 4652 | + }) |
| 4653 | + |
| 4654 | + self.assertEqual(response.status_code, 400) |
| 4655 | + |
| 4656 | + def test_change_due_date_v2_json_content_type(self): |
| 4657 | + """Test that V2 API works with both JSON and form data""" |
| 4658 | + url = reverse('instructor_api_v2:change_due_date', kwargs={'course_id': str(self.course.id)}) |
| 4659 | + # Send as form data instead of JSON |
| 4660 | + response = self.client.post(url, { |
| 4661 | + 'email_or_username': self.user1.username, |
| 4662 | + 'block_id': self.homework.location, |
| 4663 | + 'due_datetime': '12/30/2013 00:00' |
| 4664 | + }) |
| 4665 | + |
| 4666 | + # The V2 endpoint works with form data and should succeed |
| 4667 | + self.assertEqual(response.status_code, 200) |
| 4668 | + response_data = json.loads(response.content.decode('utf-8')) |
| 4669 | + self.assertIn('Successfully changed due date for learner', response_data['message']) |
| 4670 | + |
| 4671 | + |
4497 | 4672 | class TestDueDateExtensionsDeletedDate(ModuleStoreTestCase, LoginEnrollmentTestCase): |
4498 | 4673 | """ |
4499 | 4674 | Tests for deleting due date extensions |
|
0 commit comments