Skip to content

Commit

Permalink
Remove WEB3_INFURA_API_KEY environment variable in favor of WEB3_INFU…
Browse files Browse the repository at this point in the history
…RA_PROJECT_ID
  • Loading branch information
kclowes committed Sep 9, 2022
1 parent bf8e046 commit e6197ea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 48 deletions.
1 change: 1 addition & 0 deletions newsfragments/2634.breaking-change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove ``WEB3_INFURA_API_KEY`` environment variable in favor of ``WEB3_INFURA_PROJECT_ID``
61 changes: 18 additions & 43 deletions tests/core/providers/test_auto_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)

# Ugly hack to import infura now that API KEY is required
os.environ["WEB3_INFURA_API_KEY"] = "test"
os.environ["WEB3_INFURA_PROJECT_ID"] = "test"
from web3.auto import ( # noqa E402 isort:skip
infura,
)
Expand All @@ -31,7 +31,6 @@
@pytest.fixture(autouse=True)
def delete_environment_variables(monkeypatch):
monkeypatch.delenv("WEB3_INFURA_PROJECT_ID", raising=False)
monkeypatch.delenv("WEB3_INFURA_API_KEY", raising=False)
monkeypatch.delenv("WEB3_INFURA_API_SECRET", raising=False)
monkeypatch.delenv("WEB3_INFURA_SCHEME", raising=False)

Expand Down Expand Up @@ -69,57 +68,42 @@ def test_get_dev_ipc_path(monkeypatch, tmp_path):
assert path == uri


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_empty_key(monkeypatch, caplog, environ_name):
def test_web3_auto_infura_empty_key(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_SCHEME", "https")
monkeypatch.setenv(environ_name, "")
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "")

with pytest.raises(InfuraKeyNotFound):
importlib.reload(infura)


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_deleted_key(monkeypatch, caplog, environ_name):
def test_web3_auto_infura_deleted_key(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_SCHEME", "https")

monkeypatch.delenv(environ_name, raising=False)
monkeypatch.delenv("WEB3_INFURA_PROJECT_ID", raising=False)

with pytest.raises(InfuraKeyNotFound):
importlib.reload(infura)


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_websocket_empty_key(monkeypatch, caplog, environ_name):
monkeypatch.setenv(environ_name, "")
def test_web3_auto_infura_websocket_empty_key(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "")

with pytest.raises(InfuraKeyNotFound):
importlib.reload(infura)


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_websocket_deleted_key(monkeypatch, caplog, environ_name):
monkeypatch.delenv(environ_name, raising=False)
def test_web3_auto_infura_websocket_deleted_key(monkeypatch):
monkeypatch.delenv("WEB3_INFURA_PROJECT_ID", raising=False)

with pytest.raises(InfuraKeyNotFound):
importlib.reload(infura)


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura(monkeypatch, caplog, environ_name):
def test_web3_auto_infura(monkeypatch, caplog):
monkeypatch.setenv("WEB3_INFURA_SCHEME", "https")
API_KEY = "aoeuhtns"

monkeypatch.setenv(environ_name, API_KEY)
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", API_KEY)

importlib.reload(infura)
assert len(caplog.record_tuples) == 0
Expand All @@ -130,13 +114,10 @@ def test_web3_auto_infura(monkeypatch, caplog, environ_name):
assert getattr(w3.provider, "endpoint_uri") == expected_url


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_websocket_default(monkeypatch, caplog, environ_name):
def test_web3_auto_infura_websocket_default(monkeypatch, caplog):
monkeypatch.setenv("WEB3_INFURA_SCHEME", "wss")
API_KEY = "aoeuhtns"
monkeypatch.setenv(environ_name, API_KEY)
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", API_KEY)
expected_url = f"wss://{infura.INFURA_MAINNET_DOMAIN}/ws/v3/{API_KEY}"

importlib.reload(infura)
Expand All @@ -148,19 +129,16 @@ def test_web3_auto_infura_websocket_default(monkeypatch, caplog, environ_name):


def test_web3_auto_infura_raises_error_with_nonexistent_scheme(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_API_KEY", "test")
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "test")
monkeypatch.setenv("WEB3_INFURA_SCHEME", "not-a-scheme")

error_msg = "Cannot connect to Infura with scheme 'not-a-scheme'"
with pytest.raises(ValidationError, match=error_msg):
importlib.reload(infura)


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_websocket_with_secret(monkeypatch, caplog, environ_name):
monkeypatch.setenv(environ_name, "test")
def test_web3_auto_infura_websocket_with_secret(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "test")
monkeypatch.setenv("WEB3_INFURA_API_SECRET", "secret")

importlib.reload(infura)
Expand All @@ -171,12 +149,9 @@ def test_web3_auto_infura_websocket_with_secret(monkeypatch, caplog, environ_nam
assert getattr(w3.provider, "endpoint_uri") == expected_url


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_with_secret(monkeypatch, caplog, environ_name):
def test_web3_auto_infura_with_secret(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_SCHEME", "https")
monkeypatch.setenv(environ_name, "test")
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "test")
monkeypatch.setenv("WEB3_INFURA_API_SECRET", "secret")

importlib.reload(infura)
Expand Down
7 changes: 2 additions & 5 deletions web3/auto/infura/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@
HTTP_SCHEME = "https"


def load_api_key() -> str:
# in web3py v6 remove outdated WEB3_INFURA_API_KEY
key = os.environ.get(
"WEB3_INFURA_PROJECT_ID", os.environ.get("WEB3_INFURA_API_KEY", "")
)
def load_project_id() -> str:
key = os.environ.get("WEB3_INFURA_PROJECT_ID", "")
if key == "":
raise InfuraKeyNotFound(
"No Infura Project ID found. Please ensure "
Expand Down

0 comments on commit e6197ea

Please sign in to comment.