forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_topic.py
More file actions
803 lines (662 loc) · 30.4 KB
/
Copy pathtest_topic.py
File metadata and controls
803 lines (662 loc) · 30.4 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
class TestTopic(unittest.TestCase):
PROJECT = 'PROJECT'
TOPIC_NAME = 'topic_name'
TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
def _getTargetClass(self):
from google.cloud.pubsub.topic import Topic
return Topic
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor_w_explicit_timestamp(self):
client = _Client(project=self.PROJECT)
topic = self._makeOne(self.TOPIC_NAME,
client=client,
timestamp_messages=True)
self.assertEqual(topic.name, self.TOPIC_NAME)
self.assertEqual(topic.project, self.PROJECT)
self.assertEqual(topic.full_name, self.TOPIC_PATH)
self.assertTrue(topic.timestamp_messages)
def test_from_api_repr(self):
client = _Client(project=self.PROJECT)
resource = {'name': self.TOPIC_PATH}
klass = self._getTargetClass()
topic = klass.from_api_repr(resource, client=client)
self.assertEqual(topic.name, self.TOPIC_NAME)
self.assertIs(topic._client, client)
self.assertEqual(topic.project, self.PROJECT)
self.assertEqual(topic.full_name, self.TOPIC_PATH)
def test_from_api_repr_with_bad_client(self):
PROJECT1 = 'PROJECT1'
PROJECT2 = 'PROJECT2'
client = _Client(project=PROJECT1)
PATH = 'projects/%s/topics/%s' % (PROJECT2, self.TOPIC_NAME)
resource = {'name': PATH}
klass = self._getTargetClass()
self.assertRaises(ValueError, klass.from_api_repr,
resource, client=client)
def test_create_w_bound_client(self):
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_create_response = {'name': self.TOPIC_PATH}
topic = self._makeOne(self.TOPIC_NAME, client=client)
topic.create()
self.assertEqual(api._topic_created, self.TOPIC_PATH)
def test_create_w_alternate_client(self):
client1 = _Client(project=self.PROJECT)
client2 = _Client(project=self.PROJECT)
api = client2.publisher_api = _FauxPublisherAPI()
api._topic_create_response = {'name': self.TOPIC_PATH}
topic = self._makeOne(self.TOPIC_NAME, client=client1)
topic.create(client=client2)
self.assertEqual(api._topic_created, self.TOPIC_PATH)
def test_exists_miss_w_bound_client(self):
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
topic = self._makeOne(self.TOPIC_NAME, client=client)
self.assertFalse(topic.exists())
self.assertEqual(api._topic_got, self.TOPIC_PATH)
def test_exists_hit_w_alternate_client(self):
client1 = _Client(project=self.PROJECT)
client2 = _Client(project=self.PROJECT)
api = client2.publisher_api = _FauxPublisherAPI()
api._topic_get_response = {'name': self.TOPIC_PATH}
topic = self._makeOne(self.TOPIC_NAME, client=client1)
self.assertTrue(topic.exists(client=client2))
self.assertEqual(api._topic_got, self.TOPIC_PATH)
def test_delete_w_bound_client(self):
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_delete_response = {}
topic = self._makeOne(self.TOPIC_NAME, client=client)
topic.delete()
self.assertEqual(api._topic_deleted, self.TOPIC_PATH)
def test_delete_w_alternate_client(self):
client1 = _Client(project=self.PROJECT)
client2 = _Client(project=self.PROJECT)
api = client2.publisher_api = _FauxPublisherAPI()
api._topic_delete_response = {}
topic = self._makeOne(self.TOPIC_NAME, client=client1)
topic.delete(client=client2)
self.assertEqual(api._topic_deleted, self.TOPIC_PATH)
def test_publish_single_bytes_wo_attrs_w_bound_client(self):
import base64
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MSGID = 'DEADBEEF'
MESSAGE = {'data': B64, 'attributes': {}}
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID]
topic = self._makeOne(self.TOPIC_NAME, client=client)
msgid = topic.publish(PAYLOAD)
self.assertEqual(msgid, MSGID)
self.assertEqual(api._topic_published, (self.TOPIC_PATH, [MESSAGE]))
def test_publish_single_bytes_wo_attrs_w_add_timestamp_alt_client(self):
import base64
import datetime
from google.cloud.pubsub import topic as MUT
from google.cloud._helpers import _RFC3339_MICROS
from google.cloud._testing import _Monkey
NOW = datetime.datetime.utcnow()
def _utcnow():
return NOW
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MSGID = 'DEADBEEF'
MESSAGE = {
'data': B64,
'attributes': {'timestamp': NOW.strftime(_RFC3339_MICROS)},
}
client1 = _Client(project=self.PROJECT)
client2 = _Client(project=self.PROJECT)
api = client2.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID]
topic = self._makeOne(self.TOPIC_NAME, client=client1,
timestamp_messages=True)
with _Monkey(MUT, _NOW=_utcnow):
msgid = topic.publish(PAYLOAD, client=client2)
self.assertEqual(msgid, MSGID)
self.assertEqual(api._topic_published, (self.TOPIC_PATH, [MESSAGE]))
def test_publish_single_bytes_w_add_timestamp_w_ts_in_attrs(self):
import base64
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MSGID = 'DEADBEEF'
OVERRIDE = '2015-04-10T16:46:22.868399Z'
MESSAGE = {'data': B64,
'attributes': {'timestamp': OVERRIDE}}
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID]
topic = self._makeOne(self.TOPIC_NAME, client=client,
timestamp_messages=True)
msgid = topic.publish(PAYLOAD, timestamp=OVERRIDE)
self.assertEqual(msgid, MSGID)
self.assertEqual(api._topic_published, (self.TOPIC_PATH, [MESSAGE]))
def test_publish_single_w_attrs(self):
import base64
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MSGID = 'DEADBEEF'
MESSAGE = {'data': B64,
'attributes': {'attr1': 'value1', 'attr2': 'value2'}}
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID]
topic = self._makeOne(self.TOPIC_NAME, client=client)
msgid = topic.publish(PAYLOAD, attr1='value1', attr2='value2')
self.assertEqual(msgid, MSGID)
self.assertEqual(api._topic_published, (self.TOPIC_PATH, [MESSAGE]))
def test_publish_multiple_w_bound_client(self):
import base64
PAYLOAD1 = b'This is the first message text'
PAYLOAD2 = b'This is the second message text'
B64_1 = base64.b64encode(PAYLOAD1)
B64_2 = base64.b64encode(PAYLOAD2)
MSGID1 = 'DEADBEEF'
MSGID2 = 'BEADCAFE'
MESSAGE1 = {'data': B64_1.decode('ascii'),
'attributes': {}}
MESSAGE2 = {'data': B64_2.decode('ascii'),
'attributes': {'attr1': 'value1', 'attr2': 'value2'}}
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID1, MSGID2]
topic = self._makeOne(self.TOPIC_NAME, client=client)
with topic.batch() as batch:
batch.publish(PAYLOAD1)
batch.publish(PAYLOAD2, attr1='value1', attr2='value2')
self.assertEqual(list(batch), [MSGID1, MSGID2])
self.assertEqual(list(batch.messages), [])
self.assertEqual(api._topic_published,
(self.TOPIC_PATH, [MESSAGE1, MESSAGE2]))
def test_publish_w_no_messages(self):
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = []
topic = self._makeOne(self.TOPIC_NAME, client=client)
with topic.batch() as batch:
pass
self.assertEqual(list(batch.messages), [])
self.assertEqual(api._api_called, 0)
def test_publish_multiple_w_alternate_client(self):
import base64
PAYLOAD1 = b'This is the first message text'
PAYLOAD2 = b'This is the second message text'
B64_1 = base64.b64encode(PAYLOAD1)
B64_2 = base64.b64encode(PAYLOAD2)
MSGID1 = 'DEADBEEF'
MSGID2 = 'BEADCAFE'
MESSAGE1 = {'data': B64_1.decode('ascii'), 'attributes': {}}
MESSAGE2 = {
'data': B64_2.decode('ascii'),
'attributes': {'attr1': 'value1', 'attr2': 'value2'},
}
client1 = _Client(project=self.PROJECT)
client2 = _Client(project=self.PROJECT)
api = client2.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID1, MSGID2]
topic = self._makeOne(self.TOPIC_NAME, client=client1)
with topic.batch(client=client2) as batch:
batch.publish(PAYLOAD1)
batch.publish(PAYLOAD2, attr1='value1', attr2='value2')
self.assertEqual(list(batch), [MSGID1, MSGID2])
self.assertEqual(list(batch.messages), [])
self.assertEqual(api._topic_published,
(self.TOPIC_PATH, [MESSAGE1, MESSAGE2]))
def test_publish_multiple_error(self):
PAYLOAD1 = b'This is the first message text'
PAYLOAD2 = b'This is the second message text'
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
topic = self._makeOne(self.TOPIC_NAME, client=client)
try:
with topic.batch() as batch:
batch.publish(PAYLOAD1)
batch.publish(PAYLOAD2, attr1='value1', attr2='value2')
raise _Bugout()
except _Bugout:
pass
self.assertEqual(list(batch), [])
self.assertEqual(getattr(api, '_topic_published', self), self)
def test_subscription(self):
from google.cloud.pubsub.subscription import Subscription
client = _Client(project=self.PROJECT)
topic = self._makeOne(self.TOPIC_NAME, client=client)
SUBSCRIPTION_NAME = 'subscription_name'
subscription = topic.subscription(SUBSCRIPTION_NAME)
self.assertIsInstance(subscription, Subscription)
self.assertEqual(subscription.name, SUBSCRIPTION_NAME)
self.assertIs(subscription.topic, topic)
def test_list_subscriptions_no_paging(self):
from google.cloud.pubsub.subscription import Subscription
SUB_NAME_1 = 'subscription_1'
SUB_PATH_1 = 'projects/%s/subscriptions/%s' % (
self.PROJECT, SUB_NAME_1)
SUB_NAME_2 = 'subscription_2'
SUB_PATH_2 = 'projects/%s/subscriptions/%s' % (
self.PROJECT, SUB_NAME_2)
SUBS_LIST = [SUB_PATH_1, SUB_PATH_2]
TOKEN = 'TOKEN'
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_list_subscriptions_response = SUBS_LIST, TOKEN
topic = self._makeOne(self.TOPIC_NAME, client=client)
subscriptions, next_page_token = topic.list_subscriptions()
self.assertEqual(len(subscriptions), 2)
subscription = subscriptions[0]
self.assertIsInstance(subscription, Subscription)
self.assertEqual(subscriptions[0].name, SUB_NAME_1)
self.assertIs(subscription.topic, topic)
subscription = subscriptions[1]
self.assertIsInstance(subscription, Subscription)
self.assertEqual(subscriptions[1].name, SUB_NAME_2)
self.assertIs(subscription.topic, topic)
self.assertEqual(next_page_token, TOKEN)
self.assertEqual(api._topic_listed,
(self.TOPIC_PATH, None, None))
def test_list_subscriptions_with_paging(self):
from google.cloud.pubsub.subscription import Subscription
SUB_NAME_1 = 'subscription_1'
SUB_PATH_1 = 'projects/%s/subscriptions/%s' % (
self.PROJECT, SUB_NAME_1)
SUB_NAME_2 = 'subscription_2'
SUB_PATH_2 = 'projects/%s/subscriptions/%s' % (
self.PROJECT, SUB_NAME_2)
SUBS_LIST = [SUB_PATH_1, SUB_PATH_2]
PAGE_SIZE = 10
TOKEN = 'TOKEN'
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_list_subscriptions_response = SUBS_LIST, None
topic = self._makeOne(self.TOPIC_NAME, client=client)
subscriptions, next_page_token = topic.list_subscriptions(
page_size=PAGE_SIZE, page_token=TOKEN)
self.assertEqual(len(subscriptions), 2)
subscription = subscriptions[0]
self.assertIsInstance(subscription, Subscription)
self.assertEqual(subscriptions[0].name, SUB_NAME_1)
self.assertIs(subscription.topic, topic)
subscription = subscriptions[1]
self.assertIsInstance(subscription, Subscription)
self.assertEqual(subscriptions[1].name, SUB_NAME_2)
self.assertIs(subscription.topic, topic)
self.assertIsNone(next_page_token)
self.assertEqual(api._topic_listed,
(self.TOPIC_PATH, PAGE_SIZE, TOKEN))
def test_list_subscriptions_missing_key(self):
client = _Client(project=self.PROJECT)
api = client.publisher_api = _FauxPublisherAPI()
api._topic_list_subscriptions_response = (), None
topic = self._makeOne(self.TOPIC_NAME, client=client)
subscriptions, next_page_token = topic.list_subscriptions()
self.assertEqual(len(subscriptions), 0)
self.assertIsNone(next_page_token)
self.assertEqual(api._topic_listed,
(self.TOPIC_PATH, None, None))
def test_get_iam_policy_w_bound_client(self):
from google.cloud.pubsub.iam import (
PUBSUB_ADMIN_ROLE,
PUBSUB_EDITOR_ROLE,
PUBSUB_VIEWER_ROLE,
PUBSUB_PUBLISHER_ROLE,
PUBSUB_SUBSCRIBER_ROLE,
)
OWNER1 = 'user:phred@example.com'
OWNER2 = 'group:cloud-logs@google.com'
EDITOR1 = 'domain:google.com'
EDITOR2 = 'user:phred@example.com'
VIEWER1 = 'serviceAccount:1234-abcdef@service.example.com'
VIEWER2 = 'user:phred@example.com'
PUBLISHER = 'user:phred@example.com'
SUBSCRIBER = 'serviceAccount:1234-abcdef@service.example.com'
POLICY = {
'etag': 'DEADBEEF',
'version': 17,
'bindings': [
{'role': PUBSUB_ADMIN_ROLE, 'members': [OWNER1, OWNER2]},
{'role': PUBSUB_EDITOR_ROLE, 'members': [EDITOR1, EDITOR2]},
{'role': PUBSUB_VIEWER_ROLE, 'members': [VIEWER1, VIEWER2]},
{'role': PUBSUB_PUBLISHER_ROLE, 'members': [PUBLISHER]},
{'role': PUBSUB_SUBSCRIBER_ROLE, 'members': [SUBSCRIBER]},
],
}
client = _Client(project=self.PROJECT)
api = client.iam_policy_api = _FauxIAMPolicy()
api._get_iam_policy_response = POLICY
topic = self._makeOne(self.TOPIC_NAME, client=client)
policy = topic.get_iam_policy()
self.assertEqual(policy.etag, 'DEADBEEF')
self.assertEqual(policy.version, 17)
self.assertEqual(sorted(policy.owners), [OWNER2, OWNER1])
self.assertEqual(sorted(policy.editors), [EDITOR1, EDITOR2])
self.assertEqual(sorted(policy.viewers), [VIEWER1, VIEWER2])
self.assertEqual(sorted(policy.publishers), [PUBLISHER])
self.assertEqual(sorted(policy.subscribers), [SUBSCRIBER])
self.assertEqual(api._got_iam_policy, self.TOPIC_PATH)
def test_get_iam_policy_w_alternate_client(self):
POLICY = {
'etag': 'ACAB',
}
client1 = _Client(project=self.PROJECT)
client2 = _Client(project=self.PROJECT)
api = client2.iam_policy_api = _FauxIAMPolicy()
api._get_iam_policy_response = POLICY
topic = self._makeOne(self.TOPIC_NAME, client=client1)
policy = topic.get_iam_policy(client=client2)
self.assertEqual(policy.etag, 'ACAB')
self.assertIsNone(policy.version)
self.assertEqual(sorted(policy.owners), [])
self.assertEqual(sorted(policy.editors), [])
self.assertEqual(sorted(policy.viewers), [])
self.assertEqual(api._got_iam_policy, self.TOPIC_PATH)
def test_set_iam_policy_w_bound_client(self):
from google.cloud.pubsub.iam import Policy
from google.cloud.pubsub.iam import (
PUBSUB_ADMIN_ROLE,
PUBSUB_EDITOR_ROLE,
PUBSUB_VIEWER_ROLE,
PUBSUB_PUBLISHER_ROLE,
PUBSUB_SUBSCRIBER_ROLE,
)
OWNER1 = 'group:cloud-logs@google.com'
OWNER2 = 'user:phred@example.com'
EDITOR1 = 'domain:google.com'
EDITOR2 = 'user:phred@example.com'
VIEWER1 = 'serviceAccount:1234-abcdef@service.example.com'
VIEWER2 = 'user:phred@example.com'
PUBLISHER = 'user:phred@example.com'
SUBSCRIBER = 'serviceAccount:1234-abcdef@service.example.com'
POLICY = {
'etag': 'DEADBEEF',
'version': 17,
'bindings': [
{'role': PUBSUB_ADMIN_ROLE,
'members': [OWNER1, OWNER2]},
{'role': PUBSUB_EDITOR_ROLE,
'members': [EDITOR1, EDITOR2]},
{'role': PUBSUB_VIEWER_ROLE,
'members': [VIEWER1, VIEWER2]},
{'role': PUBSUB_PUBLISHER_ROLE,
'members': [PUBLISHER]},
{'role': PUBSUB_SUBSCRIBER_ROLE,
'members': [SUBSCRIBER]},
],
}
RESPONSE = POLICY.copy()
RESPONSE['etag'] = 'ABACABAF'
RESPONSE['version'] = 18
client = _Client(project=self.PROJECT)
api = client.iam_policy_api = _FauxIAMPolicy()
api._set_iam_policy_response = RESPONSE
topic = self._makeOne(self.TOPIC_NAME, client=client)
policy = Policy('DEADBEEF', 17)
policy.owners.add(OWNER1)
policy.owners.add(OWNER2)
policy.editors.add(EDITOR1)
policy.editors.add(EDITOR2)
policy.viewers.add(VIEWER1)
policy.viewers.add(VIEWER2)
policy.publishers.add(PUBLISHER)
policy.subscribers.add(SUBSCRIBER)
new_policy = topic.set_iam_policy(policy)
self.assertEqual(new_policy.etag, 'ABACABAF')
self.assertEqual(new_policy.version, 18)
self.assertEqual(sorted(new_policy.owners), [OWNER1, OWNER2])
self.assertEqual(sorted(new_policy.editors), [EDITOR1, EDITOR2])
self.assertEqual(sorted(new_policy.viewers), [VIEWER1, VIEWER2])
self.assertEqual(sorted(new_policy.publishers), [PUBLISHER])
self.assertEqual(sorted(new_policy.subscribers), [SUBSCRIBER])
self.assertEqual(api._set_iam_policy, (self.TOPIC_PATH, POLICY))
def test_set_iam_policy_w_alternate_client(self):
from google.cloud.pubsub.iam import Policy
RESPONSE = {'etag': 'ACAB'}
client1 = _Client(project=self.PROJECT)
client2 = _Client(project=self.PROJECT)
api = client2.iam_policy_api = _FauxIAMPolicy()
api._set_iam_policy_response = RESPONSE
topic = self._makeOne(self.TOPIC_NAME, client=client1)
policy = Policy()
new_policy = topic.set_iam_policy(policy, client=client2)
self.assertEqual(new_policy.etag, 'ACAB')
self.assertIsNone(new_policy.version)
self.assertEqual(sorted(new_policy.owners), [])
self.assertEqual(sorted(new_policy.editors), [])
self.assertEqual(sorted(new_policy.viewers), [])
self.assertEqual(api._set_iam_policy, (self.TOPIC_PATH, {}))
def test_check_iam_permissions_w_bound_client(self):
from google.cloud.pubsub.iam import OWNER_ROLE
from google.cloud.pubsub.iam import EDITOR_ROLE
from google.cloud.pubsub.iam import VIEWER_ROLE
ROLES = [VIEWER_ROLE, EDITOR_ROLE, OWNER_ROLE]
client = _Client(project=self.PROJECT)
api = client.iam_policy_api = _FauxIAMPolicy()
api._test_iam_permissions_response = ROLES[:-1]
topic = self._makeOne(self.TOPIC_NAME, client=client)
allowed = topic.check_iam_permissions(ROLES)
self.assertEqual(allowed, ROLES[:-1])
self.assertEqual(api._tested_iam_permissions,
(self.TOPIC_PATH, ROLES))
def test_check_iam_permissions_w_alternate_client(self):
from google.cloud.pubsub.iam import OWNER_ROLE
from google.cloud.pubsub.iam import EDITOR_ROLE
from google.cloud.pubsub.iam import VIEWER_ROLE
ROLES = [VIEWER_ROLE, EDITOR_ROLE, OWNER_ROLE]
client1 = _Client(project=self.PROJECT)
client2 = _Client(project=self.PROJECT)
api = client2.iam_policy_api = _FauxIAMPolicy()
api._test_iam_permissions_response = []
topic = self._makeOne(self.TOPIC_NAME, client=client1)
allowed = topic.check_iam_permissions(ROLES, client=client2)
self.assertEqual(len(allowed), 0)
self.assertEqual(api._tested_iam_permissions,
(self.TOPIC_PATH, ROLES))
class TestBatch(unittest.TestCase):
PROJECT = 'PROJECT'
def _getTargetClass(self):
from google.cloud.pubsub.topic import Batch
return Batch
def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)
def test_ctor_defaults(self):
topic = _Topic()
client = _Client(project=self.PROJECT)
batch = self._makeOne(topic, client)
self.assertIs(batch.topic, topic)
self.assertIs(batch.client, client)
self.assertEqual(len(batch.messages), 0)
self.assertEqual(len(batch.message_ids), 0)
def test___iter___empty(self):
topic = _Topic()
client = object()
batch = self._makeOne(topic, client)
self.assertEqual(list(batch), [])
def test___iter___non_empty(self):
topic = _Topic()
client = object()
batch = self._makeOne(topic, client)
batch.message_ids[:] = ['ONE', 'TWO', 'THREE']
self.assertEqual(list(batch), ['ONE', 'TWO', 'THREE'])
def test_publish_bytes_wo_attrs(self):
import base64
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MESSAGE = {'data': B64,
'attributes': {}}
client = _Client(project=self.PROJECT)
topic = _Topic()
batch = self._makeOne(topic, client=client)
batch.publish(PAYLOAD)
self.assertEqual(batch.messages, [MESSAGE])
def test_publish_bytes_w_add_timestamp(self):
import base64
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MESSAGE = {'data': B64,
'attributes': {'timestamp': 'TIMESTAMP'}}
client = _Client(project=self.PROJECT)
topic = _Topic(timestamp_messages=True)
batch = self._makeOne(topic, client=client)
batch.publish(PAYLOAD)
self.assertEqual(batch.messages, [MESSAGE])
def test_commit_w_bound_client(self):
import base64
PAYLOAD1 = b'This is the first message text'
PAYLOAD2 = b'This is the second message text'
B64_1 = base64.b64encode(PAYLOAD1)
B64_2 = base64.b64encode(PAYLOAD2)
MSGID1 = 'DEADBEEF'
MSGID2 = 'BEADCAFE'
MESSAGE1 = {'data': B64_1.decode('ascii'),
'attributes': {}}
MESSAGE2 = {'data': B64_2.decode('ascii'),
'attributes': {'attr1': 'value1', 'attr2': 'value2'}}
client = _Client(project='PROJECT')
api = client.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID1, MSGID2]
topic = _Topic()
batch = self._makeOne(topic, client=client)
batch.publish(PAYLOAD1)
batch.publish(PAYLOAD2, attr1='value1', attr2='value2')
batch.commit()
self.assertEqual(list(batch), [MSGID1, MSGID2])
self.assertEqual(list(batch.messages), [])
self.assertEqual(api._topic_published,
(topic.full_name, [MESSAGE1, MESSAGE2]))
def test_commit_w_alternate_client(self):
import base64
PAYLOAD1 = b'This is the first message text'
PAYLOAD2 = b'This is the second message text'
B64_1 = base64.b64encode(PAYLOAD1)
B64_2 = base64.b64encode(PAYLOAD2)
MSGID1 = 'DEADBEEF'
MSGID2 = 'BEADCAFE'
MESSAGE1 = {'data': B64_1.decode('ascii'),
'attributes': {}}
MESSAGE2 = {'data': B64_2.decode('ascii'),
'attributes': {'attr1': 'value1', 'attr2': 'value2'}}
client1 = _Client(project='PROJECT')
client2 = _Client(project='PROJECT')
api = client2.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID1, MSGID2]
topic = _Topic()
batch = self._makeOne(topic, client=client1)
batch.publish(PAYLOAD1)
batch.publish(PAYLOAD2, attr1='value1', attr2='value2')
batch.commit(client=client2)
self.assertEqual(list(batch), [MSGID1, MSGID2])
self.assertEqual(list(batch.messages), [])
self.assertEqual(api._topic_published,
(topic.full_name, [MESSAGE1, MESSAGE2]))
def test_context_mgr_success(self):
import base64
PAYLOAD1 = b'This is the first message text'
PAYLOAD2 = b'This is the second message text'
B64_1 = base64.b64encode(PAYLOAD1)
B64_2 = base64.b64encode(PAYLOAD2)
MSGID1 = 'DEADBEEF'
MSGID2 = 'BEADCAFE'
MESSAGE1 = {'data': B64_1.decode('ascii'),
'attributes': {}}
MESSAGE2 = {'data': B64_2.decode('ascii'),
'attributes': {'attr1': 'value1', 'attr2': 'value2'}}
client = _Client(project='PROJECT')
api = client.publisher_api = _FauxPublisherAPI()
api._topic_publish_response = [MSGID1, MSGID2]
topic = _Topic()
batch = self._makeOne(topic, client=client)
with batch as other:
batch.publish(PAYLOAD1)
batch.publish(PAYLOAD2, attr1='value1', attr2='value2')
self.assertIs(other, batch)
self.assertEqual(list(batch), [MSGID1, MSGID2])
self.assertEqual(list(batch.messages), [])
self.assertEqual(api._topic_published,
(topic.full_name, [MESSAGE1, MESSAGE2]))
def test_context_mgr_failure(self):
import base64
PAYLOAD1 = b'This is the first message text'
PAYLOAD2 = b'This is the second message text'
B64_1 = base64.b64encode(PAYLOAD1)
B64_2 = base64.b64encode(PAYLOAD2)
MESSAGE1 = {'data': B64_1.decode('ascii'),
'attributes': {}}
MESSAGE2 = {'data': B64_2.decode('ascii'),
'attributes': {'attr1': 'value1', 'attr2': 'value2'}}
client = _Client(project='PROJECT')
api = client.publisher_api = _FauxPublisherAPI()
topic = _Topic()
batch = self._makeOne(topic, client=client)
try:
with batch as other:
batch.publish(PAYLOAD1)
batch.publish(PAYLOAD2, attr1='value1', attr2='value2')
raise _Bugout()
except _Bugout:
pass
self.assertIs(other, batch)
self.assertEqual(list(batch), [])
self.assertEqual(list(batch.messages), [MESSAGE1, MESSAGE2])
self.assertEqual(getattr(api, '_topic_published', self), self)
class _FauxPublisherAPI(object):
_api_called = 0
def topic_create(self, topic_path):
self._topic_created = topic_path
return self._topic_create_response
def topic_get(self, topic_path):
from google.cloud.exceptions import NotFound
self._topic_got = topic_path
try:
return self._topic_get_response
except AttributeError:
raise NotFound(topic_path)
def topic_delete(self, topic_path):
self._topic_deleted = topic_path
return self._topic_delete_response
def topic_publish(self, topic_path, messages):
self._topic_published = topic_path, messages
self._api_called += 1
return self._topic_publish_response
def topic_list_subscriptions(self, topic_path, page_size=None,
page_token=None):
self._topic_listed = topic_path, page_size, page_token
return self._topic_list_subscriptions_response
class _FauxIAMPolicy(object):
def get_iam_policy(self, target_path):
self._got_iam_policy = target_path
return self._get_iam_policy_response
def set_iam_policy(self, target_path, policy):
self._set_iam_policy = target_path, policy
return self._set_iam_policy_response
def test_iam_permissions(self, target_path, permissions):
self._tested_iam_permissions = target_path, permissions
return self._test_iam_permissions_response
class _Topic(object):
def __init__(self, name="NAME", project="PROJECT",
timestamp_messages=False):
self.full_name = 'projects/%s/topics/%s' % (project, name)
self.path = '/%s' % (self.full_name,)
self.timestamp_messages = timestamp_messages
def _timestamp_message(self, attrs):
if self.timestamp_messages:
attrs['timestamp'] = 'TIMESTAMP'
class _Client(object):
connection = None
def __init__(self, project):
self.project = project
class _Bugout(Exception):
pass