|
6 | 6 |
|
7 | 7 | import geoip2 |
8 | 8 | import requests_mock |
9 | | -from geoip2.errors import AddressNotFoundError, AuthenticationError, \ |
10 | | - GeoIP2Error, HTTPError, InvalidRequestError, OutOfQueriesError |
| 9 | +from geoip2.errors import (AddressNotFoundError, AuthenticationError, |
| 10 | + GeoIP2Error, HTTPError, InvalidRequestError, OutOfQueriesError, |
| 11 | + PermissionRequiredError) |
11 | 12 | from geoip2.webservice import Client |
12 | 13 |
|
13 | 14 | if sys.version_info[:2] == (2, 6): |
|
21 | 22 |
|
22 | 23 |
|
23 | 24 | class TestClient(unittest.TestCase): |
| 25 | + |
24 | 26 | def setUp(self): |
25 | 27 | self.client = Client(42, 'abcdef123456') |
26 | 28 |
|
@@ -149,63 +151,54 @@ def test_300_error(self, mock): |
149 | 151 | self.client.country('1.2.3.11') |
150 | 152 |
|
151 | 153 | @requests_mock.mock() |
152 | | - def test_address_not_found_error(self, mock): |
153 | | - body = {'error': 'Not in DB', 'code': 'IP_ADDRESS_NOT_FOUND'} |
154 | | - mock.get(self.base_uri + 'country/' + '1.2.3.13', |
155 | | - json=body, |
156 | | - status_code=404, |
157 | | - headers={'Content-Type': self._content_type('country')}) |
158 | | - with self.assertRaisesRegex(AddressNotFoundError, 'Not in DB'): |
159 | | - self.client.country('1.2.3.13') |
| 154 | + def test_ip_address_required(self, mock): |
| 155 | + self._test_error(mock, 400, 'IP_ADDRESS_REQUIRED', InvalidRequestError) |
160 | 156 |
|
161 | 157 | @requests_mock.mock() |
162 | | - def test_private_address_error(self, mock): |
163 | | - body = {'error': 'Private', 'code': 'IP_ADDRESS_RESERVED'} |
164 | | - mock.get(self.base_uri + 'country/' + '1.2.3.14', |
165 | | - json=body, |
166 | | - status_code=401, |
167 | | - headers={'Content-Type': self._content_type('country')}) |
168 | | - with self.assertRaisesRegex(AddressNotFoundError, 'Private'): |
169 | | - self.client.country('1.2.3.14') |
| 158 | + def test_ip_address_not_found(self, mock): |
| 159 | + self._test_error(mock, 404, 'IP_ADDRESS_NOT_FOUND', |
| 160 | + AddressNotFoundError) |
| 161 | + |
| 162 | + @requests_mock.mock() |
| 163 | + def test_ip_address_reserved(self, mock): |
| 164 | + self._test_error(mock, 400, 'IP_ADDRESS_RESERVED', |
| 165 | + AddressNotFoundError) |
| 166 | + |
| 167 | + @requests_mock.mock() |
| 168 | + def test_permission_required(self, mock): |
| 169 | + self._test_error(mock, 403, 'PERMISSION_REQUIRED', |
| 170 | + PermissionRequiredError) |
170 | 171 |
|
171 | 172 | @requests_mock.mock() |
172 | 173 | def test_auth_invalid(self, mock): |
173 | | - body = {'error': 'Invalid auth', 'code': 'AUTHORIZATION_INVALID'} |
174 | | - mock.get(self.base_uri + 'country/' + '1.2.3.15', |
175 | | - json=body, |
176 | | - status_code=400, |
177 | | - headers={'Content-Type': self._content_type('country')}) |
178 | | - with self.assertRaisesRegex(AuthenticationError, 'Invalid auth'): |
179 | | - self.client.country('1.2.3.15') |
| 174 | + self._test_error(mock, 400, 'AUTHORIZATION_INVALID', |
| 175 | + AuthenticationError) |
180 | 176 |
|
181 | 177 | @requests_mock.mock() |
182 | | - def test_license_required(self, mock): |
183 | | - body = {'error': 'License required', 'code': 'LICENSE_KEY_REQUIRED'} |
184 | | - mock.get(self.base_uri + 'country/' + '1.2.3.16', |
185 | | - json=body, |
186 | | - status_code=401, |
187 | | - headers={'Content-Type': self._content_type('country')}) |
188 | | - with self.assertRaisesRegex(AuthenticationError, 'License required'): |
189 | | - self.client.country('1.2.3.16') |
| 178 | + def test_license_key_required(self, mock): |
| 179 | + self._test_error(mock, 401, 'LICENSE_KEY_REQUIRED', |
| 180 | + AuthenticationError) |
190 | 181 |
|
191 | 182 | @requests_mock.mock() |
192 | 183 | def test_user_id_required(self, mock): |
193 | | - body = {'error': 'User ID required', 'code': 'USER_ID_REQUIRED'} |
194 | | - mock.get(self.base_uri + 'country/' + '1.2.3.17', |
195 | | - json=body, |
196 | | - status_code=401, |
197 | | - headers={'Content-Type': self._content_type('country')}) |
198 | | - with self.assertRaisesRegex(AuthenticationError, 'User ID required'): |
199 | | - self.client.country('1.2.3.17') |
| 184 | + self._test_error(mock, 401, 'USER_ID_REQUIRED', AuthenticationError) |
| 185 | + |
| 186 | + @requests_mock.mock() |
| 187 | + def test_user_id_unkown(self, mock): |
| 188 | + self._test_error(mock, 401, 'USER_ID_UNKNOWN', AuthenticationError) |
200 | 189 |
|
201 | 190 | @requests_mock.mock() |
202 | 191 | def test_out_of_queries_error(self, mock): |
203 | | - body = {'error': 'Out of Queries', 'code': 'OUT_OF_QUERIES'} |
204 | | - mock.get(self.base_uri + 'country/' + '1.2.3.18', |
| 192 | + self._test_error(mock, 402, 'OUT_OF_QUERIES', OutOfQueriesError) |
| 193 | + |
| 194 | + def _test_error(self, mock, status, error_code, error_class): |
| 195 | + msg = 'Some error message' |
| 196 | + body = {'error': msg, 'code': error_code} |
| 197 | + mock.get(self.base_uri + 'country/1.2.3.18', |
205 | 198 | json=body, |
206 | | - status_code=402, |
| 199 | + status_code=status, |
207 | 200 | headers={'Content-Type': self._content_type('country')}) |
208 | | - with self.assertRaisesRegex(OutOfQueriesError, 'Out of Queries'): |
| 201 | + with self.assertRaisesRegex(error_class, msg): |
209 | 202 | self.client.country('1.2.3.18') |
210 | 203 |
|
211 | 204 | @requests_mock.mock() |
|
0 commit comments