コード例 #1
0
def test_router_timeout(hass, mock_exception_logger):
    """Test the router with a timeout error."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA(
            {
                CONF_PLATFORM: tomato.DOMAIN,
                CONF_HOST: "tomato-router",
                CONF_USERNAME: "******",
                CONF_PASSWORD: "******",
                tomato.CONF_HTTP_ID: "gimmie_good_data",
            }
        )
    }

    with requests_mock.Mocker() as adapter:
        adapter.register_uri(
            "POST",
            "http://tomato-router:80/update.cgi",
            exc=requests.exceptions.Timeout,
        ),
        tomato.get_scanner(hass, config)
    assert mock_exception_logger.call_count == 1
    assert mock_exception_logger.mock_calls[0] == mock.call(
        "Connection to the router timed out"
    )
コード例 #2
0
def test_config_valid_verify_ssl_bool(hass, mock_session_send):
    """Test the setup with a bool for ssl_verify."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_PORT: 1234,
            CONF_SSL: True,
            CONF_VERIFY_SSL: "False",
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: '0987654321'
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "https://tomato-router:1234/update.cgi"
    assert result.req.headers == {
        'Content-Length': '32',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic YmFyOmZvbw=='
    }
    assert "_http_id=0987654321" in result.req.body
    assert "exec=devlist" in result.req.body
    assert mock_session_send.call_count == 1
    assert mock_session_send.mock_calls[0] == \
        mock.call(result.req, timeout=3, verify=False)
コード例 #3
0
def test_config_verify_ssl_but_no_ssl_enabled(hass, mock_session_send):
    """Test the setup with a string with ssl_verify but ssl not enabled."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_PORT: 1234,
            CONF_SSL: False,
            CONF_VERIFY_SSL: "/tmp/tomato.crt",
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: '1234567890'
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "http://tomato-router:1234/update.cgi"
    assert result.req.headers == {
        'Content-Length': '32',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic Zm9vOnBhc3N3b3Jk'
    }
    assert "_http_id=1234567890" in result.req.body
    assert "exec=devlist" in result.req.body
    assert mock_session_send.call_count == 1
    assert mock_session_send.mock_calls[0] == \
        mock.call(result.req, timeout=3)
コード例 #4
0
def test_config_valid_verify_ssl_path(hass, mock_session_send):
    """Test the setup with a string for ssl_verify.

    Representing the absolute path to a CA certificate bundle.
    """
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_PORT: 1234,
            CONF_SSL: True,
            CONF_VERIFY_SSL: "/tmp/tomato.crt",
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: '0987654321'
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "https://tomato-router:1234/update.cgi"
    assert result.req.headers == {
        'Content-Length': '32',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic YmFyOmZvbw=='
    }
    assert "_http_id=0987654321" in result.req.body
    assert "exec=devlist" in result.req.body
    assert mock_session_send.call_count == 1
    assert mock_session_send.mock_calls[0] == \
        mock.call(result.req, timeout=3, verify="/tmp/tomato.crt")
コード例 #5
0
def test_config_verify_ssl_but_no_ssl_enabled(hass, mock_session_send):
    """Test the setup with a string with ssl_verify but ssl not enabled."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: "tomato-router",
            CONF_PORT: 1234,
            CONF_SSL: False,
            CONF_VERIFY_SSL: "/test/tomato.crt",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
            tomato.CONF_HTTP_ID: "1234567890",
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "http://tomato-router:1234/update.cgi"
    assert result.req.headers == {
        "Content-Length": "32",
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": "Basic Zm9vOnBhc3N3b3Jk",
    }
    assert "_http_id=1234567890" in result.req.body
    assert "exec=devlist" in result.req.body
    assert mock_session_send.call_count == 1
    assert mock_session_send.mock_calls[0] == mock.call(result.req, timeout=3)
コード例 #6
0
def test_config_valid_verify_ssl_bool(hass, mock_session_send):
    """Test the setup with a bool for ssl_verify."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: "tomato-router",
            CONF_PORT: 1234,
            CONF_SSL: True,
            CONF_VERIFY_SSL: "False",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
            tomato.CONF_HTTP_ID: "0987654321",
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "https://tomato-router:1234/update.cgi"
    assert result.req.headers == {
        "Content-Length": "32",
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": "Basic YmFyOmZvbw==",
    }
    assert "_http_id=0987654321" in result.req.body
    assert "exec=devlist" in result.req.body
    assert mock_session_send.call_count == 1
    assert mock_session_send.mock_calls[0] == mock.call(result.req,
                                                        timeout=3,
                                                        verify=False)
コード例 #7
0
def test_bad_response(hass, mock_exception_logger):
    """Test the setup with bad response from router."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: 'gimmie_bad_data'
        })
    }

    tomato.get_scanner(hass, config)

    assert mock_exception_logger.call_count == 1
    assert mock_exception_logger.mock_calls[0] == \
        mock.call("Failed to parse response from router")
コード例 #8
0
def test_config_bad_credentials(hass, mock_exception_logger):
    """Test the setup with bad credentials."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: '1234'
        })
    }

    tomato.get_scanner(hass, config)

    assert mock_exception_logger.call_count == 1
    assert mock_exception_logger.mock_calls[0] == \
        mock.call("Failed to authenticate, "
                  "please check your username and password")
コード例 #9
0
def test_config_bad_credentials(hass, mock_exception_logger):
    """Test the setup with bad credentials."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: "tomato-router",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
            tomato.CONF_HTTP_ID: "1234",
        })
    }

    tomato.get_scanner(hass, config)

    assert mock_exception_logger.call_count == 1
    assert mock_exception_logger.mock_calls[0] == mock.call(
        "Failed to authenticate, please check your username and password")
コード例 #10
0
def test_bad_response(hass, mock_exception_logger):
    """Test the setup with bad response from router."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: "tomato-router",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
            tomato.CONF_HTTP_ID: "gimmie_bad_data",
        })
    }

    tomato.get_scanner(hass, config)

    assert mock_exception_logger.call_count == 1
    assert mock_exception_logger.mock_calls[0] == mock.call(
        "Failed to parse response from router")
コード例 #11
0
def test_router_timeout(hass, mock_exception_logger):
    """Test the router with a timeout error."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: 'gimmie_good_data'
        })
    }

    with requests_mock.Mocker() as adapter:
        adapter.register_uri('POST', 'http://tomato-router:80/update.cgi',
                             exc=requests.exceptions.Timeout),
        tomato.get_scanner(hass, config)
    assert mock_exception_logger.call_count == 1
    assert mock_exception_logger.mock_calls[0] == \
        mock.call("Connection to the router timed out")
コード例 #12
0
def test_bad_connection(hass, mock_exception_logger):
    """Test the router with a connection error."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: 'gimmie_good_data'
        })
    }

    with requests_mock.Mocker() as adapter:
        adapter.register_uri('POST',
                             'http://tomato-router:80/update.cgi',
                             exc=requests.exceptions.ConnectionError),
        tomato.get_scanner(hass, config)
    assert mock_exception_logger.call_count == 1
    assert mock_exception_logger.mock_calls[0] == \
        mock.call("Failed to connect to the router "
                  "or invalid http_id supplied")
コード例 #13
0
def test_config_default_nonssl_port(hass, mock_session_send):
    """Test the setup without a default port set without ssl enabled."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: '1234567890'
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "http://tomato-router:80/update.cgi"
コード例 #14
0
def test_config_default_nonssl_port(hass, mock_session_send):
    """Test the setup without a default port set without ssl enabled."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: "tomato-router",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
            tomato.CONF_HTTP_ID: "1234567890",
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "http://tomato-router:80/update.cgi"
コード例 #15
0
def test_scan_devices(hass, mock_exception_logger):
    """Test scanning for new devices."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: 'gimmie_good_data'
        })
    }

    scanner = tomato.get_scanner(hass, config)
    assert scanner.scan_devices() == ['F4:F5:D8:AA:AA:AA', '58:EF:68:00:00:00']
コード例 #16
0
def test_scan_devices(hass, mock_exception_logger):
    """Test scanning for new devices."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: "tomato-router",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
            tomato.CONF_HTTP_ID: "gimmie_good_data",
        })
    }

    scanner = tomato.get_scanner(hass, config)
    assert scanner.scan_devices() == ["F4:F5:D8:AA:AA:AA", "58:EF:68:00:00:00"]
コード例 #17
0
def test_scan_devices(hass, mock_exception_logger):
    """Test scanning for new devices."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: 'gimmie_good_data'
        })
    }

    scanner = tomato.get_scanner(hass, config)
    assert scanner.scan_devices() == ['F4:F5:D8:AA:AA:AA', '58:EF:68:00:00:00']
コード例 #18
0
def test_config_default_ssl_port(hass, mock_session_send):
    """Test the setup without a default port set with ssl enabled."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_SSL: True,
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: '1234567890'
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "https://tomato-router:443/update.cgi"
コード例 #19
0
def test_get_device_name(hass, mock_exception_logger):
    """Test getting device names."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: 'gimmie_good_data'
        })
    }

    scanner = tomato.get_scanner(hass, config)
    assert scanner.get_device_name('F4:F5:D8:AA:AA:AA') == 'chromecast'
    assert scanner.get_device_name('58:EF:68:00:00:00') == 'wemo'
    assert scanner.get_device_name('AA:BB:CC:00:00:00') is None
コード例 #20
0
def test_get_device_name(hass, mock_exception_logger):
    """Test getting device names."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: "tomato-router",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
            tomato.CONF_HTTP_ID: "gimmie_good_data",
        })
    }

    scanner = tomato.get_scanner(hass, config)
    assert scanner.get_device_name("F4:F5:D8:AA:AA:AA") == "chromecast"
    assert scanner.get_device_name("58:EF:68:00:00:00") == "wemo"
    assert scanner.get_device_name("AA:BB:CC:00:00:00") is None
コード例 #21
0
def test_get_device_name(hass, mock_exception_logger):
    """Test getting device names."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: 'gimmie_good_data'
        })
    }

    scanner = tomato.get_scanner(hass, config)
    assert scanner.get_device_name('F4:F5:D8:AA:AA:AA') == 'chromecast'
    assert scanner.get_device_name('58:EF:68:00:00:00') == 'wemo'
    assert scanner.get_device_name('AA:BB:CC:00:00:00') is None
コード例 #22
0
def test_config_missing_optional_params(hass, mock_session_send):
    """Test the setup without optional parameters."""
    config = {
        DOMAIN: tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: '1234567890'
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "http://tomato-router:80/update.cgi"
    assert result.req.headers == {
        'Content-Length': '32',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic Zm9vOnBhc3N3b3Jk'
    }
    assert "_http_id=1234567890" in result.req.body
    assert "exec=devlist" in result.req.body
コード例 #23
0
def test_config_missing_optional_params(hass, mock_session_send):
    """Test the setup without optional parameters."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: "tomato-router",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
            tomato.CONF_HTTP_ID: "1234567890",
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "http://tomato-router:80/update.cgi"
    assert result.req.headers == {
        "Content-Length": "32",
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": "Basic Zm9vOnBhc3N3b3Jk",
    }
    assert "_http_id=1234567890" in result.req.body
    assert "exec=devlist" in result.req.body
コード例 #24
0
def test_config_missing_optional_params(hass, mock_session_send):
    """Test the setup without optional parameters."""
    config = {
        DOMAIN:
        tomato.PLATFORM_SCHEMA({
            CONF_PLATFORM: tomato.DOMAIN,
            CONF_HOST: 'tomato-router',
            CONF_USERNAME: '******',
            CONF_PASSWORD: '******',
            tomato.CONF_HTTP_ID: '1234567890'
        })
    }
    result = tomato.get_scanner(hass, config)
    assert result.req.url == "http://tomato-router:80/update.cgi"
    assert result.req.headers == {
        'Content-Length': '32',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic Zm9vOnBhc3N3b3Jk'
    }
    assert "_http_id=1234567890" in result.req.body
    assert "exec=devlist" in result.req.body