|
| 1 | +import asyncio |
| 2 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +import yaml |
| 6 | + |
| 7 | +from plugins.training.app.flags.autonomous.blue_2 import AutonomousBlue2 |
| 8 | + |
| 9 | + |
| 10 | +class TestAutonomousBlue2: |
| 11 | + """Tests for the 'Malicious file on system' blue training flag (#167).""" |
| 12 | + |
| 13 | + def test_flag_importable_from_correct_path(self): |
| 14 | + """The flag class must be importable from flags.autonomous.blue_2.""" |
| 15 | + assert AutonomousBlue2 is not None |
| 16 | + assert AutonomousBlue2.name == 'Malicious file on system' |
| 17 | + |
| 18 | + def test_flag_registered_in_blue_certificate_yaml(self): |
| 19 | + """The flag must be uncommented and listed in the Blue Certificate YAML.""" |
| 20 | + with open('plugins/training/data/certifications/8da8f0b3-194a-4eed-95b0-43c1f1b64091.yml') as f: |
| 21 | + cert = yaml.safe_load(f) |
| 22 | + autonomous_flags = cert['badges']['autonomous'] |
| 23 | + assert 'flags.autonomous.blue_2.AutonomousBlue2' in autonomous_flags |
| 24 | + |
| 25 | + def test_flag_has_no_visible_false(self): |
| 26 | + """The flag should not have visible=False which would hide it from users.""" |
| 27 | + assert not hasattr(AutonomousBlue2, 'visible') or AutonomousBlue2.visible is not False |
| 28 | + |
| 29 | + @pytest.mark.asyncio |
| 30 | + async def test_verify_returns_true_when_file_found_and_deleted(self): |
| 31 | + """Flag should be granted when the file is found AND deleted.""" |
| 32 | + flag = AutonomousBlue2(number=1) |
| 33 | + |
| 34 | + mock_op = MagicMock() |
| 35 | + mock_op.ran_ability_id = MagicMock(return_value=True) |
| 36 | + |
| 37 | + mock_fact_hash = MagicMock() |
| 38 | + mock_fact_hash.trait = 'file.malicious.hash' |
| 39 | + mock_fact_file = MagicMock() |
| 40 | + mock_fact_file.trait = 'host.malicious.file' |
| 41 | + mock_op.all_facts = AsyncMock(return_value=[mock_fact_hash, mock_fact_file]) |
| 42 | + |
| 43 | + mock_data_svc = AsyncMock() |
| 44 | + mock_data_svc.locate = AsyncMock(return_value=[mock_op]) |
| 45 | + services = {'data_svc': mock_data_svc} |
| 46 | + |
| 47 | + result = await flag.verify(services) |
| 48 | + assert result is True |
| 49 | + |
| 50 | + @pytest.mark.asyncio |
| 51 | + async def test_verify_returns_false_when_file_not_deleted(self): |
| 52 | + """Flag should NOT be granted when file is found but not deleted.""" |
| 53 | + flag = AutonomousBlue2(number=1) |
| 54 | + |
| 55 | + mock_op = MagicMock() |
| 56 | + # ran_ability_id returns True for find, False for delete |
| 57 | + def selective_ran(ability_id): |
| 58 | + if ability_id == 'f9b3eff0-e11c-48de-9338-1578b351b14b': |
| 59 | + return True # file found ability |
| 60 | + return False # file delete ability not run |
| 61 | + |
| 62 | + mock_op.ran_ability_id = MagicMock(side_effect=selective_ran) |
| 63 | + |
| 64 | + mock_fact_hash = MagicMock() |
| 65 | + mock_fact_hash.trait = 'file.malicious.hash' |
| 66 | + mock_fact_file = MagicMock() |
| 67 | + mock_fact_file.trait = 'host.malicious.file' |
| 68 | + mock_op.all_facts = AsyncMock(return_value=[mock_fact_hash, mock_fact_file]) |
| 69 | + |
| 70 | + mock_data_svc = AsyncMock() |
| 71 | + mock_data_svc.locate = AsyncMock(return_value=[mock_op]) |
| 72 | + services = {'data_svc': mock_data_svc} |
| 73 | + |
| 74 | + result = await flag.verify(services) |
| 75 | + assert result is False |
| 76 | + |
| 77 | + @pytest.mark.asyncio |
| 78 | + async def test_verify_returns_false_when_no_operations(self): |
| 79 | + """Flag should NOT be granted when there are no Blue Autonomous operations.""" |
| 80 | + flag = AutonomousBlue2(number=1) |
| 81 | + |
| 82 | + mock_data_svc = AsyncMock() |
| 83 | + mock_data_svc.locate = AsyncMock(return_value=[]) |
| 84 | + services = {'data_svc': mock_data_svc} |
| 85 | + |
| 86 | + result = await flag.verify(services) |
| 87 | + assert result is False |
0 commit comments