|
| 1 | +import json |
| 2 | +import unittest |
| 3 | +import uuid |
| 4 | + |
| 5 | +import mock |
| 6 | +import requests |
| 7 | + |
| 8 | +import urbanairship as ua |
| 9 | +from tests import TEST_KEY, TEST_SECRET |
| 10 | +from urbanairship.client import BasicAuthClient |
| 11 | + |
| 12 | + |
| 13 | +class TestAirshipResponse(unittest.TestCase): |
| 14 | + test_channel = str(uuid.uuid4()) |
| 15 | + airship = BasicAuthClient(TEST_KEY, TEST_SECRET, location="us") |
| 16 | + common_push = ua.Push(airship=airship) |
| 17 | + common_push.device_types = ua.device_types("ios") |
| 18 | + common_push.audience = ua.channel(test_channel) |
| 19 | + common_push.notification = ua.notification(alert="testing") |
| 20 | + |
| 21 | + def test_unauthorized(self): |
| 22 | + with mock.patch.object(BasicAuthClient, "_request") as mock_request: |
| 23 | + response = requests.Response() |
| 24 | + response._content = json.dumps({"ok": False}).encode("utf-8") |
| 25 | + response.status_code = 401 |
| 26 | + mock_request.return_value = response |
| 27 | + |
| 28 | + try: |
| 29 | + self.common_push.send() |
| 30 | + except Exception as e: |
| 31 | + self.assertIsInstance(ua.Unauthorized, e) |
| 32 | + |
| 33 | + def test_client_error(self): |
| 34 | + with mock.patch.object(BasicAuthClient, "_request") as mock_request: |
| 35 | + response = requests.Response() |
| 36 | + response._content = json.dumps({"ok": False}).encode("utf-8") |
| 37 | + response.status_code = 400 |
| 38 | + mock_request.return_value = response |
| 39 | + |
| 40 | + try: |
| 41 | + r = self.common_push.send() |
| 42 | + except Exception as e: |
| 43 | + self.assertIsInstance(ua.AirshipFailure, e) |
| 44 | + self.assertEqual(r.status_code, 400) |
| 45 | + |
| 46 | + def test_server_error(self): |
| 47 | + with mock.patch.object(BasicAuthClient, "_request") as mock_request: |
| 48 | + response = requests.Response() |
| 49 | + response._content = json.dumps({"ok": False}).encode("utf-8") |
| 50 | + response.status_code = 500 |
| 51 | + mock_request.return_value = response |
| 52 | + |
| 53 | + try: |
| 54 | + r = self.common_push.send() |
| 55 | + except Exception as e: |
| 56 | + self.assertIsInstance(ua.AirshipFailure, e) |
| 57 | + self.assertEqual(r.status_code, 500) |
0 commit comments