forked from photos-network/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_authentication.py
More file actions
60 lines (51 loc) · 2 KB
/
test_authentication.py
File metadata and controls
60 lines (51 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Test."""
import aiohttp_jinja2
import jinja2
import pytest
from aiohttp import web
from aiohttp.test_utils import TestClient, TestServer
from core.authentication import Auth, AuthClient
from core.authentication.auth_database import AuthDatabase
@pytest.mark.asyncio
async def test_authorization_grant(tmp_path):
"""test oauth authorization grant."""
# Frontend as client_server
async def handler(request):
return web.Response(text="Frontend is running")
client_app = web.Application()
client_app.router.add_get("/", handler)
client_server = TestServer(client_app)
await client_server.start_server()
redirect = f"http://{client_server.host}:{client_server.port}"
# Core as authorization_server
application = web.Application()
aiohttp_jinja2.setup(
application, loader=jinja2.FileSystemLoader("core/webserver/templates")
)
database_file = tmp_path / "system.sqlite3"
file_object = open(database_file, "w")
file_object.close()
auth_database = AuthDatabase(database_file)
auth = Auth(application, auth_database)
auth.add_client(
AuthClient(
client_name="Frontend",
client_id="a12b345c",
client_secret="ABcD1E2F",
redirect_uris=[redirect],
)
)
authorization_server = TestServer(application)
async with TestClient(authorization_server) as auth_client:
await auth_client.start_server()
get_resp = await auth_client.get(
f"/oauth/authorize?client_id=a12b345c&response_type=code&redirect_uri={redirect}&scope=openid+profile+email+phone"
)
assert get_resp.status == 200
text = await get_resp.text()
assert "access the users public profile" in text
resp = await auth_client.post(
f"/oauth/authorize?client_id=a12b345c&response_type=code&redirect_uri={redirect}&scope=openid+profile+email+phone",
data={"uname": "admin", "password": "admin"},
)
assert resp.status == 200