示例#1
0
    def test_file_slug__with_height_in_querystring__cloud_function_error(self):
        """Test /answer without auth"""
        self.headers(academy=1)
        media_kwargs = {
            'url': 'https://potato.io/harcoded',
            'mime': 'image/png',
            'hash': 'harcoded'
        }
        model = self.generate_models(academy=True,
                                     media=True,
                                     media_kwargs=media_kwargs)

        with patch('google.oauth2.id_token.fetch_id_token') as token_mock:
            token_mock.return_value = 'blablabla'

            with patch(REQUESTS_PATH['post'],
                       apply_requests_post_mock([bad_server_response()
                                                 ])) as mock:
                url = reverse_lazy('media:file_slug',
                                   kwargs={'media_slug': model['media'].slug
                                           }) + '?height=1000'
                response = self.client.get(url)
                json = response.json()

        expected = {
            "detail": "cloud-function-bad-input",
            "status_code": 500,
        }

        self.assertEqual(json, expected)
        self.assertEqual(response.status_code,
                         status.HTTP_500_INTERNAL_SERVER_ERROR)

        self.assertEqual(mock.call_args_list, [
            call(
                'https://us-central1-breathecode-197918.cloudfunctions.net/resize-image',
                data=
                '{"width": null, "height": "1000", "filename": "harcoded", "bucket": "bucket-name"}',
                headers={
                    'Authorization': 'Bearer blablabla',
                    'Content-Type': 'application/json',
                    'Accept': 'application/json'
                })
        ])

        self.assertEqual(self.all_media_dict(), [{
            **self.model_to_dict(model, 'media'),
            'hits':
            model['media'].hits + 1,
        }])

        self.assertEqual(self.all_media_resolution_dict(), [])
示例#2
0
    def test_file_slug__with_height_in_querystring(self):
        """Test /answer without auth"""
        self.headers(academy=1)
        media_kwargs = {
            'url': 'https://potato.io/harcoded',
            'mime': 'image/png',
            'hash': 'harcoded'
        }
        model = self.generate_models(academy=True,
                                     media=True,
                                     media_kwargs=media_kwargs)

        with patch('google.oauth2.id_token.fetch_id_token') as token_mock:
            token_mock.return_value = 'blablabla'

            with patch(REQUESTS_PATH['post'],
                       apply_requests_post_mock([resized_response()])) as mock:
                url = reverse_lazy('media:file_slug',
                                   kwargs={'media_slug': model['media'].slug
                                           }) + '?height=1000'
                response = self.client.get(url)

        self.assertEqual(response.url, 'https://potato.io/harcoded-1000x1000')
        self.assertEqual(response.status_code,
                         status.HTTP_301_MOVED_PERMANENTLY)

        self.assertEqual(mock.call_args_list, [
            call(
                'https://us-central1-breathecode-197918.cloudfunctions.net/resize-image',
                data=
                '{"width": null, "height": "1000", "filename": "harcoded", "bucket": "bucket-name"}',
                headers={
                    'Authorization': 'Bearer blablabla',
                    'Content-Type': 'application/json',
                    'Accept': 'application/json'
                })
        ])

        self.assertEqual(self.all_media_dict(), [{
            **self.model_to_dict(model, 'media'),
            'hits':
            model['media'].hits + 1,
        }])

        self.assertEqual(self.all_media_resolution_dict(),
                         [{
                             'hash': model.media.hash,
                             'height': 1000,
                             'hits': 1,
                             'id': 1,
                             'width': 1000,
                         }])
示例#3
0
    def test_file_slug__with_height_in_querystring__resolution_exist(self):
        """Test /answer without auth"""
        self.headers(academy=1)
        media_kwargs = {
            'url': 'https://potato.io/harcoded',
            'mime': 'image/png',
            'hash': 'harcoded'
        }
        media_resolution_kwargs = {
            'width': 1000,
            'height': 1000,
            'hash': 'harcoded'
        }
        model = self.generate_models(
            academy=True,
            media=True,
            media_resolution=True,
            media_kwargs=media_kwargs,
            media_resolution_kwargs=media_resolution_kwargs)

        with patch(REQUESTS_PATH['post'],
                   apply_requests_post_mock([resized_response()])) as mock:
            url = reverse_lazy('media:file_slug',
                               kwargs={'media_slug': model['media'].slug
                                       }) + '?height=1000'
            response = self.client.get(url)

        self.assertEqual(response.url, 'https://potato.io/harcoded-1000x1000')
        self.assertEqual(response.status_code,
                         status.HTTP_301_MOVED_PERMANENTLY)

        self.assertEqual(mock.call_args_list, [])
        self.assertEqual(self.all_media_dict(), [{
            **self.model_to_dict(model, 'media'),
            'hits':
            model['media'].hits + 1,
        }])

        self.assertEqual(self.all_media_resolution_dict(), [{
            **self.model_to_dict(model, 'media_resolution'),
            'hits':
            model['media_resolution'].hits + 1,
        }])