diff options
| author | Devendra | 2014-11-25 19:29:23 +0530 | 
|---|---|---|
| committer | Devendra | 2014-11-25 19:29:23 +0530 | 
| commit | 99b1453493c82812333062fabe9e91143b2ff0c5 (patch) | |
| tree | 9033ef0056484dc93eece977a06d0b84eee76e35 | |
| parent | f97718ca0682a627857ae66f454582ec71670716 (diff) | |
| download | pubnub-python-99b1453493c82812333062fabe9e91143b2ff0c5.tar.bz2 | |
channel group support admin api's only
| -rw-r--r-- | Pubnub.py | 26 | ||||
| -rw-r--r-- | python/examples/cr.py | 16 | ||||
| -rw-r--r-- | python/tests/test_cg.py | 107 | ||||
| -rw-r--r-- | python/tests/test_grant.py | 61 | 
4 files changed, 170 insertions, 40 deletions
| @@ -19,6 +19,7 @@ except ImportError:  import time  import hashlib  import uuid as uuid_lib +import random  import sys  from base64 import urlsafe_b64encode  from base64 import encodestring, decodestring @@ -320,6 +321,9 @@ class PubnubBase(object):              sha256          ).digest()) +    def set_u(self, u=False): +        self.u = u +      def _pam_auth(self, query, apicode=0, callback=None, error=None):          if 'timestamp' not in query: @@ -332,6 +336,10 @@ class PubnubBase(object):          if 'channel' in query and not query['channel']:              del query['channel'] +        if 'channel-group' in query and not query['channel-group']: +            del query['channel-group'] + +          params = "&".join([              x + "=" + quote(                  str(query[x]), safe="" @@ -362,8 +370,8 @@ class PubnubBase(object):      def get_auth_key(self):          return auth_key -    def grant(self, channel=None, auth_key=False, read=True, -              write=True, ttl=5, callback=None, error=None): +    def grant(self, channel=None, channel_group=None, auth_key=False, read=False, +              write=False, manage=False, ttl=5, callback=None, error=None):          """Method for granting permissions.          This function establishes subscribe and/or write permissions for @@ -437,14 +445,16 @@ class PubnubBase(object):          return self._pam_auth({              'channel'   : channel, +            'channel-group'   : channel_group,              'auth'      : auth_key,              'r'         : read and 1 or 0,              'w'         : write and 1 or 0, +            'm'         : manage and 1 or 0,              'ttl'       : ttl,              'pnsdk'     : self.pnsdk          }, callback=callback, error=error) -    def revoke(self, channel=None, auth_key=None, ttl=1, callback=None, error=None): +    def revoke(self, channel=None, channel_group=None, auth_key=None, ttl=1, callback=None, error=None):          """Method for revoking permissions.          Args: @@ -501,6 +511,7 @@ class PubnubBase(object):          return self._pam_auth({              'channel'   : channel, +            'channel-group' : channel_group,              'auth'      : auth_key,              'r'         : 0,              'w'         : 0, @@ -508,7 +519,7 @@ class PubnubBase(object):              'pnsdk'     : self.pnsdk          }, callback=callback, error=error) -    def audit(self, channel=None, auth_key=None, callback=None, error=None): +    def audit(self, channel=None, channel_group=None, auth_key=None, callback=None, error=None):          """Method for fetching permissions from pubnub servers.          This method provides a mechanism to reveal existing PubNub Access Manager attributes @@ -564,6 +575,7 @@ class PubnubBase(object):          return self._pam_auth({              'channel'   : channel, +            'channel-group' : channel_group,              'auth'      : auth_key,              'pnsdk'     : self.pnsdk          }, 1, callback=callback, error=error) @@ -869,6 +881,9 @@ class PubnubBase(object):                       ]) for bit in request]      def getUrl(self, request): +  +        if self.u is True and "urlparams" in request: +            request['urlparams']['u'] = str(random.randint(1, 100000000000))          ## Build URL          url = self.origin + '/' + "/".join([              "".join([' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.find(ch) > -1 and @@ -878,7 +893,7 @@ class PubnubBase(object):          if ("urlparams" in request):              url = url + '?' + "&".join([x + "=" + str(y) for x, y in request[                  "urlparams"].items() if y is not None]) -        print url +          return url      def _channel_registry(self, url=None, params=None, callback=None, error=None): @@ -1015,6 +1030,7 @@ class PubnubCoreAsync(PubnubBase):          self._tt_lock = _tt_lock          self._channel_list_lock = _channel_list_lock          self._connect = lambda: None +        self.u = None      def get_channel_list(self, channels):          channel = '' diff --git a/python/examples/cr.py b/python/examples/cr.py index ad8e3c9..c537780 100644 --- a/python/examples/cr.py +++ b/python/examples/cr.py @@ -9,9 +9,9 @@  import sys  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' -secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo' +publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam' +subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' +secret_key = len(sys.argv) > 3 and sys.argv[3] or 'pam'  cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''  ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False @@ -19,12 +19,15 @@ 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, auth_key="abcd")  channel = 'hello_world'  def callback(message):      print(message) +print pubnub.revoke(channel_group='dev:abcd', auth_key="abcd") +print pubnub.audit(channel_group="dev:abcd") +print pubnub.grant(channel_group='dev:abcd', read=True, write=True, manage=True, auth_key="abcd")  print pubnub.channel_group_list_namespaces()  print pubnub.channel_group_list_groups(namespace='aaa')  print pubnub.channel_group_list_groups(namespace='foo') @@ -35,6 +38,9 @@ print pubnub.channel_group_remove_channel(channel_group='dev:abcd', channel="hi"  print pubnub.channel_group_list_channels(channel_group='dev:abcd') +pubnub.revoke(channel_group='dev:abcd', auth_key="abcd", callback=callback, error=callback) +pubnub.audit(channel_group="dev:abcd", callback=callback, error=callback) +pubnub.grant(channel_group='dev:abcd', read=True, write=True, manage=True, auth_key="abcd", callback=callback, error=callback)  pubnub.channel_group_list_namespaces(callback=callback, error=callback)  pubnub.channel_group_list_groups(namespace='aaa', callback=callback, error=callback)  pubnub.channel_group_list_groups(namespace='foo', callback=callback, error=callback) @@ -42,4 +48,4 @@ pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback,  pubnub.channel_group_add_channel(channel_group='dev:abcd', channel="hi", callback=callback, error=callback)  pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback, error=callback)  pubnub.channel_group_remove_channel(channel_group='dev:abcd', channel="hi", callback=callback, error=callback) -pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback, error=callback)
\ No newline at end of file +pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback, error=callback) diff --git a/python/tests/test_cg.py b/python/tests/test_cg.py new file mode 100644 index 0000000..a823e44 --- /dev/null +++ b/python/tests/test_cg.py @@ -0,0 +1,107 @@ +from Pubnub import Pubnub +import time +import random + + +pubnub = Pubnub("demo","demo") +pubnub.set_u(True) + +def rand_str(s): +	return str(s) + '-' + str(random.randint(1, 100000000000)) + + +def test_1(): +	channel 		= rand_str('channel') +	channel2 		= rand_str('channel') +	channel_group 	= rand_str('group') +	channel_group2  = rand_str('group') +	namespace       = rand_str('ns') + +	resp = pubnub.channel_group_add_channel(channel_group=namespace + ':' + channel_group, channel=channel) +	assert resp['status'] 	== 200 +	assert resp['message'] 	== 'OK' +	assert resp['error'] 	== False + +	resp = pubnub.channel_group_add_channel(channel_group=namespace + ':' + channel_group, channel=channel2) +	assert resp['status'] 	== 200 +	assert resp['message'] 	== 'OK' +	assert resp['error'] 	== False + +	resp = pubnub.channel_group_add_channel(channel_group=namespace + ':' + channel_group2, channel=channel) +	assert resp['status'] 	== 200 +	assert resp['message'] 	== 'OK' +	assert resp['error'] 	== False + +	resp = pubnub.channel_group_add_channel(channel_group=namespace + ':' + channel_group2, channel=channel2) +	assert resp['status'] 	== 200 +	assert resp['message'] 	== 'OK' +	assert resp['error'] 	== False + + +	resp = pubnub.channel_group_list_channels(channel_group=namespace + ':' + channel_group) +	assert channel in resp['payload']['channels'] +	assert channel2 in resp['payload']['channels'] +	assert len(resp['payload']['channels']) == 2 + +	resp = pubnub.channel_group_remove_channel(channel_group=namespace + ':' + channel_group, channel=channel2) +	print resp +	assert resp['status'] 	== 200 +	assert resp['message'] 	== 'OK' +	assert resp['error'] 	== False + +	resp = pubnub.channel_group_list_channels(channel_group=namespace + ':' + channel_group) +	print resp +	assert channel in resp['payload']['channels'] +	assert len(resp['payload']['channels']) == 1 + + +	resp = pubnub.channel_group_list_channels(channel_group=namespace + ':' + channel_group2) +	assert channel in resp['payload']['channels'] +	assert channel2 in resp['payload']['channels'] +	assert len(resp['payload']['channels']) == 2 + +	resp = pubnub.channel_group_remove_channel(channel_group=namespace + ':' + channel_group2, channel=channel2) +	print resp +	assert resp['status'] 	== 200 +	assert resp['message'] 	== 'OK' +	assert resp['error'] 	== False + +	resp = pubnub.channel_group_list_channels(channel_group=namespace + ':' + channel_group2) +	print resp +	assert channel in resp['payload']['channels'] +	assert len(resp['payload']['channels']) == 1 + + + +	resp = pubnub.channel_group_list_groups(namespace=namespace) +	assert channel_group in resp['payload']['groups'] +	assert channel_group2 in resp['payload']['groups'] +	assert len(resp['payload']['groups']) == 2 + +	resp = pubnub.channel_group_remove_group(channel_group=namespace + ':' + channel_group2) +	print resp +	assert resp['status'] 	== 200 +	assert resp['message'] 	== 'OK' +	assert resp['error'] 	== False + + +	resp = pubnub.channel_group_list_groups(namespace=namespace) +	assert channel_group in resp['payload']['groups'] +	assert len(resp['payload']['groups']) == 1 + + +	resp = pubnub.channel_group_list_namespaces() +	assert namespace in resp['payload']['namespaces'] + +	resp = pubnub.channel_group_remove_namespace(namespace=namespace) +	print resp +	assert resp['status'] 	== 200 +	assert resp['message'] 	== 'OK' +	assert resp['error'] 	== False + +	resp = pubnub.channel_group_list_namespaces() +	assert namespace not in resp['payload']['namespaces'] + + + + diff --git a/python/tests/test_grant.py b/python/tests/test_grant.py index 6826335..9ecafdf 100644 --- a/python/tests/test_grant.py +++ b/python/tests/test_grant.py @@ -4,17 +4,18 @@ 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") +pubnub_pam = Pubnub("pam",  +	"pam", "pam")  # Grant permission read true, write true, on channel ( Sync Mode )  def test_1():  	resp = pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=True, ttl=1) +	print resp  	assert resp['message'] == 'Success'  	assert resp['payload'] == { -								'auths': {'abcd': {'r': 1, 'w': 1}}, -								'subscribe_key': 'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', +								'auths': {'abcd': {'r': 1, 'w': 1, 'm' : 0}}, +								'subscribe_key': 'pam',  								'level': 'user', 'channel': 'abcd', 'ttl': 1  							  } @@ -24,8 +25,8 @@ def test_2():  	resp = pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=False, ttl=1)  	assert resp['message'] == 'Success'  	assert resp['payload'] == { -								'auths': {'abcd': {'r': 0, 'w': 0}}, -								'subscribe_key': 'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', +								'auths': {'abcd': {'r': 0, 'w': 0, 'm' : 0}}, +								'subscribe_key': 'pam',  								'level': 'user', 'channel': 'abcd', 'ttl': 1  							  } @@ -34,8 +35,8 @@ def test_3():  	resp = pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=False, ttl=1)  	assert resp['message'] == 'Success'  	assert resp['payload'] == { -								'auths': {'abcd': {'r': 1, 'w': 0}}, -								'subscribe_key': 'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', +								'auths': {'abcd': {'r': 1, 'w': 0, 'm' : 0}}, +								'subscribe_key': 'pam',  								'level': 'user', 'channel': 'abcd', 'ttl': 1  							  } @@ -44,8 +45,8 @@ def test_4():  	resp = pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=False, ttl=1)  	assert resp['message'] == 'Success'  	assert resp['payload'] == { -								'auths': {'abcd': {'r': 1, 'w': 0}}, -								'subscribe_key': 'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', +								'auths': {'abcd': {'r': 1, 'w': 0, 'm' : 0}}, +								'subscribe_key': 'pam',  								'level': 'user', 'channel': 'abcd', 'ttl': 1  							  } @@ -54,8 +55,8 @@ def test_5():  	resp = pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=False, ttl=10)  	assert resp['message'] == 'Success'  	assert resp['payload'] == { -								'auths': {'abcd': {'r': 1, 'w': 0}}, -								'subscribe_key': 'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', +								'auths': {'abcd': {'r': 1, 'w': 0, 'm' : 0}}, +								'subscribe_key': 'pam',  								'level': 'user', 'channel': 'abcd', 'ttl': 10  							  } @@ -64,8 +65,8 @@ def test_6():  	resp = pubnub_pam.grant(auth_key="abcd", read=True, write=False, ttl=10)  	assert resp['message'] == 'Success'  	assert resp['payload'] == { -								'subscribe_key': 'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -								'level': 'subkey' , u'r': 1, u'w': 0, 'ttl': 10 +								'subscribe_key': 'pam', +								'level': 'subkey' , u'r': 1, u'w': 0, 'm' : 0, 'ttl': 10  							  } @@ -74,8 +75,8 @@ def test_7():  	resp = pubnub_pam.grant(auth_key="abcd", read=False, write=False)  	assert resp['message'] == 'Success'  	assert resp['payload'] == { -								'subscribe_key': 'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', -								'level': 'subkey' , u'r': 0, u'w': 0, 'ttl': 1 +								'subscribe_key': 'pam', +								'level': 'subkey' , u'r': 0, u'w': 0, 'm' : 0, 'ttl': 1  							  } @@ -92,8 +93,8 @@ def test_8():  	resp = pubnub_pam.grant(channel=channel, read=True, write=True, auth_key=auth_key, ttl=10)  	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', +								'payload': {u'auths': {auth_key : {u'r': 1, u'w': 1, 'm' : 0}}, +								u'subscribe_key': u'pam',  								u'level': u'user', u'channel': channel, u'ttl': 10}  							}  	resp = pubnub_pam.publish(channel=channel,message=message) @@ -114,8 +115,8 @@ def test_9():  	print resp  	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', +								'payload': {u'auths': {auth_key : {u'r': 1, u'w': 1, 'm' : 0}}, +								u'subscribe_key': u'pam',  								u'level': u'user', u'channel': channel, u'ttl': 10}  							}  	resp = pubnub_pam.publish(channel=channel,message=message) @@ -124,8 +125,8 @@ def test_9():  	print resp  	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', +								'payload': {u'auths': {auth_key : {u'r': 0, u'w': 0, 'm' : 0}}, +								u'subscribe_key': u'pam',  								u'level': u'user', u'channel': channel, u'ttl': 1}  							}  	resp = pubnub_pam.publish(channel=channel,message=message) @@ -146,8 +147,8 @@ def test_10():  	print resp  	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', +									'payload': { u'channels': {channel: {u'r': 1, u'w': 1, 'm' : 0}}, +									u'subscribe_key': u'pam',  									u'level': u'channel', u'ttl': 10}  								}  	resp = pubnub_pam.publish(channel=channel,message=message) @@ -156,8 +157,8 @@ def test_10():  	print resp  	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', +									'payload': { u'channels': {channel : {u'r': 0, u'w': 0, 'm' : 0}}, +									u'subscribe_key': u'pam',  									u'level': u'channel', u'ttl': 1}  								}  	resp = pubnub_pam.publish(channel=channel,message=message) @@ -178,8 +179,8 @@ def test_11():  	print resp  	assert resp == 			{  									'message': u'Success', -									'payload': { u'r': 1, u'w': 1, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', +									'payload': { u'r': 1, u'w': 1, 'm' : 0, +									u'subscribe_key': u'pam',  									u'level': u'subkey', u'ttl': 10}  								}  	resp = pubnub_pam.publish(channel=channel,message=message) @@ -188,8 +189,8 @@ def test_11():  	print resp  	assert resp == 			{  									'message': u'Success', -									'payload': {u'r': 0, u'w': 0, -									u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', +									'payload': {u'r': 0, u'w': 0, 'm' : 0, +									u'subscribe_key': u'pam',  									u'level': u'subkey', u'ttl': 1}  								}  	resp = pubnub_pam.publish(channel=channel,message=message) | 
