async def test_list_files(event_loop): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) envelope = await pubnub.list_files().channel(CHANNEL).future() assert isinstance(envelope.result, PNGetFilesResult) assert envelope.result.count == 23 pubnub.stop()
def test_multiple_channel_groups_with_auth(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "my_uuid" gr1 = "test-pam-asyncio-cg1" gr2 = "test-pam-asyncio-cg2" auth = "test-pam-asyncio-auth" env = (yield from pubnub.grant() .channel_groups([gr1, gr2]) .write(True) .read(True) .auth_keys(auth) .future()) assert isinstance(env.result, PNAccessManagerGrantResult) assert env.result.groups[gr1].auth_keys[auth].read_enabled is True assert env.result.groups[gr2].auth_keys[auth].read_enabled is True assert env.result.groups[gr1].auth_keys[auth].write_enabled is True assert env.result.groups[gr2].auth_keys[auth].write_enabled is True assert env.result.groups[gr1].auth_keys[auth].manage_enabled is False assert env.result.groups[gr2].auth_keys[auth].manage_enabled is False env = (yield from pubnub.audit() .channel_groups([gr1, gr2]) .future()) assert isinstance(env.result, PNAccessManagerAuditResult) assert env.result.groups[gr1].auth_keys[auth].read_enabled is True assert env.result.groups[gr2].auth_keys[auth].read_enabled is True assert env.result.groups[gr1].auth_keys[auth].write_enabled is True assert env.result.groups[gr2].auth_keys[auth].write_enabled is True assert env.result.groups[gr1].auth_keys[auth].manage_enabled is False assert env.result.groups[gr2].auth_keys[auth].manage_enabled is False pubnub.stop()
def test_success(event_loop): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) res = yield from pubnub.delete_messages().channel("my-ch").start(123).end(456).future() if res.status.is_error(): raise AssertionError()
async def test_multiple_channels(event_loop, sleeper=asyncio.sleep): pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) ch1 = 'test-where-now-asyncio-ch1' ch2 = 'test-where-now-asyncio-ch2' uuid = 'test-where-now-asyncio-uuid' pubnub.config.uuid = uuid callback = VCR599Listener(1) pubnub.add_listener(callback) pubnub.subscribe().channels([ch1, ch2]).execute() await callback.wait_for_connect() await sleeper(7) env = await pubnub.where_now() \ .uuid(uuid) \ .future() channels = env.result.channels assert len(channels) == 2 assert ch1 in channels assert ch2 in channels pubnub.unsubscribe().channels([ch1, ch2]).execute() await callback.wait_for_disconnect() pubnub.stop()
def test_publish_future(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) result = yield from pubnub.publish().message('hey').channel( 'blah').result() assert isinstance(result, PNPublishResult) pubnub.stop()
async def test_cg_subscribe_unsubscribe(event_loop, sleeper=asyncio.sleep): ch = "test-subscribe-asyncio-channel" gr = "test-subscribe-asyncio-group" pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) envelope = await pubnub.add_channel_to_channel_group().channel_group( gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 await sleeper(3) callback_messages = SubscribeListener() pubnub.add_listener(callback_messages) pubnub.subscribe().channel_groups(gr).execute() await callback_messages.wait_for_connect() pubnub.unsubscribe().channel_groups(gr).execute() await callback_messages.wait_for_disconnect() envelope = await pubnub.remove_channel_from_channel_group().channel_group( gr).channels(ch).future() assert envelope.status.original_response['status'] == 200 pubnub.stop()
def test_not_permitted(event_loop): pnconf = pnconf_pam_copy() pnconf.secret_key = None pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) yield from assert_server_side_error_yield(pubnub.publish().channel(ch).message("hey"), "HTTP Client Error (403") pubnub.stop()
def test_multiple_channel_groups_with_auth(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "my_uuid" gr1 = "test-pam-asyncio-cg1" gr2 = "test-pam-asyncio-cg2" auth = "test-pam-asyncio-auth" env = (yield from pubnub.grant() .channel_groups([gr1, gr2]) .write(True) .read(True) .auth_keys(auth) .future()) assert isinstance(env.result, PNAccessManagerGrantResult) assert env.result.groups[gr1].auth_keys[auth].read_enabled is True assert env.result.groups[gr2].auth_keys[auth].read_enabled is True assert env.result.groups[gr1].auth_keys[auth].write_enabled is True assert env.result.groups[gr2].auth_keys[auth].write_enabled is True assert env.result.groups[gr1].auth_keys[auth].manage_enabled is False assert env.result.groups[gr2].auth_keys[auth].manage_enabled is False assert env.result.groups[gr1].auth_keys[auth].delete_enabled is False assert env.result.groups[gr2].auth_keys[auth].delete_enabled is False pubnub.stop()
async def test_global_level(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "my_uuid" env = await pubnub.grant().write(True).read(True).future() assert isinstance(env.result, PNAccessManagerGrantResult) assert len(env.result.channels) == 0 assert len(env.result.groups) == 0 assert env.result.read_enabled is True assert env.result.write_enabled is True assert env.result.manage_enabled is False assert env.result.delete_enabled is False env = await pubnub.revoke().future() assert isinstance(env.result, PNAccessManagerGrantResult) assert len(env.result.channels) == 0 assert len(env.result.groups) == 0 assert env.result.read_enabled is False assert env.result.write_enabled is False assert env.result.manage_enabled is False assert env.result.delete_enabled is False pubnub.stop()
async def test_multiple_channels(event_loop): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) ch1 = 'test-state-asyncio-ch1' ch2 = 'test-state-asyncio-ch2' pubnub.config.uuid = 'test-state-asyncio-uuid' state = {"name": "Alex", "count": 5} env = await pubnub.set_state() \ .channels([ch1, ch2]) \ .state(state) \ .future() assert env.result.state['name'] == "Alex" assert env.result.state['count'] == 5 env = await pubnub.get_state() \ .channels([ch1, ch2]) \ .future() assert env.result.channels[ch1]['name'] == "Alex" assert env.result.channels[ch2]['name'] == "Alex" assert env.result.channels[ch1]['count'] == 5 assert env.result.channels[ch2]['count'] == 5 pubnub.stop()
async def test_super_call(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) ch = "channel-groups-torna|do-ch" gr = "channel-groups-torna|do-cg" pubnub.config.auth = "h.e|l%l,0" # add env = await pubnub.add_channel_to_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) # list env = await pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) # remove channel env = await pubnub.remove_channel_from_channel_group().channel_group( gr).channels(ch).future() assert isinstance(env.result, PNChannelGroupsRemoveChannelResult) # remove group env = await pubnub.remove_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveGroupResult) # list env = await pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) pubnub.stop()
async def test_error_invalid_key(event_loop): pnconf = pnconf_pam_copy() pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) await assert_server_side_error_yield(pubnub.publish().channel(ch).message("hey"), "Invalid Key") await pubnub.stop()
def test_single_channel_group(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "test-pam-asyncio-uuid" cg = "test-pam-asyncio-cg" env = (yield from pubnub.grant() .channel_groups(cg) .write(True) .read(True) .future()) assert isinstance(env.result, PNAccessManagerGrantResult) assert env.result.level == 'channel-group' assert env.result.groups[cg].read_enabled == 1 assert env.result.groups[cg].write_enabled == 1 assert env.result.groups[cg].manage_enabled == 0 env = (yield from pubnub.audit() .channel_groups(cg) .future()) assert isinstance(env.result, PNAccessManagerAuditResult) assert env.result.level == 'channel-group' assert env.result.groups[cg].read_enabled == 1 assert env.result.groups[cg].write_enabled == 1 assert env.result.groups[cg].manage_enabled == 0 pubnub.stop()
def test_single_channel_with_auth(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "test-pam-asyncio-uuid" ch = "test-pam-asyncio-ch" auth = "test-pam-asyncio-auth" env = (yield from pubnub.grant() .channels(ch) .write(True) .read(True) .auth_keys(auth) .future()) assert isinstance(env.result, PNAccessManagerGrantResult) assert env.result.channels[ch].auth_keys[auth].read_enabled == 1 assert env.result.channels[ch].auth_keys[auth].write_enabled == 1 assert env.result.channels[ch].auth_keys[auth].manage_enabled == 0 env = (yield from pubnub.audit() .channels(ch) .auth_keys(auth) .future()) assert isinstance(env.result, PNAccessManagerAuditResult) assert env.result.channels[ch].auth_keys[auth].read_enabled == 1 assert env.result.channels[ch].auth_keys[auth].write_enabled == 1 assert env.result.channels[ch].auth_keys[auth].manage_enabled == 0 pubnub.stop()
def test_multiple_channels(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "test-pam-asyncio-uuid" ch1 = "test-pam-asyncio-ch1" ch2 = "test-pam-asyncio-ch2" env = (yield from pubnub.grant() .channels([ch1, ch2]) .write(True) .read(True) .future()) assert isinstance(env.result, PNAccessManagerGrantResult) assert env.result.channels[ch1].read_enabled is True assert env.result.channels[ch2].read_enabled is True assert env.result.channels[ch1].write_enabled is True assert env.result.channels[ch2].write_enabled is True assert env.result.channels[ch1].manage_enabled is False assert env.result.channels[ch2].manage_enabled is False env = (yield from pubnub.audit() .channels([ch1, ch2]) .future()) assert isinstance(env.result, PNAccessManagerAuditResult) assert env.result.channels[ch1].read_enabled is True assert env.result.channels[ch2].read_enabled is True assert env.result.channels[ch1].write_enabled is True assert env.result.channels[ch2].write_enabled is True assert env.result.channels[ch1].manage_enabled is False assert env.result.channels[ch2].manage_enabled is False pubnub.stop()
async def test_single_channel_with_subscription(event_loop, sleeper=asyncio.sleep): pnconf = pnconf_sub_copy() pnconf.set_presence_timeout(12) pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) ch = 'test-state-asyncio-ch' pubnub.config.uuid = 'test-state-asyncio-uuid' state = {"name": "Alex", "count": 5} callback = VCR599Listener(1) pubnub.add_listener(callback) pubnub.subscribe().channels(ch).execute() await callback.wait_for_connect() await sleeper(20) env = await pubnub.set_state() \ .channels(ch) \ .state(state) \ .future() assert env.result.state['name'] == "Alex" assert env.result.state['count'] == 5 env = await pubnub.get_state() \ .channels(ch) \ .future() assert env.result.channels[ch]['name'] == "Alex" assert env.result.channels[ch]['count'] == 5 pubnub.unsubscribe().channels(ch).execute() await callback.wait_for_disconnect() pubnub.stop()
async def test_publish_envelope(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) envelope = await pubnub.publish().message('hey').channel('blah').future() assert isinstance(envelope, AsyncioEnvelope) assert not envelope.is_error() pubnub.stop()
def test_publish_envelope(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) envelope = yield from pubnub.publish().message('hey').channel('blah').future() assert isinstance(envelope, AsyncioEnvelope) assert not envelope.is_error() pubnub.stop()
def test_not_permitted(event_loop): pnconf = pnconf_pam_copy() pnconf.secret_key = None pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) yield from assert_server_side_error_yield(pubnub.publish().channel(ch).message("hey"), "HTTP Client Error (403") pubnub.stop()
def test_multiple_channels(event_loop): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) ch1 = 'test-state-asyncio-ch1' ch2 = 'test-state-asyncio-ch2' pubnub.config.uuid = 'test-state-asyncio-uuid' state = {"name": "Alex", "count": 5} env = yield from pubnub.set_state() \ .channels([ch1, ch2]) \ .state(state) \ .future() assert env.result.state['name'] == "Alex" assert env.result.state['count'] == 5 env = yield from pubnub.get_state() \ .channels([ch1, ch2]) \ .future() assert env.result.channels[ch1]['name'] == "Alex" assert env.result.channels[ch2]['name'] == "Alex" assert env.result.channels[ch1]['count'] == 5 assert env.result.channels[ch2]['count'] == 5 pubnub.stop()
async def test_add_channel_remove_group(event_loop, sleeper=asyncio.sleep): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) ch = "channel-groups-tornado-ch" gr = "channel-groups-tornado-cg" # add env = await pubnub.add_channel_to_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) await sleeper(1) # list env = await pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 1 assert env.result.channels[0] == ch # remove group env = await pubnub.remove_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveGroupResult) await sleeper(1) # list env = await pubnub.list_channels_in_channel_group().channel_group( gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 0 pubnub.stop()
def test_multiple_channels(event_loop, sleeper=asyncio.sleep): pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) ch1 = 'test-where-now-asyncio-ch1' ch2 = 'test-where-now-asyncio-ch2' uuid = 'test-where-now-asyncio-uuid' pubnub.config.uuid = uuid callback = VCR599Listener(1) pubnub.add_listener(callback) pubnub.subscribe().channels([ch1, ch2]).execute() yield from callback.wait_for_connect() yield from sleeper(7) env = yield from pubnub.where_now() \ .uuid(uuid) \ .future() channels = env.result.channels assert len(channels) == 2 assert ch1 in channels assert ch2 in channels pubnub.unsubscribe().channels([ch1, ch2]).execute() yield from callback.wait_for_disconnect() pubnub.stop()
async def test_publish_envelope_raises(event_loop): pubnub = PubNubAsyncio(corrupted_keys, custom_event_loop=event_loop) e = await pubnub.publish().message('hey').channel('blah').future() assert isinstance(e, PubNubAsyncioException) assert e.is_error() assert 400 == e.value()._status_code pubnub.stop()
def test_fetch_file_upload_s3_data_with_result_invocation(event_loop, file_upload_test_data): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) result = yield from pubnub._fetch_file_upload_s3_data().\ channel(CHANNEL).\ file_name(file_upload_test_data["UPLOADED_FILENAME"]).result() assert isinstance(result, PNFetchFileUploadS3DataResult) pubnub.stop()
def test_error_non_serializable(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) def method(): pass yield from assert_client_side_error(pubnub.publish().channel(ch).message(method), "not JSON serializable") pubnub.stop()
def test_delete_with_space_and_wildcard_in_channel_name(event_loop): pubnub = PubNubAsyncio(mocked_config_copy(), custom_event_loop=event_loop) res = yield from pubnub.delete_messages().channel("my-ch- |.* $").start( 123).end(456).future() if res.status.is_error(): raise AssertionError()
def test_error_non_serializable(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) def method(): pass yield from assert_client_side_error(pubnub.publish().channel(ch).message(method), "not JSON serializable") pubnub.stop()
def test_publish_envelope_raises(event_loop): pubnub = PubNubAsyncio(corrupted_keys, custom_event_loop=event_loop) e = yield from pubnub.publish().message('hey').channel('blah').future() assert isinstance(e, PubNubAsyncioException) assert e.is_error() assert 400 == e.value()._status_code pubnub.stop()
def test_delete_space(event_loop): config = pnconf_obj_copy() pn = PubNubAsyncio(config, custom_event_loop=event_loop) envelope = yield from pn.delete_space().space_id('in_space').future() assert (isinstance(envelope, AsyncioEnvelope)) assert not envelope.status.is_error() assert isinstance(envelope.result, PNDeleteSpaceResult) assert isinstance(envelope.status, PNStatus)
async def test_publish_with_meta(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) await assert_success_await( pubnub.publish().channel(ch).message("hey").meta({ 'a': 2, 'b': 'qwer' })) await pubnub.stop()
def test_publish_mixed_via_post(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) yield from asyncio.gather( asyncio.ensure_future(assert_success_publish_post(pubnub, "hi")), asyncio.ensure_future(assert_success_publish_post(pubnub, 5)), asyncio.ensure_future(assert_success_publish_post(pubnub, True)), asyncio.ensure_future(assert_success_publish_post(pubnub, ["hi", "hi2", "hi3"]))) pubnub.stop()
def test_publish_super_admin_call(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) yield from pubnub.publish().channel(ch).message("hey").future() yield from pubnub.publish().channel("foo.bar").message("hey^&#$").should_store(True).meta({ 'name': 'alex' }).future() pubnub.stop()
async def test_publish_object_via_post_encrypted(event_loop): pubnub = PubNubAsyncio(pnconf_enc_copy(), custom_event_loop=event_loop) await asyncio.ensure_future( assert_success_publish_post(pubnub, { "name": "Alex", "online": True })) pubnub.stop()
def test_time(event_loop): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) env = yield from pubnub.time().future() assert int(env.result) > 0 assert isinstance(env.result.date_time(), date) pubnub.stop()
def test_publish_mixed_via_post(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) yield from asyncio.gather( asyncio.ensure_future(assert_success_publish_post(pubnub, "hi")), asyncio.ensure_future(assert_success_publish_post(pubnub, 5)), asyncio.ensure_future(assert_success_publish_post(pubnub, True)), asyncio.ensure_future(assert_success_publish_post(pubnub, ["hi", "hi2", "hi3"]))) pubnub.stop()
def test_time(event_loop): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) res = yield from pubnub.time().result() assert int(res) > 0 assert isinstance(res.date_time(), date) pubnub.stop()
async def test_time(event_loop): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) res = await pubnub.time().result() assert int(res) > 0 assert isinstance(res.date_time(), date) pubnub.stop()
async def test_success(event_loop): pubnub = PubNubAsyncio(mocked_config_copy(), custom_event_loop=event_loop) res = await pubnub.delete_messages().channel("my-ch").start(123).end(456).future() if res.status.is_error(): raise AssertionError() pubnub.stop()
def test_publish_object_via_get(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) yield from asyncio.ensure_future( assert_success_publish_get(pubnub, { "name": "Alex", "online": True })) pubnub.stop()
def test_publish_future_raises_pubnub_error(event_loop): pubnub = PubNubAsyncio(corrupted_keys, custom_event_loop=event_loop) with pytest.raises(PubNubException) as exinfo: yield from pubnub.publish().message('hey').channel('blah').result() assert 'Invalid Subscribe Key' in str(exinfo.value) assert 400 == exinfo.value._status_code pubnub.stop()
async def test_send_and_download_file(event_loop, file_for_upload): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) envelope = await send_file(pubnub, file_for_upload) download_envelope = await pubnub.download_file().\ channel(CHANNEL).\ file_id(envelope.result.file_id).\ file_name(envelope.result.name).future() assert isinstance(download_envelope.result, PNDownloadFileResult) pubnub.stop()
def test_error_invalid_key(event_loop): conf = PNConfiguration() conf.publish_key = "fake" conf.subscribe_key = "demo" conf.enable_subscribe = False pubnub = PubNubAsyncio(conf, custom_event_loop=event_loop) yield from assert_server_side_error_yield(pubnub.publish().channel(ch).message("hey"), "Invalid Key") pubnub.stop()
async def test_publish_future_raises_pubnub_error(event_loop): pubnub = PubNubAsyncio(corrupted_keys, custom_event_loop=event_loop) with pytest.raises(PubNubException) as exinfo: await pubnub.publish().message('hey').channel('blah').result() assert 'Invalid Subscribe Key' in str(exinfo.value) assert 400 == exinfo.value._status_code pubnub.stop()
async def test_get_file_url(event_loop, file_for_upload): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) envelope = await send_file(pubnub, file_for_upload) file_url_envelope = await pubnub.get_file_url().\ channel(CHANNEL).\ file_id(envelope.result.file_id).\ file_name(envelope.result.name).future() assert isinstance(file_url_envelope.result, PNGetFileDownloadURLResult) pubnub.stop()
def test_error_invalid_key(event_loop): conf = PNConfiguration() conf.publish_key = "fake" conf.subscribe_key = "demo" conf.enable_subscribe = False pubnub = PubNubAsyncio(conf, custom_event_loop=event_loop) yield from assert_server_side_error_yield(pubnub.publish().channel(ch).message("hey"), "Invalid Key") pubnub.stop()
def test_publish_future_raises_lower_level_error(event_loop): pubnub = PubNubAsyncio(corrupted_keys, custom_event_loop=event_loop) pubnub._connector.close() with pytest.raises(RuntimeError) as exinfo: yield from pubnub.publish().message('hey').channel('blah').result() assert 'Session is closed' in str(exinfo.value) pubnub.stop()
def test_publish_envelope_raises_lower_level_error(event_loop): pubnub = PubNubAsyncio(corrupted_keys, custom_event_loop=event_loop) pubnub._connector.close() e = yield from pubnub.publish().message('hey').channel('blah').future() assert isinstance(e, PubNubAsyncioException) assert e.is_error() assert str(e.value()) == 'Session is closed' pubnub.stop()
def test_where_now_super_admin_call(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) uuid = 'test-where-now-asyncio-uuid' pubnub.config.uuid = uuid res = yield from pubnub.where_now() \ .uuid(uuid) \ .result() assert isinstance(res, PNWhereNowResult) pubnub.stop()
def test_super_call(event_loop): pubnub = PubNubAsyncio(pnconf_pam_copy(), custom_event_loop=event_loop) ch = "channel-groups-torna|do-ch" gr = "channel-groups-torna|do-cg" pubnub.config.auth = "h.e|l%l,0" # add env = yield from pubnub.add_channel_to_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) # list env = yield from pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) # remove channel env = yield from pubnub.remove_channel_from_channel_group().channel_group(gr).channels(ch).future() assert isinstance(env.result, PNChannelGroupsRemoveChannelResult) # remove group env = yield from pubnub.remove_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveGroupResult) # list env = yield from pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) pubnub.stop()
def test_state_super_admin_call(event_loop): pnconf = pnconf_pam_copy() pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) ch1 = "test-state-asyncio-ch1" ch2 = "test-state-asyncio-ch2" pubnub.config.uuid = "test-state-asyncio-uuid" state = {"name": "Alex", "count": 5} env = yield from pubnub.set_state().channels([ch1, ch2]).state(state).future() assert isinstance(env.result, PNSetStateResult) env = yield from pubnub.get_state().channels([ch1, ch2]).future() assert isinstance(env.result, PNGetStateResult) pubnub.stop()
def test_single_channelx(event_loop): pubnub = PubNubAsyncio(pnconf_copy(), custom_event_loop=event_loop) ch = "test-state-asyncio-ch" pubnub.config.uuid = "test-state-asyncio-uuid" state = {"name": "Alex", "count": 5} env = yield from pubnub.set_state().channels(ch).state(state).future() assert env.result.state["name"] == "Alex" assert env.result.state["count"] == 5 env = yield from pubnub.get_state().channels(ch).future() assert env.result.channels[ch]["name"] == "Alex" assert env.result.channels[ch]["count"] == 5 pubnub.stop()
def test_add_channel_remove_group(event_loop, sleeper=asyncio.sleep): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) ch = "channel-groups-tornado-ch" gr = "channel-groups-tornado-cg" # add env = yield from pubnub.add_channel_to_channel_group() \ .channels(ch).channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsAddChannelResult) yield from sleeper(1) # list env = yield from pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 1 assert env.result.channels[0] == ch # remove group env = yield from pubnub.remove_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsRemoveGroupResult) yield from sleeper(1) # list env = yield from pubnub.list_channels_in_channel_group().channel_group(gr).future() assert isinstance(env.result, PNChannelGroupsListResult) assert len(env.result.channels) == 0 pubnub.stop()
def test_multiple_channels(event_loop): pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) ch1 = "test-state-asyncio-ch1" ch2 = "test-state-asyncio-ch2" pubnub.config.uuid = "test-state-asyncio-uuid" state = {"name": "Alex", "count": 5} env = yield from pubnub.set_state().channels([ch1, ch2]).state(state).future() assert env.result.state["name"] == "Alex" assert env.result.state["count"] == 5 env = yield from pubnub.get_state().channels([ch1, ch2]).future() assert env.result.channels[ch1]["name"] == "Alex" assert env.result.channels[ch2]["name"] == "Alex" assert env.result.channels[ch1]["count"] == 5 assert env.result.channels[ch2]["count"] == 5 pubnub.stop()
def test_blah(): pnconf = pnconf_sub_copy() assert isinstance(pnconf, PNConfiguration) pnconf.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL pubnub = PubNubAsyncio(pnconf) time_until_open_again = 8 @asyncio.coroutine def close_soon(): yield from asyncio.sleep(2) pubnub._connector.close() print(">>> connection is broken") @asyncio.coroutine def open_again(): yield from asyncio.sleep(time_until_open_again) pubnub.set_connector(aiohttp.TCPConnector(conn_timeout=pubnub.config.connect_timeout, verify_ssl=True)) print(">>> connection is open again") @asyncio.coroutine def countdown(): asyncio.sleep(2) opened = False count = time_until_open_again while not opened: print(">>> %ds to open again" % count) count -= 1 if count <= 0: break yield from asyncio.sleep(1) my_listener = MySubscribeCallback() pubnub.add_listener(my_listener) pubnub.subscribe().channels("my_channel").execute() asyncio.ensure_future(close_soon()) asyncio.ensure_future(open_again()) asyncio.ensure_future(countdown()) yield from asyncio.sleep(1000)
def test_encrypted_subscribe_publish_unsubscribe(event_loop): pubnub = PubNubAsyncio(pnconf_enc_sub_copy(), custom_event_loop=event_loop) pubnub.config.uuid = 'test-subscribe-asyncio-uuid' callback = VCR599Listener(1) channel = "test-subscribe-asyncio-ch" message = "hey" pubnub.add_listener(callback) pubnub.subscribe().channels(channel).execute() yield from callback.wait_for_connect() publish_future = asyncio.ensure_future(pubnub.publish().channel(channel).message(message).future()) subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel)) yield from asyncio.wait([ publish_future, subscribe_message_future ]) publish_envelope = publish_future.result() subscribe_envelope = subscribe_message_future.result() assert isinstance(subscribe_envelope, PNMessageResult) assert subscribe_envelope.channel == channel assert subscribe_envelope.subscription is None assert subscribe_envelope.message == message assert subscribe_envelope.timetoken > 0 assert isinstance(publish_envelope, AsyncioEnvelope) assert publish_envelope.result.timetoken > 0 assert publish_envelope.status.original_response[0] == 1 pubnub.unsubscribe().channels(channel).execute() yield from callback.wait_for_disconnect() pubnub.stop()
def test_global(event_loop, sleeper=asyncio.sleep): pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) pubnub.config.uuid = 'test-here-now-asyncio-uuid1' ch1 = "test-here-now-asyncio-ch1" ch2 = "test-here-now-asyncio-ch2" callback = VCR599Listener(1) pubnub.add_listener(callback) pubnub.subscribe().channels([ch1, ch2]).execute() yield from callback.wait_for_connect() yield from sleeper(5) env = yield from pubnub.here_now().future() assert env.result.total_channels >= 2 assert env.result.total_occupancy >= 1 pubnub.unsubscribe().channels([ch1, ch2]).execute() yield from callback.wait_for_disconnect() pubnub.stop()
async def test_subscribe_publish_unsubscribe(event_loop): pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop) callback = SubscribeListener() channel = helper.gen_channel("test-sub-pub-unsub") message = "hey" pubnub.add_listener(callback) pubnub.subscribe().channels(channel).execute() await callback.wait_for_connect() publish_future = asyncio.ensure_future(pubnub.publish().channel(channel).message(message).future()) subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel)) await asyncio.wait([ publish_future, subscribe_message_future ]) publish_envelope = publish_future.result() subscribe_envelope = subscribe_message_future.result() assert isinstance(subscribe_envelope, PNMessageResult) assert subscribe_envelope.channel == channel assert subscribe_envelope.subscription is None assert subscribe_envelope.message == message assert subscribe_envelope.timetoken > 0 assert isinstance(publish_envelope, AsyncioEnvelope) assert publish_envelope.result.timetoken > 0 assert publish_envelope.status.original_response[0] == 1 pubnub.unsubscribe().channels(channel).execute() await callback.wait_for_disconnect() pubnub.stop()
def test_access_denied_unsubscribe_operation(event_loop): channel = "not-permitted-channel" pnconf = pnconf_pam_copy() pnconf.secret_key = None pnconf.enable_subscribe = True pubnub = PubNubAsyncio(pnconf, custom_event_loop=event_loop) callback = AccessDeniedListener() pubnub.add_listener(callback) pubnub.subscribe().channels(channel).execute() yield from callback.access_denied_event.wait() pubnub.stop()
async def message_pump(self): pnconfig = PNConfiguration() pnconfig.subscribe_key = config['cardsubkey'] # Why aren't these the default settings? pnconfig.ssl = True pnconfig.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL pubnub = PubNubAsyncio(pnconfig) listener = SubscribeListener() pubnub.add_listener(listener) pubnub.subscribe().channels(config['cardviewerchannel']).execute() await listener.wait_for_connect() log.info("Connected to PubNub") message_future = asyncio.ensure_future(listener.wait_for_message_on(config['cardviewerchannel'])) while True: await asyncio.wait([self.stop_future, message_future], return_when=asyncio.FIRST_COMPLETED) if message_future.done(): message = message_future.result().message log.info("Message from PubNub: %r", message) card_id = self._extract(message) if card_id is not None: await self._card(card_id) message_future = asyncio.ensure_future(listener.wait_for_message_on(config['cardviewerchannel'])) if self.stop_future.done(): break if not message_future.done(): message_future.cancel() pubnub.unsubscribe().channels(config['cardviewerchannel']).execute() await listener.wait_for_disconnect() pubnub.stop() log.info("Disconnected from PubNub")