'Ensure the image v2 API image transfer applied \'download_image\'
policy enforcement.'

@skip_if_disabled
def test_cache_middleware_trans_v2_without_download_image_policy(self):

    self.cleanup()
    self.start_servers(**self.__dict__.copy())
    path = ('http://%s:%d/v2/images' % ('0.0.0.0', self.api_port))
    http = httplib2.Http()
    headers = {'content-type': 'application/json'}
    image_entity = {'name': 'Image1', 'visibility': 'public', 'container_format': 'bare', 'disk_format': 'raw'}
    (response, content) = http.request(path, 'POST', headers=headers, body=jsonutils.dumps(image_entity))
    self.assertEqual(http_client.CREATED, response.status)
    data = jsonutils.loads(content)
    image_id = data['id']
    path = ('http://%s:%d/v2/images/%s/file' % ('0.0.0.0', self.api_port, image_id))
    headers = {'content-type': 'application/octet-stream'}
    image_data = ('*' * FIVE_KB)
    (response, content) = http.request(path, 'PUT', headers=headers, body=image_data)
    self.assertEqual(http_client.NO_CONTENT, response.status)
    image_cached_path = os.path.join(self.api_server.image_cache_dir, image_id)
    self.assertFalse(os.path.exists(image_cached_path))
    rules = {'context_is_admin': 'role:admin', 'default': '', 'download_image': '!'}
    self.set_policy_rules(rules)
    http = httplib2.Http()
    (response, content) = http.request(path, 'GET')
    self.assertEqual(http_client.FORBIDDEN, response.status)
    path = ('http://%s:%d/v2/images/%s' % ('0.0.0.0', self.api_port, image_id))
    http = httplib2.Http()
    (response, content) = http.request(path, 'DELETE')
    self.assertEqual(http_client.NO_CONTENT, response.status)
    self.assertFalse(os.path.exists(image_cached_path))
    self.stop_servers()
