def test_client_raises_if_login_fails(monkeypatch): post = MagicMock(return_value=MagicMock(ok=False)) monkeypatch.setattr("requests.post", post) with set_temporary_config({"cloud.graphql": "http://my-cloud.foo"}): client = Client() with pytest.raises(AuthorizationError): client.login("*****@*****.**", "1234") assert post.called assert post.call_args[0][0] == "http://my-cloud.foo/login_email"
def test_logout_removes_token(monkeypatch): with tempfile.NamedTemporaryFile(delete=False) as f: monkeypatch.setattr("prefect.client.Client.local_token_path", f.name) client = Client() client.login(api_token="a") assert f.read() == b"a" client.logout() assert not os.path.exists(f.name)
def test_login_creates_directories(monkeypatch): with tempfile.TemporaryDirectory() as tmp: f_path = os.path.join(tmp, "a", "b", "c") monkeypatch.setattr("prefect.client.Client.local_token_path", f_path) client = Client() client.login(api_token="a") with open(f_path) as f: assert f.read() == "a"
def test_login_writes_token(monkeypatch): with tempfile.NamedTemporaryFile() as f: monkeypatch.setattr("prefect.client.Client.local_token_path", f.name) client = Client() client.login(api_token="a") assert f.read() == b"a" f.seek(0) client.login(api_token="b") assert f.read() == b"b"
def test_client_logs_out_and_deletes_auth_token(monkeypatch): post = MagicMock(return_value=MagicMock( ok=True, json=MagicMock(return_value=dict(token="secrettoken")))) monkeypatch.setattr("requests.post", post) with set_temporary_config({"cloud.graphql": "http://my-cloud.foo"}): client = Client() client.login("*****@*****.**", "1234") token_path = os.path.expanduser("~/.prefect/.credentials/auth_token") assert os.path.exists(token_path) with open(token_path, "r") as f: assert f.read() == "secrettoken" client.logout() assert not os.path.exists(token_path)