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

Add in capability to supply user and password with mongodb #1795

Merged
merged 13 commits into from
Jul 14, 2021
Merged
6 changes: 4 additions & 2 deletions docs/databases/mongo.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ None.
```yaml
databases:
mongo:
host: "my host" # (optional) default "localhost"
host: "my_host" # (optional) default "localhost"
port: "12345" # (optional) default "27017"
database: "mydatabase" # (optional) default "opsdroid"
database: "my_database" # (optional) default "opsdroid"
user: "my_user" # (optional)
password: "pwd123!" # (optional)
```

## Usage
Expand Down
17 changes: 15 additions & 2 deletions opsdroid/database/mongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
from opsdroid.database import Database

_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = {"host": str, "port": Any(int, str), "database": str}
CONFIG_SCHEMA = {
"host": str,
"port": Any(int, str),
"database": str,
"user": str,
"password": str,
}


class DatabaseMongo(Database):
Expand Down Expand Up @@ -37,7 +43,14 @@ async def connect(self):
host = self.config.get("host", "localhost")
port = self.config.get("port", "27017")
database = self.config.get("database", "opsdroid")
path = "mongodb://{host}:{port}".format(host=host, port=port)
user = self.config.get("user")
pwd = self.config.get("password")
if user and pwd:
path = "mongodb://{user}:{pwd}@{host}:{port}".format(
user=user, pwd=pwd, host=host, port=port
)
else:
path = "mongodb://{host}:{port}".format(host=host, port=port)
self.client = AsyncIOMotorClient(path)
self.database = self.client[database]
_LOGGER.info("Connected to MongoDB.")
Expand Down
8 changes: 7 additions & 1 deletion opsdroid/database/mongo/tests/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ def test_init(database):


@pytest.mark.asyncio
@pytest.mark.parametrize("config", [{}])
@pytest.mark.parametrize(
"config",
[
{},
{"database": "test_db", "user": "root", "password": "mongo"},
],
)
async def test_connect(database):
"""Test that the mongo database has implemented connect function properly"""
try:
Expand Down