Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer parameters up to specific client constructors for twilio and messagebird #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ SMS_BACKEND = 'sms.backends.messagebird.SmsBackend'
MESSAGEBIRD_ACCESS_KEY = 'live_redacted-messagebird-access-key'
```

The `access_key` (overriding `MESSAGEBIRD_ACCESS_KEY`) and other optional backend keyword parameters transferred from the `get_connection()` call are passed to the `messagebird.Client` constructor.

Make sure the MessageBird Python SDK is installed by running the following command:

```console
Expand All @@ -208,6 +210,8 @@ TWILIO_ACCOUNT_SID = 'live_redacted-twilio-account-sid'
TWILIO_AUTH_TOKEN = 'live_redacted-twilio-auth-token'
```

The `username` and `password` (overriding `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN`) and other optional backend keyword parameters transferred from the `get_connection()` call are passed to the `twilio.rest.Client` constructor.

Make sure the Twilio Python SDK is installed by running the following command:

```console
Expand Down
6 changes: 4 additions & 2 deletions sms/backends/messagebird.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def __init__(self, fail_silently: bool = False, **kwargs) -> None:
"another SMS backend."
)

access_key: Optional[str] = getattr(settings, 'MESSAGEBIRD_ACCESS_KEY')
access_key: Optional[str] = kwargs.pop(
'access_key', getattr(settings, 'MESSAGEBIRD_ACCESS_KEY')
)
if not access_key and not self.fail_silently:
raise ImproperlyConfigured(
"You're using the SMS backend "
Expand All @@ -38,7 +40,7 @@ def __init__(self, fail_silently: bool = False, **kwargs) -> None:

self.client = None
if HAS_MESSAGEBIRD:
self.client = messagebird.Client(access_key)
self.client = messagebird.Client(access_key, **kwargs)

def send_messages(self, messages: List[Message]) -> int:
if not self.client:
Expand Down
10 changes: 7 additions & 3 deletions sms/backends/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ def __init__(self, fail_silently: bool = False, **kwargs) -> None:
"another SMS backend."
)

account_sid: Optional[str] = getattr(settings, 'TWILIO_ACCOUNT_SID')
account_sid: Optional[str] = kwargs.pop(
'username', getattr(settings, 'TWILIO_ACCOUNT_SID')
)
if not account_sid and not self.fail_silently:
raise ImproperlyConfigured(
"You're using the SMS backend "
"'sms.backends.twilio.SmsBackend' without having the "
"setting 'TWILIO_ACCOUNT_SID' set."
)

auth_token: Optional[str] = getattr(settings, 'TWILIO_AUTH_TOKEN')
auth_token: Optional[str] = kwargs.pop(
'password', getattr(settings, 'TWILIO_AUTH_TOKEN')
)
if not auth_token and not self.fail_silently:
raise ImproperlyConfigured(
"You're using the SMS backend "
Expand All @@ -46,7 +50,7 @@ def __init__(self, fail_silently: bool = False, **kwargs) -> None:

self.client = None
if HAS_TWILIO:
self.client = Client(account_sid, auth_token)
self.client = Client(account_sid, auth_token, **kwargs)

def send_messages(self, messages: List[Message]) -> int:
if not self.client:
Expand Down
43 changes: 43 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,26 @@ def test_send_messages(self) -> None:
['+441134960000'],
'Here is the message'
)
self.assertEqual(connection.client.access_key, 'fake_access_key') # type: ignore

def test_send_messages_explicit_backend(self) -> None:
"""Test send_messages with the MessageBird backend with explicit parameters."""
with sms.get_connection( # type: ignore
access_key='another_access_key',
) as connection:
connection.client.message_create = MagicMock() # type: ignore
message = Message(
'Here is the message',
'+12065550100',
['+441134960000'],
connection=connection
).send()
connection.client.message_create.assert_called_with( # type: ignore
'+12065550100',
['+441134960000'],
'Here is the message'
)
self.assertEqual(connection.client.access_key, 'another_access_key') # type: ignore


class TwilioBackendTests(BaseSmsBackendTests, SimpleTestCase):
Expand Down Expand Up @@ -244,6 +264,29 @@ def test_send_messages(self) -> None:
from_='+12065550100',
body='Here is the message'
)
self.assertEqual(connection.client.username, 'fake_account_sid') # type: ignore
self.assertEqual(connection.client.password, 'fake_auth_token') # type: ignore

def test_send_messages_explicit_backend(self) -> None:
"""Test send_messages with the Twilio backend with explicit parameters."""
with sms.get_connection( # type: ignore
username='another_account_sid',
password='another_auth_token',
) as connection:
connection.client.messages.create = MagicMock() # type: ignore
message = Message(
'Here is the message',
'+12065550100',
['+441134960000'],
connection=connection
).send()
connection.client.messages.create.assert_called_with( # type: ignore
to='+441134960000',
from_='+12065550100',
body='Here is the message'
)
self.assertEqual(connection.client.username, 'another_account_sid') # type: ignore
self.assertEqual(connection.client.password, 'another_auth_token') # type: ignore


class SignalTests(SimpleTestCase):
Expand Down