|
1 | 1 | """Tests for authentication.""" |
| 2 | +import pytest |
| 3 | + |
2 | 4 | from monzo import authentication |
| 5 | +from monzo.exceptions import MonzoAuthenticationError |
3 | 6 | from tests.helpers import Handler, load_data |
4 | 7 |
|
5 | 8 |
|
@@ -44,3 +47,38 @@ def test_logout(self, mocker): |
44 | 47 | path=expected_data['path'], |
45 | 48 | timeout=expected_data['timeout'] |
46 | 49 | ) |
| 50 | + |
| 51 | + def test_authenticate_with_empty_token(self): |
| 52 | + """ |
| 53 | + Test authenticate raises an error when the authorization token is empty. |
| 54 | + """ |
| 55 | + auth = authentication.Authentication( |
| 56 | + client_id='client_id', |
| 57 | + client_secret='client_secret', |
| 58 | + redirect_url='', |
| 59 | + ) |
| 60 | + |
| 61 | + with pytest.raises(MonzoAuthenticationError): |
| 62 | + auth.authenticate(authorization_token='', state_token='state_token') |
| 63 | + |
| 64 | + def test_authenticate_state_token_mismatch(self, tmp_path, mocker): |
| 65 | + """ |
| 66 | + Test authenticate raises an error when the provided state token does not match. |
| 67 | +
|
| 68 | + Args: |
| 69 | + tmp_path: Pytest fixture for temporary directory. |
| 70 | + mocker: Pytest mocker fixture. |
| 71 | + """ |
| 72 | + mocker.patch('monzo.authentication.gettempdir', return_value=str(tmp_path)) |
| 73 | + auth = authentication.Authentication( |
| 74 | + client_id='client_id', |
| 75 | + client_secret='client_secret', |
| 76 | + redirect_url='', |
| 77 | + ) |
| 78 | + state_token = auth.state_token |
| 79 | + |
| 80 | + with pytest.raises(MonzoAuthenticationError): |
| 81 | + auth.authenticate( |
| 82 | + authorization_token='auth_token', |
| 83 | + state_token=f'{state_token}invalid', |
| 84 | + ) |
0 commit comments