Skip to content

Commit

Permalink
Merge pull request #54 from TogetherCrew/fix/credentials-passing
Browse files Browse the repository at this point in the history
fix: update mongodb credentials!
  • Loading branch information
cyri113 authored May 21, 2024
2 parents 9f11973 + 0d4aec6 commit 88efcc1
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 57 deletions.
8 changes: 4 additions & 4 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ services:
dockerfile: Dockerfile
environment:
- PORT=3000
- DB_HOST=mongo
- DB_PORT=27017
- DB_USER=root
- MONGODB_HOST=mongo
- MONGODB_PORT=27017
- MONGODB_USER=root
- DB_SAGA_NAME=Saga
- DB_SAGA_COLLECTION_NAME=saga
- DB_PASSWORD=pass
- MONGODB_PASS=pass
- RABBIT_HOST=rabbitmq
- RABBIT_PORT=5672
- RABBIT_USER=root
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="tc-messageBroker",
version="1.6.6",
version="1.6.7",
author="Mohammad Amin Dadgar, RnDAO",
maintainer="Mohammad Amin Dadgar",
maintainer_email="[email protected]",
Expand Down
34 changes: 26 additions & 8 deletions tc_messageBroker/rabbit_mq/saga/saga_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
from tc_messageBroker.rabbit_mq.db_operations import MongoDB
from tc_messageBroker.rabbit_mq.status import Status
from tc_messageBroker.utils.credentials import load_mongo_credentials

from .choreography_base import IChoreography
from .transaction_base import ITransaction
Expand Down Expand Up @@ -56,8 +57,8 @@ def next(
self,
publish_method: Callable,
call_function: Callable,
mongo_creds: dict[str, Any],
test_mode=False,
mongo_creds: dict[str, Any] | None = None,
**kwargs,
):
"""
calling the next transaction within the saga
Expand All @@ -69,13 +70,30 @@ def next(
the publish methods that are from the RabbitMQ
call_function : Callable
a function to be called when the message recieved
mongo_creds : dict[str, Any]
the mongodb credentials to update the db
the keys must be `connection_str`, `db_name`, and `collection_name`
test_mode : bool
testing the function indicates that we wouldn't read or write on DB
default is False
**kwargs :
test_mode : bool
testing the function indicates that we wouldn't read or write on DB
default is False
mongo_creds : dict[str, Any] | None
the mongodb credentials to update the db
if not `None`, the keys must be `connection_str`, `db_name`, and `collection_name`
saga_db_name : str
the saga database that data is going to be saved within it
default would be `Saga`
saga_collection_name : str
the saga collection that data is going to be saved within it
default would be `sagas`
"""
test_mode = kwargs.get("test_mode", False)
mongo_creds = kwargs.get("mongo_creds", None)
saga_db_name = kwargs.get("saga_db_name", "Saga")
saga_collection_name = kwargs.get("saga_db_name", "sagas")

if mongo_creds is None:
mongo_creds = load_mongo_credentials()
mongo_creds["db_name"] = saga_db_name
mongo_creds["collection_name"] = saga_collection_name

tx_sorted, tx_not_started_count = self._sort_transactions(
self.choreography.transactions
)
Expand Down
Empty file.
38 changes: 38 additions & 0 deletions tc_messageBroker/utils/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os

from dotenv import load_dotenv


def load_mongo_credentials():
"""
load mongo db credentials from .env
Returns:
---------
mongo_creds : dict[str, Any]
mongodb credentials
a dictionary representive of
`user`: str
`password` : str
`host` : str
`port` : int
`connection_str`: str
"""
load_dotenv()

mongo_creds = {}

user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")

mongo_creds["user"] = user
mongo_creds["password"] = password
mongo_creds["host"] = host
mongo_creds["port"] = port

connection = f"mongodb://{user}:{password}@{host}:{port}"
mongo_creds["connection_str"] = connection

return mongo_creds
56 changes: 28 additions & 28 deletions tests/integration/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

def test_insert_one():
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_url = f"mongodb://{user}:{password}@{host}:{port}"

Expand All @@ -30,10 +30,10 @@ def test_insert_one():

def test_insert_multiple():
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_url = f"mongodb://{user}:{password}@{host}:{port}"
db_name = os.getenv("DB_SAGA_NAME")
Expand All @@ -56,10 +56,10 @@ def test_read_count_one():
read from db after writing using the previous function (above)
"""
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_url = f"mongodb://{user}:{password}@{host}:{port}"
db_name = os.getenv("DB_SAGA_NAME")
Expand Down Expand Up @@ -93,10 +93,10 @@ def test_read_count_multiple():
read from db after writing using the previous function (above)
"""
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_url = f"mongodb://{user}:{password}@{host}:{port}"
db_name = os.getenv("DB_SAGA_NAME")
Expand Down Expand Up @@ -135,10 +135,10 @@ def test_replace():
test replacing a document we just inserted
"""
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_url = f"mongodb://{user}:{password}@{host}:{port}"
db_name = os.getenv("DB_SAGA_NAME")
Expand Down Expand Up @@ -178,10 +178,10 @@ def test_delete():
delete all except one of the ligitimate data which we choose
"""
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_url = f"mongodb://{user}:{password}@{host}:{port}"
db_name = os.getenv("DB_SAGA_NAME")
Expand All @@ -201,10 +201,10 @@ def test_wrong_input():
give wrong input to insert
"""
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_url = f"mongodb://{user}:{password}@{host}:{port}"
db_name = os.getenv("DB_SAGA_NAME")
Expand Down
16 changes: 8 additions & 8 deletions tests/integration/test_saga.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

def test_inputs():
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")
saga_db = os.getenv("DB_SAGA_NAME")
saga_collection = os.getenv("DB_SAGA_COLLECTION_NAME")

Expand All @@ -48,10 +48,10 @@ def test_saga_update():
test updating a saga instance in db
"""
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_creds = {}
connection_creds["connection_str"] = f"mongodb://{user}:{password}@{host}:{port}"
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from unittest import TestCase

from tc_messageBroker.utils.credentials import load_mongo_credentials


class TestCredentialsLoading(TestCase):
def test_non_none_values(self):
creds = load_mongo_credentials()
self.assertIsNotNone(creds["user"])
self.assertIsNotNone(creds["password"])
self.assertIsNotNone(creds["host"])
self.assertIsNotNone(creds["port"])
self.assertNotEqual(creds["connection_str"], "mongodb://None:None@None:None")
16 changes: 8 additions & 8 deletions tests/unit/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

def test_mongo_class():
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

connection_url = f"{user}:{password}@{host}:{port}"
db_name = os.getenv("DB_SAGA_NAME")
Expand All @@ -26,10 +26,10 @@ def test_mongo_class():

def test_credentials():
load_dotenv()
host = os.getenv("DB_HOST")
port = os.getenv("DB_PORT")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
host = os.getenv("MONGODB_HOST")
port = os.getenv("MONGODB_PORT")
user = os.getenv("MONGODB_USER")
password = os.getenv("MONGODB_PASS")

db_name = os.getenv("DB_SAGA_NAME")
collection_name = os.getenv("DB_SAGA_COLLECTION_NAME")
Expand Down

0 comments on commit 88efcc1

Please sign in to comment.