1
1
import pytest
2
+ import unittest
2
3
3
4
from pubnub .pubnub import PubNub
4
5
from pubnub .exceptions import PubNubException
5
6
from pubnub .endpoints .signal import Signal
6
- from tests .helper import url_encode , pnconf_copy
7
+ from pubnub .models .consumer .message_type import PNMessageType
8
+ from pubnub .managers import TelemetryManager
9
+ from tests .helper import url_encode , pnconf_copy , sdk_name
10
+ from unittest .mock import MagicMock
11
+
7
12
8
13
pnconf = pnconf_copy ()
9
14
SUB_KEY = pnconf .subscribe_key
@@ -28,3 +33,73 @@ def test_signal():
28
33
assert signal .build_path () == Signal .SIGNAL_PATH % (PUB_KEY , SUB_KEY , CHAN , MSG_ENCODED )
29
34
assert 'auth' in signal .build_params_callback ()({})
30
35
assert AUTH == signal .build_params_callback ()({})['auth' ]
36
+
37
+
38
+ class TestPublish (unittest .TestCase ):
39
+ def setUp (self ):
40
+ self .sm = MagicMock (
41
+ get_next_sequence = MagicMock (return_value = 2 )
42
+ )
43
+
44
+ self .pubnub = MagicMock (
45
+ spec = PubNub ,
46
+ config = pnconf ,
47
+ sdk_name = sdk_name ,
48
+ _publish_sequence_manager = self .sm ,
49
+ _get_token = lambda : None
50
+ )
51
+
52
+ self .pubnub .uuid = "UUID_PublishUnitTest"
53
+ self .pubnub ._telemetry_manager = TelemetryManager ()
54
+ self .signal = Signal (self .pubnub )
55
+
56
+ def test_signal_with_space_id (self ):
57
+ message = "hi"
58
+ space_id = "test_space"
59
+ encoded_message = url_encode (message )
60
+
61
+ self .signal .channel ("ch1" ).message (message ).space_id (space_id )
62
+
63
+ self .assertEqual (self .signal .build_path (), "/signal/%s/%s/0/ch1/0/%s"
64
+ % (pnconf .publish_key , pnconf .subscribe_key , encoded_message ))
65
+
66
+ self .assertEqual (self .signal .build_params_callback ()({}), {
67
+ 'auth' : 'auth' ,
68
+ 'pnsdk' : sdk_name ,
69
+ 'space-id' : space_id ,
70
+ 'uuid' : self .pubnub .uuid ,
71
+ })
72
+
73
+ def test_signal_with_pn_message_type (self ):
74
+ message = "hi"
75
+ message_type = 'test_type'
76
+ encoded_message = url_encode (message )
77
+
78
+ self .signal .channel ("ch1" ).message (message ).message_type (PNMessageType (message_type ))
79
+
80
+ self .assertEqual (self .signal .build_path (), "/signal/%s/%s/0/ch1/0/%s"
81
+ % (pnconf .publish_key , pnconf .subscribe_key , encoded_message ))
82
+
83
+ self .assertEqual (self .signal .build_params_callback ()({}), {
84
+ 'auth' : 'auth' ,
85
+ 'pnsdk' : sdk_name ,
86
+ 'type' : message_type ,
87
+ 'uuid' : self .pubnub .uuid ,
88
+ })
89
+
90
+ def test_signal_with_str_message_type (self ):
91
+ message = "hi"
92
+ message_type = 'test_type'
93
+ encoded_message = url_encode (message )
94
+
95
+ self .signal .channel ("ch1" ).message (message ).message_type (message_type )
96
+
97
+ self .assertEqual (self .signal .build_path (), "/signal/%s/%s/0/ch1/0/%s"
98
+ % (pnconf .publish_key , pnconf .subscribe_key , encoded_message ))
99
+
100
+ self .assertEqual (self .signal .build_params_callback ()({}), {
101
+ 'auth' : 'auth' ,
102
+ 'pnsdk' : sdk_name ,
103
+ 'type' : message_type ,
104
+ 'uuid' : self .pubnub .uuid ,
105
+ })
0 commit comments