Refactor Whois tests (#88456)

This commit is contained in:
Franck Nijhof 2023-02-20 03:14:10 +01:00 committed by GitHub
parent 037b3ebd26
commit 172d505314
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 942 additions and 194 deletions

View File

@ -28,13 +28,6 @@ def mock_config_entry() -> MockConfigEntry:
) )
@pytest.fixture
def mock_whois_config_flow() -> Generator[MagicMock, None, None]:
"""Return a mocked whois."""
with patch("homeassistant.components.whois.config_flow.whois.query") as whois_mock:
yield whois_mock
@pytest.fixture @pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]: def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Mock setting up a config entry.""" """Mock setting up a config entry."""
@ -47,10 +40,11 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
@pytest.fixture @pytest.fixture
def mock_whois() -> Generator[MagicMock, None, None]: def mock_whois() -> Generator[MagicMock, None, None]:
"""Return a mocked query.""" """Return a mocked query."""
with patch( with patch(
"homeassistant.components.whois.whois_query", "homeassistant.components.whois.whois_query",
) as whois_mock: ) as whois_mock, patch(
"homeassistant.components.whois.config_flow.whois.query", new=whois_mock
):
domain = whois_mock.return_value domain = whois_mock.return_value
domain.abuse_contact = "abuse@example.com" domain.abuse_contact = "abuse@example.com"
domain.admin = "admin@example.com" domain.admin = "admin@example.com"

View File

@ -0,0 +1,181 @@
# serializer version: 1
# name: test_full_flow_with_error[FailedParsingWhoisOutput-unexpected_response]
FlowResultSnapshot({
'context': dict({
'source': 'user',
'unique_id': 'example.com',
}),
'data': dict({
'domain': 'example.com',
}),
'description': None,
'description_placeholders': None,
'flow_id': <ANY>,
'handler': 'whois',
'options': dict({
}),
'result': ConfigEntrySnapshot({
'data': dict({
'domain': 'example.com',
}),
'disabled_by': None,
'domain': 'whois',
'entry_id': <ANY>,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Example.com',
'unique_id': 'example.com',
'version': 1,
}),
'title': 'Example.com',
'type': <FlowResultType.CREATE_ENTRY: 'create_entry'>,
'version': 1,
})
# ---
# name: test_full_flow_with_error[UnknownDateFormat-unknown_date_format]
FlowResultSnapshot({
'context': dict({
'source': 'user',
'unique_id': 'example.com',
}),
'data': dict({
'domain': 'example.com',
}),
'description': None,
'description_placeholders': None,
'flow_id': <ANY>,
'handler': 'whois',
'options': dict({
}),
'result': ConfigEntrySnapshot({
'data': dict({
'domain': 'example.com',
}),
'disabled_by': None,
'domain': 'whois',
'entry_id': <ANY>,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Example.com',
'unique_id': 'example.com',
'version': 1,
}),
'title': 'Example.com',
'type': <FlowResultType.CREATE_ENTRY: 'create_entry'>,
'version': 1,
})
# ---
# name: test_full_flow_with_error[UnknownTld-unknown_tld]
FlowResultSnapshot({
'context': dict({
'source': 'user',
'unique_id': 'example.com',
}),
'data': dict({
'domain': 'example.com',
}),
'description': None,
'description_placeholders': None,
'flow_id': <ANY>,
'handler': 'whois',
'options': dict({
}),
'result': ConfigEntrySnapshot({
'data': dict({
'domain': 'example.com',
}),
'disabled_by': None,
'domain': 'whois',
'entry_id': <ANY>,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Example.com',
'unique_id': 'example.com',
'version': 1,
}),
'title': 'Example.com',
'type': <FlowResultType.CREATE_ENTRY: 'create_entry'>,
'version': 1,
})
# ---
# name: test_full_flow_with_error[WhoisCommandFailed-whois_command_failed]
FlowResultSnapshot({
'context': dict({
'source': 'user',
'unique_id': 'example.com',
}),
'data': dict({
'domain': 'example.com',
}),
'description': None,
'description_placeholders': None,
'flow_id': <ANY>,
'handler': 'whois',
'options': dict({
}),
'result': ConfigEntrySnapshot({
'data': dict({
'domain': 'example.com',
}),
'disabled_by': None,
'domain': 'whois',
'entry_id': <ANY>,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Example.com',
'unique_id': 'example.com',
'version': 1,
}),
'title': 'Example.com',
'type': <FlowResultType.CREATE_ENTRY: 'create_entry'>,
'version': 1,
})
# ---
# name: test_full_user_flow
FlowResultSnapshot({
'context': dict({
'source': 'user',
'unique_id': 'example.com',
}),
'data': dict({
'domain': 'example.com',
}),
'description': None,
'description_placeholders': None,
'flow_id': <ANY>,
'handler': 'whois',
'options': dict({
}),
'result': ConfigEntrySnapshot({
'data': dict({
'domain': 'example.com',
}),
'disabled_by': None,
'domain': 'whois',
'entry_id': <ANY>,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Example.com',
'unique_id': 'example.com',
'version': 1,
}),
'title': 'Example.com',
'type': <FlowResultType.CREATE_ENTRY: 'create_entry'>,
'version': 1,
})
# ---

View File

@ -0,0 +1,13 @@
# serializer version: 1
# name: test_diagnostics
dict({
'creation_date': '2019-01-01T00:00:00',
'dnssec': True,
'expiration_date': '2023-01-01T00:00:00',
'last_updated': '2022-01-01T00:00:00+01:00',
'status': 'OK',
'statuses': list([
'OK',
]),
})
# ---

View File

@ -0,0 +1,679 @@
# serializer version: 1
# name: test_whois_sensors[sensor.home_assistant_io_admin]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'home-assistant.io Admin',
'icon': 'mdi:account-star',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_admin',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'admin@example.com',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_admin].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_admin',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:account-star',
'original_name': 'Admin',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_admin',
'unit_of_measurement': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_admin].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_created]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'timestamp',
'friendly_name': 'home-assistant.io Created',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_created',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '2019-01-01T00:00:00+00:00',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_created].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_created',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
'original_icon': None,
'original_name': 'Created',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_creation_date',
'unit_of_measurement': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_created].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_days_until_expiration]
StateSnapshot({
'attributes': ReadOnlyDict({
'expires': '2023-01-01T00:00:00',
'friendly_name': 'home-assistant.io Days until expiration',
'icon': 'mdi:calendar-clock',
'name_servers': 'ns1.example.com ns2.example.com',
'registrar': 'My Registrar',
'unit_of_measurement': <UnitOfTime.DAYS: 'd'>,
'updated': '2022-01-01T00:00:00+01:00',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_days_until_expiration',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '364',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_days_until_expiration].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.home_assistant_io_days_until_expiration',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:calendar-clock',
'original_name': 'Days until expiration',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_days_until_expiration',
'unit_of_measurement': <UnitOfTime.DAYS: 'd'>,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_days_until_expiration].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_expires]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'timestamp',
'friendly_name': 'home-assistant.io Expires',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_expires',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '2023-01-01T00:00:00+00:00',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_expires].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_expires',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
'original_icon': None,
'original_name': 'Expires',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_expiration_date',
'unit_of_measurement': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_expires].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_last_updated]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'timestamp',
'friendly_name': 'home-assistant.io Last updated',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_last_updated',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '2021-12-31T23:00:00+00:00',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_last_updated].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_last_updated',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
'original_icon': None,
'original_name': 'Last updated',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_last_updated',
'unit_of_measurement': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_last_updated].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_owner]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'home-assistant.io Owner',
'icon': 'mdi:account',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_owner',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'owner@example.com',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_owner].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_owner',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:account',
'original_name': 'Owner',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_owner',
'unit_of_measurement': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_owner].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_registrant]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'home-assistant.io Registrant',
'icon': 'mdi:account-edit',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_registrant',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'registrant@example.com',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_registrant].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_registrant',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:account-edit',
'original_name': 'Registrant',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_registrant',
'unit_of_measurement': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_registrant].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_registrar]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'home-assistant.io Registrar',
'icon': 'mdi:store',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_registrar',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'My Registrar',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_registrar].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_registrar',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:store',
'original_name': 'Registrar',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_registrar',
'unit_of_measurement': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_registrar].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_reseller]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'home-assistant.io Reseller',
'icon': 'mdi:store',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_reseller',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'Top Domains, Low Prices',
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_reseller].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_reseller',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:store',
'original_name': 'Reseller',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_reseller',
'unit_of_measurement': None,
})
# ---
# name: test_whois_sensors[sensor.home_assistant_io_reseller].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'whois',
'home-assistant.io',
),
}),
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'home-assistant.io',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_whois_sensors_missing_some_attrs
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'timestamp',
'friendly_name': 'home-assistant.io Last updated',
}),
'context': <ANY>,
'entity_id': 'sensor.home_assistant_io_last_updated',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '2021-12-31T23:00:00+00:00',
})
# ---
# name: test_whois_sensors_missing_some_attrs.1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.home_assistant_io_last_updated',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
'original_icon': None,
'original_name': 'Last updated',
'platform': 'whois',
'supported_features': 0,
'translation_key': None,
'unique_id': 'home-assistant.io_last_updated',
'unit_of_measurement': None,
})
# ---

View File

@ -2,6 +2,7 @@
from unittest.mock import AsyncMock, MagicMock from unittest.mock import AsyncMock, MagicMock
import pytest import pytest
from syrupy.assertion import SnapshotAssertion
from whois.exceptions import ( from whois.exceptions import (
FailedParsingWhoisOutput, FailedParsingWhoisOutput,
UnknownDateFormat, UnknownDateFormat,
@ -18,10 +19,11 @@ from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@pytest.mark.usefixtures("mock_whois")
async def test_full_user_flow( async def test_full_user_flow(
hass: HomeAssistant, hass: HomeAssistant,
mock_setup_entry: AsyncMock, mock_setup_entry: AsyncMock,
mock_whois_config_flow: MagicMock, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test the full user configuration flow.""" """Test the full user configuration flow."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -37,8 +39,7 @@ async def test_full_user_flow(
) )
assert result2.get("type") == FlowResultType.CREATE_ENTRY assert result2.get("type") == FlowResultType.CREATE_ENTRY
assert result2.get("title") == "Example.com" assert result2 == snapshot
assert result2.get("data") == {CONF_DOMAIN: "example.com"}
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@ -55,7 +56,8 @@ async def test_full_user_flow(
async def test_full_flow_with_error( async def test_full_flow_with_error(
hass: HomeAssistant, hass: HomeAssistant,
mock_setup_entry: AsyncMock, mock_setup_entry: AsyncMock,
mock_whois_config_flow: MagicMock, mock_whois: MagicMock,
snapshot: SnapshotAssertion,
throw: Exception, throw: Exception,
reason: str, reason: str,
) -> None: ) -> None:
@ -71,7 +73,7 @@ async def test_full_flow_with_error(
assert result.get("type") == FlowResultType.FORM assert result.get("type") == FlowResultType.FORM
assert result.get("step_id") == SOURCE_USER assert result.get("step_id") == SOURCE_USER
mock_whois_config_flow.side_effect = throw mock_whois.side_effect = throw
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
user_input={CONF_DOMAIN: "Example.com"}, user_input={CONF_DOMAIN: "Example.com"},
@ -82,27 +84,26 @@ async def test_full_flow_with_error(
assert result2.get("errors") == {"base": reason} assert result2.get("errors") == {"base": reason}
assert len(mock_setup_entry.mock_calls) == 0 assert len(mock_setup_entry.mock_calls) == 0
assert len(mock_whois_config_flow.mock_calls) == 1 assert len(mock_whois.mock_calls) == 1
mock_whois_config_flow.side_effect = None mock_whois.side_effect = None
result3 = await hass.config_entries.flow.async_configure( result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], result2["flow_id"],
user_input={CONF_DOMAIN: "Example.com"}, user_input={CONF_DOMAIN: "Example.com"},
) )
assert result3.get("type") == FlowResultType.CREATE_ENTRY assert result3.get("type") == FlowResultType.CREATE_ENTRY
assert result3.get("title") == "Example.com" assert result3 == snapshot
assert result3.get("data") == {CONF_DOMAIN: "example.com"}
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
assert len(mock_whois_config_flow.mock_calls) == 2 assert len(mock_whois.mock_calls) == 2
@pytest.mark.usefixtures("mock_whois")
async def test_already_configured( async def test_already_configured(
hass: HomeAssistant, hass: HomeAssistant,
mock_setup_entry: AsyncMock, mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_whois_config_flow: MagicMock,
) -> None: ) -> None:
"""Test we abort if already configured.""" """Test we abort if already configured."""
mock_config_entry.add_to_hass(hass) mock_config_entry.add_to_hass(hass)

View File

@ -1,4 +1,6 @@
"""Tests for the diagnostics data provided by the Whois integration.""" """Tests for the diagnostics data provided by the Whois integration."""
from syrupy.assertion import SnapshotAssertion
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -10,15 +12,10 @@ async def test_diagnostics(
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
init_integration: MockConfigEntry, init_integration: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test diagnostics.""" """Test diagnostics."""
assert await get_diagnostics_for_config_entry( assert (
hass, hass_client, init_integration await get_diagnostics_for_config_entry(hass, hass_client, init_integration)
) == { == snapshot
"creation_date": "2019-01-01T00:00:00", )
"expiration_date": "2023-01-01T00:00:00",
"last_updated": "2022-01-01T00:00:00+01:00",
"status": "OK",
"statuses": ["OK"],
"dnssec": True,
}

View File

@ -44,7 +44,6 @@ async def test_error_handling(
hass: HomeAssistant, hass: HomeAssistant,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_whois: MagicMock, mock_whois: MagicMock,
caplog: pytest.LogCaptureFixture,
side_effect: Exception, side_effect: Exception,
) -> None: ) -> None:
"""Test the Whois threw an error.""" """Test the Whois threw an error."""

View File

@ -1,172 +1,67 @@
"""Tests for the sensors provided by the Whois integration.""" """Tests for the sensors provided by the Whois integration."""
from unittest.mock import AsyncMock, MagicMock from unittest.mock import MagicMock
import pytest import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.sensor import SensorDeviceClass from homeassistant.components.whois.const import SCAN_INTERVAL
from homeassistant.components.whois.const import DOMAIN, SCAN_INTERVAL from homeassistant.const import STATE_UNKNOWN
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_FRIENDLY_NAME,
ATTR_ICON,
STATE_UNKNOWN,
EntityCategory,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed from tests.common import async_fire_time_changed
pytestmark = [
pytest.mark.usefixtures("init_integration"),
pytest.mark.freeze_time("2022-01-01 12:00:00", tz_offset=0),
]
@pytest.mark.freeze_time("2022-01-01 12:00:00", tz_offset=0) @pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize(
"entity_id",
(
"sensor.home_assistant_io_admin",
"sensor.home_assistant_io_created",
"sensor.home_assistant_io_days_until_expiration",
"sensor.home_assistant_io_expires",
"sensor.home_assistant_io_last_updated",
"sensor.home_assistant_io_owner",
"sensor.home_assistant_io_registrant",
"sensor.home_assistant_io_registrar",
"sensor.home_assistant_io_reseller",
),
)
async def test_whois_sensors( async def test_whois_sensors(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry_enabled_by_default: AsyncMock, device_registry: dr.DeviceRegistry,
init_integration: MockConfigEntry, entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
entity_id: str,
) -> None: ) -> None:
"""Test the Whois sensors.""" """Test the Whois sensors."""
entity_registry = er.async_get(hass)
device_registry = dr.async_get(hass)
state = hass.states.get("sensor.home_assistant_io_admin") assert (state := hass.states.get(entity_id))
entry = entity_registry.async_get("sensor.home_assistant_io_admin") assert state == snapshot
assert entry
assert state
assert entry.unique_id == "home-assistant.io_admin"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "admin@example.com"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Admin"
assert state.attributes.get(ATTR_ICON) == "mdi:account-star"
assert ATTR_DEVICE_CLASS not in state.attributes
state = hass.states.get("sensor.home_assistant_io_created") assert (entity_entry := entity_registry.async_get(entity_id))
entry = entity_registry.async_get("sensor.home_assistant_io_created") assert entity_entry == snapshot
assert entry
assert state
assert entry.unique_id == "home-assistant.io_creation_date"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "2019-01-01T00:00:00+00:00"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Created"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
assert ATTR_ICON not in state.attributes
state = hass.states.get("sensor.home_assistant_io_days_until_expiration") assert entity_entry.device_id
entry = entity_registry.async_get("sensor.home_assistant_io_days_until_expiration") assert (device_entry := device_registry.async_get(entity_entry.device_id))
assert entry assert device_entry == snapshot
assert state
assert entry.unique_id == "home-assistant.io_days_until_expiration"
assert entry.entity_category is None
assert state.state == "364"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "home-assistant.io Days until expiration"
)
assert state.attributes.get(ATTR_ICON) == "mdi:calendar-clock"
assert ATTR_DEVICE_CLASS not in state.attributes
state = hass.states.get("sensor.home_assistant_io_expires")
entry = entity_registry.async_get("sensor.home_assistant_io_expires")
assert entry
assert state
assert entry.unique_id == "home-assistant.io_expiration_date"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "2023-01-01T00:00:00+00:00"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Expires"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
assert ATTR_ICON not in state.attributes
state = hass.states.get("sensor.home_assistant_io_last_updated")
entry = entity_registry.async_get("sensor.home_assistant_io_last_updated")
assert entry
assert state
assert entry.unique_id == "home-assistant.io_last_updated"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "2021-12-31T23:00:00+00:00"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Last updated"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
assert ATTR_ICON not in state.attributes
state = hass.states.get("sensor.home_assistant_io_owner")
entry = entity_registry.async_get("sensor.home_assistant_io_owner")
assert entry
assert state
assert entry.unique_id == "home-assistant.io_owner"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "owner@example.com"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Owner"
assert state.attributes.get(ATTR_ICON) == "mdi:account"
assert ATTR_DEVICE_CLASS not in state.attributes
state = hass.states.get("sensor.home_assistant_io_registrant")
entry = entity_registry.async_get("sensor.home_assistant_io_registrant")
assert entry
assert state
assert entry.unique_id == "home-assistant.io_registrant"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "registrant@example.com"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Registrant"
assert state.attributes.get(ATTR_ICON) == "mdi:account-edit"
assert ATTR_DEVICE_CLASS not in state.attributes
state = hass.states.get("sensor.home_assistant_io_registrar")
entry = entity_registry.async_get("sensor.home_assistant_io_registrar")
assert entry
assert state
assert entry.unique_id == "home-assistant.io_registrar"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "My Registrar"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Registrar"
assert state.attributes.get(ATTR_ICON) == "mdi:store"
assert ATTR_DEVICE_CLASS not in state.attributes
state = hass.states.get("sensor.home_assistant_io_reseller")
entry = entity_registry.async_get("sensor.home_assistant_io_reseller")
assert entry
assert state
assert entry.unique_id == "home-assistant.io_reseller"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "Top Domains, Low Prices"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Reseller"
assert state.attributes.get(ATTR_ICON) == "mdi:store"
assert ATTR_DEVICE_CLASS not in state.attributes
assert entry.device_id
device_entry = device_registry.async_get(entry.device_id)
assert device_entry
assert device_entry.configuration_url is None
assert device_entry.entry_type == dr.DeviceEntryType.SERVICE
assert device_entry.identifiers == {(DOMAIN, "home-assistant.io")}
assert device_entry.name == "home-assistant.io"
assert device_entry.manufacturer is None
assert device_entry.model is None
assert device_entry.sw_version is None
@pytest.mark.freeze_time("2022-01-01 12:00:00", tz_offset=0)
async def test_whois_sensors_missing_some_attrs( async def test_whois_sensors_missing_some_attrs(
hass: HomeAssistant, hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
entity_registry_enabled_by_default: AsyncMock,
init_integration_missing_some_attrs: MockConfigEntry,
) -> None: ) -> None:
"""Test the Whois sensors with owner and reseller missing.""" """Test the Whois sensors with owner and reseller missing."""
entity_registry = er.async_get(hass) assert (state := hass.states.get("sensor.home_assistant_io_last_updated"))
assert state == snapshot
state = hass.states.get("sensor.home_assistant_io_last_updated") assert (entry := entity_registry.async_get("sensor.home_assistant_io_last_updated"))
entry = entity_registry.async_get("sensor.home_assistant_io_last_updated") assert entry == snapshot
assert entry
assert state
assert entry.unique_id == "home-assistant.io_last_updated"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == "2021-12-31T23:00:00+00:00"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home-assistant.io Last updated"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
assert ATTR_ICON not in state.attributes
assert hass.states.get("sensor.home_assistant_io_owner").state == STATE_UNKNOWN
assert hass.states.get("sensor.home_assistant_io_reseller").state == STATE_UNKNOWN
assert hass.states.get("sensor.home_assistant_io_registrant").state == STATE_UNKNOWN
assert hass.states.get("sensor.home_assistant_io_admin").state == STATE_UNKNOWN
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -180,22 +75,16 @@ async def test_whois_sensors_missing_some_attrs(
), ),
) )
async def test_disabled_by_default_sensors( async def test_disabled_by_default_sensors(
hass: HomeAssistant, hass: HomeAssistant, entity_id: str, entity_registry: er.EntityRegistry
init_integration: MockConfigEntry,
entity_id: str,
) -> None: ) -> None:
"""Test the disabled by default Whois sensors.""" """Test the disabled by default Whois sensors."""
registry = er.async_get(hass) assert hass.states.get(entity_id) is None
assert (entry := entity_registry.async_get(entity_id))
state = hass.states.get(entity_id)
assert state is None
entry = registry.async_get(entity_id)
assert entry
assert entry.disabled assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize( @pytest.mark.parametrize(
"entity_id", "entity_id",
( (
@ -211,11 +100,7 @@ async def test_disabled_by_default_sensors(
), ),
) )
async def test_no_data( async def test_no_data(
hass: HomeAssistant, hass: HomeAssistant, mock_whois: MagicMock, entity_id: str
mock_whois: MagicMock,
entity_registry_enabled_by_default: AsyncMock,
init_integration: MockConfigEntry,
entity_id: str,
) -> None: ) -> None:
"""Test whois sensors become unknown when there is no data provided.""" """Test whois sensors become unknown when there is no data provided."""
mock_whois.return_value = None mock_whois.return_value = None
@ -223,6 +108,5 @@ async def test_no_data(
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(entity_id) assert (state := hass.states.get(entity_id))
assert state
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN