def test_dispatch_response_enqueues_message_to_outgoing_queue(self): testMessage = NetworkMessage.Builder( MessageTypes.RESPONSE).withRandomUUID().build() self.dispatcher.dispatchResponse(testMessage) try: self.dispatcher.incoming_task_queue.get_nowait() self.dispatcher.incoming_task_queue.task_done() self.fail( "Response message got queued to the incoming_task_queue! Should be sent to the outgoing_message_queue!" ) except Empty: pass try: self.dispatcher.incoming_instant_task_queue.get_nowait() self.dispatcher.incoming_instant_task_queue.task_done() self.fail( "Response message got queued to the incoming_instant_task_queue! Should be sent to the outgoing_message_queue!" ) except Empty: pass try: message = self.dispatcher.outgoing_message_queue.get_nowait() self.dispatcher.outgoing_message_queue.task_done() except Empty: self.fail( "Response message did not get queued to the incoming_instant_task_queue! Should be sent to the outgoing_message_queue!" )
def test_incoming_set_accountlist_message_is_instant(self): testMessage = NetworkMessage.Builder( MessageTypes.SET_ACCOUNT_LIST).withRandomUUID().build() self.dispatcher.dispatchIncomingMessage(testMessage) try: self.dispatcher.incoming_task_queue.get_nowait() self.dispatcher.incoming_task_queue.task_done() self.fail( "SET_ACCOUNT_LIST message got queued to the long task queue 'incoming_task_queue'! Should be an instant task on the incoming_instant_task_queue!" ) except Empty: pass try: self.dispatcher.outgoing_message_queue.get_nowait() self.dispatcher.outgoing_message_queue.task_done() self.fail( "SET_ACCOUNT_LIST message got queued to the outgoing task queue 'outgoing_message_queue'! Should be an instant task on the incoming_instant_task_queue!" ) except Empty: pass try: task = self.dispatcher.incoming_instant_task_queue.get_nowait() self.dispatcher.incoming_instant_task_queue.task_done() self.assertEqual(task.taskType, testMessage.header.messageType) except Empty: self.fail( "SET_ACCOUNT_LIST message was not queued to the incoming instant task queue 'incoming_instant_task_queue'! Should be an instant task on the incoming_instant_task_queue!" )
def __onSSHStatusChanged(self, event): if event.eventType == ConnectionEventTypes.SSH_CONNECTED: self.__loader.setStatusText("Cleaning remote workspace") message = NetworkMessage.Builder( MessageTypes.GET_WORKSPACE).withRandomUUID().build() self.__serviceHub.sendNetworkMessage(message, self.__onWorkspaceRetrieved)
def test_uuid_and_data_are_not_required(self): messageType = MessageTypes.GET_WORKSPACE message = NetworkMessage.Builder(messageType).build() self.assertEqual(message.header.messageType, messageType) self.assertIsNone(message.header.uuid) self.assertIsNone(message.data)
def initData(self): self._serviceHub.networkStatusChannel.connect( self.__onNetworkStatusChanged) self._serviceHub.startNetworkService() self._serviceHub.connectToServer() message = NetworkMessage.Builder( MessageTypes.GET_ACCOUNT_LIST).withRandomUUID().build() self._serviceHub.sendNetworkMessage(message, self.__onAccountsRetrieved)
def test_builder_can_generate_random_32_byte_uuid(self): messageType = MessageTypes.UPLOAD_FILE testData = {"testValue": "testKey"} builder = NetworkMessage.Builder(MessageTypes.UPLOAD_FILE) message = builder.withRandomUUID().withData(testData).build() self.assertIsNotNone(message.header.uuid) self.assertEqual(len(message.header.uuid), 32) self.assertEqual(str, type(message.header.uuid))
def test_builder_builds_with_passed_parameters(self): messageType = MessageTypes.UPLOAD_FILE testUUID = uuid4().hex testData = {"testValue": "testKey"} builder = NetworkMessage.Builder(MessageTypes.UPLOAD_FILE) message = builder.withUUID(testUUID).withData(testData).build() self.assertEqual(message.header.messageType, messageType) self.assertEqual(message.data, testData)
def __firstStartSetupAccounts(self): self.__loader.setStatusText("Updating accounts") data = { "accounts": [acc.serialize() for acc in self.__firstStartAccounts] } message = NetworkMessage.Builder( MessageTypes.SET_ACCOUNT_LIST).withRandomUUID().withData( data).build() self.__serviceHub.sendNetworkMessage(message, self.__onFirstStartAccountsSaved)
def test_incoming_move_file_message_is_instant_and_stops_and_removes_related_file_tasks( self, cancelTaskMock, removeTaskMock): testData = { "source": "testSourcePath", "target": { "fullPath": "testTargetPath" } } testMessage = NetworkMessage.Builder( MessageTypes.MOVE_FILE).withRandomUUID().withData( testData).build() self.dispatcher.dispatchIncomingMessage(testMessage) try: self.dispatcher.incoming_task_queue.get_nowait() self.dispatcher.incoming_task_queue.task_done() self.fail( "MOVE_FILE message got queued to the long task queue 'incoming_task_queue'! Should be an instant task on the incoming_instant_task_queue!" ) except Empty: pass try: self.dispatcher.outgoing_message_queue.get_nowait() self.dispatcher.outgoing_message_queue.task_done() self.fail( "MOVE_FILE message got queued to the outgoing task queue 'outgoing_message_queue'! Should be an instant task on the incoming_instant_task_queue!" ) except Empty: pass try: task = self.dispatcher.incoming_instant_task_queue.get_nowait() self.dispatcher.incoming_instant_task_queue.task_done() self.assertEqual(task.taskType, testMessage.header.messageType) except Empty: self.fail( "MOVE_FILE message was not queued to the incoming instant task queue 'incoming_instant_task_queue'! Should be an instant task on the incoming_instant_task_queue!" ) removeTaskMock self.assertEqual(cancelTaskMock.call_count, 2) self.assertEqual(cancelTaskMock.call_args_list[0][0][0], testData["source"]) self.assertEqual(cancelTaskMock.call_args_list[1][0][0], testData["target"]["fullPath"]) self.assertEqual(removeTaskMock.call_count, 2) self.assertEqual(removeTaskMock.call_args_list[0][0][0], testData["source"]) self.assertEqual(removeTaskMock.call_args_list[1][0][0], testData["target"]["fullPath"])
def test_builder_generated_uuid_is_random(self): messageType = MessageTypes.UPLOAD_FILE testData = {"testValue": "testKey"} builder = NetworkMessage.Builder(MessageTypes.UPLOAD_FILE) message1 = builder.withRandomUUID().withData(testData).build() message2 = builder.withRandomUUID().withData(testData).build() self.assertIsNotNone(message1.header.uuid) self.assertIsNotNone(message2.header.uuid) self.assertEqual(len(message1.header.uuid), 32) self.assertEqual(len(message2.header.uuid), 32) self.assertEqual(str, type(message1.header.uuid)) self.assertEqual(str, type(message2.header.uuid)) self.assertNotEqual(message1.header.uuid, message2.header.uuid)
def handle(self): self._logger.debug("Updating account list") currentAccounts = {acc.id: acc.identifier for acc in self._databaseAccess.getAllAccounts()} newAccounts = [AccountData(id=raw.get('id', None), identifier=raw['identifier'], accountType=raw['accountType'], cryptoKey=raw['cryptoKey'], data=raw['data']) for raw in self._task.data['accounts']] newAccountIDs = [acc.id for acc in newAccounts] for accID, accName in currentAccounts.items(): if accID not in newAccountIDs: self._logger.debug(f"Deleting account: {accName}(ID: {accID})") self._databaseAccess.deleteAccount(accID) for account in newAccounts: self._databaseAccess.createOrUpdateAccount(account) self._databaseAccess.commit() self._logger.debug("Accounts updated") response = NetworkMessage.Builder(MessageTypes.RESPONSE).withUUID(self._task.uuid).build() self._messageDispatcher.dispatchResponse(response)
def test_incoming_delete_file_message_is_instant_but_not_queued_and_stops_and_removes_related_file_tasks( self, cancelTaskMock, removeTaskMock): testData = {"fullPath": "testCancelledFilePath"} testMessage = NetworkMessage.Builder( MessageTypes.FILE_TASK_CANCELLED).withRandomUUID().withData( testData).build() self.dispatcher.dispatchIncomingMessage(testMessage) try: self.dispatcher.incoming_task_queue.get_nowait() self.dispatcher.incoming_task_queue.task_done() self.fail( "FILE_TASK_CANCELLED message got queued to the instant task queue 'incoming_instant_task_queue'! Should be an instant task handled by the dispatcher." ) except Empty: pass try: self.dispatcher.incoming_task_queue.get_nowait() self.dispatcher.incoming_task_queue.task_done() self.fail( "FILE_TASK_CANCELLED message got queued to the long task queue 'incoming_task_queue'! Should be an instant task handled by the dispatcher." ) except Empty: pass try: self.dispatcher.outgoing_message_queue.get_nowait() self.dispatcher.outgoing_message_queue.task_done() self.fail( "FILE_TASK_CANCELLED message got queued to the outgoing task queue 'outgoing_message_queue'! Should be an instant task handled by the dispatcher." ) except Empty: pass self.assertEqual(cancelTaskMock.call_count, 1) self.assertEqual(cancelTaskMock.call_args[0][0], testData["fullPath"]) self.assertEqual(removeTaskMock.call_count, 1) self.assertEqual(removeTaskMock.call_args[0][0], testData["fullPath"])
def test_incoming_download_file_is_a_long_task_and_is_added_to_the_task_archive( self, addTaskMock): testData = {"fullPath": "testFullPath"} testMessage = NetworkMessage.Builder( MessageTypes.UPLOAD_FILE).withRandomUUID().withData( testData).build() self.dispatcher.dispatchIncomingMessage(testMessage) try: self.dispatcher.incoming_instant_task_queue.get_nowait() self.dispatcher.incoming_instant_task_queue.task_done() self.fail( "DOWNLOAD_FILE message got queued to the incoming_instant_task_queue! Should be sent to the instant_task_queue!" ) except Empty: pass try: self.dispatcher.outgoing_message_queue.get_nowait() self.dispatcher.outgoing_message_queue.task_done() self.fail( "DOWNLOAD_FILE message got queued to the outgoing_message_queue! Should be sent to the instant_task_queue!" ) except Empty: pass try: task = self.dispatcher.incoming_task_queue.get_nowait() self.dispatcher.incoming_task_queue.task_done() self.assertEqual(task.data["fullPath"], testData["fullPath"]) except Empty: self.fail( "DOWNLOAD_FILE message was not queued to the incoming_task_queue! Should be sent to the incoming_task_queue!" ) self.assertEqual(addTaskMock.call_count, 1) self.assertEqual(addTaskMock.call_args[0][0], testData["fullPath"])
def handle(self): # targetPath has to be deleted if exists. No matter the parts. cachedTargetFile = self._filesCache.getFile(self._task.data["target"]["fullPath"]) if cachedTargetFile: self._logger.info("Pre-cleaning target file parts") self.__cleanFromRemote(cachedTargetFile) self._filesCache.removeFile(self._task.data["target"]["fullPath"]) targetFileData = self._task.data["target"] cachedSourceFile = self._filesCache.getFile(self._task.data["source"]) responseData = None # if sourcePath is synced, simple move and respond with moved, else delete sourcePath and respond with reupload. if self.__isSourceSynced(cachedSourceFile, targetFileData): self.__moveFile(cachedSourceFile, targetFileData) responseData = {"moveSuccessful": True, "from": self._task.data["source"], "to": targetFileData["fullPath"]} else: if cachedSourceFile: self.__cleanFromRemote(cachedSourceFile) self._filesCache.removeFile(cachedSourceFile.data.fullPath) responseData = {"moveSuccessful": False, "from": self._task.data["source"], "to": targetFileData["fullPath"]} response = NetworkMessage.Builder(MessageTypes.RESPONSE).withUUID(self._task.uuid).withData(responseData).build() self._messageDispatcher.dispatchResponse(response) self._task = None
def __sendResponse(self, fullFiles): response = NetworkMessage.Builder(MessageTypes.RESPONSE).withUUID(self._task.uuid).withData(fullFiles).build() self._messageDispatcher.dispatchResponse(response)
def handle(self): data = {"accounts": [acc.serialize() for acc in self._databaseAccess.getAllAccounts()]} response = NetworkMessage.Builder(MessageTypes.RESPONSE).withUUID(self._task.uuid).withData(data).build() self._messageDispatcher.dispatchResponse(response) self._task = None
def __sendResponse(self): data = self._task.data data["status"] = FileStatuses.DOWNLOADING_TO_LOCAL response = NetworkMessage.Builder(MessageTypes.FILE_STATUS_UPDATE).withData(data).withUUID(self._task.uuid).build() self._messageDispatcher.dispatchResponse(response)
def __sendResponse(self): if not self._task.stale: data = {"fullPath": self._task.data["fullPath"], "filename": self._task.data["filename"], "modified": self._task.data["utcModified"], "size": self._task.data["size"], "path": self._task.data["path"], "status": FileStatuses.SYNCED} response = NetworkMessage.Builder(MessageTypes.FILE_STATUS_UPDATE).withData(data).withRandomUUID().build() self._messageDispatcher.dispatchResponse(response)
def test_invalid_message_type(self): builder = NetworkMessage.Builder("asd") with self.assertRaises(ValueError): builder.build()
def handle(self): data = {"workspace": control.cli.CONSOLE_ARGUMENTS.workspace} response = NetworkMessage.Builder(MessageTypes.RESPONSE).withUUID(self._task.uuid).withData(data).build() self._messageDispatcher.dispatchResponse(response) self._task = None
def syncFileList(self): self.__logger.debug("Syncing file list") message = NetworkMessage.Builder(MessageTypes.SYNC_FILES).withRandomUUID().build() self.__serviceHub.sendNetworkMessage(message, self.__onFilelistRetrieved)