feat: password model

This commit is contained in:
Ruidy 2021-08-03 20:23:58 +02:00
parent a1d4ef498b
commit 668ac34dbc
8 changed files with 46 additions and 9 deletions

View file

@ -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()

View file

@ -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])

0
app/models/__init__.py Normal file
View file

10
app/models/password.py Normal file
View file

@ -0,0 +1,10 @@
from pydantic import BaseModel, SecretStr
class Password(BaseModel):
id: int
service: str
password: SecretStr
class Config:
anystr_strip_whitespace = True

View file

@ -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:

View file

@ -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:

View file

@ -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]:
...

View file

@ -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