def test_filter_by_pattern(app): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/cats?pattern=Tabby') assert response.status_code == 200 assert response.json['count'] == 2 assert response.json['page'] == 1 assert response.json['total_pages'] == 1 assert len(response.json['data']) == 2 assert dict_contains( response.json['data'][0], { 'id': 1, 'name': 'Ada', 'age': 5, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) assert dict_contains( response.json['data'][1], { 'id': 2, 'name': 'Leo', 'age': 2, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex })
def test_pagination(app): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/cats?$page_size=1') assert response.status_code == 200 assert response.json['count'] == 3 assert response.json['page'] == 1 assert response.json['total_pages'] == 3 assert len(response.json['data']) == 1 assert dict_contains( response.json['data'][0], { 'id': 1, 'name': 'Ada', 'age': 5, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) response = json_call(test_client.get, '/cats?$page=2&$page_size=1') assert response.status_code == 200 assert response.json['count'] == 3 assert response.json['page'] == 2 assert response.json['total_pages'] == 3 assert len(response.json['data']) == 1 assert dict_contains( response.json['data'][0], { 'id': 2, 'name': 'Leo', 'age': 2, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) response = json_call(test_client.get, '/cats?$page=3&$page_size=1') assert response.status_code == 200 assert response.json['count'] == 3 assert response.json['page'] == 3 assert response.json['total_pages'] == 3 assert len(response.json['data']) == 1 assert dict_contains( response.json['data'][0], { 'id': 3, 'name': 'Wilhelmina', 'age': 4, 'pattern': 'Calico', 'updated_at': iso_regex, 'created_at': iso_regex })
def test_get_recurring_latest(app, instances, dbsession): test_client = app.test_client() login(test_client) workflow_instance = WorkflowInstance(workflow_name='workflow1', scheduled=True, run_at=datetime(2017, 6, 4, 6), started_at=datetime(2017, 6, 4, 6), status='running', priority='normal') dbsession.add(workflow_instance) dbsession.commit() response = json_call(test_client.get, '/v1/workflow-instances/recurring-latest') assert response.status_code == 200 assert dict_contains( response.json[0], { 'id': 2, 'workflow_name': 'workflow1', 'status': 'running', 'run_at': '2017-06-04T06:00:00+00:00', 'unique': None, 'params': None, 'priority': 'normal', 'started_at': '2017-06-04T06:00:00+00:00', 'scheduled': True, 'ended_at': None, 'created_at': iso_regex, 'updated_at': iso_regex })
def test_get_task_instance(app, instances): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/v1/task-instances/1') assert response.status_code == 200 assert dict_contains( response.json, { 'id': 1, 'task_name': 'task1', 'workflow_instance_id': 1, 'status': 'success', 'run_at': iso_regex, 'unique': None, 'params': {}, 'priority': 'normal', 'started_at': iso_regex, 'scheduled': True, 'ended_at': iso_regex, 'attempts': 1, 'max_attempts': 1, 'timeout': 300, 'retry_delay': 300, 'push': False, 'push_state': None, 'worker_id': None, 'locked_at': None, 'created_at': iso_regex, 'updated_at': iso_regex })
def test_update_workflow_instance(app, instances): test_client = app.test_client() csrf_token = login(test_client) response = json_call(test_client.get, '/v1/workflow-instances/1') assert response.status_code == 200 workflow_instance = response.json workflow_instance['priority'] = 'high' previous_updated_at = response.json['updated_at'] response = json_call(test_client.put, '/v1/workflow-instances/1', workflow_instance, headers={'X-CSRF': csrf_token}) assert response.status_code == 200 assert dict_contains( response.json, { 'id': 1, 'workflow_name': 'workflow1', 'status': 'running', 'run_at': iso_regex, 'unique': None, 'params': None, 'priority': 'high', 'started_at': iso_regex, 'scheduled': True, 'ended_at': None, 'created_at': iso_regex, 'updated_at': iso_regex }) assert response.json['updated_at'] > previous_updated_at
def test_list_workflow_instances(app, instances): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/v1/workflow-instances') assert response.status_code == 200 assert response.json['count'] == 1 assert response.json['page'] == 1 assert response.json['total_pages'] == 1 assert len(response.json['data']) == 1 assert dict_contains( response.json['data'][0], { 'id': 1, 'workflow_name': 'workflow1', 'status': 'running', 'run_at': '2017-06-03T06:00:00+00:00', 'unique': None, 'params': None, 'priority': 'normal', 'started_at': '2017-06-03T06:00:00+00:00', 'scheduled': True, 'ended_at': None, 'created_at': iso_regex, 'updated_at': iso_regex })
def test_create_workflow_instance(app, instances): test_client = app.test_client() csrf_token = login(test_client) workflow_instance = { 'workflow_name': 'workflow2', 'unique': 'user-32324-payment-973794' } response = json_call(test_client.post, '/v1/workflow-instances', workflow_instance, headers={'X-CSRF': csrf_token}) assert response.status_code == 201 assert dict_contains( response.json, { 'id': 2, 'workflow_name': 'workflow2', 'status': 'queued', 'run_at': iso_regex, 'unique': 'user-32324-payment-973794', 'params': None, 'priority': 'normal', 'started_at': None, 'scheduled': False, 'ended_at': None, 'created_at': iso_regex, 'updated_at': iso_regex })
def test_update(app): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/cats/2') assert response.status_code == 200 assert dict_contains( response.json, { 'id': 2, 'name': 'Leo', 'age': 2, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) cat = response.json cat['age'] = 3 previous_updated_at = cat['updated_at'] response = json_call(test_client.put, '/cats/2', cat, headers={'X-Requested-With': 'requests'}) assert response.status_code == 200 assert dict_contains( response.json, { 'id': 2, 'name': 'Leo', 'age': 3, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) assert response.json['updated_at'] > previous_updated_at response = json_call(test_client.put, '/cats/1234', {}, headers={'X-Requested-With': 'requests'}) assert response.status_code == 404
def test_list_workflows(app): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/v1/workflows') assert response.status_code == 200 assert response.json['count'] == 2 assert response.json['page'] == 1 assert response.json['total_pages'] == 1 assert len(response.json['data']) == 2 assert dict_contains( response.json['data'][0], { 'name': 'workflow1', 'active': True, 'title': None, 'description': None, 'schedule': '0 6 * * *', 'start_date': None, 'end_date': None, 'concurrency': 1, 'sla': None, 'default_priority': 'normal' }) assert dict_contains( response.json['data'][1], { 'name': 'workflow2', 'active': True, 'title': None, 'description': None, 'schedule': None, 'start_date': None, 'end_date': None, 'concurrency': 1, 'sla': None, 'default_priority': 'normal' })
def test_retrieve(app): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/cats/2') assert response.status_code == 200 assert dict_contains( response.json, { 'id': 2, 'name': 'Leo', 'age': 2, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) response = json_call(test_client.get, '/cats/1234') assert response.status_code == 404
def test_get_workflow(app): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/v1/workflows/workflow1') assert response.status_code == 200 assert dict_contains( response.json, { 'name': 'workflow1', 'active': True, 'title': None, 'description': None, 'schedule': '0 6 * * *', 'start_date': None, 'end_date': None, 'concurrency': 1, 'sla': None, 'default_priority': 'normal' })
def test_update_task_instance(app, instances): test_client = app.test_client() csrf_token = login(test_client) response = json_call(test_client.get, '/v1/task-instances/1') assert response.status_code == 200 task_instance = response.json task_instance['worker_id'] = 'foo' previous_updated_at = task_instance['updated_at'] response = json_call(test_client.put, '/v1/task-instances/1', task_instance, headers={'X-CSRF': csrf_token}) assert response.status_code == 200 assert dict_contains( response.json, { 'id': 1, 'task_name': 'task1', 'workflow_instance_id': 1, 'status': 'success', 'run_at': iso_regex, 'unique': None, 'params': {}, 'priority': 'normal', 'started_at': iso_regex, 'scheduled': True, 'ended_at': iso_regex, 'attempts': 1, 'max_attempts': 1, 'timeout': 300, 'retry_delay': 300, 'push': False, 'push_state': None, 'worker_id': 'foo', 'locked_at': None, 'created_at': iso_regex, 'updated_at': iso_regex }) assert response.json['updated_at'] > previous_updated_at
def test_create(app): test_client = app.test_client() login(test_client) response = json_call(test_client.post, '/cats', { 'name': 'Dr. Kitty McMoewMoew', 'pattern': 'Tabby', 'age': 7 }, headers={'X-Requested-With': 'requests'}) assert response.status_code == 201 assert dict_contains( response.json, { 'id': 4, 'name': 'Dr. Kitty McMoewMoew', 'pattern': 'Tabby', 'age': 7, 'updated_at': iso_regex, 'created_at': iso_regex })
def test_create_task_instance(app, instances): test_client = app.test_client() csrf_token = login(test_client) task_instance = { 'task_name': 'task1', 'unique': 'user-32324-payment-973794' } response = json_call(test_client.post, '/v1/task-instances', task_instance, headers={'X-CSRF': csrf_token}) assert response.status_code == 201 assert dict_contains( response.json, { 'id': 5, 'task_name': 'task1', 'workflow_instance_id': None, 'status': 'queued', 'run_at': iso_regex, 'unique': 'user-32324-payment-973794', 'params': {}, 'priority': 'normal', 'started_at': None, 'scheduled': False, 'ended_at': None, 'attempts': 0, 'max_attempts': 1, 'timeout': 300, 'retry_delay': 300, 'push': False, 'push_state': None, 'worker_id': None, 'locked_at': None, 'created_at': iso_regex, 'updated_at': iso_regex })
def test_slack_monitor(dbsession, workflows): taskflow = Taskflow() taskflow.add_workflows(workflows) workflow_instance = WorkflowInstance(workflow_name='workflow1', scheduled=True, run_at=datetime(2017, 6, 3, 6), started_at=datetime(2017, 6, 3, 6), ended_at=datetime( 2017, 6, 3, 6, 0, 36), status='running', priority='normal') dbsession.add(workflow_instance) dbsession.commit() task_instance1 = TaskInstance(task_name='task1', scheduled=True, workflow_instance_id=workflow_instance.id, status='success', run_at=datetime(2017, 6, 3, 6, 0, 34), attempts=1, priority='normal', push=False, timeout=300, retry_delay=300) task_instance2 = TaskInstance(task_name='task2', scheduled=True, workflow_instance_id=workflow_instance.id, status='success', run_at=datetime(2017, 6, 3, 6, 0, 34), attempts=1, priority='normal', push=False, timeout=300, retry_delay=300) task_instance3 = TaskInstance(task_name='task3', scheduled=True, workflow_instance_id=workflow_instance.id, status='failed', run_at=datetime(2017, 6, 3, 6, 0, 34), attempts=1, priority='normal', push=False, timeout=300, retry_delay=300) dbsession.add(task_instance1) dbsession.add(task_instance2) dbsession.add(task_instance3) dbsession.commit() slack_url = 'http://fakeurl.com/foo' slack_monitor = SlackMonitor(taskflow, slack_url=slack_url) with requests_mock.Mocker() as m: m.post(slack_url) slack_monitor.workflow_failed(dbsession, workflow_instance) assert m.called assert m.call_count == 1 request = m.request_history[0] data = request.json() assert dict_contains( data['attachments'][0], { 'text': '<!channel> A workflow in Taskflow failed', 'title': 'Workflow Failure', 'color': '#ff0000', 'fields': [{ 'title': 'Workflow', 'short': False, 'value': 'workflow1' }, { 'title': 'ID', 'value': 1 }, { 'title': 'Priority', 'value': 'normal' }, { 'title': 'Scheduled Run Time', 'value': '2017-06-03 06:00:00' }, { 'title': 'Start Time', 'value': '2017-06-03 06:00:00' }, { 'title': 'Failure Time', 'value': '2017-06-03 06:00:36' }, { 'title': 'Schedule', 'value': 'At 06:00 AM (0 6 * * *)' }] }) assert dict_contains( data['attachments'][1], { 'title': 'Failed Workflow Task', 'color': '#ff0000', 'fields': [{ 'title': 'Task', 'value': 'task3' }, { 'title': 'ID', 'value': 3 }, { 'title': 'Number of Attempts', 'value': 1 }, { 'title': 'Logs', 'value': None }] })
def test_list_task_instances(app, instances): test_client = app.test_client() login(test_client) response = json_call(test_client.get, '/v1/task-instances') assert response.status_code == 200 assert response.json['count'] == 4 assert response.json['page'] == 1 assert response.json['total_pages'] == 1 assert len(response.json['data']) == 4 assert dict_contains( response.json['data'][0], { 'id': 1, 'task_name': 'task1', 'workflow_instance_id': 1, 'status': 'success', 'run_at': iso_regex, 'unique': None, 'params': {}, 'priority': 'normal', 'started_at': iso_regex, 'scheduled': True, 'ended_at': iso_regex, 'attempts': 1, 'max_attempts': 1, 'timeout': 300, 'retry_delay': 300, 'push': False, 'push_state': None, 'worker_id': None, 'locked_at': None, 'created_at': iso_regex, 'updated_at': iso_regex }) assert dict_contains( response.json['data'][1], { 'id': 2, 'task_name': 'task2', 'workflow_instance_id': 1, 'status': 'success', 'run_at': iso_regex, 'unique': None, 'params': {}, 'priority': 'normal', 'started_at': iso_regex, 'scheduled': True, 'ended_at': iso_regex, 'attempts': 1, 'max_attempts': 1, 'timeout': 300, 'retry_delay': 300, 'push': False, 'push_state': None, 'worker_id': None, 'locked_at': None, 'created_at': iso_regex, 'updated_at': iso_regex }) assert dict_contains( response.json['data'][2], { 'id': 3, 'task_name': 'task3', 'workflow_instance_id': 1, 'status': 'success', 'run_at': iso_regex, 'unique': None, 'params': {}, 'priority': 'normal', 'started_at': iso_regex, 'scheduled': True, 'ended_at': iso_regex, 'attempts': 1, 'max_attempts': 1, 'timeout': 300, 'retry_delay': 300, 'push': False, 'push_state': None, 'worker_id': None, 'locked_at': None, 'created_at': iso_regex, 'updated_at': iso_regex }) assert dict_contains( response.json['data'][3], { 'id': 4, 'task_name': 'task4', 'workflow_instance_id': 1, 'status': 'running', 'run_at': iso_regex, 'unique': None, 'params': {}, 'priority': 'normal', 'started_at': iso_regex, 'scheduled': True, 'ended_at': None, 'attempts': 1, 'max_attempts': 1, 'timeout': 300, 'retry_delay': 300, 'push': False, 'push_state': None, 'worker_id': None, 'locked_at': None, 'created_at': iso_regex, 'updated_at': iso_regex })
def test_order_by_pattern(app): test_client = app.test_client() login(test_client) ## asc response = json_call(test_client.get, '/cats?$order_by=pattern') assert response.status_code == 200 assert response.json['count'] == 3 assert response.json['page'] == 1 assert response.json['total_pages'] == 1 assert len(response.json['data']) == 3 assert dict_contains( response.json['data'][0], { 'id': 3, 'name': 'Wilhelmina', 'age': 4, 'pattern': 'Calico', 'updated_at': iso_regex, 'created_at': iso_regex }) assert dict_contains( response.json['data'][1], { 'id': 1, 'name': 'Ada', 'age': 5, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) assert dict_contains( response.json['data'][2], { 'id': 2, 'name': 'Leo', 'age': 2, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) ## desc response = json_call(test_client.get, '/cats?$order_by=-pattern') assert response.status_code == 200 assert response.json['count'] == 3 assert response.json['page'] == 1 assert response.json['total_pages'] == 1 assert len(response.json['data']) == 3 assert dict_contains( response.json['data'][0], { 'id': 1, 'name': 'Ada', 'age': 5, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) assert dict_contains( response.json['data'][1], { 'id': 2, 'name': 'Leo', 'age': 2, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) assert dict_contains( response.json['data'][2], { 'id': 3, 'name': 'Wilhelmina', 'age': 4, 'pattern': 'Calico', 'updated_at': iso_regex, 'created_at': iso_regex })
def test_csrf(app): test_client = app.test_client() login(test_client) ## POST response = json_call(test_client.post, '/cats', { 'name': 'Dr. Kitty McMoewMoew', 'pattern': 'Tabby', 'age': 7 }) assert response.status_code == 401 response = json_call(test_client.post, '/cats', { 'name': 'Dr. Kitty McMoewMoew', 'pattern': 'Tabby', 'age': 7 }, headers={'x-requested-with': 'requests'}) assert response.status_code == 201 assert dict_contains( response.json, { 'id': 4, 'name': 'Dr. Kitty McMoewMoew', 'pattern': 'Tabby', 'age': 7, 'updated_at': iso_regex, 'created_at': iso_regex }) ## PUT response = json_call(test_client.get, '/cats/2') assert response.status_code == 200 cat = response.json cat['age'] = 3 response = json_call(test_client.put, '/cats/2', cat) assert response.status_code == 401 response = json_call(test_client.put, '/cats/2', cat, headers={'X-Requested-With': 'requests'}) assert response.status_code == 200 assert dict_contains( response.json, { 'id': 2, 'name': 'Leo', 'age': 3, 'pattern': 'Tabby', 'updated_at': iso_regex, 'created_at': iso_regex }) ## DELETE response = test_client.delete('/cats/2') assert response.status_code == 401 response = test_client.delete('/cats/2', headers={'X-Requested-With': 'requests'}) assert response.status_code == 204 response = test_client.get('/cats/2') assert response.status_code == 404