|
| 1 | +"""Test the Http class.""" |
| 2 | + |
| 3 | +from datetime import datetime, timedelta |
| 4 | +import json |
| 5 | +import responses |
| 6 | +import unittest |
| 7 | + |
| 8 | +from PyTado.const import CLIENT_ID, CLIENT_SECRET |
| 9 | +from PyTado.exceptions import TadoException, TadoWrongCredentialsException |
| 10 | + |
| 11 | +from . import common |
| 12 | + |
| 13 | +from PyTado.http import Http |
| 14 | + |
| 15 | + |
| 16 | +class TestHttp(unittest.TestCase): |
| 17 | + """Testcases for Http class.""" |
| 18 | + |
| 19 | + def setUp(self): |
| 20 | + super().setUp() |
| 21 | + |
| 22 | + # Mock the login response |
| 23 | + responses.add( |
| 24 | + responses.POST, |
| 25 | + "https://auth.tado.com/oauth/token", |
| 26 | + json={ |
| 27 | + "access_token": "value", |
| 28 | + "expires_in": 1234, |
| 29 | + "refresh_token": "another_value", |
| 30 | + }, |
| 31 | + status=200, |
| 32 | + ) |
| 33 | + |
| 34 | + responses.add( |
| 35 | + responses.GET, |
| 36 | + "https://my.tado.com/api/v2/homes/1234/", |
| 37 | + json={"homes": [{"id": 1234}]}, |
| 38 | + status=200, |
| 39 | + ) |
| 40 | + |
| 41 | + # Mock the get me response |
| 42 | + responses.add( |
| 43 | + responses.GET, |
| 44 | + "https://my.tado.com/api/v2/me", |
| 45 | + json={"homes": [{"id": 1234}]}, |
| 46 | + status=200, |
| 47 | + ) |
| 48 | + |
| 49 | + @responses.activate |
| 50 | + def test_login_successful(self): |
| 51 | + |
| 52 | + instance = Http( |
| 53 | + username="test_user", |
| 54 | + password="test_pass", |
| 55 | + debug=True, |
| 56 | + ) |
| 57 | + |
| 58 | + # Verify that the login was successful |
| 59 | + self.assertEqual(instance._id, 1234) |
| 60 | + self.assertEqual(instance.is_x_line, False) |
| 61 | + |
| 62 | + @responses.activate |
| 63 | + def test_login_failed(self): |
| 64 | + |
| 65 | + responses.replace( |
| 66 | + responses.POST, |
| 67 | + "https://auth.tado.com/oauth/token", |
| 68 | + json={"error": "invalid_grant"}, |
| 69 | + status=400, |
| 70 | + ) |
| 71 | + |
| 72 | + with self.assertRaises( |
| 73 | + expected_exception=TadoWrongCredentialsException, |
| 74 | + msg="Your username or password is invalid", |
| 75 | + ): |
| 76 | + Http( |
| 77 | + username="test_user", |
| 78 | + password="test_pass", |
| 79 | + debug=True, |
| 80 | + ) |
| 81 | + |
| 82 | + responses.replace( |
| 83 | + responses.POST, |
| 84 | + "https://auth.tado.com/oauth/token", |
| 85 | + json={"error": "server failed"}, |
| 86 | + status=503, |
| 87 | + ) |
| 88 | + |
| 89 | + with self.assertRaises( |
| 90 | + expected_exception=TadoException, |
| 91 | + msg="Login failed for unknown reason with status code 503", |
| 92 | + ): |
| 93 | + Http( |
| 94 | + username="test_user", |
| 95 | + password="test_pass", |
| 96 | + debug=True, |
| 97 | + ) |
| 98 | + |
| 99 | + @responses.activate |
| 100 | + def test_line_x(self): |
| 101 | + |
| 102 | + responses.replace( |
| 103 | + responses.GET, |
| 104 | + "https://my.tado.com/api/v2/homes/1234/", |
| 105 | + json=json.loads(common.load_fixture("tadox/homes_response.json")), |
| 106 | + status=200, |
| 107 | + ) |
| 108 | + |
| 109 | + instance = Http( |
| 110 | + username="test_user", |
| 111 | + password="test_pass", |
| 112 | + debug=True, |
| 113 | + ) |
| 114 | + |
| 115 | + # Verify that the login was successful |
| 116 | + self.assertEqual(instance._id, 1234) |
| 117 | + self.assertEqual(instance.is_x_line, True) |
| 118 | + |
| 119 | + @responses.activate |
| 120 | + def test_refresh_token_success(self): |
| 121 | + instance = Http( |
| 122 | + username="test_user", |
| 123 | + password="test_pass", |
| 124 | + debug=True, |
| 125 | + ) |
| 126 | + |
| 127 | + expected_params = { |
| 128 | + "client_id": CLIENT_ID, |
| 129 | + "client_secret": CLIENT_SECRET, |
| 130 | + "grant_type": "refresh_token", |
| 131 | + "scope": "home.user", |
| 132 | + "refresh_token": "another_value", |
| 133 | + } |
| 134 | + # Mock the refresh token response |
| 135 | + refresh_token = responses.replace( |
| 136 | + responses.POST, |
| 137 | + "https://auth.tado.com/oauth/token", |
| 138 | + match=[ |
| 139 | + responses.matchers.query_param_matcher(expected_params), |
| 140 | + ], |
| 141 | + json={ |
| 142 | + "access_token": "new_value", |
| 143 | + "expires_in": 1234, |
| 144 | + "refresh_token": "new_refresh_value", |
| 145 | + }, |
| 146 | + status=200, |
| 147 | + ) |
| 148 | + |
| 149 | + # Force token refresh |
| 150 | + instance._refresh_at = datetime.now() - timedelta(seconds=1) |
| 151 | + instance._refresh_token() |
| 152 | + |
| 153 | + assert refresh_token.call_count == 1 |
| 154 | + |
| 155 | + # Verify that the token was refreshed |
| 156 | + self.assertEqual(instance._headers["Authorization"], "Bearer new_value") |
| 157 | + |
| 158 | + @responses.activate |
| 159 | + def test_refresh_token_failure(self): |
| 160 | + instance = Http( |
| 161 | + username="test_user", |
| 162 | + password="test_pass", |
| 163 | + debug=True, |
| 164 | + ) |
| 165 | + |
| 166 | + # Mock the refresh token response with failure |
| 167 | + refresh_token = responses.replace( |
| 168 | + responses.POST, |
| 169 | + "https://auth.tado.com/oauth/token", |
| 170 | + json={"error": "invalid_grant"}, |
| 171 | + status=400, |
| 172 | + ) |
| 173 | + |
| 174 | + # Force token refresh |
| 175 | + instance._refresh_at = datetime.now() - timedelta(seconds=1) |
| 176 | + |
| 177 | + with self.assertRaises(TadoException): |
| 178 | + instance._refresh_token() |
| 179 | + |
| 180 | + assert refresh_token.call_count == 1 |
0 commit comments