def workflow_receiver_video_failing(api_app, db, video, receiver_id): """Workflow receiver for video.""" video_depid = video['_deposit']['id'] class TestReceiver(CeleryAsyncReceiver): def run(self, event): workflow = chain( sse_simple_add().s(x=1, y=2, deposit_id=video_depid), group(sse_failing_task().s(), sse_success_task().s())) event.payload['deposit_id'] = video_depid with db.session.begin_nested(): event.response_headers = {} flag_modified(event, 'response_headers') flag_modified(event, 'payload') db.session.expunge(event) db.session.commit() result = workflow.apply_async() self._serialize_result(event=event, result=result) self.persist(event=event, result=result) def _raw_info(self, event): result = self._deserialize_result(event) return ([{ 'add': result.parent }], [{ 'failing': result.children[0] }, { 'failing': result.children[1] }]) current_webhooks.register(receiver_id, TestReceiver) return receiver_id
def workflow_receiver(api_app, db, webhooks, es, cds_depid): """Workflow receiver.""" class TestReceiver(CeleryAsyncReceiver): def run(self, event): workflow = chain( sse_simple_add().s(x=1, y=2, deposit_id=cds_depid), group(sse_failing_task().s(), sse_success_task().s()) ) event.payload['deposit_id'] = cds_depid with db.session.begin_nested(): flag_modified(event, 'payload') db.session.expunge(event) db.session.commit() result = workflow.apply_async() self._serialize_result(event=event, result=result) self.persist(event=event, result=result) def _raw_info(self, event): result = self._deserialize_result(event) return ( [{'add': result.parent}], [ {'failing': result.children[0]}, {'failing': result.children[1]} ] ) receiver_id = 'add-receiver' from cds.celery import celery celery.flask_app.extensions['invenio-webhooks'].register( receiver_id, TestReceiver) current_webhooks.register(receiver_id, TestReceiver) return receiver_id
def receiver(api_app): """Register test celery receiver.""" class TestReceiver(CeleryReceiver): def run(self, event): ret = add.apply(kwargs=event.payload).get() event.response['message'] = ret current_webhooks.register('test-receiver', TestReceiver) return 'test-receiver'
def test_async_receiver_status_fail(api_app, access_token, u_email, json_headers, receiver_id, workflow, status, http_status, payload, result): """Test AVC workflow test-case.""" ctx = dict() class TestReceiver(CeleryAsyncReceiver): def run(self, event): assert payload == event.payload ctx['myresult'] = workflow.s(**event.payload).apply_async() self._serialize_result(event, ctx['myresult']) def _status_and_info(self, event): result = self._deserialize_result(event) status = result.status info = _info_extractor(result, receiver_id) return dict(status=status, info=info) current_webhooks.register(receiver_id, TestReceiver) with api_app.test_request_context(): url = url_for('invenio_webhooks.event_list', receiver_id=receiver_id, access_token=access_token) with api_app.test_client() as client: # run the task resp = client.post(url, headers=json_headers, data=json.dumps(payload)) assert resp.status_code == http_status data = json.loads(resp.headers['X-Hub-Info']) assert data['name'] == receiver_id extra_info = json.loads(resp.headers['X-Hub-Info']) assert extra_info['id'] == ctx['myresult'].id assert ctx['myresult'].result == result with api_app.test_request_context(): event_id = resp.headers['X-Hub-Delivery'] url = url_for('invenio_webhooks.event_item', receiver_id=receiver_id, event_id=event_id, access_token=access_token) with api_app.test_client() as client: # check status resp = client.get(url, headers=json_headers) assert resp.status_code == http_status data = json.loads(resp.headers['X-Hub-Info']) # assert data['status'] == status assert data['name'] == receiver_id extra_info = json.loads(resp.headers['X-Hub-Info']) assert extra_info['id'] == ctx['myresult'].id
def test_deposit_events_on_worlflow(api_app, db, depid, bucket, access_token, json_headers): """Test deposit events.""" class TestReceiver(CeleryAsyncReceiver): def run(self, event): workflow = chain(simple_add.s(1, 2), group(failing_task.s(), success_task.s())) self._serialize_result(event, workflow.apply_async()) event.payload['deposit_id'] = depid flag_modified(event, 'payload') def _status_and_info(self, event, fun=_info_extractor): result = self._deserialize_result(event) status = _compute_status([ result.parent.status, result.children[0].status, result.children[1].status ]) info = fun(result.parent, 'add', [ fun(result.children[0], 'failing'), fun(result.children[1], 'failing') ]) return dict(status=status, info=info) receiver_id = 'add-receiver' current_webhooks.register(receiver_id, TestReceiver) db.session.add(bucket) with api_app.test_request_context(): url = url_for('invenio_webhooks.event_list', receiver_id=receiver_id, access_token=access_token) with api_app.test_client() as client: # run workflow resp = client.post(url, headers=json_headers, data=json.dumps({})) assert resp.status_code == 500 # run again workflow resp = client.post(url, headers=json_headers, data=json.dumps({})) assert resp.status_code == 500 # resolve deposit and events # TODO use a deposit resolver video = video_resolver([depid])[0] events = video._events # check events assert len(events) == 2 assert events[0].payload['deposit_id'] == depid assert events[1].payload['deposit_id'] == depid # check computed status status = video._compute_tasks_status() assert status['add'] == states.PENDING assert status['failing'] == states.FAILURE # check every task for every event for event in events: result = event.receiver._deserialize_result(event) assert result.parent.status == states.PENDING assert result.children[0].status == states.FAILURE assert result.children[1].status == states.SUCCESS # check if the states are inside the deposit res = client.get(url_for('invenio_deposit_rest.depid_item', pid_value=depid, access_token=access_token), headers=json_headers) assert res.status_code == 200 data = json.loads(res.data.decode('utf-8'))['metadata'] assert data['_deposit']['state']['add'] == states.PENDING assert data['_deposit']['state']['failing'] == states.FAILURE