async def test_base_url(): async with GitHubDeviceAPI(client_id=CLIENT_ID) as device: assert device._client._base_request_data.base_url == "https://github.com" async with GitHubDeviceAPI( client_id=CLIENT_ID, **{GitHubClientKwarg.BASE_URL: "https://example.com"} ) as device: assert device._client._base_request_data.base_url == "https://example.com"
async def test_session_creation_with_enter(): async with GitHubDeviceAPI(client_id=CLIENT_ID) as device: assert device._session assert isinstance(device._session, aiohttp.ClientSession) assert not device._session.closed assert device._session.closed
async def test_session_creation(): device = GitHubDeviceAPI(client_id=CLIENT_ID) assert device._session assert isinstance(device._session, aiohttp.ClientSession) assert not device._session.closed await device.close_session() assert device._session.closed
async def github_device_api(client_session): """Fixture to provide a GitHub Devlice object.""" async with GitHubDeviceAPI( CLIENT_ID, session=client_session, ) as github_device_obj: github_device_obj._interval = 1 github_device_obj._expires = datetime.timestamp(datetime.now()) + 900 yield github_device_obj
async def async_step_device( self, user_input: dict[str, Any] | None = None, ) -> FlowResult: """Handle device steps.""" async def _wait_for_login() -> None: # mypy is not aware that we can't get here without having these set already assert self._device is not None assert self._login_device is not None try: response = await self._device.activation( device_code=self._login_device.device_code ) self._login = response.data finally: self.hass.async_create_task( self.hass.config_entries.flow.async_configure(flow_id=self.flow_id) ) if not self._device: self._device = GitHubDeviceAPI( client_id=CLIENT_ID, session=async_get_clientsession(self.hass), **{"client_name": SERVER_SOFTWARE}, ) try: response = await self._device.register() self._login_device = response.data except GitHubException as exception: LOGGER.exception(exception) return self.async_abort(reason="could_not_register") if not self.login_task: self.login_task = self.hass.async_create_task(_wait_for_login()) return self.async_show_progress( step_id="device", progress_action="wait_for_device", description_placeholders={ "url": OAUTH_USER_LOGIN, "code": self._login_device.user_code, }, ) try: await self.login_task except GitHubException as exception: LOGGER.exception(exception) return self.async_show_progress_done(next_step_id="could_not_register") return self.async_show_progress_done(next_step_id="repositories")
async def async_step_device(self, _user_input): """Handle device steps""" async def _wait_for_activation(_=None): if self._login_device is None or self._login_device.expires_in is None: async_call_later(self.hass, 1, _wait_for_activation) return response = await self.device.activation(device_code=self._login_device.device_code) self.activation = response.data self.hass.async_create_task( self.hass.config_entries.flow.async_configure(flow_id=self.flow_id) ) if not self.activation: integration = await async_get_integration(self.hass, DOMAIN) if not self.device: self.device = GitHubDeviceAPI( client_id=CLIENT_ID, session=aiohttp_client.async_get_clientsession(self.hass), **{"client_name": f"HACS/{integration.version}"}, ) async_call_later(self.hass, 1, _wait_for_activation) try: response = await self.device.register() self._login_device = response.data return self.async_show_progress( step_id="device", progress_action="wait_for_device", description_placeholders={ "url": OAUTH_USER_LOGIN, "code": self._login_device.user_code, }, ) except GitHubException as exception: self.hacs.log.error(exception) return self.async_abort(reason="github") return self.async_show_progress_done(next_step_id="device_done")
async def test_user_too_slow(github_device_api: GitHubDeviceAPI): github_device_api._expires = datetime.timestamp(datetime.now()) - 1 with pytest.raises(GitHubException, match="User took too long to enter key"): await github_device_api.activation(device_code=DEVICE_CODE)
async def test_missing_expiration(github_device_api: GitHubDeviceAPI): github_device_api._expires = None with pytest.raises(GitHubException, match="Expiration has passed, re-run the registration"): await github_device_api.activation(device_code=DEVICE_CODE)
async def test_session_pass(): async with aiohttp.ClientSession() as session: device = GitHubDeviceAPI(client_id=CLIENT_ID, session=session) assert device._session is session