def test_webhook_handler_all(self, q): """Test WEBHOOK requeing all works.""" self.register() self.signin() 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)
def test_webhook_handler_failed(self, q): """Test WEBHOOK requeing failed works.""" self.register() self.signin() 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)
def create_webhooks(projectId): """Create webhooks for a given project ID.""" from pybossa.core import project_repo, task_repo, result_repo from pybossa.model.webhook import Webhook project = project_repo.get(projectId) tasks = task_repo.filter_tasks_by(state='completed', project_id=projectId) print "Analyzing %s tasks" % len(tasks) for task in tasks: sql = text( '''select * from webhook where project_id=:project_id and payload@>'{"task_id": :task_id}';''') results = db.engine.execute( sql, project_id=project.id, task_id=task.id) webhook = None for result in results: webhook = result if not webhook: result = result_repo.get_by(project_id=project.id, task_id=task.id) payload = dict( fired_at=None, project_short_name=project.short_name, project_id=project.id, task_id=task.id, result_id=result.id, event='task_completed') wh = Webhook(project_id=projectId, payload=payload) print(wh) db.session.add(wh) db.session.commit() print "Project %s completed!" % project.short_name
def test_save_fails_if_integrity_error(self): """Test save raises a DBIntegrityError if the instance to be saved lacks a required value""" wh = Webhook(project_id=None) assert_raises(DBIntegrityError, self.webhook_repo.save, wh)
def test_admin_user_cannot_crud_webhook(self): """Test admin users cannot crud webhooks""" webhook = Webhook() assert_raises(Forbidden, ensure_authorized_to, 'create', webhook) assert_raises(Forbidden, ensure_authorized_to, 'update', webhook) assert_raises(Forbidden, ensure_authorized_to, 'delete', webhook)
def test_anonymous_user_cannot_crud_webhook(self): """Test anonymous users cannot crud webhooks""" webhook = Webhook() assert_raises(Unauthorized, ensure_authorized_to, 'create', webhook) assert_raises(Unauthorized, ensure_authorized_to, 'update', webhook) assert_raises(Unauthorized, ensure_authorized_to, 'delete', webhook)
def test_delete_entries_from_project(self): """Test delete entries from project works.""" project = ProjectFactory.create() wh = Webhook(project_id=project.id) self.webhook_repo.save(wh) self.webhook_repo.delete_entries_from_project(project) res = self.webhook_repo.get_by(project_id=project.id) assert res is None
def webhook(url, payload=None, oid=None): """Post to a webhook.""" from flask import current_app try: import json from pybossa.core import sentinel, webhook_repo headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} if oid: webhook = webhook_repo.get(oid) else: webhook = Webhook(project_id=payload['project_id'], payload=payload) if url: response = requests.post(url, data=json.dumps(payload), headers=headers) webhook.response = response.text webhook.response_status_code = response.status_code else: raise requests.exceptions.ConnectionError('Not URL') if oid: webhook_repo.update(webhook) webhook = webhook_repo.get(oid) else: webhook_repo.save(webhook) except requests.exceptions.ConnectionError: webhook.response = 'Connection Error' webhook.response_status_code = None webhook_repo.save(webhook) if current_app.config.get('SSE'): publish_channel(sentinel, payload['project_short_name'], data=webhook.dictize(), type='webhook', private=True) return webhook
def webhook(url, payload=None, oid=None): """Post to a webhook.""" from flask import current_app try: import json from pybossa.core import sentinel, webhook_repo headers = {"Content-type": "application/json", "Accept": "text/plain"} if oid: webhook = webhook_repo.get(oid) else: webhook = Webhook(project_id=payload["project_id"], payload=payload) if url: response = requests.post(url, data=json.dumps(payload), headers=headers) webhook.response = response.text webhook.response_status_code = response.status_code else: raise requests.exceptions.ConnectionError("Not URL") if oid: webhook_repo.update(webhook) webhook = webhook_repo.get(oid) else: webhook_repo.save(webhook) except requests.exceptions.ConnectionError: webhook.response = "Connection Error" webhook.response_status_code = None webhook_repo.save(webhook) if current_app.config.get("SSE"): publish_channel(sentinel, payload["project_short_name"], data=webhook.dictize(), type="webhook", private=True) return webhook
def webhook(url, payload=None, oid=None, rerun=False): """Post to a webhook.""" from flask import current_app from readability.readability import Document try: import json from pybossa.core import sentinel, webhook_repo, project_repo project = project_repo.get(payload['project_id']) headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} if oid: webhook = webhook_repo.get(oid) else: webhook = Webhook(project_id=payload['project_id'], payload=payload) if url: params = dict() if rerun: params['rerun'] = True response = requests.post(url, params=params, data=json.dumps(payload), headers=headers) webhook.response = Document(response.text).summary() webhook.response_status_code = response.status_code else: raise requests.exceptions.ConnectionError('Not URL') if oid: webhook_repo.update(webhook) webhook = webhook_repo.get(oid) else: webhook_repo.save(webhook) except requests.exceptions.ConnectionError: webhook.response = 'Connection Error' webhook.response_status_code = None webhook_repo.save(webhook) finally: if project.published and webhook.response_status_code != 200 and current_app.config.get( 'ADMINS'): subject = "Broken: %s webhook failed" % project.name body = 'Sorry, but the webhook failed' mail_dict = dict(recipients=current_app.config.get('ADMINS'), subject=subject, body=body, html=webhook.response) send_mail(mail_dict) if current_app.config.get('SSE'): publish_channel(sentinel, payload['project_short_name'], data=webhook.dictize(), type='webhook', private=True) return webhook
def test_webhook_handler_post_oid_404(self): """Test WEBHOOK post oid 404 works.""" self.register() self.signin() user = user_repo.get(1) project = ProjectFactory.create(owner=user) task = TaskFactory.create(project=project, n_answers=1) AnonymousTaskRunFactory.create(project=project, task=task) payload = self.payload(project, task) webhook = Webhook(project_id=project.id, payload=payload, response='OK', response_status_code=200) webhook_repo.save(webhook) url = "/project/%s/webhook/%s" % (project.short_name, 9999) res = self.app.post(url) assert res.status_code == 404, res.status_code
def test_webhook_handler_post_oid(self): """Test WEBHOOK post oid works.""" self.register() self.signin() user = user_repo.get(1) project = ProjectFactory.create(owner=user) task = TaskFactory.create(project=project, n_answers=1) AnonymousTaskRunFactory.create(project=project, task=task) payload = self.payload(project, task) webhook = Webhook(project_id=project.id, payload=payload, response='OK', response_status_code=200) webhook_repo.save(webhook) webhook = webhook_repo.get(1) url = "/project/%s/webhook/%s" % (project.short_name, webhook.id) res = self.app.post(url) tmp = json.loads(res.data) assert res.status_code == 200, res.status_code assert tmp['payload']['project_short_name'] == project.short_name assert tmp['payload']['project_id'] == project.id assert tmp['payload']['task_id'] == task.id
def webhook(url, payload=None, oid=None, rerun=False): """Post to a webhook.""" from flask import current_app from readability.readability import Document try: import json from pybossa.core import sentinel, webhook_repo, project_repo project = project_repo.get(payload['project_id']) headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} if oid: webhook = webhook_repo.get(oid) else: webhook = Webhook(project_id=payload['project_id'], payload=payload) if url: params = dict() if rerun: params['rerun'] = True response = requests.post(url, params=params, data=json.dumps(payload), headers=headers) webhook.response = Document(response.text).summary() webhook.response_status_code = response.status_code else: raise requests.exceptions.ConnectionError('Not URL') if oid: webhook_repo.update(webhook) webhook = webhook_repo.get(oid) else: webhook_repo.save(webhook) except requests.exceptions.ConnectionError: webhook.response = 'Connection Error' webhook.response_status_code = None webhook_repo.save(webhook) finally: if project.published and webhook.response_status_code != 200 and current_app.config.get('ADMINS'): subject = "Broken: %s webhook failed" % project.name body = 'Sorry, but the webhook failed' mail_dict = dict(recipients=current_app.config.get('ADMINS'), subject=subject, body=body, html=webhook.response) send_mail(mail_dict) if current_app.config.get('SSE'): publish_channel(sentinel, payload['project_short_name'], data=webhook.dictize(), type='webhook', private=True) return webhook
def setUp(self): super(TestWebhookRepository, self).setUp() self.webhook_repo = WebhookRepository(db) TaskRunFactory.create() webhook = Webhook(project_id=1, payload=self.payload) self.webhook_repo.save(webhook)