def test_video_delete_with_workflow(api_app, users, api_project, webhooks, es): """Test publish a project with a workflow.""" project, video_1, video_2 = api_project video_1_depid = video_1['_deposit']['id'] receiver_id = 'test_video_delete_with_workflow' workflow_receiver_video_failing(api_app, db, video_1, receiver_id=receiver_id) mock_delete = MagicMock(return_value=None) current_webhooks.receivers[receiver_id].delete = mock_delete headers = [('Content-Type', 'application/json')] payload = json.dumps(dict(somekey='somevalue')) with api_app.test_request_context(headers=headers, data=payload): event = Event.create(receiver_id=receiver_id) db.session.add(event) event.process() db.session.commit() video_1 = deposit_video_resolver(video_1_depid) video_1.delete() assert mock_delete.called is True
def test_project_publish_with_workflow(api_app, users, api_project, es): """Test publish a project with a workflow.""" project, video_1, video_2 = api_project prepare_videos_for_publish([video_1, video_2]) project_depid = project['_deposit']['id'] project_id = str(project.id) video_1_depid = video_1['_deposit']['id'] video_1_id = str(video_1.id) video_2_depid = video_2['_deposit']['id'] receiver_id = 'test_project_publish_with_workflow' workflow_receiver_video_failing(api_app, db, video_1, receiver_id=receiver_id) headers = [('Content-Type', 'application/json')] payload = json.dumps(dict(somekey='somevalue')) with mock.patch('invenio_indexer.tasks.index_record.delay') \ as mock_indexer, \ api_app.test_request_context(headers=headers, data=payload): event = Event.create(receiver_id=receiver_id) db.session.add(event) event.process() # check video and project are indexed assert mock_indexer.called is True ids = get_indexed_records_from_mock(mock_indexer) assert video_1_id == ids[0] assert project_id == ids[1] db.session.commit() # check tasks status is propagated to video and project video_1 = deposit_video_resolver(video_1_depid) expected = {u'add': u'SUCCESS', u'failing': u'FAILURE'} assert video_1['_cds']['state'] == expected assert video_1.project['_cds']['state'] == expected events = get_deposit_events(deposit_id=video_1_depid) assert len(events) == 1 def check(project_status, video_1_status, video_2_status): project = deposit_project_resolver(project_depid) video_1 = deposit_video_resolver(video_1_depid) video_2 = deposit_video_resolver(video_2_depid) assert project.status == project_status assert video_1.status == video_1_status assert video_2.status == video_2_status check('draft', 'draft', 'draft') login_user(User.query.get(users[0])) video_2 = deposit_video_resolver(video_2_depid) video_2.publish() check('draft', 'draft', 'published') project = deposit_project_resolver(project_depid) project.publish() check('published', 'published', 'published')
def test_webhooks_failing_feedback(api_app, db, cds_depid, access_token, json_headers, api_project): """Test webhooks feedback with a failing task.""" (project, video_1, video_2) = api_project sse_channel = 'mychannel' receiver_id = 'test_feedback_with_workflow' workflow_receiver_video_failing(api_app, db, video_1, receiver_id=receiver_id, sse_channel=sse_channel) 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 # data = json.loads(resp.data.decode('utf-8')) # check feedback url event_id = resp.headers['X-Hub-Delivery'] # event_id = data['tags']['_event_id'] with api_app.test_request_context(): url = url_for('invenio_webhooks.event_feedback_item', event_id=event_id, access_token=access_token, receiver_id=receiver_id) resp = client.get(url, headers=json_headers) assert resp.status_code == 200 data = json.loads(resp.data.decode('utf-8')) assert 'id' in data[0][0] assert data[0][0]['name'] == 'add' assert data[0][0]['status'] == states.SUCCESS assert 'id' in data[1][0] assert data[1][0]['name'] == 'failing' assert data[1][0]['status'] == states.FAILURE
def test_video_events_on_workflow(webhooks, api_app, db, api_project, bucket, access_token, json_headers): """Test deposit events.""" (project, video_1, video_2) = api_project project_depid = project['_deposit']['id'] video_1_depid = video_1['_deposit']['id'] db.session.add(bucket) # registering receiver receiver_id = 'test_video_events_on_workflow' workflow_receiver_video_failing(api_app, db, video_1, receiver_id=receiver_id) 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 deposit = deposit_video_resolver(video_1_depid) events = get_deposit_events(deposit['_deposit']['id']) # check events assert len(events) == 2 assert events[0].payload['deposit_id'] == video_1_depid assert events[1].payload['deposit_id'] == video_1_depid # check computed status status = get_tasks_status_by_task(events) assert status['add'] == states.SUCCESS 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.SUCCESS 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.video_item', pid_value=video_1_depid, access_token=access_token), headers=json_headers) assert res.status_code == 200 data = json.loads(res.data.decode('utf-8'))['metadata'] assert data['_cds']['state']['add'] == states.SUCCESS assert data['_cds']['state']['failing'] == states.FAILURE # run indexer RecordIndexer().process_bulk_queue() sleep(2) # check elasticsearch video_1 state resp = client.get(url_for('invenio_deposit_rest.video_list', q='_deposit.id:{0}'.format(video_1_depid), access_token=access_token), headers=json_headers) assert resp.status_code == 200 data = json.loads(resp.data.decode('utf-8')) status = data['hits']['hits'][0]['metadata']['_cds']['state'] assert status['add'] == states.SUCCESS assert status['failing'] == states.FAILURE # check elasticsearch project state resp = client.get(url_for('invenio_deposit_rest.video_list', q='_deposit.id:{0}'.format(project_depid), access_token=access_token), headers=json_headers) assert resp.status_code == 200 data = json.loads(resp.data.decode('utf-8')) status = data['hits']['hits'][0]['metadata']['_cds']['state'] assert status['add'] == states.SUCCESS assert status['failing'] == states.FAILURE
def test_project_publish_with_workflow(api_app, users, api_project, es): """Test publish a project with a workflow.""" project, video_1, video_2 = api_project prepare_videos_for_publish([video_1, video_2]) project_depid = project['_deposit']['id'] project_id = str(project.id) video_1_depid = video_1['_deposit']['id'] video_1_id = str(video_1.id) video_2_depid = video_2['_deposit']['id'] sse_channel = 'mychannel' receiver_id = 'test_project_publish_with_workflow' workflow_receiver_video_failing(api_app, db, video_1, receiver_id=receiver_id, sse_channel=sse_channel) headers = [('Content-Type', 'application/json')] payload = json.dumps(dict(somekey='somevalue')) with mock.patch('invenio_sse.ext._SSEState.publish') as mock_sse, \ mock.patch('invenio_indexer.api.RecordIndexer.bulk_index') \ as mock_indexer, \ api_app.test_request_context(headers=headers, data=payload): event = Event.create(receiver_id=receiver_id) db.session.add(event) event.process() # check messages are sent to the sse channel assert mock_sse.called is True args = list(mock_sse.mock_calls[0])[2] assert args['channel'] == sse_channel assert args['type_'] == 'update_deposit' assert args['data']['meta']['payload']['deposit_id'] == video_1_depid args = list(mock_sse.mock_calls[1])[2] assert args['channel'] == sse_channel assert args['type_'] == 'update_deposit' assert args['data']['meta']['payload']['deposit_id'] == project_depid # check video and project are indexed assert mock_indexer.called is True ids = get_indexed_records_from_mock(mock_indexer) assert video_1_id == ids[0] assert project_id == ids[1] db.session.commit() # check tasks status is propagated to video and project video_1 = deposit_video_resolver(video_1_depid) expected = {u'add': u'SUCCESS', u'failing': u'FAILURE'} assert video_1['_cds']['state'] == expected assert video_1.project['_cds']['state'] == expected events = get_deposit_events(deposit_id=video_1_depid) assert len(events) == 1 def check(project_status, video_1_status, video_2_status): project = deposit_project_resolver(project_depid) video_1 = deposit_video_resolver(video_1_depid) video_2 = deposit_video_resolver(video_2_depid) assert project.status == project_status assert video_1.status == video_1_status assert video_2.status == video_2_status check('draft', 'draft', 'draft') video_2 = deposit_video_resolver(video_2_depid) video_2.publish() check('draft', 'draft', 'published') project = deposit_project_resolver(project_depid) project.publish() check('published', 'published', 'published')