aboutsummaryrefslogtreecommitdiffstats
path: root/python-tornado/tests/test_grant_async.py
blob: b51b27562fea5c24fab73b21134ff5cc86a7892d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

from Pubnub import PubnubTornado as 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():

	def _callback(resp, ch= None):
		assert resp == {
									'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} 
								}

	def _error(response):
		assert False

	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=True, ttl=1, callback=_callback, error=_error)
							

# Grant permission read false, write false, on channel ( Async Mode )
def test_2():
	
	def _callback(resp, ch=None):
		assert resp == {
									'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}
								}

	def _error(response):
		assert False

	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=False, ttl=1, callback=_callback, error=_error)


# Grant permission read True, write false, on channel ( Async Mode )
def test_3():

	def _callback(resp, ch=None):
		assert resp == {
									'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}
								}

	def _error(response):
		assert False

	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=False, ttl=1, callback=_callback, error=_error)


# Grant permission read False, write True, on channel ( Async Mode )
def test_4():

	def _callback(resp, ch=None):
		assert resp == {
									'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}
								}

	def _error(response):
		assert False

	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=True, ttl=1, callback=_callback, error=_error)


# Grant permission read False, write True, on channel ( Async Mode ), TTL 10
def test_5():

	def _callback(resp,ch=None):
		assert resp == {
									'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}
								}


	def _error(response):
		assert False

	pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=True, ttl=10, callback=_callback, error=_error)


# Grant permission read False, write True, without channel ( Async Mode ), TTL 10
def test_6():
	def _callback(resp, ch=None):
		assert resp == {
										'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}
									}

	def _error(response):
		assert False

	pubnub_pam.grant(auth_key="abcd", read=False, write=True, ttl=10, callback=_callback, error=_error)



# Grant permission read False, write False, without channel ( Async Mode )
def test_7():

	def _callback(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 _error(response):
		resp['response'] = response

	pubnub_pam.grant(auth_key="abcd", read=False, write=False, callback=_callback, error=_error)


# 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]}
		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 _cb3(resp, ch=None):
				assert resp[0] == 1
			def _err3(resp):
				assert False

			pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3)
		def _err2(resp):
			assert False


		pubnub_pam.grant(channel=channel, read=True, write=True, auth_key=auth_key, ttl=10, callback=_cb2, error=_err2)

	pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1)


# 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]}
		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 _cb3(resp, ch=None):
				assert resp[0] == 1
				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 _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)
				def _err4(resp):
					assert False
				pubnub_pam.revoke(channel=channel, auth_key=auth_key, callback=_cb4, error=_err4)
			def _err3(resp):
				assert False

			pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3)
		def _err2(resp):
			assert False


		pubnub_pam.grant(channel=channel, read=True, write=True, auth_key=auth_key, ttl=10, callback=_cb2, error=_err2)

	pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1)


# 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]}
		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 _cb3(resp, ch=None):
				assert resp[0] == 1
				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 _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)
				def _err4(resp):
					assert False
				pubnub_pam.revoke(channel=channel, callback=_cb4, error=_err4)
			def _err3(resp):
				assert False

			pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3)
		def _err2(resp):
			assert False


		pubnub_pam.grant(channel=channel, read=True, write=True, ttl=10, callback=_cb2, error=_err2)

	pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1)






# 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]}
		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 _cb3(resp, ch=None):
				assert resp[0] == 1
				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 _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)
				def _err4(resp):
					assert False
				pubnub_pam.revoke(callback=_cb4, error=_err4)
			def _err3(resp):
				assert False

			pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3)
		def _err2(resp):
			assert False


		pubnub_pam.grant(read=True, write=True, ttl=10, callback=_cb2, error=_err2)

	pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1)


x = 5
def run_test(t):
	global x
	x += 5
	i = (x / 5) - 1
	def _print():
		print('Running test ' + str(i))
	pubnub.timeout(x, _print)
	pubnub.timeout(x + 1,t)

def stop():
	pubnub.stop()

run_test(test_1)
run_test(test_2)
run_test(test_3)
run_test(test_4)
run_test(test_5)
run_test(test_6)
run_test(test_7)
run_test(test_8)
run_test(test_9)
run_test(test_10)
run_test(test_11)
run_test(stop)


pubnub_pam.start()