|
| 1 | +import pytest |
| 2 | +from pubnub.endpoints.endpoint import Endpoint |
| 3 | +from pubnub.endpoints.objects_v2.channel.get_all_channels import GetAllChannels |
| 4 | +from pubnub.endpoints.objects_v2.channel.get_channel import GetChannel |
| 5 | +from pubnub.endpoints.objects_v2.channel.remove_channel import RemoveChannel |
| 6 | +from pubnub.endpoints.objects_v2.channel.set_channel import SetChannel |
| 7 | +from pubnub.exceptions import PubNubException |
| 8 | +from pubnub.models.consumer.common import PNStatus |
| 9 | +from pubnub.models.consumer.objects_v2.channel import PNSetChannelMetadataResult, PNGetChannelMetadataResult, \ |
| 10 | + PNRemoveChannelMetadataResult, PNGetAllChannelMetadataResult |
| 11 | +from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue |
| 12 | +from pubnub.pubnub_asyncio import PubNubAsyncio |
| 13 | +from pubnub.models.envelopes import AsyncioEnvelope |
| 14 | +from tests.helper import pnconf_env_copy |
| 15 | +from tests.integrational.vcr_helper import pn_vcr |
| 16 | + |
| 17 | + |
| 18 | +def _pubnub(): |
| 19 | + config = pnconf_env_copy() |
| 20 | + return PubNubAsyncio(config) |
| 21 | + |
| 22 | + |
| 23 | +class TestObjectsV2Channel: |
| 24 | + _some_channel_id = "somechannelid" |
| 25 | + _some_name = "Some name" |
| 26 | + _some_description = "Some description" |
| 27 | + _some_custom = { |
| 28 | + "key1": "val1", |
| 29 | + "key2": "val2" |
| 30 | + } |
| 31 | + |
| 32 | + def test_set_channel_endpoint_available(self): |
| 33 | + pn = _pubnub() |
| 34 | + set_channel = pn.set_channel_metadata() |
| 35 | + assert set_channel is not None |
| 36 | + assert isinstance(set_channel, SetChannel) |
| 37 | + assert isinstance(set_channel, Endpoint) |
| 38 | + |
| 39 | + def test_set_channel_is_endpoint(self): |
| 40 | + pn = _pubnub() |
| 41 | + set_channel = pn.set_channel_metadata() |
| 42 | + assert isinstance(set_channel, SetChannel) |
| 43 | + assert isinstance(set_channel, Endpoint) |
| 44 | + |
| 45 | + @pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/set_channel.json', |
| 46 | + filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') |
| 47 | + @pytest.mark.asyncio |
| 48 | + async def test_set_channel_happy_path(self): |
| 49 | + pn = _pubnub() |
| 50 | + |
| 51 | + set_channel_result = await pn.set_channel_metadata() \ |
| 52 | + .include_custom(True) \ |
| 53 | + .channel(TestObjectsV2Channel._some_channel_id) \ |
| 54 | + .set_name(TestObjectsV2Channel._some_name) \ |
| 55 | + .description(TestObjectsV2Channel._some_description) \ |
| 56 | + .custom(TestObjectsV2Channel._some_custom) \ |
| 57 | + .future() |
| 58 | + |
| 59 | + assert isinstance(set_channel_result, AsyncioEnvelope) |
| 60 | + assert isinstance(set_channel_result.result, PNSetChannelMetadataResult) |
| 61 | + assert isinstance(set_channel_result.status, PNStatus) |
| 62 | + assert not set_channel_result.status.is_error() |
| 63 | + data = set_channel_result.result.data |
| 64 | + assert data['id'] == TestObjectsV2Channel._some_channel_id |
| 65 | + assert data['name'] == TestObjectsV2Channel._some_name |
| 66 | + assert data['description'] == TestObjectsV2Channel._some_description |
| 67 | + assert data['custom'] == TestObjectsV2Channel._some_custom |
| 68 | + |
| 69 | + def test_get_channel_endpoint_available(self): |
| 70 | + pn = _pubnub() |
| 71 | + get_channel = pn.get_channel_metadata() |
| 72 | + assert get_channel is not None |
| 73 | + assert isinstance(get_channel, GetChannel) |
| 74 | + assert isinstance(get_channel, Endpoint) |
| 75 | + |
| 76 | + def test_get_channel_is_endpoint(self): |
| 77 | + pn = _pubnub() |
| 78 | + get_channel = pn.get_channel_metadata() |
| 79 | + assert isinstance(get_channel, GetChannel) |
| 80 | + assert isinstance(get_channel, Endpoint) |
| 81 | + |
| 82 | + @pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/get_channel.json', |
| 83 | + filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') |
| 84 | + @pytest.mark.asyncio |
| 85 | + async def test_get_channel_happy_path(self): |
| 86 | + pn = _pubnub() |
| 87 | + |
| 88 | + get_channel_result = await pn.get_channel_metadata() \ |
| 89 | + .include_custom(True) \ |
| 90 | + .channel(TestObjectsV2Channel._some_channel_id) \ |
| 91 | + .future() |
| 92 | + |
| 93 | + assert isinstance(get_channel_result, AsyncioEnvelope) |
| 94 | + assert isinstance(get_channel_result.result, PNGetChannelMetadataResult) |
| 95 | + assert isinstance(get_channel_result.status, PNStatus) |
| 96 | + assert not get_channel_result.status.is_error() |
| 97 | + data = get_channel_result.result.data |
| 98 | + assert data['id'] == TestObjectsV2Channel._some_channel_id |
| 99 | + assert data['name'] == TestObjectsV2Channel._some_name |
| 100 | + assert data['description'] == TestObjectsV2Channel._some_description |
| 101 | + assert data['custom'] == TestObjectsV2Channel._some_custom |
| 102 | + |
| 103 | + def test_remove_channel_endpoint_available(self): |
| 104 | + pn = _pubnub() |
| 105 | + remove_channel = pn.remove_channel_metadata() |
| 106 | + assert remove_channel is not None |
| 107 | + assert isinstance(remove_channel, RemoveChannel) |
| 108 | + assert isinstance(remove_channel, Endpoint) |
| 109 | + |
| 110 | + def test_remove_channel_is_endpoint(self): |
| 111 | + pn = _pubnub() |
| 112 | + remove_channel = pn.remove_channel_metadata() |
| 113 | + assert isinstance(remove_channel, RemoveChannel) |
| 114 | + assert isinstance(remove_channel, Endpoint) |
| 115 | + |
| 116 | + @pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/remove_channel.json', |
| 117 | + filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') |
| 118 | + @pytest.mark.asyncio |
| 119 | + async def test_remove_channel_happy_path(self): |
| 120 | + pn = _pubnub() |
| 121 | + |
| 122 | + remove_uid_result = await pn.remove_channel_metadata() \ |
| 123 | + .channel(TestObjectsV2Channel._some_channel_id) \ |
| 124 | + .future() |
| 125 | + |
| 126 | + assert isinstance(remove_uid_result, AsyncioEnvelope) |
| 127 | + assert isinstance(remove_uid_result.result, PNRemoveChannelMetadataResult) |
| 128 | + assert isinstance(remove_uid_result.status, PNStatus) |
| 129 | + assert not remove_uid_result.status.is_error() |
| 130 | + |
| 131 | + def test_get_all_channel_endpoint_available(self): |
| 132 | + pn = _pubnub() |
| 133 | + get_all_channel = pn.get_all_channel_metadata() |
| 134 | + assert get_all_channel is not None |
| 135 | + assert isinstance(get_all_channel, GetAllChannels) |
| 136 | + assert isinstance(get_all_channel, Endpoint) |
| 137 | + |
| 138 | + def test_get_all_channel_is_endpoint(self): |
| 139 | + pn = _pubnub() |
| 140 | + get_all_channel = pn.get_all_channel_metadata() |
| 141 | + assert isinstance(get_all_channel, GetAllChannels) |
| 142 | + assert isinstance(get_all_channel, Endpoint) |
| 143 | + |
| 144 | + @pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/get_all_channel.json', |
| 145 | + filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') |
| 146 | + @pytest.mark.asyncio |
| 147 | + async def test_get_all_channel_happy_path(self): |
| 148 | + pn = _pubnub() |
| 149 | + |
| 150 | + await pn.set_channel_metadata() \ |
| 151 | + .include_custom(True) \ |
| 152 | + .channel(TestObjectsV2Channel._some_channel_id) \ |
| 153 | + .set_name(TestObjectsV2Channel._some_name) \ |
| 154 | + .description(TestObjectsV2Channel._some_description) \ |
| 155 | + .custom(TestObjectsV2Channel._some_custom) \ |
| 156 | + .future() |
| 157 | + |
| 158 | + get_all_channel_result = await pn.get_all_channel_metadata() \ |
| 159 | + .include_custom(True) \ |
| 160 | + .limit(10) \ |
| 161 | + .include_total_count(True) \ |
| 162 | + .sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \ |
| 163 | + .page(None) \ |
| 164 | + .future() |
| 165 | + |
| 166 | + assert isinstance(get_all_channel_result, AsyncioEnvelope) |
| 167 | + assert isinstance(get_all_channel_result.result, PNGetAllChannelMetadataResult) |
| 168 | + assert isinstance(get_all_channel_result.status, PNStatus) |
| 169 | + assert not get_all_channel_result.status.is_error() |
| 170 | + data = get_all_channel_result.result.data |
| 171 | + assert isinstance(data, list) |
| 172 | + assert get_all_channel_result.result.total_count != 0 |
| 173 | + assert get_all_channel_result.result.next is not None |
| 174 | + assert get_all_channel_result.result.prev is None |
| 175 | + |
| 176 | + @pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/if_matches_etag.json', |
| 177 | + filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') |
| 178 | + @pytest.mark.asyncio |
| 179 | + async def test_if_matches_etag(self): |
| 180 | + pubnub = _pubnub() |
| 181 | + |
| 182 | + set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=self._some_name).future() |
| 183 | + original_etag = set_channel.result.data.get('eTag') |
| 184 | + get_channel = await pubnub.get_channel_metadata(channel=self._some_channel_id).future() |
| 185 | + assert original_etag == get_channel.result.data.get('eTag') |
| 186 | + |
| 187 | + # Update without eTag should be possible |
| 188 | + set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=f"{self._some_name}-2") \ |
| 189 | + .future() |
| 190 | + |
| 191 | + # Response should contain new eTag |
| 192 | + new_etag = set_channel.result.data.get('eTag') |
| 193 | + assert original_etag != new_etag |
| 194 | + assert set_channel.result.data.get('name') == f"{self._some_name}-2" |
| 195 | + |
| 196 | + get_channel = await pubnub.get_channel_metadata(channel=self._some_channel_id).future() |
| 197 | + assert original_etag != get_channel.result.data.get('eTag') |
| 198 | + assert get_channel.result.data.get('name') == f"{self._some_name}-2" |
| 199 | + |
| 200 | + # Update with correct eTag should be possible |
| 201 | + set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=f"{self._some_name}-3") \ |
| 202 | + .if_matches_etag(new_etag) \ |
| 203 | + .future() |
| 204 | + assert set_channel.result.data.get('name') == f"{self._some_name}-3" |
| 205 | + |
| 206 | + try: |
| 207 | + # Update with original - now outdated - eTag should fail |
| 208 | + set_channel = await pubnub.set_channel_metadata( |
| 209 | + channel=self._some_channel_id, |
| 210 | + name=f"{self._some_name}-3" |
| 211 | + ).if_matches_etag(original_etag).future() |
| 212 | + |
| 213 | + except PubNubException as e: |
| 214 | + assert e.get_status_code() == 412 |
| 215 | + assert e.get_error_message().get('message') == 'Channel to update has been modified after it was read.' |
0 commit comments