diff --git a/tests/conftest.py b/tests/conftest.py index 52daa3b7..812d2534 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,7 @@ from hushline import create_app from hushline.crypto import _SCRYPT_PARAMS from hushline.db import db -from hushline.model import User, Username +from hushline.model import Message, User, Username CONN_FMT_STR = "postgresql+psycopg://hushline:hushline@postgres:5432/{database}" TEMPLATE_DB_NAME = "app_db_template" @@ -163,3 +163,11 @@ def _pgp_user(client: FlaskClient, user: User) -> None: with open("tests/test_pgp_key.txt") as f: user.pgp_key = f.read() db.session.commit() + + +@pytest.fixture() +def message(app: Flask, user: User) -> Message: + msg = Message(content=str(uuid4()), username_id=user.primary_username.id) + db.session.add(msg) + db.session.commit() + return msg diff --git a/tests/test_profile.py b/tests/test_profile.py index 0f18f6e7..aca76a89 100644 --- a/tests/test_profile.py +++ b/tests/test_profile.py @@ -4,7 +4,7 @@ from flask.testing import FlaskClient from hushline.db import db -from hushline.model import Message, User +from hushline.model import Message, User, Username def get_captcha_from_session(client: FlaskClient, username: str) -> str: diff --git a/tests/test_settings.py b/tests/test_settings.py index 620417f0..809b36ac 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,11 +1,12 @@ from unittest.mock import ANY, MagicMock, patch +from uuid import uuid4 import pytest from flask import url_for from flask.testing import FlaskClient from hushline.db import db -from hushline.model import SMTPEncryption, User, Username +from hushline.model import Message, SMTPEncryption, User, Username @pytest.mark.usefixtures("_authenticated_user") @@ -301,3 +302,18 @@ def test_update_smtp_settings_default_forwarding( assert updated_user.smtp_password is None assert updated_user.smtp_encryption.value == SMTPEncryption.default().value assert updated_user.smtp_sender is None + + +@pytest.mark.usefixtures("_authenticated_user") +def test_delete_account(client: FlaskClient, user: User, message: Message) -> None: + # save these because SqlAlchemy is too smart about nullifying them on deletion + user_id = user.id + username_id = user.primary_username.id + msg_id = message.id + + resp = client.post(url_for("settings.delete_account"), follow_redirects=True) + assert resp.status_code == 200 + assert "Your account and all related information have been deleted." in resp.text + assert db.session.scalars(db.select(User).filter_by(id=user_id)).one_or_none() is None + assert db.session.scalars(db.select(Username).filter_by(id=username_id)).one_or_none() is None + assert db.session.scalars(db.select(Message).filter_by(id=msg_id)).one_or_none() is None