Exemple #1
0
    def test_edit_validation(self):
        invalid = M.Webhook(type='invalid type',
                            app_config_id=None,
                            hook_url='http://httpbin.org/post',
                            secret='secret')
        session(invalid).flush(invalid)
        self.app.get(self.url + '/repo-push/%s' % invalid._id, status=404)

        data = {'url': u'http://httpbin.org/post', 'secret': u'secret'}
        self.create_webhook(data).follow()
        wh = M.Webhook.query.get(hook_url=data['url'], type='repo-push')

        # invalid id in hidden field, just in case
        r = self.app.get(self.url + '/repo-push/%s' % wh._id)
        data = {k: v[0].value for (k, v) in r.forms[0].fields.items()}
        data['webhook'] = unicode(invalid._id)
        self.app.post(self.url + '/repo-push/edit', data, status=404)

        # empty values
        data = {'url': '', 'secret': '', 'webhook': str(wh._id)}
        r = self.app.post(self.url + '/repo-push/edit', data)
        self.find_error(r, 'url', 'Please enter a value', 'edit')

        data = {'url': 'qwe', 'secret': 'qwe', 'webhook': str(wh._id)}
        r = self.app.post(self.url + '/repo-push/edit', data)
        self.find_error(r, 'url',
                        'You must provide a full domain name (like qwe.com)',
                        'edit')
Exemple #2
0
    def test_webhook_validator(self):
        sender = Mock(type='repo-push')
        app = self.git
        invalid_app = self.project.app_instance('src2')
        v = WebhookValidator(sender=sender, app=app, not_empty=True)
        with assert_raises(Invalid) as cm:
            v.to_python(None)
        assert_equal(cm.exception.msg, u'Please enter a value')
        with assert_raises(Invalid) as cm:
            v.to_python('invalid id')
        assert_equal(cm.exception.msg, u'Invalid webhook')

        wh = M.Webhook(type='invalid type',
                       app_config_id=invalid_app.config._id,
                       hook_url='http://hooks.slack.com',
                       secret='secret')
        session(wh).flush(wh)
        # invalid type
        with assert_raises(Invalid) as cm:
            v.to_python(wh._id)
        assert_equal(cm.exception.msg, u'Invalid webhook')

        wh.type = 'repo-push'
        session(wh).flush(wh)
        # invalild app
        with assert_raises(Invalid) as cm:
            v.to_python(wh._id)
        assert_equal(cm.exception.msg, u'Invalid webhook')

        wh.app_config_id = app.config._id
        session(wh).flush(wh)
        assert_equal(v.to_python(wh._id), wh)
        assert_equal(v.to_python(unicode(wh._id)), wh)
Exemple #3
0
 def add_webhooks(suffix, n):
     for i in range(n):
         webhook = M.Webhook(type='repo-push',
                             app_config_id=self.git.config._id,
                             hook_url='http://httpbin.org/{}/{}'.format(
                                 suffix, i),
                             secret='secret')
         session(webhook).flush(webhook)
Exemple #4
0
 def setUp(self):
     setup_basic_test()
     self.patches = self.monkey_patch()
     for p in self.patches:
         p.start()
     self.setup_with_tools()
     self.project = M.Project.query.get(shortname=test_project_with_repo)
     self.git = self.project.app_instance('src')
     self.wh = M.Webhook(type='repo-push',
                         app_config_id=self.git.config._id,
                         hook_url='http://httpbin.org/post',
                         secret='secret')
     session(self.wh).flush(self.wh)
Exemple #5
0
 def create(self, url, secret):
     if self.sender.enforce_limit(self.app):
         webhook = M.Webhook(type=self.sender.type,
                             app_config_id=self.app.config._id)
         self.update_webhook(webhook, url, secret)
         M.AuditLog.log('add webhook %s %s %s', webhook.type,
                        webhook.hook_url, webhook.app_config.url())
         flash('Created successfully', 'ok')
     else:
         flash(
             'You have exceeded the maximum number of webhooks '
             'you are allowed to create for this project/app', 'error')
     redirect(self.app.admin_url + 'webhooks')
Exemple #6
0
    def test_delete_validation(self):
        invalid = M.Webhook(type='invalid type',
                            app_config_id=None,
                            hook_url='http://httpbin.org/post',
                            secret='secret')
        session(invalid).flush(invalid)
        assert_equal(M.Webhook.query.find().count(), 1)

        data = {'webhook': ''}
        self.app.post(self.url + '/repo-push/delete', data, status=404)

        data = {'webhook': unicode(invalid._id)}
        self.app.post(self.url + '/repo-push/delete', data, status=404)
        assert_equal(M.Webhook.query.find().count(), 1)
Exemple #7
0
 def index(self, **kw):
     response.content_type = str('application/json')
     try:
         params = {
             'secret': kw.pop('secret', ''),
             'url': kw.pop('url', None)
         }
         valid = self.create_form().to_python(params)
     except Exception as e:
         response.status_int = 400
         return {'result': 'error', 'error': self._error(e)}
     if self.sender.enforce_limit(self.app):
         webhook = M.Webhook(type=self.sender.type,
                             app_config_id=self.app.config._id)
         try:
             self.update_webhook(webhook, valid['url'], valid['secret'])
         except Invalid as e:
             response.status_int = 400
             return {'result': 'error', 'error': self._error(e)}
         M.AuditLog.log('add webhook %s %s %s', webhook.type,
                        webhook.hook_url, webhook.app_config.url())
         response.status_int = 201
         # refetch updated values (e.g. mod_date)
         session(webhook).expunge(webhook)
         webhook = M.Webhook.query.get(_id=webhook._id)
         return webhook.__json__()
     else:
         limits = {
             'max':
             M.Webhook.max_hooks(self.sender.type,
                                 self.app.config.tool_name),
             'used':
             M.Webhook.query.find({
                 'type': self.sender.type,
                 'app_config_id': self.app.config._id,
             }).count(),
         }
         resp = {
             'result': 'error',
             'error': 'You have exceeded the maximum number of webhooks '
             'you are allowed to create for this project/app',
             'limits': limits,
         }
         response.status_int = 400
         return resp
Exemple #8
0
 def setUp(self):
     super(TestWebhookRestController, self).setUp()
     self.patches = self.monkey_patch()
     for p in self.patches:
         p.start()
     self.setup_with_tools()
     self.project = M.Project.query.get(shortname=test_project_with_repo)
     self.git = self.project.app_instance('src')
     self.url = str('/rest' + self.git.admin_url + 'webhooks')
     self.webhooks = []
     for i in range(3):
         webhook = M.Webhook(
             type='repo-push',
             app_config_id=self.git.config._id,
             hook_url='http://httpbin.org/post/{}'.format(i),
             secret='secret-{}'.format(i))
         session(webhook).flush(webhook)
         self.webhooks.append(webhook)
Exemple #9
0
    def create(self, url, secret):
        if self.sender.enforce_limit(self.app):
            webhook = M.Webhook(type=self.sender.type,
                                app_config_id=self.app.config._id)
            try:
                self.update_webhook(webhook, url, secret)
            except Invalid as e:
                # trigger error_handler directly
                c.form_errors['_the_form'] = e
                return self.index(url=url, secret=secret)

            M.AuditLog.log('add webhook %s %s %s', webhook.type,
                           webhook.hook_url, webhook.app_config.url())
            flash('Created successfully', 'ok')
        else:
            flash(
                'You have exceeded the maximum number of webhooks '
                'you are allowed to create for this project/app', 'error')
        redirect(self.app.admin_url + 'webhooks')