Skip to content

Commit 85eac82

Browse files
committed
VED-79: Fix tests issues
1 parent da3d958 commit 85eac82

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

mns_subscription/models/errors.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class Code(str, Enum):
2222

2323
@dataclass
2424
class UnauthorizedError(RuntimeError):
25+
response: dict | str
26+
message: str
27+
28+
def __str__(self):
29+
return f"{self.message}\n{self.response}"
30+
2531
@staticmethod
2632
def to_operation_outcome() -> dict:
2733
msg = "Unauthorized request"
@@ -35,6 +41,12 @@ def to_operation_outcome() -> dict:
3541

3642
@dataclass
3743
class TokenValidationError(RuntimeError):
44+
response: dict | str
45+
message: str
46+
47+
def __str__(self):
48+
return f"{self.message}\n{self.response}"
49+
3850
@staticmethod
3951
def to_operation_outcome() -> dict:
4052
msg = "Missing/Invalid Token"
@@ -48,6 +60,12 @@ def to_operation_outcome() -> dict:
4860

4961
@dataclass
5062
class ConflictError(RuntimeError):
63+
response: dict | str
64+
message: str
65+
66+
def __str__(self):
67+
return f"{self.message}\n{self.response}"
68+
5169
@staticmethod
5270
def to_operation_outcome() -> dict:
5371
msg = "Conflict"

mns_subscription/src/mns_service.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def subscribe_notification(self) -> dict | None:
4848
print(f"Headers: {self.request_headers}")
4949
print(f"Payload: {json.dumps(self.subscription_payload, indent=2)}")
5050

51-
if response.status_code == 200:
51+
if response.status_code in (200, 201):
5252
return response.json()
5353
elif response.status_code == 409:
5454
msg = "SQS Queue Already Subscribed, can't re-subscribe"
@@ -83,8 +83,6 @@ def get_subscription(self) -> dict | None:
8383
if channel.get("endpoint") == SQS_ARN:
8484
return resource # Found a matching subscription
8585
return None # No subscription for this SQS ARN
86-
elif response.status_code == 404:
87-
return None
8886
elif response.status_code == 401:
8987
msg = "Token validation failed for the request"
9088
raise TokenValidationError(response=response.json(), message=msg)

mns_subscription/tests/test_mns_service.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest.mock import patch, MagicMock, Mock, create_autospec
33
from mns_service import MnsService
44
from authentication import AppRestrictedAuth
5-
from models.errors import UnhandledResponseError
5+
from models.errors import ServerError, UnhandledResponseError
66

77

88
class 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

6071
if __name__ == "__main__":

mns_subscription/tests/test_subscription.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_run_subscription_success(self, mock_boto_client, mock_auth_class, mock_
1717
mock_auth_class.return_value = mock_auth_instance
1818

1919
mock_mns_instance = MagicMock()
20-
mock_mns_instance.subscribe_notification.return_value = {"subscriptionId": "abc123"}
20+
mock_mns_instance.check_subscription.return_value = {"subscriptionId": "abc123"}
2121
mock_mns_service.return_value = mock_mns_instance
2222

2323
# Act
@@ -26,7 +26,7 @@ def test_run_subscription_success(self, mock_boto_client, mock_auth_class, mock_
2626
# Assert
2727
self.assertEqual(result, {"subscriptionId": "abc123"})
2828
mock_auth_class.assert_called_once()
29-
mock_mns_instance.subscribe_notification.assert_called_once()
29+
mock_mns_instance.check_subscription.assert_called_once()
3030

3131

3232
if __name__ == "__main__":

0 commit comments

Comments
 (0)