diff --git a/app/data/sqlite.py b/app/data/sqlite.py index 1d23722..379051a 100644 --- a/app/data/sqlite.py +++ b/app/data/sqlite.py @@ -6,7 +6,10 @@ class DB: def __init__(self, db_str: str = "pg.db") -> None: self.connection = sqlite3.connect(db_str) self.cursor = self.connection.cursor() - self.execute("CREATE TABLE IF NOT EXISTS passwords (password text)") + self.execute( + "CREATE TABLE IF NOT EXISTS passwords (id integer PRIMARY KEY , service text UNIQUE NOT NULL, " + "password text NOT NULL)" + ) def commit(self) -> None: self.connection.commit() diff --git a/app/main.py b/app/main.py index b3cceba..cf432d5 100644 --- a/app/main.py +++ b/app/main.py @@ -63,4 +63,4 @@ def save( def read() -> None: sqlite_repo = sqlite.get_instance() stored_passwords = sqlite_repo.list_all() - typer.echo(stored_passwords) + typer.echo(*[f"{p.service}: {p.password.get_secret_value()}" for p in stored_passwords]) diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/models/password.py b/app/models/password.py new file mode 100644 index 0000000..ae9e2c3 --- /dev/null +++ b/app/models/password.py @@ -0,0 +1,10 @@ +from pydantic import BaseModel, SecretStr + + +class Password(BaseModel): + id: int + service: str + password: SecretStr + + class Config: + anystr_strip_whitespace = True diff --git a/app/repositories/fake.py b/app/repositories/fake.py index e5a4822..66d2868 100644 --- a/app/repositories/fake.py +++ b/app/repositories/fake.py @@ -1,12 +1,19 @@ from __future__ import annotations +from pydantic import SecretStr + +from app.models.password import Password + class FakeRepository: def save(self, password: str) -> None: ... - def list_all(self) -> list[str]: - return ["2yW4AcqG", "iK2ZWeqh"] + def list_all(self) -> list[Password]: + return [ + Password(id=0, service="first", password=SecretStr("2yW4AcqG")), + Password(id=1, service="second", password=SecretStr("iK2ZWeqh")), + ] @classmethod def get_instance(cls) -> FakeRepository: diff --git a/app/repositories/sqlite.py b/app/repositories/sqlite.py index 13e9eba..ef435c4 100644 --- a/app/repositories/sqlite.py +++ b/app/repositories/sqlite.py @@ -1,5 +1,6 @@ import app.data.sqlite as sqlite from app.data.type import DBConnector +from app.models.password import Password class PasswordRepository: @@ -8,13 +9,20 @@ class PasswordRepository: def save(self, password: str) -> None: try: - self.db.execute("INSERT INTO passwords VALUES (:password)", {"password": password}) + self.db.execute( + "INSERT INTO passwords VALUES (null, :service, :password)", + {"service": "service", "password": password}, + ) + self.db.commit() except Exception as e: print(e) - def list_all(self) -> list[str]: - return [row[0] for row in self.db.execute("SELECT * FROM passwords").fetchall()] + def list_all(self) -> list[Password]: + return [ + Password(id=row[0], service=row[1], password=row[2]) + for row in self.db.execute("SELECT * FROM " "passwords").fetchall() + ] def get_instance() -> PasswordRepository: diff --git a/app/repositories/type.py b/app/repositories/type.py index dd56122..179a547 100644 --- a/app/repositories/type.py +++ b/app/repositories/type.py @@ -1,9 +1,11 @@ from typing import Protocol +from app.models.password import Password + class Repository(Protocol): def save(self, password: str) -> None: ... - def list_all(self) -> list[str]: + def list_all(self) -> list[Password]: ... diff --git a/tests/pass_gen_test.py b/tests/pass_gen_test.py index fa43351..882f817 100644 --- a/tests/pass_gen_test.py +++ b/tests/pass_gen_test.py @@ -1,5 +1,7 @@ import pytest +from pydantic import SecretStr +from app.models.password import Password from app.repositories.fake import FakeRepository from app.usecases.pass_gen import PassGenOptions, generate_password, list_all_saved_passwords @@ -56,7 +58,12 @@ def test_password_can_contain_numbers(seed: int, numbers: bool, expected: str) - @pytest.mark.parametrize( "expected", - [["2yW4AcqG", "iK2ZWeqh"]], + [ + [ + Password(id=0, service="first", password=SecretStr("2yW4AcqG")), + Password(id=1, service="second", password=SecretStr("iK2ZWeqh")), + ], + ], ) def test_can_read_all_saved_passwords(expected: list[str]) -> None: assert list_all_saved_passwords(fake_repo) == expected