@@ -553,3 +553,93 @@ def test_move_election_post_retains_nominations(
553553 noms = moved_post .get ("nominations" , [])
554554 matching = [n for n in noms if n .get ("nominee_email" ) == admin_user .email ]
555555 assert len (matching ) == 1
556+
557+
558+ def test_move_election_post_keeps_remaining_candidations_in_original_sub_election (
559+ admin_token , admin_user , client , admin_post , member_post , open_election
560+ ):
561+ # Create sub-election with two posts
562+ resp_sub = create_sub_election (
563+ client ,
564+ open_election .election_id ,
565+ token = admin_token ,
566+ title_sv = "Source" ,
567+ title_en = "Source" ,
568+ post_ids = [admin_post .id , member_post .id ],
569+ )
570+ assert resp_sub .status_code in (200 , 201 ), resp_sub .text
571+ sub_election = resp_sub .json ()
572+ sub_election_id = sub_election ["sub_election_id" ]
573+
574+ # Create candidations for both posts for the same user
575+ resp_cand_admin = create_candidation (
576+ client ,
577+ sub_election_id = sub_election_id ,
578+ post_id = admin_post .id ,
579+ token = admin_token ,
580+ user_id = admin_user .id ,
581+ )
582+ assert resp_cand_admin .status_code in (200 , 201 ), resp_cand_admin .text
583+
584+ resp_cand_member = create_candidation (
585+ client ,
586+ sub_election_id = sub_election_id ,
587+ post_id = member_post .id ,
588+ token = admin_token ,
589+ user_id = admin_user .id ,
590+ )
591+ assert resp_cand_member .status_code in (200 , 201 ), resp_cand_member .text
592+ candidate = resp_cand_member .json ()
593+ candidate_id = candidate ["candidate_id" ]
594+ post_ids_before_move = {candidation ["post_id" ] for candidation in candidate ["candidations" ]}
595+ assert post_ids_before_move == {admin_post .id , member_post .id }
596+
597+ # Destination sub-election without posts
598+ resp_dest = create_sub_election (
599+ client ,
600+ open_election .election_id ,
601+ token = admin_token ,
602+ title_sv = "Destination" ,
603+ title_en = "Destination" ,
604+ )
605+ assert resp_dest .status_code in (200 , 201 ), resp_dest .text
606+ dest_sub_election = resp_dest .json ()
607+ dest_sub_election_id = dest_sub_election ["sub_election_id" ]
608+
609+ # Resolve election_post identifier for the admin post
610+ election_post_admin = next (ep for ep in sub_election ["election_posts" ] if ep ["post_id" ] == admin_post .id )
611+ election_post_id = election_post_admin ["election_post_id" ]
612+
613+ # Move election post to the destination sub-election
614+ resp_move = client .patch (
615+ f"/sub-election/{ sub_election_id } /move-election-post" ,
616+ json = {
617+ "election_post_id" : election_post_id ,
618+ "new_sub_election_id" : dest_sub_election_id ,
619+ },
620+ headers = auth_headers (admin_token ),
621+ )
622+ assert resp_move .status_code in (200 , 204 ), resp_move .text
623+
624+ # Original sub-election should still have the candidate with the remaining post
625+ resp_candidates_source = client .get (f"/candidate/sub-election/{ sub_election_id } " , headers = auth_headers (admin_token ))
626+ assert resp_candidates_source .status_code == 200 , resp_candidates_source .text
627+ candidates_source = resp_candidates_source .json ()
628+ original_candidate = next (c for c in candidates_source if c ["candidate_id" ] == candidate_id )
629+ remaining_post_ids = {candidation ["post_id" ] for candidation in original_candidate ["candidations" ]}
630+ assert remaining_post_ids == {member_post .id }
631+ assert all (candidation ["sub_election_id" ] == sub_election_id for candidation in original_candidate ["candidations" ])
632+
633+ # Destination sub-election should have a candidate for the moved post only
634+ resp_candidates_dest = client .get (
635+ f"/candidate/sub-election/{ dest_sub_election_id } " , headers = auth_headers (admin_token )
636+ )
637+ assert resp_candidates_dest .status_code == 200 , resp_candidates_dest .text
638+ candidates_dest = resp_candidates_dest .json ()
639+ moved_candidate = next (c for c in candidates_dest if c ["user_id" ] == admin_user .id )
640+ moved_post_ids = {candidation ["post_id" ] for candidation in moved_candidate ["candidations" ]}
641+ assert moved_post_ids == {admin_post .id }
642+ assert all (
643+ candidation ["sub_election_id" ] == dest_sub_election_id for candidation in moved_candidate ["candidations" ]
644+ )
645+ assert moved_candidate ["candidate_id" ] != candidate_id
0 commit comments