diff --git a/test/services/unit/assignment_service_test.rb b/test/services/unit/assignment_service_test.rb index dd88d681..f7599d74 100644 --- a/test/services/unit/assignment_service_test.rb +++ b/test/services/unit/assignment_service_test.rb @@ -114,6 +114,86 @@ class AssignmentServiceTest < ForemanAnsibleDirectorTestCase ) assert_equal 2, target_assignments.count end + + test 'does nothing for empty assignments' do + FactoryBot.create( + :ansible_content_assignment, + consumable: @collection_role, + assignable: @host + ) + + ::ForemanAnsibleDirector::AssignmentService.create_bulk_assignments(assignments: []) + + assert_equal 1, ::ForemanAnsibleDirector::AnsibleContentAssignment.where(assignable: @host).count + end + + test 'raises and keeps existing assignments when source is missing' do + existing_assignment = FactoryBot.create( + :ansible_content_assignment, + consumable: @collection_role, + assignable: @host + ) + + assert_raises(ActiveRecord::StatementInvalid) do + ::ForemanAnsibleDirector::AssignmentService.create_bulk_assignments( + assignments: [ + { source: { type: 'ACR', id: -1 }, target: { type: 'HOST', id: @host.id } } + ] + ) + end + + assert_equal [existing_assignment], ::ForemanAnsibleDirector::AnsibleContentAssignment.where( + assignable: @host + ).to_a + end + + test 'raises and keeps existing assignments when target is missing' do + existing_assignment = FactoryBot.create( + :ansible_content_assignment, + consumable: @collection_role, + assignable: @host + ) + + assert_raises(NoMethodError) do + ::ForemanAnsibleDirector::AssignmentService.create_bulk_assignments( + assignments: [ + { source: { type: 'ACR', id: @collection_role.id }, target: { type: 'HOST', id: -1 } } + ] + ) + end + + assert_equal [existing_assignment], ::ForemanAnsibleDirector::AnsibleContentAssignment.where( + assignable: @host + ).to_a + end + + test 'rolls back all target changes when a later assignment fails' do + existing_assignment = FactoryBot.create( + :ansible_content_assignment, + consumable: @collection_role, + assignable: @host + ) + + collection2 = FactoryBot.create(:ansible_collection, organization: @organization) + collection_version2 = FactoryBot.create(:content_unit_version, :for_collection, versionable: collection2) + collection_role2 = FactoryBot.create( + :ansible_collection_role, + ansible_collection_version: collection_version2 + ) + + assert_raises(ActiveRecord::StatementInvalid) do + ::ForemanAnsibleDirector::AssignmentService.create_bulk_assignments( + assignments: [ + { source: { type: 'ACR', id: collection_role2.id }, target: { type: 'HOST', id: @host.id } }, + { source: { type: 'ACR', id: -1 }, target: { type: 'HOST', id: @host.id } } + ] + ) + end + + assert_equal [existing_assignment], ::ForemanAnsibleDirector::AnsibleContentAssignment.where( + assignable: @host + ).to_a + end end describe '#finder' do