示例#1
0
 def test_last_modified(self):
     """Should return the modified timestamp."""
     EFModel.objects.create(name='test', content='test')
     efo = EFModel.objects.get(name='test')
     self.assertEqual(
         externalfiles.ExternalFile('test').last_modified,
         efo.last_modified)
示例#2
0
 def test_validate_resp_404(self):
     """Should raise an exception for a missing file."""
     response = Mock(status_code=404)
     ef = externalfiles.ExternalFile('test')
     with self.assertRaisesMessage(ValueError,
                                   'File not found (404): ' + ef.name):
         ef.validate_resp(response)
示例#3
0
 def test_last_modified_http(self, elm_mock):
     """should properly convert stored datetime to HTTP value."""
     good_value = 'Tue, 08 Jul 2014 12:00:00 GMT'
     good_datetime = datetime(year=2014, month=7, day=8, hour=4)
     good_datetime = timezone.make_aware(good_datetime, utc)
     elm_mock.return_value = good_datetime
     efo = externalfiles.ExternalFile('test')
     self.assertEqual(efo.last_modified_http, good_value)
示例#4
0
    def test_validate_resp_500(self):
        """Should raise an exception for all other codes."""
        response = Mock(status_code=500)
        ef = externalfiles.ExternalFile('test')
        with self.assertRaises(ValueError) as e:
            ef.validate_resp(response)

        self.assertTrue(str(e.exception).startswith('Unknown error'))
示例#5
0
 def test_last_modified_datetime(self, open_mock):
     """should properly convert stored date stamp to datetime."""
     read_mock = open_mock.return_value.__enter__.return_value.read
     read_mock.return_value = 'Tue, 08 Jul 2014 12:00:00 GMT'
     good_datetime = datetime(year=2014, month=7, day=8, hour=12)
     self.assertEqual(
         externalfiles.ExternalFile('test').last_modified_datetime,
         good_datetime)
示例#6
0
 def test_update_force_not_add_headers(self, requests_mock, elmh_mock):
     """Should not add proper modified headers when force is True."""
     requests_mock.get.side_effect = requests.RequestException
     ef = externalfiles.ExternalFile('test')
     elmh_mock.return_value = 'YabbaDabbaDooo!!'
     try:
         ef.update(force=True)
     except requests.RequestException:
         pass
     requests_mock.get.called_once_with(settings.EXTERNAL_FILES['test']['url'], headers={},
                                        verify=True)
示例#7
0
 def test_update_adds_headers(self, requests_mock, elmh_mock):
     """Should add proper modified headers when possible."""
     requests_mock.get.side_effect = requests.RequestException
     ef = externalfiles.ExternalFile('test')
     modified_str = 'Willlllmaaaaaaa!!'
     elmh_mock.return_value = modified_str
     try:
         ef.update()
     except requests.RequestException:
         pass
     requests_mock.get.called_once_with(settings.EXTERNAL_FILES['test']['url'],
                                        headers={'if-modified-since': modified_str},
                                        verify=True)
示例#8
0
 def test_validate_resp_304(self):
     """Should return None if the URL is up-to-date."""
     response = Mock(status_code=304)
     ef = externalfiles.ExternalFile('test')
     self.assertIsNone(ef.validate_resp(response))
示例#9
0
 def test_validate_resp_200(self):
     """Should return the content for a successful request."""
     response = Mock(status_code=200, text='Huge Success')
     ef = externalfiles.ExternalFile('test')
     self.assertEqual(ef.validate_resp(response), 'Huge Success')
示例#10
0
 def test_last_modified_read_error(self):
     """Should return None if object not in DB."""
     self.assertIsNone(externalfiles.ExternalFile('test').last_modified)
示例#11
0
 def test_file_name_provided(self):
     """Should be based on URL if not provided."""
     filename = 'there.is.no.data.xul'
     settings.EXTERNAL_FILES['test']['name'] = filename
     self.assertEqual(externalfiles.ExternalFile('test').name, filename)
示例#12
0
 def test_file_name(self):
     """Should be based on URL if not provided."""
     self.assertEqual(externalfiles.ExternalFile('test').name, 'there.is.only.xul')
示例#13
0
 def tearDown(self):
     externalfiles.ExternalFile('test').clear_cache()
     del settings.EXTERNAL_FILES['test']
示例#14
0
 def test_last_modified_datetime_bad_str(self, open_mock):
     """Should return None with bad date string."""
     open_mock.return_value.__enter__.return_value.read.return_value = 'The Dude abides.'
     self.assertIsNone(
         externalfiles.ExternalFile('test').last_modified_datetime)
示例#15
0
 def test_last_modified_datetime_error(self, open_mock):
     """Should return None on error."""
     open_mock.return_value.__enter__.return_value.read.side_effect = IOError
     self.assertIsNone(
         externalfiles.ExternalFile('test').last_modified_datetime)
示例#16
0
 def test_last_modified(self, open_mock):
     """Should return the modified timestamp."""
     open_mock.return_value.__enter__.return_value.read.return_value = 'The Dude abides.'
     self.assertEqual(
         externalfiles.ExternalFile('test').last_modified,
         'The Dude abides.')