authentik.enterprise.endpoints.connectors.fleet.tests.test_connector

  1from json import loads
  2
  3from requests_mock import Mocker
  4from rest_framework.test import APITestCase
  5
  6from authentik.endpoints.facts import OSFamily
  7from authentik.endpoints.models import Device
  8from authentik.enterprise.endpoints.connectors.fleet.models import FleetConnector
  9from authentik.events.models import NotificationWebhookMapping
 10from authentik.lib.generators import generate_id
 11from authentik.lib.tests.utils import load_fixture
 12
 13TEST_HOST_UBUNTU = loads(load_fixture("fixtures/host_ubuntu.json"))
 14TEST_HOST_FEDORA = loads(load_fixture("fixtures/host_fedora.json"))
 15TEST_HOST_MACOS = loads(load_fixture("fixtures/host_macos.json"))
 16TEST_HOST_WINDOWS = loads(load_fixture("fixtures/host_windows.json"))
 17
 18TEST_HOST = {"hosts": [TEST_HOST_UBUNTU, TEST_HOST_MACOS, TEST_HOST_WINDOWS, TEST_HOST_FEDORA]}
 19
 20
 21class TestFleetConnector(APITestCase):
 22    def setUp(self):
 23        self.connector = FleetConnector.objects.create(
 24            name=generate_id(), url="http://localhost", token=generate_id()
 25        )
 26
 27    def test_sync(self):
 28        controller = self.connector.controller(self.connector)
 29        with Mocker() as mock:
 30            mock.get(
 31                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=0&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
 32                json=TEST_HOST,
 33            )
 34            mock.get(
 35                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=1&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
 36                json={"hosts": []},
 37            )
 38            controller.sync_endpoints()
 39        device = Device.objects.filter(
 40            identifier="VMware-56 4d 4a 5a b0 22 7b d7-9b a5 0b dc 8f f2 3b 60"
 41        ).first()
 42        self.assertIsNotNone(device)
 43        self.assertEqual(
 44            device.cached_facts.data,
 45            {
 46                "os": {
 47                    "arch": "x86_64",
 48                    "name": "Ubuntu",
 49                    "family": "linux",
 50                    "version": "24.04.3 LTS",
 51                },
 52                "disks": [],
 53                "vendor": {"fleetdm.com": {"policies": [], "agent_version": ""}},
 54                "network": {"hostname": "ubuntu-desktop", "interfaces": []},
 55                "hardware": {
 56                    "model": "VMware20,1",
 57                    "serial": "VMware-56 4d 4a 5a b0 22 7b d7-9b a5 0b dc 8f f2 3b 60",
 58                    "cpu_count": 2,
 59                    "cpu_name": "Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz",
 60                    "manufacturer": "VMware, Inc.",
 61                    "memory_bytes": 2062721024,
 62                },
 63                "software": [],
 64            },
 65        )
 66
 67    def test_sync_headers(self):
 68        mapping = NotificationWebhookMapping.objects.create(
 69            name=generate_id(), expression="""return {"foo": "bar"}"""
 70        )
 71        self.connector.headers_mapping = mapping
 72        self.connector.save()
 73        controller = self.connector.controller(self.connector)
 74        with Mocker() as mock:
 75            mock.get(
 76                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=0&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
 77                json=TEST_HOST,
 78            )
 79            mock.get(
 80                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=1&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
 81                json={"hosts": []},
 82            )
 83            controller.sync_endpoints()
 84        self.assertEqual(mock.call_count, 2)
 85        self.assertEqual(mock.request_history[0].method, "GET")
 86        self.assertEqual(mock.request_history[0].headers["foo"], "bar")
 87        self.assertEqual(mock.request_history[1].method, "GET")
 88        self.assertEqual(mock.request_history[1].headers["foo"], "bar")
 89
 90    def test_map_host_linux(self):
 91        controller = self.connector.controller(self.connector)
 92        self.assertEqual(
 93            controller.map_os(TEST_HOST_UBUNTU),
 94            {
 95                "arch": "x86_64",
 96                "family": OSFamily.linux,
 97                "name": "Ubuntu",
 98                "version": "24.04.3 LTS",
 99            },
100        )
101        self.assertEqual(
102            controller.map_os(TEST_HOST_FEDORA),
103            {
104                "arch": "x86_64",
105                "family": OSFamily.linux,
106                "name": "Fedora Linux",
107                "version": "43.0.0",
108            },
109        )
110
111    def test_map_host_windows(self):
112        controller = self.connector.controller(self.connector)
113        self.assertEqual(
114            controller.map_os(TEST_HOST_WINDOWS),
115            {
116                "arch": "x86_64",
117                "family": OSFamily.windows,
118                "name": "Windows Server 2022 Datacenter 21H2",
119                "version": "10.0.20348.4405",
120            },
121        )
122
123    def test_map_host_macos(self):
124        controller = self.connector.controller(self.connector)
125        self.assertEqual(
126            controller.map_os(TEST_HOST_MACOS),
127            {
128                "arch": "arm64e",
129                "family": OSFamily.macOS,
130                "name": "macOS",
131                "version": "26.0.1",
132            },
133        )
TEST_HOST_UBUNTU = {'created_at': '2025-11-01T17:25:34Z', 'updated_at': '2026-01-23T12:58:55Z', 'software': None, 'software_updated_at': '2026-01-23T12:58:55Z', 'id': 14, 'detail_updated_at': '2026-01-23T12:58:55Z', 'label_updated_at': '2026-01-23T12:58:55Z', 'policy_updated_at': '2026-01-23T12:29:58Z', 'last_enrolled_at': '2025-11-01T17:25:38Z', 'seen_time': '2026-01-23T13:17:27Z', 'refetch_requested': False, 'hostname': 'ubuntu-desktop', 'uuid': '5a4a4d56-22b0-d77b-9ba5-0bdc8ff23b60', 'platform': 'ubuntu', 'osquery_version': '5.21.0', 'orbit_version': None, 'fleet_desktop_version': None, 'scripts_enabled': None, 'os_version': 'Ubuntu 24.04.3 LTS', 'build': '', 'platform_like': 'debian', 'code_name': 'noble', 'uptime': 1631433000000000, 'memory': 2062721024, 'cpu_type': 'x86_64', 'cpu_subtype': '165', 'cpu_brand': 'Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz', 'cpu_physical_cores': 2, 'cpu_logical_cores': 2, 'hardware_vendor': 'VMware, Inc.', 'hardware_model': 'VMware20,1', 'hardware_version': 'None', 'hardware_serial': 'VMware-56 4d 4a 5a b0 22 7b d7-9b a5 0b dc 8f f2 3b 60', 'computer_name': 'ubuntu-desktop', 'public_ip': '92.116.178.120', 'primary_ip': '10.120.20.61', 'primary_mac': '00:0c:29:f2:3b:60', 'distributed_interval': 10, 'config_tls_refresh': 60, 'logger_tls_period': 10, 'team_id': 2, 'pack_stats': None, 'team_name': 'prod', 'gigs_disk_space_available': 7.37, 'percent_disk_space_available': 31, 'gigs_total_disk_space': 23.08, 'gigs_all_disk_space': 23.08, 'issues': {'failing_policies_count': 0, 'critical_vulnerabilities_count': 0, 'total_issues_count': 0}, 'device_mapping': None, 'mdm': {'enrollment_status': None, 'dep_profile_error': False, 'server_url': None, 'name': '', 'encryption_key_available': False, 'connected_to_fleet': False}, 'refetch_critical_queries_until': None, 'last_restarted_at': '2026-01-04T15:48:22.390118Z', 'status': 'online', 'display_text': 'ubuntu-desktop', 'display_name': 'ubuntu-desktop'}
TEST_HOST_FEDORA = {'created_at': '2026-01-23T15:32:17Z', 'updated_at': '2026-01-23T15:32:28Z', 'software': None, 'software_updated_at': '2026-01-23T15:32:17Z', 'id': 16, 'detail_updated_at': '1970-01-02T00:00:00Z', 'label_updated_at': '1970-01-02T00:00:00Z', 'policy_updated_at': '1970-01-02T00:00:00Z', 'last_enrolled_at': '2026-01-23T15:32:19Z', 'seen_time': '2026-01-23T15:32:21Z', 'refetch_requested': True, 'hostname': 'fedora-workstation', 'uuid': '578c4d56-aff8-0793-14ae-7947392f5fec', 'platform': 'rhel', 'osquery_version': '5.21.0', 'orbit_version': None, 'fleet_desktop_version': None, 'scripts_enabled': None, 'os_version': 'Fedora Linux 43.0.0', 'build': '', 'platform_like': 'rhel', 'code_name': '', 'uptime': 0, 'memory': 4092518400, 'cpu_type': 'x86_64', 'cpu_subtype': '165', 'cpu_brand': 'Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz', 'cpu_physical_cores': 2, 'cpu_logical_cores': 2, 'hardware_vendor': 'VMware, Inc.', 'hardware_model': 'VMware20,1', 'hardware_version': 'None', 'hardware_serial': 'VMware-56 4d 8c 57 f8 af 93 07-14 ae 79 47 39 2f 5f ec', 'computer_name': 'fedora-workstation', 'public_ip': '', 'primary_ip': '', 'primary_mac': '', 'distributed_interval': 10, 'config_tls_refresh': 0, 'logger_tls_period': 10, 'team_id': 2, 'pack_stats': None, 'team_name': 'prod', 'gigs_disk_space_available': 0, 'percent_disk_space_available': 0, 'gigs_total_disk_space': 0, 'gigs_all_disk_space': None, 'issues': {'failing_policies_count': 0, 'critical_vulnerabilities_count': 0, 'total_issues_count': 0}}
TEST_HOST_MACOS = {'created_at': '2025-06-25T22:21:35Z', 'updated_at': '2025-12-20T11:42:09Z', 'software': None, 'software_updated_at': '2025-10-22T02:24:25Z', 'id': 1, 'detail_updated_at': '2025-10-23T23:30:31Z', 'label_updated_at': '2025-10-23T23:30:31Z', 'policy_updated_at': '2025-10-23T23:02:11Z', 'last_enrolled_at': '2025-06-25T22:21:37Z', 'seen_time': '2025-10-23T23:59:08Z', 'refetch_requested': False, 'hostname': 'jens-mac-vm.local', 'uuid': 'C8B98348-A0A6-5838-A321-57B59D788269', 'platform': 'darwin', 'osquery_version': '5.19.0', 'orbit_version': None, 'fleet_desktop_version': None, 'scripts_enabled': None, 'os_version': 'macOS 26.0.1', 'build': '25A362', 'platform_like': 'darwin', 'code_name': '', 'uptime': 256356000000000, 'memory': 4294967296, 'cpu_type': 'arm64e', 'cpu_subtype': 'ARM64E', 'cpu_brand': 'Apple M1 Pro (Virtual)', 'cpu_physical_cores': 8, 'cpu_logical_cores': 8, 'hardware_vendor': 'Apple Inc.', 'hardware_model': 'VirtualMac2,1', 'hardware_version': '', 'hardware_serial': 'Z5DDF07GK6', 'computer_name': 'jens-mac-vm', 'public_ip': '92.116.179.252', 'primary_ip': '192.168.85.3', 'primary_mac': 'e6:9d:21:c2:2f:19', 'distributed_interval': 10, 'config_tls_refresh': 60, 'logger_tls_period': 10, 'team_id': 2, 'pack_stats': None, 'team_name': 'prod', 'gigs_disk_space_available': 23.82, 'percent_disk_space_available': 37, 'gigs_total_disk_space': 62.83, 'gigs_all_disk_space': None, 'issues': {'failing_policies_count': 1, 'critical_vulnerabilities_count': 2, 'total_issues_count': 3}, 'device_mapping': None, 'mdm': {'enrollment_status': 'On (manual)', 'dep_profile_error': False, 'server_url': 'https://fleet.beryjuio-home.k8s.beryju.io/mdm/apple/mdm', 'name': 'Fleet', 'encryption_key_available': False, 'connected_to_fleet': True}, 'refetch_critical_queries_until': None, 'last_restarted_at': '2025-10-21T00:17:55Z', 'status': 'offline', 'display_text': 'jens-mac-vm.local', 'display_name': 'jens-mac-vm'}
TEST_HOST_WINDOWS = {'created_at': '2025-10-19T12:44:09Z', 'updated_at': '2026-01-23T13:11:45Z', 'software': None, 'software_updated_at': '2026-01-22T06:57:30Z', 'id': 13, 'detail_updated_at': '2026-01-23T12:51:35Z', 'label_updated_at': '2026-01-23T12:51:35Z', 'policy_updated_at': '2026-01-23T13:11:45Z', 'last_enrolled_at': '2025-11-05T20:27:14Z', 'seen_time': '2026-01-23T13:17:33Z', 'refetch_requested': False, 'hostname': 'windows-server', 'uuid': 'CFF12F42-9F7D-A575-2C48-01BDC6A733FB', 'platform': 'windows', 'osquery_version': '5.21.0', 'orbit_version': None, 'fleet_desktop_version': None, 'scripts_enabled': None, 'os_version': 'Windows Server 2022 Datacenter 21H2 10.0.20348.4405', 'build': '20348', 'platform_like': 'windows', 'code_name': 'Microsoft Windows Server 2022 Datacenter', 'uptime': 217075000000000, 'memory': 4294967296, 'cpu_type': 'x86_64', 'cpu_subtype': '-1', 'cpu_brand': 'Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz', 'cpu_physical_cores': 1, 'cpu_logical_cores': 2, 'hardware_vendor': 'VMware, Inc.', 'hardware_model': 'VMware20,1', 'hardware_version': '-1', 'hardware_serial': 'VMware-42 2f f1 cf 7d 9f 75 a5-2c 48 01 bd c6 a7 33 fb', 'computer_name': 'WINDOWS-SERVER', 'public_ip': '92.116.178.120', 'primary_ip': '10.120.20.78', 'primary_mac': '00:50:56:af:fb:3a', 'distributed_interval': 10, 'config_tls_refresh': 60, 'logger_tls_period': 10, 'team_id': 2, 'pack_stats': None, 'team_name': 'prod', 'gigs_disk_space_available': 68, 'percent_disk_space_available': 71, 'gigs_total_disk_space': 96, 'gigs_all_disk_space': None, 'issues': {'failing_policies_count': 0, 'critical_vulnerabilities_count': 5, 'total_issues_count': 5}, 'device_mapping': None, 'mdm': {'enrollment_status': None, 'dep_profile_error': False, 'server_url': None, 'name': '', 'encryption_key_available': False, 'connected_to_fleet': False}, 'refetch_critical_queries_until': None, 'last_restarted_at': '2026-01-21T00:33:38.178036Z', 'status': 'online', 'display_text': 'windows-server', 'display_name': 'WINDOWS-SERVER'}
TEST_HOST = {'hosts': [{'created_at': '2025-11-01T17:25:34Z', 'updated_at': '2026-01-23T12:58:55Z', 'software': None, 'software_updated_at': '2026-01-23T12:58:55Z', 'id': 14, 'detail_updated_at': '2026-01-23T12:58:55Z', 'label_updated_at': '2026-01-23T12:58:55Z', 'policy_updated_at': '2026-01-23T12:29:58Z', 'last_enrolled_at': '2025-11-01T17:25:38Z', 'seen_time': '2026-01-23T13:17:27Z', 'refetch_requested': False, 'hostname': 'ubuntu-desktop', 'uuid': '5a4a4d56-22b0-d77b-9ba5-0bdc8ff23b60', 'platform': 'ubuntu', 'osquery_version': '5.21.0', 'orbit_version': None, 'fleet_desktop_version': None, 'scripts_enabled': None, 'os_version': 'Ubuntu 24.04.3 LTS', 'build': '', 'platform_like': 'debian', 'code_name': 'noble', 'uptime': 1631433000000000, 'memory': 2062721024, 'cpu_type': 'x86_64', 'cpu_subtype': '165', 'cpu_brand': 'Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz', 'cpu_physical_cores': 2, 'cpu_logical_cores': 2, 'hardware_vendor': 'VMware, Inc.', 'hardware_model': 'VMware20,1', 'hardware_version': 'None', 'hardware_serial': 'VMware-56 4d 4a 5a b0 22 7b d7-9b a5 0b dc 8f f2 3b 60', 'computer_name': 'ubuntu-desktop', 'public_ip': '92.116.178.120', 'primary_ip': '10.120.20.61', 'primary_mac': '00:0c:29:f2:3b:60', 'distributed_interval': 10, 'config_tls_refresh': 60, 'logger_tls_period': 10, 'team_id': 2, 'pack_stats': None, 'team_name': 'prod', 'gigs_disk_space_available': 7.37, 'percent_disk_space_available': 31, 'gigs_total_disk_space': 23.08, 'gigs_all_disk_space': 23.08, 'issues': {'failing_policies_count': 0, 'critical_vulnerabilities_count': 0, 'total_issues_count': 0}, 'device_mapping': None, 'mdm': {'enrollment_status': None, 'dep_profile_error': False, 'server_url': None, 'name': '', 'encryption_key_available': False, 'connected_to_fleet': False}, 'refetch_critical_queries_until': None, 'last_restarted_at': '2026-01-04T15:48:22.390118Z', 'status': 'online', 'display_text': 'ubuntu-desktop', 'display_name': 'ubuntu-desktop'}, {'created_at': '2025-06-25T22:21:35Z', 'updated_at': '2025-12-20T11:42:09Z', 'software': None, 'software_updated_at': '2025-10-22T02:24:25Z', 'id': 1, 'detail_updated_at': '2025-10-23T23:30:31Z', 'label_updated_at': '2025-10-23T23:30:31Z', 'policy_updated_at': '2025-10-23T23:02:11Z', 'last_enrolled_at': '2025-06-25T22:21:37Z', 'seen_time': '2025-10-23T23:59:08Z', 'refetch_requested': False, 'hostname': 'jens-mac-vm.local', 'uuid': 'C8B98348-A0A6-5838-A321-57B59D788269', 'platform': 'darwin', 'osquery_version': '5.19.0', 'orbit_version': None, 'fleet_desktop_version': None, 'scripts_enabled': None, 'os_version': 'macOS 26.0.1', 'build': '25A362', 'platform_like': 'darwin', 'code_name': '', 'uptime': 256356000000000, 'memory': 4294967296, 'cpu_type': 'arm64e', 'cpu_subtype': 'ARM64E', 'cpu_brand': 'Apple M1 Pro (Virtual)', 'cpu_physical_cores': 8, 'cpu_logical_cores': 8, 'hardware_vendor': 'Apple Inc.', 'hardware_model': 'VirtualMac2,1', 'hardware_version': '', 'hardware_serial': 'Z5DDF07GK6', 'computer_name': 'jens-mac-vm', 'public_ip': '92.116.179.252', 'primary_ip': '192.168.85.3', 'primary_mac': 'e6:9d:21:c2:2f:19', 'distributed_interval': 10, 'config_tls_refresh': 60, 'logger_tls_period': 10, 'team_id': 2, 'pack_stats': None, 'team_name': 'prod', 'gigs_disk_space_available': 23.82, 'percent_disk_space_available': 37, 'gigs_total_disk_space': 62.83, 'gigs_all_disk_space': None, 'issues': {'failing_policies_count': 1, 'critical_vulnerabilities_count': 2, 'total_issues_count': 3}, 'device_mapping': None, 'mdm': {'enrollment_status': 'On (manual)', 'dep_profile_error': False, 'server_url': 'https://fleet.beryjuio-home.k8s.beryju.io/mdm/apple/mdm', 'name': 'Fleet', 'encryption_key_available': False, 'connected_to_fleet': True}, 'refetch_critical_queries_until': None, 'last_restarted_at': '2025-10-21T00:17:55Z', 'status': 'offline', 'display_text': 'jens-mac-vm.local', 'display_name': 'jens-mac-vm'}, {'created_at': '2025-10-19T12:44:09Z', 'updated_at': '2026-01-23T13:11:45Z', 'software': None, 'software_updated_at': '2026-01-22T06:57:30Z', 'id': 13, 'detail_updated_at': '2026-01-23T12:51:35Z', 'label_updated_at': '2026-01-23T12:51:35Z', 'policy_updated_at': '2026-01-23T13:11:45Z', 'last_enrolled_at': '2025-11-05T20:27:14Z', 'seen_time': '2026-01-23T13:17:33Z', 'refetch_requested': False, 'hostname': 'windows-server', 'uuid': 'CFF12F42-9F7D-A575-2C48-01BDC6A733FB', 'platform': 'windows', 'osquery_version': '5.21.0', 'orbit_version': None, 'fleet_desktop_version': None, 'scripts_enabled': None, 'os_version': 'Windows Server 2022 Datacenter 21H2 10.0.20348.4405', 'build': '20348', 'platform_like': 'windows', 'code_name': 'Microsoft Windows Server 2022 Datacenter', 'uptime': 217075000000000, 'memory': 4294967296, 'cpu_type': 'x86_64', 'cpu_subtype': '-1', 'cpu_brand': 'Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz', 'cpu_physical_cores': 1, 'cpu_logical_cores': 2, 'hardware_vendor': 'VMware, Inc.', 'hardware_model': 'VMware20,1', 'hardware_version': '-1', 'hardware_serial': 'VMware-42 2f f1 cf 7d 9f 75 a5-2c 48 01 bd c6 a7 33 fb', 'computer_name': 'WINDOWS-SERVER', 'public_ip': '92.116.178.120', 'primary_ip': '10.120.20.78', 'primary_mac': '00:50:56:af:fb:3a', 'distributed_interval': 10, 'config_tls_refresh': 60, 'logger_tls_period': 10, 'team_id': 2, 'pack_stats': None, 'team_name': 'prod', 'gigs_disk_space_available': 68, 'percent_disk_space_available': 71, 'gigs_total_disk_space': 96, 'gigs_all_disk_space': None, 'issues': {'failing_policies_count': 0, 'critical_vulnerabilities_count': 5, 'total_issues_count': 5}, 'device_mapping': None, 'mdm': {'enrollment_status': None, 'dep_profile_error': False, 'server_url': None, 'name': '', 'encryption_key_available': False, 'connected_to_fleet': False}, 'refetch_critical_queries_until': None, 'last_restarted_at': '2026-01-21T00:33:38.178036Z', 'status': 'online', 'display_text': 'windows-server', 'display_name': 'WINDOWS-SERVER'}, {'created_at': '2026-01-23T15:32:17Z', 'updated_at': '2026-01-23T15:32:28Z', 'software': None, 'software_updated_at': '2026-01-23T15:32:17Z', 'id': 16, 'detail_updated_at': '1970-01-02T00:00:00Z', 'label_updated_at': '1970-01-02T00:00:00Z', 'policy_updated_at': '1970-01-02T00:00:00Z', 'last_enrolled_at': '2026-01-23T15:32:19Z', 'seen_time': '2026-01-23T15:32:21Z', 'refetch_requested': True, 'hostname': 'fedora-workstation', 'uuid': '578c4d56-aff8-0793-14ae-7947392f5fec', 'platform': 'rhel', 'osquery_version': '5.21.0', 'orbit_version': None, 'fleet_desktop_version': None, 'scripts_enabled': None, 'os_version': 'Fedora Linux 43.0.0', 'build': '', 'platform_like': 'rhel', 'code_name': '', 'uptime': 0, 'memory': 4092518400, 'cpu_type': 'x86_64', 'cpu_subtype': '165', 'cpu_brand': 'Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz', 'cpu_physical_cores': 2, 'cpu_logical_cores': 2, 'hardware_vendor': 'VMware, Inc.', 'hardware_model': 'VMware20,1', 'hardware_version': 'None', 'hardware_serial': 'VMware-56 4d 8c 57 f8 af 93 07-14 ae 79 47 39 2f 5f ec', 'computer_name': 'fedora-workstation', 'public_ip': '', 'primary_ip': '', 'primary_mac': '', 'distributed_interval': 10, 'config_tls_refresh': 0, 'logger_tls_period': 10, 'team_id': 2, 'pack_stats': None, 'team_name': 'prod', 'gigs_disk_space_available': 0, 'percent_disk_space_available': 0, 'gigs_total_disk_space': 0, 'gigs_all_disk_space': None, 'issues': {'failing_policies_count': 0, 'critical_vulnerabilities_count': 0, 'total_issues_count': 0}}]}
class TestFleetConnector(rest_framework.test.APITestCase):
 22class TestFleetConnector(APITestCase):
 23    def setUp(self):
 24        self.connector = FleetConnector.objects.create(
 25            name=generate_id(), url="http://localhost", token=generate_id()
 26        )
 27
 28    def test_sync(self):
 29        controller = self.connector.controller(self.connector)
 30        with Mocker() as mock:
 31            mock.get(
 32                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=0&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
 33                json=TEST_HOST,
 34            )
 35            mock.get(
 36                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=1&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
 37                json={"hosts": []},
 38            )
 39            controller.sync_endpoints()
 40        device = Device.objects.filter(
 41            identifier="VMware-56 4d 4a 5a b0 22 7b d7-9b a5 0b dc 8f f2 3b 60"
 42        ).first()
 43        self.assertIsNotNone(device)
 44        self.assertEqual(
 45            device.cached_facts.data,
 46            {
 47                "os": {
 48                    "arch": "x86_64",
 49                    "name": "Ubuntu",
 50                    "family": "linux",
 51                    "version": "24.04.3 LTS",
 52                },
 53                "disks": [],
 54                "vendor": {"fleetdm.com": {"policies": [], "agent_version": ""}},
 55                "network": {"hostname": "ubuntu-desktop", "interfaces": []},
 56                "hardware": {
 57                    "model": "VMware20,1",
 58                    "serial": "VMware-56 4d 4a 5a b0 22 7b d7-9b a5 0b dc 8f f2 3b 60",
 59                    "cpu_count": 2,
 60                    "cpu_name": "Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz",
 61                    "manufacturer": "VMware, Inc.",
 62                    "memory_bytes": 2062721024,
 63                },
 64                "software": [],
 65            },
 66        )
 67
 68    def test_sync_headers(self):
 69        mapping = NotificationWebhookMapping.objects.create(
 70            name=generate_id(), expression="""return {"foo": "bar"}"""
 71        )
 72        self.connector.headers_mapping = mapping
 73        self.connector.save()
 74        controller = self.connector.controller(self.connector)
 75        with Mocker() as mock:
 76            mock.get(
 77                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=0&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
 78                json=TEST_HOST,
 79            )
 80            mock.get(
 81                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=1&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
 82                json={"hosts": []},
 83            )
 84            controller.sync_endpoints()
 85        self.assertEqual(mock.call_count, 2)
 86        self.assertEqual(mock.request_history[0].method, "GET")
 87        self.assertEqual(mock.request_history[0].headers["foo"], "bar")
 88        self.assertEqual(mock.request_history[1].method, "GET")
 89        self.assertEqual(mock.request_history[1].headers["foo"], "bar")
 90
 91    def test_map_host_linux(self):
 92        controller = self.connector.controller(self.connector)
 93        self.assertEqual(
 94            controller.map_os(TEST_HOST_UBUNTU),
 95            {
 96                "arch": "x86_64",
 97                "family": OSFamily.linux,
 98                "name": "Ubuntu",
 99                "version": "24.04.3 LTS",
100            },
101        )
102        self.assertEqual(
103            controller.map_os(TEST_HOST_FEDORA),
104            {
105                "arch": "x86_64",
106                "family": OSFamily.linux,
107                "name": "Fedora Linux",
108                "version": "43.0.0",
109            },
110        )
111
112    def test_map_host_windows(self):
113        controller = self.connector.controller(self.connector)
114        self.assertEqual(
115            controller.map_os(TEST_HOST_WINDOWS),
116            {
117                "arch": "x86_64",
118                "family": OSFamily.windows,
119                "name": "Windows Server 2022 Datacenter 21H2",
120                "version": "10.0.20348.4405",
121            },
122        )
123
124    def test_map_host_macos(self):
125        controller = self.connector.controller(self.connector)
126        self.assertEqual(
127            controller.map_os(TEST_HOST_MACOS),
128            {
129                "arch": "arm64e",
130                "family": OSFamily.macOS,
131                "name": "macOS",
132                "version": "26.0.1",
133            },
134        )

Similar to TransactionTestCase, but use transaction.atomic() to achieve test isolation.

In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).

On database backends with no transaction support, TestCase behaves as TransactionTestCase.

def setUp(self):
23    def setUp(self):
24        self.connector = FleetConnector.objects.create(
25            name=generate_id(), url="http://localhost", token=generate_id()
26        )

Hook method for setting up the test fixture before exercising it.

def test_sync(self):
28    def test_sync(self):
29        controller = self.connector.controller(self.connector)
30        with Mocker() as mock:
31            mock.get(
32                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=0&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
33                json=TEST_HOST,
34            )
35            mock.get(
36                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=1&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
37                json={"hosts": []},
38            )
39            controller.sync_endpoints()
40        device = Device.objects.filter(
41            identifier="VMware-56 4d 4a 5a b0 22 7b d7-9b a5 0b dc 8f f2 3b 60"
42        ).first()
43        self.assertIsNotNone(device)
44        self.assertEqual(
45            device.cached_facts.data,
46            {
47                "os": {
48                    "arch": "x86_64",
49                    "name": "Ubuntu",
50                    "family": "linux",
51                    "version": "24.04.3 LTS",
52                },
53                "disks": [],
54                "vendor": {"fleetdm.com": {"policies": [], "agent_version": ""}},
55                "network": {"hostname": "ubuntu-desktop", "interfaces": []},
56                "hardware": {
57                    "model": "VMware20,1",
58                    "serial": "VMware-56 4d 4a 5a b0 22 7b d7-9b a5 0b dc 8f f2 3b 60",
59                    "cpu_count": 2,
60                    "cpu_name": "Intel(R) Core(TM) i5-10500T CPU @ 2.30GHz",
61                    "manufacturer": "VMware, Inc.",
62                    "memory_bytes": 2062721024,
63                },
64                "software": [],
65            },
66        )
def test_sync_headers(self):
68    def test_sync_headers(self):
69        mapping = NotificationWebhookMapping.objects.create(
70            name=generate_id(), expression="""return {"foo": "bar"}"""
71        )
72        self.connector.headers_mapping = mapping
73        self.connector.save()
74        controller = self.connector.controller(self.connector)
75        with Mocker() as mock:
76            mock.get(
77                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=0&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
78                json=TEST_HOST,
79            )
80            mock.get(
81                "http://localhost/api/v1/fleet/hosts?order_key=hardware_serial&page=1&per_page=50&device_mapping=true&populate_software=true&populate_users=true",
82                json={"hosts": []},
83            )
84            controller.sync_endpoints()
85        self.assertEqual(mock.call_count, 2)
86        self.assertEqual(mock.request_history[0].method, "GET")
87        self.assertEqual(mock.request_history[0].headers["foo"], "bar")
88        self.assertEqual(mock.request_history[1].method, "GET")
89        self.assertEqual(mock.request_history[1].headers["foo"], "bar")
def test_map_host_linux(self):
 91    def test_map_host_linux(self):
 92        controller = self.connector.controller(self.connector)
 93        self.assertEqual(
 94            controller.map_os(TEST_HOST_UBUNTU),
 95            {
 96                "arch": "x86_64",
 97                "family": OSFamily.linux,
 98                "name": "Ubuntu",
 99                "version": "24.04.3 LTS",
100            },
101        )
102        self.assertEqual(
103            controller.map_os(TEST_HOST_FEDORA),
104            {
105                "arch": "x86_64",
106                "family": OSFamily.linux,
107                "name": "Fedora Linux",
108                "version": "43.0.0",
109            },
110        )
def test_map_host_windows(self):
112    def test_map_host_windows(self):
113        controller = self.connector.controller(self.connector)
114        self.assertEqual(
115            controller.map_os(TEST_HOST_WINDOWS),
116            {
117                "arch": "x86_64",
118                "family": OSFamily.windows,
119                "name": "Windows Server 2022 Datacenter 21H2",
120                "version": "10.0.20348.4405",
121            },
122        )
def test_map_host_macos(self):
124    def test_map_host_macos(self):
125        controller = self.connector.controller(self.connector)
126        self.assertEqual(
127            controller.map_os(TEST_HOST_MACOS),
128            {
129                "arch": "arm64e",
130                "family": OSFamily.macOS,
131                "name": "macOS",
132                "version": "26.0.1",
133            },
134        )