Пример #1
0
    def test_anonymous_user_cannot_read_webhook(self):
        """Test anonymous users cannot read a webhook"""

        project = ProjectFactory.create()
        webhook = WebhookFactory.create(project_id=project.id)

        assert_raises(Unauthorized, ensure_authorized_to, 'read', webhook)
Пример #2
0
 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)
Пример #3
0
    def test_admin_user_can_read_webhook(self):
        """Test admin users can read a webhook"""

        owner = UserFactory.create_batch(2)[1]
        project = ProjectFactory.create(owner=owner)
        webhook = WebhookFactory.create(project_id=project.id)

        assert self.mock_admin.id != project.owner_id
        assert_not_raises(Exception, ensure_authorized_to, 'read', webhook)
Пример #4
0
 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)
Пример #5
0
    def test_trigger_webhook_with_url(self):
        """Test WEBHOOK is triggered with url."""
        url = 'http://server.com'
        owner = UserFactory.create(pro=True)
        project = ProjectFactory.create(webhook=url, owner=owner)
        task = TaskFactory.create(project=project, n_answers=1)
        TaskRunFactory.create(project=project, task=task)
        result = result_repo.get_by(project_id=project.id, task_id=task.id)
        payload = dict(
            event='task_completed',
            project_short_name=project.short_name,
            project_id=project.id,
            task_id=task.id,
            result_id=result.id,
            fired_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
        assert queue.enqueue.called
        assert queue.called_with(webhook, url, payload)

        u = '/project/%s/webhook?api_key=%s&all=1' % (project.short_name,
                                                      project.owner.api_key)
        res = self.app.get(u)
        assert queue.enqueue.called
        assert queue.called_with(webhook, url, payload, True)

        wh = WebhookFactory(response_status_code=500,
                            project_id=project.id,
                            payload=payload,
                            response="500")
        u = '/project/%s/webhook?api_key=%s&failed=1' % (project.short_name,
                                                         project.owner.api_key)
        res = self.app.get(u)
        assert queue.enqueue.called
        assert queue.called_with(webhook, url, payload, True)

        u = '/project/%s/webhook/%s?api_key=%s&failed=1' % (
            wh.id, project.short_name, project.owner.api_key)
        res = self.app.post(u)
        assert queue.enqueue.called
        assert queue.called_with(webhook, url, payload, True)

        queue.reset_mock()
Пример #6
0
 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)