def test_auth_logout(mock_write, mock_cognito):
    """Test renewing an access token."""
    auth = auth_api.Auth(None, mock_cognito)
    auth.account = MagicMock()
    auth.logout()
    assert auth.account is None
    assert len(mock_write.mock_calls) == 1
def test_auth_validate_auth_token_refresh_needed_fails(mock_cognito):
    """Test validate authentication with refresh needed which gets 401."""
    mock_cognito.get_user.side_effect = aws_error('NotAuthorizedException')
    mock_cognito.renew_access_token.side_effect = \
        aws_error('NotAuthorizedException')

    auth = auth_api.Auth(None, mock_cognito)
    assert auth.validate_auth() is False
def test_auth_logout_fails(mock_write, mock_cognito):
    """Test error while logging out."""
    mock_cognito.logout.side_effect = aws_error('SomeError')
    auth = auth_api.Auth(None, mock_cognito)
    auth.account = MagicMock()
    with pytest.raises(auth_api.CloudError):
        auth.logout()
    assert auth.account is not None
    assert len(mock_write.mock_calls) == 0
def test_auth_login_user_not_found(mock_cognito, mock_write):
    """Test trying to login with invalid credentials."""
    mock_cognito.authenticate.side_effect = aws_error('UserNotFoundException')
    auth = auth_api.Auth(None, None)
    with pytest.raises(auth_api.UserNotFound):
        auth.login('user', 'pass')

    assert not auth.is_logged_in
    assert len(mock_cognito.get_user.mock_calls) == 0
    assert len(mock_write.mock_calls) == 0
def test_auth_login_user_not_confirmed(mock_cognito, mock_write):
    """Test trying to login without confirming account."""
    mock_cognito.authenticate.side_effect = \
        aws_error('UserNotConfirmedException')
    auth = auth_api.Auth(None, None)
    with pytest.raises(auth_api.UserNotConfirmed):
        auth.login('user', 'pass')

    assert not auth.is_logged_in
    assert len(mock_cognito.get_user.mock_calls) == 0
    assert len(mock_write.mock_calls) == 0
def test_auth_validate_auth_token_refresh_needed_succeeds(
        mock_write, mock_cognito):
    """Test validate authentication with refresh."""
    mock_cognito.get_user.side_effect = [
        aws_error('NotAuthorizedException'),
        MagicMock(email='*****@*****.**')
    ]

    auth = auth_api.Auth(None, mock_cognito)
    assert auth.validate_auth() is True
    assert len(mock_write.mock_calls) == 1
def test_auth_login(cloud_hass, mock_cognito, mock_write):
    """Test trying to login without confirming account."""
    mock_cognito.get_user.return_value = \
        MagicMock(email='*****@*****.**')
    auth = auth_api.Auth(cloud_hass, None)
    auth.login('user', 'pass')
    assert auth.is_logged_in
    assert len(mock_cognito.authenticate.mock_calls) == 1
    assert len(mock_write.mock_calls) == 1
    result_hass, result_auth = mock_write.mock_calls[0][1]
    assert result_hass is cloud_hass
    assert result_auth is auth
def test_auth_validate_auth_verification_fails(mock_cognito):
    """Test validate authentication with verify request failing."""
    mock_cognito.get_user.side_effect = aws_error('UserNotFoundException')

    auth = auth_api.Auth(None, mock_cognito)
    assert auth.validate_auth() is False
def test_auth_properties():
    """Test Auth class properties."""
    auth = auth_api.Auth(None, None)
    assert not auth.is_logged_in
    auth.account = {}
    assert auth.is_logged_in
def test_auth_renew_access_token_fails(mock_write, mock_cognito):
    """Test failing to renew an access token."""
    mock_cognito.renew_access_token.side_effect = aws_error('SomeError')
    auth = auth_api.Auth(None, mock_cognito)
    assert not auth.renew_access_token()
    assert len(mock_write.mock_calls) == 0
def test_auth_renew_access_token(mock_write, mock_cognito):
    """Test renewing an access token."""
    auth = auth_api.Auth(None, mock_cognito)
    assert auth.renew_access_token()
    assert len(mock_write.mock_calls) == 1