def test_message_encryption_with_random_iv_taken_from_config(self): pn_config = pnconf_file_copy() pn_config.use_random_initialization_vector = True crypto_with_custom_settings = PubNubCryptodome(pn_config) self.test_message_encryption_with_random_iv( crypto_with_custom_settings)
async def test_list_files(event_loop): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) envelope = await pubnub.list_files().channel(CHANNEL).future() assert isinstance(envelope.result, PNGetFilesResult) assert envelope.result.count == 23 pubnub.stop()
def test_fetch_file_upload_s3_data_with_result_invocation(event_loop, file_upload_test_data): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) result = yield from pubnub._fetch_file_upload_s3_data().\ channel(CHANNEL).\ file_name(file_upload_test_data["UPLOADED_FILENAME"]).result() assert isinstance(result, PNFetchFileUploadS3DataResult) pubnub.stop()
def test_encrypt_and_decrypt_file(self, file_for_upload, file_upload_test_data): pubnub = PubNub(pnconf_file_copy()) with open(file_for_upload.strpath, "rb") as fd: encrypted_file = pubnub.encrypt(KEY, fd.read()) decrypted_file = pubnub.decrypt(KEY, encrypted_file) assert file_upload_test_data["FILE_CONTENT"] == decrypted_file.decode( "utf-8")
async def test_send_and_download_file(event_loop, file_for_upload): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) envelope = await send_file(pubnub, file_for_upload) download_envelope = await pubnub.download_file().\ channel(CHANNEL).\ file_id(envelope.result.file_id).\ file_name(envelope.result.name).future() assert isinstance(download_envelope.result, PNDownloadFileResult) pubnub.stop()
async def test_get_file_url(event_loop, file_for_upload): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) envelope = await send_file(pubnub, file_for_upload) file_url_envelope = await pubnub.get_file_url().\ channel(CHANNEL).\ file_id(envelope.result.file_id).\ file_name(envelope.result.name).future() assert isinstance(file_url_envelope.result, PNGetFileDownloadURLResult) pubnub.stop()
def test_get_file_url_has_auth_key_in_url_and_signature(file_upload_test_data): pubnub = PubNub(pnconf_file_copy()) pubnub.config.uuid = "files_native_sync_uuid" pubnub.config.auth_key = "test_auth_key" file_url_envelope = pubnub.get_file_url().\ channel(CHANNEL).\ file_id("random_file_id").\ file_name("random_file_name").sync() assert "auth=test_auth_key" in file_url_envelope.status.client_request.url
async def test_delete_file(event_loop, file_for_upload): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) pubnub.config.uuid = "files_asyncio_uuid" envelope = await send_file(pubnub, file_for_upload) delete_envelope = await pubnub.delete_file().\ channel(CHANNEL).\ file_id(envelope.result.file_id).\ file_name(envelope.result.name).future() assert isinstance(delete_envelope.result, PNDeleteFileResult) pubnub.stop()
def test_publish_with_ptto_and_replicate(self): timetoken_to_override = 16057799474000000 env = PubNub(pnconf_file_copy()).publish()\ .channel("ch1")\ .message("hi")\ .replicate(False)\ .ptto(timetoken_to_override)\ .sync() assert isinstance(env.result, PNPublishResult) assert "ptto" in env.status.client_request.url assert "norep" in env.status.client_request.url
def test_send_and_download_file_encrypted(event_loop, file_for_upload, file_upload_test_data): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) envelope = yield from send_file(pubnub, file_for_upload, cipher_key="test") download_envelope = yield from pubnub.download_file().\ channel(CHANNEL).\ file_id(envelope.result.file_id).\ file_name(envelope.result.name).\ cipher_key("test").\ future() assert isinstance(download_envelope.result, PNDownloadFileResult) assert download_envelope.result.data == bytes(file_upload_test_data["FILE_CONTENT"], "utf-8") pubnub.stop()
def test_publish_file_message_with_encryption(event_loop, file_upload_test_data): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) envelope = yield from PublishFileMessage(pubnub).\ channel(CHANNEL).\ meta({}).\ message({"test": "test"}).\ file_id("2222").\ file_name("test").\ should_store(True).\ ttl(222).future() assert isinstance(envelope.result, PNPublishFileMessageResult) pubnub.stop()
async def test_send_and_download_file_encrypted(event_loop, file_for_upload, file_upload_test_data): pubnub = PubNubAsyncio(pnconf_file_copy(), custom_event_loop=event_loop) with patch("pubnub.crypto.PubNubCryptodome.get_initialization_vector", return_value="knightsofni12345"): envelope = await send_file(pubnub, file_for_upload, cipher_key="test") download_envelope = await pubnub.download_file().\ channel(CHANNEL).\ file_id(envelope.result.file_id).\ file_name(envelope.result.name).\ cipher_key("test").\ future() assert isinstance(download_envelope.result, PNDownloadFileResult) assert download_envelope.result.data == bytes( file_upload_test_data["FILE_CONTENT"], "utf-8") await pubnub.stop()
sys.path.append(PUBNUB_ROOT) from tests.helper import pnconf_file_copy import pubnub as pn from pubnub.callbacks import SubscribeCallback from pubnub.pubnub import PubNub pn.set_stream_logger('pubnub', logging.DEBUG) logger = logging.getLogger("file_upload") class FileSubscribeCallback(SubscribeCallback): def message(self, pubnub, event): print("MESSAGE: ") print(event.message) def file(self, pubnub, event): print("FILE: ") print(event.message) print(event.file_url) print(event.file_name) print(event.file_id) pubnub = PubNub(pnconf_file_copy()) pubnub.config.cipher_key = "silly_walk" my_listener = FileSubscribeCallback() pubnub.add_listener(my_listener) pubnub.subscribe().channels("files_native_sync_ch").execute()
from pubnub.pubnub import PubNub from pubnub.crypto import PubNubCryptodome from tests.helper import gen_decrypt_func from tests.helper import pnconf_file_copy crypto = PubNubCryptodome(pnconf_file_copy()) todecode = 'QfD1NCBJCmt1aPPGU2cshw==' plaintext_message = "hey-0" KEY = 'testKey' class TestPubNubCryptodome: def test_decode_aes(self): multiline_test_message = """ dfjn t564 sdfhp\n """ assert crypto.decrypt(KEY, crypto.encrypt( KEY, multiline_test_message)) == multiline_test_message assert crypto.decrypt(KEY, todecode) == plaintext_message def test_vc_body_decoder(self): input = b'"9P/7+NNs54o7Go41yh+3rIn8BW0H0ad+mKlKTKGw2i1eoQP1ddHrnIzkRUPEC3ko"' # print(json.loads(input.decode('utf-8'))) assert {"name": "Alex", "online": True} == \ gen_decrypt_func()(input.decode('utf-8'))