feat: can read stored passwords

This commit is contained in:
Ruidy 2021-08-03 14:28:58 +02:00
parent bcf359950d
commit a1d4ef498b
9 changed files with 47 additions and 6 deletions

View file

@ -11,5 +11,5 @@ class DB:
def commit(self) -> None:
self.connection.commit()
def execute(self, query: str, *args: Any) -> None:
self.cursor.execute(query, *args)
def execute(self, query: str, *args: Any) -> sqlite3.Cursor:
return self.cursor.execute(query, *args)

View file

@ -1,3 +1,4 @@
from sqlite3 import Cursor
from typing import Any, Protocol
@ -5,5 +6,5 @@ class DBConnector(Protocol):
def commit(self) -> None:
...
def execute(self, query: str, *args: Any) -> None:
def execute(self, query: str, *args: Any) -> Cursor:
...

View file

@ -11,7 +11,7 @@ app = typer.Typer()
@app.command()
def main(
def save(
length: int = typer.Option(
8,
"--length",
@ -57,3 +57,10 @@ def main(
utils.copy_to_clipboard(password)
return typer.echo("The password has been copied to your clipboard 😉\nPaste it using cmd + v")
@app.command()
def read() -> None:
sqlite_repo = sqlite.get_instance()
stored_passwords = sqlite_repo.list_all()
typer.echo(stored_passwords)

View file

@ -5,6 +5,9 @@ class FakeRepository:
def save(self, password: str) -> None:
...
def list_all(self) -> list[str]:
return ["2yW4AcqG", "iK2ZWeqh"]
@classmethod
def get_instance(cls) -> FakeRepository:
return FakeRepository()

View file

@ -13,6 +13,9 @@ class PasswordRepository:
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 get_instance() -> PasswordRepository:
db = sqlite.DB()

View file

@ -4,3 +4,6 @@ from typing import Protocol
class Repository(Protocol):
def save(self, password: str) -> None:
...
def list_all(self) -> list[str]:
...

View file

@ -39,3 +39,7 @@ def _build_characters(symbols: bool, numbers: bool) -> str:
string.digits if numbers else "",
]
)
def list_all_saved_passwords(repo: Repository) -> list[str]:
return repo.list_all()

View file

@ -40,7 +40,19 @@ def test_cli_can_save_to_file() -> None:
assert "2yW4AcqG" in content
def test_cli_can_save_to_db() -> None:
_run_cli()
result = _run_cli_read()
assert "2yW4AcqG" in result.stdout
def _run_cli(*args: Any) -> Result:
result = runner.invoke(app, ["--no-random", *args])
result = runner.invoke(app, ["save", "--no-random", *args])
assert result.exit_code == 0
return result
def _run_cli_read(*args: Any) -> Result:
result = runner.invoke(app, ["read", *args])
assert result.exit_code == 0
return result

View file

@ -1,7 +1,7 @@
import pytest
from app.repositories.fake import FakeRepository
from app.usecases.pass_gen import PassGenOptions, generate_password
from app.usecases.pass_gen import PassGenOptions, generate_password, list_all_saved_passwords
fake_repo = FakeRepository.get_instance()
@ -52,3 +52,11 @@ def test_password_can_contain_symbols(seed: int, symbols: bool, expected: str) -
def test_password_can_contain_numbers(seed: int, numbers: bool, expected: str) -> None:
options = PassGenOptions(seed=seed, numbers=numbers)
assert generate_password(fake_repo, options) == expected
@pytest.mark.parametrize(
"expected",
[["2yW4AcqG", "iK2ZWeqh"]],
)
def test_can_read_all_saved_passwords(expected: list[str]) -> None:
assert list_all_saved_passwords(fake_repo) == expected