Skip to content

Commit 6397f43

Browse files
committed
pymongo4 updates
1 parent 1ef44e2 commit 6397f43

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

st2auth_mongodb_backend/mongodb.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class MongoDBAuthenticationBackend(object):
4242

4343
_collection_name = 'users'
4444
_indexes = [
45-
('username', pymongo.ASCENDING)
45+
pymongo.IndexModel([('username', pymongo.ASCENDING)], unique=True)
4646
]
4747
_hash_function = hashlib.sha256
4848

@@ -53,12 +53,17 @@ def __init__(self, db_host='localhost', db_port=27017, db_name='st2auth', db_use
5353
self._db_port = db_port
5454
self._db_username = db_username
5555
self._db_password = db_password
56+
self._db_indexes_created = False
5657

57-
self._client = MongoClient(host=self._db_host, port=self._db_port, tz_aware=True)
58-
self._db = self._client[db_name]
59-
58+
kwargs = {}
6059
if self._db_username:
61-
self._db.authenticate(name=self._db_username, password=self._db_password)
60+
kwargs["username"] = self._db_username
61+
kwargs["password"] = self._db_password
62+
63+
self._client = MongoClient(
64+
host=self._db_host, port=self._db_port, tz_aware=True, **kwargs
65+
)
66+
self._db = self._client[db_name]
6267

6368
self._collection = self._db[self._collection_name]
6469
self._ensure_indexes()
@@ -87,4 +92,7 @@ def get_user(self, username):
8792
pass
8893

8994
def _ensure_indexes(self):
90-
self._collection.ensure_index(self._indexes, unique=True)
95+
if self._db_indexes_created:
96+
return
97+
self._collection.create_indexes(self._indexes)
98+
self._db_indexes_created = True

tests/unit/test_mongodb_backend.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ def setUp(self):
3434
self._backend = MongoDBAuthenticationBackend(db_name='st2authtest')
3535

3636
# Clear database
37-
self._backend._collection.remove()
37+
self._backend._collection.delete_many({})
3838

3939
# Add fixtures
40-
for fixture in self.fixtures:
41-
self._backend._collection.insert(fixture)
40+
self._backend._collection.insert_many(self.fixtures)
4241

4342
def tearDown(self):
4443
# Clear database
45-
self._backend._collection.remove()
44+
self._backend._collection.delete_many({})
4645

4746
def test_authenticate(self):
4847
# Inexistent user

0 commit comments

Comments
 (0)