Exemplo n.º 1
0
    def test_db_access_creates_or_updates_accounts(self, expanduserMock, mkdirMock, sqlite3_connectMock):
        testAccountData = [
            AccountData(AccountTypes.Dropbox, "testAccount1", "testCryptoKey1", {"apiToken": "testApiToken1"}),
            AccountData(AccountTypes.Dropbox, "testAccount2", "testCryptoKey2", {"apiToken": "testApiToken2"}, 1)
        ]

        fakeConnection = MagicMock()
        fakeCursor = MagicMock()

        sqlite3_connectMock.return_value = fakeConnection
        sqlite3_connectMock.return_value.cursor.return_value = fakeCursor

        fakeCursor.fetchone.return_value = [1]

        db = DatabaseAccess()
        db.createOrUpdateAccount(testAccountData[0])
        db.createOrUpdateAccount(testAccountData[1])

        self.assertEqual(fakeCursor.execute.call_count, 4)
        self.assertEqual(fakeCursor.execute.call_args_list[1][0][0], "SELECT MAX(id) from accounts;")

        insertCommand = f"INSERT INTO accounts(id, identifier, accountType, cryptoKey, data) VALUES(2, '{testAccountData[0].identifier}', {testAccountData[0].accountType}, '{testAccountData[0].cryptoKey}', '{json.dumps(testAccountData[0].data)}');"
        self.assertEqual(fakeCursor.execute.call_args_list[2][0][0], insertCommand)

        updateCommand = f"UPDATE accounts SET identifier = '{testAccountData[1].identifier}', cryptoKey = '{testAccountData[1].cryptoKey}', data = '{json.dumps(testAccountData[1].data)}' WHERE id = 1;"
        self.assertEqual(fakeCursor.execute.call_args_list[3][0][0], updateCommand)

        self.assertEqual(fakeConnection.commit.call_count, 1)
    def test_stops_download_and_does_not_send_response_and_cleans_up_if_file_download_is_interrupted(
            self, openMock, cloudApiMock, getFileMock, dispatchResponseMock,
            os_removeMock):
        openMock.return_value = BytesIO()
        testFileData = FileData(filename="apple.txt",
                                modified=10,
                                size=10,
                                path="subDir",
                                fullPath="subDir/apple.txt")
        testFilePart = FilePart(filename="apple.txt__1__1.enc",
                                modified=10,
                                size=testFileData.size + 16,
                                path="subDir",
                                fullPath="subDir/apple.txt__1__1.enc",
                                storingAccountID=1)
        fakeAccounts = [
            AccountData(id=1,
                        identifier="testAccountID1",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken1"}),
            AccountData(id=2,
                        identifier="testAccountID2",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken2"})
        ]
        self.fakeDB.getAllAccounts.return_value = fakeAccounts
        testCachedFileInfo = CachedFileData(
            data=testFileData,
            availablePartCount=1,
            totalPartCount=1,
            parts={"subDir/apple.txt__1__1.enc": testFilePart})
        getFileMock.return_value = testCachedFileInfo

        testTask = Task(taskType=MessageTypes.DOWNLOAD_FILE,
                        data=testFileData.serialize(),
                        uuid=uuid4().hex,
                        stale=True)

        testHandler = DownloadFileHandler(self.fakeDB)
        testHandler.setTask(testTask)
        testHandler.handle()

        self.assertEqual(openMock.call_count, 1)
        self.assertEqual(openMock.call_args[0][0],
                         f"testWorkspace/server/{testTask.uuid}")
        self.assertEqual(openMock.call_args[0][1], "wb")

        self.assertEqual(dispatchResponseMock.call_count, 0)

        self.assertEqual(os_removeMock.call_count, 1)
        self.assertEqual(os_removeMock.call_args[0][0],
                         f"testWorkspace/server/{testTask.uuid}")
    def test_uploads_file_to_all_accounts_if_per_accounts_size_is_greater_than_one(
            self, openMock, cloudApiMock, insertFilePartMock, getFileMock,
            os_unlinkMock, dispatchResponseMock):
        fakeFile = BytesIO(b"Lorem ipsum")
        openMock.return_value = fakeFile
        fakeAccounts = [
            AccountData(id=1,
                        identifier="testAccountID1",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken1"}),
            AccountData(id=2,
                        identifier="testAccountID2",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken2"})
        ]
        self.fakeDB.getAllAccounts.return_value = fakeAccounts
        getFileMock.return_value = None

        testTaskData = {
            "filename": "apple.txt",
            "size": 10,
            "fullPath": "subDir/apple.txt",
            "status": None,
            "utcModified": 10,
            "path": "subDir"
        }
        fakeUploadResult = FilePart(filename=testTaskData["filename"],
                                    modified=testTaskData["utcModified"],
                                    size=testTaskData["size"],
                                    path=testTaskData["path"],
                                    fullPath=testTaskData["fullPath"],
                                    storingAccountID=fakeAccounts[0].id)
        fakeCloudAccount1 = MagicMock()
        fakeCloudAccount2 = MagicMock()
        cloudApiMock.side_effect = [fakeCloudAccount1, fakeCloudAccount2]

        testTask = Task(taskType=MessageTypes.UPLOAD_FILE,
                        data=testTaskData,
                        uuid=uuid4().hex)
        testHandler = UploadFileHandler(self.fakeDB)
        testHandler.setTask(testTask)

        testHandler.handle()

        self.assertEqual(fakeCloudAccount1.upload.call_count, 1)
        self.assertEqual(fakeCloudAccount1.upload.call_args[0][2],
                         f"{testTaskData['filename']}__1__2.enc")
        self.assertEqual(fakeCloudAccount2.upload.call_count, 1)
        self.assertEqual(fakeCloudAccount2.upload.call_args[0][2],
                         f"{testTaskData['filename']}__2__2.enc")
    def test_handler_retrieves_accounts_from_database_and_sends_response(
            self, dispatchResponseMock):
        testAccounts = [
            AccountData(id=1,
                        identifier="testAccountID",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken"})
        ]
        testTask = Task(taskType=MessageTypes.GET_ACCOUNT_LIST,
                        uuid=uuid4().hex)

        self.fakeDB.getAllAccounts.return_value = testAccounts

        self.testHandler.setTask(testTask)
        self.testHandler.handle()

        self.assertEqual(self.fakeDB.getAllAccounts.call_count, 1)

        self.assertEqual(dispatchResponseMock.call_count, 1)
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].header.messageType,
            MessageTypes.RESPONSE)
        self.assertEqual(dispatchResponseMock.call_args[0][0].header.uuid,
                         testTask.uuid)

        self.assertEqual(
            type(dispatchResponseMock.call_args[0][0].data["accounts"]), list)
        self.assertEqual(
            len(dispatchResponseMock.call_args[0][0].data["accounts"]),
            len(testAccounts))

        self.assertEqual(
            dispatchResponseMock.call_args[0][0].data["accounts"][0],
            testAccounts[0].serialize())
    def test_get_file_list_returns_all_non_full_files(self,
                                                      dispatchResponseMock,
                                                      fakeAPIFactory):

        fakeCloudAPI = MagicMock()
        fakeGetFileListResponse = [
            FilePart(filename="full_file__1__1.enc",
                     modified=1,
                     size=32,
                     path="",
                     fullPath="full_file__1__1.enc",
                     storingAccountID=1),
            FilePart(filename="partial_file__1__2.enc",
                     modified=1,
                     size=48,
                     path="subDir",
                     fullPath="subDir/partial_file__1__2.enc",
                     storingAccountID=1)
        ]
        fakeCloudAPI.getFileList.return_value = fakeGetFileListResponse

        self.fakeDB.getAllAccounts.return_value = [
            AccountData(id=1,
                        identifier="testAccountID",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken"})
        ]
        fakeAPIFactory.return_value = fakeCloudAPI
        testTask = Task(taskType=MessageTypes.SYNC_FILES, uuid=uuid4().hex)

        testHandler = GetFileListHandler(self.fakeDB)
        testHandler.setTask(testTask)
        testHandler.handle()

        self.assertEqual(fakeCloudAPI.getFileList.call_count, 1)
        self.assertEqual(fakeAPIFactory.call_count, 1)
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].header.messageType,
            MessageTypes.RESPONSE)
        self.assertEqual(dispatchResponseMock.call_args[0][0].header.uuid,
                         testTask.uuid)

        self.assertEqual(len(dispatchResponseMock.call_args[0][0].data), 1)
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].data[0]["filename"],
            "full_file")
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].data[0]["modified"],
            fakeGetFileListResponse[0].modified)
        self.assertEqual(dispatchResponseMock.call_args[0][0].data[0]["size"],
                         fakeGetFileListResponse[0].size - 16)
        self.assertEqual(dispatchResponseMock.call_args[0][0].data[0]["path"],
                         fakeGetFileListResponse[0].path)
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].data[0]["fullPath"],
            "full_file")
Exemplo n.º 6
0
 def getAllAccounts(self):
     self.__cursor.execute("SELECT * FROM accounts;")
     rawAccounts = self.__cursor.fetchall()
     return [
         AccountData(id=raw[0],
                     identifier=raw[1],
                     accountType=raw[2],
                     cryptoKey=raw[3],
                     data=json.loads(raw[4])) for raw in rawAccounts
     ]
    def test_upload_is_interrupted_if_task_is_stale_and_no_response_is_sent(
            self, openMock, cloudApiMock, insertFilePartMock, getFileMock,
            os_unlinkMock, dispatchResponseMock):
        fakeFile = BytesIO(b"Lorem ipsum")
        openMock.return_value = fakeFile
        fakeAccounts = [
            AccountData(id=1,
                        identifier="testAccountID",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken"})
        ]
        self.fakeDB.getAllAccounts.return_value = fakeAccounts
        getFileMock.return_value = None

        testTaskData = {
            "filename": "apple.txt",
            "size": 11,
            "fullPath": "subDir/apple.txt",
            "status": None,
            "utcModified": 10,
            "path": "subDir"
        }
        fakeUploadResult = None
        cloudApiMock.return_value.upload.return_value = fakeUploadResult

        testTask = Task(taskType=MessageTypes.UPLOAD_FILE,
                        data=testTaskData,
                        uuid=uuid4().hex,
                        stale=True)
        testHandler = UploadFileHandler(self.fakeDB)
        testHandler.setTask(testTask)
        testHandler.handle()

        self.assertEqual(openMock.call_count, 1)
        self.assertEqual(openMock.call_args[0][0],
                         f"testWorkspace/server/{testTask.uuid}")
        self.assertEqual(openMock.call_args[0][1], "rb")

        self.assertEqual(cloudApiMock.call_count, 0)

        self.assertEqual(insertFilePartMock.call_count, 0)

        self.assertEqual(getFileMock.call_count, 1)
        self.assertEqual(getFileMock.call_args[0][0], testTaskData["fullPath"])

        self.assertEqual(os_unlinkMock.call_count, 1)
        self.assertEqual(os_unlinkMock.call_args[0][0],
                         f"testWorkspace/server/{testTask.uuid}")

        self.assertEqual(dispatchResponseMock.call_count, 0)
Exemplo n.º 8
0
    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_set_account_list_handler_sets_accounts_and_returns_response(
            self, dispatchResponseMock):
        testAccounts = {
            "accounts": [
                AccountData(id=1,
                            identifier="testAccountID",
                            accountType=AccountTypes.Dropbox,
                            cryptoKey="sixteen byte key",
                            data={
                                "apiToken": "testApitoken"
                            }).serialize()
            ]
        }
        testTask = Task(taskType=MessageTypes.SET_ACCOUNT_LIST,
                        uuid=uuid4().hex,
                        data=testAccounts)

        self.testHandler.setTask(testTask)
        self.testHandler.handle()

        self.assertEqual(self.fakeDB.createOrUpdateAccount.call_count, 1)
        self.assertEqual(self.fakeDB.createOrUpdateAccount.call_args[0][0].id,
                         testAccounts["accounts"][0]["id"])
        self.assertEqual(
            self.fakeDB.createOrUpdateAccount.call_args[0][0].identifier,
            testAccounts["accounts"][0]["identifier"])
        self.assertEqual(
            self.fakeDB.createOrUpdateAccount.call_args[0][0].accountType,
            testAccounts["accounts"][0]["accountType"])
        self.assertEqual(
            self.fakeDB.createOrUpdateAccount.call_args[0][0].cryptoKey,
            testAccounts["accounts"][0]["cryptoKey"])
        self.assertEqual(
            self.fakeDB.createOrUpdateAccount.call_args[0][0].data,
            testAccounts["accounts"][0]["data"])

        self.assertEqual(dispatchResponseMock.call_count, 1)
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].header.messageType,
            MessageTypes.RESPONSE)
        self.assertEqual(dispatchResponseMock.call_args[0][0].header.uuid,
                         testTask.uuid)
Exemplo n.º 10
0
    def test_removes_destination_path_on_all_accounts_then_tries_to_move_unsynced_files_and_sends_unsuccessful_response(
            self, getFileMock, cloudApiMock, removeFileMock,
            dispatchResponseMock, moveFileMock):
        testSourceFileData = FileData(filename="apple.txt",
                                      modified=10,
                                      size=10,
                                      path="",
                                      fullPath="apple.txt")
        testTargetFileData = FileData(filename="apple.txt",
                                      modified=10,
                                      size=20,
                                      path="subDir",
                                      fullPath="subDir/apple.txt")

        testTask = Task(taskType=MessageTypes.MOVE_FILE,
                        data={
                            "source": "apple.txt",
                            "target": testTargetFileData.serialize()
                        })
        fakeAccounts = [
            AccountData(id=1,
                        identifier="testAccountID1",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken1"}),
            AccountData(id=2,
                        identifier="testAccountID2",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken2"})
        ]
        self.fakeDB.getAllAccounts.return_value = fakeAccounts

        testSourceFileParts = [
            FilePart(filename="apple.txt__1__2.enc",
                     modified=10,
                     size=21,
                     path="",
                     fullPath="apple.txt__1__2.enc",
                     storingAccountID=1),
        ]
        testTargetFileParts = [
            FilePart(filename="apple.txt__1__2.enc",
                     modified=15,
                     size=26,
                     path="subDir",
                     fullPath="subDir/apple.txt__1__2.enc",
                     storingAccountID=1),
            FilePart(filename="apple.txt__2__2.enc",
                     modified=15,
                     size=26,
                     path="subDir",
                     fullPath="subDir/apple.txt__2__2.enc",
                     storingAccountID=2)
        ]

        testSourceCachedFileInfo = CachedFileData(
            data=testSourceFileData,
            availablePartCount=1,
            totalPartCount=2,
            parts={
                filepart.filename: filepart
                for filepart in testSourceFileParts
            })
        testTargetCachedFileInfo = CachedFileData(
            data=testTargetFileData,
            availablePartCount=2,
            totalPartCount=2,
            parts={
                filepart.filename: filepart
                for filepart in testTargetFileParts
            })
        getFileMock.side_effect = [
            testTargetCachedFileInfo, testSourceCachedFileInfo
        ]

        fakeCloudAccount1 = MagicMock()
        fakeCloudAccount1.accountData = fakeAccounts[0]
        fakeCloudAccount2 = MagicMock()
        fakeCloudAccount2.accountData = fakeAccounts[1]
        cloudApiMock.side_effect = [
            fakeCloudAccount1, fakeCloudAccount2, fakeCloudAccount1,
            fakeCloudAccount2
        ]

        testHandler = MoveFileHandler(self.fakeDB)
        testHandler.setTask(testTask)
        testHandler.handle()

        self.assertEqual(fakeCloudAccount1.deleteFile.call_count, 2)
        self.assertEqual(fakeCloudAccount2.deleteFile.call_count, 1)
        self.assertEqual(fakeCloudAccount1.moveFile.call_count, 0)
        self.assertEqual(fakeCloudAccount2.moveFile.call_count, 0)
        self.assertEqual(getFileMock.call_count, 2)
        self.assertEqual(removeFileMock.call_count, 2)
        self.assertEqual(dispatchResponseMock.call_count, 1)
        self.assertEqual(moveFileMock.call_count, 0)

        self.assertEqual(fakeCloudAccount1.deleteFile.call_args[0][0],
                         testSourceFileParts[0])
        self.assertEqual(fakeCloudAccount2.deleteFile.call_args[0][0],
                         testTargetFileParts[1])

        self.assertEqual(getFileMock.call_args_list[0][0][0],
                         testTargetFileData.fullPath)
        self.assertEqual(removeFileMock.call_args[0][0],
                         testSourceFileData.fullPath)

        self.assertEqual(
            dispatchResponseMock.call_args[0][0].header.messageType,
            MessageTypes.RESPONSE)
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].data, {
                "moveSuccessful": False,
                "from": "apple.txt",
                "to": testTargetFileData.fullPath
            })
Exemplo n.º 11
0
    def test_delete_file_handler_removes_filedata_from_all_accounts(
            self, getFileMock, cloudApiMock, removeFileMock):
        testFileData = FileData(filename="apple.txt",
                                modified=10,
                                size=10,
                                path="subDir",
                                fullPath="subDir/apple.txt")
        testTask = Task(taskType=MessageTypes.DELETE_FILE,
                        data={"fullPath": "apple.txt"})
        fakeAccounts = [
            AccountData(id=1,
                        identifier="testAccountID1",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken1"}),
            AccountData(id=2,
                        identifier="testAccountID2",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken2"})
        ]
        self.fakeDB.getAllAccounts.return_value = fakeAccounts
        testFileParts = [
            FilePart(filename="apple.txt__1__2.enc",
                     modified=10,
                     size=21,
                     path="subDir",
                     fullPath="subDir/apple.txt__1__2.enc",
                     storingAccountID=1),
            FilePart(filename="apple.txt__2__2.enc",
                     modified=10,
                     size=21,
                     path="subDir",
                     fullPath="subDir/apple.txt__2__2.enc",
                     storingAccountID=2)
        ]
        testCachedFileInfo = CachedFileData(
            data=testFileData,
            availablePartCount=2,
            totalPartCount=2,
            parts={filepart.filename: filepart
                   for filepart in testFileParts})
        getFileMock.return_value = testCachedFileInfo

        fakeCloudAccount1 = MagicMock()
        fakeCloudAccount2 = MagicMock()
        cloudApiMock.side_effect = [fakeCloudAccount1, fakeCloudAccount2]

        testHandler = DeleteFileHandler(self.fakeDB)
        testHandler.setTask(testTask)
        testHandler.handle()

        self.assertEqual(getFileMock.call_count, 1)
        self.assertEqual(getFileMock.call_args[0][0],
                         testTask.data["fullPath"])

        self.assertEqual(cloudApiMock.call_count, 2)
        self.assertEqual(cloudApiMock.call_args_list[0][0][0], fakeAccounts[0])
        self.assertEqual(cloudApiMock.call_args_list[1][0][0], fakeAccounts[1])

        self.assertEqual(removeFileMock.call_count, 1)
        self.assertEqual(removeFileMock.call_args[0][0],
                         testTask.data["fullPath"])

        self.assertEqual(fakeCloudAccount1.deleteFile.call_count, 1)
        self.assertEqual(fakeCloudAccount1.deleteFile.call_args[0][0],
                         testFileParts[0])

        self.assertEqual(fakeCloudAccount2.deleteFile.call_count, 1)
        self.assertEqual(fakeCloudAccount2.deleteFile.call_args[0][0],
                         testFileParts[1])
Exemplo n.º 12
0
    def test_downloads_file_and_moves_it_to_client_workspace_and_sends_response(
            self, openMock, cloudApiMock, getFileMock, dispatchResponseMock,
            os_renameMock):
        openMock.return_value = BytesIO()
        testFileData = FileData(filename="apple.txt",
                                modified=10,
                                size=10,
                                path="subDir",
                                fullPath="subDir/apple.txt")
        testFilePart = FilePart(filename="apple.txt__1__1.enc",
                                modified=10,
                                size=testFileData.size + 16,
                                path="subDir",
                                fullPath="subDir/apple.txt__1__1.enc",
                                storingAccountID=1)
        fakeAccounts = [
            AccountData(id=1,
                        identifier="testAccountID1",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken1"}),
            AccountData(id=2,
                        identifier="testAccountID2",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken2"})
        ]
        self.fakeDB.getAllAccounts.return_value = fakeAccounts
        testCachedFileInfo = CachedFileData(
            data=testFileData,
            availablePartCount=1,
            totalPartCount=1,
            parts={"subDir/apple.txt__1__1.enc": testFilePart})
        getFileMock.return_value = testCachedFileInfo

        testTask = Task(taskType=MessageTypes.DOWNLOAD_FILE,
                        data=testFileData.serialize(),
                        uuid=uuid4().hex)

        testHandler = DownloadFileHandler(self.fakeDB)
        testHandler.setTask(testTask)
        testHandler.handle()

        self.assertEqual(openMock.call_count, 1)
        self.assertEqual(openMock.call_args[0][0],
                         f"testWorkspace/server/{testTask.uuid}")
        self.assertEqual(openMock.call_args[0][1], "wb")

        self.assertEqual(cloudApiMock.call_count, 1)
        self.assertEqual(cloudApiMock.call_args[0][0], fakeAccounts[0])

        self.assertEqual(getFileMock.call_count, 1)
        self.assertEqual(getFileMock.call_args[0][0], testFileData.fullPath)

        self.assertEqual(dispatchResponseMock.call_count, 1)
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].header.messageType,
            MessageTypes.FILE_STATUS_UPDATE)
        self.assertEqual(dispatchResponseMock.call_args[0][0].header.uuid,
                         testTask.uuid)
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["filename"],
                         testFileData.filename)
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["path"],
                         testFileData.path)
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["fullPath"],
                         testFileData.fullPath)
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["modified"],
                         testFileData.modified)
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["size"],
                         testFileData.size)
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["status"],
                         FileStatuses.DOWNLOADING_TO_LOCAL)

        self.assertEqual(os_renameMock.call_count, 1)
        self.assertEqual(os_renameMock.call_args[0][0],
                         f"testWorkspace/server/{testTask.uuid}")
        self.assertEqual(os_renameMock.call_args[0][1],
                         f"testWorkspace/client/{testTask.uuid}")
Exemplo n.º 13
0
    def test_uploads_file_to_single_account_and_cleans_up_server_side_file_and_creates_new_filescache_entry_and_sends_response(
            self, openMock, cloudApiMock, insertFilePartMock, getFileMock,
            os_unlinkMock, dispatchResponseMock):
        fakeFile = BytesIO(b"Lorem ipsum")
        openMock.return_value = fakeFile
        fakeAccounts = [
            AccountData(id=1,
                        identifier="testAccountID",
                        accountType=AccountTypes.Dropbox,
                        cryptoKey="sixteen byte key",
                        data={"apiToken": "testApitoken"})
        ]
        self.fakeDB.getAllAccounts.return_value = fakeAccounts
        getFileMock.return_value = None

        testTaskData = {
            "filename": "apple.txt",
            "size": 11,
            "fullPath": "subDir/apple.txt",
            "status": None,
            "utcModified": 10,
            "path": "subDir"
        }
        fakeUploadResult = FilePart(filename=testTaskData["filename"],
                                    modified=testTaskData["utcModified"],
                                    size=testTaskData["size"],
                                    path=testTaskData["path"],
                                    fullPath=testTaskData["fullPath"],
                                    storingAccountID=fakeAccounts[0].id)
        cloudApiMock.return_value.upload.return_value = fakeUploadResult

        testTask = Task(taskType=MessageTypes.UPLOAD_FILE,
                        data=testTaskData,
                        uuid=uuid4().hex)
        testHandler = UploadFileHandler(self.fakeDB)
        testHandler.setTask(testTask)

        testHandler.handle()

        self.assertEqual(openMock.call_count, 1)
        self.assertEqual(openMock.call_args[0][0],
                         f"testWorkspace/server/{testTask.uuid}")
        self.assertEqual(openMock.call_args[0][1], "rb")

        self.assertEqual(cloudApiMock.call_count, 1)
        self.assertEqual(cloudApiMock.call_args[0][0], fakeAccounts[0])

        self.assertEqual(cloudApiMock.return_value.upload.call_count, 1)
        self.assertEqual(cloudApiMock.return_value.upload.call_args[0][0],
                         fakeFile)
        self.assertEqual(cloudApiMock.return_value.upload.call_args[0][1],
                         testTaskData["size"])
        self.assertEqual(cloudApiMock.return_value.upload.call_args[0][2],
                         f"{testTaskData['filename']}__1__1.enc")

        self.assertEqual(insertFilePartMock.call_count, 1)
        self.assertEqual(insertFilePartMock.call_args[0][0], fakeUploadResult)

        self.assertEqual(getFileMock.call_count, 1)
        self.assertEqual(getFileMock.call_args[0][0], testTaskData["fullPath"])

        self.assertEqual(os_unlinkMock.call_count, 1)
        self.assertEqual(os_unlinkMock.call_args[0][0],
                         f"testWorkspace/server/{testTask.uuid}")

        self.assertEqual(dispatchResponseMock.call_count, 1)
        self.assertEqual(
            dispatchResponseMock.call_args[0][0].header.messageType,
            MessageTypes.FILE_STATUS_UPDATE)

        self.assertEqual(dispatchResponseMock.call_args[0][0].data["filename"],
                         testTaskData["filename"])
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["path"],
                         testTaskData["path"])
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["fullPath"],
                         testTaskData["fullPath"])
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["size"],
                         testTaskData["size"])
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["modified"],
                         testTaskData["utcModified"])
        self.assertEqual(dispatchResponseMock.call_args[0][0].data["status"],
                         FileStatuses.SYNCED)
Exemplo n.º 14
0
 def setUp(self):
     self.testAccountData = AccountData(AccountTypes.Dropbox,
                                        "testIdentifier",
                                        "sixteen byte key",
                                        {"apiToken": "testApiToken"}, 1)