Skip to content

Commit

Permalink
[FIX] attachment_logging: Test are updated
Browse files Browse the repository at this point in the history
  • Loading branch information
geomer198 committed Jan 15, 2025
1 parent e9e2678 commit 81f090d
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions attachment_logging/tests/test_ir_attachments.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import json
import tempfile

from odoo.http import Request
from odoo.tests import HttpCase, new_test_user

from odoo.addons.base.tests.common import HttpCaseWithUserDemo


class TestIrAttachments(HttpCaseWithUserDemo):
def _make_rpc(self, route, params, headers=None):
data = json.dumps(
{
"id": 0,
"jsonrpc": "2.0",
"method": "call",
"params": params,
}
).encode()
headers = headers or {}
headers["Content-Type"] = "application/json"
return self.url_open(route, data, headers=headers)

class TestIrAttachments(HttpCase):
def setUp(self):
super().setUp()
self.partner = self.env["res.partner"].create({"name": "Test Partner #1"})
user = new_test_user(self.env, login="test_user", password="Password!1")
user.groups_id += self.env.ref("base.group_partner_manager")
self.mt_attachment = self.env.ref("attachment_logging.mt_attachment")

def _attach_temp_file(self):
"""Attach temp file to partner record"""
self.authenticate("test_user", "Password!1")
with tempfile.NamedTemporaryFile(mode="rb") as f:
response = self.url_open(
url=f"{self.base_url()}/mail/attachment/upload",
Expand All @@ -29,21 +41,17 @@ def _attach_temp_file(self):

def _delete_attachment(self, attachment_id):
"""Delete attachment from partner record"""
self.authenticate("test_user", "Password!1")
response = self.opener.post(
url=f"{self.base_url()}/mail/attachment/delete",
json={
"jsonrpc": "2.0",
"id": None,
"params": {
"attachment_id": attachment_id,
},
response = self._make_rpc(
f"{self.base_url()}/mail/attachment/delete",
{
"attachment_id": attachment_id,
},
)
return response

def test_upload_file_without_config(self):
"""Test flow where upload temp file to record (default behavior)"""
self.authenticate("demo", "demo")
message_count = len(self.partner.message_ids)
response = self._attach_temp_file()
data = response.json()
Expand All @@ -68,6 +76,7 @@ def test_upload_file_without_config(self):

def test_upload_file_with_config(self):
"""Test flow where upload temp file to record with log message"""
self.authenticate("demo", "demo")
message_count = len(self.partner.message_ids)
self.env["ir.config_parameter"].sudo().set_param(
"attachment_logging.use_attachment_log", True
Expand Down Expand Up @@ -109,6 +118,7 @@ def test_upload_file_with_config(self):

def test_delete_attachment_without_config(self):
"""Test flow where delete attachment from record (default behavior)"""
self.authenticate("demo", "demo")
message_count = len(self.partner.message_ids)
response = self._attach_temp_file()
data = response.json()
Expand Down Expand Up @@ -138,6 +148,7 @@ def test_delete_attachment_without_config(self):

def test_delete_attachment_with_config(self):
"""Test flow where delete attachment from record with log message"""
self.authenticate("demo", "demo")
message_count = len(self.partner.message_ids)
self.env["ir.config_parameter"].sudo().set_param(
"attachment_logging.use_attachment_log", True
Expand Down

0 comments on commit 81f090d

Please sign in to comment.