Skip to content

Commit 7a0f6fb

Browse files
committed
✅(backend) fix test suites relying on boto3
In a5b2c2d, we mocked `socket.getaddrinfo` module but this one is also used by boto3 under the hood, so the mock brokes all tests using boto3.
1 parent 9a44d2c commit 7a0f6fb

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/backend/core/tests/api/test_messages_import.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# pylint: disable=redefined-outer-name, unused-argument, no-value-for-parameter, too-many-lines
33

44
import datetime
5-
import socket
65
from unittest.mock import patch
76

87
from django.core.files.storage import storages
@@ -27,14 +26,14 @@ def _mock_ssrf_dns():
2726
2827
The IMAP endpoint validates the server hostname via
2928
``core.services.ssrf.validate_hostname``; tests use unresolvable fixtures
30-
like ``imap.example.com`` so we return a public IP to reach the mocked
31-
IMAP task code.
29+
like ``imap.example.com`` so we bypass validation to reach the mocked IMAP
30+
task code. We patch the symbol imported into
31+
``core.services.importer.imap`` rather than ``socket.getaddrinfo``, which
32+
would also break real DNS lookups (e.g. boto3 reaching the S3 bucket).
3233
"""
3334
with patch(
34-
"core.services.ssrf.socket.getaddrinfo",
35-
return_value=[
36-
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0))
37-
],
35+
"core.services.importer.imap.validate_hostname",
36+
return_value=["93.184.216.34"],
3837
):
3938
yield
4039

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Shared fixtures for importer tests."""
22

3-
import socket
43
from unittest import mock
54

65
import pytest
@@ -11,14 +10,14 @@ def _mock_ssrf_dns():
1110
"""Short-circuit SSRF DNS validation for IMAP tests.
1211
1312
The IMAP import path validates the server hostname via
14-
``core.services.ssrf.validate_hostname`` which calls ``socket.getaddrinfo``.
15-
Test fixtures use unresolvable hostnames like ``imap.example.com``, so we
16-
return a valid public IP here to let tests reach the mocked IMAP code.
13+
``core.services.ssrf.validate_hostname``. Test fixtures use unresolvable
14+
hostnames like ``imap.example.com``, so we bypass validation here to let
15+
tests reach the mocked IMAP code. We patch the symbol imported into
16+
``core.services.importer.imap`` rather than ``socket.getaddrinfo``, which
17+
would also break real DNS lookups (e.g. boto3 reaching the S3 bucket).
1718
"""
1819
with mock.patch(
19-
"core.services.ssrf.socket.getaddrinfo",
20-
return_value=[
21-
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0))
22-
],
20+
"core.services.importer.imap.validate_hostname",
21+
return_value=["93.184.216.34"],
2322
):
2423
yield

src/backend/messages/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,8 @@ class Test(Base):
12391239
# Add a test encryption key for django-fernet-encrypted-fields
12401240
SALT_KEY = ["test-salt-for-development-only"]
12411241

1242+
FEATURE_MAILBOX_ADMIN_CHANNELS = ["api_key", "widget"]
1243+
12421244
SCHEMA_CUSTOM_ATTRIBUTES_USER = {}
12431245
SCHEMA_CUSTOM_ATTRIBUTES_MAILDOMAIN = {}
12441246

0 commit comments

Comments
 (0)