-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Green test
- Loading branch information
Showing
12 changed files
with
64 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |