Merge branch 'swaggerlikeus' of ssh://bitbucket.org/yackob03/quay into swaggerlikeus

Conflicts:
	endpoints/api/trigger.py
This commit is contained in:
jakedt 2014-03-19 12:13:07 -04:00
commit 7bd4b9a71c
3 changed files with 53 additions and 59 deletions

View file

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

View file

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

View file

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