Add tests for repository notification api

This commit is contained in:
Evan Cordell 2017-07-17 17:56:32 -04:00
parent 9dad44e93d
commit 57517adef3
7 changed files with 239 additions and 32 deletions

View file

@ -6,12 +6,12 @@ from flask import request
from endpoints.api import (RepositoryParamResource, nickname, resource, require_repo_admin,
log_action, validate_json_request, request_error,
path_param, disallow_for_app_repositories)
from endpoints.exception import NotFound
from endpoints.exception import NotFound, InvalidRequest
from endpoints.notificationmethod import (NotificationMethod,
CannotValidateNotificationMethodException)
from endpoints.notificationhelper import build_notification_data
from workers.notificationworker.models_pre_oci import notification
from endpoints.api.tag_models_pre_oci import pre_oci_model as model
from endpoints.api.repositorynotification_models_pre_oci import pre_oci_model as model
logger = logging.getLogger(__name__)
@ -68,7 +68,6 @@ class RepositoryNotificationList(RepositoryParamResource):
except CannotValidateNotificationMethodException as ex:
raise request_error(message=ex.message)
new_notification = model.create_repo_notification(namespace_name, repository_name,
parsed['event'],
parsed['method'],
@ -79,7 +78,7 @@ class RepositoryNotificationList(RepositoryParamResource):
log_action('add_repo_notification', namespace_name,
{'repo': repository_name, 'namespace': namespace_name,
'notification_id': new_notification.uuid,
'event': parsed['event'], 'method': parsed['method']},
'event': new_notification.event_name, 'method': new_notification.method_name},
repo_name=repository_name)
return new_notification.to_dict(), 201
@ -104,11 +103,9 @@ class RepositoryNotification(RepositoryParamResource):
@disallow_for_app_repositories
def get(self, namespace_name, repository_name, uuid):
""" Get information for the specified notification. """
try:
found = model.get_repo_notification(uuid)
except model.InvalidNotificationException:
found = model.get_repo_notification(uuid)
if not found:
raise NotFound()
return found.to_dict()
@require_repo_admin
@ -117,9 +114,12 @@ class RepositoryNotification(RepositoryParamResource):
def delete(self, namespace_name, repository_name, uuid):
""" Deletes the specified notification. """
deleted = model.delete_repo_notification(namespace_name, repository_name, uuid)
if not deleted:
raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid))
log_action('delete_repo_notification', namespace_name,
{'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid,
'event': deleted.event.name, 'method': deleted.method.name},
'event': deleted.event_name, 'method': deleted.method_name},
repo_name=repository_name)
return 'No Content', 204
@ -130,11 +130,13 @@ class RepositoryNotification(RepositoryParamResource):
def post(self, namespace_name, repository_name, uuid):
""" Resets repository notification to 0 failures. """
reset = model.reset_notification_number_of_failures(namespace_name, repository_name, uuid)
if reset is not None:
log_action('reset_repo_notification', namespace_name,
{'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid,
'event': reset.event.name, 'method': reset.method.name},
repo_name=repository_name)
if not reset:
raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid))
log_action('reset_repo_notification', namespace_name,
{'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid,
'event': reset.event_name, 'method': reset.method_name},
repo_name=repository_name)
return 'No Content', 204
@ -147,10 +149,11 @@ class TestRepositoryNotification(RepositoryParamResource):
@require_repo_admin
@nickname('testRepoNotification')
@disallow_for_app_repositories
def post(self, namespace, repository, uuid):
def post(self, namespace_name, repository_name, uuid):
""" Queues a test notification for this repository. """
test_note = model.get_repo_notification(uuid)
test_note = model.queue_test_notification(uuid)
if not test_note:
raise NotFound()
model.queue_test_notification(test_note)
return {}
raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid))
return {}, 200