Skip to content

Commit

Permalink
Add tests for allowed team membership
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nnyr0berts committed Aug 5, 2021
1 parent cb8a884 commit 2567271
Showing 1 changed file with 63 additions and 4 deletions.
67 changes: 63 additions & 4 deletions oauthenticator/tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from urllib.parse import urlparse

from pytest import fixture
from pytest import mark
from tornado.httpclient import HTTPResponse
from tornado.httputil import HTTPHeaders
from traitlets.config import Config
Expand Down Expand Up @@ -76,6 +77,8 @@ async def test_allowed_org_membership(github_client):
'blue': ['tucker', 'caboose', 'burns', 'sheila', 'texas'],
}

org_teams = {'blue': {'alpha': ['tucker', 'caboose', 'burns']}}

member_regex = re.compile(r'/orgs/(.*)/members')

def org_members(paginate, request):
Expand Down Expand Up @@ -112,27 +115,51 @@ def org_members_paginated(org, page, urlinfo, response):
buffer=BytesIO(json.dumps(ret).encode('utf-8')),
)

membership_regex = re.compile(r'/orgs/(.*)/members/(.*)')
org_membership_regex = re.compile(r'/orgs/(.*)/members/(.*)')

def org_membership(request):
urlinfo = urlparse(request.url)
urlmatch = membership_regex.match(urlinfo.path)
urlmatch = org_membership_regex.match(urlinfo.path)
org = urlmatch.group(1)
username = urlmatch.group(2)
print('Request org = %s, username = %s' % (org, username))
if org not in orgs:
print('Team not found: org = %s' % (org))
print('Org not found: org = %s' % (org))
return HTTPResponse(request, 404)
if username not in orgs[org]:
print('Member not found: org = %s, username = %s' % (org, username))
return HTTPResponse(request, 404)
return HTTPResponse(request, 204)

team_membership_regex = re.compile(r'/orgs/(.*)/teams/(.*)/members/(.*)')

def team_membership(request):
urlinfo = urlparse(request.url)
urlmatch = team_membership_regex.match(urlinfo.path)
org = urlmatch.group(1)
team = urlmatch.group(2)
username = urlmatch.group(3)
print('Request org = %s, team = %s username = %s' % (org, team, username))
if org not in orgs:
print('Org not found: org = %s' % (org))
return HTTPResponse(request, 404)
if team not in org_teams[org]:
print('Team not found in org: team = %s, org = %s' % (team, org))
return HTTPResponse(request, 404)
if username not in org_teams[org][team]:
print(
'Member not found: org = %s, team = %s, username = %s'
% (org, team, username)
)
return HTTPResponse(request, 404)
return HTTPResponse(request, 204)

## Perform tests

for paginate in (False, True):
client_hosts = client.hosts['api.github.com']
client_hosts.append((membership_regex, org_membership))
client_hosts.append((team_membership_regex, team_membership))
client_hosts.append((org_membership_regex, org_membership))
client_hosts.append((member_regex, functools.partial(org_members, paginate)))

authenticator.allowed_organizations = ['blue']
Expand All @@ -156,10 +183,42 @@ def org_membership(request):
user = await authenticator.authenticate(handler)
assert user['name'] == 'donut'

# test team membership
authenticator.allowed_organizations = ['blue:alpha', 'red']

handler = client.handler_for_user(user_model('tucker'))
user = await authenticator.authenticate(handler)
assert user['name'] == 'tucker'

handler = client.handler_for_user(user_model('grif'))
user = await authenticator.authenticate(handler)
assert user['name'] == 'grif'

handler = client.handler_for_user(user_model('texas'))
user = await authenticator.authenticate(handler)
assert user is None

client_hosts.pop()
client_hosts.pop()


@mark.parametrize(
"org, username, expected",
[
("blue", "texas", "https://api.github.com/orgs/blue/members/texas"),
(
"blue:alpha",
"tucker",
"https://api.github.com/orgs/blue/teams/alpha/members/tucker",
),
("red", "grif", "https://api.github.com/orgs/red/members/grif"),
],
)
def test_build_check_membership_url(org, username, expected):
output = GitHubOAuthenticator()._build_check_membership_url(org, username)
assert output == expected


def test_deprecated_config(caplog):
cfg = Config()
cfg.GitHubOAuthenticator.github_organization_whitelist = ["jupy"]
Expand Down

0 comments on commit 2567271

Please sign in to comment.