Skip to content

Commit

Permalink
Fix celery#5734 Celery does not consider authMechanism on mongodb bac…
Browse files Browse the repository at this point in the history
…kend URLs (celery#5795)

* Fix celery#5734 Celery does not consider authMechanism on mongodb backend URLs

* Add unit test: test_get_connection_with_authmechanism

* Add unit test: test_get_connection_with_authmechanism_no_username

* Fix errors in Python 2.7

Remove "," after "**" operator
  • Loading branch information
spengjie authored and jeyrce committed Aug 25, 2021
1 parent 30a2e61 commit a3eac73
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions celery/backends/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ def _get_connection(self):
# don't change self.options
conf = dict(self.options)
conf['host'] = host
if self.user:
conf['username'] = self.user
if self.password:
conf['password'] = self.password

self._connection = MongoClient(**conf)

Expand Down
37 changes: 37 additions & 0 deletions t/unit/backends/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
from kombu.exceptions import EncodeError
from pymongo.errors import ConfigurationError

from case import ANY, MagicMock, Mock, mock, patch, sentinel, skip
from celery import states, uuid
Expand Down Expand Up @@ -220,6 +221,42 @@ def test_get_connection_no_connection_mongodb_uri(self):
)
assert sentinel.connection == connection

def test_get_connection_with_authmechanism(self):
with patch('pymongo.MongoClient') as mock_Connection:
self.app.conf.mongodb_backend_settings = None
uri = ('mongodb://'
'celeryuser:celerypassword@'
'localhost:27017/'
'celerydatabase?authMechanism=SCRAM-SHA-256')
mb = MongoBackend(app=self.app, url=uri)
mock_Connection.return_value = sentinel.connection
connection = mb._get_connection()
mock_Connection.assert_called_once_with(
host=['localhost:27017'],
username='celeryuser',
password='celerypassword',
authmechanism='SCRAM-SHA-256',
**mb._prepare_client_options()
)
assert sentinel.connection == connection

def test_get_connection_with_authmechanism_no_username(self):
with patch('pymongo.MongoClient') as mock_Connection:
self.app.conf.mongodb_backend_settings = None
uri = ('mongodb://'
'localhost:27017/'
'celerydatabase?authMechanism=SCRAM-SHA-256')
mb = MongoBackend(app=self.app, url=uri)
mock_Connection.side_effect = ConfigurationError(
'SCRAM-SHA-256 requires a username.')
with pytest.raises(ConfigurationError):
mb._get_connection()
mock_Connection.assert_called_once_with(
host=['localhost:27017'],
authmechanism='SCRAM-SHA-256',
**mb._prepare_client_options()
)

@patch('celery.backends.mongodb.MongoBackend._get_connection')
def test_get_database_no_existing(self, mock_get_connection):
# Should really check for combinations of these two, to be complete.
Expand Down

0 comments on commit a3eac73

Please sign in to comment.