Skip to content

Commit 83add2b

Browse files
committed
1 parent dd1c723 commit 83add2b

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

tests/test_datasette_auth_github.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datasette.app import Datasette
22
import pytest
3+
import pytest_asyncio
34
import sqlite_utils
45
import re
56

@@ -19,20 +20,23 @@ def mocked_github_api(httpx_mock):
1920
httpx_mock.add_response(
2021
url="https://github.com/login/oauth/access_token",
2122
method="POST",
22-
data="access_token=x_access_token",
23+
content=b"access_token=x_access_token",
24+
is_optional=True,
2325
)
2426
for org, state in (("demouser-org", "active"), ("pending-org", "pending")):
2527
httpx_mock.add_response(
2628
url=re.compile(
2729
r"^https://api.github.com/orgs/{}/memberships/.*".format(org)
2830
),
2931
json={"state": state, "role": "member"},
32+
is_optional=True,
3033
)
3134
# Catch-all for other orgs
3235
httpx_mock.add_response(
3336
url=re.compile(r"^https://api.github.com/orgs/.*/memberships/.*"),
3437
status_code=403,
3538
json={"message": "Not a member"},
39+
is_optional=True,
3640
)
3741
# Team lookups by ID
3842
for team in (
@@ -52,12 +56,14 @@ def mocked_github_api(httpx_mock):
5256
r"^https://api.github.com/orgs/.*/teams/{}".format(team["slug"])
5357
),
5458
json=team,
59+
is_optional=True,
5560
)
5661
# Catch-all for other teams
5762
httpx_mock.add_response(
5863
url=re.compile(r"^https://api.github.com/orgs/.*/teams/.*"),
5964
status_code=404,
6065
json={"message": "Not found"},
66+
is_optional=True,
6167
)
6268
# Team membership check
6369
for id, state in (("54321", "active"), ("59999", "pending")):
@@ -66,12 +72,14 @@ def mocked_github_api(httpx_mock):
6672
r"^https://api.github.com/teams/{}/memberships/.*".format(id)
6773
),
6874
json={"state": state, "role": "member"},
75+
is_optional=True,
6976
)
7077
# Catch-all for other membership checks
7178
httpx_mock.add_response(
7279
url=re.compile(r"^https://api.github.com/teams/\d+/memberships/.*"),
7380
status_code=404,
7481
json={"message": "Not found"},
82+
is_optional=True,
7583
)
7684
# User lookup
7785
httpx_mock.add_response(
@@ -82,10 +90,11 @@ def mocked_github_api(httpx_mock):
8290
"login": "demouser",
8391
"email": "demouser@example.com",
8492
},
93+
is_optional=True,
8594
)
8695

8796

88-
@pytest.fixture
97+
@pytest_asyncio.fixture
8998
async def ds(tmpdir):
9099
filepath = str(tmpdir / "test.db")
91100
filepath2 = str(tmpdir / "demouser_org_only.db")
@@ -130,7 +139,7 @@ async def test_ds_fixture(ds):
130139

131140
@pytest.mark.asyncio
132141
async def test_github_auth_start(ds):
133-
response = await ds.client.get("/-/github-auth-start", allow_redirects=False)
142+
response = await ds.client.get("/-/github-auth-start", follow_redirects=False)
134143
assert (
135144
"https://github.com/login/oauth/authorize?scope=read:org&client_id=x_client_id"
136145
== response.headers["location"]
@@ -141,7 +150,7 @@ async def test_github_auth_start(ds):
141150
async def test_github_auth_callback(ds, mocked_github_api):
142151
response = await ds.client.get(
143152
"/-/github-auth-callback?code=github-code-here",
144-
allow_redirects=False,
153+
follow_redirects=False,
145154
)
146155
actor = ds.unsign(response.cookies["ds_actor"], "actor")["a"]
147156
assert {
@@ -176,15 +185,15 @@ async def test_sign_in_with_github_button(ds):
176185
(True, {"test", "demouser_org_only"}),
177186
],
178187
)
179-
async def test_sign_in_with_github_button(
188+
async def test_database_access_permissions(
180189
ds, mocked_github_api, authenticated, expected_databases
181190
):
182191
cookies = {}
183192
if authenticated:
184193
auth_response = await ds.client.get(
185194
"/-/github-auth-callback?code=github-code-here",
186-
allow_redirects=False,
195+
follow_redirects=False,
187196
)
188197
cookies = {"ds_actor": auth_response.cookies["ds_actor"]}
189198
databases = await ds.client.get("/.json", cookies=cookies)
190-
assert set(databases.json().keys()) == expected_databases
199+
assert set(databases.json()["databases"].keys()) == expected_databases

0 commit comments

Comments
 (0)