22from unittest .mock import patch , MagicMock , Mock , create_autospec
33from mns_service import MnsService
44from authentication import AppRestrictedAuth
5- from models .errors import UnhandledResponseError
5+ from models .errors import ServerError , UnhandledResponseError
66
77
88class TestMnsService (unittest .TestCase ):
@@ -14,7 +14,15 @@ def setUp(self):
1414 self .mock_cache = Mock ()
1515
1616 @patch ("mns_service.requests.post" )
17- def test_successful_subscription (self , mock_post ):
17+ @patch ("mns_service.requests.get" )
18+ def test_successful_subscription (self , mock_get , mock_post ):
19+
20+ # Arrange GET to return no subscription found
21+ mock_get_response = MagicMock ()
22+ mock_get_response .status_code = 200
23+ mock_get_response .json .return_value = {"entry" : []} # No entries!
24+ mock_get .return_value = mock_get_response
25+
1826 # Arrange
1927 mock_response = MagicMock ()
2028 mock_response .status_code = 201
@@ -24,11 +32,12 @@ def test_successful_subscription(self, mock_post):
2432 service = MnsService (self .authenticator )
2533
2634 # Act
27- result = service .subscribe_notification ()
35+ result = service .check_subscription ()
2836
2937 # Assert
3038 self .assertEqual (result , {"subscriptionId" : "abc123" })
3139 mock_post .assert_called_once ()
40+ mock_get .assert_called_once ()
3241 self .authenticator .get_access_token .assert_called_once ()
3342
3443 @patch ("mns_service.requests.post" )
@@ -38,9 +47,11 @@ def test_not_found_subscription(self, mock_post):
3847 mock_post .return_value = mock_response
3948
4049 service = MnsService (self .authenticator )
41- result = service .subscribe_notification ()
4250
43- self .assertIsNone (result )
51+ with self .assertRaises (UnhandledResponseError ) as context :
52+ service .subscribe_notification ()
53+ self .assertIn ("404" , str (context .exception ))
54+ self .assertIn ("Unhandled error" , str (context .exception ))
4455
4556 @patch ("mns_service.requests.post" )
4657 def test_unhandled_error (self , mock_post ):
@@ -51,10 +62,10 @@ def test_unhandled_error(self, mock_post):
5162
5263 service = MnsService (self .authenticator )
5364
54- with self .assertRaises (UnhandledResponseError ) as context :
65+ with self .assertRaises (ServerError ) as context :
5566 service .subscribe_notification ()
5667
57- self .assertIn ("MNS subscription failed " , str (context .exception ))
68+ self .assertIn ("Internal Server Error " , str (context .exception ))
5869
5970
6071if __name__ == "__main__" :
0 commit comments