-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_postmen.py
276 lines (217 loc) · 8.23 KB
/
test_postmen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import pytest
from django.core.urlresolvers import reverse
from mock import patch, MagicMock
from model_mommy import mommy
from . import get_testdata
def make_carrier_service():
tnt = mommy.make('Carrier', name='TNT', config={})
tnt_express = mommy.make('CarrierService', carrier=tnt, name='TNT Express')
mommy.make('CarrierMapping', carrier=tnt, provider='postmen', step='process', link_name='')
return tnt, tnt_express
def make_shipment(status='pending', **kwargs):
carrier, service = make_carrier_service()
frm = mommy.make('Address', country='HK')
to = mommy.make('Address', country='CA')
shipment = mommy.make('Shipment',
status=status,
carrier_service=service.name,
source=frm,
destination=to,
**kwargs)
return shipment
@pytest.mark.django_db
@patch('shipment.views.request_label')
def test_api_shipment_process_async(celery_task, client):
"""
API call creates a shipment,
changes its status to in_progress
and calls celery task
"""
from shipment.models import Shipment
user = mommy.make('User', username='test_user')
client.force_authenticate(user=user)
carrier, service = make_carrier_service()
data = {
"async": True,
"reference": "REF1",
"source": {
"country": "HKG",
},
"destination": {
"country": "CAN",
},
"packages": [{
"width": 2,
"height": 2,
"length": 2,
"weight": "2.000",
"items": [{
"item_type": "a thing",
"sku": "fk34kf",
"description": "thingy thing",
"manufacturer": "AU",
"length": 2,
"width": "2.00",
"height": 2,
"weight": 2.0,
"customs_value": {
"amount": 0.0,
"currency": "USD"
}
}],
}],
"carrier_service": service.name,
"tax_state": "shipper",
}
assert Shipment.objects.count() == 0
resp = client.post(reverse('api:shipment-process'),
data,
format='json')
print(resp.status_code)
print(resp)
assert Shipment.objects.count() == 1
shipment = Shipment.objects.first()
assert shipment.status == 'in_progress'
celery_task.delay.assert_called_once_with(shipment.id)
@pytest.mark.django_db
@patch('carrier.providers.postmen.service.Postmen')
def test_ask_postmen_to_create_label(postmen):
"""
This task calls Postmen API with async=true flag,
stores id field of the Postmen response
and keeps shipment status 'in_progress'
"""
from carrier.providers.postmen.models import PostmenLabelRequest
from shipment.models import Shipment
from shipment.tasks import request_label
api = postmen.return_value
api.create.return_value = {"id": "1-2-3-4-5"}
#api.create.side_effect = Exception('OMG!')
shipment = make_shipment(status='in_progress')
request_label(shipment.id)
assert api.create.call_count == 1
args, kwargs = api.create.call_args
assert args[1]['async'] == True
label_request = PostmenLabelRequest.objects.get(shipment=shipment)
assert label_request.label_id == '1-2-3-4-5'
assert Shipment.objects.get(pk=shipment.pk).status == 'in_progress'
@pytest.mark.django_db
@patch('shipment.views.download_and_store_label')
def test_postmen_callback(celery_task, client):
"""This view does nothing - it just responds and calls a celery task"""
data = get_testdata('postmen-create-label-hook-resp.json')
shipment = make_shipment()
mommy.make('PostmenLabelRequest', label_id=data['data']['id'], shipment=shipment)
resp = client.post(reverse('postmen-callback'),
data,
format='json')
print(resp)
assert resp.status_code == 200
assert resp.data == {}
celery_task.delay.assert_called_once_with(shipment.id)
@pytest.mark.django_db
@patch('carrier.providers.postmen.service.requests')
@patch('carrier.providers.postmen.service.Postmen')
def test_download_and_store_postmen_label(postmen, requests, settings, tmpdir):
"""
This task gets a label info from postmen, and then downloads and stores label
"""
from shipment.models import Shipment
from shipment.tasks import download_and_store_label
settings.MEDIA_ROOT = str(tmpdir)
postmen_resp = get_testdata('postmen-create-label-hook-resp.json')['data']
api = postmen.return_value
api.get.return_value = postmen_resp
label_download_resp = MagicMock()
label_download_resp.status_code = 200
label_download_resp.content = 'SomePDFContent'
requests.get.return_value = label_download_resp
label_id = '1-2-3-4-5'
shipment = make_shipment(status='in_progress')
mommy.make('PostmenLabelRequest', shipment=shipment, label_id=label_id)
download_and_store_label(shipment.id)
api.get.assert_called_once_with('labels', label_id)
requests.get.assert_called_once_with(postmen_resp['files']['label']['url'])
assert shipment.documents.count() == 1
label = shipment.documents.first()
assert label.document.read() == 'SomePDFContent'
assert Shipment.objects.get(pk=shipment.pk).status == 'processed'
@pytest.mark.django_db
@patch('shipment.tasks.requests')
def test_call_floship_callback(requests):
from shipment.serializers import ShipmentSerializer
from shipment.tasks import call_floship_callback
user = mommy.make('User', username='test_user')
settings = mommy.make('UserSettings', user=user, callback_url='http://someurl.com')
shipment = make_shipment(user=user)
call_floship_callback(shipment.id)
requests.post.assert_called_once_with(settings.callback_url,
json=ShipmentSerializer(shipment).data)
@pytest.mark.django_db
@patch('carrier.providers.postmen.service.requests')
@patch('carrier.providers.postmen.service.Postmen')
def test_api_shipment_process(postmen, requests, client):
from shipment.models import Shipment
user = mommy.make('User', username='test_user')
client.force_authenticate(user=user)
carrier, service = make_carrier_service()
data = {
"async": False,
"reference": "REF1",
"source": {
"country": "HK",
},
"destination": {
"country": "CA",
},
"packages": [{
"width": 2,
"height": 2,
"length": 2,
"weight": "2.000",
"items": [{
"item_type": "a thing",
"sku": "fk34kf",
"description": "thingy thing",
"manufacturer": "AU",
"length": 2,
"width": "2.00",
"height": 2,
"weight": 2.0,
"customs_value": {
"amount": 0.0,
"currency": "USD"
}
}],
}],
"carrier_service": service.name,
"tax_state": "shipper",
}
postmen_resp = {"tracking_numbers": ["123"],
"files": {
"label": {
"paper_size": "a4",
"url": "https://some/path/to/pdf",
"file_type": "pdf"
}
}}
api = postmen.return_value
api.create.return_value = postmen_resp
label_download_resp = MagicMock()
label_download_resp.status_code = 200
label_download_resp.content = 'SomePDFContent'
requests.get.return_value = label_download_resp
assert Shipment.objects.count() == 0
resp = client.post(reverse('api:shipment-process'),
data,
format='json')
print(resp.status_code)
print(resp)
assert resp.status_code == 201
assert len(resp.data['documents']) == 1
assert Shipment.objects.count() == 1
shipment = Shipment.objects.first()
assert shipment.status == 'processed'
assert shipment.documents.count() == 1
label = shipment.documents.first()
assert label.document.read() == 'SomePDFContent'