3 |
3 |
import pytest
|
4 |
4 |
import mock
|
5 |
5 |
import requests
|
|
6 |
import json
|
6 |
7 |
|
7 |
8 |
from django.template.defaultfilters import slugify
|
8 |
9 |
from django.contrib.auth.models import User
|
... | ... | |
102 |
103 |
assert mock_get.call_args[1]['without_user']
|
103 |
104 |
assert 'user' not in mock_get.call_args[1]
|
104 |
105 |
|
|
106 |
mock_get.side_effect = requests.RequestException
|
|
107 |
assert cell.get_newsletters() == []
|
|
108 |
|
|
109 |
|
105 |
110 |
@mock.patch('combo.apps.newsletters.models.requests.get')
|
106 |
111 |
def test_get_newsletters_by_unrestricted_transports(mock_get, cell):
|
107 |
112 |
cell.transports_restrictions = ''
|
... | ... | |
148 |
153 |
assert cell.get_subscriptions(user) == expected_subscriptions
|
149 |
154 |
assert mock_get.call_args[1]['user'].email == USER_EMAIL
|
150 |
155 |
|
|
156 |
mock_get.side_effect = requests.RequestException
|
|
157 |
assert cell.get_subscriptions(user) == []
|
|
158 |
|
|
159 |
|
151 |
160 |
@mock.patch('combo.utils.requests_wrapper.RequestsSession.send')
|
152 |
161 |
def test_get_subscriptions_signature_check(mock_send, cell, user):
|
153 |
162 |
restrictions = ('mail', 'sms')
|
... | ... | |
167 |
176 |
assert check_query(url.split('?', 1)[-1], 'combo')
|
168 |
177 |
|
169 |
178 |
|
170 |
|
def mocked_requests_connection_error(*args, **kwargs):
|
171 |
|
raise requests.ConnectionError()
|
172 |
|
|
173 |
|
@mock.patch('combo.apps.newsletters.models.requests.post',
|
174 |
|
side_effect=mocked_requests_connection_error)
|
|
179 |
@mock.patch('combo.apps.newsletters.models.requests.post')
|
175 |
180 |
def test_failed_set_subscriptions(mock_post, cell, user):
|
176 |
181 |
restrictions = ('sms', 'mail')
|
177 |
182 |
cell.transports_restrictions = ','.join(restrictions)
|
... | ... | |
179 |
184 |
{'id': '7', 'transports': [{'id': 'sms', 'text': 'sms'}]},
|
180 |
185 |
{'id': '8', 'transports': [{'id': 'sms', 'text': 'sms'},
|
181 |
186 |
{'id': 'mail', 'text': 'mail'}]}]
|
|
187 |
mock_post.side_effect = requests.ConnectionError
|
182 |
188 |
with pytest.raises(SubscriptionsSaveError):
|
183 |
189 |
cell.set_subscriptions(subscriptions, user, uuid='useruuid')
|
184 |
190 |
|
185 |
|
@mock.patch('combo.apps.newsletters.models.requests.post')
|
186 |
|
def test_set_subscriptions_with_no_uuid(mocked_post, cell, user):
|
|
191 |
mock_post.side_effect = requests.HTTPError(response=mock.MagicMock())
|
|
192 |
with pytest.raises(SubscriptionsSaveError):
|
|
193 |
cell.set_subscriptions(subscriptions, user, uuid='useruuid')
|
|
194 |
|
|
195 |
|
|
196 |
def test_set_subscriptions_with_no_uuid(cell, user):
|
187 |
197 |
restrictions = ('sms', 'mail')
|
188 |
198 |
cell.transports_restrictions = ','.join(restrictions)
|
189 |
199 |
subscriptions = [{'id': '1', 'transports': [{'id': 'mail', 'text': 'mail'}]},
|
190 |
200 |
{'id': '8', 'transports': [{'id': 'sms', 'text': 'sms'},
|
191 |
201 |
{'id': 'mail', 'text': 'mail'}]}]
|
192 |
|
mock_json = mock.Mock()
|
193 |
|
mock_json.json.return_value = {'err': 0, 'data': True}
|
194 |
202 |
with pytest.raises(SubscriptionsSaveError):
|
195 |
203 |
cell.set_subscriptions(subscriptions, user)
|
196 |
204 |
|
|
205 |
|
197 |
206 |
@mock.patch('combo.apps.newsletters.models.requests.post')
|
198 |
207 |
def test_set_subscriptions(mock_post, cell, user):
|
199 |
208 |
restrictions = ('sms', 'mail')
|
... | ... | |
230 |
239 |
args, kwargs = mock_get.call_args
|
231 |
240 |
assert kwargs['params'] == {'uuid': 'nameid', 'mobile': '0607080900'}
|
232 |
241 |
|
|
242 |
|
233 |
243 |
def mocked_requests_get(*args, **kwargs):
|
234 |
244 |
url = args[0]
|
235 |
|
class MockResponse:
|
236 |
|
def __init__(self, json_data):
|
237 |
|
self.json_data = json_data
|
238 |
245 |
|
239 |
|
def ok(self):
|
240 |
|
return True
|
|
246 |
class MockResponse(mock.Mock):
|
|
247 |
status_code = 200
|
241 |
248 |
|
242 |
249 |
def json(self):
|
243 |
|
return self.json_data
|
|
250 |
return json.loads(self.content)
|
244 |
251 |
|
245 |
252 |
if 'newsletters' in url:
|
246 |
|
return MockResponse({'data': NEWSLETTERS})
|
|
253 |
return MockResponse(content=json.dumps({'data': NEWSLETTERS}))
|
247 |
254 |
else:
|
248 |
|
return MockResponse({'data': SUBSCRIPTIONS})
|
|
255 |
return MockResponse(content=json.dumps({'data': SUBSCRIPTIONS}))
|
|
256 |
|
249 |
257 |
|
250 |
258 |
@mock.patch('combo.apps.newsletters.models.requests.get',
|
251 |
259 |
side_effect=mocked_requests_get)
|
252 |
|
-
|