コード例 #1
0
async def test_full_flow_implementation(hass: HomeAssistant) -> None:
    """Test registering an integration and finishing flow works."""
    with patch(
            "homeassistant.components.azure_devops.async_setup",
            return_value=True
    ) as mock_setup, patch(
            "homeassistant.components.azure_devops.async_setup_entry",
            return_value=True,
    ) as mock_setup_entry, patch(
            "homeassistant.components.azure_devops.config_flow.DevOpsClient.authorized",
            return_value=True,
    ), patch(
            "homeassistant.components.azure_devops.config_flow.DevOpsClient.authorize",
    ), patch(
            "homeassistant.components.azure_devops.config_flow.DevOpsClient.get_project",
            return_value=DevOpsProject("abcd-abcd-abcd-abcd",
                                       FIXTURE_USER_INPUT[CONF_PROJECT]),
    ):
        result = await hass.config_entries.flow.async_init(
            DOMAIN, context={"source": config_entries.SOURCE_USER})

        assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
        assert result["step_id"] == "user"

        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            FIXTURE_USER_INPUT,
        )
        await hass.async_block_till_done()
        assert len(mock_setup.mock_calls) == 1
        assert len(mock_setup_entry.mock_calls) == 1

        assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
        assert (
            result2["title"] ==
            f"{FIXTURE_USER_INPUT[CONF_ORG]}/{FIXTURE_USER_INPUT[CONF_PROJECT]}"
        )
        assert result2["data"][CONF_ORG] == FIXTURE_USER_INPUT[CONF_ORG]
        assert result2["data"][CONF_PROJECT] == FIXTURE_USER_INPUT[
            CONF_PROJECT]
コード例 #2
0
async def test_reauth_flow(hass: HomeAssistant) -> None:
    """Test reauth works."""
    with patch(
        "homeassistant.components.azure_devops.config_flow.DevOpsClient.authorize",
        return_value=False,
    ):
        mock_config = MockConfigEntry(
            domain=DOMAIN, unique_id=UNIQUE_ID, data=FIXTURE_USER_INPUT
        )
        mock_config.add_to_hass(hass)

        result = await hass.config_entries.flow.async_init(
            DOMAIN,
            context={"source": config_entries.SOURCE_REAUTH},
            data=FIXTURE_USER_INPUT,
        )

        assert result["type"] == data_entry_flow.FlowResultType.FORM
        assert result["step_id"] == "reauth"
        assert result["errors"] == {"base": "invalid_auth"}

    with patch(
        "homeassistant.components.azure_devops.config_flow.DevOpsClient.authorize",
    ), patch(
        "homeassistant.components.azure_devops.config_flow.DevOpsClient.authorized",
        return_value=True,
    ), patch(
        "homeassistant.components.azure_devops.config_flow.DevOpsClient.get_project",
        return_value=DevOpsProject(
            "abcd-abcd-abcd-abcd", FIXTURE_USER_INPUT[CONF_PROJECT]
        ),
    ):
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            FIXTURE_REAUTH_INPUT,
        )
        await hass.async_block_till_done()

        assert result2["type"] == data_entry_flow.FlowResultType.ABORT
        assert result2["reason"] == "reauth_successful"
コード例 #3
0
    async def get_project(self, organization: str,
                          project: str) -> DevOpsProject:
        """Get DevOps project."""
        async with aiohttp.ClientSession() as session:
            response: aiohttp.ClientResponse = await self.fetch(
                session,
                f"https://dev.azure.com/{organization}/_apis/projects/{project}",
            )
            if response.status is not 200:
                return None
            json = await response.json()
            if json is None:
                return None

            return DevOpsProject(
                json["id"],
                json["name"],
                json["description"] if "description" in json else None,
                json["url"] if "url" in json else None,
                json["state"] if "state" in json else None,
                json["revision"] if "revision" in json else None,
                json["visibility"] if "visibility" in json else None,
                datetime.strptime(json["lastUpdateTime"],
                                  "%Y-%m-%dT%H:%M:%S.%fZ")
                if "lastUpdateTime" in json else None,
                DevOpsTeam(
                    json["defaultTeam"]["id"],
                    json["defaultTeam"]["name"],
                    json["defaultTeam"]["url"],
                ) if "defaultTeam" in json else None,
                DevOpsLinks(
                    json["_links"]["self"]["href"],
                    json["_links"]["collection"]["href"],
                    json["_links"]["web"]["href"],
                ) if "_links" in json else None,
            )
コード例 #4
0
    async def get_builds(self, organization: str, project: str,
                         parameters: str) -> List[DevOpsBuild]:
        """Get DevOps builds."""
        async with aiohttp.ClientSession() as session:
            response: aiohttp.ClientResponse = await self.fetch(
                session,
                f"https://dev.azure.com/{organization}/{project}/_apis/build/builds{parameters}",
            )
            if response.status is not 200:
                return None
            json = await response.json()
            if json is None:
                return None

            builds = []
            for build in json["value"]:
                builds.append(
                    DevOpsBuild(
                        build["id"],
                        build["buildNumber"]
                        if "buildNumber" in build else None,
                        build["status"] if "status" in build else None,
                        build["result"] if "result" in build else None,
                        build["sourceBranch"]
                        if "sourceBranch" in build else None,
                        build["sourceVersion"]
                        if "sourceVersion" in build else None,
                        build["priority"] if "priority" in build else None,
                        build["reason"] if "reason" in build else None,
                        build["queueTime"] if "queueTime" in build else None,
                        build["startTime"] if "startTime" in build else None,
                        build["startTime"] if "startTime" in build else None,
                        DevOpsBuildDefinition(
                            build["definition"]["id"],
                            build["definition"]["name"],
                            build["definition"]["url"]
                            if "url" in build["definition"] else None,
                            build["definition"]["path"]
                            if "path" in build["definition"] else None,
                            build["definition"]["type"]
                            if "type" in build["definition"] else None,
                            build["definition"]["queueStatus"]
                            if "queueStatus" in build["definition"] else None,
                            build["definition"]["revision"]
                            if "revision" in build["definition"] else None,
                        ) if "definition" in build else None,
                        DevOpsProject(
                            build["project"]["id"],
                            build["project"]["name"],
                            build["project"]["description"]
                            if "description" in build["project"] else None,
                            build["project"]["url"]
                            if "url" in build["project"] else None,
                            build["project"]["state"]
                            if "state" in build["project"] else None,
                            build["project"]["revision"]
                            if "revision" in build["project"] else None,
                            build["project"]["visibility"]
                            if "visibility" in build["project"] else None,
                            datetime.strptime(
                                build["project"]["lastUpdateTime"],
                                "%Y-%m-%dT%H:%M:%S.%fZ",
                            )
                            if "lastUpdateTime" in build["project"] else None,
                        ) if "project" in build else None,
                        DevOpsBuildLinks(
                            build["_links"]["self"]["href"]
                            if "self" in build["_links"] else None,
                            build["_links"]["web"]["href"]
                            if "web" in build["_links"] else None,
                            build["_links"]["sourceVersionDisplayUri"]["href"]
                            if "sourceVersionDisplayUri" in build["_links"]
                            else None,
                            build["_links"]["timeline"]["href"]
                            if "timeline" in build["_links"] else None,
                            build["_links"]["badge"]["href"]
                            if "badge" in build["_links"] else None,
                        ) if "_links" in build else None,
                    ))

            return builds