|
12 | 12 | from django.conf import settings |
13 | 13 | from django_dynamic_fixture import G |
14 | 14 | import pytz |
| 15 | +from mock import patch, Mock |
15 | 16 |
|
16 | 17 | from analytics_data_api.constants.country import get_country |
17 | 18 | from analytics_data_api.v0 import models |
18 | 19 | from analytics_data_api.constants import country, enrollment_modes, genders |
19 | 20 | from analytics_data_api.v0.models import CourseActivityWeekly |
20 | 21 | from analytics_data_api.v0.tests.utils import flatten |
21 | | -from analytics_data_api.v0.tests.views import DemoCourseMixin, DEMO_COURSE_ID |
| 22 | +from analytics_data_api.v0.tests.views import DemoCourseMixin, DEMO_COURSE_ID, SANITIZED_DEMO_COURSE_ID |
22 | 23 | from analyticsdataserver.tests import TestCaseWithAuthentication |
23 | 24 |
|
24 | 25 |
|
@@ -785,3 +786,119 @@ def test_get(self): |
785 | 786 | def test_get_404(self): |
786 | 787 | response = self._get_data('foo/bar/course') |
787 | 788 | self.assertEquals(response.status_code, 404) |
| 789 | + |
| 790 | + |
| 791 | +class CourseReportDownloadViewTests(DemoCourseMixin, TestCaseWithAuthentication): |
| 792 | + |
| 793 | + path = '/api/v0/courses/{course_id}/reports/{report_name}' |
| 794 | + |
| 795 | + @patch('django.core.files.storage.default_storage.exists', Mock(return_value=False)) |
| 796 | + def test_report_file_not_found(self): |
| 797 | + response = self.authenticated_get( |
| 798 | + self.path.format( |
| 799 | + course_id=DEMO_COURSE_ID, |
| 800 | + report_name='problem_response' |
| 801 | + ) |
| 802 | + ) |
| 803 | + self.assertEqual(response.status_code, 404) |
| 804 | + |
| 805 | + def test_report_not_supported(self): |
| 806 | + response = self.authenticated_get( |
| 807 | + self.path.format( |
| 808 | + course_id=DEMO_COURSE_ID, |
| 809 | + report_name='fake_problem_that_we_dont_support' |
| 810 | + ) |
| 811 | + ) |
| 812 | + self.assertEqual(response.status_code, 404) |
| 813 | + |
| 814 | + @patch('analytics_data_api.utils.default_storage', object()) |
| 815 | + def test_incompatible_storage_provider(self): |
| 816 | + response = self.authenticated_get( |
| 817 | + self.path.format( |
| 818 | + course_id=DEMO_COURSE_ID, |
| 819 | + report_name='problem_response' |
| 820 | + ) |
| 821 | + ) |
| 822 | + self.assertEqual(response.status_code, 501) |
| 823 | + |
| 824 | + @patch('django.core.files.storage.default_storage.exists', Mock(return_value=True)) |
| 825 | + @patch('django.core.files.storage.default_storage.url', Mock(return_value='http://fake')) |
| 826 | + @patch( |
| 827 | + 'django.core.files.storage.default_storage.modified_time', |
| 828 | + Mock(return_value=datetime.datetime(2014, 1, 1, tzinfo=pytz.utc)) |
| 829 | + ) |
| 830 | + @patch('django.core.files.storage.default_storage.size', Mock(return_value=1000)) |
| 831 | + @patch( |
| 832 | + 'analytics_data_api.utils.get_expiration_date', |
| 833 | + Mock(return_value=datetime.datetime(2014, 1, 1, tzinfo=pytz.utc)) |
| 834 | + ) |
| 835 | + def test_make_working_link(self): |
| 836 | + response = self.authenticated_get( |
| 837 | + self.path.format( |
| 838 | + course_id=DEMO_COURSE_ID, |
| 839 | + report_name='problem_response' |
| 840 | + ) |
| 841 | + ) |
| 842 | + self.assertEqual(response.status_code, 200) |
| 843 | + expected = { |
| 844 | + 'course_id': SANITIZED_DEMO_COURSE_ID, |
| 845 | + 'report_name': 'problem_response', |
| 846 | + 'download_url': 'http://fake', |
| 847 | + 'last_modified': datetime.datetime(2014, 1, 1, tzinfo=pytz.utc).strftime(settings.DATETIME_FORMAT), |
| 848 | + 'expiration_date': datetime.datetime(2014, 1, 1, tzinfo=pytz.utc).strftime(settings.DATETIME_FORMAT), |
| 849 | + 'file_size': 1000 |
| 850 | + } |
| 851 | + self.assertEqual(response.data, expected) |
| 852 | + |
| 853 | + @patch('django.core.files.storage.default_storage.exists', Mock(return_value=True)) |
| 854 | + @patch('django.core.files.storage.default_storage.url', Mock(return_value='http://fake')) |
| 855 | + @patch( |
| 856 | + 'django.core.files.storage.default_storage.modified_time', |
| 857 | + Mock(return_value=datetime.datetime(2014, 1, 1, tzinfo=pytz.utc)) |
| 858 | + ) |
| 859 | + @patch('django.core.files.storage.default_storage.size', Mock(side_effect=NotImplementedError())) |
| 860 | + @patch( |
| 861 | + 'analytics_data_api.utils.get_expiration_date', |
| 862 | + Mock(return_value=datetime.datetime(2014, 1, 1, tzinfo=pytz.utc)) |
| 863 | + ) |
| 864 | + def test_make_working_link_with_missing_size(self): |
| 865 | + response = self.authenticated_get( |
| 866 | + self.path.format( |
| 867 | + course_id=DEMO_COURSE_ID, |
| 868 | + report_name='problem_response' |
| 869 | + ) |
| 870 | + ) |
| 871 | + self.assertEqual(response.status_code, 200) |
| 872 | + expected = { |
| 873 | + 'course_id': SANITIZED_DEMO_COURSE_ID, |
| 874 | + 'report_name': 'problem_response', |
| 875 | + 'download_url': 'http://fake', |
| 876 | + 'last_modified': datetime.datetime(2014, 1, 1, tzinfo=pytz.utc).strftime(settings.DATETIME_FORMAT), |
| 877 | + 'expiration_date': datetime.datetime(2014, 1, 1, tzinfo=pytz.utc).strftime(settings.DATETIME_FORMAT) |
| 878 | + } |
| 879 | + self.assertEqual(response.data, expected) |
| 880 | + |
| 881 | + @patch('django.core.files.storage.default_storage.exists', Mock(return_value=True)) |
| 882 | + @patch('django.core.files.storage.default_storage.url', Mock(return_value='http://fake')) |
| 883 | + @patch('django.core.files.storage.default_storage.modified_time', Mock(side_effect=NotImplementedError())) |
| 884 | + @patch('django.core.files.storage.default_storage.size', Mock(return_value=1000)) |
| 885 | + @patch( |
| 886 | + 'analytics_data_api.utils.get_expiration_date', |
| 887 | + Mock(return_value=datetime.datetime(2014, 1, 1, tzinfo=pytz.utc)) |
| 888 | + ) |
| 889 | + def test_make_working_link_with_missing_last_modified_date(self): |
| 890 | + response = self.authenticated_get( |
| 891 | + self.path.format( |
| 892 | + course_id=DEMO_COURSE_ID, |
| 893 | + report_name='problem_response' |
| 894 | + ) |
| 895 | + ) |
| 896 | + self.assertEqual(response.status_code, 200) |
| 897 | + expected = { |
| 898 | + 'course_id': SANITIZED_DEMO_COURSE_ID, |
| 899 | + 'report_name': 'problem_response', |
| 900 | + 'download_url': 'http://fake', |
| 901 | + 'file_size': 1000, |
| 902 | + 'expiration_date': datetime.datetime(2014, 1, 1, tzinfo=pytz.utc).strftime(settings.DATETIME_FORMAT) |
| 903 | + } |
| 904 | + self.assertEqual(response.data, expected) |
0 commit comments