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
* 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
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:
request = Request(scope, receive=receive)

View File

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