-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtest_publish.py
201 lines (155 loc) · 6.08 KB
/
test_publish.py
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
import logging
import threading
import unittest
import pubnub
from pubnub.enums import PNStatusCategory
from pubnub.models.consumer.pubsub import PNPublishResult
from pubnub.pubnub import PubNub
from tests.helper import pnconf_enc_env_copy, pnconf_pam_env_copy, pnconf_env_copy
pubnub.set_stream_logger('pubnub', logging.DEBUG)
class TestPubNubSuccessPublish(unittest.TestCase):
def setUp(self):
self.event = threading.Event()
def callback(self, response, status):
self.response = response
self.status = status
self.event.set()
def assert_success(self):
self.event.wait()
if self.status.is_error():
self.fail(str(self.status.error_data.exception))
assert isinstance(self.response, PNPublishResult)
assert self.response.timetoken > 1
self.event.clear()
self.response = None
self.status = None
def assert_success_publish_get(self, msg):
PubNub(pnconf_env_copy()).publish() \
.channel("ch1") \
.message(msg) \
.pn_async(self.callback)
self.assert_success()
def assert_success_publish_post(self, msg):
PubNub(pnconf_env_copy()).publish() \
.channel("ch1") \
.message(msg) \
.use_post(True) \
.pn_async(self.callback)
self.assert_success()
def test_publish_get(self):
self.assert_success_publish_get("hi")
self.assert_success_publish_get(5)
self.assert_success_publish_get(True)
self.assert_success_publish_get(["hi", "hi2", "hi3"])
self.assert_success_publish_get({"name": "Alex", "online": True})
def test_publish_post(self):
self.assert_success_publish_post("hi")
self.assert_success_publish_post(5)
self.assert_success_publish_post(True)
self.assert_success_publish_post(["hi", "hi2", "hi3"])
self.assert_success_publish_post({"name": "Alex", "online": True})
def test_publish_encrypted_list_get(self):
pubnub = PubNub(pnconf_enc_env_copy())
pubnub.publish() \
.channel("ch1") \
.message(["encrypted", "list"]) \
.pn_async(self.callback)
self.assert_success()
def test_publish_encrypted_string_get(self):
PubNub(pnconf_enc_env_copy()).publish() \
.channel("ch1") \
.message("encrypted string") \
.pn_async(self.callback)
self.assert_success()
def test_publish_encrypted_list_post(self):
PubNub(pnconf_enc_env_copy()).publish() \
.channel("ch1") \
.message(["encrypted", "list"]) \
.use_post(True) \
.pn_async(self.callback)
self.assert_success()
def test_publish_encrypted_string_post(self):
PubNub(pnconf_enc_env_copy()).publish() \
.channel("ch1") \
.message("encrypted string") \
.use_post(True) \
.pn_async(self.callback)
self.assert_success()
def test_publish_with_meta(self):
meta = {'a': 2, 'b': 'qwer'}
PubNub(pnconf_enc_env_copy()).publish() \
.channel("ch1") \
.message("hey") \
.meta(meta) \
.pn_async(self.callback)
self.assert_success()
def test_publish_do_not_store(self):
PubNub(pnconf_enc_env_copy()).publish() \
.channel("ch1") \
.message("hey") \
.should_store(False) \
.pn_async(self.callback)
self.assert_success()
class TestPubNubErrorPublish(unittest.TestCase):
def setUp(self):
self.event = threading.Event()
def callback(self, response, status):
self.response = response
self.status = status
self.event.set()
def test_invalid_key(self):
self.invalid_key_message = ""
pn_fake_key_config = pnconf_env_copy()
pn_fake_key_config.publish_key = "fake"
PubNub(pn_fake_key_config).publish() \
.channel("ch1") \
.message("hey") \
.pn_async(self.callback)
self.event.wait()
assert self.status.is_error()
assert self.status.category is PNStatusCategory.PNBadRequestCategory
assert self.status.original_response[0] == 0
assert self.status.original_response[1] == 'Invalid Key'
assert "HTTP Client Error (400):" in str(self.status.error_data.exception)
assert "Invalid Key" in str(self.status.error_data.exception)
def test_missing_message(self):
PubNub(pnconf_env_copy()).publish() \
.channel("ch1") \
.message(None) \
.pn_async(self.callback)
self.event.wait()
assert self.status.is_error()
assert self.response is None
assert "Message missing" in str(self.status.error_data.exception)
def test_missing_chanel(self):
PubNub(pnconf_env_copy()).publish() \
.channel("") \
.message("hey") \
.pn_async(self.callback)
assert self.status.is_error()
assert self.response is None
assert "Channel missing" in str(self.status.error_data.exception)
def test_non_serializable(self):
def method():
pass
PubNub(pnconf_env_copy()).publish() \
.channel("ch1") \
.message(method) \
.pn_async(self.callback)
self.event.wait()
assert self.status.is_error()
assert self.response is None
assert "not JSON serializable" in str(self.status.error_data.exception)
def test_not_permitted(self):
pnconf = pnconf_pam_env_copy()
pnconf.secret_key = None
PubNub(pnconf).publish()\
.channel("not_permitted_channel")\
.message("correct message")\
.pn_async(self.callback)
self.event.wait()
assert self.status.is_error()
assert self.response is None
assert "HTTP Client Error (403)" in str(self.status.error_data.exception)
assert "Forbidden" in str(self.status.error_data.exception)
assert "Access Manager" in str(self.status.error_data.exception)