|
1 | 1 | # Copyright 2025 Akretion (http://www.akretion.com). |
2 | 2 | # @author Florian Mounier <florian.mounier@akretion.com> |
| 3 | +# Copyright 2025 Simone Rubino - PyTech |
3 | 4 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 5 | + |
4 | 6 | import os |
5 | 7 | import unittest |
6 | 8 |
|
7 | | -from odoo.addons.api_log.tests.common import CommonAPILog |
8 | 9 | from odoo.addons.fastapi.schemas import DemoExceptionType |
9 | | -from odoo.addons.mail.tests.common import MailCase |
| 10 | +from odoo.addons.fastapi_log.tests.common import Common |
| 11 | + |
| 12 | +from fastapi import status |
10 | 13 |
|
11 | 14 |
|
12 | | -@unittest.skipIf(os.getenv("SKIP_HTTP_CASE"), "FastAPIEncryptedErrorsCase skipped") |
13 | | -class FastAPIEncryptedErrorsCase(CommonAPILog, MailCase): |
| 15 | +@unittest.skipIf(os.getenv("SKIP_HTTP_CASE"), "TestFastapiLogMail skipped") |
| 16 | +class TestFastapiLogMail(Common): |
14 | 17 | @classmethod |
15 | 18 | def setUpClass(cls): |
16 | 19 | super().setUpClass() |
17 | | - cls.fastapi_demo_app = cls.env.ref("fastapi.fastapi_endpoint_demo") |
18 | | - |
19 | | - cls.fastapi_demo_app._handle_registry_sync() |
20 | | - cls.fastapi_demo_app.log_requests = True |
21 | | - cls.fastapi_demo_app.fastapi_log_mail_template_id = cls.env[ |
22 | | - "mail.template" |
| 20 | + cls.fastapi_demo_app.api_log_mail_exception_activity_type_id = cls.env[ |
| 21 | + "mail.activity.type" |
23 | 22 | ].create( |
24 | 23 | { |
25 | | - "name": "Test exception email template", |
26 | | - "model_id": cls.env.ref("api_log.model_api_log").id, |
| 24 | + "name": "Test exception activity", |
| 25 | + "res_model": "api.log", |
27 | 26 | } |
28 | 27 | ) |
29 | 28 |
|
30 | | - def test_endpoint_exception_send_email(self): |
31 | | - """If an endpoint has an email template, |
32 | | - when an exception occurs an email is sent using the configured template. |
| 29 | + def test_endpoint_exception_create_activity(self): |
| 30 | + """If an endpoint has an activity type, |
| 31 | + when an exception occurs an activity of the configured type is created. |
33 | 32 | """ |
34 | 33 | # Arrange |
35 | | - mail_template = self.fastapi_demo_app.fastapi_log_mail_template_id |
| 34 | + app = self.fastapi_demo_app |
| 35 | + activity_type = app.api_log_mail_exception_activity_type_id |
36 | 36 | route = ( |
37 | | - "/fastapi_demo/demo/exception?" |
| 37 | + "/fastapi_demo/test/demo/exception?" |
38 | 38 | f"exception_type={DemoExceptionType.user_error.value}" |
39 | | - "&error_message=User Error" |
| 39 | + "&error_message=An error happened" |
40 | 40 | ) |
41 | 41 | # pre-condition |
42 | | - self.assertTrue(mail_template) |
| 42 | + self.assertTrue(activity_type) |
43 | 43 |
|
44 | 44 | # Act |
45 | | - with self.mock_mail_gateway(): |
46 | | - self.url_open(route, timeout=200) |
| 45 | + with self.log_capturer() as capturer: |
| 46 | + response = self.url_open(route, timeout=200) |
47 | 47 |
|
48 | 48 | # Assert |
49 | | - sent_email = self._filter_mail() |
50 | | - self.assertTrue(sent_email) |
| 49 | + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 50 | + log = capturer.records |
| 51 | + self.assertEqual(len(log), 1) |
| 52 | + self.assertTrue(log.activity_ids) |
0 commit comments