Ejemplo n.º 1
0
    def setUp(self):
        self.resource_url = 'http://example.com/resource'

        self.provider = Provider()
        self.provider.api_endpoint = 'http://foo.com/oembed'
Ejemplo n.º 2
0
class ProviderTestCase(TestCase):

    def setUp(self):
        self.resource_url = 'http://example.com/resource'

        self.provider = Provider()
        self.provider.api_endpoint = 'http://foo.com/oembed'

    def test_get_request_url(self):
        params = {'url': self.resource_url, 'format': 'json'}
        request_url = self.provider.get_request_url(**params)

        self.assertIn(self.provider.api_endpoint, request_url)
        self.assertIn(urlencode({'url': self.resource_url}), request_url)
        self.assertIn('format=json', request_url)

    def test_get_request_url_filters_zeros(self):
        params = {
            'url': self.resource_url,
            'format': 'json'
        }

        params.update({'maxwidth': '0', 'maxheight': 100})
        request_url = self.provider.get_request_url(**params)
        self.assertNotIn('maxwidth', request_url)

        params.update({'maxwidth': 100, 'maxheight': None})
        request_url = self.provider.get_request_url(**params)
        self.assertNotIn('maxheight', request_url)

    @patch('monocle.providers.request_external_oembed')
    @patch('monocle.providers.cache')
    def test_get_resource_stale_calls_task(self, mock_cache, mock_task):
        resource = Resource(self.resource_url)
        resource.created = resource.created - (60*60*24*365*10)

        mock_cache.get_or_prime = mock_cache
        mock_cache.return_value = (resource, False)
        mock_task.apply_async = mock_task

        self.assertTrue(resource.is_stale)
        resource = self.provider.get_resource(self.resource_url)
        self.assertFalse(resource.is_stale)
        self.assertTrue(mock_task.called)

    @patch('monocle.providers.request_external_oembed')
    @patch('monocle.providers.cache')
    def test_get_resource_primed_calls_task(self, mock_cache, mock_task):
        resource = Resource(self.resource_url)

        mock_cache.get_or_prime = mock_cache
        mock_cache.return_value = (resource, True)
        mock_task.apply_async = mock_task

        self.assertFalse(resource.is_stale)
        resource = self.provider.get_resource(self.resource_url)
        self.assertTrue(mock_task.called)

    @patch('monocle.providers.request_external_oembed')
    @patch('monocle.providers.cache')
    def test_get_resource_no_task(self, mock_cache, mock_task):
        resource = Resource(self.resource_url)

        mock_cache.get_or_prime = mock_cache
        mock_cache.return_value = (resource, False)
        mock_task.apply_async = mock_task

        self.provider.get_resource(self.resource_url)
        self.assertFalse(mock_task.called)

    def test_match_provides(self):
        self.provider.url_schemes = ['http://*.foo.com/bar']
        self.assertTrue(self.provider.match('http://sub.blah.foo.com/bar'))

    def test_match_does_not_provide(self):
        self.provider.url_schemes = ['http://*.foo.com/bar']
        self.assertFalse(self.provider.match('http://youtube.com/video'))