From b5cd9c22d156a28a6fd39bbdd0be29b6b647d00c Mon Sep 17 00:00:00 2001 From: marwoodandrew Date: Tue, 6 Aug 2024 11:33:30 +1000 Subject: [PATCH] Fix broken test build --- .github/workflows/tests-py38.yml | 8 +++++-- .github/workflows/tests.yml | 4 ++-- dev-requirements.txt | 2 +- features/news_api_auth.feature | 2 +- tests/test_agenda.py | 22 +++++++++++++++++++ tests/test_agenda_events_only.py | 7 ++++++ tests/test_commands.py | 5 +++++ tests/test_company_expiry_alerts.py | 2 ++ tests/test_download.py | 5 +++++ tests/test_push.py | 23 +++++++++++++++++++ tests/test_push_events.py | 34 ++++++++++++++++++++++++++++- tests/test_settings.py | 3 +++ tests/test_wire.py | 32 +++++++++++++++++++++++++++ 13 files changed, 142 insertions(+), 7 deletions(-) diff --git a/.github/workflows/tests-py38.yml b/.github/workflows/tests-py38.yml index afa638535..95c5aadc0 100644 --- a/.github/workflows/tests-py38.yml +++ b/.github/workflows/tests-py38.yml @@ -17,7 +17,11 @@ jobs: python-version: 3.8 - name: docker-compose - run: docker-compose -f .actions-docker-compose.yml up -d + run: docker compose -f .actions-docker-compose.yml up -d + + - run: | + sudo apt-get update + sudo apt-get install pkg-config libxml2-dev libxmlsec1-dev libxmlsec1-openssl - name: cache pip uses: actions/cache@v2 @@ -30,7 +34,7 @@ jobs: - name: pip install run: | - python -m pip install --upgrade pip wheel setuptools + python -m pip install --upgrade pip==24.0 wheel setuptools pip install -r dev-requirements.txt - name: flake8 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2427ff764..00208a706 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,7 +17,7 @@ jobs: python-version: 3.6 - name: docker-compose - run: docker-compose -f .actions-docker-compose.yml up -d + run: docker compose -f .actions-docker-compose.yml up -d - run: | sudo apt-get update @@ -76,4 +76,4 @@ jobs: run: npm run test - name: build - run: npm run build + run: npm run build \ No newline at end of file diff --git a/dev-requirements.txt b/dev-requirements.txt index 541fde948..a1de46a22 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -3,7 +3,7 @@ flake8 sphinx sphinx-autobuild -pytest==3.10.0 +pytest==7.0.0 pytest-cov==2.6.1 pytest-mock==1.10.1 responses>=0.10.6,<0.11 diff --git a/features/news_api_auth.feature b/features/news_api_auth.feature index ccef347d8..709b49eaf 100644 --- a/features/news_api_auth.feature +++ b/features/news_api_auth.feature @@ -118,7 +118,7 @@ Feature: News API Authorization "product_type": "news_api" }] """ - When we set header "X-Forwarded-For" to value "123.123.123.001, 192.192.192.192" + When we set header "X-Forwarded-For" to value "123.123.123.1, 192.192.192.192" When we get "news/search?q=fish&include_fields=body_html" Then we get list with 1 items """ diff --git a/tests/test_agenda.py b/tests/test_agenda.py index 08e66d989..d14614bc1 100644 --- a/tests/test_agenda.py +++ b/tests/test_agenda.py @@ -12,6 +12,7 @@ from .utils import post_json, delete_json, get_json, get_admin_user_id, mock_send_email from copy import deepcopy from bson import ObjectId +from pytest import fixture date_time_format = '%Y-%m-%dT%H:%M:%S' @@ -79,6 +80,7 @@ def mock_utcnow(): return datetime.strptime('2018-11-23T22:00:00', date_time_format) +@fixture def test_item_detail(client): resp = client.get('/agenda/urn:conference') assert resp.status_code == 200 @@ -86,6 +88,7 @@ def test_item_detail(client): assert 'Conference Planning' in resp.get_data().decode() +@fixture def test_item_json(client): resp = client.get('/agenda/urn:conference?format=json') data = json.loads(resp.get_data()) @@ -96,6 +99,7 @@ def test_item_json(client): assert 'internal_note' in data['coverages'][0]['planning'] +@fixture def test_item_json_does_not_return_files(client, app): # public user with client.session_transaction() as session: @@ -110,6 +114,7 @@ def test_item_json_does_not_return_files(client, app): assert 'internal_note' not in data['coverages'][0]['planning'] +@fixture def get_bookmarks_count(client, user): resp = client.get('/agenda/search?bookmarks=%s' % str(user)) assert resp.status_code == 200 @@ -117,6 +122,7 @@ def get_bookmarks_count(client, user): return data['_meta']['total'] +@fixture def test_bookmarks(client, app): user_id = get_admin_user_id(app) assert user_id @@ -138,6 +144,7 @@ def test_bookmarks(client, app): assert 0 == get_bookmarks_count(client, user_id) +@fixture def test_item_copy(client, app): resp = client.post('/wire/{}/copy?type=agenda'.format('urn:conference'), content_type='application/json') assert resp.status_code == 200 @@ -150,6 +157,7 @@ def test_item_copy(client, app): assert str(user_id) in data['copies'] +@fixture @mock.patch('newsroom.wire.views.send_email', mock_send_email) def test_share_items(client, app, mocker): user_ids = app.data.insert('users', [{ @@ -183,6 +191,7 @@ def test_share_items(client, app, mocker): assert str(user_id) in data['shares'] +@fixture def test_agenda_search_filtered_by_query_product(client, app): app.data.insert('navigations', [{ '_id': 51, @@ -233,6 +242,7 @@ def test_agenda_search_filtered_by_query_product(client, app): assert '_aggregations' in data +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_coverage_request(client, app): post_json(client, '/settings/general_settings', {'coverage_request_recipients': 'admin@bar.com'}) @@ -252,6 +262,7 @@ def test_coverage_request(client, app): assert 'Some info message' in outbox[0].body +@fixture def test_watch_event(client, app): user_id = get_admin_user_id(app) assert 0 == get_bookmarks_count(client, user_id) @@ -263,6 +274,7 @@ def test_watch_event(client, app): assert 0 == get_bookmarks_count(client, user_id) +@fixture def test_watch_coverages(client, app): user_id = get_admin_user_id(app) @@ -275,6 +287,7 @@ def test_watch_coverages(client, app): assert after_watch_item['coverages'][0]['watches'] == [user_id] +@fixture def test_unwatch_coverages(client, app): user_id = get_admin_user_id(app) @@ -295,6 +308,7 @@ def test_unwatch_coverages(client, app): assert after_watch_item['coverages'][0]['watches'] == [] +@fixture def test_remove_watch_coverages_on_watch_item(client, app): user_id = ObjectId(get_admin_user_id(app)) other_user_id = PUBLIC_USER_ID @@ -318,6 +332,7 @@ def test_remove_watch_coverages_on_watch_item(client, app): assert after_watch_item['watches'] == [user_id] +@fixture def test_fail_watch_coverages(client, app): user_id = get_admin_user_id(app) @@ -344,6 +359,7 @@ def test_fail_watch_coverages(client, app): assert resp.status_code == 403 +@fixture @mock.patch('newsroom.utils.get_utcnow', mock_utcnow) def test_local_time(client, app, mocker): # 9 am Sydney Time - day light saving on @@ -372,6 +388,7 @@ def test_local_time(client, app, mocker): assert '2018-12-23T12:59:59' == end_date.strftime(date_time_format) +@fixture def test_get_location_string(): agenda = {} assert get_location_string(agenda) == '' @@ -402,6 +419,7 @@ def test_get_location_string(): assert get_location_string(agenda) == 'Sydney Opera House, 2 Macquarie Street, Sydney, Sydney, 2000, Australia' +@fixture def test_get_public_contacts(): agenda = {} assert get_public_contacts(agenda) == [] @@ -436,6 +454,7 @@ def test_get_public_contacts(): }] +@fixture def test_get_agenda_dates(): agenda = { 'dates': { @@ -470,6 +489,7 @@ def test_get_agenda_dates(): assert get_agenda_dates(agenda) == '08:00 30/05/2018' +@fixture def test_filter_agenda_by_coverage_status(client): client.post('/push', data=json.dumps(test_planning), content_type='application/json') @@ -497,6 +517,7 @@ def test_filter_agenda_by_coverage_status(client): assert 'urn:conference' == data['_items'][2]['_id'] +@fixture def test_filter_events_only(client): test_planning = { "description_text": "description here", @@ -572,6 +593,7 @@ def test_filter_events_only(client): assert 'coverages' not in data['_items'][0] +@fixture def test_related_wire_items(client, app): test_planning_with_coveragre = deepcopy(test_planning) test_planning_with_coveragre["coverages"] = [ diff --git a/tests/test_agenda_events_only.py b/tests/test_agenda_events_only.py index f0fcb2c1b..74a84c878 100644 --- a/tests/test_agenda_events_only.py +++ b/tests/test_agenda_events_only.py @@ -54,6 +54,7 @@ def set_products(app): }]) +@fixture def test_item_json(client): # public user with client.session_transaction() as session: @@ -67,6 +68,7 @@ def test_item_json(client): assert 'coverages' not in data +@fixture def test_search(client, app): # public user set_products(app) @@ -114,6 +116,7 @@ def set_watch_products(app): }]) +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_event_update(client, app, mocker): event = deepcopy(test_event) @@ -148,6 +151,7 @@ def test_watched_event_sends_notification_for_event_update(client, app, mocker): assert notifications[0]['_id'] == '{}_foo'.format(PUBLIC_USER_ID) +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_unpost_event(client, app, mocker): event = deepcopy(test_event) @@ -178,6 +182,7 @@ def test_watched_event_sends_notification_for_unpost_event(client, app, mocker): assert notifications[0]['_id'] == '{}_foo'.format(PUBLIC_USER_ID) +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_added_planning(client, app, mocker): event = deepcopy(test_event) @@ -205,6 +210,7 @@ def test_watched_event_sends_notification_for_added_planning(client, app, mocker assert len(push_mock.call_args[1]['users']) == 0 +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_cancelled_planning(client, app, mocker): event = deepcopy(test_event) @@ -235,6 +241,7 @@ def test_watched_event_sends_notification_for_cancelled_planning(client, app, mo assert len(push_mock.call_args[1]['users']) == 0 +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_added_coverage(client, app, mocker): event = deepcopy(test_event) diff --git a/tests/test_commands.py b/tests/test_commands.py index 404123e6b..d768a8de6 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -2,6 +2,7 @@ from time import sleep from datetime import datetime, timedelta from eve.utils import ParsedRequest +from pytest import fixture from newsroom.mongo_utils import index_elastic_from_mongo, index_elastic_from_mongo_from_timestamp @@ -15,6 +16,7 @@ def remove_elastic_index(app): es.indices.delete(indices, ignore=[404]) +@fixture def test_item_detail(app, client): remove_elastic_index(app) app.data.init_elastic(app) @@ -38,6 +40,7 @@ def test_item_detail(app, client): assert 3 == len(data['_items']) +@fixture def test_index_from_mongo_hours_from(app, client): remove_elastic_index(app) app.data.init_elastic(app) @@ -56,6 +59,7 @@ def test_index_from_mongo_hours_from(app, client): assert 1 == len(data['_items']) +@fixture def test_index_from_mongo_collection(app, client): remove_elastic_index(app) app.data.init_elastic(app) @@ -74,6 +78,7 @@ def test_index_from_mongo_collection(app, client): assert 3 == len(data['_items']) +@fixture def test_index_from_mongo_from_timestamp(app, client): app.data.remove('items') sorted_items = [{ diff --git a/tests/test_company_expiry_alerts.py b/tests/test_company_expiry_alerts.py index 1be13e88d..53352720f 100644 --- a/tests/test_company_expiry_alerts.py +++ b/tests/test_company_expiry_alerts.py @@ -5,8 +5,10 @@ from superdesk import get_resource_service from .fixtures import items, init_items, init_auth # noqa from unittest import mock +from pytest import fixture +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_company_expiry_alerts(client, app): now = utcnow() diff --git a/tests/test_download.py b/tests/test_download.py index 01f4f3547..b23dd7da5 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -11,6 +11,7 @@ from .fixtures import items, init_items, init_auth, agenda_items, init_agenda_items # noqa from .test_push import upload_binary +from pytest import fixture items_ids = [item['_id'] for item in items[:2]] item = items[:2][0] @@ -254,6 +255,7 @@ def setup_embeds(client, app): '

Par 4

'}, item) +@fixture def test_download_single(client, app): setup_image(client, app) for _format in wire_formats: @@ -264,6 +266,7 @@ def test_download_single(client, app): 'attachment; filename="%s"' % _format['filename']] +@fixture def test_wire_download(client, app): setup_image(client, app) for _format in wire_formats: @@ -284,6 +287,7 @@ def test_wire_download(client, app): assert history[0].get('section') == 'wire' +@fixture def test_ninjs_download(client, app): setup_embeds(client, app) app.config['EMBED_PRODUCT_FILTERING'] = True @@ -323,6 +327,7 @@ def test_ninjs_download(client, app): assert history[0].get('section') == 'wire' +@fixture def test_agenda_download(client, app): setup_image(client, app) for _format in agenda_formats: diff --git a/tests/test_push.py b/tests/test_push.py index 9b9a3db63..d1a9ef3a6 100644 --- a/tests/test_push.py +++ b/tests/test_push.py @@ -13,6 +13,7 @@ from .fixtures import init_auth # noqa from .utils import mock_send_email from unittest import mock +from pytest import fixture def get_signature_headers(data, key): @@ -49,6 +50,7 @@ def get_signature_headers(data, key): } +@fixture def test_push_item_inserts_missing(client, app): assert not app.config['PUSH_KEY'] resp = client.post('/push', data=json.dumps(item), content_type='application/json') @@ -61,6 +63,7 @@ def test_push_item_inserts_missing(client, app): assert '/assets/bar' == data['associations']['featured']['renditions']['thumbnail']['href'] +@fixture def test_push_valid_signature(client, app, mocker): key = b'something random' app.config['PUSH_KEY'] = key @@ -70,6 +73,7 @@ def test_push_valid_signature(client, app, mocker): assert 200 == resp.status_code +@fixture def test_notify_invalid_signature(client, app): app.config['PUSH_KEY'] = b'foo' data = json.dumps({}) @@ -78,6 +82,7 @@ def test_notify_invalid_signature(client, app): assert 403 == resp.status_code +@fixture def test_push_binary(client): media_id = str(bson.ObjectId()) @@ -114,6 +119,7 @@ def upload_binary(fixture, client, media_id=None): return client.get('/assets/%s' % media_id) +@fixture def test_push_binary_thumbnail_saves_copy(client): resp = upload_binary('thumbnail.jpg', client) assert resp.content_type == 'image/jpeg' @@ -121,6 +127,7 @@ def test_push_binary_thumbnail_saves_copy(client): assert resp.content_length == len(picture.read()) +@fixture def test_push_featuremedia_generates_renditions(client): media_id = str(bson.ObjectId()) upload_binary('picture.jpg', client, media_id=media_id) @@ -160,6 +167,7 @@ def test_push_featuremedia_generates_renditions(client): assert 200 == resp.status_code +@fixture def test_push_update_removes_featuremedia(client): media_id = str(bson.ObjectId()) upload_binary('picture.jpg', client, media_id=media_id) @@ -209,6 +217,7 @@ def test_push_update_removes_featuremedia(client): assert data['associations'] is None +@fixture def test_push_featuremedia_has_renditions_for_existing_media(client): media_id = str(bson.ObjectId()) upload_binary('picture.jpg', client, media_id=media_id) @@ -254,6 +263,7 @@ def test_push_featuremedia_has_renditions_for_existing_media(client): assert 200 == resp.status_code +@fixture def test_push_binary_invalid_signature(client, app): app.config['PUSH_KEY'] = b'foo' resp = client.post('/push_binary', data=dict( @@ -263,6 +273,7 @@ def test_push_binary_invalid_signature(client, app): assert 403 == resp.status_code +@fixture def test_notify_topic_matches_for_new_item(client, app, mocker): user_ids = app.data.insert('users', [{ 'email': 'foo@bar.com', @@ -292,6 +303,7 @@ def test_notify_topic_matches_for_new_item(client, app, mocker): assert len(push_mock.call_args[1]['topics']) == 1 +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_user_matches_for_new_item_in_history(client, app, mocker): company_ids = app.data.insert('companies', [{ @@ -333,6 +345,7 @@ def test_notify_user_matches_for_new_item_in_history(client, app, mocker): assert 'http://localhost:5050/wire?item=bar' in outbox[0].body +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_user_matches_for_killed_item_in_history(client, app, mocker): company_ids = app.data.insert('companies', [{ @@ -379,6 +392,7 @@ def test_notify_user_matches_for_killed_item_in_history(client, app, mocker): assert notification['item'] == 'bar' +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_user_matches_for_new_item_in_bookmarks(client, app, mocker): app.data.insert('companies', [{ @@ -445,6 +459,7 @@ def test_notify_user_matches_for_new_item_in_bookmarks(client, app, mocker): assert 'http://localhost:5050/wire?item=bar' in outbox[0].body +@fixture def test_do_not_notify_inactive_user(client, app, mocker): user_ids = app.data.insert('users', [{ 'email': 'foo@bar.com', @@ -470,6 +485,7 @@ def test_do_not_notify_inactive_user(client, app, mocker): assert push_mock.call_args[1]['_items'][0]['_id'] == 'foo' +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_checks_service_subscriptions(client, app, mocker): app.data.insert('companies', [{ @@ -515,6 +531,7 @@ def test_notify_checks_service_subscriptions(client, app, mocker): assert len(outbox) == 0 +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_send_notification_emails(client, app): user_ids = app.data.insert('users', [{ @@ -551,6 +568,7 @@ def test_send_notification_emails(client, app): assert 'http://localhost:5050/wire?item=foo' in outbox[0].body +@fixture def test_matching_topics(client, app): client.post('/push', data=json.dumps(item), content_type='application/json') search = get_resource_service('wire_search') @@ -567,6 +585,7 @@ def test_matching_topics(client, app): assert ['created_from_future', 'query'] == matching +@fixture def test_matching_topics_for_public_user(client, app): app.data.insert('products', [{ '_id': ObjectId('59b4c5c61d41c8d736852fbf'), @@ -594,6 +613,7 @@ def test_matching_topics_for_public_user(client, app): assert ['created_from_future', 'query'] == matching +@fixture def test_matching_topics_for_user_with_inactive_company(client, app): app.data.insert('products', [{ '_id': ObjectId('59b4c5c61d41c8d736852fbf'), @@ -622,6 +642,7 @@ def test_matching_topics_for_user_with_inactive_company(client, app): assert ['created_from_future', 'query'] == matching +@fixture def test_push_parsed_item(client, app): client.post('/push', data=json.dumps(item), content_type='application/json') parsed = get_entity_or_404(item['guid'], 'wire_search') @@ -630,6 +651,7 @@ def test_push_parsed_item(client, app): assert 7 == parsed['charcount'] +@fixture def test_push_parsed_dates(client, app): payload = item.copy() payload['embargoed'] = '2019-01-31T00:01:00+00:00' @@ -640,6 +662,7 @@ def test_push_parsed_dates(client, app): assert isinstance(parsed['embargoed'], datetime) +@fixture def test_push_event_coverage_info(client, app): client.post('/push', data=json.dumps(item), content_type='application/json') parsed = get_entity_or_404(item['guid'], 'items') diff --git a/tests/test_push_events.py b/tests/test_push_events.py index 57b60b3ff..29edd0ba5 100644 --- a/tests/test_push_events.py +++ b/tests/test_push_events.py @@ -12,7 +12,7 @@ from newsroom.notifications import get_user_notifications from .fixtures import init_auth # noqa from unittest import mock - +from pytest import fixture test_event = { 'state': 'scheduled', @@ -190,6 +190,7 @@ 'urgency': 3} +@fixture def test_push_parsed_event(client, app): event = deepcopy(test_event) client.post('/push', data=json.dumps(event), content_type='application/json') @@ -209,6 +210,7 @@ def test_push_parsed_event(client, app): assert 1 == len(data['_items']) +@fixture def test_push_cancelled_event(client, app): event = deepcopy(test_event) event['guid'] = 'foo2' @@ -231,6 +233,7 @@ def test_push_cancelled_event(client, app): assert parsed['event']['pubstatus'] == 'cancelled' +@fixture def test_push_updated_event(client, app): event = deepcopy(test_event) event['guid'] = 'foo3' @@ -252,6 +255,7 @@ def test_push_updated_event(client, app): assert parsed['dates']['end'].day == 30 +@fixture def test_push_parsed_planning_for_an_existing_event(client, app): event = deepcopy(test_event) event['guid'] = 'foo4' @@ -294,6 +298,7 @@ def test_push_parsed_planning_for_an_existing_event(client, app): assert 2 == len(parsed_planning['coverages']) +@fixture def test_push_coverages_with_different_dates_for_an_existing_event(client, app): event = deepcopy(test_event) event['guid'] = 'foo4' @@ -329,6 +334,7 @@ def test_push_coverages_with_different_dates_for_an_existing_event(client, app): planning['coverages'][1]['planning']['scheduled'].replace('0000', '00:00') +@fixture def test_push_planning_with_different_dates_for_an_existing_event(client, app): event = deepcopy(test_event) event['guid'] = 'foo4' @@ -360,6 +366,7 @@ def test_push_planning_with_different_dates_for_an_existing_event(client, app): assert parsed_planning['slugline'] == planning['slugline'] +@fixture def test_push_cancelled_planning_for_an_existing_event(client, app): event = deepcopy(test_event) event['guid'] = 'foo5' @@ -389,6 +396,7 @@ def test_push_cancelled_planning_for_an_existing_event(client, app): assert len(parsed['planning_items']) == 0 +@fixture def test_push_parsed_adhoc_planning_for_an_non_existing_event(client, app): # pushing an event to create the index event = deepcopy(test_event) @@ -410,6 +418,7 @@ def test_push_parsed_adhoc_planning_for_an_non_existing_event(client, app): assert parsed['definition_long'] == test_planning['abstract'] +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_topic_matches_for_new_event_item(client, app, mocker): event = deepcopy(test_event) @@ -444,6 +453,7 @@ def test_notify_topic_matches_for_new_event_item(client, app, mocker): assert len(push_mock.call_args[1]['topics']) == 1 +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_topic_matches_for_new_planning_item(client, app, mocker): event = deepcopy(test_event) @@ -481,6 +491,7 @@ def test_notify_topic_matches_for_new_planning_item(client, app, mocker): assert len(push_mock.call_args[1]['topics']) == 1 +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_topic_matches_for_ad_hoc_planning_item(client, app, mocker): # remove event link from planning item @@ -519,6 +530,7 @@ def test_notify_topic_matches_for_ad_hoc_planning_item(client, app, mocker): assert len(push_mock.call_args[1]['topics']) == 1 +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_user_matches_for_ad_hoc_agenda_in_history(client, app, mocker): company_ids = app.data.insert('companies', [{ @@ -562,6 +574,7 @@ def test_notify_user_matches_for_ad_hoc_agenda_in_history(client, app, mocker): assert notification['item'] == 'bar3' +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_user_matches_for_new_agenda_in_history(client, app, mocker): company_ids = app.data.insert('companies', [{ @@ -600,6 +613,7 @@ def test_notify_user_matches_for_new_agenda_in_history(client, app, mocker): assert notification['item'] == 'foo' +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_user_matches_for_new_planning_in_history(client, app, mocker): event = deepcopy(test_event) @@ -645,6 +659,7 @@ def test_notify_user_matches_for_new_planning_in_history(client, app, mocker): assert notification['item'] == 'foo' +@fixture @mock.patch('newsroom.email.send_email', mock_send_email) def test_notify_user_matches_for_killed_item_in_history(client, app, mocker): event = deepcopy(test_event) @@ -689,6 +704,7 @@ def test_notify_user_matches_for_killed_item_in_history(client, app, mocker): assert notification['item'] == 'foo' +@fixture def test_push_event_with_files(client, app): event = deepcopy(test_event) media_id = 'media-id' @@ -716,6 +732,7 @@ def test_push_event_with_files(client, app): assert 'foo' == resp.get_data().decode('utf-8') +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_push_story_wont_notify_for_first_publish(client, app, mocker): test_item = { @@ -745,6 +762,7 @@ def test_push_story_wont_notify_for_first_publish(client, app, mocker): assert len(outbox) == 0 +@fixture def assign_active_company(app): company_ids = app.data.insert('companies', [{ 'name': 'Press co.', @@ -756,6 +774,7 @@ def assign_active_company(app): return current_user['_id'] +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_event_update(client, app, mocker): event = deepcopy(test_event) @@ -786,6 +805,7 @@ def test_watched_event_sends_notification_for_event_update(client, app, mocker): assert notifications[0]['_id'] == '{}_foo'.format(user_id) +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_unpost_event(client, app, mocker): event = deepcopy(test_event) @@ -814,6 +834,7 @@ def test_watched_event_sends_notification_for_unpost_event(client, app, mocker): assert notifications[0]['_id'] == '{}_foo'.format(user_id) +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_added_planning(client, app, mocker): event = deepcopy(test_event) @@ -842,6 +863,7 @@ def test_watched_event_sends_notification_for_added_planning(client, app, mocker assert notifications[0]['_id'] == '{}_foo'.format(user_id) +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_cancelled_planning(client, app, mocker): event = deepcopy(test_event) @@ -871,6 +893,7 @@ def test_watched_event_sends_notification_for_cancelled_planning(client, app, mo assert notifications[0]['_id'] == '{}_foo'.format(user_id) +@fixture @mock.patch('newsroom.agenda.email.send_email', mock_send_email) def test_watched_event_sends_notification_for_added_coverage(client, app, mocker): event = deepcopy(test_event) @@ -920,6 +943,7 @@ def test_watched_event_sends_notification_for_added_coverage(client, app, mocker assert notifications[0]['_id'] == '{}_foo'.format(user_id) +@fixture def test_filter_killed_events(client, app): event = deepcopy(test_event) post_json(client, '/push', event) @@ -941,6 +965,7 @@ def test_filter_killed_events(client, app): assert 'killed' == parsed['state'] +@fixture def test_push_cancelled_planning_cancels_adhoc_planning(client, app): # pushing an event to create the index event = deepcopy(test_event) @@ -966,6 +991,7 @@ def test_push_cancelled_planning_cancels_adhoc_planning(client, app): assert 'Reason' in parsed['ednote'][0] +@fixture def test_push_update_for_an_item_with_coverage(client, app, mocker): test_item = { 'type': 'text', @@ -1006,6 +1032,7 @@ def test_push_update_for_an_item_with_coverage(client, app, mocker): assert wire_item['_id'] == 'update' +@fixture def test_push_coverages_with_linked_stories(client, app): event = deepcopy(test_event) event['guid'] = 'foo7' @@ -1035,6 +1062,7 @@ def test_push_coverages_with_linked_stories(client, app): assert parsed['coverages'][0]['delivery_href'] is None +@fixture def test_push_coverages_with_updates_to_linked_stories(client, app): event = deepcopy(test_event) event['guid'] = 'foo7' @@ -1080,6 +1108,7 @@ def test_push_coverages_with_updates_to_linked_stories(client, app): assert parsed['coverages'][0]['delivery_href'] == '/wire/item8' +@fixture def test_push_coverages_with_correction_to_linked_stories(client, app): event = deepcopy(test_event) event['guid'] = 'foo7' @@ -1129,6 +1158,7 @@ def test_push_coverages_with_correction_to_linked_stories(client, app): assert parsed['coverages'][0]['delivery_href'] == '/wire/item8' +@fixture def test_push_event_from_planning(client, app): plan = deepcopy(test_planning) plan['guid'] = 'adhoc_plan' @@ -1167,6 +1197,7 @@ def test_push_event_from_planning(client, app): assert parsed['dates']['end'].isoformat() == event['dates']['end'].replace('0000', '00:00') +@fixture def test_coverages_delivery_sequence_has_default(client, app): event = deepcopy(test_event) event['guid'] = 'foo7' @@ -1191,6 +1222,7 @@ def test_coverages_delivery_sequence_has_default(client, app): assert parsed['coverages'][0]['deliveries'][0]['sequence_no'] == 0 +@fixture def test_item_planning_reference_set_on_fulfill(client, app): planning = deepcopy(test_planning) planning['guid'] = 'bar1' diff --git a/tests/test_settings.py b/tests/test_settings.py index 0bee6798a..5de90e68b 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -3,8 +3,10 @@ from .fixtures import items, init_items, init_auth, init_company # noqa from .utils import post_json, get_json +from pytest import fixture +@fixture def test_general_settings(client, app): app.general_setting('foo', 'Foo', default='bar') assert 'bar' == get_setting('foo') @@ -19,6 +21,7 @@ def test_general_settings(client, app): assert 'bar' == get_setting()['foo']['default'] +@fixture def test_news_only_filter(client, app): query = get_setting('news_only_filter') assert query is None diff --git a/tests/test_wire.py b/tests/test_wire.py index 697776646..7bdac8196 100644 --- a/tests/test_wire.py +++ b/tests/test_wire.py @@ -11,8 +11,10 @@ from tests.test_users import ADMIN_USER_ID from tests.test_download import setup_embeds from superdesk import get_resource_service +from pytest import fixture +@fixture def test_item_detail(client): resp = client.get('/wire/tag:foo') assert resp.status_code == 200 @@ -20,12 +22,14 @@ def test_item_detail(client): assert 'Amazon Is Opening More Bookstores' in html +@fixture def test_item_json(client): resp = client.get('/wire/tag:foo?format=json') data = json.loads(resp.get_data()) assert 'headline' in data +@fixture @mock.patch('newsroom.wire.views.send_email', mock_send_email) def test_share_items(client, app): user_ids = app.data.insert('users', [{ @@ -62,6 +66,7 @@ def test_share_items(client, app): assert str(user_id) in data['shares'] +@fixture def get_bookmarks_count(client, user): resp = client.get('/api/wire_search?bookmarks=%s' % str(user)) assert resp.status_code == 200 @@ -69,6 +74,7 @@ def get_bookmarks_count(client, user): return data['_meta']['total'] +@fixture def test_bookmarks(client, app): user_id = get_admin_user_id(app) assert user_id @@ -90,6 +96,7 @@ def test_bookmarks(client, app): assert 0 == get_bookmarks_count(client, user_id) +@fixture def test_bookmarks_by_section(client, app): products = [ { @@ -129,6 +136,7 @@ def test_bookmarks_by_section(client, app): assert 0 == get_bookmarks_count(client, PUBLIC_USER_ID) +@fixture def test_item_copy(client, app): resp = client.post('/wire/{}/copy'.format(items[0]['_id']), content_type='application/json') assert resp.status_code == 200 @@ -141,6 +149,7 @@ def test_item_copy(client, app): assert str(user_id) in data['copies'] +@fixture def test_versions(client, app): resp = client.get('/wire/%s/versions' % items[0]['_id']) assert 200 == resp.status_code @@ -155,6 +164,7 @@ def test_versions(client, app): assert 'c' == data['_items'][1]['service'][0]['code'] +@fixture def test_search_filters_items_with_updates(client, app): resp = client.get('/wire/search') data = json.loads(resp.get_data()) @@ -162,6 +172,7 @@ def test_search_filters_items_with_updates(client, app): assert 'tag:weather' not in [item['_id'] for item in data['_items']] +@fixture def test_search_includes_killed_items(client, app): app.data.insert('items', [{'_id': 'foo', 'pubstatus': 'canceled', 'headline': 'killed'}]) resp = client.get('/wire/search?q=headline:killed') @@ -169,6 +180,7 @@ def test_search_includes_killed_items(client, app): assert 1 == len(data['_items']) +@fixture def test_search_by_products_id(client, app): app.data.insert('items', [{'_id': 'foo', 'headline': 'product test', 'products': [{'code': '12345'}]}]) resp = client.get('/wire/search?q=products.code:12345') @@ -176,12 +188,14 @@ def test_search_by_products_id(client, app): assert 1 == len(data['_items']) +@fixture def test_search_filter_by_category(client, app): resp = client.get('/wire/search?filter=%s' % json.dumps({'service': ['Service A']})) data = json.loads(resp.get_data()) assert 1 == len(data['_items']) +@fixture def test_filter_by_product_anonymous_user_gets_all(client, app): resp = client.get('/wire/search?products=%s' % json.dumps({'10': True})) data = json.loads(resp.get_data()) @@ -189,6 +203,7 @@ def test_filter_by_product_anonymous_user_gets_all(client, app): assert '_aggregations' in data +@fixture def test_logged_in_user_no_product_gets_no_results(client, app): with client.session_transaction() as session: session['user'] = str(PUBLIC_USER_ID) @@ -197,6 +212,7 @@ def test_logged_in_user_no_product_gets_no_results(client, app): assert 403 == resp.status_code +@fixture def test_logged_in_user_no_company_gets_no_results(client, app): with client.session_transaction() as session: session['user'] = str(PUBLIC_USER_ID) @@ -206,6 +222,7 @@ def test_logged_in_user_no_company_gets_no_results(client, app): assert resp.status_code == 403 +@fixture def test_administrator_gets_all_results(client, app): with client.session_transaction() as session: session['user'] = ADMIN_USER_ID @@ -216,6 +233,7 @@ def test_administrator_gets_all_results(client, app): assert 3 == len(data['_items']) +@fixture def test_search_filtered_by_users_products(client, app): app.data.insert('products', [{ '_id': 10, @@ -236,6 +254,7 @@ def test_search_filtered_by_users_products(client, app): assert '_aggregations' in data +@fixture def test_search_filter_by_individual_navigation(client, app): app.data.insert('navigations', [{ '_id': 51, @@ -293,6 +312,7 @@ def test_search_filter_by_individual_navigation(client, app): assert 1 == len(data['_items']) +@fixture def test_search_filtered_by_query_product(client, app): app.data.insert('navigations', [{ '_id': 51, @@ -338,6 +358,7 @@ def test_search_filtered_by_query_product(client, app): assert '_aggregations' in data +@fixture def test_search_pagination(client): resp = client.get('/wire/search?from=25') assert 200 == resp.status_code @@ -349,6 +370,7 @@ def test_search_pagination(client): assert 400 == resp.status_code +@fixture def test_search_created_from(client): resp = client.get('/wire/search?created_from=now/d') data = json.loads(resp.get_data()) @@ -364,6 +386,7 @@ def test_search_created_from(client): assert 1 <= len(data['_items']) +@fixture def test_search_created_to(client): resp = client.get('/wire/search?created_to=%s' % datetime.now().strftime('%Y-%m-%d')) data = json.loads(resp.get_data()) @@ -377,6 +400,7 @@ def test_search_created_to(client): assert 0 == len(data['_items']) +@fixture def test_item_detail_access(client, app): item_url = '/wire/%s' % items[0]['_id'] data = get_json(client, item_url) @@ -409,6 +433,7 @@ def test_item_detail_access(client, app): assert data['body_html'] +@fixture def test_search_using_section_filter_for_public_user(client, app): app.data.insert('navigations', [{ '_id': 51, @@ -474,6 +499,7 @@ def test_search_using_section_filter_for_public_user(client, app): assert 0 == len(data['_items']) +@fixture def test_administrator_gets_results_based_on_section_filter(client, app): with client.session_transaction() as session: session['user'] = ADMIN_USER_ID @@ -492,6 +518,7 @@ def test_administrator_gets_results_based_on_section_filter(client, app): assert 1 == len(data['_items']) +@fixture def test_time_limited_access(client, app): app.data.insert('products', [{ '_id': 10, @@ -529,6 +556,7 @@ def test_time_limited_access(client, app): assert 2 == len(data['_items']) +@fixture def test_company_type_filter(client, app): app.data.insert('products', [{ '_id': 10, @@ -569,6 +597,7 @@ def test_company_type_filter(client, app): assert 'WEATHER' != data['_items'][0]['slugline'] +@fixture def test_search_by_products_and_filtered_by_embargoe(client, app): app.data.insert('products', [{ '_id': 10, @@ -601,6 +630,7 @@ def test_search_by_products_and_filtered_by_embargoe(client, app): assert items[0]['headline'] == 'china story' +@fixture def test_wire_delete(client, app): docs = [ items[1], @@ -642,6 +672,7 @@ def test_wire_delete(client, app): assert get_resource_service('items_versions').find_one(req=None, _id_document=doc['_id']) is None +@fixture def test_highlighting(client, app): app.data.insert('items', [{'_id': 'foo', 'body_html': 'Story that involves cheese and onions'}]) resp = client.get('/wire/search?q=cheese&es_highlight=1') @@ -650,6 +681,7 @@ def test_highlighting(client, app): 'cheese and onions' +@fixture def test_embed_mark_disable_download(client, app): app.config['EMBED_PRODUCT_FILTERING'] = True user = app.data.find_one('users', req=None, _id=ADMIN_USER_ID)