Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Feb 8, 2022
2 parents a647916 + 8bf5c23 commit 8c4e07a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion emmett/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.4.2"
__version__ = "2.4.3"
22 changes: 15 additions & 7 deletions emmett/testing/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@
from .urls import get_host, url_parse, url_unparse


class ClientContext(object):
def __init__(self):
self.request = Request(current.request._scope, None, None)
self.response = Response()
self.response.__dict__.update(current.response.__dict__)
self.session = copy.deepcopy(current.session)
class ClientContextResponse(Response):
def __init__(self, original_response: Response):
super().__init__()
self.status = original_response.status
self.headers._data.update(original_response.headers._data)
self.cookies.update(original_response.cookies.copy())
self.__dict__.update(original_response.__dict__)


class ClientContext:
def __init__(self, ctx):
self.request = Request(ctx.request._scope, None, None)
self.response = ClientContextResponse(ctx.response)
self.session = copy.deepcopy(ctx.session)
self.T = current.T

def __enter__(self):
Expand Down Expand Up @@ -77,7 +85,7 @@ async def dynamic_handler(self, scope, receive, send):
headers=ctx.response.headers
)
finally:
scope['emt.ctx'] = ClientContext()
scope['emt.ctx'] = ClientContext(ctx)
current._close_(ctx_token)
return http

Expand Down
25 changes: 14 additions & 11 deletions examples/bloggy/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

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


Expand All @@ -14,40 +14,43 @@ def 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': 'walter@massivedynamics.com',
'password': 'pocketuniverse',
'email': 'doc@emmettbrown.com',
'password': 'fluxcapacitor',
'_csrf_token': list(ctx.session._csrf)[-1]
}, follow_redirects=True)
return c


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


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


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


def test_admin_access(logged_client):
logged_client.get('/new')
assert response.status == 200
r = logged_client.get('/new')
assert r.context.response.status == 200
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "Emmett"
version = "2.4.2"
version = "2.4.3"
description = "The web framework for inventors"
authors = ["Giovanni Barillari <[email protected]>"]
license = "BSD-3-Clause"
Expand Down

0 comments on commit 8c4e07a

Please sign in to comment.