[IMP] mail: send cache control header on avatars

Before this PR cache control header for avatar was set to no-cache which is not
the right value.
This PR mark the avatar routes response as immutable when a unique string is
present. We also use the partner write_date as unique string when possible.

task-3551636

closes odoo/odoo#141326

X-original-commit: 3de4f1f1225229cbdb0512df5790ce491f04ea7e
Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
This commit is contained in:
Didier (did)
2023-11-07 21:02:27 +00:00
parent 7b1dd79f46
commit 4064508af5
11 changed files with 98 additions and 19 deletions
@@ -5,6 +5,8 @@ import json
import odoo
from odoo.tools import mute_logger, date_utils
from odoo.tests import HttpCase
from odoo.http import STATIC_CACHE_LONG
from odoo import Command
@odoo.tests.tagged("-at_install", "post_install")
@@ -342,3 +344,44 @@ class TestMessageController(HttpCase):
self.env["res.partner"].search_count([('email', '=', "john2@test.be")]),
"'mail/message/post' does not create another user if there's already a user with matching email",
)
def test_mail_cache_control_header(self):
testuser = self.env['res.users'].create({
'email': 'testuser@testuser.com',
'groups_id': [Command.set([self.ref('base.group_portal')])],
'name': 'Test User',
'login': 'testuser',
'password': 'testuser',
})
test_user = self.authenticate("testuser", "testuser")
partner = self.env["res.users"].browse(test_user.uid).partner_id
self.channel.add_members(testuser.partner_id.ids)
res = self.url_open(
url=f"/discuss/channel/{self.channel.id}/avatar_128?unique={self.channel._get_avatar_cache_key()}"
)
self.assertEqual(res.headers["Cache-Control"], f"public, max-age={STATIC_CACHE_LONG}")
res = self.url_open(
url=f"/discuss/channel/{self.channel.id}/avatar_128"
)
self.assertEqual(res.headers["Cache-Control"], "no-cache")
res = self.url_open(
url=f"/discuss/channel/{self.channel.id}/partner/{partner.id}/avatar_128?unique={partner.write_date.isoformat()}"
)
self.assertEqual(res.headers["Cache-Control"], f"public, max-age={STATIC_CACHE_LONG}")
res = self.url_open(
url=f"/discuss/channel/{self.channel.id}/partner/{partner.id}/avatar_128"
)
self.assertEqual(res.headers["Cache-Control"], "no-cache")
res = self.url_open(
url=f"/discuss/channel/{self.channel.id}/guest/{self.guest.id}/avatar_128?unique={self.guest.write_date.isoformat()}"
)
self.assertEqual(res.headers["Cache-Control"], f"public, max-age={STATIC_CACHE_LONG}")
res = self.url_open(
url=f"/discuss/channel/{self.channel.id}/guest/{self.guest.id}/avatar_128"
)
self.assertEqual(res.headers["Cache-Control"], "no-cache")