|
7 | 7 | import requests |
8 | 8 |
|
9 | 9 | from src.iracingdataapi.client import irDataClient |
| 10 | +from src.iracingdataapi.exceptions import AccessTokenInvalid |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class TestIrDataClient(unittest.TestCase): |
13 | 14 | def setUp(self): |
14 | 15 | self.client = irDataClient(username="test_user", password="test_password") |
| 16 | + self.access_token_client = irDataClient(access_token="some-mock-token") |
15 | 17 |
|
16 | 18 | def test_encode_password(self): |
17 | 19 | expected_password = base64.b64encode( |
@@ -579,8 +581,8 @@ def test_parse_csv_response_mismatch(self, mock_print): |
579 | 581 | result = self.client._parse_csv_response(csv_text) |
580 | 582 | self.assertEqual(result, expected_output) |
581 | 583 | self.assertTrue(mock_print.called) |
582 | | - self.assertTrue( |
583 | | - mock_print.called_with("Warning: Row length does not match headers length") |
| 584 | + mock_print.assert_called_with( |
| 585 | + "Warning: Row length does not match headers length" |
584 | 586 | ) |
585 | 587 |
|
586 | 588 | @patch.object(irDataClient, "get_cars") |
@@ -1592,6 +1594,31 @@ def test_series_seasons(self, mock_get_resource): |
1592 | 1594 | ) |
1593 | 1595 | self.assertEqual(result, mock_get_resource.return_value) |
1594 | 1596 |
|
| 1597 | + def test_access_token_or_credentials(self): |
| 1598 | + """ |
| 1599 | + It should not be possible to supply both credentials and an access token |
| 1600 | + """ |
| 1601 | + with self.assertRaises(AttributeError): |
| 1602 | + irDataClient( |
| 1603 | + |
| 1604 | + password="somepassword", |
| 1605 | + access_token="an-access-token", |
| 1606 | + ) |
| 1607 | + |
| 1608 | + @patch("requests.Session.get") |
| 1609 | + def test_access_token_invalid_raises(self, mock_requests_get): |
| 1610 | + """ |
| 1611 | + When an access token is no longer valid, it should raise a specific exception for consumers to build around |
| 1612 | + """ |
| 1613 | + |
| 1614 | + mock_requests_get.return_value = MagicMock(status_code=401) |
| 1615 | + with self.assertRaises(AccessTokenInvalid): |
| 1616 | + subsession_id = 12345 |
| 1617 | + include_licenses = True |
| 1618 | + response = self.access_token_client.result( |
| 1619 | + subsession_id=subsession_id, include_licenses=include_licenses |
| 1620 | + ) |
| 1621 | + |
1595 | 1622 |
|
1596 | 1623 | if __name__ == "__main__": |
1597 | 1624 | unittest.main() |
0 commit comments