Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oauth2 scaffold code doesn't have working unit tests setup for a custom component #113223

Closed
jwillemsen opened this issue Mar 13, 2024 · 5 comments

Comments

@jwillemsen
Copy link
Contributor

jwillemsen commented Mar 13, 2024

The problem

I have migrated the custom component daikin_onecta to oauth2 application_credentials and I want to add unit tests now. As part of this effort I used the oauth2 scaffolding script as described at https://developers.home-assistant.io/docs/config_entries_config_flow_handler.

After making some minor changes to the code to get it to compile, I have a problem using the scaffolding generated test, it fails with ValueError: No application_credentials platform for daikin_onecta.

As reproducer I have taken the generated daikin_onecta component and migrated it to its own repo, only made a few minor changes to get it to compile, see https://github.com/jwillemsen/haoauth2_test

When running pytest tests it fails with the error below. For the repository I have setup a github action CI which uses python 3.11 which fails in the same way.

As I am completely lost why this happens creating this issue, shouldn't the scaffold regenerated code be usable for a custom component?


hass = <homeassistant.core.HomeAssistant object at 0x7fcc95e22d70>

    @pytest.fixture
    async def setup_credentials(hass: HomeAssistant) -> None:
        """Fixture to setup credentials."""
        assert await async_setup_component(hass, "application_credentials", {})
>       await async_import_client_credential(
            hass,
            DOMAIN,
            ClientCredential(CLIENT_ID, CLIENT_SECRET),
        )

tests/test_config_flow.py:29: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib/python3.10/site-packages/homeassistant/components/application_credentials/__init__.py:185: in async_import_client_credential
    await storage_collection.async_import_item(item)
/usr/lib/python3.10/site-packages/homeassistant/components/application_credentials/__init__.py:125: in async_import_item
    await self.async_create_item(info)
/usr/lib/python3.10/site-packages/homeassistant/helpers/collection.py:310: in async_create_item
    validated_data = await self._process_create_data(data)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <homeassistant.components.application_credentials.ApplicationCredentialsStorageCollection object at 0x7fcc95e3f100>, data = {'auth_domain': 'daikin_onecta', 'client_id': '1234', 'client_secret': '5678', 'domain': 'daikin_onecta', ...}

    async def _process_create_data(self, data: dict[str, str]) -> dict[str, str]:
        """Validate the config is valid."""
        result = self.CREATE_SCHEMA(data)
        domain = result[CONF_DOMAIN]
        if not await _get_platform(self.hass, domain):
>           raise ValueError(f"No application_credentials platform for {domain}")
E           ValueError: No application_credentials platform for daikin_onecta

/usr/lib/python3.10/site-packages/homeassistant/components/application_credentials/__init__.py:90: ValueError

What version of Home Assistant Core has the issue?

core-2024.3.0

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

oauth2

Link to integration documentation on our website

https://www.home-assistant.io/integrations/application_credentials/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Test session starts (platform: linux, Python 3.10.8, pytest 7.3.1, pytest-sugar 0.9.6)
rootdir: /home/johnny/ACE/github/jwillemsen/haoauth2_test
configfile: setup.cfg
plugins: anyio-4.3.0, syrupy-4.0.2, requests-mock-1.11.0, xdist-3.2.1, unordered-0.5.2, timeout-2.1.0, test-groups-1.0.3, sugar-0.9.6, socket-0.5.1, picked-0.4.6, asyncio-0.20.3, pytest_freezer-0.4.6, cov-3.0.0, respx-0.20.1, aiohttp-1.0.4, homeassistant-custom-component-0.13.45
asyncio: mode=auto


――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ERROR at setup of test_full_flow ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

hass = <homeassistant.core.HomeAssistant object at 0x7ffa78e26e60>

    @pytest.fixture
    async def setup_credentials(hass: HomeAssistant) -> None:
        """Fixture to setup credentials."""
        assert await async_setup_component(hass, "application_credentials", {})
>       await async_import_client_credential(
            hass,
            DOMAIN,
            ClientCredential(CLIENT_ID, CLIENT_SECRET),
        )

tests/test_config_flow.py:29: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib/python3.10/site-packages/homeassistant/components/application_credentials/__init__.py:185: in async_import_client_credential
    await storage_collection.async_import_item(item)
/usr/lib/python3.10/site-packages/homeassistant/components/application_credentials/__init__.py:125: in async_import_item
    await self.async_create_item(info)
/usr/lib/python3.10/site-packages/homeassistant/helpers/collection.py:310: in async_create_item
    validated_data = await self._process_create_data(data)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <homeassistant.components.application_credentials.ApplicationCredentialsStorageCollection object at 0x7ffa78e43160>, data = {'auth_domain': 'daikin_onecta', 'client_id': '1234', 'client_secret': '5678', 'domain': 'daikin_onecta', ...}

    async def _process_create_data(self, data: dict[str, str]) -> dict[str, str]:
        """Validate the config is valid."""
        result = self.CREATE_SCHEMA(data)
        domain = result[CONF_DOMAIN]
        if not await _get_platform(self.hass, domain):
>           raise ValueError(f"No application_credentials platform for {domain}")
E           ValueError: No application_credentials platform for daikin_onecta

/usr/lib/python3.10/site-packages/homeassistant/components/application_credentials/__init__.py:90: ValueError
------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Captured stderr setup -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
INFO:homeassistant.loader:Loaded application_credentials from homeassistant.components.application_credentials
INFO:homeassistant.loader:Loaded auth from homeassistant.components.auth
INFO:homeassistant.loader:Loaded http from homeassistant.components.http
INFO:homeassistant.loader:Loaded websocket_api from homeassistant.components.websocket_api
INFO:homeassistant.setup:Setting up http
INFO:homeassistant.setup:Setup of domain http took 0.0 seconds
INFO:homeassistant.setup:Setting up auth
INFO:homeassistant.setup:Setup of domain auth took 0.0 seconds
INFO:homeassistant.setup:Setting up websocket_api
INFO:homeassistant.setup:Setup of domain websocket_api took 0.0 seconds
INFO:homeassistant.setup:Setting up application_credentials
INFO:homeassistant.setup:Setup of domain application_credentials took 0.0 seconds
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Captured log setup ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
INFO     homeassistant.loader:loader.py:638 Loaded application_credentials from homeassistant.components.application_credentials
INFO     homeassistant.loader:loader.py:638 Loaded auth from homeassistant.components.auth
INFO     homeassistant.loader:loader.py:638 Loaded http from homeassistant.components.http
INFO     homeassistant.loader:loader.py:638 Loaded websocket_api from homeassistant.components.websocket_api
INFO     homeassistant.setup:setup.py:257 Setting up http
INFO     homeassistant.setup:setup.py:308 Setup of domain http took 0.0 seconds
INFO     homeassistant.setup:setup.py:257 Setting up auth
INFO     homeassistant.setup:setup.py:308 Setup of domain auth took 0.0 seconds
INFO     homeassistant.setup:setup.py:257 Setting up websocket_api
INFO     homeassistant.setup:setup.py:308 Setup of domain websocket_api took 0.0 seconds
INFO     homeassistant.setup:setup.py:257 Setting up application_credentials
INFO     homeassistant.setup:setup.py:308 Setup of domain application_credentials took 0.0 seconds
                                                                                                                                                                                                                                                                                                                                                          100% ██████████
=========================================================================================================================================================================== warnings summary ============================================================================================================================================================================

Additional information

See https://github.com/jwillemsen/haoauth2_test for the test HA custom component to reproduce this

@home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (application_credentials) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of application_credentials can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign application_credentials Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


application_credentials documentation
application_credentials source
(message by IssueLinks)

@jwillemsen jwillemsen changed the title oauth2 boiler place code doesn't have working unit tests setup oauth2 scaffold code doesn't have working unit tests setup for a custom component Mar 13, 2024
@jwillemsen
Copy link
Contributor Author

Could this be related to the fact that application_credentials must be in the manifest.json when a component want to use it? Is async_setup_component at that moment the correct way to setup applicaton_credentials?

@jwillemsen
Copy link
Contributor Author

I have been able to validate that the generate code works for a core component test but not for a custom component.

@mvdwetering
Copy link
Contributor

I noticed that you don't have a contest file with the auto_enable_custom_integrations(enable_custom_integrations) fixture like mentioned in the pytest-homeassistant-custom-component readme. Not sure if it fixes this, but might want to check that.

@jwillemsen
Copy link
Contributor Author

Thanks for the hint, tried that, doesn't work. As the code works within HA I am closing the issue here, has to something todo with pytest-homeassistant-custom-component MatthewFlamm/pytest-homeassistant-custom-component#180

@github-actions github-actions bot locked and limited conversation to collaborators Apr 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants