prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  [15.0] mail_tracking: portal tests fail
  <!-- Provide a general summary of the issue in the Title above.
  Before opening the issue, please check that there isn't another issue addressing the same problem already created -->
  
  ## Module
  mail_tracking
  
  ## Describe the bug
  mail_tracking causes TestPortalWizard.test_portal_wizard_public_user and TestPortalWizard.test_portal_wizard_partner to fail
  
  ## To Reproduce
  **Affected versions**: 15.0
  
  Steps to reproduce the behavior:
  1. init db with portal and mail_tracking
  2. run portal tests
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `mail_tracking/models/ir_mail_server.py`
  Content:
  ```
  1 # Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  2 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3 
  4 import re
  5 import threading
  6 
  7 from odoo import api, models, tools
  8 
  9 
  10 class IrMailServer(models.Model):
  11     _inherit = "ir.mail_server"
  12 
  13     def _tracking_headers_add(self, tracking_email_id, headers):
  14         """Allow other addons to add its own tracking SMTP headers"""
  15         headers = headers or {}
  16         headers["X-Odoo-Database"] = getattr(threading.current_thread(), "dbname", None)
  17         headers["X-Odoo-MailTracking-ID"] = tracking_email_id
  18         return headers
  19 
  20     def _tracking_email_id_body_get(self, body):
  21         body = body or ""
  22         # https://regex101.com/r/lW4cB1/2
  23         match = re.search(r'<img[^>]*data-odoo-tracking-email=["\']([0-9]*)["\']', body)
  24         return str(match.group(1)) if match and match.group(1) else False
  25 
  26     def build_email(
  27         self,
  28         email_from,
  29         email_to,
  30         subject,
  31         body,
  32         email_cc=None,
  33         email_bcc=None,
  34         reply_to=False,
  35         attachments=None,
  36         message_id=None,
  37         references=None,
  38         object_id=False,
  39         subtype="plain",
  40         headers=None,
  41         body_alternative=None,
  42         subtype_alternative="plain",
  43     ):
  44         tracking_email_id = self._tracking_email_id_body_get(body)
  45         if tracking_email_id:
  46             headers = self._tracking_headers_add(tracking_email_id, headers)
  47         msg = super(IrMailServer, self).build_email(
  48             email_from,
  49             email_to,
  50             subject,
  51             body,
  52             email_cc=email_cc,
  53             email_bcc=email_bcc,
  54             reply_to=reply_to,
  55             attachments=attachments,
  56             message_id=message_id,
  57             references=references,
  58             object_id=object_id,
  59             subtype=subtype,
  60             headers=headers,
  61             body_alternative=body_alternative,
  62             subtype_alternative=subtype_alternative,
  63         )
  64         return msg
  65 
  66     def _tracking_email_get(self, message):
  67         try:
  68             tracking_email_id = int(
  69                 message.get(
  70                     "X-Odoo-MailTracking-ID",
  71                     # Deprecated tracking header, kept as fallback
  72                     message["X-Odoo-Tracking-ID"],
  73                 )
  74             )
  75         except (TypeError, ValueError, KeyError):
  76             tracking_email_id = False
  77         return self.env["mail.tracking.email"].browse(tracking_email_id)
  78 
  79     def _smtp_server_get(self, mail_server_id, smtp_server):
  80         smtp_server_used = False
  81         mail_server = None
  82         if mail_server_id:
  83             mail_server = self.browse(mail_server_id)
  84         elif not smtp_server:
  85             mail_server_ids = self.search([], order="sequence", limit=1)
  86             mail_server = mail_server_ids[0] if mail_server_ids else None
  87         if mail_server:
  88             smtp_server_used = mail_server.smtp_host
  89         else:
  90             smtp_server_used = smtp_server or tools.config.get("smtp_server")
  91         return smtp_server_used
  92 
  93     @api.model
  94     def send_email(
  95         self,
  96         message,
  97         mail_server_id=None,
  98         smtp_server=None,
  99         smtp_port=None,
  100         smtp_user=None,
  101         smtp_password=None,
  102         smtp_encryption=None,
  103         smtp_debug=False,
  104         smtp_session=None,
  105     ):
  106         message_id = False
  107         tracking_email = self._tracking_email_get(message)
  108         smtp_server_used = self.sudo()._smtp_server_get(mail_server_id, smtp_server)
  109         try:
  110             message_id = super(IrMailServer, self).send_email(
  111                 message,
  112                 mail_server_id=mail_server_id,
  113                 smtp_server=smtp_server,
  114                 smtp_port=smtp_port,
  115                 smtp_user=smtp_user,
  116                 smtp_password=smtp_password,
  117                 smtp_encryption=smtp_encryption,
  118                 smtp_debug=smtp_debug,
  119                 smtp_session=smtp_session,
  120             )
  121         except Exception as e:
  122             if tracking_email:
  123                 tracking_email.smtp_error(self, smtp_server_used, e)
  124         if message_id and tracking_email:
  125             vals = tracking_email._tracking_sent_prepare(
  126                 self, smtp_server_used, message, message_id
  127             )
  128             if vals:
  129                 self.env["mail.tracking.event"].sudo().create(vals)
  130         return message_id
  131
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/mail_tracking/models/ir_mail_server.py b/mail_tracking/models/ir_mail_server.py
  --- a/mail_tracking/models/ir_mail_server.py
  +++ b/mail_tracking/models/ir_mail_server.py
  @@ -45,10 +45,10 @@
           if tracking_email_id:
               headers = self._tracking_headers_add(tracking_email_id, headers)
           msg = super(IrMailServer, self).build_email(
  -            email_from,
  -            email_to,
  -            subject,
  -            body,
  +            email_from=email_from,
  +            email_to=email_to,
  +            subject=subject,
  +            body=body,
               email_cc=email_cc,
               email_bcc=email_bcc,
               reply_to=reply_to,
  
verification_info: {"golden_diff": "diff --git a/mail_tracking/models/ir_mail_server.py b/mail_tracking/models/ir_mail_server.py\n--- a/mail_tracking/models/ir_mail_server.py\n+++ b/mail_tracking/models/ir_mail_server.py\n@@ -45,10 +45,10 @@\n         if tracking_email_id:\n             headers = self._tracking_headers_add(tracking_email_id, headers)\n         msg = super(IrMailServer, self).build_email(\n-            email_from,\n-            email_to,\n-            subject,\n-            body,\n+            email_from=email_from,\n+            email_to=email_to,\n+            subject=subject,\n+            body=body,\n             email_cc=email_cc,\n             email_bcc=email_bcc,\n             reply_to=reply_to,\n", "issue": "[15.0] mail_tracking: portal tests fail\n<!-- Provide a general summary of the issue in the Title above.\r\nBefore opening the issue, please check that there isn't another issue addressing the same problem already created -->\r\n\r\n## Module\r\nmail_tracking\r\n\r\n## Describe the bug\r\nmail_tracking causes TestPortalWizard.test_portal_wizard_public_user and TestPortalWizard.test_portal_wizard_partner to fail\r\n\r\n## To Reproduce\r\n**Affected versions**: 15.0\r\n\r\nSteps to reproduce the behavior:\r\n1. init db with portal and mail_tracking\r\n2. run portal tests\r\n\n", "before_files": [{"content": "# Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>\n# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).\n\nimport re\nimport threading\n\nfrom odoo import api, models, tools\n\n\nclass IrMailServer(models.Model):\n    _inherit = \"ir.mail_server\"\n\n    def _tracking_headers_add(self, tracking_email_id, headers):\n        \"\"\"Allow other addons to add its own tracking SMTP headers\"\"\"\n        headers = headers or {}\n        headers[\"X-Odoo-Database\"] = getattr(threading.current_thread(), \"dbname\", None)\n        headers[\"X-Odoo-MailTracking-ID\"] = tracking_email_id\n        return headers\n\n    def _tracking_email_id_body_get(self, body):\n        body = body or \"\"\n        # https://regex101.com/r/lW4cB1/2\n        match = re.search(r'<img[^>]*data-odoo-tracking-email=[\"\\']([0-9]*)[\"\\']', body)\n        return str(match.group(1)) if match and match.group(1) else False\n\n    def build_email(\n        self,\n        email_from,\n        email_to,\n        subject,\n        body,\n        email_cc=None,\n        email_bcc=None,\n        reply_to=False,\n        attachments=None,\n        message_id=None,\n        references=None,\n        object_id=False,\n        subtype=\"plain\",\n        headers=None,\n        body_alternative=None,\n        subtype_alternative=\"plain\",\n    ):\n        tracking_email_id = self._tracking_email_id_body_get(body)\n        if tracking_email_id:\n            headers = self._tracking_headers_add(tracking_email_id, headers)\n        msg = super(IrMailServer, self).build_email(\n            email_from,\n            email_to,\n            subject,\n            body,\n            email_cc=email_cc,\n            email_bcc=email_bcc,\n            reply_to=reply_to,\n            attachments=attachments,\n            message_id=message_id,\n            references=references,\n            object_id=object_id,\n            subtype=subtype,\n            headers=headers,\n            body_alternative=body_alternative,\n            subtype_alternative=subtype_alternative,\n        )\n        return msg\n\n    def _tracking_email_get(self, message):\n        try:\n            tracking_email_id = int(\n                message.get(\n                    \"X-Odoo-MailTracking-ID\",\n                    # Deprecated tracking header, kept as fallback\n                    message[\"X-Odoo-Tracking-ID\"],\n                )\n            )\n        except (TypeError, ValueError, KeyError):\n            tracking_email_id = False\n        return self.env[\"mail.tracking.email\"].browse(tracking_email_id)\n\n    def _smtp_server_get(self, mail_server_id, smtp_server):\n        smtp_server_used = False\n        mail_server = None\n        if mail_server_id:\n            mail_server = self.browse(mail_server_id)\n        elif not smtp_server:\n            mail_server_ids = self.search([], order=\"sequence\", limit=1)\n            mail_server = mail_server_ids[0] if mail_server_ids else None\n        if mail_server:\n            smtp_server_used = mail_server.smtp_host\n        else:\n            smtp_server_used = smtp_server or tools.config.get(\"smtp_server\")\n        return smtp_server_used\n\n    @api.model\n    def send_email(\n        self,\n        message,\n        mail_server_id=None,\n        smtp_server=None,\n        smtp_port=None,\n        smtp_user=None,\n        smtp_password=None,\n        smtp_encryption=None,\n        smtp_debug=False,\n        smtp_session=None,\n    ):\n        message_id = False\n        tracking_email = self._tracking_email_get(message)\n        smtp_server_used = self.sudo()._smtp_server_get(mail_server_id, smtp_server)\n        try:\n            message_id = super(IrMailServer, self).send_email(\n                message,\n                mail_server_id=mail_server_id,\n                smtp_server=smtp_server,\n                smtp_port=smtp_port,\n                smtp_user=smtp_user,\n                smtp_password=smtp_password,\n                smtp_encryption=smtp_encryption,\n                smtp_debug=smtp_debug,\n                smtp_session=smtp_session,\n            )\n        except Exception as e:\n            if tracking_email:\n                tracking_email.smtp_error(self, smtp_server_used, e)\n        if message_id and tracking_email:\n            vals = tracking_email._tracking_sent_prepare(\n                self, smtp_server_used, message, message_id\n            )\n            if vals:\n                self.env[\"mail.tracking.event\"].sudo().create(vals)\n        return message_id\n", "path": "mail_tracking/models/ir_mail_server.py"}], "after_files": [{"content": "# Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>\n# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).\n\nimport re\nimport threading\n\nfrom odoo import api, models, tools\n\n\nclass IrMailServer(models.Model):\n    _inherit = \"ir.mail_server\"\n\n    def _tracking_headers_add(self, tracking_email_id, headers):\n        \"\"\"Allow other addons to add its own tracking SMTP headers\"\"\"\n        headers = headers or {}\n        headers[\"X-Odoo-Database\"] = getattr(threading.current_thread(), \"dbname\", None)\n        headers[\"X-Odoo-MailTracking-ID\"] = tracking_email_id\n        return headers\n\n    def _tracking_email_id_body_get(self, body):\n        body = body or \"\"\n        # https://regex101.com/r/lW4cB1/2\n        match = re.search(r'<img[^>]*data-odoo-tracking-email=[\"\\']([0-9]*)[\"\\']', body)\n        return str(match.group(1)) if match and match.group(1) else False\n\n    def build_email(\n        self,\n        email_from,\n        email_to,\n        subject,\n        body,\n        email_cc=None,\n        email_bcc=None,\n        reply_to=False,\n        attachments=None,\n        message_id=None,\n        references=None,\n        object_id=False,\n        subtype=\"plain\",\n        headers=None,\n        body_alternative=None,\n        subtype_alternative=\"plain\",\n    ):\n        tracking_email_id = self._tracking_email_id_body_get(body)\n        if tracking_email_id:\n            headers = self._tracking_headers_add(tracking_email_id, headers)\n        msg = super(IrMailServer, self).build_email(\n            email_from=email_from,\n            email_to=email_to,\n            subject=subject,\n            body=body,\n            email_cc=email_cc,\n            email_bcc=email_bcc,\n            reply_to=reply_to,\n            attachments=attachments,\n            message_id=message_id,\n            references=references,\n            object_id=object_id,\n            subtype=subtype,\n            headers=headers,\n            body_alternative=body_alternative,\n            subtype_alternative=subtype_alternative,\n        )\n        return msg\n\n    def _tracking_email_get(self, message):\n        try:\n            tracking_email_id = int(\n                message.get(\n                    \"X-Odoo-MailTracking-ID\",\n                    # Deprecated tracking header, kept as fallback\n                    message[\"X-Odoo-Tracking-ID\"],\n                )\n            )\n        except (TypeError, ValueError, KeyError):\n            tracking_email_id = False\n        return self.env[\"mail.tracking.email\"].browse(tracking_email_id)\n\n    def _smtp_server_get(self, mail_server_id, smtp_server):\n        smtp_server_used = False\n        mail_server = None\n        if mail_server_id:\n            mail_server = self.browse(mail_server_id)\n        elif not smtp_server:\n            mail_server_ids = self.search([], order=\"sequence\", limit=1)\n            mail_server = mail_server_ids[0] if mail_server_ids else None\n        if mail_server:\n            smtp_server_used = mail_server.smtp_host\n        else:\n            smtp_server_used = smtp_server or tools.config.get(\"smtp_server\")\n        return smtp_server_used\n\n    @api.model\n    def send_email(\n        self,\n        message,\n        mail_server_id=None,\n        smtp_server=None,\n        smtp_port=None,\n        smtp_user=None,\n        smtp_password=None,\n        smtp_encryption=None,\n        smtp_debug=False,\n        smtp_session=None,\n    ):\n        message_id = False\n        tracking_email = self._tracking_email_get(message)\n        smtp_server_used = self.sudo()._smtp_server_get(mail_server_id, smtp_server)\n        try:\n            message_id = super(IrMailServer, self).send_email(\n                message,\n                mail_server_id=mail_server_id,\n                smtp_server=smtp_server,\n                smtp_port=smtp_port,\n                smtp_user=smtp_user,\n                smtp_password=smtp_password,\n                smtp_encryption=smtp_encryption,\n                smtp_debug=smtp_debug,\n                smtp_session=smtp_session,\n            )\n        except Exception as e:\n            if tracking_email:\n                tracking_email.smtp_error(self, smtp_server_used, e)\n        if message_id and tracking_email:\n            vals = tracking_email._tracking_sent_prepare(\n                self, smtp_server_used, message, message_id\n            )\n            if vals:\n                self.env[\"mail.tracking.event\"].sudo().create(vals)\n        return message_id\n", "path": "mail_tracking/models/ir_mail_server.py"}]}