Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
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()
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
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()
Ejemplo n.º 7
0
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()
Ejemplo n.º 8
0
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()
Ejemplo n.º 9
0
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()
Ejemplo n.º 10
0
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()
Ejemplo n.º 11
0
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()
Ejemplo n.º 12
0
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()
Ejemplo n.º 13
0
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()
Ejemplo n.º 14
0
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()
Ejemplo n.º 15
0
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()
Ejemplo n.º 16
0
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()
Ejemplo n.º 17
0
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()
Ejemplo n.º 18
0
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()
Ejemplo n.º 19
0
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()
Ejemplo n.º 20
0
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()
Ejemplo n.º 21
0
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()
Ejemplo n.º 22
0
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()
Ejemplo n.º 23
0
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()
Ejemplo n.º 24
0
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()
Ejemplo n.º 25
0
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()
Ejemplo n.º 26
0
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()
Ejemplo n.º 27
0
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()
Ejemplo n.º 28
0
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()
Ejemplo n.º 29
0
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)
Ejemplo n.º 30
0
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()
Ejemplo n.º 31
0
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()
Ejemplo n.º 32
0
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()
Ejemplo n.º 33
0
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()
Ejemplo n.º 34
0
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()
Ejemplo n.º 35
0
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()
Ejemplo n.º 36
0
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()
Ejemplo n.º 37
0
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()
Ejemplo n.º 38
0
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()
Ejemplo n.º 39
0
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()
Ejemplo n.º 40
0
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()
Ejemplo n.º 41
0
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()
Ejemplo n.º 42
0
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()
Ejemplo n.º 43
0
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()
Ejemplo n.º 44
0
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()
Ejemplo n.º 45
0
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()
Ejemplo n.º 46
0
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()
Ejemplo n.º 47
0
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()
Ejemplo n.º 48
0
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()
Ejemplo n.º 49
0
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()
Ejemplo n.º 50
0
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()
Ejemplo n.º 51
0
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()
Ejemplo n.º 52
0
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()
Ejemplo n.º 53
0
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()
Ejemplo n.º 54
0
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)
Ejemplo n.º 55
0
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()
Ejemplo n.º 56
0
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()
Ejemplo n.º 58
0
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()
Ejemplo n.º 59
0
	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")