Fix the remainder of the API usage tests. Note that this still fails when the blueprint is registered again, so each subset of tests has to be run on its own

This commit is contained in:
Joseph Schorr 2014-03-18 20:32:37 -04:00
parent 5e7ffd95ca
commit 807fa68fe4
3 changed files with 55 additions and 61 deletions

View file

@ -11,7 +11,7 @@ from endpoints.api import (RepositoryParamResource, nickname, resource, require_
validate_json_request, api, Unauthorized, NotFound, InvalidRequest)
from endpoints.api.build import build_status_view, trigger_view, RepositoryBuildStatus
from endpoints.common import start_build
from endpoints.trigger import (BuildTrigger, TriggerDeactivationException,
from endpoints.trigger import (BuildTrigger as BuildTriggerBase, TriggerDeactivationException,
TriggerActivationException, EmptyRepositoryException)
from data import model
from auth.permissions import UserAdminPermission
@ -63,7 +63,7 @@ class BuildTrigger(RepositoryParamResource):
except model.InvalidBuildTriggerException:
raise NotFound()
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
handler = BuildTriggerBase.get_trigger_for_service(trigger.service.name)
config_dict = json.loads(trigger.config)
if handler.is_active(config_dict):
try:
@ -102,7 +102,7 @@ class BuildTriggerSubdirs(RepositoryParamResource):
except model.InvalidBuildTriggerException:
raise NotFound()
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
handler = BuildTriggerBase.get_trigger_for_service(trigger.service.name)
user_permission = UserAdminPermission(trigger.connected_user.username)
if user_permission.can():
new_config_dict = request.get_json()
@ -144,12 +144,12 @@ class BuildTriggerActivate(RepositoryParamResource):
except model.InvalidBuildTriggerException:
raise NotFound()
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
handler = BuildTriggerBase.get_trigger_for_service(trigger.service.name)
existing_config_dict = json.loads(trigger.config)
if handler.is_active(existing_config_dict):
raise InvalidRequest('Trigger config is not sufficient for activation.')
user_permission = UserPermission(trigger.connected_user.username)
user_permission = UserAdminPermission(trigger.connected_user.username)
if user_permission.can():
new_config_dict = request.get_json()
@ -202,7 +202,7 @@ class ActivateBuildTrigger(RepositoryParamResource):
except model.InvalidBuildTriggerException:
raise NotFound()
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
handler = BuildTriggerBase.get_trigger_for_service(trigger.service.name)
existing_config_dict = json.loads(trigger.config)
if not handler.is_active(existing_config_dict):
raise InvalidRequest('Trigger is not active.')
@ -252,12 +252,12 @@ class BuildTriggerSources(RepositoryParamResource):
except model.InvalidBuildTriggerException:
raise NotFound()
user_permission = UserPermission(trigger.connected_user.username)
user_permission = UserAdminPermission(trigger.connected_user.username)
if user_permission.can():
trigger_handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
trigger_handler = BuildTriggerBase.get_trigger_for_service(trigger.service.name)
return {
'sources': trigger_handler.list_build_sources(trigger.auth_token)
}
else:
raise Unauthorized()
raise Unauthorized()

View file

@ -74,7 +74,7 @@ def common_login(db_user):
@app.errorhandler(model.DataModelException)
def handle_dme(ex):
logger.exception(ex)
return make_response(ex.message, 400)
return make_response(json.dumps({'message': ex.message}), 400)
def generate_csrf_token():

View file

@ -3,7 +3,7 @@ import json as py_json
from endpoints.api import api_bp, api
from endpoints.webhooks import webhooks
from endpoints.trigger import BuildTrigger
from endpoints.trigger import BuildTrigger as BuildTriggerBase
from app import app
from initdb import setup_database_for_testing, finished_database_for_testing
from data import model, database
@ -62,9 +62,9 @@ class ApiTestCase(unittest.TestCase):
finished_database_for_testing(self)
self.ctx.__exit__(True, None, None)
def getJsonResponse(self, resource_name, params={}):
def getJsonResponse(self, resource_name, params={}, expected_code=200):
rv = self.app.get(api.url_for(resource_name, **params))
self.assertEquals(200, rv.status_code)
self.assertEquals(expected_code, rv.status_code)
data = rv.data
parsed = py_json.loads(data)
return parsed
@ -121,11 +121,7 @@ class ApiTestCase(unittest.TestCase):
class TestDiscovery(ApiTestCase):
def test_discovery(self):
json = self.getJsonResponse(DiscoveryResource)
found = set([])
for method_info in json['endpoints']:
found.add(method_info['name'])
assert 'discovery' in found
assert 'apis' in json
class TestPlans(ApiTestCase):
@ -140,8 +136,7 @@ class TestPlans(ApiTestCase):
class TestLoggedInUser(ApiTestCase):
def test_guest(self):
json = self.getJsonResponse(User)
assert json['anonymous'] == True
self.getJsonResponse(User, expected_code=401)
def test_user(self):
self.login(READ_ACCESS_USER)
@ -169,7 +164,8 @@ class TestConvertToOrganization(ApiTestCase):
self.login(READ_ACCESS_USER)
json = self.postJsonResponse(ConvertToOrganization,
data={'adminUser': READ_ACCESS_USER,
'adminPassword': 'password'},
'adminPassword': 'password',
'plan': 'free'},
expected_code=400)
self.assertEqual('The admin user is not valid', json['message'])
@ -178,7 +174,8 @@ class TestConvertToOrganization(ApiTestCase):
self.login(READ_ACCESS_USER)
json = self.postJsonResponse(ConvertToOrganization,
data={'adminUser': 'unknownuser',
'adminPassword': 'password'},
'adminPassword': 'password',
'plan': 'free'},
expected_code=400)
self.assertEqual('The admin user credentials are not valid',
@ -188,7 +185,8 @@ class TestConvertToOrganization(ApiTestCase):
self.login(READ_ACCESS_USER)
json = self.postJsonResponse(ConvertToOrganization,
data={'adminUser': ADMIN_ACCESS_USER,
'adminPassword': 'invalidpass'},
'adminPassword': 'invalidpass',
'plan': 'free'},
expected_code=400)
self.assertEqual('The admin user credentials are not valid',
@ -249,7 +247,7 @@ class TestCreateNewUser(ApiTestCase):
data = self.postResponse(User,
data=NEW_USER_DETAILS,
expected_code=201)
self.assertEquals('Created', data)
self.assertEquals('"Created"', data)
class TestSignout(ApiTestCase):
@ -261,8 +259,8 @@ class TestSignout(ApiTestCase):
self.postResponse(Signout)
json = self.getJsonResponse(User)
assert json['anonymous'] == True
# Make sure we're now signed out.
self.getJsonResponse(User, expected_code=401)
class TestGetMatchingEntities(ApiTestCase):
@ -294,7 +292,7 @@ class TestCreateOrganization(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
json = self.postJsonResponse(OrganizationList,
data=dict(name=ADMIN_ACCESS_USER),
data=dict(name=ADMIN_ACCESS_USER, email='testorg@example.com'),
expected_code=400)
self.assertEquals('A user or organization with this name already exists',
@ -304,9 +302,9 @@ class TestCreateOrganization(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
json = self.postJsonResponse(OrganizationList,
data=dict(name=ORGANIZATION),
data=dict(name=ORGANIZATION, email='testorg@example.com'),
expected_code=400)
self.assertEquals('A user or organization with this name already exists',
json['message'])
@ -315,10 +313,10 @@ class TestCreateOrganization(ApiTestCase):
data = self.postResponse(OrganizationList,
data=dict(name='neworg',
email='test@example.com'),
email='testorg@example.com'),
expected_code=201)
self.assertEquals('Created', data)
self.assertEquals('"Created"', data)
# Ensure the org was created.
organization = model.get_organization('neworg')
@ -411,12 +409,11 @@ class TestCreateOrganizationPrototypes(ApiTestCase):
def test_missingdelegate(self):
self.login(ADMIN_ACCESS_USER)
json = self.postJsonResponse(PermissionPrototypeList,
params=dict(orgname=ORGANIZATION),
data=dict(role='read'),
expected_code=400)
self.postJsonResponse(PermissionPrototypeList,
params=dict(orgname=ORGANIZATION),
data=dict(role='read'),
expected_code=400)
self.assertEquals('Missing delegate user or team', json['message'])
def test_createprototype(self):
self.login(ADMIN_ACCESS_USER)
@ -533,11 +530,11 @@ class TestUpdateOrganizationTeam(ApiTestCase):
def test_updateexisting(self):
self.login(ADMIN_ACCESS_USER)
data = self.postJsonResponse(OrganizationTeam,
params=dict(orgname=ORGANIZATION,
teamname='readers'),
data=dict(description='My cool team',
role='creator'))
data = self.putJsonResponse(OrganizationTeam,
params=dict(orgname=ORGANIZATION,
teamname='readers'),
data=dict(description='My cool team',
role='creator'))
self.assertEquals('My cool team', data['description'])
self.assertEquals('creator', data['role'])
@ -545,10 +542,10 @@ class TestUpdateOrganizationTeam(ApiTestCase):
def test_attemptchangeroleonowners(self):
self.login(ADMIN_ACCESS_USER)
self.postResponse(OrganizationTeam,
params=dict(orgname=ORGANIZATION, teamname='owners'),
data=dict(role = 'creator'),
expected_code=400)
self.putJsonResponse(OrganizationTeam,
params=dict(orgname=ORGANIZATION, teamname='owners'),
data=dict(role = 'creator'),
expected_code=400)
def test_createnewteam(self):
self.login(ADMIN_ACCESS_USER)
@ -557,8 +554,7 @@ class TestUpdateOrganizationTeam(ApiTestCase):
params=dict(orgname=ORGANIZATION,
teamname='newteam'),
data=dict(description='My cool team',
role='member'),
expected_code=201)
role='member'))
self.assertEquals('My cool team', data['description'])
self.assertEquals('member', data['role'])
@ -611,9 +607,9 @@ class TestUpdateOrganizationTeamMember(ApiTestCase):
def test_addmember(self):
self.login(ADMIN_ACCESS_USER)
self.postJsonResponse(TeamMember,
params=dict(orgname=ORGANIZATION, teamname='readers',
membername=NO_ACCESS_USER))
self.putJsonResponse(TeamMember,
params=dict(orgname=ORGANIZATION, teamname='readers',
membername=NO_ACCESS_USER))
# Verify the user was added to the team.
@ -647,7 +643,8 @@ class TestCreateRepo(ApiTestCase):
json = self.postJsonResponse(RepositoryList,
data=dict(repository='simple',
visibility='public'),
visibility='public',
description=''),
expected_code=400)
self.assertEquals('Repository already exists', json['message'])
@ -659,7 +656,8 @@ class TestCreateRepo(ApiTestCase):
json = self.postJsonResponse(RepositoryList,
data=dict(repository='newrepo',
visibility='public',
description=''))
description=''),
expected_code=201)
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
@ -673,7 +671,8 @@ class TestCreateRepo(ApiTestCase):
data=dict(namespace=ORGANIZATION,
repository='newrepo',
visibility='private',
description=''))
description=''),
expected_code=201)
self.assertEquals(ORGANIZATION, json['namespace'])
self.assertEquals('newrepo', json['name'])
@ -884,12 +883,6 @@ class TestRepoBuilds(ApiTestCase):
self.assertEquals(status_json, build)
# Check the archive URL.
archive_json = self.getJsonResponse('api.get_repo_build_archive_url',
params=dict(repository=ADMIN_ACCESS_USER + '/building', build_uuid=build['id']))
assert archive_json['url']
# Check the logs.
logs_json = self.getJsonResponse(RepositoryBuildLogs,
params=dict(repository=ADMIN_ACCESS_USER + '/building', build_uuid=build['id']))
@ -926,7 +919,8 @@ class TestWebhooks(ApiTestCase):
# Add a webhook.
json = self.postJsonResponse(WebhookList,
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
data=dict(url='http://example.com'))
data=dict(url='http://example.com'),
expected_code=201)
self.assertEquals('http://example.com', json['parameters']['url'])
wid = json['public_id']
@ -1371,7 +1365,7 @@ class TestLogs(ApiTestCase):
self.assertEquals(READ_ACCESS_USER, log['performer']['name'])
class FakeBuildTrigger(BuildTrigger):
class FakeBuildTrigger(BuildTriggerBase):
@classmethod
def service_name(cls):
return 'fakeservice'