-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtest_history.py
106 lines (82 loc) · 4.21 KB
/
test_history.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
import logging
import time
import unittest
import pubnub
import pytest
from unittest.mock import patch
from pubnub.exceptions import PubNubException
from pubnub.models.consumer.history import PNHistoryResult
from pubnub.models.consumer.pubsub import PNPublishResult
from pubnub.pubnub import PubNub
from tests.helper import pnconf_copy, pnconf_enc_copy, pnconf_pam_env_copy
from tests.integrational.vcr_helper import use_cassette_and_stub_time_sleep_native
pubnub.set_stream_logger('pubnub', logging.DEBUG)
COUNT = 5
class TestPubNubHistory(unittest.TestCase):
@use_cassette_and_stub_time_sleep_native('tests/integrational/fixtures/native_sync/history/basic.yaml',
filter_query_parameters=['uuid', 'pnsdk', 'l_pub'])
def test_basic(self):
ch = "history-native-sync-ch"
pubnub = PubNub(pnconf_copy())
pubnub.config.uuid = "history-native-sync-uuid"
for i in range(COUNT):
envelope = pubnub.publish().channel(ch).message("hey-%s" % i).sync()
assert isinstance(envelope.result, PNPublishResult)
assert envelope.result.timetoken > 0
time.sleep(5)
envelope = pubnub.history().channel(ch).count(COUNT).sync()
assert isinstance(envelope.result, PNHistoryResult)
assert envelope.result.start_timetoken > 0
assert envelope.result.end_timetoken > 0
assert len(envelope.result.messages) == 5
assert envelope.result.messages[0].entry == 'hey-0'
assert envelope.result.messages[1].entry == 'hey-1'
assert envelope.result.messages[2].entry == 'hey-2'
assert envelope.result.messages[3].entry == 'hey-3'
assert envelope.result.messages[4].entry == 'hey-4'
@patch("pubnub.crypto.PubNubCryptodome.get_initialization_vector", return_value="knightsofni12345")
@use_cassette_and_stub_time_sleep_native(
'tests/integrational/fixtures/native_sync/history/encoded.yaml',
filter_query_parameters=['uuid', 'pnsdk', 'l_pub']
)
def test_encrypted(self, crypto_mock):
ch = "history-native-sync-ch"
pubnub = PubNub(pnconf_enc_copy())
pubnub.config.uuid = "history-native-sync-uuid"
for i in range(COUNT):
envelope = pubnub.publish().channel(ch).message("hey-%s" % i).sync()
assert isinstance(envelope.result, PNPublishResult)
assert envelope.result.timetoken > 0
time.sleep(5)
envelope = pubnub.history().channel(ch).count(COUNT).sync()
assert isinstance(envelope.result, PNHistoryResult)
assert envelope.result.start_timetoken > 0
assert envelope.result.end_timetoken > 0
assert len(envelope.result.messages) == 5
assert envelope.result.messages[0].entry == 'hey-0'
assert envelope.result.messages[1].entry == 'hey-1'
assert envelope.result.messages[2].entry == 'hey-2'
assert envelope.result.messages[3].entry == 'hey-3'
assert envelope.result.messages[4].entry == 'hey-4'
@use_cassette_and_stub_time_sleep_native('tests/integrational/fixtures/native_sync/history/not_permitted.yaml',
filter_query_parameters=['uuid', 'pnsdk'])
def test_not_permitted(self):
ch = "history-native-sync-ch"
pubnub = PubNub(pnconf_pam_env_copy())
pubnub.config.uuid = "history-native-sync-uuid"
with pytest.raises(PubNubException):
pubnub.history().channel(ch).count(5).sync()
def test_super_call_with_channel_only(self):
ch = "history-native-sync-ch"
pubnub = PubNub(pnconf_pam_env_copy())
pubnub.config.uuid = "history-native-sync-uuid"
envelope = pubnub.history().channel(ch).sync()
assert isinstance(envelope.result, PNHistoryResult)
assert not envelope.status.is_error()
def test_super_call_with_all_params(self):
ch = "history-native-sync-ch"
pubnub = PubNub(pnconf_pam_env_copy())
pubnub.config.uuid = "history-native-sync-uuid"
envelope = pubnub.history().channel(ch).count(2).include_timetoken(True).reverse(True).start(1).end(2).sync()
assert isinstance(envelope.result, PNHistoryResult)
assert not envelope.status.is_error()