1212from flask_sqlalchemy import get_debug_queries
1313from journalist_app import api2 , create_app
1414from journalist_app .api2 .shared import json_version
15- from journalist_app .api2 .types import Event , EventType , ItemTarget , SourceTarget
15+ from journalist_app .api2 .types import VERSION_LEN , Event , EventType , ItemTarget , SourceTarget
1616from models import Reply , Source , SourceStar , Submission , db
1717from sqlalchemy .orm .exc import MultipleResultsFound
1818from tests .factories import SecureDropConfigFactory
@@ -550,15 +550,18 @@ def test_api2_item_deleted(
550550 assert Reply .query .filter (Reply .uuid == event .target .item_uuid ).one_or_none () is None
551551
552552 # Try to delete something that doesn't exist:
553- event .id = "345678"
554- event .target .item_uuid = "does not exist"
553+ nonexistent_event = Event (
554+ id = "345678" ,
555+ target = ItemTarget (item_uuid = str (uuid .uuid4 ()), version = reply_version ),
556+ type = EventType .ITEM_DELETED ,
557+ )
555558 response = app .post (
556559 url_for ("api2.data" ),
557- json = {"events" : [asdict (event )]},
560+ json = {"events" : [asdict (nonexistent_event )]},
558561 headers = get_api_headers (journalist_api_token ),
559562 )
560- assert response .json ["events" ][event .id ] == [410 , None ]
561- assert event .target .item_uuid not in response .json ["items" ]
563+ assert response .json ["events" ][nonexistent_event .id ] == [410 , None ]
564+ assert nonexistent_event .target .item_uuid not in response .json ["items" ]
562565
563566
564567def test_api2_source_deleted (
@@ -574,7 +577,7 @@ def test_api2_source_deleted(
574577 # Try deleting the source with the wrong version
575578 event = Event (
576579 id = "394758" ,
577- target = SourceTarget (source_uuid = source_uuid , version = "wrong-version" ),
580+ target = SourceTarget (source_uuid = source_uuid , version = "a" * VERSION_LEN ),
578581 type = EventType .SOURCE_DELETED ,
579582 )
580583 response = app .post (
@@ -624,14 +627,17 @@ def test_api2_source_deleted(
624627 assert Source .query .filter (Source .uuid == source_uuid ).one_or_none () is None
625628
626629 # Try to delete a source that doesn't exist
627- event .id = "234567"
628- event .target .source_uuid = "does-not-exist"
630+ nonexistent_event = Event (
631+ id = "234567" ,
632+ target = SourceTarget (source_uuid = str (uuid .uuid4 ()), version = source_version ),
633+ type = EventType .SOURCE_DELETED ,
634+ )
629635 response = app .post (
630636 url_for ("api2.data" ),
631- json = {"events" : [asdict (event )]},
637+ json = {"events" : [asdict (nonexistent_event )]},
632638 headers = get_api_headers (journalist_api_token ),
633639 )
634- assert response .json ["events" ][event .id ] == [410 , None ]
640+ assert response .json ["events" ][nonexistent_event .id ] == [410 , None ]
635641 assert "does-not-exist" not in response .json ["sources" ]
636642
637643
@@ -653,7 +659,7 @@ def test_api2_source_conversation_deleted(
653659 # (intentionally not fetching the correct version)
654660 event = Event (
655661 id = "498567" ,
656- target = SourceTarget (source_uuid = source_uuid , version = "wrong-version" ),
662+ target = SourceTarget (source_uuid = source_uuid , version = "a" * VERSION_LEN ),
657663 type = EventType .SOURCE_CONVERSATION_DELETED ,
658664 )
659665 response = app .post (
@@ -842,16 +848,19 @@ def test_api2_item_seen(
842848 updated_submission = Submission .query .filter (Submission .uuid == submission_uuid ).one ()
843849 assert updated_submission .downloaded is True
844850
845- # Try with invalid item UUID
846- event .id = "234567"
847- event .target .item_uuid = "invalid-uuid"
851+ # Try to mark seen an item that doesn't exist
852+ no_such_item_event = Event (
853+ id = "234567" ,
854+ target = ItemTarget (item_uuid = str (uuid .uuid4 ()), version = item_version ),
855+ type = EventType .ITEM_SEEN ,
856+ )
848857 response = app .post (
849858 url_for ("api2.data" ),
850- json = {"events" : [asdict (event )]},
859+ json = {"events" : [asdict (no_such_item_event )]},
851860 headers = get_api_headers (journalist_api_token ),
852861 )
853- assert response .json ["events" ][event .id ][0 ] == 404
854- assert "could not find item" in response .json ["events" ][event .id ][1 ]
862+ assert response .json ["events" ][no_such_item_event .id ][0 ] == 404
863+ assert "could not find item" in response .json ["events" ][no_such_item_event .id ][1 ]
855864
856865
857866def test_api2_idempotence_period (journalist_app ):
@@ -924,7 +933,7 @@ def test_api2_source_conversation_deleted_resubmission(
924933 # 1. Submit with the wrong version --> Conflict (409).
925934 event = Event (
926935 id = "600100" ,
927- target = SourceTarget (source_uuid = source_uuid , version = "wrong-version" ),
936+ target = SourceTarget (source_uuid = source_uuid , version = "a" * VERSION_LEN ),
928937 type = EventType .SOURCE_CONVERSATION_DELETED ,
929938 )
930939 res1 = app .post (
@@ -955,14 +964,18 @@ def test_api2_source_conversation_deleted_resubmission(
955964 assert index .status_code == 200
956965 correct_version = index .json ["sources" ][source_uuid ]
957966
958- event .target = SourceTarget (source_uuid = source_uuid , version = correct_version )
967+ corrected_event = Event (
968+ id = event .id ,
969+ target = SourceTarget (source_uuid = source_uuid , version = correct_version ),
970+ type = event .type ,
971+ )
959972 res2 = app .post (
960973 url_for ("api2.data" ),
961- json = {"events" : [asdict (event )]},
974+ json = {"events" : [asdict (corrected_event )]},
962975 headers = get_api_headers (journalist_api_token ),
963976 )
964977 assert res2 .status_code == 200
965- assert res2 .json ["events" ][event .id ] == [200 , None ]
978+ assert res2 .json ["events" ][corrected_event .id ] == [200 , None ]
966979
967980 # Confirm that items are returned as deleted.
968981 assert res2 .json ["sources" ][source_uuid ] is not None
@@ -980,11 +993,11 @@ def test_api2_source_conversation_deleted_resubmission(
980993 # 3. Resubmit the same event again --> Already Reported (208).
981994 res3 = app .post (
982995 url_for ("api2.data" ),
983- json = {"events" : [asdict (event )]},
996+ json = {"events" : [asdict (corrected_event )]},
984997 headers = get_api_headers (journalist_api_token ),
985998 )
986999 assert res3 .status_code == 200
987- assert res3 .json ["events" ][event .id ][0 ] == 208
1000+ assert res3 .json ["events" ][corrected_event .id ][0 ] == 208
9881001
9891002
9901003def test_api2_reply_sent_then_requested_item_is_deduped (
0 commit comments