def then_get_transmitted_item(context, path): path = apply_placeholders(context, path) assert os.path.isfile(path), '{} is not a file'.format(path) with open(path, 'r') as json_file: data = json.load(json_file) json_file.close() context_data = json.loads(apply_placeholders(context, context.text)) json_match(context_data, data)
def step_impl_when_patch_url(context, url): with context.app.mail.record_messages() as outbox: data = apply_placeholders(context, context.text) url = apply_placeholders(context, url) set_user_default(url, data) context.response = context.client.patch(get_prefixed_url(context.app, url), data=data, headers=context.headers) item = json.loads(context.response.get_data()) context.outbox = outbox store_placeholder(context, url) return item
def step_impl_when_unspike_resource(context, resource, item_id): resource = apply_placeholders(context, resource) item_id = apply_placeholders(context, item_id) item_url = '/{}/{}'.format(resource, item_id) unspike_url = '/{}/unspike/{}'.format(resource, item_id) res = get_res(item_url, context) headers = if_match(context, res.get('_etag')) context.response = context.client.patch(get_prefixed_url(context.app, unspike_url), data='{}', headers=headers)
def step_imp_when_action_resource(context, action, resource, item_id): data = context.text or {} resource = apply_placeholders(context, resource) item_id = apply_placeholders(context, item_id) item_url = '/{}/{}'.format(resource, item_id) action_url = '/{}/{}/{}'.format(resource, action, item_id) res = get_res(item_url, context) headers = if_match(context, res.get('_etag')) context.response = context.client.patch(get_prefixed_url(context.app, action_url), data=json.dumps(data), headers=headers)
def then_versioned_file_exists(context, path): path = apply_placeholders(context, path) assert os.path.isfile(path), '{} is not a file'.format(path) file_name = os.path.basename(path) with open(path, 'r') as json_file: data = json.load(json_file) set_placeholder(context, file_name, data)
def then_we_pushed_x_items(context, count): history = context.http_mock.request_history assert count == len(history), 'there were %d calls' % (len(history), ) if context.text: context_data = json.loads(apply_placeholders(context, context.text)) for i, _ in enumerate(context_data): assert_equal(json_match(context_data[i], history[i].json()), True, msg='item[%d]: %s' % (i, history[i]))
def step_impl_given_(context, resource): tests.setup_auth_consumer(context, tests.test_consumer) data = apply_placeholders(context, context.text) with context.app.test_request_context(context.app.config['URL_PREFIX']): items = [parse(item, resource) for item in json.loads(data)] get_resource_service(resource).post(items) context.data = items context.resource = resource setattr(context, resource, items[-1])
def step_assert_response_header(context): test_headers = json.loads(apply_placeholders(context, context.text)) response_headers = context.response.headers headers_dict = {} for h in response_headers: headers_dict[h[0]] = h[1] for t_h in test_headers: json_match(t_h, headers_dict)
def then_we_get_formatted_item(context): assert_200(context.response) try: response_data = json.loads(context.response.get_data()) formatted_item = json.loads(response_data.get('formatted_item', '')) except Exception: fail_and_print_body(context.response, 'response does not contain a valid formatted_item field') context_data = json.loads(apply_placeholders(context, context.text)) assert_equal(json_match(context_data, formatted_item), True, msg=str(context_data) + '\n != \n' + str(formatted_item))
def step_impl_then_get_media_stream(context, check_string): assert_200(context.response) data = get_json_data(context.response) url = '/upload-raw/%s' % data['filemeta']['media_id'] headers = [('Content - Type', 'application / octet - stream')] headers = unique_headers(headers, context.headers) response = context.client.get(get_prefixed_url(context.app, url), headers=headers) assert_200(response) assert len(response.get_data()), response check_string = apply_placeholders(context, check_string) assert check_string in str(response.stream.response.data)
def then_we_get_array_of_by(context, field, fid): response = get_json_data(context.response) assert field in response, '{} field not defined'.format(field) assert len(response.get(field)), '{} field not defined'.format(field) context_data = json.loads(apply_placeholders(context, context.text)) for row in response[field]: if row[fid] not in context_data.keys(): continue assert_equal(json_match(context_data[row[fid]], row), True, msg=str(row) + '\n != \n' + str(context_data[row[fid]]))
def step_impl_then_we_get_config(context, report_id): assert_200(context.response) if not context.text: return data = get_json_data(context.response) config = next( (c for c in (data.get('_items') or []) if c.get('_id') == report_id), None) expected_config = json.loads(apply_placeholders(context, context.text)) assert_equal(json_match(expected_config, config), True)
def step_impl_given_resource_as_item_list(context, resource): # TODO: Add as contribution to superdesk-core. data = apply_placeholders(context, context.text) with context.app.test_request_context(context.app.config['URL_PREFIX']): if not is_user_resource(resource): get_resource_service(resource).delete_action() items = [parse(item, resource) for item in json.loads(data)] if is_user_resource(resource): for item in items: item.setdefault('needs_activation', False) get_resource_service(resource).post(items) context.data = items context.resource = resource for i, item in enumerate(items): setattr(context, '{}[{}]'.format(resource, i), item)
def step_impl_then_get_charts(context, total_count): assert_200(context.response) data = get_json_data(context.response) int_count = int(total_count) report = (data.get('_items') or [{}])[0] num_reports = len(report.get('highcharts')) assert int_count == num_reports, 'Number of charts not equal. {} != {}'.format( int_count, num_reports) if context.text: try: response_data = json.loads(context.response.get_data()) except Exception: fail_and_print_body(context.response, 'response is not valid json') return report = (response_data.get('_items') or [{}])[0] context_data = json.loads(apply_placeholders(context, context.text)) chart_index = 0 for context_chart in context_data: response_chart = report.get('highcharts')[chart_index] for key, value in context_chart.items(): if isinstance(value, dict): for subkey, subvalue in value.items(): assert response_chart[key][subkey] == subvalue,\ 'chart[{}][{}][{}] {} != {}'.format( chart_index, key, subkey, subvalue, response_chart[key][subkey] ) else: assert response_chart[ key] == value, 'chart[{}][{}] {} != {}'.format( chart_index, key, value, response_chart[key]) chart_index += 1 return response_data
def then_versioned_file_exists(context, path): path = apply_placeholders(context, path) assert os.path.isfile(path), '{} is not a file'.format(path)
def step_impl_then_get_stats_for_item(context): assert_200(context.response) if context.text: try: response_data = json.loads(context.response.get_data()) except Exception: fail_and_print_body(context.response, 'response is not valid json') return stats = response_data.get('stats') or {} context_stats = json.loads(apply_placeholders(context, context.text)) # parent stat entries (i.e. timeline, desk_transitions, featuremedia_updates) for stat_type, stat_entries in context_stats.items(): assert stat_type in stats.keys(), 'stats.{} does not exist'.format( stat_type) if stat_entries is None: assert stats[stat_type] is None, 'stats.{} is not empty'.format( stat_type) continue elif stats[stat_type] is None: assert stat_entries == stats[ stat_type], 'stats.{} {} != {}'.format( stat_type, stats[stat_type], stat_entries) assert len(stat_entries) == len(stats[stat_type]),\ 'stats.{}. len {} != {}.\nStats={}'.format( stat_type, len(stat_entries), len(stats[stat_type]), stats[stat_type] ) stat_index = 0 for stat_entry in stat_entries: expected_stats = stats[stat_type][stat_index] for key, value in stat_entry.items(): assert key in expected_stats.keys( ), 'stats.{}[{}] key "{}" not found'.format( stat_type, stat_index, key) if isinstance(value, dict): for subkey, subvalue in value.items(): assert subkey in expected_stats[key].keys(),\ 'stats.{}[{}][{}] key "{}" not found.\nEntry={}'.format( stat_type, stat_index, key, subkey, expected_stats[key] ) assert json_match(subvalue, expected_stats[key][subkey]),\ 'stats.{}[{}].{}.{} {} != {}\nEntry={}'.format( stat_type, stat_index, key, subkey, expected_stats[key][subkey], subvalue, expected_stats[key] ) else: assert expected_stats[ key] == value, 'stats.{}[{}].{} {} != {}'.format( stat_type, stat_index, key, expected_stats[key], value) stat_index += 1 return response_data