Buckets:
| from __future__ import annotations | |
| from pathlib import Path | |
| from unittest.mock import patch | |
| import inline_snapshot.extra | |
| import pytest | |
| import requests | |
| import requests_mock | |
| from inline_snapshot import snapshot | |
| from logfire._internal.auth import UserToken, UserTokenCollection, request_device_code | |
| from logfire.exceptions import LogfireConfigError | |
| def test_user_token_str(base_url: str, token: str, expected: str) -> None: | |
| user_token = UserToken( | |
| token=token, | |
| base_url=base_url, | |
| expiration='1970-01-01', | |
| ) | |
| assert str(user_token) == expected | |
| def test_get_user_token_explicit_url(default_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(default_credentials) | |
| # https://logfire-us.pydantic.dev is the URL present in the default credentials fixture: | |
| token = token_collection.get_token(base_url='https://logfire-us.pydantic.dev') | |
| assert token.base_url == 'https://logfire-us.pydantic.dev' | |
| with inline_snapshot.extra.raises( | |
| snapshot( | |
| 'LogfireConfigError: No user token was found matching the https://logfire-eu.pydantic.dev Logfire URL. Please run `logfire auth` to authenticate.' | |
| ) | |
| ): | |
| token_collection.get_token(base_url='https://logfire-eu.pydantic.dev') | |
| def test_get_user_token_no_explicit_url(default_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(default_credentials) | |
| token = token_collection.get_token(base_url=None) | |
| # https://logfire-us.pydantic.dev is the URL present in the default credentials fixture: | |
| assert token.base_url == 'https://logfire-us.pydantic.dev' | |
| def test_get_user_token_input_choice(multiple_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(multiple_credentials) | |
| with patch('rich.prompt.IntPrompt.ask', side_effect=[1]): | |
| token = token_collection.get_token(base_url=None) | |
| # https://logfire-us.pydantic.dev is the first URL present in the multiple credentials fixture: | |
| assert token.base_url == 'https://logfire-us.pydantic.dev' | |
| def test_get_user_token_empty_credentials(tmp_path: Path) -> None: | |
| empty_auth_file = tmp_path / 'default.toml' | |
| empty_auth_file.touch() | |
| token_collection = UserTokenCollection(empty_auth_file) | |
| with inline_snapshot.extra.raises( | |
| snapshot("""\ | |
| LogfireConfigError: | |
| Hey, looks like you don't have Pydantic Logfire configured yet. | |
| If you're running this locally, we recommend running `uv run logfire auth`. | |
| Or you could get a write token for a specific project and set the `LOGFIRE_TOKEN` environment variable. | |
| See https://pydantic.dev/docs/logfire/get-started for more details.\ | |
| """) | |
| ): | |
| token_collection.get_token() | |
| def test_get_user_token_expired_credentials(expired_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(expired_credentials) | |
| with inline_snapshot.extra.raises( | |
| snapshot( | |
| 'LogfireConfigError: User token US (https://logfire-us.pydantic.dev) - pylf_v1_us_0kYhc**** is expired. Please run `logfire auth` to authenticate.' | |
| ) | |
| ): | |
| # https://logfire-us.pydantic.dev is the URL present in the expired credentials fixture: | |
| token_collection.get_token(base_url='https://logfire-us.pydantic.dev') | |
| def test_get_user_token_not_authenticated(default_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(default_credentials) | |
| with pytest.raises( | |
| LogfireConfigError, | |
| match=( | |
| 'No user token was found matching the http://localhost:8234 Logfire URL. ' | |
| 'Please run `logfire auth` to authenticate.' | |
| ), | |
| ): | |
| # Use a port that we don't use for local development to reduce conflicts with local configuration | |
| token_collection.get_token(base_url='http://localhost:8234') | |
| def test_logout_single_token(default_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(default_credentials) | |
| removed = token_collection.logout() | |
| assert removed == ['https://logfire-us.pydantic.dev'] | |
| assert default_credentials.read_text() == '' | |
| def test_logout_specific_region(multiple_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(multiple_credentials) | |
| removed = token_collection.logout('https://logfire-us.pydantic.dev') | |
| assert removed == ['https://logfire-us.pydantic.dev'] | |
| content = multiple_credentials.read_text() | |
| assert 'logfire-us' not in content | |
| assert 'logfire-eu' in content | |
| def test_logout_wrong_region(default_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(default_credentials) | |
| with inline_snapshot.extra.raises( | |
| snapshot( | |
| 'LogfireConfigError: No user token was found matching the https://logfire-eu.pydantic.dev Logfire URL. Please run `logfire auth` to authenticate.' | |
| ) | |
| ): | |
| token_collection.logout('https://logfire-eu.pydantic.dev') | |
| def test_logout_not_logged_in(tmp_path: Path) -> None: | |
| empty_auth_file = tmp_path / 'default.toml' | |
| empty_auth_file.touch() | |
| token_collection = UserTokenCollection(empty_auth_file) | |
| with inline_snapshot.extra.raises( | |
| snapshot('LogfireConfigError: You are not logged into Logfire. Please run `logfire auth` to authenticate.') | |
| ): | |
| token_collection.logout() | |
| def test_logout_all_multiple_regions(multiple_credentials: Path) -> None: | |
| token_collection = UserTokenCollection(multiple_credentials) | |
| removed = token_collection.logout() | |
| assert removed == ['https://logfire-us.pydantic.dev', 'https://logfire-eu.pydantic.dev'] | |
| assert multiple_credentials.read_text() == '' | |
| def test_request_device_code_sends_a_timeout() -> None: | |
| """The device code request must carry a timeout, otherwise `logfire auth` hangs on an unresponsive server.""" | |
| with requests_mock.Mocker() as m: | |
| m.post( | |
| 'https://logfire-us.pydantic.dev/v1/device-auth/new/', | |
| json={ | |
| 'device_code': 'device-code', | |
| 'frontend_auth_url': 'https://logfire-us.pydantic.dev/auth/device-code', | |
| }, | |
| ) | |
| result = request_device_code(requests.Session(), 'https://logfire-us.pydantic.dev') | |
| assert result == ('device-code', 'https://logfire-us.pydantic.dev/auth/device-code') | |
| assert m.last_request is not None | |
| # Both halves of the device flow share this timeout. | |
| assert m.last_request.timeout == 15 | |
Xet Storage Details
- Size:
- 7.83 kB
- Xet hash:
- bc7fb5ef52d65438df9bc4337d6367edc7ff1739092bc0d8b780794d1b590262
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.