async def test_multiple_config_entries_using_saved_credentials_2(
        mock_student, hass):
    """Test a successful config flow for multiple config entries using saved credentials (different situation)."""
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ] + [Student.load(load_fixture("fake_student_2.json", "vulcan"))]
    MockConfigEntry(
        domain=const.DOMAIN,
        unique_id="123456",
        data=json.loads(load_fixture("fake_config_entry_data.json", "vulcan")),
    ).add_to_hass(hass)

    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_USER})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "add_next_config_entry"
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {"use_saved_credentials": True},
    )

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "select_student"
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {"student": "0"},
    )

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result["title"] == "Jan Kowalski"
Beispiel #2
0
async def test_multiple_config_entries_using_saved_credentials_4(
        mock_student, hass):
    """Test a successful config flow for multiple config entries using saved credentials (different situation)."""
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ] + [Student.load(load_fixture("fake_student_2.json", "vulcan"))]
    MockConfigEntry(
        entry_id="456",
        domain=const.DOMAIN,
        unique_id="234567",
        data=json.loads(load_fixture("fake_config_entry_data.json", "vulcan"))
        | {
            "student_id": "456"
        },
    ).add_to_hass(hass)
    MockConfigEntry(
        entry_id="123",
        domain=const.DOMAIN,
        unique_id="123456",
        data=json.loads(load_fixture("fake_config_entry_data.json", "vulcan")),
    ).add_to_hass(hass)

    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_USER})

    assert result["type"] == data_entry_flow.FlowResultType.FORM
    assert result["step_id"] == "add_next_config_entry"
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {"use_saved_credentials": True},
    )

    assert result["type"] == data_entry_flow.FlowResultType.FORM
    assert result["step_id"] == "select_saved_credentials"
    assert result["errors"] is None

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {"credentials": "123"},
    )

    assert result["type"] == data_entry_flow.FlowResultType.FORM
    assert result["step_id"] == "select_student"
    assert result["errors"] == {}

    with patch(
            "homeassistant.components.vulcan.async_setup_entry",
            return_value=True,
    ) as mock_setup_entry:
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {"student": "0"},
        )

    assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
    assert result["title"] == "Jan Kowalski"
    assert len(mock_setup_entry.mock_calls) == 3
async def test_options_flow(mock_student, hass):
    """Test config flow options."""
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ]
    config_entry = MockConfigEntry(
        domain=const.DOMAIN,
        unique_id="0",
        data=json.loads(load_fixture("fake_config_entry_data.json", "vulcan")),
    )
    config_entry.add_to_hass(hass)

    assert await hass.config_entries.async_setup(config_entry.entry_id)
    await hass.async_block_till_done()

    result = await hass.config_entries.options.async_init(config_entry.entry_id
                                                          )

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

    result = await hass.config_entries.options.async_configure(
        result["flow_id"], user_input={CONF_SCAN_INTERVAL: 2137})

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert config_entry.options == {CONF_SCAN_INTERVAL: 2137}
async def test_student_already_exists(mock_account, mock_keystore,
                                      mock_student, hass):
    """Test config entry when student's entry already exists."""
    mock_keystore.return_value = fake_keystore
    mock_account.return_value = fake_account
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ]
    MockConfigEntry(
        domain=const.DOMAIN,
        unique_id="0",
        data=json.loads(load_fixture("fake_config_entry_data.json", "vulcan"))
        | {
            "student_id": "0"
        },
    ).add_to_hass(hass)

    await register.register(hass, "token", "region", "000000")

    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_USER})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "add_next_config_entry"
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {"use_saved_credentials": True},
    )

    assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
    assert result["reason"] == "all_student_already_configured"
async def test_config_flow_auth_success(mock_keystore, mock_account,
                                        mock_student, hass):
    """Test a successful config flow initialized by the user."""
    mock_keystore.return_value = fake_keystore
    mock_account.return_value = fake_account
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ]
    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_USER})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "auth"
    assert result["errors"] is None
    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {
            CONF_TOKEN: "token",
            CONF_REGION: "region",
            CONF_PIN: "000000"
        },
    )

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result["title"] == "Jan Kowalski"
async def test_config_flow_reauth_success(mock_account, mock_keystore,
                                          mock_student, hass):
    """Test a successful config flow reauth."""
    mock_keystore.return_value = fake_keystore
    mock_account.return_value = fake_account
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ]
    MockConfigEntry(
        domain=const.DOMAIN,
        unique_id="0",
        data={
            "student_id": "0",
            "login": "******"
        },
    ).add_to_hass(hass)
    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_REAUTH})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "reauth"
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {
            CONF_TOKEN: "token",
            CONF_REGION: "region",
            CONF_PIN: "000000"
        },
    )

    assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
    assert result["reason"] == "reauth_successful"
Beispiel #7
0
async def test_config_flow_auth_success(mock_keystore, mock_account,
                                        mock_student, hass):
    """Test a successful config flow initialized by the user."""
    mock_keystore.return_value = fake_keystore
    mock_account.return_value = fake_account
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ]
    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_USER})

    assert result["type"] == data_entry_flow.FlowResultType.FORM
    assert result["step_id"] == "auth"
    assert result["errors"] is None

    with patch(
            "homeassistant.components.vulcan.async_setup_entry",
            return_value=True,
    ) as mock_setup_entry:
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                CONF_TOKEN: "token",
                CONF_REGION: "region",
                CONF_PIN: "000000"
            },
        )
        await hass.async_block_till_done()

    assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
    assert result["title"] == "Jan Kowalski"
    assert len(mock_setup_entry.mock_calls) == 1
Beispiel #8
0
async def test_config_flow_reauth_without_matching_entries(
        mock_account, mock_keystore, mock_student, hass):
    """Test a aborted config flow reauth caused by leak of matching entries."""
    mock_keystore.return_value = fake_keystore
    mock_account.return_value = fake_account
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ]
    MockConfigEntry(
        domain=const.DOMAIN,
        unique_id="0",
        data={
            "student_id": "1"
        },
    ).add_to_hass(hass)
    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_REAUTH})

    assert result["type"] == data_entry_flow.FlowResultType.FORM
    assert result["step_id"] == "reauth_confirm"
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {
            CONF_TOKEN: "token",
            CONF_REGION: "region",
            CONF_PIN: "000000"
        },
    )

    assert result["type"] == data_entry_flow.FlowResultType.ABORT
    assert result["reason"] == "no_matching_entries"
Beispiel #9
0
async def test_multiple_config_entries(mock_account, mock_keystore,
                                       mock_student, hass):
    """Test a successful config flow for multiple config entries."""
    mock_keystore.return_value = fake_keystore
    mock_account.return_value = fake_account
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ]
    MockConfigEntry(
        domain=const.DOMAIN,
        unique_id="123456",
        data=json.loads(load_fixture("fake_config_entry_data.json", "vulcan")),
    ).add_to_hass(hass)
    await register.register(hass, "token", "region", "000000")
    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_USER})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "add_next_config_entry"
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {"use_saved_credentials": False},
    )

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "auth"
    assert result["errors"] is None

    with patch(
            "homeassistant.components.vulcan.async_setup_entry",
            return_value=True,
    ) as mock_setup_entry:
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                CONF_TOKEN: "token",
                CONF_REGION: "region",
                CONF_PIN: "000000"
            },
        )

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result["title"] == "Jan Kowalski"
    assert len(mock_setup_entry.mock_calls) == 2
Beispiel #10
0
async def test_config_flow_auth_success_with_multiple_students(
        mock_keystore, mock_account, mock_student, hass):
    """Test a successful config flow with multiple students."""
    mock_keystore.return_value = fake_keystore
    mock_account.return_value = fake_account
    mock_student.return_value = [
        Student.load(student)
        for student in [load_fixture("fake_student_1.json", "vulcan")] +
        [load_fixture("fake_student_2.json", "vulcan")]
    ]
    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_USER})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "auth"
    assert result["errors"] is None

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {
            CONF_TOKEN: "token",
            CONF_REGION: "region",
            CONF_PIN: "000000"
        },
    )

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "select_student"
    assert result["errors"] == {}

    with patch(
            "homeassistant.components.vulcan.async_setup_entry",
            return_value=True,
    ) as mock_setup_entry:
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {"student": "0"},
        )

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result["title"] == "Jan Kowalski"
    assert len(mock_setup_entry.mock_calls) == 1
Beispiel #11
0
async def test_config_flow_reauth_success(mock_account, mock_keystore,
                                          mock_student, hass):
    """Test a successful config flow reauth."""
    mock_keystore.return_value = fake_keystore
    mock_account.return_value = fake_account
    mock_student.return_value = [
        Student.load(load_fixture("fake_student_1.json", "vulcan"))
    ]
    MockConfigEntry(
        domain=const.DOMAIN,
        unique_id="0",
        data={
            "student_id": "0"
        },
    ).add_to_hass(hass)
    result = await hass.config_entries.flow.async_init(
        const.DOMAIN, context={"source": config_entries.SOURCE_REAUTH})

    assert result["type"] == data_entry_flow.FlowResultType.FORM
    assert result["step_id"] == "reauth_confirm"
    assert result["errors"] == {}

    with patch(
            "homeassistant.components.vulcan.async_setup_entry",
            return_value=True,
    ) as mock_setup_entry:
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                CONF_TOKEN: "token",
                CONF_REGION: "region",
                CONF_PIN: "000000"
            },
        )

    assert result["type"] == data_entry_flow.FlowResultType.ABORT
    assert result["reason"] == "reauth_successful"
    assert len(mock_setup_entry.mock_calls) == 1