Esempio n. 1
0
def test_delete_webhook(db):
    instance = Webhook.create('http://www.foo.bar')
    assert instance.id > 0
    instance.delete()
    assert Webhook.get(instance.id) is None
    assert instance.id not in Webhook.get_all_ids()
    assert instance.id not in Webhook.get_ids_by_type(instance.hook_type)
Esempio n. 2
0
def test_get_webhook(client, test_application_token, test_application):
    webhook_normal = Webhook.create('http://www.foo.bar')
    headers = {'Authorization': test_application_token}

    query_string = {'application_name': test_application.application_name}
    r = client.get('/api/webhook/%s' % webhook_normal.id, headers=headers,
                   query_string=query_string)
    assert_response_ok(r)
    assert r.json['data'] == {
        'webhook_id': webhook_normal.id,
        'webhook_url': webhook_normal.url,
        'webhook_type': webhook_normal.hook_type,
        'event_list': []
    }

    webhook_universal = Webhook.create('http://www.foo.bar', 1)
    headers = {'Authorization': test_application_token}
    r = client.get('/api/webhook/%s' % webhook_universal.id, headers=headers,
                   query_string=query_string)
    assert_response_ok(r)
    assert r.json['data'] == {
        'webhook_id': webhook_universal.id,
        'webhook_url': webhook_universal.url,
        'webhook_type': webhook_universal.hook_type,
        'event_list': []
    }
Esempio n. 3
0
def test_update_webhook_url(db):
    instance = Webhook.create('http://www.foo.bar')
    assert instance.id > 0
    assert instance.url == 'http://www.foo.bar'
    assert instance.hook_type == Webhook.TYPE_NORMAL

    instance.update_url('http://www.foo.me')
    assert instance.url == 'http://www.foo.me'
    assert Webhook.get(instance.id).url == instance.url
Esempio n. 4
0
def test_create_webhook(db):
    instance = Webhook.create('http://www.foo.bar')
    assert instance.id > 0
    assert instance.url == 'http://www.foo.bar'
    assert instance.hook_type == Webhook.TYPE_NORMAL
    assert instance.is_normal

    instance = Webhook.create('http://foo.foo.bar', Webhook.TYPE_UNIVERSAL)
    assert instance.id > 0
    assert instance.url == 'http://foo.foo.bar'
    assert instance.hook_type == Webhook.TYPE_UNIVERSAL

    assert instance.id in Webhook.get_all_ids()
    assert instance.id in Webhook.get_ids_by_type(instance.hook_type)
Esempio n. 5
0
def test_webhook(db):
    hook = Webhook(
        url='http://www.foo.me',
        hook_type=Webhook.TYPE_NORMAL)
    db.add(hook)
    db.commit()
    return hook
Esempio n. 6
0
    def get(self):
        """List all webhooks registered in Huskar.

        The response looks like::

            {
              "status": "SUCCESS",
              "message": "",
              "data": {
                "webhook_list": [
                    {
                        "webhook_id": 1,
                        "webhook_url": "http://www.example.com",
                        "webhook_type": 0
                    }
                ]
              }
            }

        :status 200: The request is successful.
        :status 404: The application not found.
        """
        webhooks = Webhook.get_all()
        webhook_list = [{
            'webhook_id': webhook.id,
            'webhook_url': webhook.url,
            'webhook_type': webhook.hook_type
        } for webhook in webhooks]
        return api_response(data={'webhook_list': webhook_list})
Esempio n. 7
0
def test_update_webhook_subscriptions(
        db, client, test_application, test_application_token,
        admin_token, present_data, input_data):
    webhooks = add_test_webhook_sub(present_data, test_application.id)
    webhook = list(webhooks)[0][0]
    if webhook.is_normal:
        token = test_application_token
        event_num = len(input_data['event_list'])
    else:
        token = admin_token
        event_num = 0
    headers = {
        'Authorization': token,
        'Content-Type': 'application/json'
    }
    query_string = {'application_name': test_application.application_name}

    r = client.put('/api/webhook/%s' % webhook.id, data=json.dumps(input_data),
                   headers=headers, query_string=query_string)
    assert_response_ok(r)
    assert r.json['data'] is None

    webhook_subs = Webhook.search_subscriptions(
        application_id=test_application.id)
    assert len(webhook_subs) == event_num
Esempio n. 8
0
    def get(self, application_name):
        """List the subscriptions of an application specified with
        the ``application_name``.

        The response looks like::

            {
              "status": "SUCCESS",
              "message": "",
              "data": {
                "webhook_list": [
                  {
                    "webhook_id": 1,
                    "webhook_url": "http://www.example.com",
                    "webhook_type": 0,
                    "event_list": [
                        "CREATE_CONFIG_CLUSTER",
                        "DELETE_CONFIG_CLUSTER",
                        ...
                    ]
                  },
                  ...
                ]
              }
            }

        :param application_name: The name of application.
        :status 200: The request is successful.
        """
        application = check_application(application_name)
        subscriptions = Webhook.search_subscriptions(
            application_id=application.id)
        groups = itertools.groupby(subscriptions, key=attrgetter('webhook_id'))
        webhook_list = []
        for webhook_id, group in groups:
            webhook = Webhook.get(webhook_id)
            webhook_list.append({
                'webhook_id':
                webhook.id,
                'webhook_url':
                webhook.url,
                'webhook_type':
                webhook.hook_type,
                'event_list': [action_types[x.action_type] for x in group]
            })
        return api_response(data={'webhook_list': webhook_list})
Esempio n. 9
0
    def post(self):
        """Create a new webhook.

        The request accepting a JSON body, the schema likes::

            {
                "webhook_url": "http://www.example.com",
                "event_list": [
                    "CREATE_CONFIG_CLUSTER",
                    "DELETE_CONFIG_CLUSTER"
                ]
            }

        The content of ``event_list`` should be a list of action that
        already defined in Huskar.

        The ``application_name`` is only required when the ``webhook_type``
        is 0, it means the webhook want to subscribe some events of specified
        application. If the ``webhook_type`` value specified with 1,
        a universal webhook will be registered which will receive all
        the events of Huskar site, so the ``event_list`` will be ignored
        because that is unnecessary.

        :param webhook_type: default 0, set ``site`` level with 1.
        :param application_name: The name of application, optional.
        :form webhook_url: the webhook url.
        :form event_list: event list want subscribed
        :status 404: The application not found.
        :status 200: successful request.
        """
        webhook_type = request.args.get('webhook_type', default=0, type=int)
        self._check_authority(webhook_type)
        data = request.get_json() or {}
        validate_fields(webhook_schema, data, partial=False)

        if webhook_type == Webhook.TYPE_UNIVERSAL:
            webhook = Webhook.create(data['webhook_url'], webhook_type)
            return api_response()
        application_name = request.args['application_name']
        application = Application.get_by_name(application_name)
        webhook = Webhook.create(data['webhook_url'], webhook_type)
        for action_name in data.get('event_list', []):
            action_type = getattr(action_types, action_name)
            webhook.subscribe(application.id, action_type)
        return api_response()
Esempio n. 10
0
def test_search_subscriptions(db, test_webhook, test_application):
    action_type_list = [
        action_types.CREATE_CONFIG_CLUSTER,
        action_types.DELETE_CONFIG_CLUSTER
    ]
    for action_type in action_type_list:
        test_webhook.subscribe(test_application.id, action_type)
    subs = Webhook.search_subscriptions(application_id=test_application.id)
    assert len(subs) == len(action_type_list)
Esempio n. 11
0
def test_delete_webhook(
        db, client, test_application, test_application_token,
        admin_token, present_data, input_data):
    webhooks = add_test_webhook_sub(present_data, test_application.id)
    webhook = list(webhooks)[0][0]
    if webhook.is_normal:
        token = test_application_token
    else:
        token = admin_token
    headers = {'Authorization': token}
    query_string = {'application_name': test_application.application_name}
    r = client.delete('/api/webhook/%s' % webhook.id, headers=headers,
                      query_string=query_string)
    assert_response_ok(r)
    assert r.json['data'] is None

    assert Webhook.get(webhook.id) is None
    assert len(Webhook.search_subscriptions(webhook_id=webhook.id)) == 0
Esempio n. 12
0
def add_test_webhook_sub(prepare_data, application_id):
    for data in prepare_data:
        webhook = Webhook.create(
            data['webhook_url'], data.get('webhook_type', 0))
        if webhook.is_normal:
            for action_name in data['event_list']:
                action_type = getattr(action_types, action_name)
                webhook.subscribe(application_id, action_type)
        yield webhook, data
Esempio n. 13
0
def test_delete_webhook_fialed(
        client, test_application, test_application_token):
    headers = {'Authorization': test_application_token}
    r = client.get('/api/webhook/100', headers=headers)
    assert r.status_code == 404
    assert r.json['status'] == 'NotFound'
    assert r.json['message'] == 'Webhook not registered.'

    webhook = Webhook.create('http://www.foo.bar')
    r = client.delete('/api/webhook/%s' % webhook.id, headers=headers,
                      query_string={'application_name': 'foo'})
    assert r.status_code == 400
Esempio n. 14
0
def test_get_webhook_list(client, test_application_token):
    webhook_list = [
        {
            'webhook_url': 'http://www.foo.bar',
            'webhook_type': 0,
        },
        {
            'webhook_url': 'http://www.foo.me',
            'webhook_type': 1,
        },
        {
            'webhook_url': 'http://www.bar.me',
            'webhook_type': 0
        }
    ]
    for data in webhook_list:
        webhook = Webhook.create(data['webhook_url'], data['webhook_type'])
        data.update(webhook_id=webhook.id)

    headers = {'Authorization': test_application_token}
    r = client.get('/api/webhook', headers=headers)
    assert_response_ok(r)
    assert r.json['data']['webhook_list'] == webhook_list
Esempio n. 15
0
def test_update_webhook_then_get(
        client, test_application_token, test_application):
    webhook_normal = Webhook.create('http://www.foo.bar')
    headers = {
        'Authorization': test_application_token,
        'Content-Type': 'application/json',
    }

    query_string = {'application_name': test_application.application_name}
    r = client.get('/api/webhook/%s' % webhook_normal.id, headers=headers,
                   query_string=query_string)
    assert_response_ok(r)
    assert r.json['data'] == {
        'webhook_id': webhook_normal.id,
        'webhook_url': webhook_normal.url,
        'webhook_type': webhook_normal.hook_type,
        'event_list': []
    }

    payload = {
        'event_list': ['UPDATE_CONFIG', 'DELETE_CONFIG'],
        'webhook_url': 'http://abc.example.com',
        'webhook_type': 0,
    }
    r = client.put('/api/webhook/%s' % webhook_normal.id, headers=headers,
                   query_string=query_string, data=json.dumps(payload))
    assert_response_ok(r)

    r = client.get('/api/webhook/%s' % webhook_normal.id, headers=headers,
                   query_string=query_string)
    assert_response_ok(r)
    assert r.json['data'] == {
        'webhook_id': webhook_normal.id,
        'webhook_url': payload['webhook_url'],
        'webhook_type': webhook_normal.hook_type,
        'event_list': payload['event_list'],
    }
Esempio n. 16
0
def test_add_webhook_subscriptions(
        db, client, test_application, present_data, input_data, admin_token):
    add_test_webhook_sub(present_data, test_application.id)
    headers = {
        'Authorization': admin_token,
        'Content-Type': 'application/json'
    }
    query_string = {
        'webhook_type': input_data['webhook_type'],
        'application_name': test_application.application_name
    }
    r = client.post('/api/webhook', data=json.dumps(input_data),
                    headers=headers, query_string=query_string)
    assert_response_ok(r)
    assert r.json['data'] is None

    if int(input_data['webhook_type']) == 1:
        event_num = 0
    else:
        event_num = len(input_data['event_list'])

    webhook_sub_set = Webhook.search_subscriptions(
        application_id=test_application.id)
    assert len(webhook_sub_set) == event_num
Esempio n. 17
0
def test_universal_webhook_subscribe(db, test_application):
    webhook = Webhook.create('http://foo.foo.bar', Webhook.TYPE_UNIVERSAL)
    action_type = action_types.CREATE_CONFIG_CLUSTER
    sub = webhook.subscribe(test_application.id, action_type)
    assert sub is None
    assert webhook.get_subscription(test_application.id, action_type) is None
Esempio n. 18
0
def test_get_all_universal(db):
    Webhook.create('http://uni.foo.bar', hook_type=Webhook.TYPE_UNIVERSAL)
    Webhook.create('http://normal.foo.bar')
    hooks = Webhook.get_all_universal()
    assert [x.url for x in hooks] == ['http://uni.foo.bar']
Esempio n. 19
0
 def _get_webhook_or_404(self, webhook_id):
     instance = Webhook.get(webhook_id)
     if not instance:
         abort(404, 'Webhook not registered.')
     return instance
Esempio n. 20
0
def add_webhook_subscriptions(test_application):
    webhook = Webhook.create('http://foo.foo.bar')
    for action_name in action_types._action_map:
        action_type = getattr(action_types, action_name)
        webhook.subscribe(test_application.id, action_type)
Esempio n. 21
0
def universal_webhook():
    return Webhook.create('http://universal.foo.bar', Webhook.TYPE_UNIVERSAL)