class test_Subscriber(Case): def setUp(self): self.user = get_user_model()(username='******') self.subscriber = Subscriber( event='foo.created', user=self.user, url='http://example.com', ) def test_str(self): self.assertTrue(text_t(self.subscriber)) def test_as_dict(self): self.assertDictEqual(self.subscriber.as_dict(), { 'event': self.subscriber.event, 'user': self.subscriber.user.pk, 'url': self.subscriber.url, 'content_type': self.subscriber.content_type, 'hmac_secret': self.subscriber.hmac_secret, 'hmac_digest': self.subscriber.hmac_digest, }) def test_from_dict__arg(self): x = Subscriber.from_dict('http://e.com', event='foo.bar') self.assertIsInstance(x, Subscriber) self.assertEqual(x.url, 'http://e.com') self.assertEqual(x.event, 'foo.bar') def test_from_dict__dict(self): x = Subscriber.from_dict(url='http://e.com', event='foo.bar') self.assertIsInstance(x, Subscriber) self.assertEqual(x.url, 'http://e.com') self.assertEqual(x.event, 'foo.bar')
class test_dispatch_request: def setup(self): self.session = Mock(name='session') self.user, _ = get_user_model().objects.get_or_create( username='******', password='******', email='*****@*****.**', ) self.subscriber = Subscriber( event='foo.created', url='http://example.com', ) self.subscriber2 = Subscriber( event='foo.changed', url='http://example.com', user=self.user, ) self.req = Request(self.subscriber.event, 'a', 501, self.subscriber, timeout=3.03) self.req2 = Request(self.subscriber2.event, 'a', 501, self.subscriber2, timeout=3.03) @pytest.fixture() def app_or_default(self, patching): return patching('thorn.tasks.app_or_default') def test_success(self, app_or_default): _Request = app_or_default().Request dispatch_request(session=self.session, **self.req.as_dict()) subscriber_dict = self.subscriber.as_dict() subscriber_dict.pop('user', None) app_or_default().Subscriber.assert_called_once_with(**subscriber_dict) _Request.assert_called_once_with( self.req.event, self.req.data, self.req.sender, app_or_default().Subscriber(), id=self.req.id, timeout=self.req.timeout, retry=self.req.retry, retry_max=self.req.retry_max, retry_delay=self.req.retry_delay, recipient_validators=DEFAULT_RECIPIENT_VALIDATORS, allow_keepalive=True, on_error=None, on_success=None, on_timeout=None) _Request().dispatch.assert_called_once_with(session=self.session, propagate=_Request().retry) def test_when_keepalive_disabled(self, app_or_default): _Request = app_or_default().Request self.req.allow_keepalive = False dispatch_request(session=self.session, **self.req.as_dict()) subscriber_dict = self.subscriber.as_dict() subscriber_dict.pop('user', None) app_or_default().Subscriber.assert_called_once_with(**subscriber_dict) _Request.assert_called_once_with( self.req.event, self.req.data, self.req.sender, app_or_default().Subscriber(), id=self.req.id, timeout=self.req.timeout, retry=self.req.retry, retry_max=self.req.retry_max, retry_delay=self.req.retry_delay, recipient_validators=DEFAULT_RECIPIENT_VALIDATORS, allow_keepalive=False, on_error=None, on_success=None, on_timeout=None) _Request().dispatch.assert_called_once_with(session=self.session, propagate=_Request().retry) def test_success__with_user(self, app_or_default): _Request = app_or_default().Request dispatch_request(session=self.session, **self.req2.as_dict()) subscriber_dict = self.subscriber2.as_dict() subscriber_dict.pop('user', None) app_or_default().Subscriber.assert_called_once_with(**subscriber_dict) _Request.assert_called_once_with( self.req2.event, self.req2.data, self.req2.sender, app_or_default().Subscriber(), id=self.req2.id, timeout=self.req2.timeout, retry=self.req2.retry, retry_max=self.req2.retry_max, retry_delay=self.req2.retry_delay, recipient_validators=DEFAULT_RECIPIENT_VALIDATORS, allow_keepalive=True, on_error=None, on_success=None, on_timeout=None) _Request().dispatch.assert_called_once_with(session=self.session, propagate=_Request().retry) def test_connection_error(self, task_retry, app_or_default): _Request = app_or_default().Request _Request.return_value.connection_errors = (ValueError, ) _Request.return_value.timeout_errors = () exc = _Request.return_value.dispatch.side_effect = ValueError(10) task_retry.side_effect = exc with pytest.raises(ValueError): dispatch_request(session=self.session, **self.req.as_dict()) task_retry.assert_called_with( exc=exc, max_retries=_Request().retry_max, countdown=_Request().retry_delay, ) def test_connection_error__retry_disabled(self, task_retry, app_or_default): _Request = app_or_default().Request _Request.return_value.connection_errors = (ValueError, ) _Request.return_value.timeout_errors = () _Request.return_value.retry = False exc = _Request.return_value.dispatch.side_effect = ValueError(11) task_retry.side_effect = exc self.req.retry = False with pytest.raises(ValueError): dispatch_request(session=self.session, **self.req.as_dict()) task_retry.assert_not_called()
class test_dispatch_request: def setup(self): self.session = Mock(name='session') self.user, _ = get_user_model().objects.get_or_create( username='******', password='******', email='*****@*****.**', ) self.subscriber = Subscriber( event='foo.created', url='http://example.com', ) self.subscriber2 = Subscriber( event='foo.changed', url='http://example.com', user=self.user, ) self.req = Request( self.subscriber.event, 'a', 501, self.subscriber, timeout=3.03) self.req2 = Request( self.subscriber2.event, 'a', 501, self.subscriber2, timeout=3.03) @pytest.fixture() def app_or_default(self, patching): return patching('thorn.tasks.app_or_default') def test_success(self, app_or_default): _Request = app_or_default().Request dispatch_request(session=self.session, **self.req.as_dict()) subscriber_dict = self.subscriber.as_dict() subscriber_dict.pop('user', None) app_or_default().Subscriber.assert_called_once_with(**subscriber_dict) _Request.assert_called_once_with( self.req.event, self.req.data, self.req.sender, app_or_default().Subscriber(), id=self.req.id, timeout=self.req.timeout, retry=self.req.retry, retry_max=self.req.retry_max, retry_delay=self.req.retry_delay, recipient_validators=DEFAULT_RECIPIENT_VALIDATORS, allow_keepalive=True, ) _Request().dispatch.assert_called_once_with( session=self.session, propagate=_Request().retry) def test_when_keepalive_disabled(self, app_or_default): _Request = app_or_default().Request self.req.allow_keepalive = False dispatch_request(session=self.session, **self.req.as_dict()) subscriber_dict = self.subscriber.as_dict() subscriber_dict.pop('user', None) app_or_default().Subscriber.assert_called_once_with(**subscriber_dict) _Request.assert_called_once_with( self.req.event, self.req.data, self.req.sender, app_or_default().Subscriber(), id=self.req.id, timeout=self.req.timeout, retry=self.req.retry, retry_max=self.req.retry_max, retry_delay=self.req.retry_delay, recipient_validators=DEFAULT_RECIPIENT_VALIDATORS, allow_keepalive=False, ) _Request().dispatch.assert_called_once_with( session=self.session, propagate=_Request().retry) def test_success__with_user(self, app_or_default): _Request = app_or_default().Request dispatch_request(session=self.session, **self.req2.as_dict()) subscriber_dict = self.subscriber2.as_dict() subscriber_dict.pop('user', None) app_or_default().Subscriber.assert_called_once_with(**subscriber_dict) _Request.assert_called_once_with( self.req2.event, self.req2.data, self.req2.sender, app_or_default().Subscriber(), id=self.req2.id, timeout=self.req2.timeout, retry=self.req2.retry, retry_max=self.req2.retry_max, retry_delay=self.req2.retry_delay, recipient_validators=DEFAULT_RECIPIENT_VALIDATORS, allow_keepalive=True, ) _Request().dispatch.assert_called_once_with( session=self.session, propagate=_Request().retry) def test_connection_error(self, task_retry, app_or_default): _Request = app_or_default().Request _Request.return_value.connection_errors = (ValueError,) _Request.return_value.timeout_errors = () exc = _Request.return_value.dispatch.side_effect = ValueError(10) task_retry.side_effect = exc with pytest.raises(ValueError): dispatch_request(session=self.session, **self.req.as_dict()) task_retry.assert_called_with( exc=exc, max_retries=_Request().retry_max, countdown=_Request().retry_delay, ) def test_connection_error__retry_disabled( self, task_retry, app_or_default): _Request = app_or_default().Request _Request.return_value.connection_errors = (ValueError,) _Request.return_value.timeout_errors = () _Request.return_value.retry = False exc = _Request.return_value.dispatch.side_effect = ValueError(11) task_retry.side_effect = exc self.req.retry = False with pytest.raises(ValueError): dispatch_request(session=self.session, **self.req.as_dict()) task_retry.assert_not_called()
class test_dispatch_request(EventCase): def setUp(self): EventCase.setUp(self) self.session = Mock(name='session') self.user, _ = get_user_model().objects.get_or_create( username='******', password='******', email='*****@*****.**', ) self.subscriber = Subscriber( event='foo.created', url='http://example.com', ) self.subscriber2 = Subscriber( event='foo.changed', url='http://example.com', user=self.user, ) self.req = Request(self.subscriber.event, 'a', 501, self.subscriber, timeout=3.03) self.req2 = Request(self.subscriber2.event, 'a', 501, self.subscriber2, timeout=3.03) @patch('thorn.tasks.current_app') def test_success(self, current_app): _Request = current_app().Request dispatch_request(session=self.session, **self.req.as_dict()) subscriber_dict = self.subscriber.as_dict() subscriber_dict.pop('user', None) current_app().Subscriber.assert_called_once_with(**subscriber_dict) _Request.assert_called_once_with( self.req.event, self.req.data, self.req.sender, current_app().Subscriber(), id=self.req.id, timeout=self.req.timeout, retry=self.req.retry, retry_max=self.req.retry_max, retry_delay=self.req.retry_delay, recipient_validators=DEFAULT_RECIPIENT_VALIDATORS, ) _Request().dispatch.assert_called_once_with(session=self.session, propagate=_Request().retry) @patch('thorn.tasks.current_app') def test_success__with_user(self, current_app): _Request = current_app().Request dispatch_request(session=self.session, **self.req2.as_dict()) subscriber_dict = self.subscriber2.as_dict() subscriber_dict.pop('user', None) current_app().Subscriber.assert_called_once_with(**subscriber_dict) _Request.assert_called_once_with( self.req2.event, self.req2.data, self.req2.sender, current_app().Subscriber(), id=self.req2.id, timeout=self.req2.timeout, retry=self.req2.retry, retry_max=self.req2.retry_max, retry_delay=self.req2.retry_delay, recipient_validators=DEFAULT_RECIPIENT_VALIDATORS, ) _Request().dispatch.assert_called_once_with(session=self.session, propagate=_Request().retry) @patch('thorn.tasks.current_app') @patch('celery.app.task.Task.retry') def test_connection_error(self, retry, current_app): _Request = current_app().Request _Request.return_value.connection_errors = (ValueError, ) _Request.return_value.timeout_errors = () exc = _Request.return_value.dispatch.side_effect = ValueError(10) retry.side_effect = exc with self.assertRaises(ValueError): dispatch_request(session=self.session, **self.req.as_dict()) retry.assert_called_with( exc=exc, max_retries=_Request().retry_max, countdown=_Request().retry_delay, ) @patch('thorn.tasks.current_app') @patch('celery.app.task.Task.retry') def test_connection_error__retry_disabled(self, retry, current_app): _Request = current_app().Request _Request.return_value.connection_errors = (ValueError, ) _Request.return_value.timeout_errors = () _Request.return_value.retry = False exc = _Request.return_value.dispatch.side_effect = ValueError(11) retry.side_effect = exc self.req.retry = False with self.assertRaises(ValueError): dispatch_request(session=self.session, **self.req.as_dict()) retry.assert_not_called()