|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -import graphistry, pandas as pd, pytest, unittest |
| 3 | +import base64, graphistry, json, pandas as pd, pytest, unittest |
4 | 4 | try: |
5 | 5 | import mock # type: ignore |
6 | 6 | except ImportError: # pragma: no cover - fallback for stdlib-only envs |
@@ -473,19 +473,41 @@ def test_sso_login_get_sso_token_ok(self, mock_get): |
473 | 473 | mock_switch.assert_called_once_with('mock-org', '123') |
474 | 474 |
|
475 | 475 | @mock.patch('requests.get') |
476 | | - def test_sso_get_token_missing_org_raises(self, mock_get): |
| 476 | + def test_sso_get_token_missing_org_falls_back_to_personal(self, mock_get): |
| 477 | + payload = base64.urlsafe_b64encode( |
| 478 | + json.dumps({'user_id': 1, 'username': 'testuser', 'exp': 9999999999}).encode() |
| 479 | + ).rstrip(b'=').decode() |
| 480 | + fake_token = f"eyJhbGciOiJIUzI1NiJ9.{payload}.fakesig" |
477 | 481 |
|
478 | 482 | mock_resp = self._mock_response( |
479 | 483 | json_data={ |
480 | 484 | 'status': 'OK', |
481 | 485 | 'message': 'State is valid', |
482 | | - 'data': { |
483 | | - 'token': '123', |
484 | | - } |
| 486 | + 'data': {'token': fake_token}, |
485 | 487 | }) |
486 | 488 | mock_get.return_value = mock_resp |
487 | 489 |
|
488 | 490 | au = ArrowUploader() |
| 491 | + with mock.patch.object(ArrowUploader, "_switch_org") as mock_switch: |
| 492 | + au.sso_get_token(state='ignored-valid') |
489 | 493 |
|
490 | | - with pytest.raises(Exception): |
| 494 | + assert au.token == fake_token |
| 495 | + assert au.org_name == 'testuser' |
| 496 | + mock_switch.assert_called_once_with('testuser', fake_token) |
| 497 | + |
| 498 | + @mock.patch('requests.get') |
| 499 | + def test_sso_get_token_missing_org_no_username_in_jwt(self, mock_get): |
| 500 | + mock_resp = self._mock_response( |
| 501 | + json_data={ |
| 502 | + 'status': 'OK', |
| 503 | + 'message': 'State is valid', |
| 504 | + 'data': {'token': '123'}, |
| 505 | + }) |
| 506 | + mock_get.return_value = mock_resp |
| 507 | + |
| 508 | + au = ArrowUploader() |
| 509 | + with mock.patch.object(ArrowUploader, "_switch_org") as mock_switch: |
491 | 510 | au.sso_get_token(state='ignored-valid') |
| 511 | + |
| 512 | + assert au.token == '123' |
| 513 | + mock_switch.assert_not_called() |
0 commit comments