-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5444 from hypothesis/get-group-api
Add `GET /groups/{id}` endpoint
- Loading branch information
Showing
9 changed files
with
210 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
import pytest | ||
import base64 | ||
|
||
|
||
from h.models.auth_client import GrantType | ||
|
||
native_str = str | ||
|
||
|
||
@pytest.mark.functional | ||
class TestReadGroup(object): | ||
|
||
def test_it_returns_http_200_for_world_readable_group_pubid(self, app, factories, db_session): | ||
group = factories.OpenGroup() | ||
db_session.commit() | ||
|
||
res = app.get('/api/groups/{pubid}'.format(pubid=group.pubid)) | ||
|
||
assert res.status_code == 200 | ||
|
||
data = res.json | ||
assert 'id' in data | ||
|
||
def test_it_returns_http_200_for_world_readable_groupid(self, app, factories, db_session): | ||
factories.OpenGroup(authority_provided_id='foo', authority='bar.com') | ||
db_session.commit() | ||
res = app.get('/api/groups/group:foo@bar.com') | ||
|
||
assert res.status_code == 200 | ||
|
||
def test_it_returns_http_404_for_private_group_no_authentication(self, app, factories, db_session): | ||
group = factories.Group() | ||
db_session.commit() | ||
|
||
res = app.get('/api/groups/{pubid}'.format(pubid=group.pubid), expect_errors=True) | ||
|
||
assert res.status_code == 404 | ||
|
||
def test_it_returns_http_200_for_private_group_with_creator_authentication(self, app, user_with_token, token_auth_header, factories, db_session): | ||
user, _ = user_with_token | ||
group = factories.Group(creator=user) | ||
db_session.commit() | ||
|
||
res = app.get('/api/groups/{pubid}'.format(pubid=group.pubid), headers=token_auth_header) | ||
|
||
assert res.status_code == 200 | ||
|
||
def test_it_returns_http_200_for_private_group_with_member_authentication(self, app, user_with_token, token_auth_header, factories, db_session): | ||
user, _ = user_with_token | ||
group = factories.Group() | ||
group.members.append(user) | ||
db_session.commit() | ||
|
||
res = app.get('/api/groups/{pubid}'.format(pubid=group.pubid), headers=token_auth_header) | ||
|
||
assert res.status_code == 200 | ||
|
||
def test_it_returns_http_404_for_private_group_if_token_user_not_creator(self, app, token_auth_header, factories, db_session): | ||
group = factories.Group() | ||
db_session.commit() | ||
|
||
res = app.get('/api/groups/{pubid}'.format(pubid=group.pubid), headers=token_auth_header, expect_errors=True) | ||
|
||
assert res.status_code == 404 | ||
|
||
def test_it_returns_http_200_for_private_group_with_auth_client_matching_authority(self, app, auth_client_header, factories, db_session): | ||
group = factories.Group(authority='thirdparty.com') | ||
db_session.commit() | ||
|
||
res = app.get('/api/groups/{pubid}'.format(pubid=group.pubid), headers=auth_client_header) | ||
|
||
assert res.status_code == 200 | ||
|
||
def test_it_returns_http_404_for_private_group_with_auth_client_mismatched_authority(self, app, auth_client_header, factories, db_session): | ||
group = factories.Group(authority='somewhere-else.com') | ||
db_session.commit() | ||
|
||
res = app.get('/api/groups/{pubid}'.format(pubid=group.pubid), headers=auth_client_header, expect_errors=True) | ||
|
||
assert res.status_code == 404 | ||
|
||
|
||
@pytest.fixture | ||
def auth_client(db_session, factories): | ||
auth_client = factories.ConfidentialAuthClient(authority='thirdparty.com', | ||
grant_type=GrantType.client_credentials) | ||
db_session.commit() | ||
return auth_client | ||
|
||
|
||
@pytest.fixture | ||
def auth_client_header(auth_client): | ||
user_pass = "{client_id}:{secret}".format(client_id=auth_client.id, secret=auth_client.secret) | ||
encoded = base64.standard_b64encode(user_pass.encode('utf-8')) | ||
return {native_str('Authorization'): native_str("Basic {creds}".format(creds=encoded.decode('ascii')))} | ||
|
||
|
||
@pytest.fixture | ||
def user_with_token(db_session, factories): | ||
user = factories.User() | ||
token = factories.DeveloperToken(userid=user.userid) | ||
db_session.add(token) | ||
db_session.commit() | ||
return (user, token) | ||
|
||
|
||
@pytest.fixture | ||
def token_auth_header(user_with_token): | ||
user, token = user_with_token | ||
return {native_str('Authorization'): native_str('Bearer {}'.format(token.value))} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters