From 955f4de7d23386bd0b3ebe91d0311af90deceb4e Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Mon, 24 Apr 2023 15:49:33 +0800 Subject: [PATCH] Add initial testing setup for file tag model Signed-off-by: Claudio Cambra --- test/CMakeLists.txt | 1 + test/testfiletagmodel.cpp | 109 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 test/testfiletagmodel.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f6d8f57d9..d6c27a130 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -70,6 +70,7 @@ nextcloud_add_test(ShareModel) nextcloud_add_test(ShareeModel) nextcloud_add_test(SortedShareModel) nextcloud_add_test(SecureFileDrop) +nextcloud_add_test(FileTagModel) target_link_libraries(SecureFileDropTest PRIVATE Nextcloud::sync) configure_file(fake2eelocksucceeded.json "${PROJECT_BINARY_DIR}/bin/fake2eelocksucceeded.json" COPYONLY) diff --git a/test/testfiletagmodel.cpp b/test/testfiletagmodel.cpp new file mode 100644 index 000000000..86028de3d --- /dev/null +++ b/test/testfiletagmodel.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (C) by Claudio Cambra + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "gui/filetagmodel.h" + +#include +#include +#include + +#include "accountmanager.h" +#include "syncenginetestutils.h" + +using namespace OCC; + +namespace { +const auto testTaglessXmlResponse = QByteArray("\n\n \n /remote.php/dav/files/tag/Documents/\n \n \n \n \n \n HTTP/1.1 404 Not Found\n \n \n\n"); +const auto testSystemAndNormalTagsOnlyXmlResponse = QByteArray("\n\n \n /remote.php/dav/files/tag/Documents/\n \n \n \n test 0\n test 1\n test 2\n test 3\n \n \n important\n marino\n marino2\n one\n two\n \n \n HTTP/1.1 200 OK\n \n \n\n"); +const auto testTagsOnlyXmlResponse = QByteArray("\n\n \n /remote.php/dav/files/tag/Documents/\n \n \n \n test 0\n test 1\n test 2\n test 3\n \n \n HTTP/1.1 200 OK\n \n \n\n"); +const auto testSystemTagsOnlyXmlResponse = QByteArray("\n\n \n /remote.php/dav/files/tag/Documents/\n \n \n \n important\n marino\n marino2\n one\n two\n \n \n HTTP/1.1 200 OK\n \n \n\n"); +const auto testErrorXmlResponse = QByteArray("\n\n \n /remote.php/dav/files/tag/\n \n \n Wed, 19 Apr 2023 15:09:16 GMT\n \n \n \n 25789373\n -3\n \"083237f7b434afbedeace283d655cc41\"\n \n HTTP/1.1 200 OK\n \n \n\n"); +const QString testUser = "admin"; +const QString testFilePath = "Documents"; +const QString testUrlPath = "/remote.php/dav/files/" + testUser + '/' + testFilePath; +constexpr auto testNumTags = 9; + +} + +class TestFileTagModel : public QObject +{ + Q_OBJECT + +private: + AccountPtr _account; + AccountStatePtr _accountState; + QScopedPointer _fakeQnam; + QStringList _expectedTags; + +private slots: + void initTestCase() + { + _fakeQnam.reset(new FakeQNAM({})); + _fakeQnam->setOverride([this](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice * const device) { + Q_UNUSED(device); + QNetworkReply *reply = nullptr; + + const auto urlQuery = QUrlQuery(req.url()); + const auto path = req.url().path(); + + auto requestDom = QDomDocument(); + const auto parsedCorrectly = requestDom.setContent(device); + const auto tagElems = requestDom.elementsByTagName("tags"); + const auto systemTagElems = requestDom.elementsByTagName("system-tags"); + + if (!parsedCorrectly || !req.url().toString().startsWith(_accountState->account()->url().toString())) { + reply = new FakePayloadReply(op, req, testErrorXmlResponse, qnamDelay, _fakeQnam.data()); + } + + if (path.startsWith(testUrlPath)) { + if (tagElems.count() > 0 && systemTagElems.count() > 0) { + reply = new FakePayloadReply(op, req, testSystemAndNormalTagsOnlyXmlResponse, qnamDelay, _fakeQnam.data()); + } else if (tagElems.count() > 0) { + reply = new FakePayloadReply(op, req, testTagsOnlyXmlResponse, qnamDelay, _fakeQnam.data()); + } else if (systemTagElems.count() > 0) { + reply = new FakePayloadReply(op, req, testSystemTagsOnlyXmlResponse, qnamDelay, _fakeQnam.data()); + } else { + reply = new FakePayloadReply(op, req, testTaglessXmlResponse, qnamDelay, _fakeQnam.data()); + } + } + + if (!reply) { + reply = new FakePayloadReply(op, req, testErrorXmlResponse, qnamDelay, _fakeQnam.data()); + } + + return reply; + }); + + _account = Account::create(); + _account->setCredentials(new FakeCredentials{_fakeQnam.data()}); + _account->setUrl(QUrl(("owncloud://somehost/owncloud"))); + _accountState = new AccountState(_account); + AccountManager::instance()->addAccount(_account); + + _expectedTags = QStringList{ + "test 0", + "test 1", + "test 2", + "test 3", + "important", + "marino", + "marino2", + "one", + "two" + }; + } +}; + +QTEST_MAIN(TestFileTagModel) +#include "testfiletagmodel.moc" -- 2.39.5