Ejemplo n.º 1
0
def test__http_request_methods(mock_requests):

    mocked_response_get_post = MockRequestsResponse(
        {"key": "value"}, HTTP_CODE.OK, {"Content-Type": "application/json"}
    )
    mocked_response_options = MockRequestsResponse(
        None, HTTP_CODE.OK, {"Content-Type": "somethingelse"}
    )

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(return_value=mocked_response_get_post),
        post=mock.MagicMock(return_value=mocked_response_get_post),
        options=mock.MagicMock(return_value=mocked_response_options),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")

    assert test_ksh._http_request("GET", "http://fake=url") == {"key": "value"}

    assert test_ksh._http_request("POST", "http://fake=url") == {
        "key": "value"
    }

    assert test_ksh._http_request("OPTIONS", "http://fake=url")

    with pytest.raises(TypeError) as exception_msg:
        test_ksh._http_request("INVALID", "http://fake=url")
        assert "Invalid Method INVALID" in str(exception_msg.value)
Ejemplo n.º 2
0
def test__http_request_session_exception(mock_requests):

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(side_effect=requests.exceptions.ConnectionError),
        post=mock.MagicMock(side_effect=requests.exceptions.ConnectionError),
        options=mock.MagicMock(
            side_effect=requests.exceptions.ConnectionError
        ),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")

    with pytest.raises(TypeError):
        test_ksh._http_request("GET", "http://fake=url")
Ejemplo n.º 3
0
def test__http_request_200_withou_body_methods_get_post(mock_requests):

    no_response = None
    mocked_response_get_post = MockRequestsResponse(
        no_response, HTTP_CODE.OK, {"Content-Type": "NOT_OK"}
    )

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(return_value=mocked_response_get_post),
        post=mock.MagicMock(return_value=mocked_response_get_post),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")

    with pytest.raises(TypeError) as exception_msg:
        assert test_ksh._http_request("GET", "http://fake=url")
        assert test_ksh.is_connected is False
        assert "Unexpected response format" in str(exception_msg.value)
Ejemplo n.º 4
0
def test__http_request_not_200_401_http_code(mock_requests):

    mocked_response_get_post = MockRequestsResponse(
        None, HTTP_CODE.INTERNAL_SERVER_ERROR, "None"
    )

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(return_value=mocked_response_get_post),
        post=mock.MagicMock(return_value=mocked_response_get_post),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")
    test_ksh.is_connected = True

    with pytest.raises(ConnectionError) as exception_msg:
        assert test_ksh._http_request("GET", "http://fake=url")
        assert test_ksh.is_connected is False
        assert "Unexpected HTTP CODE error" in str(exception_msg.value)
Ejemplo n.º 5
0
def test__http_request_401_access_denied_is_connected(mock_requests):

    access_denied_response = {"msg": "Access Denied"}
    mocked_response_get_post = MockRequestsResponse(
        access_denied_response,
        HTTP_CODE.UNAUTHORIZED,
        {"Content-Type": "application/json"},
    )

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(return_value=mocked_response_get_post),
        post=mock.MagicMock(return_value=mocked_response_get_post),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")
    test_ksh.is_connected = True

    assert test_ksh._http_request("GET", "http://fake=url")
    assert test_ksh.is_connected is False
Ejemplo n.º 6
0
def test__http_request_401_unexpected(mock_requests):

    unexpected_response = {"not_mapped": "Strange Error"}
    mocked_response_get_post = MockRequestsResponse(
        unexpected_response,
        HTTP_CODE.UNAUTHORIZED,
        {"Content-Type": "application/json"},
    )

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(return_value=mocked_response_get_post),
        post=mock.MagicMock(return_value=mocked_response_get_post),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")
    test_ksh.is_connected = True

    with pytest.raises(ConnectionError) as exception_msg:
        assert test_ksh._http_request("GET", "http://fake=url")
        assert test_ksh.is_connected is False
        assert "Unexpected 401 error" in str(exception_msg.value)
Ejemplo n.º 7
0
def test__http_request_401_auth_error_is_connected(mock_requests):

    invalid_grant_response = {
        "error": {"reason": "authError", "message": "auth error"}
    }
    mocked_response_get_post = MockRequestsResponse(
        invalid_grant_response,
        HTTP_CODE.UNAUTHORIZED,
        {"Content-Type": "application/json"},
    )

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(return_value=mocked_response_get_post),
        post=mock.MagicMock(return_value=mocked_response_get_post),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")
    test_ksh.is_connected = True

    assert test_ksh._http_request("GET", "http://fake=url")
    assert test_ksh.is_connected is False
Ejemplo n.º 8
0
def test__http_request_401_invalid_grant(mock_requests):

    invalid_grant_response = {
        "error": "invalid_grant",
        "error_description": " ... invalid grant ...",
    }
    mocked_response_get_post = MockRequestsResponse(
        invalid_grant_response,
        HTTP_CODE.UNAUTHORIZED,
        {"Content-Type": "application/json"},
    )

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(return_value=mocked_response_get_post),
        post=mock.MagicMock(return_value=mocked_response_get_post),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")

    with pytest.raises(ConnectionError) as exception_msg:
        assert test_ksh._http_request("GET", "http://fake=url")
        assert test_ksh.is_connected is False
        assert "invalid grant" in str(exception_msg.value)
Ejemplo n.º 9
0
def test__http_request_401_auth_error_is_connected_false(mock_requests):

    auth_error_response = {
        "error": {"reason": "authError", "message": "auth error"}
    }
    mocked_response_get_post = MockRequestsResponse(
        auth_error_response,
        HTTP_CODE.UNAUTHORIZED,
        {"Content-Type": "application/json"},
    )

    mock_requests.Session.return_value = mock.MagicMock(
        get=mock.MagicMock(return_value=mocked_response_get_post),
        post=mock.MagicMock(return_value=mocked_response_get_post),
    )

    test_ksh = KodakSmartHome("fake_user", "fake_pass")
    test_ksh.is_connected = False

    with pytest.raises(ConnectionError) as exception_msg:
        assert test_ksh._http_request("GET", "http://fake=url")
        assert test_ksh.is_connected is False
        assert "auth error" in str(exception_msg.value)