예제 #1
0
async def test_ip_bans_file_creation(hass, aiohttp_client):
    """Testing if banned IP file created."""
    app = web.Application()
    app['hass'] = hass

    async def unauth_handler(request):
        """Return a mock web response."""
        raise HTTPUnauthorized

    app.router.add_get('/', unauth_handler)
    setup_bans(hass, app, 2)
    mock_real_ip(app)("200.201.202.204")

    with patch('homeassistant.components.http.ban.async_load_ip_bans_config',
               return_value=mock_coro(
                   [IpBan(banned_ip) for banned_ip in BANNED_IPS])):
        client = await aiohttp_client(app)

    m = mock_open()

    with patch('homeassistant.components.http.ban.open', m, create=True):
        resp = await client.get('/')
        assert resp.status == 401
        assert len(app[KEY_BANNED_IPS]) == len(BANNED_IPS)
        assert m.call_count == 0

        resp = await client.get('/')
        assert resp.status == 401
        assert len(app[KEY_BANNED_IPS]) == len(BANNED_IPS) + 1
        m.assert_called_once_with(hass.config.path(IP_BANS_FILE), 'a')

        resp = await client.get('/')
        assert resp.status == 403
        assert m.call_count == 1
예제 #2
0
def mock_api_client(hass, test_client):
    """Start the Hass HTTP component."""
    hass.loop.run_until_complete(async_setup_component(hass, 'api', {
        'http': {
            http.CONF_API_PASSWORD: API_PASSWORD,
        }
    }))
    hass.http.app[KEY_BANNED_IPS] = [IpBan(banned_ip) for banned_ip
                                     in BANNED_IPS]
    return hass.loop.run_until_complete(test_client(hass.http.app))
예제 #3
0
async def test_access_from_banned_ip(hass, aiohttp_client):
    """Test accessing to server from banned IP. Both trusted and not."""
    app = web.Application()
    setup_bans(hass, app, 5)
    set_real_ip = mock_real_ip(app)

    with patch('homeassistant.components.http.ban.load_ip_bans_config',
               return_value=[IpBan(banned_ip) for banned_ip in BANNED_IPS]):
        client = await aiohttp_client(app)

    for remote_addr in BANNED_IPS:
        set_real_ip(remote_addr)
        resp = await client.get('/')
        assert resp.status == 403
예제 #4
0
async def test_ip_bans_file_creation(hass, aiohttp_client):
    """Testing if banned IP file created."""
    notification_calls = async_mock_service(hass, "persistent_notification", "create")

    app = web.Application()
    app["hass"] = hass

    async def unauth_handler(request):
        """Return a mock web response."""
        raise HTTPUnauthorized

    app.router.add_get("/", unauth_handler)
    setup_bans(hass, app, 2)
    mock_real_ip(app)("200.201.202.204")

    with patch(
        "homeassistant.components.http.ban.async_load_ip_bans_config",
        return_value=[IpBan(banned_ip) for banned_ip in BANNED_IPS],
    ):
        client = await aiohttp_client(app)

    m_open = mock_open()

    with patch("homeassistant.components.http.ban.open", m_open, create=True):
        resp = await client.get("/")
        assert resp.status == 401
        assert len(app[KEY_BANNED_IPS]) == len(BANNED_IPS)
        assert m_open.call_count == 0

        resp = await client.get("/")
        assert resp.status == 401
        assert len(app[KEY_BANNED_IPS]) == len(BANNED_IPS) + 1
        m_open.assert_called_once_with(
            hass.config.path(IP_BANS_FILE), "a", encoding="utf8"
        )

        resp = await client.get("/")
        assert resp.status == HTTP_FORBIDDEN
        assert m_open.call_count == 1

        assert len(notification_calls) == 3
        assert (
            notification_calls[0].data["message"]
            == "Login attempt or request with invalid authentication from example.com (200.201.202.204). See the log for details."
        )
예제 #5
0
def setUpModule():
    """Initialize a Home Assistant server."""
    global hass

    hass = get_test_home_assistant()

    bootstrap.setup_component(
        hass, http.DOMAIN, {
            http.DOMAIN: {
                http.CONF_API_PASSWORD: API_PASSWORD,
                http.CONF_SERVER_PORT: SERVER_PORT,
            }
        })

    bootstrap.setup_component(hass, 'api')

    hass.http.app[KEY_BANNED_IPS] = [
        IpBan(banned_ip) for banned_ip in BANNED_IPS
    ]
    hass.start()