def test_the_instance_should_be_able_to_unregister_multiple_realms():
    driver = RespectfulWebdriver(webdriver=webdriver)

    realm_tuples = [
        ["TEST123", 100, 300],
        ["TEST234", 200, 600],
        ["TEST345", 300, 900],
    ]

    driver.register_realms(realm_tuples)

    driver.get("http://google.com", realms=["TEST123", "TEST234", "TEST345"])

    driver.unregister_realms(["TEST123", "TEST234", "TEST345"])

    assert driver.redis.get(driver._realm_redis_key("TEST123")) is None
    assert not driver.redis.sismember("%s:REALMS" % driver.redis_prefix,
                                      "TEST123")
    assert not len(
        driver.redis.keys("%s:REQUESTS:%s:*" %
                          (driver.redis_prefix, "TEST123")))

    assert driver.redis.get(driver._realm_redis_key("TEST234")) is None
    assert not driver.redis.sismember("%s:REALMS" % driver.redis_prefix,
                                      "TEST234")
    assert not len(
        driver.redis.keys("%s:REQUESTS:%s:*" %
                          (driver.redis_prefix, "TEST234")))

    assert driver.redis.get(driver._realm_redis_key("TEST345")) is None
    assert not driver.redis.sismember("%s:REALMS" % driver.redis_prefix,
                                      "TEST345")
    assert not len(
        driver.redis.keys("%s:REQUESTS:%s:*" %
                          (driver.redis_prefix, "TEST345")))
def test_the_instance_should_return_a_rate_limit_exception_if_the_request_is_not_allowed_on_a_registered_realm(
):
    driver = RespectfulWebdriver(webdriver=webdriver)

    driver.register_realm("TEST123", max_requests=0, timespan=5)

    with pytest.raises(SeleniumRespectfulRateLimitedError):
        driver.get("http://google.com", realms=["TEST123"])

    driver.unregister_realm("TEST123")
def test_the_instance_should_perform_the_request_if_it_is_allowed_to_on_a_registered_realm(
):
    driver = RespectfulWebdriver(webdriver=webdriver)

    driver.register_realm("TEST123", max_requests=1000, timespan=5)

    driver.get("http://google.com", realms=["TEST123"])
    assert driver.find_element_by_tag_name("body") is not None

    driver.unregister_realm("TEST123")
def test_the_instance_should_be_able_to_wait_for_a_request_to_be_allowed_on_a_registered_realm(
):
    driver = RespectfulWebdriver(webdriver=webdriver)
    driver.config["safety_threshold"] = 0

    driver.register_realm("TEST123", max_requests=1, timespan=2)

    driver.get("http://google.com", realms=["TEST123"], wait=True)
    driver.get("http://google.com", realms=["TEST123"], wait=True)
    driver.get("http://google.com", realms=["TEST123"], wait=True)

    driver.unregister_realm("TEST123")
def test_the_safety_threshold_configuration_value_should_have_the_expected_effect(
):
    driver = RespectfulWebdriver(webdriver=webdriver)
    driver.config["safety_threshold"] = 10

    driver.register_realm("TEST123", max_requests=11, timespan=300)

    driver.get("http://google.com", realms=["TEST123"])

    with pytest.raises(SeleniumRespectfulRateLimitedError):
        driver.get("http://google.com", realms=["TEST123"])

    driver.unregister_realm("TEST123")
def test_the_instance_should_be_able_to_unregister_a_realm():
    driver = RespectfulWebdriver(webdriver=webdriver)

    driver.register_realm("TEST123", max_requests=100, timespan=300)

    driver.get("http://google.com", realms=["TEST123"])

    driver.unregister_realm("TEST123")

    assert driver.redis.get(driver._realm_redis_key("TEST123")) is None
    assert not driver.redis.sismember("%s:REALMS" % driver.redis_prefix,
                                      "TEST123")
    assert not len(
        driver.redis.keys("%s:REQUESTS:%s:*" %
                          (driver.redis_prefix, "TEST123")))
def test_the_instance_should_not_allow_a_request_to_be_made_on_an_unregistered_realm(
):
    driver = RespectfulWebdriver(webdriver=webdriver)

    with pytest.raises(SeleniumRespectfulError):
        driver.get("http://google.com", realms=["TEST123"])