|
9 | 9 | import mock
|
10 | 10 | import quopri
|
11 | 11 |
|
| 12 | +from dataclasses import dataclass |
| 13 | + |
12 | 14 | from django.conf import settings
|
13 | 15 | from django.urls import reverse as urlreverse
|
14 | 16 | from django.utils import timezone
|
| 17 | +from django.test.utils import override_settings |
15 | 18 |
|
16 | 19 | import debug # pyflakes:ignore
|
17 | 20 |
|
|
20 | 23 | from ietf.doc.utils import add_state_change_event
|
21 | 24 | from ietf.group.factories import GroupFactory
|
22 | 25 | from ietf.person.models import Person
|
23 |
| -from ietf.sync import iana, rfceditor |
| 26 | +from ietf.sync import iana, rfceditor, tasks |
24 | 27 | from ietf.utils.mail import outbox, empty_outbox
|
25 | 28 | from ietf.utils.test_utils import login_testing_unauthorized
|
26 | 29 | from ietf.utils.test_utils import TestCase
|
@@ -672,3 +675,90 @@ def test_rfceditor_undo(self):
|
672 | 675 |
|
673 | 676 | e.content_type.model_class().objects.create(**json.loads(e.json))
|
674 | 677 | self.assertTrue(StateDocEvent.objects.filter(desc="First", doc=draft))
|
| 678 | + |
| 679 | + |
| 680 | +class TaskTests(TestCase): |
| 681 | + @override_settings( |
| 682 | + RFC_EDITOR_INDEX_URL="https://rfc-editor.example.com/index/", |
| 683 | + RFC_EDITOR_ERRATA_JSON_URL="https://rfc-editor.example.com/errata/", |
| 684 | + ) |
| 685 | + @mock.patch("ietf.sync.tasks.update_docs_from_rfc_index") |
| 686 | + @mock.patch("ietf.sync.tasks.parse_index") |
| 687 | + @mock.patch("ietf.sync.tasks.requests.get") |
| 688 | + def test_rfc_editor_index_update_task( |
| 689 | + self, requests_get_mock, parse_index_mock, update_docs_mock |
| 690 | + ) -> None: # the annotation here prevents mypy from complaining about annotation-unchecked |
| 691 | + """rfc_editor_index_update_task calls helpers correctly |
| 692 | + |
| 693 | + This tests that data flow is as expected. Assumes the individual helpers are |
| 694 | + separately tested to function correctly. |
| 695 | + """ |
| 696 | + @dataclass |
| 697 | + class MockIndexData: |
| 698 | + """Mock index item that claims to be a specified length""" |
| 699 | + length: int |
| 700 | + |
| 701 | + def __len__(self): |
| 702 | + return self.length |
| 703 | + |
| 704 | + @dataclass |
| 705 | + class MockResponse: |
| 706 | + """Mock object that contains text and json() that claims to be a specified length""" |
| 707 | + text: str |
| 708 | + json_length: int = 0 |
| 709 | + |
| 710 | + def json(self): |
| 711 | + return MockIndexData(length=self.json_length) |
| 712 | + |
| 713 | + # Response objects |
| 714 | + index_response = MockResponse(text="this is the index") |
| 715 | + errata_response = MockResponse( |
| 716 | + text="these are the errata", json_length=rfceditor.MIN_ERRATA_RESULTS |
| 717 | + ) |
| 718 | + |
| 719 | + # Test with full_index = False |
| 720 | + requests_get_mock.side_effect = (index_response, errata_response) # will step through these |
| 721 | + parse_index_mock.return_value = MockIndexData(length=rfceditor.MIN_INDEX_RESULTS) |
| 722 | + update_docs_mock.return_value = [] # not tested |
| 723 | + |
| 724 | + tasks.rfc_editor_index_update_task(full_index=False) |
| 725 | + |
| 726 | + # Check parse_index() call |
| 727 | + self.assertTrue(parse_index_mock.called) |
| 728 | + (parse_index_args, _) = parse_index_mock.call_args |
| 729 | + self.assertEqual( |
| 730 | + parse_index_args[0].read(), # arg is a StringIO |
| 731 | + "this is the index", |
| 732 | + "parse_index is called with the index text in a StringIO", |
| 733 | + ) |
| 734 | + |
| 735 | + # Check update_docs_from_rfc_index call |
| 736 | + self.assertTrue(update_docs_mock.called) |
| 737 | + (update_docs_args, update_docs_kwargs) = update_docs_mock.call_args |
| 738 | + self.assertEqual( |
| 739 | + update_docs_args, (parse_index_mock.return_value, errata_response.json()) |
| 740 | + ) |
| 741 | + self.assertIsNotNone(update_docs_kwargs["skip_older_than_date"]) |
| 742 | + |
| 743 | + # Test again with full_index = True |
| 744 | + requests_get_mock.side_effect = (index_response, errata_response) # will step through these |
| 745 | + parse_index_mock.return_value = MockIndexData(length=rfceditor.MIN_INDEX_RESULTS) |
| 746 | + update_docs_mock.return_value = [] # not tested |
| 747 | + tasks.rfc_editor_index_update_task(full_index=True) |
| 748 | + |
| 749 | + # Check parse_index() call |
| 750 | + self.assertTrue(parse_index_mock.called) |
| 751 | + (parse_index_args, _) = parse_index_mock.call_args |
| 752 | + self.assertEqual( |
| 753 | + parse_index_args[0].read(), # arg is a StringIO |
| 754 | + "this is the index", |
| 755 | + "parse_index is called with the index text in a StringIO", |
| 756 | + ) |
| 757 | + |
| 758 | + # Check update_docs_from_rfc_index call |
| 759 | + self.assertTrue(update_docs_mock.called) |
| 760 | + (update_docs_args, update_docs_kwargs) = update_docs_mock.call_args |
| 761 | + self.assertEqual( |
| 762 | + update_docs_args, (parse_index_mock.return_value, errata_response.json()) |
| 763 | + ) |
| 764 | + self.assertIsNone(update_docs_kwargs["skip_older_than_date"]) |
0 commit comments