diff options
| author | Devendra | 2015-06-25 11:42:00 +0530 | 
|---|---|---|
| committer | Devendra | 2015-06-25 11:42:00 +0530 | 
| commit | ad89de8312f3f407150c7eb411cbf02765910e53 (patch) | |
| tree | 6f683a1d154a4d4f3bb11ed6f2d56ec813db6ccb /python | |
| parent | 4a44c563ea8af7211197d166596be41ede05c179 (diff) | |
| parent | 232f7389274f0d9ff06835fc5da0970f7918ba25 (diff) | |
| download | pubnub-python-ad89de8312f3f407150c7eb411cbf02765910e53.tar.bz2 | |
Merge branch 'develop'
Diffstat (limited to 'python')
| -rw-r--r-- | python/README.md | 17 | ||||
| -rw-r--r-- | python/examples/audit.py | 2 | ||||
| -rw-r--r-- | python/examples/console.py | 2 | ||||
| -rw-r--r-- | python/examples/cr.py | 2 | ||||
| -rwxr-xr-x | python/examples/dev-console.py | 2 | ||||
| -rw-r--r-- | python/examples/grant.py | 2 | ||||
| -rw-r--r-- | python/examples/here-now.py | 2 | ||||
| -rw-r--r-- | python/examples/history.py | 14 | ||||
| -rw-r--r-- | python/examples/pam_demo/demo.py | 114 | ||||
| -rw-r--r-- | python/examples/presence.py | 2 | ||||
| -rw-r--r-- | python/examples/publish.py | 4 | ||||
| -rw-r--r-- | python/examples/pubnub-console/pubnub-console | 2 | ||||
| -rw-r--r-- | python/examples/revoke.py | 2 | ||||
| -rw-r--r-- | python/examples/subscribe.py | 9 | ||||
| -rw-r--r-- | python/examples/subscribe_group.py | 2 | ||||
| -rwxr-xr-x | python/tests/subscribe-test.py | 154 | ||||
| -rw-r--r-- | python/tests/test_cg.py | 2 | ||||
| -rw-r--r-- | python/tests/test_grant.py | 2 | ||||
| -rw-r--r-- | python/tests/test_grant_async.py | 369 | ||||
| -rw-r--r-- | python/tests/test_history.py | 58 | ||||
| -rw-r--r-- | python/tests/test_publish_async.py | 228 | 
21 files changed, 222 insertions, 769 deletions
| diff --git a/python/README.md b/python/README.md index 80a6b08..10bb30f 100644 --- a/python/README.md +++ b/python/README.md @@ -191,6 +191,23 @@ def callback(message):  pubnub.history(channel=channel, count=2, callback=callback, error=callback)  ``` +#### HISTORY (including timetokens) + +``` +# Synchronous usage + +print pubnub.history(channel, count=2, include_token=True) + +# Asynchronous usage + + +def callback(message): +    print(message) + +pubnub.history(channel, count=2, include_token=True, callback=callback, error=callback) +``` + +  #### GRANT  ``` diff --git a/python/examples/audit.py b/python/examples/audit.py index ebf31af..c53c2bd 100644 --- a/python/examples/audit.py +++ b/python/examples/audit.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' diff --git a/python/examples/console.py b/python/examples/console.py index bfa4486..a1915ed 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  import threading  from datetime import datetime diff --git a/python/examples/cr.py b/python/examples/cr.py index c537780..f63e6d2 100644 --- a/python/examples/cr.py +++ b/python/examples/cr.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' diff --git a/python/examples/dev-console.py b/python/examples/dev-console.py index 134d2e7..8527307 100755 --- a/python/examples/dev-console.py +++ b/python/examples/dev-console.py @@ -6,7 +6,7 @@  ## http://www.pubnub.com/  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  from optparse import OptionParser diff --git a/python/examples/grant.py b/python/examples/grant.py index af9352e..b0832e4 100644 --- a/python/examples/grant.py +++ b/python/examples/grant.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' diff --git a/python/examples/here-now.py b/python/examples/here-now.py index 9640cc5..4c2dc4c 100644 --- a/python/examples/here-now.py +++ b/python/examples/here-now.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' diff --git a/python/examples/history.py b/python/examples/history.py index 603a0f8..19593e1 100644 --- a/python/examples/history.py +++ b/python/examples/history.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' @@ -33,3 +33,15 @@ def callback(message):      print(message)  pubnub.history(channel, count=2, callback=callback, error=callback) + +# Synchronous usage + +print pubnub.history(channel, count=2, include_token=True) + +# Asynchronous usage + + +def callback(message): +    print(message) + +pubnub.history(channel, count=2, include_token=True, callback=callback, error=callback) diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py new file mode 100644 index 0000000..c08f9f5 --- /dev/null +++ b/python/examples/pam_demo/demo.py @@ -0,0 +1,114 @@ +from gevent.monkey import patch_all +patch_all() + +import sys +from pubnub import Pubnub +import random +import json + +rand = str(random.randint(1,99999999)) + +def get_unique(s): +	return 'str-' + rand + '-' + s + +# public channel +# This is the channel all clients announce themselves on -- or more generally speaking, a channel you expect the client +# to "check-in" on to announce his state + +#channel_public = get_unique("channel_public") +channel_public = "channel_public" + +# server auth key +# Only the server has/knows about this auth token. It will be used to grant read on the "check-in" Presence channel + +#server_auth_token = get_unique("server_auth_token") +server_auth_token = "server_auth_token" + +# client auth key +# only clients will use this authey -- it does not provide presence channel read access +  +#client_auth_token = get_unique("client_auth_token") +client_auth_token = "client_auth_token" + +# each client must have a unique id -- a UUID, for presence information/state to bind to + +# client uuid +client_uuid = get_unique("client_uuid") + +# server uuid +server_uuid = get_unique("server_uuid") + +# For the demo, we'll implement a SERVER called server, who is the authoritative 'admin' entity in the system +# We'll also implement a CLIENT called client, who is an arbitrary hardware device member of the network + +# Please swap out the default 'pam' demo keys with your own PAM-enabled keys + +# init server object +server = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam", auth_key=server_auth_token, uuid=server_uuid) + +# init client object +client = Pubnub(publish_key="pam", subscribe_key="pam", auth_key=client_auth_token, uuid=client_uuid) + +# To access a Presence channel with PAM, its format is CHANNELNAME-pnpres + +# Grant permission to server auth keys +# grant r/w to public, and r/w public Presence (public-pnpres) + +print(server.grant(channel=channel_public, auth_key=server_auth_token, read=True, write=True)) +print(server.grant(channel=channel_public + '-pnpres', auth_key=server_auth_token, read=True, write=True)) + +# Grant permission to client auth keys +# grant r/w to public, and w-only access to public Presence (public-pnpres) +print(server.grant(channel=channel_public, auth_key=client_auth_token, read=True, write=False)) +print(server.grant(channel=channel_public + '-pnpres', auth_key=client_auth_token, read=False, write=False)) + +# Now, we'll run it to watch it work as advertised... + +# Define some simple callabcks for the Server and Client + +def _server_message_callback(message, channel): +	print("Server heard: " + json.dumps(message)) + +def _client_message_callback(message, channel): +    print("Client heard: " + json.dumps(message)) + +def _client_error_callback(error, channel): +    print("Client Error: " + error + " on channel " + channel) +    print("TTL on grant expired, or token was invalid, or revoked. Client will now unsubscribe from this unauthorized channel.") +    client.unsubscribe(channel=channel) + +def _server_error_callback(error, channel): +    print("Server Error: " + error + " on channel " + channel) +    print("TTL on grant expired, or token was revoked. Server will now unsubscribe from this unauthorized channel.") +    server.unsubscribe(channel=channel) + +def _server_presence_callback(message, channel): +    print message +    if 'action' in message: +        if message['action'] == 'join' and message['uuid'] == client_uuid: +            print "Server can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) + +def _client_presence_callback(message, channel): +    print message +    if 'action' in message: +        if message['action'] == 'join' and message['uuid'] == client_uuid: +            print "Client can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) + +# server subscribes to public channel +server.subscribe(channels=channel_public, callback=_server_message_callback, error=_server_error_callback) + +# server subscribes to presence events on public channel +# presence() is a convienence method that subscribes to channel-pnpres with special logic for handling +# presence-event formatted messages + +## uncomment out to see server able to read on presence channel +server.presence(channel=channel_public, callback=_server_presence_callback, error=_server_error_callback) + +# now if the client tried to subscribe on the presence channel, and therefore, get state info +# he is explicitly denied! + +## uncomment out to see client not able to read on presence channel +client.presence(channel=channel_public, callback=_client_presence_callback, error=_client_error_callback) + +# client subscribes to public channel +client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_message_callback, error=_client_error_callback) diff --git a/python/examples/presence.py b/python/examples/presence.py index ab91321..7d9af3b 100644 --- a/python/examples/presence.py +++ b/python/examples/presence.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' diff --git a/python/examples/publish.py b/python/examples/publish.py index 594e7c4..3bc5e8f 100644 --- a/python/examples/publish.py +++ b/python/examples/publish.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' @@ -19,7 +19,7 @@ ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False  ## Initiate Pubnub State  ## -----------------------------------------------------------------------  pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, -                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) +                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on, pooling=False)  channel = 'hello_world'  message = 'Hello World !!!' diff --git a/python/examples/pubnub-console/pubnub-console b/python/examples/pubnub-console/pubnub-console index bfa4486..a1915ed 100644 --- a/python/examples/pubnub-console/pubnub-console +++ b/python/examples/pubnub-console/pubnub-console @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  import threading  from datetime import datetime diff --git a/python/examples/revoke.py b/python/examples/revoke.py index 437e5b5..9bee010 100644 --- a/python/examples/revoke.py +++ b/python/examples/revoke.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' diff --git a/python/examples/subscribe.py b/python/examples/subscribe.py index 9b8b223..489c7c1 100644 --- a/python/examples/subscribe.py +++ b/python/examples/subscribe.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from pubnub import Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' @@ -19,7 +19,7 @@ ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False  ## Initiate Pubnub State  ## -----------------------------------------------------------------------  pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, -                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) +                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on, daemon=True)  channel = 'a' @@ -45,5 +45,8 @@ def disconnect(message):      print("DISCONNECTED") -pubnub.subscribe(channel, callback=callback, error=callback, +pubnub.subscribe(channels=channel, callback=callback, error=callback,                   connect=connect, reconnect=reconnect, disconnect=disconnect) +import time +while True: +    time.sleep(10) diff --git a/python/examples/subscribe_group.py b/python/examples/subscribe_group.py index ee8e190..c6dcf67 100644 --- a/python/examples/subscribe_group.py +++ b/python/examples/subscribe_group.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub as Pubnub +from pubnub import Pubnub as Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' diff --git a/python/tests/subscribe-test.py b/python/tests/subscribe-test.py deleted file mode 100755 index a1b1826..0000000 --- a/python/tests/subscribe-test.py +++ /dev/null @@ -1,154 +0,0 @@ -## www.pubnub.com - PubNub Real-time push service in the cloud. -# coding=utf8 - -## PubNub Real-time Push APIs and Notifications Framework -## Copyright (c) 2010 Stephen Blum -## http://www.pubnub.com/ - -## ----------------------------------- -## PubNub 3.1 Real-time Push Cloud API -## ----------------------------------- - -import sys -import datetime -from Pubnub import PubnubAsync as Pubnub -from functools import partial -from threading import current_thread -import threading -publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' -subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' -secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo' -cipher_key = len(sys.argv) > 4 and sys.argv[4] or None -ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False - -## ----------------------------------------------------------------------- -## Initiate Pubnub State -## ----------------------------------------------------------------------- -#pubnub = Pubnub( publish_key, subscribe_key, secret_key, cipher_key, ssl_on ) -pubnub = Pubnub(publish_key, subscribe_key, secret_key, ssl_on) -crazy = 'hello_world' - -current = -1 - -errors = 0 -received = 0 - -## ----------------------------------------------------------------------- -## Subscribe Example -## ----------------------------------------------------------------------- - - -def message_received(message): -    print(message) - - -def check_received(message): -    global current -    global errors -    global received -    print(message) -    print(current) -    if message <= current: -        print('ERROR') -        #sys.exit() -        errors += 1 -    else: -        received += 1 -    print('active thread count : ' + str(threading.activeCount())) -    print('errors = ' + str(errors)) -    print(current_thread().getName() + ' , ' + 'received = ' + str(received)) - -    if received != message: -        print('********** MISSED **************** ' + str(message - received)) -    current = message - - -def connected_test(ch): -    print('Connected ' + ch) - - -def connected(ch): -    pass - - -''' -pubnub.subscribe({ -    'channel'  : 'abcd1', -    'connect'  : connected, -    'callback' : message_received -}) -''' - - -def cb1(): -    pubnub.subscribe({ -        'channel': 'efgh1', -        'connect': connected, -        'callback': message_received -    }) - - -def cb2(): -    pubnub.subscribe({ -        'channel': 'dsm-test', -        'connect': connected_test, -        'callback': check_received -    }) - - -def cb3(): -    pubnub.unsubscribe({'channel': 'efgh1'}) - - -def cb4(): -    pubnub.unsubscribe({'channel': 'abcd1'}) - - -def subscribe(channel): -    pubnub.subscribe({ -        'channel': channel, -        'connect': connected, -        'callback': message_received -    }) - - -pubnub.timeout(15, cb1) - -pubnub.timeout(30, cb2) - - -pubnub.timeout(45, cb3) - -pubnub.timeout(60, cb4) - -#''' -for x in range(1, 1000): -    #print x -    def y(t): -        subscribe('channel-' + str(t)) - -    def z(t): -        pubnub.unsubscribe({'channel': 'channel-' + str(t)}) - -    pubnub.timeout(x + 5, partial(y, x)) -    pubnub.timeout(x + 25, partial(z, x)) -    x += 10 -#''' - -''' -for x in range(1,1000): -    def cb(r): print r , ' : ', threading.activeCount() -    def y(t): -        pubnub.publish({ -            'message' : t, -            'callback' : cb, -            'channel' : 'dsm-test' -        }) - - -    pubnub.timeout(x + 1, partial(y,x)) -    x += 1 -''' - - -pubnub.start() diff --git a/python/tests/test_cg.py b/python/tests/test_cg.py index a823e44..f4a1080 100644 --- a/python/tests/test_cg.py +++ b/python/tests/test_cg.py @@ -1,4 +1,4 @@ -from Pubnub import Pubnub +from pubnub import Pubnub  import time  import random diff --git a/python/tests/test_grant.py b/python/tests/test_grant.py index 9ecafdf..57f0b14 100644 --- a/python/tests/test_grant.py +++ b/python/tests/test_grant.py @@ -1,6 +1,6 @@ -from Pubnub import Pubnub +from pubnub import Pubnub  import time  pubnub = Pubnub("demo","demo") diff --git a/python/tests/test_grant_async.py b/python/tests/test_grant_async.py deleted file mode 100644 index 4d38a0a..0000000 --- a/python/tests/test_grant_async.py +++ /dev/null @@ -1,369 +0,0 @@ - - -from Pubnub import Pubnub -import time - -pubnub = Pubnub("demo","demo") -pubnub_pam = Pubnub("pub-c-c077418d-f83c-4860-b213-2f6c77bde29a",  -	"sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe", "sec-c-OGU3Y2Q4ZWUtNDQwMC00NTI1LThjNWYtNWJmY2M4OGIwNjEy") - - - -# Grant permission read true, write true, on channel ( Async Mode ) -def test_1(): -	resp = {'response' : None} -	def _callback(response): -		resp['response'] = response - -	def _error(response): -		resp['response'] = response - -	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=True, ttl=1, callback=_callback, error=_error) -	time.sleep(2) -	assert resp['response'] == { -									'message': u'Success', -									'payload': {u'auths': {u'abcd': {u'r': 1, u'w': 1}}, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'user', u'channel': u'abcd', u'ttl': 1} -								} -							 - -# Grant permission read false, write false, on channel ( Async Mode ) -def test_2(): -	resp = {'response' : None} -	def _callback(response): -		resp['response'] = response - -	def _error(response): -		resp['response'] = response - -	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=False, ttl=1, callback=_callback, error=_error) -	time.sleep(2) -	assert resp['response'] == { -									'message': u'Success', -									'payload': {u'auths': {u'abcd': {u'r': 0, u'w': 0}}, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'user', u'channel': u'abcd', u'ttl': 1} -								} - - -# Grant permission read True, write false, on channel ( Async Mode ) -def test_3(): -	resp = {'response' : None} -	def _callback(response): -		resp['response'] = response - -	def _error(response): -		resp['response'] = response - -	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=False, ttl=1, callback=_callback, error=_error) -	time.sleep(2) -	assert resp['response'] == { -									'message': u'Success', -									'payload': {u'auths': {u'abcd': {u'r': 1, u'w': 0}}, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'user', u'channel': u'abcd', u'ttl': 1} -								} - -# Grant permission read False, write True, on channel ( Async Mode ) -def test_4(): -	resp = {'response' : None} -	def _callback(response): -		resp['response'] = response - -	def _error(response): -		resp['response'] = response - -	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=True, ttl=1, callback=_callback, error=_error) -	time.sleep(2) -	assert resp['response'] == { -									'message': u'Success', -									'payload': {u'auths': {u'abcd': {u'r': 0, u'w': 1}}, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'user', u'channel': u'abcd', u'ttl': 1} -								} - - -# Grant permission read False, write True, on channel ( Async Mode ), TTL 10 -def test_5(): -	resp = {'response' : None} -	def _callback(response): -		resp['response'] = response - -	def _error(response): -		resp['response'] = response - -	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=True, ttl=10, callback=_callback, error=_error) -	time.sleep(2) -	assert resp['response'] == { -									'message': u'Success', -									'payload': {u'auths': {u'abcd': {u'r': 0, u'w': 1}}, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'user', u'channel': u'abcd', u'ttl': 10} -								} - - -# Grant permission read False, write True, without channel ( Async Mode ), TTL 10 -def test_6(): -	resp = {'response' : None} -	def _callback(response): -		resp['response'] = response - -	def _error(response): -		resp['response'] = response - -	pubnub_pam.grant(auth_key="abcd", read=False, write=True, ttl=10, callback=_callback, error=_error) -	time.sleep(2) -	assert resp['response'] == { -									'message': u'Success', -									'payload': { u'r': 0, u'w': 1, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'subkey', u'ttl': 10} -								} - - -# Grant permission read False, write False, without channel ( Async Mode ) -def test_7(): -	resp = {'response' : None} -	def _callback(response): -		resp['response'] = response - -	def _error(response): -		resp['response'] = response - -	pubnub_pam.grant(auth_key="abcd", read=False, write=False, callback=_callback, error=_error) -	time.sleep(2) -	assert resp['response'] == { -									'message': u'Success', -									'payload': { u'r': 0, u'w': 0, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'subkey', u'ttl': 1} -								} - - -# Complete flow , try publish on forbidden channel, grant permission to subkey and try again. ( Sync Mode) - -def test_8(): -	channel = "test_8-" + str(time.time()) -	message = "Hello World" -	auth_key = "auth-" + channel -	pubnub_pam.set_auth_key(auth_key) - - - -	def _cb1(resp, ch=None): -		assert False -	def _err1(resp): -		assert resp['message'] == 'Forbidden' -		assert resp['payload'] == {u'channels': [channel]} -	pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1) -	time.sleep(2) - - -	def _cb2(resp, ch=None): -		assert resp == 		{ -								'message': u'Success', -								'payload': {u'auths': {auth_key : {u'r': 1, u'w': 1}}, -								u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -								u'level': u'user', u'channel': channel, u'ttl': 10} -							} -	def _err2(resp): -		assert False - - -	resp = pubnub_pam.grant(channel=channel, read=True, write=True, auth_key=auth_key, ttl=10, callback=_cb2, error=_err2) -	time.sleep(2) - -	def _cb3(resp, ch=None): -		assert resp[0] == 1 -	def _err3(resp): -		assert False - -	pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3) -	time.sleep(2) - - - - - -# Complete flow , try publish on forbidden channel, grant permission to authkey and try again.  -# then revoke and try again -def test_9(): -	channel = "test_9-" + str(time.time()) -	message = "Hello World" -	auth_key = "auth-" + channel -	pubnub_pam.set_auth_key(auth_key) - -	def _cb1(resp, ch=None): -		assert False -	def _err1(resp): -		assert resp['message'] == 'Forbidden' -		assert resp['payload'] == {u'channels': [channel]} -	pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1) -	time.sleep(2) - - -	def _cb2(resp, ch=None): -		assert resp == 		{ -								'message': u'Success', -								'payload': {u'auths': {auth_key : {u'r': 1, u'w': 1}}, -								u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -								u'level': u'user', u'channel': channel, u'ttl': 10} -							} -	def _err2(resp): -		assert False - - -	pubnub_pam.grant(channel=channel, read=True, write=True, auth_key=auth_key, ttl=10, callback=_cb2, error=_err2) -	time.sleep(2) - -	def _cb3(resp, ch=None): -		assert resp[0] == 1 -	def _err3(resp): -		assert False - -	pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3) -	time.sleep(2) - -	def _cb4(resp, ch=None): -		assert resp == 		{ -								'message': u'Success', -								'payload': {u'auths': {auth_key : {u'r': 0, u'w': 0}}, -								u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -								u'level': u'user', u'channel': channel, u'ttl': 1} -							} -	def _err4(resp): -		assert False -	pubnub_pam.revoke(channel=channel, auth_key=auth_key, callback=_cb4, error=_err4) -	time.sleep(2) - -	def _cb5(resp, ch=None): -		assert False -	def _err5(resp): -		assert resp['message'] == 'Forbidden' -		assert resp['payload'] == {u'channels': [channel]} -	pubnub_pam.publish(channel=channel,message=message, callback=_cb5, error=_err5) -	time.sleep(2) - - - - -# Complete flow , try publish on forbidden channel, grant permission channel level for subkey and try again. -# then revoke and try again -def test_10(): -	channel = "test_10-" + str(time.time()) -	message = "Hello World" -	auth_key = "auth-" + channel -	pubnub_pam.set_auth_key(auth_key) - -	def _cb1(resp, ch=None): -		assert False -	def _err1(resp): -		assert resp['message'] == 'Forbidden' -		assert resp['payload'] == {u'channels': [channel]} -	pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1) -	time.sleep(2) - - -	def _cb2(resp, ch=None): -		assert resp == 		{ -									'message': u'Success', -									'payload': { u'channels': {channel: {u'r': 1, u'w': 1}}, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'channel', u'ttl': 10} -								} -	def _err2(resp): -		assert False - - -	pubnub_pam.grant(channel=channel, read=True, write=True, ttl=10, callback=_cb2, error=_err2) -	time.sleep(2) - -	def _cb3(resp, ch=None): -		assert resp[0] == 1 -	def _err3(resp): -		assert False - -	pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3) -	time.sleep(2) - -	def _cb4(resp, ch=None): -		assert resp == 		{ -									'message': u'Success', -									'payload': { u'channels': {channel : {u'r': 0, u'w': 0}}, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'channel', u'ttl': 1} -								} -	def _err4(resp): -		assert False -	pubnub_pam.revoke(channel=channel, callback=_cb4, error=_err4) -	time.sleep(2) - -	def _cb5(resp, ch=None): -		assert False -	def _err5(resp): -		assert resp['message'] == 'Forbidden' -		assert resp['payload'] == {u'channels': [channel]} -	pubnub_pam.publish(channel=channel,message=message, callback=_cb5, error=_err5) -	time.sleep(2) - - - -# Complete flow , try publish on forbidden channel, grant permission subkey level for subkey and try again. -# then revoke and try again -def test_11(): -	channel = "test_11-" + str(time.time()) -	message = "Hello World" -	auth_key = "auth-" + channel -	pubnub_pam.set_auth_key(auth_key) - -	def _cb1(resp, ch=None): -		assert False -	def _err1(resp): -		assert resp['message'] == 'Forbidden' -		assert resp['payload'] == {u'channels': [channel]} -	pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1) -	time.sleep(2) - - -	def _cb2(resp, ch=None): -		assert resp == 		{ -									'message': u'Success', -									'payload': { u'r': 1, u'w': 1, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'subkey', u'ttl': 10} -								} -	def _err2(resp): -		assert False - - -	pubnub_pam.grant(read=True, write=True, ttl=10, callback=_cb2, error=_err2) -	time.sleep(2) - -	def _cb3(resp, ch=None): -		assert resp[0] == 1 -	def _err3(resp): -		assert False - -	pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3) -	time.sleep(2) - -	def _cb4(resp, ch=None): -		assert resp == 		{ -									'message': u'Success', -									'payload': {u'r': 0, u'w': 0, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -									u'level': u'subkey', u'ttl': 1} -								} -	def _err4(resp): -		assert False -	pubnub_pam.revoke(callback=_cb4, error=_err4) -	time.sleep(2) - -	def _cb5(resp, ch=None): -		assert False -	def _err5(resp): -		assert resp['message'] == 'Forbidden' -		assert resp['payload'] == {u'channels': [channel]} -	pubnub_pam.publish(channel=channel,message=message, callback=_cb5, error=_err5) -	time.sleep(2) diff --git a/python/tests/test_history.py b/python/tests/test_history.py new file mode 100644 index 0000000..a5feb37 --- /dev/null +++ b/python/tests/test_history.py @@ -0,0 +1,58 @@ +from pubnub import Pubnub +import time +import random +from nose.tools import with_setup + + +pubnub = Pubnub("ds","ds") +pubnub_enc = Pubnub(publish_key="ds",subscribe_key="ds",cipher_key="enigma") +pubnub_pam = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam") + + +def rand(msg): +	return "rand-" + str(random.random()) + "-" + msg + +channel = rand("channel") +channel_enc = rand("channel_enc") +channel_pam = rand("channel_pam") + +messages = [] + + +def setup_func(): +     for i in range(0,20): +     	msg = rand("message-" + str(i)) +     	messages.append(msg) +     	pubnub_pam.grant(channel=channel_pam, read=True, write=True, ttl=144000) +     	pubnub.publish(channel=channel, message=msg) +     	pubnub_enc.publish(channel=channel_enc, message=msg) +     	pubnub_pam.publish(channel=channel_pam, message=msg) + + +@with_setup(setup_func) +def test_1(): +	time.sleep(3) +	hresp = pubnub.history(channel=channel, count=20) +	hresp2 = pubnub_enc.history(channel=channel_enc, count=20) +	hresp3 = pubnub_pam.history(channel=channel_pam, count=20) +	hresp4 = pubnub_pam.history(channel=channel_pam + "no_rw", count=20) +	assert hresp[0] == messages +	assert hresp2[0] == messages +	assert hresp3[0] == messages +	assert hresp4['message'] == 'Forbidden' +	assert channel_pam + "no_rw" in hresp4['payload']['channels'] + +def test_2(): +	time.sleep(3) +	hresp = pubnub.history(channel=channel, count=20, include_token=True) +	hresp2 = pubnub_enc.history(channel=channel_enc, count=20, include_token=True) +	hresp3 = pubnub_pam.history(channel=channel_pam, count=20, include_token=True) +	hresp4 = pubnub_pam.history(channel=channel_pam + "no_rw", count=20, include_token=True) +	assert len(hresp[0]) == len(messages) +	assert hresp[0][0]['timetoken'] +	assert len(hresp2[0]) == len(messages) +	assert hresp2[0][0]['timetoken'] +	assert len(hresp3[0]) == len(messages) +	assert hresp3[0][0]['timetoken'] +	assert hresp4['message'] == 'Forbidden' +	assert channel_pam + "no_rw" in hresp4['payload']['channels'] diff --git a/python/tests/test_publish_async.py b/python/tests/test_publish_async.py deleted file mode 100644 index 7270727..0000000 --- a/python/tests/test_publish_async.py +++ /dev/null @@ -1,228 +0,0 @@ - - -from Pubnub import Pubnub -import time - -pubnub = Pubnub("demo","demo") -pubnub_enc = Pubnub("demo", "demo", cipher_key="enigma") -pubnub_pam = Pubnub("pub-c-c077418d-f83c-4860-b213-2f6c77bde29a",  -	"sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe", "sec-c-OGU3Y2Q4ZWUtNDQwMC00NTI1LThjNWYtNWJmY2M4OGIwNjEy") - - - -# Publish and receive string -def test_1(): - -	channel = "test_1-" + str(time.time()) -	message = "I am a string" - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub.unsubscribe(channel) - -	def _connect(resp): -		pubnub.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive array -def test_2(): - -	channel = "test_2-" + str(time.time()) -	message = [1,2] - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub.unsubscribe(channel) - -	def _connect(resp): -		pubnub.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive json object -def test_3(): - -	channel = "test_2-" + str(time.time()) -	message = { "a" : "b" } - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub.unsubscribe(channel) - -	def _connect(resp): -		pubnub.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive number -def test_4(): - -	channel = "test_2-" + str(time.time()) -	message = 100 - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub.unsubscribe(channel) - -	def _connect(resp): -		pubnub.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive number string -def test_5(): - -	channel = "test_5-" + str(time.time()) -	message = "100" - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub.unsubscribe(channel) - -	def _connect(resp): -		pubnub.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - - -# Publish and receive string (Encryption enabled) -def test_6(): - -	channel = "test_6-" + str(time.time()) -	message = "I am a string" - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub_enc.unsubscribe(channel) - -	def _connect(resp): -		pubnub_enc.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive array (Encryption enabled) -def test_7(): - -	channel = "test_7-" + str(time.time()) -	message = [1,2] - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub_enc.unsubscribe(channel) - -	def _connect(resp): -		pubnub.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive json object (Encryption enabled) -def test_8(): - -	channel = "test_8-" + str(time.time()) -	message = { "a" : "b" } - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub_enc.unsubscribe(channel) - -	def _connect(resp): -		pubnub_enc.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive number (Encryption enabled) -def test_9(): - -	channel = "test_9-" + str(time.time()) -	message = 100 - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub_enc.unsubscribe(channel) - -	def _connect(resp): -		pubnub_enc.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive number string (Encryption enabled) -def test_10(): - -	channel = "test_10-" + str(time.time()) -	message = "100" - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub_enc.unsubscribe(channel) - -	def _connect(resp): -		pubnub_enc.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive object string (Encryption enabled) -def test_11(): - -	channel = "test_11-" + str(time.time()) -	message = '{"a" : "b"}' - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub_enc.unsubscribe(channel) - -	def _connect(resp): -		pubnub_enc.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive array string (Encryption enabled) -def test_12(): - -	channel = "test_12-" + str(time.time()) -	message = '[1,2]' - -	def _cb(resp, ch=None): -		assert resp == message -		pubnub_enc.unsubscribe(channel) - -	def _connect(resp): -		pubnub_enc.publish(channel,message) - -	def _error(resp): -		assert False - -	pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) | 
