|
| 1 | +from datetime import datetime, timezone |
| 2 | +from behave import given, when, then |
| 3 | +from sinch.domains.sms.models.v1.response import BatchDeliveryReport, RecipientDeliveryReport |
| 4 | + |
| 5 | + |
| 6 | +@given('the SMS service "{service_name}" is available') |
| 7 | +def step_sms_service_available(context, service_name): |
| 8 | + """Ensures the Sinch client is initialized""" |
| 9 | + assert hasattr(context, 'sinch') and context.sinch, 'Sinch client was not initialized' |
| 10 | + |
| 11 | + |
| 12 | +@given('the SMS service "{service_name}" is available and is configured for servicePlanId authentication') |
| 13 | +def step_sms_service_available_with_service_plan(context, service_name): |
| 14 | + """Ensures the Sinch client is initialized with service_plan_id authentication""" |
| 15 | + from sinch import SinchClient |
| 16 | + |
| 17 | + # Create a new client with service_plan_id authentication |
| 18 | + context.sinch = SinchClient( |
| 19 | + service_plan_id='CappyPremiumPlan', |
| 20 | + sms_api_token='HappyCappyToken', |
| 21 | + ) |
| 22 | + context.sinch.configuration.auth_origin = 'http://localhost:3011' |
| 23 | + context.sinch.configuration.sms_origin = 'http://localhost:3017' |
| 24 | + context.sinch.configuration.sms_origin_with_service_plan_id = 'http://localhost:3017' |
| 25 | + |
| 26 | + |
| 27 | +@when('I send a request to retrieve a summary SMS delivery report') |
| 28 | +def step_retrieve_summary_delivery_report(context): |
| 29 | + """Retrieve a summary SMS delivery report""" |
| 30 | + context.response = context.sinch.sms.delivery_reports.get( |
| 31 | + batch_id='01W4FFL35P4NC4K35SMSBATCH1', |
| 32 | + status=['DELIVERED', 'FAILED'], |
| 33 | + code=[15, 0] |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@then('the response contains a summary SMS delivery report') |
| 38 | +def step_validate_summary_delivery_report(context): |
| 39 | + """Validate summary SMS delivery report response""" |
| 40 | + data: BatchDeliveryReport = context.response |
| 41 | + assert data.batch_id == '01W4FFL35P4NC4K35SMSBATCH1' |
| 42 | + assert data.client_reference == 'reference_e2e' |
| 43 | + assert data.statuses is not None |
| 44 | + assert len(data.statuses) >= 2 |
| 45 | + |
| 46 | + status = data.statuses[0] |
| 47 | + assert status.code == 15 |
| 48 | + assert status.count == 1 |
| 49 | + assert status.recipients is None |
| 50 | + assert status.status == 'Failed' |
| 51 | + |
| 52 | + status = data.statuses[1] |
| 53 | + assert status.code == 0 |
| 54 | + assert status.count == 1 |
| 55 | + assert status.recipients is None |
| 56 | + assert status.status == 'Delivered' |
| 57 | + |
| 58 | + assert data.total_message_count == 2 |
| 59 | + assert data.type == 'delivery_report_sms' |
| 60 | + |
| 61 | + |
| 62 | +@when('I send a request to retrieve a full SMS delivery report') |
| 63 | +def step_retrieve_full_delivery_report(context): |
| 64 | + """Retrieve a full SMS delivery report""" |
| 65 | + context.response = context.sinch.sms.delivery_reports.get( |
| 66 | + batch_id='01W4FFL35P4NC4K35SMSBATCH1', |
| 67 | + report_type='full' |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@then('the response contains a full SMS delivery report') |
| 72 | +def step_validate_full_delivery_report(context): |
| 73 | + """Validate full SMS delivery report response""" |
| 74 | + data: BatchDeliveryReport = context.response |
| 75 | + assert data.batch_id == '01W4FFL35P4NC4K35SMSBATCH1' |
| 76 | + assert data.statuses is not None |
| 77 | + status = data.statuses[0] |
| 78 | + assert status.recipients is not None |
| 79 | + assert status.code == 0 |
| 80 | + assert status.count == 1 |
| 81 | + assert status.recipients[0] == '12017777777' |
| 82 | + assert status.status == 'Delivered' |
| 83 | + |
| 84 | + |
| 85 | +@when('I send a request to retrieve a recipient\'s delivery report') |
| 86 | +def step_retrieve_recipient_delivery_report(context): |
| 87 | + """Retrieve a recipient's delivery report""" |
| 88 | + context.response = context.sinch.sms.delivery_reports.get_for_number( |
| 89 | + batch_id='01W4FFL35P4NC4K35SMSBATCH1', |
| 90 | + recipient='12017777777' |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@then('the response contains the recipient\'s delivery report details') |
| 95 | +def step_validate_recipient_delivery_report(context): |
| 96 | + """Validate recipient delivery report response""" |
| 97 | + data: RecipientDeliveryReport = context.response |
| 98 | + assert data.batch_id == '01W4FFL35P4NC4K35SMSBATCH1' |
| 99 | + assert data.recipient == '12017777777' |
| 100 | + assert data.client_reference == 'reference_e2e' |
| 101 | + assert data.status == 'Delivered' |
| 102 | + assert data.type == 'recipient_delivery_report_sms' |
| 103 | + assert data.code == 0 |
| 104 | + assert data.at == datetime(2024, 6, 6, 13, 6, 27, 833000, tzinfo=timezone.utc) |
| 105 | + assert data.operator_status_at == datetime(2024, 6, 6, 13, 6, 0, tzinfo=timezone.utc) |
| 106 | + |
| 107 | + |
| 108 | +@when('I send a request to list the SMS delivery reports') |
| 109 | +def step_list_delivery_reports(context): |
| 110 | + """List a page of SMS delivery reports""" |
| 111 | + context.response = context.sinch.sms.delivery_reports.list() |
| 112 | + |
| 113 | + |
| 114 | +@then('the response contains "{count}" SMS delivery reports') |
| 115 | +def step_validate_delivery_reports_count(context, count): |
| 116 | + """Validate the count of SMS delivery reports in response""" |
| 117 | + expected_count = int(count) |
| 118 | + assert len(context.response.content()) == expected_count, \ |
| 119 | + f'Expected {expected_count}, got {len(context.response.content())}' |
| 120 | + |
| 121 | + |
| 122 | +@when('I send a request to list all the SMS delivery reports') |
| 123 | +def step_list_all_delivery_reports(context): |
| 124 | + """List all SMS delivery reports using iterator""" |
| 125 | + response = context.sinch.sms.delivery_reports.list(page_size=2) |
| 126 | + delivery_reports_list = [] |
| 127 | + |
| 128 | + for delivery_report in response.iterator(): |
| 129 | + delivery_reports_list.append(delivery_report) |
| 130 | + |
| 131 | + context.delivery_reports_list = delivery_reports_list |
| 132 | + |
| 133 | + |
| 134 | +@then('the SMS delivery reports list contains "{count}" SMS delivery reports') |
| 135 | +def step_validate_delivery_reports_list_count(context, count): |
| 136 | + """Validate the count of SMS delivery reports in the full list""" |
| 137 | + expected_count = int(count) |
| 138 | + assert len(context.delivery_reports_list) == expected_count, \ |
| 139 | + f'Expected {expected_count}, got {len(context.delivery_reports_list)}' |
| 140 | + |
| 141 | + |
| 142 | +@when('I iterate manually over the SMS delivery reports pages') |
| 143 | +def step_iterate_manually_delivery_reports(context): |
| 144 | + """Manually iterate over SMS delivery reports pages""" |
| 145 | + context.list_response = context.sinch.sms.delivery_reports.list(page_size=2) |
| 146 | + |
| 147 | + # Iterate through all pages |
| 148 | + context.delivery_reports_list = [] |
| 149 | + context.pages_iteration = 0 |
| 150 | + reached_last_page = False |
| 151 | + |
| 152 | + while not reached_last_page: |
| 153 | + context.delivery_reports_list.extend(context.list_response.content()) |
| 154 | + context.pages_iteration += 1 |
| 155 | + if context.list_response.has_next_page: |
| 156 | + context.list_response = context.list_response.next_page() |
| 157 | + else: |
| 158 | + reached_last_page = True |
| 159 | + |
| 160 | + |
| 161 | +@then('the SMS delivery reports iteration result contains the data from "{count}" pages') |
| 162 | +def step_validate_delivery_reports_pages_count(context, count): |
| 163 | + """Validate the count of pages in the iteration result""" |
| 164 | + expected_pages_count = int(count) |
| 165 | + assert context.pages_iteration == expected_pages_count, \ |
| 166 | + f'Expected {expected_pages_count} pages, got {context.pages_iteration}' |
0 commit comments