11# Copyright (c) 2023 Apple Inc. Licensed under MIT License.
22
3+ from typing import Optional
34import unittest
45from appstoreserverlibrary .models .AutoRenewStatus import AutoRenewStatus
56from appstoreserverlibrary .models .Environment import Environment
1516from appstoreserverlibrary .models .TransactionReason import TransactionReason
1617from appstoreserverlibrary .models .Type import Type
1718
18- from tests .util import create_signed_data_from_json , get_default_signed_data_verifier
19+ from tests .util import create_signed_data_from_json , get_default_signed_data_verifier , get_signed_data_verifier
1920
2021class DecodedPayloads (unittest .TestCase ):
2122 def test_app_transaction_decoding (self ):
@@ -122,6 +123,7 @@ def test_notificaiton_decoding(self):
122123 self .assertEqual (1698148900000 , notification .signedDate )
123124 self .assertIsNotNone (notification .data )
124125 self .assertIsNone (notification .summary )
126+ self .assertIsNone (notification .externalPurchaseToken )
125127 self .assertEqual (Environment .LOCAL_TESTING , notification .data .environment )
126128 self .assertEqual ("LocalTesting" , notification .data .rawEnvironment )
127129 self .assertEqual (41234 , notification .data .appAppleId )
@@ -148,6 +150,7 @@ def test_summary_notification_decoding(self):
148150 self .assertEqual (1698148900000 , notification .signedDate )
149151 self .assertIsNone (notification .data )
150152 self .assertIsNotNone (notification .summary )
153+ self .assertIsNone (notification .externalPurchaseToken )
151154 self .assertEqual (Environment .LOCAL_TESTING , notification .summary .environment )
152155 self .assertEqual ("LocalTesting" , notification .summary .rawEnvironment )
153156 self .assertEqual (41234 , notification .summary .appAppleId )
@@ -156,4 +159,62 @@ def test_summary_notification_decoding(self):
156159 self .assertEqual ("efb27071-45a4-4aca-9854-2a1e9146f265" , notification .summary .requestIdentifier )
157160 self .assertEqual (["CAN" , "USA" , "MEX" ], notification .summary .storefrontCountryCodes )
158161 self .assertEqual (5 , notification .summary .succeededCount )
159- self .assertEqual (2 , notification .summary .failedCount )
162+ self .assertEqual (2 , notification .summary .failedCount )
163+
164+ def test_external_purchase_token_notification_decoding (self ):
165+ signed_external_purchase_token_notification = create_signed_data_from_json ('tests/resources/models/signedExternalPurchaseTokenNotification.json' )
166+
167+ signed_data_verifier = get_default_signed_data_verifier ()
168+
169+ def check_environment_and_bundle_id (bundle_id : Optional [str ], app_apple_id : Optional [int ], environment : Optional [Environment ]):
170+ self .assertEqual ("com.example" , bundle_id )
171+ self .assertEqual (55555 , app_apple_id )
172+ self .assertEqual (Environment .PRODUCTION , environment )
173+
174+ signed_data_verifier ._verify_notification = check_environment_and_bundle_id
175+
176+ notification = signed_data_verifier .verify_and_decode_notification (signed_external_purchase_token_notification )
177+
178+ self .assertEqual (NotificationTypeV2 .EXTERNAL_PURCHASE_TOKEN , notification .notificationType )
179+ self .assertEqual ("EXTERNAL_PURCHASE_TOKEN" , notification .rawNotificationType )
180+ self .assertEqual (Subtype .UNREPORTED , notification .subtype )
181+ self .assertEqual ("UNREPORTED" , notification .rawSubtype )
182+ self .assertEqual ("002e14d5-51f5-4503-b5a8-c3a1af68eb20" , notification .notificationUUID )
183+ self .assertEqual ("2.0" , notification .version )
184+ self .assertEqual (1698148900000 , notification .signedDate )
185+ self .assertIsNone (notification .data )
186+ self .assertIsNone (notification .summary )
187+ self .assertIsNotNone (notification .externalPurchaseToken )
188+ self .assertEqual ("b2158121-7af9-49d4-9561-1f588205523e" , notification .externalPurchaseToken .externalPurchaseId )
189+ self .assertEqual (1698148950000 , notification .externalPurchaseToken .tokenCreationDate )
190+ self .assertEqual (55555 , notification .externalPurchaseToken .appAppleId )
191+ self .assertEqual ("com.example" , notification .externalPurchaseToken .bundleId )
192+
193+ def test_external_purchase_token_sandbox_notification_decoding (self ):
194+ signed_external_purchase_token_notification = create_signed_data_from_json ('tests/resources/models/signedExternalPurchaseTokenSandboxNotification.json' )
195+
196+ signed_data_verifier = get_default_signed_data_verifier ()
197+
198+ def check_environment_and_bundle_id (bundle_id : Optional [str ], app_apple_id : Optional [int ], environment : Optional [Environment ]):
199+ self .assertEqual ("com.example" , bundle_id )
200+ self .assertEqual (55555 , app_apple_id )
201+ self .assertEqual (Environment .SANDBOX , environment )
202+
203+ signed_data_verifier ._verify_notification = check_environment_and_bundle_id
204+
205+ notification = signed_data_verifier .verify_and_decode_notification (signed_external_purchase_token_notification )
206+
207+ self .assertEqual (NotificationTypeV2 .EXTERNAL_PURCHASE_TOKEN , notification .notificationType )
208+ self .assertEqual ("EXTERNAL_PURCHASE_TOKEN" , notification .rawNotificationType )
209+ self .assertEqual (Subtype .UNREPORTED , notification .subtype )
210+ self .assertEqual ("UNREPORTED" , notification .rawSubtype )
211+ self .assertEqual ("002e14d5-51f5-4503-b5a8-c3a1af68eb20" , notification .notificationUUID )
212+ self .assertEqual ("2.0" , notification .version )
213+ self .assertEqual (1698148900000 , notification .signedDate )
214+ self .assertIsNone (notification .data )
215+ self .assertIsNone (notification .summary )
216+ self .assertIsNotNone (notification .externalPurchaseToken )
217+ self .assertEqual ("SANDBOX_b2158121-7af9-49d4-9561-1f588205523e" , notification .externalPurchaseToken .externalPurchaseId )
218+ self .assertEqual (1698148950000 , notification .externalPurchaseToken .tokenCreationDate )
219+ self .assertEqual (55555 , notification .externalPurchaseToken .appAppleId )
220+ self .assertEqual ("com.example" , notification .externalPurchaseToken .bundleId )
0 commit comments