From 19019a3466917e8ab2d8fd17df471aa34a07df1b Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 5 Jun 2015 23:31:51 +0530 Subject: pam demo --- python/examples/pam_demo/demo.py | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 python/examples/pam_demo/demo.py (limited to 'python') diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py new file mode 100644 index 0000000..e00914d --- /dev/null +++ b/python/examples/pam_demo/demo.py @@ -0,0 +1,74 @@ + +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 +channel_public = get_unique("channel_public") + +# server auth key +server_auth_token = get_unique("server_auth_token") + +#client auth key +client_auth_token = get_unique("client_auth_token") + +#client uuid +client_uuid = get_unique("client_uuid") + +#server uuid +server_uuid = get_unique("server_uuid") + + +#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) + +# Grant permission to server auth keys +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 +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=True, write=False)) + + + +def _server_callback(message, channel): + print(message) + +def _error_callback(error): + print(error) + +#server subscribes to public channel +server.subscribe(channels=channel_public, callback=_server_callback, error=_error_callback) + + + +def _server_presence_callback(message, channel): + print message + if 'action' in message: + if message['action'] == 'join' and message['uuid'] == client_uuid: + print "Only I can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) + +# server subscribes to presence events on public channel +server.presence(channel=channel_public, callback=_server_presence_callback, error=_error_callback) + + + + +def _client_callback(channel, message): + print(message) + +# client subscribes to public channel +client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_callback, error=_error_callback) -- cgit v1.2.3 From ae7dbdec9a5a9e147cf5e8de160455277d328f39 Mon Sep 17 00:00:00 2001 From: gcohen Date: Fri, 5 Jun 2015 13:28:52 -0700 Subject: Added verbose comments, and added a scenarion to depict the client trying to subscribe on a restricted presence channel. --- python/examples/pam_demo/demo.py | 74 ++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 22 deletions(-) (limited to 'python') diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py index e00914d..a60730c 100644 --- a/python/examples/pam_demo/demo.py +++ b/python/examples/pam_demo/demo.py @@ -1,4 +1,3 @@ - from gevent.monkey import patch_all patch_all() @@ -13,62 +12,93 @@ 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") # 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") -#client auth key +# 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 uuid +# 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 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 -#init server object +# 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 +# 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=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_callback(message, channel): - print(message) +def _server_message_callback(message, channel): + print("Server heard: " + message) + +def _client_callback(channel, message): + print("Client heard: " + message) def _error_callback(error): - print(error) + print("Error: " + error) -#server subscribes to public channel -server.subscribe(channels=channel_public, callback=_server_callback, error=_error_callback) +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=_error_callback) -def _server_presence_callback(message, channel): - print message - if 'action' in message: - if message['action'] == 'join' and message['uuid'] == client_uuid: - print "Only I can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) +# 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 -# server subscribes to presence events on public channel +## uncomment out to see server able to read on presence channel server.presence(channel=channel_public, callback=_server_presence_callback, error=_error_callback) +# now if the client tried to subscribe on the presence channel, and therefore, get state info +# he is explicitly denied! - - -def _client_callback(channel, message): - print(message) +## uncomment out to see client not able to read on presence channel +#client.presence(channel=channel_public, callback=_client_presence_callback, error=_error_callback) # client subscribes to public channel client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_callback, error=_error_callback) -- cgit v1.2.3 From d2f95778fab683b573c6778dafdd8d0006245d93 Mon Sep 17 00:00:00 2001 From: gcohen Date: Mon, 8 Jun 2015 14:54:00 -0700 Subject: Working example of secure PAM key exchange with client and server actors. --- python/examples/pam_demo/demo.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'python') diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py index a60730c..ff91b76 100644 --- a/python/examples/pam_demo/demo.py +++ b/python/examples/pam_demo/demo.py @@ -15,17 +15,20 @@ def get_unique(s): # 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 = 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 = 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 = 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 @@ -64,13 +67,18 @@ print(server.grant(channel=channel_public + '-pnpres', auth_key=client_auth_toke # Define some simple callabcks for the Server and Client def _server_message_callback(message, channel): - print("Server heard: " + message) + print("Server heard: " + json.dumps(message)) -def _client_callback(channel, message): - print("Client heard: " + message) +def _client_message_callback(message, channel): + print("Client heard: " + json.dumps(message)) -def _error_callback(error): - print("Error: " + error) +def _client_error_callback(error, channel): + print("Client Error: " + error + " on channel " + channel) + print("Client will now unsubscribe from this unauthorized channel.") + client.unsubscribe(channel=channel) + +def _server_error_callback(error): + print("Server Error: " + error) def _server_presence_callback(message, channel): print message @@ -85,20 +93,20 @@ def _client_presence_callback(message, channel): 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=_error_callback) +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=_error_callback) +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=_error_callback) +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_callback, error=_error_callback) +client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_message_callback, error=_client_error_callback) -- cgit v1.2.3 From 589a28dbf9495c3d2d904d3ffd46947278eb0a7b Mon Sep 17 00:00:00 2001 From: gcohen Date: Mon, 8 Jun 2015 18:07:40 -0700 Subject: Working example of secure PAM key exchange with client and server actors. --- python/examples/pam_demo/demo.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'python') diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py index ff91b76..decef8e 100644 --- a/python/examples/pam_demo/demo.py +++ b/python/examples/pam_demo/demo.py @@ -74,11 +74,13 @@ def _client_message_callback(message, channel): def _client_error_callback(error, channel): print("Client Error: " + error + " on channel " + channel) - print("Client will now unsubscribe from this unauthorized 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): - print("Server Error: " + error) +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 -- cgit v1.2.3 From 2f5c00ed2c1f2def95c053eee513231a81504fc4 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 12 Jun 2015 00:47:36 +0530 Subject: removing pam_demo example from develop --- python/examples/pam_demo/demo.py | 104 --------------------------------------- 1 file changed, 104 deletions(-) delete mode 100644 python/examples/pam_demo/demo.py (limited to 'python') diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py deleted file mode 100644 index a60730c..0000000 --- a/python/examples/pam_demo/demo.py +++ /dev/null @@ -1,104 +0,0 @@ -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") - -# 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") - -# 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") - -# 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: " + message) - -def _client_callback(channel, message): - print("Client heard: " + message) - -def _error_callback(error): - print("Error: " + error) - -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=_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=_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=_error_callback) - -# client subscribes to public channel -client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_callback, error=_error_callback) -- cgit v1.2.3 From 2ed5ac76e9719d3ba53ed9f7245c53e37347bddd Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 16 Jun 2015 14:10:23 +0530 Subject: changing Pubnub to pubnub in files --- python/examples/audit.py | 2 +- python/examples/console.py | 2 +- python/examples/cr.py | 2 +- python/examples/dev-console.py | 2 +- python/examples/grant.py | 2 +- python/examples/here-now.py | 2 +- python/examples/history.py | 2 +- python/examples/presence.py | 2 +- python/examples/publish.py | 4 ++-- python/examples/pubnub-console/pubnub-console | 2 +- python/examples/revoke.py | 2 +- python/examples/subscribe.py | 9 ++++++--- python/examples/subscribe_group.py | 2 +- python/tests/subscribe-test.py | 2 +- python/tests/test_cg.py | 2 +- python/tests/test_grant.py | 2 +- python/tests/test_grant_async.py | 2 +- python/tests/test_publish_async.py | 2 +- 18 files changed, 24 insertions(+), 21 deletions(-) (limited to 'python') 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..5b92828 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' 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 index a1b1826..7a4b723 100755 --- a/python/tests/subscribe-test.py +++ b/python/tests/subscribe-test.py @@ -11,7 +11,7 @@ import sys import datetime -from Pubnub import PubnubAsync as Pubnub +from pubnub import PubnubAsync as Pubnub from functools import partial from threading import current_thread import threading 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 index 4d38a0a..07c8a2c 100644 --- a/python/tests/test_grant_async.py +++ b/python/tests/test_grant_async.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_publish_async.py b/python/tests/test_publish_async.py index 7270727..cf0811e 100644 --- a/python/tests/test_publish_async.py +++ b/python/tests/test_publish_async.py @@ -1,6 +1,6 @@ -from Pubnub import Pubnub +from pubnub import Pubnub import time pubnub = Pubnub("demo","demo") -- cgit v1.2.3 From e9e089132be7a9602cccb49257acbb9719734ad4 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 19 Jun 2015 00:53:30 +0530 Subject: changing import --- python/examples/pam_demo/demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python') diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py index decef8e..c08f9f5 100644 --- a/python/examples/pam_demo/demo.py +++ b/python/examples/pam_demo/demo.py @@ -2,7 +2,7 @@ from gevent.monkey import patch_all patch_all() import sys -from Pubnub import Pubnub +from pubnub import Pubnub import random import json -- cgit v1.2.3 From d6455babd00c0747559c6d1a21fc195e1f18dbc7 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 19 Jun 2015 02:58:40 +0530 Subject: added history test --- python/tests/subscribe-test.py | 154 ---------------- python/tests/test_grant_async.py | 369 ------------------------------------- python/tests/test_history.py | 37 ++++ python/tests/test_publish_async.py | 228 ----------------------- 4 files changed, 37 insertions(+), 751 deletions(-) delete mode 100755 python/tests/subscribe-test.py delete mode 100644 python/tests/test_grant_async.py create mode 100644 python/tests/test_history.py delete mode 100644 python/tests/test_publish_async.py (limited to 'python') diff --git a/python/tests/subscribe-test.py b/python/tests/subscribe-test.py deleted file mode 100755 index 7a4b723..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_grant_async.py b/python/tests/test_grant_async.py deleted file mode 100644 index 07c8a2c..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..3cf8e03 --- /dev/null +++ b/python/tests/test_history.py @@ -0,0 +1,37 @@ +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") + + +def rand(msg): + return "rand-" + str(random.random()) + "-" + msg + +channel = rand("channel") +channel_enc = rand("channel_enc") + +messages = [] + + +def setup_func(): + for i in range(0,20): + msg = rand("message-" + str(i)) + messages.append(msg) + pubnub.publish(channel=channel, message=msg) + pubnub_enc.publish(channel=channel_enc, 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) + assert hresp[0] == messages + assert hresp2[0] == messages + + + diff --git a/python/tests/test_publish_async.py b/python/tests/test_publish_async.py deleted file mode 100644 index cf0811e..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) -- cgit v1.2.3 From 276bf54fcccd73cf694487d59e9e3319b8bd8616 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 19 Jun 2015 03:27:23 +0530 Subject: history fix --- python/tests/test_history.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'python') diff --git a/python/tests/test_history.py b/python/tests/test_history.py index 3cf8e03..1592e11 100644 --- a/python/tests/test_history.py +++ b/python/tests/test_history.py @@ -6,6 +6,7 @@ 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): @@ -13,6 +14,7 @@ def rand(msg): channel = rand("channel") channel_enc = rand("channel_enc") +channel_pam = rand("channel_pam") messages = [] @@ -21,8 +23,10 @@ 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) @@ -30,8 +34,13 @@ 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'] -- cgit v1.2.3 From 4c2bb361dcad44226d37cfdb6b8f9d3e2f6789ff Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 19 Jun 2015 23:28:23 +0530 Subject: adding include_token test --- python/tests/test_history.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'python') diff --git a/python/tests/test_history.py b/python/tests/test_history.py index 1592e11..a5feb37 100644 --- a/python/tests/test_history.py +++ b/python/tests/test_history.py @@ -42,5 +42,17 @@ def test_1(): 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'] -- cgit v1.2.3 From da687dca4001f64662540bb0791508a5a8a41fe0 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 19 Jun 2015 23:57:26 +0530 Subject: history include_token --- python/examples/history.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'python') diff --git a/python/examples/history.py b/python/examples/history.py index 5b92828..19593e1 100644 --- a/python/examples/history.py +++ b/python/examples/history.py @@ -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) -- cgit v1.2.3 From 232f7389274f0d9ff06835fc5da0970f7918ba25 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 19 Jun 2015 23:57:53 +0530 Subject: readme update --- python/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'python') diff --git a/python/README.md b/python/README.md index 33e6235..91ca9f9 100644 --- a/python/README.md +++ b/python/README.md @@ -191,6 +191,23 @@ def callback(message): pubnub.history(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 ``` -- cgit v1.2.3