Exemple #1
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
Exemple #2
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)
Exemple #3
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})
Exemple #4
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
Exemple #5
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