def test_filter_expression(self): eventisc.init_default_app(listeners=[ { "kind": "test", "event_name": "myevent", "foo": 23, "filter": { "kind": "expr", "expr": "event_data['fii'] > 10" } }, ]) app = eventisc.get_current_app() assert len(app.listeners) == 1 assert isinstance(app.listeners[0], MyTestListener) assert app.listeners[0].foo == 23 count = eventisc.trigger("myevent", {"fii": 34}) assert count == 1 app.listeners[0].mock.assert_called_once_with("myevent", {"fii": 34}) app.listeners[0].mock.reset_mock() count = eventisc.trigger("myevent", {"fii": 9}) assert count == 0 app.listeners[0].mock.assert_not_called()
def test_http_listener(self): app = eventisc.init_default_app(listeners=[ { "event_name_regex": ".*", "kind": "http", "url": "http://example.com/notify/{event_name}/?{query_string}", "query_kwargs": {"fifi": "{event_data['foo']}"}, "data": {"foo": "{event_data['foo']}", "fofofo": "Esto esta {event_data[foo]}", "bar": 3, "barcito": "{event_data['bar']}"}, "requests_kwargs": {"headers": {"X-Foo": "sss"}}, } ]) responses.add( responses.POST, "http://example.com/notify/something/?fifi=FFOO", ) app.trigger('something', {"foo": "FFOO", "bar": 43}) request = responses.calls[0].request assert json.loads(request.body) == { "foo": "FFOO", "fofofo": "Esto esta FFOO", "bar": 3, "barcito": 43 } assert request.headers["X-Foo"] == "sss"
def test_init_manual(self): my_listener = MyTestListener("testapp.myevent") app = eventisc.init_default_app("testapp.", [my_listener]) assert app is not None assert len(app.listeners) == 1 eventisc.trigger("fooevent", {"fii": "bar"}) my_listener.mock.assert_not_called() eventisc.trigger("myevent", {"fii": "bar"}) my_listener.mock.assert_called_once_with("testapp.myevent", {"fii": "bar"})
def test_dry_run(self): eventisc.init_default_app(listeners=[ { "kind": "test", "event_name": "myevent", "foo": 23 }, ]) app = eventisc.get_current_app() assert len(app.listeners) == 1 assert isinstance(app.listeners[0], MyTestListener) assert app.listeners[0].foo == 23 with mock.patch.object(eventisc.logger, "info", wraps=eventisc.logger.info) as info_log: count = eventisc.trigger("myevent", {"fii": 34}) assert count == 1 app.listeners[0].mock.assert_not_called() info_log.assert_called_once_with( 'EVENTISC_DRYRUN=Y _do_notify(%s, %s)', 'myevent', {'fii': 34})
def test_celery_default_kwargs(self, celery_mock): app = eventisc.init_default_app(listeners=[ { "event_name_regex": ".*", "kind": "celery", "queue": "cola", "task_name": "event_receiver", } ]) app.trigger('something', {"foo": "FFOO", "bar": 43}) celery_mock.send_task.assert_called_once_with( 'event_receiver', args=[], kwargs={'event_name': 'something', "event_data": {"foo": "FFOO", "bar": 43}}, queue="cola", )
def test_init_from_dict(self): eventisc.init_default_app(listeners=[ { "kind": "test", "event_name": "envname.myevent", "foo": 23 }, { "kind": "test", "event_name_regex": ".*_created", "foo": 45 }, ]) app = eventisc.get_current_app() assert app.name_prefix == "envname." assert len(app.listeners) == 2 assert isinstance(app.listeners[0], MyTestListener) assert app.listeners[0].foo == 23 assert app.listeners[1].foo == 45 count = eventisc.trigger("fooevent", {"fii": "bar"}) assert count == 0 count = eventisc.trigger("myevent", {"fii": "bar"}) assert count == 1 app.listeners[0].mock.assert_called_once_with("envname.myevent", {"fii": "bar"}) app.listeners[1].mock.assert_not_called() app.listeners[0].mock.reset_mock() count = eventisc.trigger("bar_created", {"bar_name": "Moe's"}) assert count == 1 app.listeners[0].mock.assert_not_called() app.listeners[1].mock.assert_called_once_with("envname.bar_created", {"bar_name": "Moe's"})
def test_celery_listener(self, celery_mock): app = eventisc.init_default_app(listeners=[ { "event_name_regex": ".*", "kind": "celery", "queue": "cola", "task_name": "{event_name}", "task_kwargs": {"foo": "{event_data['foo']}", "fofofo": "Esto esta {event_data[foo]}", "bar": 3, "barcito": "{event_data['bar']}"}, "send_task_kargs": {"priority": 3, "serializer": "json"}, } ]) app.trigger('something', {"foo": "FFOO", "bar": 43}) celery_mock.send_task.assert_called_once_with( 'something', args=[], kwargs={'foo': 'FFOO', 'fofofo': 'Esto esta FFOO', 'bar': 3, 'barcito': 43}, priority=3, queue='cola', serializer='json' )
def test_auth_param(self): app = eventisc.init_default_app(listeners=[ { "event_name_regex": ".*", "kind": "http", "url": "http://example.com/notify/{event_name}/", "data": {"foo": "{event_data['foo']}"}, "requests_kwargs": {"auth": ["myuser", "mypassword"]}, } ]) responses.add( responses.POST, "http://example.com/notify/something_else/", ) app.trigger('something_else', {"foo": "bar"}) request = responses.calls[0].request assert json.loads(request.body) == { "foo": "bar" } assert request.headers["Authorization"] == "Basic bXl1c2VyOm15cGFzc3dvcmQ="