Skip to content

Commit

Permalink
connect to sql db (#9)
Browse files Browse the repository at this point in the history
Green test
  • Loading branch information
lsancso authored Nov 19, 2024
1 parent c210e66 commit 1d86814
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 17 deletions.
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ repos:
language: system
stages:
- pre-commit
- id: test
name: Run pytest suite
entry: make test
language: system
stages:
- pre-commit
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ env-recreate: build env-start install-test-requirements## Force building project
bash: ## Open a bash shell in project's main container
$(DOCKER_COMMAND) exec app bash

bash-db: ## Open a bash shell in project's main container
$(DOCKER_COMMAND) exec db bash

test: ## Run test suite in project's main container
$(DOCKER_COMMAND) exec -T app pytest -vv --color=yes

Expand Down
19 changes: 19 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,22 @@ services:
- ../src:/app/src
- ../scripts:/app/scripts
- ./assets/gunicorn:/var/gunicorn/
depends_on:
- db
db:
image: postgres:13.17-alpine
volumes:
- pgdata:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=test_password
- POSTGRES_USER=test_username
- POSTGRES_DB=test_db
command: --fsync=off
networks:
default:
aliases:
- db
ports:
- 5432:5432
volumes:
pgdata:
1 change: 1 addition & 0 deletions src/domain/credentials_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
@dataclass(frozen=True)
class CredentialsRequest:
username: str
password: str
instance_name: str
database_name: str
2 changes: 1 addition & 1 deletion src/domain/db_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

class DBRepository(ABC):
@abstractmethod
def create_credentials(self, credentials_request: CredentialsRequest) -> str: ...
def create_credentials(self, credentials_request: CredentialsRequest) -> dict[str, str]: ...
21 changes: 19 additions & 2 deletions src/infrastructure/postgresql_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import psycopg2

from domain.credentials_requests import CredentialsRequest
from domain.db_repository import DBRepository


class PostgresClient(DBRepository):
def create_credentials(self, credentials_request: CredentialsRequest) -> str:
return "ChangeME!"
def __init__(self, admin_user: str, admin_password: str) -> None:
self.admin_user = admin_user
self.admin_password = admin_password

def create_credentials(self, credentials_request: CredentialsRequest) -> dict[str, str]:
DB_URL = f"postgresql://{self.admin_user}:{self.admin_password}@{credentials_request.instance_name}/{credentials_request.database_name}"

conn = psycopg2.connect(dsn=DB_URL)
cur = conn.cursor()
cur.execute(f"CREATE ROLE {credentials_request.username} WITH PASSWORD '{credentials_request.password}'")

cur.execute(f"SELECT rolname FROM pg_roles WHERE rolname = '{credentials_request.username}'")

results = cur.fetchone()
username = results[0]
credentials = {"username": username, "password": credentials_request.password}
return credentials
1 change: 1 addition & 0 deletions src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies = [
"fastapi==0.111.0",
"gunicorn==22.0.0",
"mo-monitoring[fastapi]==1.0.1",
"psycopg2-binary==2.9.10",
]

[tool.setuptools.packages.find]
Expand Down
Empty file added src/tests/conftest.py
Empty file.
2 changes: 2 additions & 0 deletions src/tests/domain/test_credentials_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
def test_credentials_request() -> None:
request = CredentialsRequest(
username="test",
password="test",
instance_name="test-instance",
database_name="test-database",
)
assert request.username == "test"
assert request.instance_name == "test-instance"
assert request.database_name == "test-database"
assert request.password == "test"
19 changes: 11 additions & 8 deletions src/tests/infrastructure/test_db_client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from domain.credentials_requests import CredentialsRequest
from infrastructure.postgresql_client import PostgresClient
from utils.password_generator import PasswordGenerator


class TestDBClient:
def test_create_users_returns_credentials(self) -> None:
request = CredentialsRequest(
username="test",
instance_name="test-instance",
database_name="test-database",
credential_request = CredentialsRequest(
username="patata",
password=PasswordGenerator().generate_password(),
instance_name="db",
database_name="test_db",
)
db_password = "test_password"

test_repo = PostgresClient()
test_repo = PostgresClient(admin_user="test_username", admin_password=db_password)

credentials = test_repo.create_credentials(credentials_request=request)
credentials = test_repo.create_credentials(credentials_request=credential_request)

assert credentials
assert credentials == "ChangeME!"
assert credentials["username"] == credential_request.username
assert credentials["password"] == credential_request.password
Empty file added src/utils/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions src/utils/password_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import secrets


class PasswordGenerator:
def generate_password(self) -> str:
password = secrets.token_urlsafe(32)
return password

0 comments on commit 1d86814

Please sign in to comment.