Skip to content

Commit

Permalink
Merge pull request #1635 from Scifabric/issue-1448
Browse files Browse the repository at this point in the history
Issue 1448
  • Loading branch information
teleyinex committed Aug 28, 2017
2 parents 57ce265 + b02c0d8 commit 5b30ecd
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pybossa/view/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,11 +1725,27 @@ def webhook_handler(short_name, oid=None):
else:
abort(404)


ensure_authorized_to('read', Webhook, project_id=project.id)
redirect_to_password = _check_if_redirect_to_password(project)
if redirect_to_password:
return redirect_to_password

if request.method == 'GET' and request.args.get('all'):
for wh in responses:
webhook_queue.enqueue(webhook, project.webhook,
wh.payload, wh.id)
flash('All webhooks enqueued')

if request.method == 'GET' and request.args.get('failed'):
for wh in responses:
if wh.response_status_code != 200:
webhook_queue.enqueue(webhook, project.webhook,
wh.payload, wh.id)
flash('All webhooks enqueued')

project = add_custom_contrib_button_to(project, get_user_id_or_ip(), ps=ps)

return render_template('projects/webhook.html', project=project,
owner=owner, responses=responses,
overall_progress=ps.overall_progress,
Expand Down
91 changes: 91 additions & 0 deletions test/test_view/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from pybossa.core import user_repo, webhook_repo
from pybossa.model import make_timestamp
from pybossa.model.webhook import Webhook
from pybossa.jobs import webhook
from mock import patch, call

class TestWebhookView(web.Helper):

Expand Down Expand Up @@ -57,6 +59,13 @@ def test_webhook_handler_auth(self):
url = "/project/%s/webhook" % project.short_name
res = self.app.get(url)
assert res.status_code == 403, res.status_code
url = "/project/%s/webhook?all=true" % project.short_name
res = self.app.get(url)
assert res.status_code == 403, res.status_code
url = "/project/%s/webhook?failed=true" % project.short_name
res = self.app.get(url)
assert res.status_code == 403, res.status_code


@with_context
def test_webhook_handler_owner_non_pro(self):
Expand All @@ -71,6 +80,12 @@ def test_webhook_handler_owner_non_pro(self):
url = "/project/%s/webhook" % project.short_name
res = self.app.get(url)
assert res.status_code == 403, res.status_code
url = "/project/%s/webhook?all=true" % project.short_name
res = self.app.get(url)
assert res.status_code == 403, res.status_code
url = "/project/%s/webhook?failed=true" % project.short_name
res = self.app.get(url)
assert res.status_code == 403, res.status_code

@with_context
def test_webhook_handler_owner_pro(self):
Expand All @@ -89,6 +104,16 @@ def test_webhook_handler_owner_pro(self):
assert res.status_code == 200, res.status_code
assert "Created" in res.data
assert "Payload" in res.data
url = "/project/%s/webhook?failed=true" % project.short_name
res = self.app.get(url)
assert res.status_code == 200, res.status_code
assert "Created" in res.data
assert "Payload" in res.data
url = "/project/%s/webhook?all=true" % project.short_name
res = self.app.get(url)
assert res.status_code == 200, res.status_code
assert "Created" in res.data
assert "Payload" in res.data

@with_context
def test_webhook_handler_admin(self):
Expand All @@ -108,6 +133,16 @@ def test_webhook_handler_admin(self):
assert res.status_code == 200, res.status_code
assert "Created" in res.data
assert "Payload" in res.data
url = "/project/%s/webhook?all=true" % project.short_name
res = self.app.get(url)
assert res.status_code == 200, res.status_code
assert "Created" in res.data
assert "Payload" in res.data
url = "/project/%s/webhook?failed=true" % project.short_name
res = self.app.get(url)
assert res.status_code == 200, res.status_code
assert "Created" in res.data
assert "Payload" in res.data

@with_context
def test_webhook_handler_post_oid(self):
Expand Down Expand Up @@ -145,3 +180,59 @@ def test_webhook_handler_post_oid_404(self):
url = "/project/%s/webhook/%s" % (project.short_name, 9999)
res = self.app.post(url)
assert res.status_code == 404, res.status_code

@with_context
@patch('pybossa.view.projects.webhook_queue.enqueue')
def test_webhook_handler_failed(self, q):
"""Test WEBHOOK requeing failed works."""
self.register()
user = user_repo.get(1)
project = ProjectFactory.create(owner=user, webhook='server')
task = TaskFactory.create(project=project, n_answers=1)
AnonymousTaskRunFactory.create(project=project, task=task)
payload = self.payload(project, task)
wh = Webhook(project_id=project.id, payload=payload,
response='error', response_status_code=500)
webhook_repo.save(wh)
wh2 = Webhook(project_id=project.id, payload=payload,
response='ok', response_status_code=200)
webhook_repo.save(wh2)
wh3 = Webhook(project_id=project.id, payload=payload,
response='ok', response_status_code=200)
webhook_repo.save(wh3)

wh = webhook_repo.get(1)
url = "/project/%s/webhook?failed=true" % (project.short_name)
res = self.app.get(url)
assert res.status_code == 200, res.status_code
q.assert_called_once_with(webhook, project.webhook,
wh.payload, wh.id)

@with_context
@patch('pybossa.view.projects.webhook_queue.enqueue')
def test_webhook_handler_all(self, q):
"""Test WEBHOOK requeing all works."""
self.register()
user = user_repo.get(1)
project = ProjectFactory.create(owner=user, webhook='server')
task = TaskFactory.create(project=project, n_answers=1)
AnonymousTaskRunFactory.create(project=project, task=task)
payload = self.payload(project, task)
wh1 = Webhook(project_id=project.id, payload=payload,
response='error', response_status_code=500)
webhook_repo.save(wh1)
wh2 = Webhook(project_id=project.id, payload=payload,
response='ok', response_status_code=200)
webhook_repo.save(wh2)
wh3 = Webhook(project_id=project.id, payload=payload,
response='ok', response_status_code=200)
webhook_repo.save(wh3)
whs = webhook_repo.filter_by(project_id=project.id)
url = "/project/%s/webhook?all=true" % (project.short_name)
res = self.app.get(url)
assert res.status_code == 200, res.status_code
calls = []
for w in whs:
calls.append(call(webhook, project.webhook,
w.payload, w.id))
q.assert_has_calls(calls)

0 comments on commit 5b30ecd

Please sign in to comment.