emmett-framework / emmett

The web framework for inventors
Other
1.03k stars 70 forks source link

Bloggy examples: test failing #418

Closed max-bertinetti closed 2 years ago

max-bertinetti commented 2 years ago

Hi @gi0baro...sorry to bother you againg...

The actual test file in the bloggy example is failing 4 tests of 4...

I changed this way but still test_no_admin_access is failing... FAILED test.py::test_no_admin_access - assert 200 == 303

# -*- coding: utf-8 -*-

from multiprocessing import context
import pytest

from emmett import request, response, session, T
from emmett.orm.migrations.utils import generate_runtime_migration
from bloggy import app, db, User, auth, setup_admin

@pytest.fixture()
def client():
    return app.test_client()

@pytest.fixture(scope='module', autouse=True)
def _prepare_db(request):
    with db.connection():
        migration = generate_runtime_migration(db)
        migration.up()
        setup_admin()
    yield
    with db.connection():
        User.all().delete()
        auth.delete_group('admin')
        migration.down()

@pytest.fixture(scope='module')
def logged_client():
    c = app.test_client()
    with c.get('/auth/login').context as ctx:
        c.post('/auth/login', data={
            'email': 'doc@emmettbrown.com',
            'password': 'fluxcapacitor',
            '_csrf_token': list(ctx.session._csrf)[-1]
        }, follow_redirects=True)
        return c

def test_empty_db(client):
    r = client.get('/')
    assert 'No posts here so far' in r.data

def test_login(logged_client):
    r = logged_client.get('/')
    assert r.context.session.auth.user is not None

def test_no_admin_access(client):
    r = client.get('/new')
    assert r.context.response.status == 303

def test_admin_access(logged_client):
    r = logged_client.get('/new')
    assert r.context.response.status == 200

I suspect it is following the redirect and so it receive a 200 code.

Thanks in advance

gi0baro commented 2 years ago

@eclecticlly you right, the tests code in the bloggy example was not updated accordingly during releases.

Thanks to your tests, I found the 303 issue is actually dependant of a bug in the testing client context which is not cloning the response data accordingly. Can you open up a PR with the edits you made in bloggy tests? I will push on that also the fix for the testing client.