Files
odoo_source/addons/web/tests/test_image.py
T
Thanh Dodeur f3e10f6980 [FIX] base, web: set 304 status in binary_content
Before this commit, the No Change status wasn't properly set because
the `filehash` wasn't passed to `_binary_set_headers` and because
the status was not changed to 304 if it was already set to 200 earlier.

This commit fixes this issue and also prevents images to be processed
in `content_image` if the status is 304.

opw-2008426

closes odoo/odoo#34032

Signed-off-by: Christophe Simonis <chs@odoo.com>
2019-06-19 12:31:07 +00:00

66 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import io
import base64
from PIL import Image
from odoo.tests.common import HttpCase
class TestImage(HttpCase):
def test_01_content_image_resize_placeholder(self):
"""The goal of this test is to make sure the placeholder image is
resized appropriately depending on the given URL parameters."""
# CASE: resize placeholder to given size
response = self.url_open('/web/image/0/200x150')
image = Image.open(io.BytesIO(response.content))
self.assertEqual(image.size, (200, 150))
# CASE: resize placeholder to small
response = self.url_open('/web/image/fake/0/image_small')
image = Image.open(io.BytesIO(response.content))
self.assertEqual(image.size, (64, 64))
# CASE: resize placeholder to medium
response = self.url_open('/web/image/fake/0/image_medium')
image = Image.open(io.BytesIO(response.content))
self.assertEqual(image.size, (128, 128))
# CASE: resize placeholder to large
response = self.url_open('/web/image/fake/0/image_large')
image = Image.open(io.BytesIO(response.content))
self.assertEqual(image.size, (256, 256))
# CASE: resize placeholder to big (but placeholder image is too small)
response = self.url_open('/web/image/fake/0/image')
image = Image.open(io.BytesIO(response.content))
self.assertEqual(image.size, (256, 256))
# CASE: no size found, use placeholder original size
response = self.url_open('/web/image/fake/0/image_no_size')
image = Image.open(io.BytesIO(response.content))
self.assertEqual(image.size, (256, 256))
def test_02_content_image_Etag_304(self):
"""This test makes sure that the 304 response is properly returned if the ETag is properly set"""
attachment = self.env['ir.attachment'].create({
'datas': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=",
'name': 'testEtag.gif',
'public': True,
'datas_fname': 'testEtag.gif',
'mimetype': 'image/gif',
})
response = self.url_open('/web/image/%s' % attachment.id)
self.assertEqual(response.status_code, 200)
self.assertEqual(base64.b64encode(response.content), attachment.datas)
etag = response.headers.get('ETag')
response2 = self.url_open('/web/image/%s' % attachment.id, headers={"If-None-Match": etag})
self.assertEqual(response2.status_code, 304)
self.assertEqual(len(response2.content), 0)