def test_trigger_fails_webhook_with_url(self, mock_post, mock_send_mail): """Test WEBHOOK fails and sends email is triggered.""" response = MagicMock() response.text = "<html>Something broken</html>" response.status_code = 500 mock_post.return_value = response project = ProjectFactory.create(published=True) payload = dict( event='task_completed', project_short_name=project.short_name, project_id=project.id, task_id=1, result_id=1, fired_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) wbh = WebhookFactory.create() tmp = webhook('url', payload=payload, oid=wbh.id) headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} mock_post.assert_called_with('url', data=json.dumps(payload), headers=headers) subject = "Broken: %s webhook failed" % project.name body = 'Sorry, but the webhook failed' mail_dict = dict(recipients=self.flask_app.config.get('ADMINS'), subject=subject, body=body, html=tmp.response) mock_send_mail.assert_called_with(mail_dict)
def test_webhooks_without_url(self, mock): """Test WEBHOOK without url works.""" mock.post.return_value = True err_msg = "The webhook should return Connection Error" res = webhook(None, self.webhook_payload, None) assert res.response == 'Connection Error', err_msg assert res.response_status_code is None, err_msg
def test_webhooks(self, mock): """Test WEBHOOK works.""" mock.post.return_value = True err_msg = "The webhook should return True from patched method" assert webhook('url'), err_msg err_msg = "The post method should be called" assert mock.post.called, err_msg
def test_webhooks(self, mock): """Test WEBHOOK works.""" mock.return_value = FakeResponse(text=json.dumps(dict(foo='bar')), status_code=200) err_msg = "The webhook should return True from patched method" assert webhook('url', self.webhook_payload), err_msg err_msg = "The post method should be called" assert mock.called, err_msg
def test_webhooks_rerun(self, mock): """Test WEBHOOK rerun works.""" mock.return_value = FakeResponse(text=json.dumps(dict(foo='bar')), status_code=200) assert webhook('url', self.webhook_payload, rerun=True), err_msg err_msg = "The post method should be called" assert mock.called, err_msg headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} mock.assert_called_with('url', params=dict(rerun=True), data=json.dumps(self.webhook_payload), headers=headers)
def test_webhooks_connection_error(self, mock): """Test WEBHOOK with connection error works.""" import requests from pybossa.core import webhook_repo mock.side_effect = requests.exceptions.ConnectionError err_msg = "A webhook should be returned" res = webhook('url', self.webhook_payload) assert res.response == 'Connection Error', err_msg assert res.response_status_code == None, err_msg wh = webhook_repo.get(1) assert wh.response == res.response, err_msg assert wh.response_status_code == res.response_status_code, err_msg
def test_trigger_fails_webhook_with_url_connection_error(self, mock_post, mock_send_mail): """Test WEBHOOK fails and sends email is triggered when there is a connection error.""" project = ProjectFactory.create(published=True) payload = dict(event='task_completed', project_short_name=project.short_name, project_id=project.id, task_id=1, result_id=1, fired_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) wbh = WebhookFactory.create() tmp = webhook('url', payload=payload, oid=wbh.id) headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} mock_post.assert_called_with('url', data=json.dumps(payload), headers=headers) subject = "Broken: %s webhook failed" % project.name body = 'Sorry, but the webhook failed' mail_dict = dict(recipients=self.flask_app.config.get('ADMINS'), subject=subject, body=body, html=tmp.response) mock_send_mail.assert_called_with(mail_dict)
def test_trigger_fails_webhook_with_no_url(self, mock_post, mock_send_mail): """Test WEBHOOK fails and sends email is triggered when no URL or failed connection.""" mock_post.side_effect = requests.exceptions.ConnectionError('Not URL') project = ProjectFactory.create(published=True) payload = dict( event='task_completed', project_short_name=project.short_name, project_id=project.id, task_id=1, result_id=1, fired_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) wbh = WebhookFactory.create() tmp = webhook(None, payload=payload, oid=wbh.id) headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} #mock_post.assert_called_with('url', data=json.dumps(payload), headers=headers) subject = "Broken: %s webhook failed" % project.name body = 'Sorry, but the webhook failed' mail_dict = dict(recipients=self.flask_app.config.get('ADMINS'), subject=subject, body=body, html=tmp.response) mock_send_mail.assert_called_with(mail_dict)
def test_webhooks_without_url(self, mock): """Test WEBHOOK without url works.""" mock.post.return_value = True err_msg = "The webhook should return False" assert webhook(None) is False, err_msg