Пример #1
0
    def test_get(self):
        try:
            find_uuid = self._put_subscription()

            # Normal request
            url = str(UrlBuilder()
                      .set(path="/v1/subscriptions/" + str(find_uuid))
                      .add_query("replica", self.replica.name)
                      .add_query("subscription_type", "elasticsearch"))
            resp_obj = self.assertGetResponse(
                url,
                requests.codes.okay,
                headers=get_auth_header())
            json_response = resp_obj.json
            self.assertEqual(self.sample_percolate_query, json_response['es_query'])
            self.assertEqual(self.endpoint, Endpoint.from_subscription(json_response))
            self.assertEquals(self.hmac_key_id, json_response['hmac_key_id'])
            self.assertNotIn('hmac_secret_key', json_response)
        finally:
            self._cleanup_subscription(find_uuid)

        # File not found request
        url = str(UrlBuilder()
                  .set(path="/v1/subscriptions/" + str(uuid.uuid4()))
                  .add_query("replica", self.replica.name)
                  .add_query("subscription_type", "elasticsearch"))
        self.assertGetResponse(
            url,
            requests.codes.not_found,
            headers=get_auth_header())
Пример #2
0
    def test_get(self):
        find_uuid = self._put_subscription()

        # Normal request
        url = str(UrlBuilder().set(path="/v1/subscriptions/" +
                                   str(find_uuid)).add_query(
                                       "replica", self.replica.name))
        resp_obj = self.assertGetResponse(url,
                                          requests.codes.okay,
                                          headers=get_auth_header())
        json_response = resp_obj.json
        self.assertEqual(self.sample_percolate_query,
                         json_response['es_query'])
        self.assertEqual(self.endpoint,
                         Endpoint.from_subscription(json_response))

        # Forbidden request w/ previous url
        with self.throw_403():
            self.assertGetResponse(url,
                                   requests.codes.forbidden,
                                   headers=get_auth_header())

        # File not found request
        url = str(UrlBuilder().set(path="/v1/subscriptions/" +
                                   str(uuid.uuid4())).add_query(
                                       "replica", self.replica.name))
        self.assertGetResponse(url,
                               requests.codes.not_found,
                               headers=get_auth_header())
Пример #3
0
    def _notify_subscriber(self, doc: BundleDocument, subscription: dict):
        transaction_id = str(uuid.uuid4())
        subscription_id = subscription['id']
        endpoint = Endpoint.from_subscription(subscription)

        payload = dict(transaction_id=transaction_id,
                       subscription_id=subscription_id,
                       es_query=subscription['es_query'],
                       match=dict(bundle_uuid=doc.fqid.uuid,
                                  bundle_version=doc.fqid.version))

        definitions = subscription.get('attachments')
        # Only mention attachments in the notification if the subscription does, too.
        if definitions is not None:
            payload['attachments'] = attachment.select(definitions, doc)

        if endpoint.encoding == 'application/json':
            body = payload
        elif endpoint.encoding == 'multipart/form-data':
            body = endpoint.form_fields.copy()
            body[endpoint.payload_form_field] = json.dumps(payload)
        else:
            raise ValueError(f"Encoding {endpoint.encoding} is not supported")

        try:
            hmac_key = subscription['hmac_secret_key']
        except KeyError:
            hmac_key = None
            hmac_key_id = None
        else:
            hmac_key = hmac_key.encode()
            hmac_key_id = subscription.get('hmac_key_id', "hca-dss:" + subscription_id)

        notification = Notification.create(notification_id=transaction_id,
                                           subscription_id=subscription_id,
                                           url=endpoint.callback_url,
                                           method=endpoint.method,
                                           encoding=endpoint.encoding,
                                           body=body,
                                           hmac_key=hmac_key,
                                           hmac_key_id=hmac_key_id,
                                           correlation_id=str(doc.fqid))
        if self.notifier:
            logger.info(f"Queueing asynchronous notification {notification} for bundle {doc.fqid}")
            self.notifier.enqueue(notification)
        else:
            logger.info(f"Synchronously sending notification {notification} about bundle {doc.fqid}")
            notification.deliver_or_raise()
Пример #4
0
 def test_find(self):
     num_additions = 25
     for _ in range(num_additions):
         self._put_subscription()
     url = str(UrlBuilder().set(path="/v1/subscriptions").add_query(
         "replica", self.replica.name))
     resp_obj = self.assertGetResponse(url,
                                       requests.codes.okay,
                                       headers=get_auth_header())
     json_response = resp_obj.json
     self.assertEqual(self.sample_percolate_query,
                      json_response['subscriptions'][0]['es_query'])
     self.assertEqual(
         self.endpoint,
         Endpoint.from_subscription(json_response['subscriptions'][0]))
     self.assertEqual(num_additions, len(json_response['subscriptions']))
Пример #5
0
 def test_find(self):
     try:
         num_additions = 3
         uuids = list()
         for _ in range(num_additions):
             uuids.append(self._put_subscription())
         url = str(UrlBuilder()
                   .set(path="/v1/subscriptions")
                   .add_query("replica", self.replica.name)
                   .add_query("subscription_type", "elasticsearch"))
         resp_obj = self.assertGetResponse(
             url,
             requests.codes.okay,
             headers=get_auth_header())
         json_response = resp_obj.json
         self.assertEqual(self.sample_percolate_query, json_response['subscriptions'][0]['es_query'])
         self.assertEqual(self.hmac_key_id, json_response['subscriptions'][0]['hmac_key_id'])
         self.assertEqual(self.endpoint, Endpoint.from_subscription(json_response['subscriptions'][0]))
         self.assertNotIn('hmac_secret_key', json_response['subscriptions'][0])
         self.assertEqual(num_additions, len(json_response['subscriptions']))
     finally:
         for _uuid in uuids:
             self._cleanup_subscription(_uuid)