Test refactoring to manage route testing with flask-login
continuous-integration/drone/push Build is passing Details

master
Drew Bednar 1 year ago
parent d728bda7be
commit abc8e54634

@ -1,5 +1,11 @@
# Notes
## Flask Login
- [This basic usage article](https://realpython.com/using-flask-login-for-user-management-with-flask/) was a good refresher. I forgot that you need a couple of methods on the User class to use it.
- It was tougher to write tests until...I RTFM'd. https://flask-login.readthedocs.io/en/latest/#automated-testing
## Toasts using Toastify.js
[In depth example of a Toast](https://www.cssscript.com/simple-vanilla-javascript-toast-notification-library-toastify/)

@ -1,7 +1,9 @@
import pytest
from flask_login.test_client import FlaskLoginClient
from htmx_contact import create_app
from htmx_contact.config import ContactSettings
from htmx_contact.models import User
@pytest.fixture()
@ -9,9 +11,15 @@ def test_config():
return ContactSettings(SECRET_KEY=b"secrettestingvalue")
@pytest.fixture()
def test_user():
return User(id=1, primary_email="test", password="password", username="test")
@pytest.fixture()
def app(test_config):
app = create_app(config=test_config)
app.test_client_class = FlaskLoginClient
app.config.update(
{
"TESTING": True,
@ -26,10 +34,15 @@ def app(test_config):
@pytest.fixture()
def client(app):
def anynomous_client(app):
return app.test_client()
@pytest.fixture()
def client(app, test_user):
return app.test_client(user=test_user)
@pytest.fixture()
def runner(app):
return app.test_cli_runner()

@ -4,7 +4,11 @@ def test_index_redirect_to_contacts(client):
assert response.headers.get("Location") == "/contacts"
def test_login_get_contacts(anynomous_client):
response = anynomous_client.get("/contacts", follow_redirects=False)
assert response.status_code == 302
def test_get_contacts(client):
response = client.get("/contacts")
response = client.get("/contacts", follow_redirects=True)
assert response.status_code == 200
assert "htmx" in response.text.lower()

Loading…
Cancel
Save