Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Feb 9, 2023
1 parent 0a49f60 commit 1109fad
Show file tree
Hide file tree
Showing 23 changed files with 26,581 additions and 16,992 deletions.
17 changes: 13 additions & 4 deletions pubnub/endpoints/fetch_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def __init__(self, pubnub):
self._count = None
self._include_meta = None
self._include_message_actions = None
self._include_message_type = None
self._include_message_type = True
self._include_space_id = None
self._include_uuid = None

def channels(self, channels):
Expand Down Expand Up @@ -76,8 +77,16 @@ def include_uuid(self, include_uuid):
self._include_uuid = include_uuid
return self

def include_space_id(self, include_space_id):
assert isinstance(include_space_id, bool)
self._include_space_id = include_space_id
return self

def custom_params(self):
params = {'max': int(self._count)}
params = {
'max': int(self._count),
'include_message_type': "true" if self._include_message_type else "false",
}

if self._start is not None:
params['start'] = str(self._start)
Expand All @@ -88,8 +97,8 @@ def custom_params(self):
if self._include_meta is not None:
params['include_meta'] = "true" if self._include_meta else "false"

if self._include_message_type is not None:
params['include_message_type'] = "true" if self._include_message_type else "false"
if self._include_space_id is not None:
params['include_space_id'] = "true" if self._include_space_id else "false"

if self.include_message_actions and self._include_uuid is not None:
params['include_uuid'] = "true" if self._include_uuid else "false"
Expand Down
4 changes: 3 additions & 1 deletion pubnub/endpoints/pubsub/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pubnub.models.consumer.pubsub import PNPublishResult
from pubnub.enums import HttpMethod, PNOperationType
from pubnub.endpoints.mixins import TimeTokenOverrideMixin
from pubnub.models.consumer.message_type import PNMessageType
from typing import Union


class Publish(Endpoint, TimeTokenOverrideMixin):
Expand Down Expand Up @@ -37,7 +39,7 @@ def message(self, message):
self._message = message
return self

def message_type(self, message_type):
def message_type(self, message_type: Union[PNMessageType, str]):
self._message_type = message_type
return self

Expand Down
6 changes: 4 additions & 2 deletions pubnub/endpoints/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pubnub.endpoints.endpoint import Endpoint
from pubnub.enums import HttpMethod, PNOperationType
from pubnub.models.consumer.signal import PNSignalResult
from pubnub.models.consumer.message_type import PNMessageType
from typing import Union


class Signal(Endpoint):
Expand All @@ -22,11 +24,11 @@ def message(self, message):
self._message = message
return self

def space(self, space):
def space_id(self, space):
self._space = str(space)
return self

def message_type(self, message_type):
def message_type(self, message_type: Union[PNMessageType, str]):
self._message_type = message_type
return self

Expand Down
54 changes: 51 additions & 3 deletions tests/functional/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import unittest


from unittest.mock import MagicMock

from pubnub.endpoints.pubsub.publish import Publish
from pubnub.managers import TelemetryManager
from pubnub.models.consumer.message_type import PNMessageType
from pubnub.pubnub import PubNub
from tests.helper import pnconf, sdk_name, url_encode
from pubnub.managers import TelemetryManager
from unittest.mock import MagicMock


class TestPublish(unittest.TestCase):
Expand Down Expand Up @@ -93,6 +93,54 @@ def test_pub_store(self):
'store': '1',
})

def test_pub_with_space_id(self):
message = "hi"
space_id = "test_space"
encoded_message = url_encode(message)

self.pub.channel("ch1").message(message).space_id(space_id)

self.assertEqual(self.pub.build_path(), "/publish/%s/%s/0/ch1/0/%s"
% (pnconf.publish_key, pnconf.subscribe_key, encoded_message))

self.assertEqual(self.pub.build_params_callback()({}), {
'space-id': space_id,
'pnsdk': sdk_name,
'uuid': self.pubnub.uuid,
})

def test_pub_with_pn_message_type(self):
message = "hi"
message_type = 'test_type'
encoded_message = url_encode(message)

self.pub.channel("ch1").message(message).message_type(PNMessageType(message_type))

self.assertEqual(self.pub.build_path(), "/publish/%s/%s/0/ch1/0/%s"
% (pnconf.publish_key, pnconf.subscribe_key, encoded_message))

self.assertEqual(self.pub.build_params_callback()({}), {
'type': message_type,
'pnsdk': sdk_name,
'uuid': self.pubnub.uuid,
})

def test_pub_with_str_message_type(self):
message = "hi"
message_type = 'test_type'
encoded_message = url_encode(message)

self.pub.channel("ch1").message(message).message_type(message_type)

self.assertEqual(self.pub.build_path(), "/publish/%s/%s/0/ch1/0/%s"
% (pnconf.publish_key, pnconf.subscribe_key, encoded_message))

self.assertEqual(self.pub.build_params_callback()({}), {
'type': message_type,
'pnsdk': sdk_name,
'uuid': self.pubnub.uuid,
})

def test_pub_do_not_store(self):
self.pubnub.uuid = "UUID_PublishUnitTest"

Expand Down
77 changes: 76 additions & 1 deletion tests/functional/test_signal.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import pytest
import unittest

from pubnub.pubnub import PubNub
from pubnub.exceptions import PubNubException
from pubnub.endpoints.signal import Signal
from tests.helper import url_encode, pnconf_copy
from pubnub.models.consumer.message_type import PNMessageType
from pubnub.managers import TelemetryManager
from tests.helper import url_encode, pnconf_copy, sdk_name
from unittest.mock import MagicMock


pnconf = pnconf_copy()
SUB_KEY = pnconf.subscribe_key
Expand All @@ -28,3 +33,73 @@ def test_signal():
assert signal.build_path() == Signal.SIGNAL_PATH % (PUB_KEY, SUB_KEY, CHAN, MSG_ENCODED)
assert 'auth' in signal.build_params_callback()({})
assert AUTH == signal.build_params_callback()({})['auth']


class TestPublish(unittest.TestCase):
def setUp(self):
self.sm = MagicMock(
get_next_sequence=MagicMock(return_value=2)
)

self.pubnub = MagicMock(
spec=PubNub,
config=pnconf,
sdk_name=sdk_name,
_publish_sequence_manager=self.sm,
_get_token=lambda: None
)

self.pubnub.uuid = "UUID_PublishUnitTest"
self.pubnub._telemetry_manager = TelemetryManager()
self.signal = Signal(self.pubnub)

def test_signal_with_space_id(self):
message = "hi"
space_id = "test_space"
encoded_message = url_encode(message)

self.signal.channel("ch1").message(message).space_id(space_id)

self.assertEqual(self.signal.build_path(), "/signal/%s/%s/0/ch1/0/%s"
% (pnconf.publish_key, pnconf.subscribe_key, encoded_message))

self.assertEqual(self.signal.build_params_callback()({}), {
'auth': 'auth',
'pnsdk': sdk_name,
'space-id': space_id,
'uuid': self.pubnub.uuid,
})

def test_signal_with_pn_message_type(self):
message = "hi"
message_type = 'test_type'
encoded_message = url_encode(message)

self.signal.channel("ch1").message(message).message_type(PNMessageType(message_type))

self.assertEqual(self.signal.build_path(), "/signal/%s/%s/0/ch1/0/%s"
% (pnconf.publish_key, pnconf.subscribe_key, encoded_message))

self.assertEqual(self.signal.build_params_callback()({}), {
'auth': 'auth',
'pnsdk': sdk_name,
'type': message_type,
'uuid': self.pubnub.uuid,
})

def test_signal_with_str_message_type(self):
message = "hi"
message_type = 'test_type'
encoded_message = url_encode(message)

self.signal.channel("ch1").message(message).message_type(message_type)

self.assertEqual(self.signal.build_path(), "/signal/%s/%s/0/ch1/0/%s"
% (pnconf.publish_key, pnconf.subscribe_key, encoded_message))

self.assertEqual(self.signal.build_params_callback()({}), {
'auth': 'auth',
'pnsdk': sdk_name,
'type': message_type,
'uuid': self.pubnub.uuid,
})
4 changes: 2 additions & 2 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
pnconf_env.publish_key = os.environ.get('PN_KEY_PUBLISH')
pnconf_env.subscribe_request_timeout = 10
pnconf_env.subscribe_key = os.environ.get('PN_KEY_SUBSCRIBE')
pnconf_env.enable_subscribe = False
pnconf_env.enable_subscribe = True
pnconf_env.uuid = uuid_mock

# configuration with keys from PN_KEY_* (enabled all except PAM, PUSH and FUNCTIONS) and encryption enabled
Expand All @@ -142,7 +142,7 @@
pnconf_enc_env.subscribe_request_timeout = 10
pnconf_enc_env.subscribe_key = os.environ.get('PN_KEY_SUBSCRIBE')
pnconf_enc_env.cipher_key = "testKey"
pnconf_enc_env.enable_subscribe = False
pnconf_enc_env.enable_subscribe = True
pnconf_enc_env.uuid = uuid_mock

# configuration with keys from PN_KEY_PAM_* (enabled with all including PAM except PUSH and FUNCTIONS)
Expand Down
17 changes: 9 additions & 8 deletions tests/integrational/asyncio/test_subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from tests.integrational.vcr_asyncio_sleeper import get_sleeper, VCR599Listener, VCR599ReconnectionManager
from tests.integrational.vcr_helper import pn_vcr


pn.set_stream_logger('pubnub', logging.DEBUG)


Expand Down Expand Up @@ -396,8 +397,8 @@ async def test_subscribe_publish_space_id(event_loop, sleeper=asyncio.sleep):
pubnub_sub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)
pubnub_pub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)

patch_pubnub(pubnub_sub)
patch_pubnub(pubnub_pub)
await patch_pubnub(pubnub_sub)
await patch_pubnub(pubnub_pub)

pubnub_sub.config.uuid = 'test-subscribe-asyncio-uuid-sub'
pubnub_pub.config.uuid = 'test-subscribe-asyncio-uuid-pub'
Expand Down Expand Up @@ -438,8 +439,8 @@ async def test_subscribe_publish_space_id(event_loop, sleeper=asyncio.sleep):
pubnub_sub.unsubscribe().channels(channel).execute()
# await callback.wait_for_disconnect()

pubnub_pub.stop()
pubnub_sub.stop()
await pubnub_pub.stop()
await pubnub_sub.stop()


@get_sleeper('tests/integrational/fixtures/asyncio/subscription/publish_message_type.yaml')
Expand All @@ -453,8 +454,8 @@ async def test_subscribe_publish_message_type(event_loop, sleeper=asyncio.sleep)
pubnub_sub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)
pubnub_pub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)

patch_pubnub(pubnub_sub)
patch_pubnub(pubnub_pub)
await patch_pubnub(pubnub_sub)
await patch_pubnub(pubnub_pub)

pubnub_sub.config.uuid = 'test-subscribe-asyncio-uuid-sub'
pubnub_pub.config.uuid = 'test-subscribe-asyncio-uuid-pub'
Expand Down Expand Up @@ -497,5 +498,5 @@ async def test_subscribe_publish_message_type(event_loop, sleeper=asyncio.sleep)
pubnub_sub.unsubscribe().channels(channel).execute()
# await callback.wait_for_disconnect()

pubnub_pub.stop()
pubnub_sub.stop()
await pubnub_pub.stop()
await pubnub_sub.stop()

This file was deleted.

Loading

0 comments on commit 1109fad

Please sign in to comment.