Version 0.9.7

This commit is contained in:
Tom Christie 2018-12-07 14:57:18 +00:00
parent 99921cef70
commit 256b6245f7
4 changed files with 42 additions and 36 deletions

View File

@ -1,3 +1,7 @@
## 0.9.7
* Ensure that `AuthenticationMiddleware` handles lifespan messages correctly.
## 0.9.6 ## 0.9.6
* Add `AuthenticationMiddleware`, and `@requires()` decorator. * Add `AuthenticationMiddleware`, and `@requires()` decorator.

View File

@ -1 +1 @@
__version__ = "0.9.6" __version__ = "0.9.7"

View File

@ -17,7 +17,9 @@ class AuthenticationMiddleware:
self.backend = backend self.backend = backend
def __call__(self, scope: Scope) -> ASGIInstance: def __call__(self, scope: Scope) -> ASGIInstance:
return functools.partial(self.asgi, scope=scope) if scope["type"] in ["http", "websockets"]:
return functools.partial(self.asgi, scope=scope)
return self.app(scope)
async def asgi(self, receive: Receive, send: Send, scope: Scope) -> None: async def asgi(self, receive: Receive, send: Send, scope: Scope) -> None:
request = Request(scope, receive=receive) request = Request(scope, receive=receive)

View File

@ -89,52 +89,52 @@ def admin(request):
) )
client = TestClient(app)
def test_user_interface(): def test_user_interface():
response = client.get("/") with TestClient(app) as client:
assert response.status_code == 200 response = client.get("/")
assert response.json() == {"authenticated": False, "user": ""} assert response.status_code == 200
assert response.json() == {"authenticated": False, "user": ""}
response = client.get("/", auth=("tomchristie", "example")) response = client.get("/", auth=("tomchristie", "example"))
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"authenticated": True, "user": "tomchristie"} assert response.json() == {"authenticated": True, "user": "tomchristie"}
def test_authentication_required(): def test_authentication_required():
response = client.get("/dashboard") with TestClient(app) as client:
assert response.status_code == 403 response = client.get("/dashboard")
assert response.status_code == 403
response = client.get("/dashboard", auth=("tomchristie", "example")) response = client.get("/dashboard", auth=("tomchristie", "example"))
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"authenticated": True, "user": "tomchristie"} assert response.json() == {"authenticated": True, "user": "tomchristie"}
response = client.get("/dashboard/sync") response = client.get("/dashboard/sync")
assert response.status_code == 403 assert response.status_code == 403
response = client.get("/dashboard/sync", auth=("tomchristie", "example")) response = client.get("/dashboard/sync", auth=("tomchristie", "example"))
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"authenticated": True, "user": "tomchristie"} assert response.json() == {"authenticated": True, "user": "tomchristie"}
response = client.get("/dashboard", headers={"Authorization": "basic foobar"}) response = client.get("/dashboard", headers={"Authorization": "basic foobar"})
assert response.status_code == 400 assert response.status_code == 400
assert response.text == "Invalid basic auth credentials" assert response.text == "Invalid basic auth credentials"
def test_authentication_redirect(): def test_authentication_redirect():
response = client.get("/admin") with TestClient(app) as client:
assert response.status_code == 200 response = client.get("/admin")
assert response.url == "http://testserver/" assert response.status_code == 200
assert response.url == "http://testserver/"
response = client.get("/admin", auth=("tomchristie", "example")) response = client.get("/admin", auth=("tomchristie", "example"))
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"authenticated": True, "user": "tomchristie"} assert response.json() == {"authenticated": True, "user": "tomchristie"}
response = client.get("/admin/sync") response = client.get("/admin/sync")
assert response.status_code == 200 assert response.status_code == 200
assert response.url == "http://testserver/" assert response.url == "http://testserver/"
response = client.get("/admin/sync", auth=("tomchristie", "example")) response = client.get("/admin/sync", auth=("tomchristie", "example"))
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"authenticated": True, "user": "tomchristie"} assert response.json() == {"authenticated": True, "user": "tomchristie"}