diff --git a/changelog/unreleased/12770.md b/changelog/unreleased/12770.md new file mode 100644 index 00000000000..6209dea64be --- /dev/null +++ b/changelog/unreleased/12770.md @@ -0,0 +1,8 @@ +Bugfix: Sync folders whose space is loaded after the account connected + +When the client started and connected to the server before the list of +spaces had arrived, folders restored from the configuration were never +scheduled. Changes made while the client was not running were only synced +once something changed on the server or on disk. + +https://github.com/owncloud/client/pull/12770 diff --git a/src/gui/folderman.cpp b/src/gui/folderman.cpp index 88db4858da0..d4fa9831208 100644 --- a/src/gui/folderman.cpp +++ b/src/gui/folderman.cpp @@ -442,7 +442,13 @@ void FolderMan::onSpacesAdded(const QUuid &accountId, QList s newUnsyncedSpaces.remove(space); else if (_folders.contains(accountId) && _folders[accountId].contains(space->id())) { newUnsyncedSpaces.remove(space); - _folders[accountId][space->id()]->setAvailable(true); + Folder *folder = _folders[accountId][space->id()]; + const bool wasAvailable = folder->isAvailable(); + folder->setAvailable(true); + // a folder restored from the config was skipped when the account connected because its space was not known yet + if (!wasAvailable && folder->canSync()) { + _scheduler->enqueueFolder(folder); + } } } diff --git a/test/testfolderman.cpp b/test/testfolderman.cpp index 0576f49cd65..c0c625434bf 100644 --- a/test/testfolderman.cpp +++ b/test/testfolderman.cpp @@ -9,9 +9,12 @@ #include #include +#include "accountmanager.h" +#include "accountstate.h" #include "common/utility.h" +#include "connectionvalidator.h" #include "folderman.h" -#include "accountstate.h" +#include "graphapi/spacesmanager.h" #include "guiutility.h" #include "testutils/testutils.h" @@ -285,6 +288,83 @@ private Q_SLOTS: QVERIFY(folderman->checkPathValidity(dirPath + QStringLiteral("/OurSpacesSyncRoot/OurSpacesFolder"), FolderMan::NewFolderType::SpacesFolder, ourUuid) .isNull()); } + + /* + * A folder restored from the config is unavailable until the spaces manager knows its space. If the account connects before the + * drives reply arrives, scheduling on connect skips the folder, so it has to be scheduled once its space shows up. + */ + void testFolderIsScheduledWhenItsSpaceArrivesAfterConnect() + { + auto dir = TestUtils::createTempDir(); + QVERIFY(dir.isValid()); + QDir localDir(dir.path()); + // created while the client was not running, so the folder watcher never reports it + QVERIFY(localDir.mkpath(QStringLiteral("Folder1"))); + + FolderMan *folderman = TestUtils::folderMan(); + folderman->setSyncEnabled(true); + + auto *fakeAm = new FakeAM(FileInfo{}, nullptr); + auto *acc = new Account(QUuid::createUuid(), QStringLiteral("admin"), QUrl(QStringLiteral("http://localhost/owncloud"))); + acc->setCredentials(new FakeCredentials(acc, fakeAm, acc)); + acc->setDavDisplayName(QStringLiteral("fakename") + acc->uuid().toString(QUuid::WithoutBraces)); + acc->setCapabilities({acc->url(), TestUtils::testCapabilities()}); + + const QString spaceId = QUuid::createUuid().toString(QUuid::WithoutBraces); + // the definition drops the trailing slash of the dav url; Folder::webDavUrl() asserts that the space url matches it + FolderDefinition definition(acc->davUrl(), spaceId, QStringLiteral("Personal")); + definition.setLocalPath(localDir.canonicalPath()); + definition.setJournalPath(SyncJournalDb::makeDbName(definition.localPath())); + const QJsonObject drive{{QStringLiteral("id"), spaceId}, {QStringLiteral("driveType"), QStringLiteral("personal")}, + {QStringLiteral("name"), QStringLiteral("Personal")}, + {QStringLiteral("root"), + QJsonObject{{QStringLiteral("id"), spaceId}, {QStringLiteral("eTag"), QStringLiteral("1")}, + {QStringLiteral("webDavUrl"), definition.webDavUrl().toString()}}}}; + const QByteArray drivesReply = QJsonDocument(QJsonObject{{QStringLiteral("value"), QJsonArray{drive}}}).toJson(); + // hold the drives back until the account is connected, like the delayed reply in the GUI test run + auto serveDrives = std::make_shared(false); + fakeAm->setOverride( + [serveDrives, drivesReply, fakeAm](QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *) -> QNetworkReply * { + if (request.url().path().endsWith(QLatin1String("/graph/v1.0/me/drives"))) { + if (!*serveDrives) { + return new FakeErrorReply(op, request, fakeAm, 503); + } + return new FakePayloadReply(op, request, drivesReply, fakeAm); + } + return nullptr; + }); + + TestUtils::TestUtilsPrivate::AccountStateRaii accountState( + AccountManager::instance()->addAccount(acc), &TestUtils::TestUtilsPrivate::accountStateDeleter); + + Folder *folder = folderman->addFolder(accountState.get(), definition); + QVERIFY(folder); + // what addFoldersFromConfigByAccount does while the spaces manager is still empty + folder->setAvailable(folder->space() != nullptr); + QVERIFY(!folder->isAvailable()); + folderman->setUpInitialSpaces(accountState.get()); + + QSignalSpy syncStarted(folder, &Folder::syncStarted); + QSignalSpy syncFinished(folder, &Folder::syncFinished); + QTRY_VERIFY(folder->isReady()); + // FakeCredentials never fetch, so report the connection validator result directly + QVERIFY(QMetaObject::invokeMethod(accountState.get(), "slotConnectionValidatorResult", Qt::DirectConnection, + Q_ARG(OCC::ConnectionValidator::Status, ConnectionValidator::Connected), Q_ARG(QStringList, {}))); + QVERIFY(accountState->isConnected()); + QVERIFY(!folder->canSync()); + QCOMPARE(syncStarted.count(), 0); + + *serveDrives = true; + // SpacesManager::refresh() is private; its refresh timer is what fetches the drives again + auto *refreshTimer = acc->spacesManager()->findChild(); + QVERIFY(refreshTimer); + refreshTimer->start(0); + QTRY_VERIFY(folder->isAvailable()); + refreshTimer->stop(); + + QTRY_VERIFY(!syncFinished.isEmpty()); + QVERIFY(fakeAm->currentRemoteState().find(QStringLiteral("Folder1"))); + } }; QTEST_GUILESS_MAIN(TestFolderMan)