From a8787ce08f9c207f36bd2c59b2cfced8c2da2781 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 01:38:44 +0000 Subject: [PATCH 1/5] Initial plan From b3b0827dfc1e082ff276c23f0d260edd2a714f7e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 01:41:15 +0000 Subject: [PATCH 2/5] Add support for reusing existing MongoDB client Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com> --- casbin_pymongo_adapter/adapter.py | 32 +++++-- .../asynchronous/adapter.py | 32 +++++-- tests/asynchronous/test_adapter.py | 81 ++++++++++++++++++ tests/test_adapter.py | 84 +++++++++++++++++++ 4 files changed, 217 insertions(+), 12 deletions(-) diff --git a/casbin_pymongo_adapter/adapter.py b/casbin_pymongo_adapter/adapter.py index 9cb4beb..ecdcb92 100644 --- a/casbin_pymongo_adapter/adapter.py +++ b/casbin_pymongo_adapter/adapter.py @@ -9,22 +9,42 @@ class Adapter(persist.Adapter): def __init__( self, - uri, - dbname, + uri=None, + dbname=None, collection="casbin_rule", filtered=False, + client=None, + db_name=None, ): """Create an adapter for Mongodb Args: - uri (str): This should be the same requiement as pymongo Client's 'uri' parameter. + uri (str, optional): This should be the same requiement as pymongo Client's 'uri' parameter. See https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient. - dbname (str): Database to store policy. + Required if client is not provided. + dbname (str, optional): Database to store policy. Required if client is not provided. collection (str, optional): Collection of the choosen database. Defaults to "casbin_rule". filtered (bool, optional): Whether to use filtered query. Defaults to False. + client (MongoClient, optional): An existing MongoClient instance to reuse. If provided, uri is ignored. + db_name (str, optional): Database name to use with the provided client. Takes precedence over dbname. """ - client = MongoClient(uri) - db = client[dbname] + # Support both db_name and dbname for backward compatibility + database_name = db_name if db_name is not None else dbname + + if client is not None: + # Use the provided client + if database_name is None: + raise ValueError("db_name or dbname must be provided when using an existing client") + mongo_client = client + else: + # Create a new client from URI + if uri is None: + raise ValueError("uri must be provided when client is not specified") + if database_name is None: + raise ValueError("dbname must be provided when client is not specified") + mongo_client = MongoClient(uri) + + db = mongo_client[database_name] self._collection = db[collection] self._filtered = filtered diff --git a/casbin_pymongo_adapter/asynchronous/adapter.py b/casbin_pymongo_adapter/asynchronous/adapter.py index b124397..35d1702 100644 --- a/casbin_pymongo_adapter/asynchronous/adapter.py +++ b/casbin_pymongo_adapter/asynchronous/adapter.py @@ -10,22 +10,42 @@ class Adapter(AsyncAdapter): def __init__( self, - uri, - dbname, + uri=None, + dbname=None, collection="casbin_rule", filtered=False, + client=None, + db_name=None, ): """Create an adapter for Mongodb Args: - uri (str): This should be the same requiement as pymongo Client's 'uri' parameter. + uri (str, optional): This should be the same requiement as pymongo Client's 'uri' parameter. See https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient. - dbname (str): Database to store policy. + Required if client is not provided. + dbname (str, optional): Database to store policy. Required if client is not provided. collection (str, optional): Collection of the choosen database. Defaults to "casbin_rule". filtered (bool, optional): Whether to use filtered query. Defaults to False. + client (AsyncMongoClient, optional): An existing AsyncMongoClient instance to reuse. If provided, uri is ignored. + db_name (str, optional): Database name to use with the provided client. Takes precedence over dbname. """ - client = AsyncMongoClient(uri) - db = client[dbname] + # Support both db_name and dbname for backward compatibility + database_name = db_name if db_name is not None else dbname + + if client is not None: + # Use the provided client + if database_name is None: + raise ValueError("db_name or dbname must be provided when using an existing client") + mongo_client = client + else: + # Create a new client from URI + if uri is None: + raise ValueError("uri must be provided when client is not specified") + if database_name is None: + raise ValueError("dbname must be provided when client is not specified") + mongo_client = AsyncMongoClient(uri) + + db = mongo_client[database_name] self._collection = db[collection] self._filtered = filtered diff --git a/tests/asynchronous/test_adapter.py b/tests/asynchronous/test_adapter.py index a86ab9b..e1aa72d 100644 --- a/tests/asynchronous/test_adapter.py +++ b/tests/asynchronous/test_adapter.py @@ -397,3 +397,84 @@ async def test_update_policies(self): self.assertFalse(e.enforce("data2_admin", "data2", "write")) self.assertTrue(e.enforce("data2_admin", "data_test", "write")) + + async def test_adapter_with_existing_client(self): + """ + test adapter with existing AsyncMongoClient using client parameter + """ + # Create an AsyncMongoClient instance + client = AsyncMongoClient("mongodb://localhost:27017") + + # Create adapter with existing client + adapter = Adapter(client=client, db_name="casbin_test") + + e = casbin.AsyncEnforcer(get_fixture("rbac_model.conf"), adapter) + model = e.get_model() + + model.clear_policy() + model.add_policy("p", "p", ["alice", "data1", "read"]) + await adapter.save_policy(model) + + # reload policies from database + await e.load_policy() + + self.assertTrue(e.enforce("alice", "data1", "read")) + self.assertFalse(e.enforce("alice", "data1", "write")) + + # Clean up + await client.drop_database("casbin_test") + + async def test_adapter_with_existing_client_and_dbname(self): + """ + test adapter with existing AsyncMongoClient using dbname parameter for backward compatibility + """ + # Create an AsyncMongoClient instance + client = AsyncMongoClient("mongodb://localhost:27017") + + # Create adapter with existing client using dbname instead of db_name + adapter = Adapter(client=client, dbname="casbin_test") + + e = casbin.AsyncEnforcer(get_fixture("rbac_model.conf"), adapter) + model = e.get_model() + + model.clear_policy() + model.add_policy("p", "p", ["bob", "data2", "write"]) + await adapter.save_policy(model) + + # reload policies from database + await e.load_policy() + + self.assertTrue(e.enforce("bob", "data2", "write")) + self.assertFalse(e.enforce("bob", "data2", "read")) + + # Clean up + await client.drop_database("casbin_test") + + async def test_adapter_with_client_requires_db_name(self): + """ + test that adapter with client parameter requires db_name or dbname + """ + client = AsyncMongoClient("mongodb://localhost:27017") + + with self.assertRaises(ValueError) as context: + adapter = Adapter(client=client) + + self.assertIn("db_name or dbname must be provided", str(context.exception)) + + async def test_adapter_without_client_requires_uri(self): + """ + test that adapter without client parameter requires uri + """ + with self.assertRaises(ValueError) as context: + adapter = Adapter(dbname="casbin_test") + + self.assertIn("uri must be provided", str(context.exception)) + + async def test_adapter_without_client_requires_dbname(self): + """ + test that adapter without client parameter requires dbname + """ + with self.assertRaises(ValueError) as context: + adapter = Adapter(uri="mongodb://localhost:27017") + + self.assertIn("dbname must be provided", str(context.exception)) diff --git a/tests/test_adapter.py b/tests/test_adapter.py index 523ed5e..f0c883f 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -427,3 +427,87 @@ def test_repr(self): self.assertEqual(repr(rule), '') # adapter.save_policy(rule) # self.assertRegex(repr(rule), r'') + + def test_adapter_with_existing_client(self): + """ + test adapter with existing MongoClient using client parameter + """ + # Create a MongoClient instance + client = MongoClient("mongodb://localhost:27017") + + # Create adapter with existing client + adapter = Adapter(client=client, db_name="casbin_test") + + e = casbin.Enforcer(get_fixture("rbac_model.conf"), adapter) + model = e.get_model() + + model.clear_policy() + model.add_policy("p", "p", ["alice", "data1", "read"]) + adapter.save_policy(model) + + # reload policies from database + e.load_policy() + + self.assertTrue(e.enforce("alice", "data1", "read")) + self.assertFalse(e.enforce("alice", "data1", "write")) + + # Clean up + client.drop_database("casbin_test") + client.close() + + def test_adapter_with_existing_client_and_dbname(self): + """ + test adapter with existing MongoClient using dbname parameter for backward compatibility + """ + # Create a MongoClient instance + client = MongoClient("mongodb://localhost:27017") + + # Create adapter with existing client using dbname instead of db_name + adapter = Adapter(client=client, dbname="casbin_test") + + e = casbin.Enforcer(get_fixture("rbac_model.conf"), adapter) + model = e.get_model() + + model.clear_policy() + model.add_policy("p", "p", ["bob", "data2", "write"]) + adapter.save_policy(model) + + # reload policies from database + e.load_policy() + + self.assertTrue(e.enforce("bob", "data2", "write")) + self.assertFalse(e.enforce("bob", "data2", "read")) + + # Clean up + client.drop_database("casbin_test") + client.close() + + def test_adapter_with_client_requires_db_name(self): + """ + test that adapter with client parameter requires db_name or dbname + """ + client = MongoClient("mongodb://localhost:27017") + + with self.assertRaises(ValueError) as context: + adapter = Adapter(client=client) + + self.assertIn("db_name or dbname must be provided", str(context.exception)) + client.close() + + def test_adapter_without_client_requires_uri(self): + """ + test that adapter without client parameter requires uri + """ + with self.assertRaises(ValueError) as context: + adapter = Adapter(dbname="casbin_test") + + self.assertIn("uri must be provided", str(context.exception)) + + def test_adapter_without_client_requires_dbname(self): + """ + test that adapter without client parameter requires dbname + """ + with self.assertRaises(ValueError) as context: + adapter = Adapter(uri="mongodb://localhost:27017") + + self.assertIn("dbname must be provided", str(context.exception)) From ae2d637e0647821d09cdfb2e5dc9e6de615e2434 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 01:42:12 +0000 Subject: [PATCH 3/5] Update README with examples of using existing client Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com> --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index e7d935f..396593d 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,24 @@ filter.raw_query = { e.load_filtered_policy(filter) ``` +## Using an Existing MongoDB Client + +If you already have a MongoDB client instance in your application, you can reuse it: + +```python +from pymongo import MongoClient +import casbin_pymongo_adapter +import casbin + +# Create or use your existing MongoDB client +mongo_client = MongoClient('mongodb://localhost:27017/') + +# Pass the client to the adapter +adapter = casbin_pymongo_adapter.Adapter(client=mongo_client, db_name="dbname") + +e = casbin.Enforcer('path/to/model.conf', adapter, True) +``` + ## Async Example ```python @@ -73,6 +91,23 @@ e = casbin.AsyncEnforcer('path/to/model.conf', adapter) await e.load_policy() ``` +### Using an Existing AsyncMongoClient + +```python +from pymongo import AsyncMongoClient +from casbin_pymongo_adapter.asynchronous import Adapter +import casbin + +# Create or use your existing AsyncMongoClient +mongo_client = AsyncMongoClient('mongodb://localhost:27017/') + +# Pass the client to the adapter +adapter = Adapter(client=mongo_client, db_name="dbname") +e = casbin.AsyncEnforcer('path/to/model.conf', adapter) + +await e.load_policy() +``` + ### Getting Help From 998573a81695d4616c75938cd5683aa5a2df8b16 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 01:45:01 +0000 Subject: [PATCH 4/5] Address code review feedback: improve docs and fix resource cleanup Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com> --- README.md | 4 +- casbin_pymongo_adapter/adapter.py | 3 + .../asynchronous/adapter.py | 3 + tests/asynchronous/test_adapter.py | 89 ++++++++++--------- 4 files changed, 57 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 396593d..783db87 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ import casbin mongo_client = MongoClient('mongodb://localhost:27017/') # Pass the client to the adapter -adapter = casbin_pymongo_adapter.Adapter(client=mongo_client, db_name="dbname") +adapter = casbin_pymongo_adapter.Adapter(client=mongo_client, db_name="casbin") e = casbin.Enforcer('path/to/model.conf', adapter, True) ``` @@ -102,7 +102,7 @@ import casbin mongo_client = AsyncMongoClient('mongodb://localhost:27017/') # Pass the client to the adapter -adapter = Adapter(client=mongo_client, db_name="dbname") +adapter = Adapter(client=mongo_client, db_name="casbin") e = casbin.AsyncEnforcer('path/to/model.conf', adapter) await e.load_policy() diff --git a/casbin_pymongo_adapter/adapter.py b/casbin_pymongo_adapter/adapter.py index ecdcb92..d586d9b 100644 --- a/casbin_pymongo_adapter/adapter.py +++ b/casbin_pymongo_adapter/adapter.py @@ -27,6 +27,9 @@ def __init__( filtered (bool, optional): Whether to use filtered query. Defaults to False. client (MongoClient, optional): An existing MongoClient instance to reuse. If provided, uri is ignored. db_name (str, optional): Database name to use with the provided client. Takes precedence over dbname. + + Note: + When both client and uri are provided, client takes precedence and uri is ignored. """ # Support both db_name and dbname for backward compatibility database_name = db_name if db_name is not None else dbname diff --git a/casbin_pymongo_adapter/asynchronous/adapter.py b/casbin_pymongo_adapter/asynchronous/adapter.py index 35d1702..bea2772 100644 --- a/casbin_pymongo_adapter/asynchronous/adapter.py +++ b/casbin_pymongo_adapter/asynchronous/adapter.py @@ -28,6 +28,9 @@ def __init__( filtered (bool, optional): Whether to use filtered query. Defaults to False. client (AsyncMongoClient, optional): An existing AsyncMongoClient instance to reuse. If provided, uri is ignored. db_name (str, optional): Database name to use with the provided client. Takes precedence over dbname. + + Note: + When both client and uri are provided, client takes precedence and uri is ignored. """ # Support both db_name and dbname for backward compatibility database_name = db_name if db_name is not None else dbname diff --git a/tests/asynchronous/test_adapter.py b/tests/asynchronous/test_adapter.py index e1aa72d..f373f38 100644 --- a/tests/asynchronous/test_adapter.py +++ b/tests/asynchronous/test_adapter.py @@ -405,24 +405,27 @@ async def test_adapter_with_existing_client(self): # Create an AsyncMongoClient instance client = AsyncMongoClient("mongodb://localhost:27017") - # Create adapter with existing client - adapter = Adapter(client=client, db_name="casbin_test") - - e = casbin.AsyncEnforcer(get_fixture("rbac_model.conf"), adapter) - model = e.get_model() - - model.clear_policy() - model.add_policy("p", "p", ["alice", "data1", "read"]) - await adapter.save_policy(model) - - # reload policies from database - await e.load_policy() - - self.assertTrue(e.enforce("alice", "data1", "read")) - self.assertFalse(e.enforce("alice", "data1", "write")) - - # Clean up - await client.drop_database("casbin_test") + try: + # Create adapter with existing client + adapter = Adapter(client=client, db_name="casbin_test") + + e = casbin.AsyncEnforcer(get_fixture("rbac_model.conf"), adapter) + model = e.get_model() + + model.clear_policy() + model.add_policy("p", "p", ["alice", "data1", "read"]) + await adapter.save_policy(model) + + # reload policies from database + await e.load_policy() + + self.assertTrue(e.enforce("alice", "data1", "read")) + self.assertFalse(e.enforce("alice", "data1", "write")) + + # Clean up + await client.drop_database("casbin_test") + finally: + client.close() async def test_adapter_with_existing_client_and_dbname(self): """ @@ -431,24 +434,27 @@ async def test_adapter_with_existing_client_and_dbname(self): # Create an AsyncMongoClient instance client = AsyncMongoClient("mongodb://localhost:27017") - # Create adapter with existing client using dbname instead of db_name - adapter = Adapter(client=client, dbname="casbin_test") - - e = casbin.AsyncEnforcer(get_fixture("rbac_model.conf"), adapter) - model = e.get_model() - - model.clear_policy() - model.add_policy("p", "p", ["bob", "data2", "write"]) - await adapter.save_policy(model) - - # reload policies from database - await e.load_policy() - - self.assertTrue(e.enforce("bob", "data2", "write")) - self.assertFalse(e.enforce("bob", "data2", "read")) - - # Clean up - await client.drop_database("casbin_test") + try: + # Create adapter with existing client using dbname instead of db_name + adapter = Adapter(client=client, dbname="casbin_test") + + e = casbin.AsyncEnforcer(get_fixture("rbac_model.conf"), adapter) + model = e.get_model() + + model.clear_policy() + model.add_policy("p", "p", ["bob", "data2", "write"]) + await adapter.save_policy(model) + + # reload policies from database + await e.load_policy() + + self.assertTrue(e.enforce("bob", "data2", "write")) + self.assertFalse(e.enforce("bob", "data2", "read")) + + # Clean up + await client.drop_database("casbin_test") + finally: + client.close() async def test_adapter_with_client_requires_db_name(self): """ @@ -456,10 +462,13 @@ async def test_adapter_with_client_requires_db_name(self): """ client = AsyncMongoClient("mongodb://localhost:27017") - with self.assertRaises(ValueError) as context: - adapter = Adapter(client=client) - - self.assertIn("db_name or dbname must be provided", str(context.exception)) + try: + with self.assertRaises(ValueError) as context: + adapter = Adapter(client=client) + + self.assertIn("db_name or dbname must be provided", str(context.exception)) + finally: + client.close() async def test_adapter_without_client_requires_uri(self): """ From 9de1ecefe1e77839dde800fa4e321b0eff671703 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 02:21:54 +0000 Subject: [PATCH 5/5] Remove test cases and fix black formatting issues Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com> --- casbin_pymongo_adapter/adapter.py | 10 ++- .../asynchronous/adapter.py | 10 ++- tests/asynchronous/test_adapter.py | 90 ------------------- tests/test_adapter.py | 84 ----------------- 4 files changed, 12 insertions(+), 182 deletions(-) diff --git a/casbin_pymongo_adapter/adapter.py b/casbin_pymongo_adapter/adapter.py index d586d9b..4e4d4c7 100644 --- a/casbin_pymongo_adapter/adapter.py +++ b/casbin_pymongo_adapter/adapter.py @@ -27,17 +27,19 @@ def __init__( filtered (bool, optional): Whether to use filtered query. Defaults to False. client (MongoClient, optional): An existing MongoClient instance to reuse. If provided, uri is ignored. db_name (str, optional): Database name to use with the provided client. Takes precedence over dbname. - + Note: When both client and uri are provided, client takes precedence and uri is ignored. """ # Support both db_name and dbname for backward compatibility database_name = db_name if db_name is not None else dbname - + if client is not None: # Use the provided client if database_name is None: - raise ValueError("db_name or dbname must be provided when using an existing client") + raise ValueError( + "db_name or dbname must be provided when using an existing client" + ) mongo_client = client else: # Create a new client from URI @@ -46,7 +48,7 @@ def __init__( if database_name is None: raise ValueError("dbname must be provided when client is not specified") mongo_client = MongoClient(uri) - + db = mongo_client[database_name] self._collection = db[collection] self._filtered = filtered diff --git a/casbin_pymongo_adapter/asynchronous/adapter.py b/casbin_pymongo_adapter/asynchronous/adapter.py index bea2772..34e8501 100644 --- a/casbin_pymongo_adapter/asynchronous/adapter.py +++ b/casbin_pymongo_adapter/asynchronous/adapter.py @@ -28,17 +28,19 @@ def __init__( filtered (bool, optional): Whether to use filtered query. Defaults to False. client (AsyncMongoClient, optional): An existing AsyncMongoClient instance to reuse. If provided, uri is ignored. db_name (str, optional): Database name to use with the provided client. Takes precedence over dbname. - + Note: When both client and uri are provided, client takes precedence and uri is ignored. """ # Support both db_name and dbname for backward compatibility database_name = db_name if db_name is not None else dbname - + if client is not None: # Use the provided client if database_name is None: - raise ValueError("db_name or dbname must be provided when using an existing client") + raise ValueError( + "db_name or dbname must be provided when using an existing client" + ) mongo_client = client else: # Create a new client from URI @@ -47,7 +49,7 @@ def __init__( if database_name is None: raise ValueError("dbname must be provided when client is not specified") mongo_client = AsyncMongoClient(uri) - + db = mongo_client[database_name] self._collection = db[collection] self._filtered = filtered diff --git a/tests/asynchronous/test_adapter.py b/tests/asynchronous/test_adapter.py index f373f38..a86ab9b 100644 --- a/tests/asynchronous/test_adapter.py +++ b/tests/asynchronous/test_adapter.py @@ -397,93 +397,3 @@ async def test_update_policies(self): self.assertFalse(e.enforce("data2_admin", "data2", "write")) self.assertTrue(e.enforce("data2_admin", "data_test", "write")) - - async def test_adapter_with_existing_client(self): - """ - test adapter with existing AsyncMongoClient using client parameter - """ - # Create an AsyncMongoClient instance - client = AsyncMongoClient("mongodb://localhost:27017") - - try: - # Create adapter with existing client - adapter = Adapter(client=client, db_name="casbin_test") - - e = casbin.AsyncEnforcer(get_fixture("rbac_model.conf"), adapter) - model = e.get_model() - - model.clear_policy() - model.add_policy("p", "p", ["alice", "data1", "read"]) - await adapter.save_policy(model) - - # reload policies from database - await e.load_policy() - - self.assertTrue(e.enforce("alice", "data1", "read")) - self.assertFalse(e.enforce("alice", "data1", "write")) - - # Clean up - await client.drop_database("casbin_test") - finally: - client.close() - - async def test_adapter_with_existing_client_and_dbname(self): - """ - test adapter with existing AsyncMongoClient using dbname parameter for backward compatibility - """ - # Create an AsyncMongoClient instance - client = AsyncMongoClient("mongodb://localhost:27017") - - try: - # Create adapter with existing client using dbname instead of db_name - adapter = Adapter(client=client, dbname="casbin_test") - - e = casbin.AsyncEnforcer(get_fixture("rbac_model.conf"), adapter) - model = e.get_model() - - model.clear_policy() - model.add_policy("p", "p", ["bob", "data2", "write"]) - await adapter.save_policy(model) - - # reload policies from database - await e.load_policy() - - self.assertTrue(e.enforce("bob", "data2", "write")) - self.assertFalse(e.enforce("bob", "data2", "read")) - - # Clean up - await client.drop_database("casbin_test") - finally: - client.close() - - async def test_adapter_with_client_requires_db_name(self): - """ - test that adapter with client parameter requires db_name or dbname - """ - client = AsyncMongoClient("mongodb://localhost:27017") - - try: - with self.assertRaises(ValueError) as context: - adapter = Adapter(client=client) - - self.assertIn("db_name or dbname must be provided", str(context.exception)) - finally: - client.close() - - async def test_adapter_without_client_requires_uri(self): - """ - test that adapter without client parameter requires uri - """ - with self.assertRaises(ValueError) as context: - adapter = Adapter(dbname="casbin_test") - - self.assertIn("uri must be provided", str(context.exception)) - - async def test_adapter_without_client_requires_dbname(self): - """ - test that adapter without client parameter requires dbname - """ - with self.assertRaises(ValueError) as context: - adapter = Adapter(uri="mongodb://localhost:27017") - - self.assertIn("dbname must be provided", str(context.exception)) diff --git a/tests/test_adapter.py b/tests/test_adapter.py index f0c883f..523ed5e 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -427,87 +427,3 @@ def test_repr(self): self.assertEqual(repr(rule), '') # adapter.save_policy(rule) # self.assertRegex(repr(rule), r'') - - def test_adapter_with_existing_client(self): - """ - test adapter with existing MongoClient using client parameter - """ - # Create a MongoClient instance - client = MongoClient("mongodb://localhost:27017") - - # Create adapter with existing client - adapter = Adapter(client=client, db_name="casbin_test") - - e = casbin.Enforcer(get_fixture("rbac_model.conf"), adapter) - model = e.get_model() - - model.clear_policy() - model.add_policy("p", "p", ["alice", "data1", "read"]) - adapter.save_policy(model) - - # reload policies from database - e.load_policy() - - self.assertTrue(e.enforce("alice", "data1", "read")) - self.assertFalse(e.enforce("alice", "data1", "write")) - - # Clean up - client.drop_database("casbin_test") - client.close() - - def test_adapter_with_existing_client_and_dbname(self): - """ - test adapter with existing MongoClient using dbname parameter for backward compatibility - """ - # Create a MongoClient instance - client = MongoClient("mongodb://localhost:27017") - - # Create adapter with existing client using dbname instead of db_name - adapter = Adapter(client=client, dbname="casbin_test") - - e = casbin.Enforcer(get_fixture("rbac_model.conf"), adapter) - model = e.get_model() - - model.clear_policy() - model.add_policy("p", "p", ["bob", "data2", "write"]) - adapter.save_policy(model) - - # reload policies from database - e.load_policy() - - self.assertTrue(e.enforce("bob", "data2", "write")) - self.assertFalse(e.enforce("bob", "data2", "read")) - - # Clean up - client.drop_database("casbin_test") - client.close() - - def test_adapter_with_client_requires_db_name(self): - """ - test that adapter with client parameter requires db_name or dbname - """ - client = MongoClient("mongodb://localhost:27017") - - with self.assertRaises(ValueError) as context: - adapter = Adapter(client=client) - - self.assertIn("db_name or dbname must be provided", str(context.exception)) - client.close() - - def test_adapter_without_client_requires_uri(self): - """ - test that adapter without client parameter requires uri - """ - with self.assertRaises(ValueError) as context: - adapter = Adapter(dbname="casbin_test") - - self.assertIn("uri must be provided", str(context.exception)) - - def test_adapter_without_client_requires_dbname(self): - """ - test that adapter without client parameter requires dbname - """ - with self.assertRaises(ValueError) as context: - adapter = Adapter(uri="mongodb://localhost:27017") - - self.assertIn("dbname must be provided", str(context.exception))