From b683088f87a4a43327a04ca27694ad8b609ce48b Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 22 Feb 2017 15:23:52 -0500 Subject: [PATCH] Update tests for teams API --- endpoints/api/test/shared.py | 2 + endpoints/api/test/test_team.py | 88 ++++++++++++++++++++++++++------- initdb.py | 12 +++++ test/test_api_usage.py | 2 +- 4 files changed, 85 insertions(+), 19 deletions(-) diff --git a/endpoints/api/test/shared.py b/endpoints/api/test/shared.py index 140f6b37e..3d1f0cffa 100644 --- a/endpoints/api/test/shared.py +++ b/endpoints/api/test/shared.py @@ -8,6 +8,8 @@ from endpoints.api import api CSRF_TOKEN_KEY = '_csrf_token' CSRF_TOKEN = '123csrfforme' + +@contextmanager def client_with_identity(auth_username, client): with client.session_transaction() as sess: if auth_username and auth_username is not None: diff --git a/endpoints/api/test/test_team.py b/endpoints/api/test/test_team.py index 1f04ed108..36a4d95a1 100644 --- a/endpoints/api/test/test_team.py +++ b/endpoints/api/test/test_team.py @@ -5,31 +5,83 @@ from mock import patch from data import model from endpoints.api import api from endpoints.api.test.shared import client_with_identity, conduct_api_call -from endpoints.api.team import OrganizationTeamSyncing +from endpoints.api.team import OrganizationTeamSyncing, TeamMemberList +from endpoints.api.organization import Organization +from endpoints.test.fixtures import app, appconfig, database_uri, init_db_path, sqlitedb_file from test.test_ldap import mock_ldap -TEAM_PARAMS = {'orgname': 'buynlarge', 'teamname': 'owners'} +SYNCED_TEAM_PARAMS = {'orgname': 'sellnsmall', 'teamname': 'synced'} +UNSYNCED_TEAM_PARAMS = {'orgname': 'sellnsmall', 'teamname': 'owners'} def test_team_syncing(client): with mock_ldap() as ldap: with patch('endpoints.api.team.authentication', ldap): - cl = client_with_identity('devtable', client) - config = { - 'group_dn': 'cn=AwesomeFolk', - } + with client_with_identity('devtable', client) as cl: + config = { + 'group_dn': 'cn=AwesomeFolk', + } - conduct_api_call(cl, OrganizationTeamSyncing, 'POST', TEAM_PARAMS, config) + conduct_api_call(cl, OrganizationTeamSyncing, 'POST', UNSYNCED_TEAM_PARAMS, config) - # Ensure the team is now synced. - sync_info = model.team.get_team_sync_information(TEAM_PARAMS['orgname'], - TEAM_PARAMS['teamname']) - assert sync_info is not None - assert json.loads(sync_info.config) == config + # Ensure the team is now synced. + sync_info = model.team.get_team_sync_information(UNSYNCED_TEAM_PARAMS['orgname'], + UNSYNCED_TEAM_PARAMS['teamname']) + assert sync_info is not None + assert json.loads(sync_info.config) == config - # Remove the syncing. - conduct_api_call(cl, OrganizationTeamSyncing, 'DELETE', TEAM_PARAMS, None) + # Remove the syncing. + conduct_api_call(cl, OrganizationTeamSyncing, 'DELETE', UNSYNCED_TEAM_PARAMS, None) - # Ensure the team is no longer synced. - sync_info = model.team.get_team_sync_information(TEAM_PARAMS['orgname'], - TEAM_PARAMS['teamname']) - assert sync_info is None + # Ensure the team is no longer synced. + sync_info = model.team.get_team_sync_information(UNSYNCED_TEAM_PARAMS['orgname'], + UNSYNCED_TEAM_PARAMS['teamname']) + assert sync_info is None + + +def test_team_member_sync_info(client): + with mock_ldap() as ldap: + with patch('endpoints.api.team.authentication', ldap): + # Check for an unsynced team, with superuser. + with client_with_identity('devtable', client) as cl: + resp = conduct_api_call(cl, TeamMemberList, 'GET', UNSYNCED_TEAM_PARAMS) + assert 'can_sync' in resp.json + assert resp.json['can_sync']['service'] == 'ldap' + + assert 'synced' not in resp.json + + # Check for an unsynced team, with non-superuser. + with client_with_identity('randomuser', client) as cl: + resp = conduct_api_call(cl, TeamMemberList, 'GET', UNSYNCED_TEAM_PARAMS) + assert 'can_sync' not in resp.json + assert 'synced' not in resp.json + + # Check for a synced team, with superuser. + with client_with_identity('devtable', client) as cl: + resp = conduct_api_call(cl, TeamMemberList, 'GET', SYNCED_TEAM_PARAMS) + assert 'can_sync' in resp.json + assert resp.json['can_sync']['service'] == 'ldap' + + assert 'synced' in resp.json + assert 'last_updated' in resp.json['synced'] + assert 'group_dn' in resp.json['synced']['config'] + + # Check for a synced team, with non-superuser. + with client_with_identity('randomuser', client) as cl: + resp = conduct_api_call(cl, TeamMemberList, 'GET', SYNCED_TEAM_PARAMS) + assert 'can_sync' not in resp.json + + assert 'synced' in resp.json + assert 'last_updated' not in resp.json['synced'] + assert 'config' not in resp.json['synced'] + + +def test_organization_teams_sync_bool(client): + with mock_ldap() as ldap: + with patch('endpoints.api.organization.authentication', ldap): + # Ensure synced teams are marked as such in the organization teams list. + with client_with_identity('devtable', client) as cl: + resp = conduct_api_call(cl, Organization, 'GET', {'orgname': 'sellnsmall'}) + + assert not resp.json['teams']['owners']['is_synced'] + + assert resp.json['teams']['synced']['is_synced'] diff --git a/initdb.py b/initdb.py index f8ce513ab..d13b0a058 100644 --- a/initdb.py +++ b/initdb.py @@ -657,6 +657,9 @@ def populate_database(minimal=False, with_storage=False): liborg = model.organization.create_organization('library', 'quay+library@devtable.com', new_user_1) liborg.save() + thirdorg = model.organization.create_organization('sellnsmall', 'quay+sell@devtable.com', new_user_1) + thirdorg.save() + model.user.create_robot('coolrobot', org) oauth_app_1 = model.oauth.create_application(org, 'Some Test App', 'http://localhost:8000', @@ -700,9 +703,18 @@ def populate_database(minimal=False, with_storage=False): model.team.add_user_to_team(creatorbot, creators) model.team.add_user_to_team(creatoruser, creators) + sell_owners = model.team.get_organization_team('sellnsmall', 'owners') + sell_owners.description = 'Owners have unfettered access across the entire org.' + sell_owners.save() + + model.team.add_user_to_team(new_user_4, sell_owners) + synced_team = model.team.create_team('synced', org, 'member', 'Some synced team.') model.team.set_team_syncing(synced_team, 'ldap', {'group_dn': 'cn=Test-Group,ou=Users'}) + another_synced_team = model.team.create_team('synced', thirdorg, 'member', 'Some synced team.') + model.team.set_team_syncing(another_synced_team, 'ldap', {'group_dn': 'cn=Test-Group,ou=Users'}) + __generate_repository(with_storage, new_user_1, 'superwide', None, False, [], [(10, [], 'latest2'), (2, [], 'latest3'), diff --git a/test/test_api_usage.py b/test/test_api_usage.py index 52dabbaaa..8066d83e3 100644 --- a/test/test_api_usage.py +++ b/test/test_api_usage.py @@ -1007,7 +1007,7 @@ class TestConductSearch(ApiTestCase): json = self.getJsonResponse(ConductSearch, params=dict(query='owners')) - self.assertEquals(2, len(json['results'])) + self.assertEquals(3, len(json['results'])) self.assertEquals(json['results'][0]['kind'], 'team') self.assertEquals(json['results'][0]['name'], 'owners')