From 2ee3ca02a48765305b49756ee4232c6a4dbca8db Mon Sep 17 00:00:00 2001 From: Felix Weilbach Date: Fri, 18 Jun 2021 14:26:50 +0200 Subject: [PATCH] Add unit test for move custom remote root Signed-off-by: Felix Weilbach --- src/libsync/discovery.cpp | 2 +- test/syncenginetestutils.cpp | 19 +++++++++++++------ test/syncenginetestutils.h | 3 ++- test/testsyncmove.cpp | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/libsync/discovery.cpp b/src/libsync/discovery.cpp index cb9fb7ab38..65a3d18eb1 100644 --- a/src/libsync/discovery.cpp +++ b/src/libsync/discovery.cpp @@ -1083,7 +1083,7 @@ void ProcessDirectoryJob::processFileAnalyzeLocalInfo( } else { // We must query the server to know if the etag has not changed _pendingAsyncJobs++; - QString serverOriginalPath = _discoveryData->_remoteFolder +_discoveryData->adjustRenamedPath(originalPath, SyncFileItem::Down); + QString serverOriginalPath = _discoveryData->_remoteFolder + _discoveryData->adjustRenamedPath(originalPath, SyncFileItem::Down); if (base.isVirtualFile() && isVfsWithSuffix()) chopVirtualFileSuffix(serverOriginalPath); auto job = new RequestEtagJob(_discoveryData->_account, serverOriginalPath, this); diff --git a/test/syncenginetestutils.cpp b/test/syncenginetestutils.cpp index 16d385299a..d8cee44e67 100644 --- a/test/syncenginetestutils.cpp +++ b/test/syncenginetestutils.cpp @@ -288,7 +288,11 @@ FakePropfindReply::FakePropfindReply(FileInfo &remoteRootFileInfo, QNetworkAcces auto writeFileResponse = [&](const FileInfo &fileInfo) { xml.writeStartElement(davUri, QStringLiteral("response")); - xml.writeTextElement(davUri, QStringLiteral("href"), prefix + QString::fromUtf8(QUrl::toPercentEncoding(fileInfo.path(), "/"))); + QString url = prefix + QString::fromUtf8(QUrl::toPercentEncoding(fileInfo.path(), "/")); + if (!url.endsWith(QChar('/'))) { + url.append(QChar('/')); + } + xml.writeTextElement(davUri, QStringLiteral("href"), url); xml.writeStartElement(davUri, QStringLiteral("propstat")); xml.writeStartElement(davUri, QStringLiteral("prop")); @@ -832,7 +836,7 @@ QNetworkReply *FakeQNAM::createRequest(QNetworkAccessManager::Operation op, cons return reply; } -FakeFolder::FakeFolder(const FileInfo &fileTemplate) +FakeFolder::FakeFolder(const FileInfo &fileTemplate, const OCC::Optional &localFileInfo, const QString &remotePath) : _localModifier(_tempDir.path()) { // Needs to be done once @@ -841,7 +845,11 @@ FakeFolder::FakeFolder(const FileInfo &fileTemplate) QDir rootDir { _tempDir.path() }; qDebug() << "FakeFolder operating on" << rootDir; - toDisk(rootDir, fileTemplate); + if (localFileInfo) { + toDisk(rootDir, *localFileInfo); + } else { + toDisk(rootDir, fileTemplate); + } _fakeQnam = new FakeQNAM(fileTemplate); _account = OCC::Account::create(); @@ -851,7 +859,7 @@ FakeFolder::FakeFolder(const FileInfo &fileTemplate) _account->setServerVersion(QStringLiteral("10.0.0")); _journalDb = std::make_unique(localPath() + QStringLiteral(".sync_test.db")); - _syncEngine = std::make_unique(_account, localPath(), QString(), _journalDb.get()); + _syncEngine = std::make_unique(_account, localPath(), remotePath, _journalDb.get()); // Ignore temporary files from the download. (This is in the default exclude list, but we don't load it) _syncEngine->excludedFiles().addManualExclude(QStringLiteral("]*.~*")); @@ -1034,5 +1042,4 @@ FakeReply::FakeReply(QObject *parent) setRawHeader(QByteArrayLiteral("Date"), QDateTime::currentDateTimeUtc().toString(Qt::RFC2822Date).toUtf8()); } -FakeReply::~FakeReply() -= default; +FakeReply::~FakeReply() = default; diff --git a/test/syncenginetestutils.h b/test/syncenginetestutils.h index ea99d359a8..3b0e36e2a4 100644 --- a/test/syncenginetestutils.h +++ b/test/syncenginetestutils.h @@ -7,6 +7,7 @@ #pragma once #include "account.h" +#include "common/result.h" #include "creds/abstractcredentials.h" #include "logger.h" #include "filesystem.h" @@ -435,7 +436,7 @@ class FakeFolder std::unique_ptr _syncEngine; public: - FakeFolder(const FileInfo &fileTemplate); + FakeFolder(const FileInfo &fileTemplate, const OCC::Optional &localFileInfo = {}, const QString &remotePath = {}); void switchToVfs(QSharedPointer vfs); diff --git a/test/testsyncmove.cpp b/test/testsyncmove.cpp index 8aca619009..f73c84838f 100644 --- a/test/testsyncmove.cpp +++ b/test/testsyncmove.cpp @@ -6,6 +6,7 @@ */ #include +#include "common/result.h" #include "syncenginetestutils.h" #include @@ -83,6 +84,37 @@ class TestSyncMove : public QObject Q_OBJECT private slots: + void testMoveCustomRemoteRoot() + { + FileInfo subFolder(QStringLiteral("AS"), { { QStringLiteral("f1"), 4 } }); + FileInfo folder(QStringLiteral("A"), { subFolder }); + FileInfo fileInfo({}, { folder }); + + FakeFolder fakeFolder(fileInfo, folder, QStringLiteral("/A")); + auto &localModifier = fakeFolder.localModifier(); + + OperationCounter counter; + fakeFolder.setServerOverride(counter.functor()); + + // Move file and then move it back again + { + counter.reset(); + localModifier.rename(QStringLiteral("AS/f1"), QStringLiteral("f1")); + + ItemCompletedSpy completeSpy(fakeFolder); + QVERIFY(fakeFolder.syncOnce()); + + QCOMPARE(counter.nGET, 0); + QCOMPARE(counter.nPUT, 0); + QCOMPARE(counter.nMOVE, 1); + QCOMPARE(counter.nDELETE, 0); + + QVERIFY(itemSuccessful(completeSpy, "f1", CSYNC_INSTRUCTION_RENAME)); + QVERIFY(fakeFolder.currentRemoteState().find("A/f1")); + QVERIFY(!fakeFolder.currentRemoteState().find("A/AS/f1")); + } + } + void testRemoteChangeInMovedFolder() { // issue #5192