async def test_lexicon_when(patch, story, async_mock, service_name): ss = StreamingService(name='name', command='command', container_name='container_name', hostname='hostname') if service_name == 'unknown_service': ss = 'foo' line = { LineConstants.service: 'http' } story.context = { 'http': ss } patch.object(story, 'next_block') patch.object(Services, 'when', new=async_mock()) patch.object(Lexicon, 'line_number_or_none') if service_name == 'unknown_service': with pytest.raises(StoryscriptError): await Lexicon.when(story.logger, story, line) else: ret = await Lexicon.when(story.logger, story, line) story.next_block.assert_called_with(line) Lexicon.line_number_or_none.assert_called_with( story.next_block.return_value) assert ret == Lexicon.line_number_or_none.return_value
async def test_unsubscribe_all(patch, app, async_mock, magic, response_code): streaming_service = StreamingService('alpine', 'echo', 'alpine-1', 'alpine.com') payload = {'sub_body': {'payload': True}} app.add_subscription('sub_id', streaming_service, 'event_name', payload) app.add_subscription('sub_id_with_no_unsub', streaming_service, 'foo', payload) app.services = { 'alpine': { ServiceConstants.config: { 'actions': { 'echo': { 'events': { 'event_name': { 'http': { 'unsubscribe': { 'port': 28, 'path': '/unsub' } } }, 'foo': { 'http': {} } } } } } } } res = magic() res.code = response_code patch.object(HttpUtils, 'fetch_with_retry', new=async_mock(return_value=res)) patch.init(AsyncHTTPClient) client = AsyncHTTPClient() await app.unsubscribe_all() url = 'http://alpine.com:28/unsub' expected_kwargs = { 'method': 'POST', 'body': json.dumps(payload['sub_body']), 'headers': { 'Content-Type': 'application/json; charset=utf-8' } } HttpUtils.fetch_with_retry.mock.assert_called_with(3, app.logger, url, client, expected_kwargs) if response_code != 200: app.logger.error.assert_called_once()
def test_safe_type_streaming_service(): assert isinstance( TypeUtils.safe_type( StreamingService(name='name', command='command', container_name='container_name', hostname='hostname')), SafeStreamingService)
def test_http_data_encoder(patch): patch.object(base64, 'b64encode', return_value=b'dg==') file_json = { 'name': 'name', 'body': 'body', 'filename': 'filename', 'content_type': 'content_type' } patch.object(FileFormField, '_asdict', return_value=file_json) form_field_json = { 'name': 'name', 'body': 'body' } patch.object(FormField, '_asdict', return_value=form_field_json) patch.object(CaseInsensitiveDict, 'items', return_value=[ ('key', 'value') ]) obj = { 'file': FileFormField( name='name', body='body', filename='filename', content_type='content_type' ), 'field': FormField( name='name', body='body' ), 'key': b'v', 'casedict': CaseInsensitiveDict(data={ 'key': 'value' }), 'regex': re.compile('/foo/i'), 'streaming_service': StreamingService( name='hello', command='world', container_name='container_name', hostname='hostname' ) } json_str = json.dumps(obj, cls=HttpDataEncoder) assert json_str == json.dumps({ 'file': file_json, 'field': form_field_json, 'key': 'dg==', 'casedict': { 'key': 'value' }, 'regex': '/foo/i', 'streaming_service': { 'name': 'hello', 'command': 'world' } }) FormField._asdict.assert_called() FileFormField._asdict.assert_called() base64.b64encode.assert_called_with(b'v') CaseInsensitiveDict.items.assert_called()
async def test_when(patch, story, async_mock, service_name): line = { 'ln': '10', LineConstants.service: service_name, LineConstants.command: 'updates', 'args': [ { '$OBJECT': 'argument', 'name': 'foo', 'argument': { '$OBJECT': 'string', 'string': 'bar' } } ] } story.app.services = { service_name: { ServiceConstants.config: { 'actions': { 'time-server': { 'events': { 'updates': { 'http': { 'port': 2000, 'subscribe': { 'method': 'post', 'path': '/sub' } }, 'arguments': { 'foo': {'required': True} } } } } } } } } story.name = 'my_event_driven_story.story' story.app.config.ENGINE_HOST = 'localhost' story.app.config.ENGINE_PORT = 8000 story.app.config.ASYNCY_SYNAPSE_HOST = 'localhost' story.app.config.ASYNCY_SYNAPSE_PORT = 9000 story.app.app_id = 'my_fav_app' story.app.app_dns = 'my_apps_hostname' streaming_service = StreamingService(service_name, 'time-server', 'asyncy--foo-1', 'foo.com') story.context = { service_name: streaming_service } expected_sub_url = 'http://foo.com:2000/sub' expected_url = f'http://{story.app.config.ASYNCY_SYNAPSE_HOST}:' \ f'{story.app.config.ASYNCY_SYNAPSE_PORT}' \ f'/subscribe' expected_body = { 'sub_id': 'my_guid_here', 'sub_url': expected_sub_url, 'sub_method': 'POST', 'sub_body': { 'endpoint': f'http://localhost:8000/story/event?' f'story={story.name}&block={line["ln"]}' f'&app=my_fav_app', 'data': { 'foo': 'bar' }, 'event': 'updates', 'id': 'my_guid_here' }, 'pod_name': streaming_service.container_name, 'app_id': story.app.app_id } if service_name == 'http': expected_body['sub_body']['data']['host'] = story.app.app_dns patch.object(uuid, 'uuid4', return_value='my_guid_here') expected_kwargs = { 'method': 'POST', 'body': json.dumps(expected_body), 'headers': {'Content-Type': 'application/json; charset=utf-8'}, 'request_timeout': 120 } patch.init(AsyncHTTPClient) patch.object(story, 'next_block') patch.object(story.app, 'add_subscription') patch.object(story, 'argument_by_name', return_value='bar') http_res = Mock() http_res.code = 204 patch.object(HttpUtils, 'fetch_with_retry', new=async_mock(return_value=http_res)) ret = await Services.when(streaming_service, story, line) client = AsyncHTTPClient() HttpUtils.fetch_with_retry.mock.assert_called_with( 100, story.logger, expected_url, client, expected_kwargs) story.app.add_subscription.assert_called_with( 'my_guid_here', story.context[service_name], 'updates', expected_body) assert ret is None http_res.code = 400 with pytest.raises(StoryscriptError): await Services.when(streaming_service, story, line)