|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +import responses |
| 5 | + |
| 6 | +from django_mercadopago import fixtures, models |
| 7 | + |
| 8 | + |
| 9 | +preference_response = { |
| 10 | + 'processing_modes': [], |
| 11 | + 'metadata': {}, |
| 12 | + 'binary_mode': False, |
| 13 | + 'payment_methods': { |
| 14 | + 'excluded_payment_methods': [{'id': ''}], |
| 15 | + 'excluded_payment_types': [{'id': ''}], |
| 16 | + 'installments': None, |
| 17 | + 'default_payment_method_id': None, |
| 18 | + 'default_installments': None |
| 19 | + }, |
| 20 | + 'collector_id': 152658942, |
| 21 | + 'operation_type': 'regular_payment', |
| 22 | + 'items': [{ |
| 23 | + 'id': '', |
| 24 | + 'picture_url': '', |
| 25 | + 'title': 'Test', |
| 26 | + 'description': 'Something that a client bought.', |
| 27 | + 'category_id': 'services', |
| 28 | + 'currency_id': 'ARS', |
| 29 | + 'quantity': 1, |
| 30 | + 'unit_price': 100 |
| 31 | + }], |
| 32 | + 'payer': { |
| 33 | + 'name': '', |
| 34 | + 'surname': '', |
| 35 | + 'email': '', |
| 36 | + 'date_created': '', |
| 37 | + 'phone': {'area_code': '', 'number': ''}, |
| 38 | + 'identification': {'type': '', 'number': ''}, |
| 39 | + 'address': { |
| 40 | + 'street_name': '', |
| 41 | + 'street_number': None, |
| 42 | + 'zip_code': '' |
| 43 | + } |
| 44 | + }, |
| 45 | + 'back_urls': { |
| 46 | + 'success': 'http://localhost:8001/post_payment/ref-123', |
| 47 | + 'pending': 'http://localhost:8001/payment_pending/ref-123', |
| 48 | + 'failure': 'http://localhost:8001/payment_failed/ref-123' |
| 49 | + }, |
| 50 | + 'auto_return': 'all', |
| 51 | + 'client_id': '3641931198760523', |
| 52 | + 'marketplace': 'NONE', |
| 53 | + 'marketplace_fee': 0, |
| 54 | + 'shipments': { |
| 55 | + 'receiver_address': { |
| 56 | + 'zip_code': '', |
| 57 | + 'street_number': None, |
| 58 | + 'street_name': '', |
| 59 | + 'floor': '', |
| 60 | + 'apartment': '' |
| 61 | + } |
| 62 | + }, |
| 63 | + 'notification_url': 'http://localhost:8001/notifications/ref-123', |
| 64 | + 'external_reference': 'ref-123', |
| 65 | + 'additional_info': '', |
| 66 | + 'expires': False, |
| 67 | + 'expiration_date_from': None, |
| 68 | + 'expiration_date_to': None, |
| 69 | + 'date_created': '2019-10-13T15:39:08.981-04:00', |
| 70 | + 'id': '152658942-f090626e-6d4d-4877-a3d5-292e8877e4cb', |
| 71 | + 'init_point': 'https://www.mercadopago.com/init_point', |
| 72 | + 'sandbox_init_point': 'https://sbox.mercadopago.com/init_point' |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.django_db |
| 77 | +@responses.activate |
| 78 | +def test_preference_request(): |
| 79 | + """Preference creation runs the expected query.""" |
| 80 | + responses.add( |
| 81 | + responses.POST, |
| 82 | + 'https://api.mercadopago.com/oauth/token', |
| 83 | + json={ |
| 84 | + "access_token": "APP_USR-3641931198760523", |
| 85 | + "refresh_token": "TG-5da379a4705f640006b69d70-152658942", |
| 86 | + "live_mode": True, |
| 87 | + "user_id": 152658949, |
| 88 | + "token_type": "bearer", |
| 89 | + "expires_in": 21600, |
| 90 | + "scope": "offline_access read write" |
| 91 | + }, |
| 92 | + status=200, |
| 93 | + ) |
| 94 | + responses.add( |
| 95 | + responses.POST, |
| 96 | + 'https://api.mercadopago.com/checkout/preferences', |
| 97 | + json=preference_response, |
| 98 | + status=200, |
| 99 | + ) |
| 100 | + |
| 101 | + models.Preference.objects.create( |
| 102 | + title="Test", |
| 103 | + description="Something that a client bought.", |
| 104 | + price=100, |
| 105 | + reference="ref-123", |
| 106 | + account=fixtures.AccountFactory(), |
| 107 | + ) |
| 108 | + |
| 109 | + expected_url = ( |
| 110 | + 'https://api.mercadopago.com/checkout/preferences?access_token=' |
| 111 | + 'APP_USR-3641931198760523' |
| 112 | + ) |
| 113 | + expected_request = { |
| 114 | + "auto_return": |
| 115 | + "all", |
| 116 | + "items": [{ |
| 117 | + "title": "Test", |
| 118 | + "currency_id": "ARS", |
| 119 | + "description": "Something that a client bought.", |
| 120 | + "category_id": "services", |
| 121 | + "quantity": 1, |
| 122 | + "unit_price": 100.0 |
| 123 | + }], |
| 124 | + "external_reference": |
| 125 | + "ref-123", |
| 126 | + "back_urls": { |
| 127 | + "success": "http://localhost:8001/post_payment/ref-123", |
| 128 | + "pending": "http://localhost:8001/payment_pending/ref-123", |
| 129 | + "failure": "http://localhost:8001/payment_failed/ref-123" |
| 130 | + }, |
| 131 | + "notification_url": |
| 132 | + "http://localhost:8001/notifications/ref-123" |
| 133 | + } |
| 134 | + |
| 135 | + assert len(responses.calls) == 2 |
| 136 | + assert responses.calls[1].request.url == expected_url |
| 137 | + assert responses.calls[1].request.body == json.dumps(expected_request) |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.django_db |
| 141 | +@responses.activate |
| 142 | +def test_preference_creation(): |
| 143 | + """Preference creation assigned attributes properly.""" |
| 144 | + responses.add( |
| 145 | + responses.POST, |
| 146 | + 'https://api.mercadopago.com/oauth/token', |
| 147 | + json={ |
| 148 | + "access_token": "APP_USR-3641931198760523", |
| 149 | + "refresh_token": "TG-5da379a4705f640006b69d70-152658942", |
| 150 | + "live_mode": True, |
| 151 | + "user_id": 152658949, |
| 152 | + "token_type": "bearer", |
| 153 | + "expires_in": 21600, |
| 154 | + "scope": "offline_access read write" |
| 155 | + }, |
| 156 | + status=200, |
| 157 | + ) |
| 158 | + responses.add( |
| 159 | + responses.POST, |
| 160 | + 'https://api.mercadopago.com/checkout/preferences', |
| 161 | + json=preference_response, |
| 162 | + status=200, |
| 163 | + ) |
| 164 | + |
| 165 | + account = fixtures.AccountFactory() |
| 166 | + preference = models.Preference.objects.create( |
| 167 | + title="Test", |
| 168 | + description="Something that a client bought.", |
| 169 | + price=100, |
| 170 | + reference="ref-123", |
| 171 | + account=account, |
| 172 | + ) |
| 173 | + |
| 174 | + assert preference.title == 'Test' |
| 175 | + assert preference.price == 100 |
| 176 | + assert preference.quantity == 1 |
| 177 | + assert preference.mp_id == '152658942-f090626e-6d4d-4877-a3d5-292e8877e4cb' |
| 178 | + assert preference.payment_url == 'https://www.mercadopago.com/init_point' |
| 179 | + assert preference.sandbox_url == 'https://sbox.mercadopago.com/init_point' |
| 180 | + assert preference.reference == 'ref-123' |
| 181 | + assert preference.owner == account |
0 commit comments