@@ -1161,12 +1161,15 @@ def test_initialize_project_create_project(tmp_dir_cwd: Path, tmp_path: Path, ca
1161
1161
)
1162
1162
1163
1163
logfire .configure (send_to_logfire = True )
1164
+ assert capsys .readouterr ().err == 'Logfire project URL: fake_project_url\n '
1164
1165
1165
- for request in request_mocker .request_history [:- 1 ]:
1166
+ for request in request_mocker .request_history [:5 ]:
1166
1167
assert request .headers ['Authorization' ] == 'fake_user_token'
1168
+ assert len (request_mocker .request_history ) in (5 , 6 )
1167
1169
1168
1170
# we check that fake_token is valid now when we configure the project
1169
1171
wait_for_check_token_thread ()
1172
+ assert len (request_mocker .request_history ) == 6
1170
1173
assert request_mocker .request_history [- 1 ].headers ['Authorization' ] == 'fake_token'
1171
1174
1172
1175
assert request_mocker .request_history [2 ].json () == create_existing_project_request_json
@@ -1203,7 +1206,6 @@ def test_initialize_project_create_project(tmp_dir_cwd: Path, tmp_path: Path, ca
1203
1206
'Project initialized successfully. You will be able to view it at: fake_project_url\n Press Enter to continue' ,
1204
1207
),
1205
1208
]
1206
- assert capsys .readouterr ().err == 'Logfire project URL: fake_project_url\n '
1207
1209
1208
1210
assert json .loads ((tmp_dir_cwd / '.logfire/logfire_credentials.json' ).read_text ()) == {
1209
1211
** create_project_response ['json' ],
@@ -1399,9 +1401,9 @@ def test_send_to_logfire_if_token_present_in_logfire_dir(tmp_path: Path, capsys:
1399
1401
json = {'project_name' : 'myproject' , 'project_url' : 'https://logfire-us.pydantic.dev' },
1400
1402
)
1401
1403
configure (send_to_logfire = 'if-token-present' , data_dir = tmp_path )
1404
+ assert capsys .readouterr ().err == 'Logfire project URL: https://logfire-us.pydantic.dev\n '
1402
1405
wait_for_check_token_thread ()
1403
1406
assert len (request_mocker .request_history ) == 1
1404
- assert capsys .readouterr ().err == 'Logfire project URL: https://logfire-us.pydantic.dev\n '
1405
1407
1406
1408
1407
1409
def test_configure_unknown_token_region (capsys : pytest .CaptureFixture [str ]) -> None :
@@ -1422,7 +1424,51 @@ def test_load_creds_file_invalid_json_content(tmp_path: Path):
1422
1424
creds_file .write_text ('invalid-data' )
1423
1425
1424
1426
with pytest .raises (LogfireConfigError , match = 'Invalid credentials file:' ):
1425
- LogfireCredentials .load_creds_file (creds_dir = tmp_path )
1427
+ logfire .configure (data_dir = tmp_path , send_to_logfire = True )
1428
+
1429
+
1430
+ def test_load_creds_file_invalid_json_content_with_token_present (tmp_path : Path , capsys : pytest .CaptureFixture [str ]):
1431
+ creds_file = tmp_path / 'logfire_credentials.json'
1432
+ creds_file .write_text ('invalid-data' )
1433
+
1434
+ with patch .dict (os .environ , {'LOGFIRE_TOKEN' : 'fake_token' }), requests_mock .Mocker () as request_mocker :
1435
+ request_mocker .get (
1436
+ 'https://logfire-us.pydantic.dev/v1/info' ,
1437
+ json = {'project_name' : 'myproject' , 'project_url' : 'fake_project_url' },
1438
+ )
1439
+ logfire .configure (data_dir = tmp_path , send_to_logfire = True )
1440
+ wait_for_check_token_thread ()
1441
+ assert len (request_mocker .request_history ) == 1
1442
+ assert request_mocker .request_history [0 ].headers ['Authorization' ] == 'fake_token'
1443
+ assert capsys .readouterr ().err == 'Logfire project URL: fake_project_url\n '
1444
+
1445
+
1446
+ def test_load_creds_file_with_token_different_from_env (tmp_path : Path , capsys : pytest .CaptureFixture [str ]):
1447
+ creds_file = tmp_path / 'logfire_credentials.json'
1448
+ creds_file .write_text (
1449
+ """
1450
+ {
1451
+ "token": "foobar",
1452
+ "project_name": "myproject",
1453
+ "project_url": "https://logfire-us.pydantic.dev",
1454
+ "logfire_api_url": "https://logfire-us.pydantic.dev"
1455
+ }
1456
+ """
1457
+ )
1458
+
1459
+ with patch .dict (os .environ , {'LOGFIRE_TOKEN' : 'fake_token' }), requests_mock .Mocker () as request_mocker :
1460
+ request_mocker .get (
1461
+ 'https://logfire-us.pydantic.dev/v1/info' ,
1462
+ json = {'project_name' : 'myproject' , 'project_url' : 'fake_project_url' },
1463
+ )
1464
+ logfire .configure (data_dir = tmp_path , send_to_logfire = True )
1465
+ # not 'foobar' from the creds file. The token in the env var takes precedence.
1466
+ assert logfire .DEFAULT_LOGFIRE_INSTANCE .config .token == 'fake_token'
1467
+
1468
+ wait_for_check_token_thread ()
1469
+ assert len (request_mocker .request_history ) == 1
1470
+ assert request_mocker .request_history [0 ].headers ['Authorization' ] == 'fake_token'
1471
+ assert capsys .readouterr ().err == 'Logfire project URL: fake_project_url\n '
1426
1472
1427
1473
1428
1474
def test_load_creds_file_legacy_key (tmp_path : Path ):
@@ -1447,7 +1493,7 @@ def test_load_creds_file_invalid_key(tmp_path: Path):
1447
1493
creds_file .write_text ('{"test": "test"}' )
1448
1494
1449
1495
with pytest .raises (LogfireConfigError , match = 'Invalid credentials file:' ):
1450
- LogfireCredentials . load_creds_file ( creds_dir = tmp_path )
1496
+ logfire . configure ( data_dir = tmp_path , send_to_logfire = True )
1451
1497
1452
1498
1453
1499
def test_initialize_credentials_from_token_unreachable ():
0 commit comments