File indexing completed on 2024-05-12 04:58:42

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Alejandro Fiestas Olivares <afiestas@kde.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "../src/daemon/kio/createnetattachjob.h"
0008 #include "../src/daemon/kio/removenetattachjob.h"
0009 
0010 #include <QTest>
0011 
0012 #include <KConfig>
0013 #include <KConfigGroup>
0014 #include <KDirNotify>
0015 #include <KWallet>
0016 
0017 #include <QDBusAbstractAdaptor>
0018 #include <QDBusConnection>
0019 #include <QSignalSpy>
0020 #include <QStandardPaths>
0021 #include <QTimer>
0022 
0023 using namespace KWallet;
0024 class testNetAttachJobs : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     explicit testNetAttachJobs(QObject *parent = 0);
0030 private Q_SLOTS:
0031     void testCreate();
0032     void testRemove();
0033 
0034 private:
0035     void enterLoop();
0036 
0037     QTimer m_timer;
0038     QEventLoop m_eventLoop;
0039 };
0040 
0041 testNetAttachJobs::testNetAttachJobs(QObject *parent)
0042     : QObject(parent)
0043 {
0044     m_timer.setSingleShot(true);
0045     m_timer.setInterval(10000); // 10 seconds timeout for eventloop
0046 
0047     connect(&m_timer, &QTimer::timeout, &m_eventLoop, &QEventLoop::quit);
0048 }
0049 
0050 void testNetAttachJobs::testCreate()
0051 {
0052     QString destPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0053     destPath.append("/remoteview/test-unique-id.desktop");
0054 
0055     qDebug() << destPath;
0056 
0057     org::kde::KDirNotify *watch = new org::kde::KDirNotify(QDBusConnection::sessionBus().baseService(), QString(), QDBusConnection::sessionBus());
0058     connect(watch, &org::kde::KDirNotify::FilesAdded, &m_eventLoop, &QEventLoop::quit);
0059 
0060     QSignalSpy signalSpy(watch, &org::kde::KDirNotify::FilesAdded);
0061 
0062     CreateNetAttachJob *job = new CreateNetAttachJob(this) job->setHost("host.com");
0063     job->setUsername("username");
0064     job->setPassword("password");
0065     job->setUniqueId("test-unique-id");
0066     job->setPath("files/webdav.php/");
0067     job->setName("test-service");
0068     job->setRealm("testRealm");
0069     job->exec();
0070 
0071     enterLoop();
0072 
0073     QCOMPARE(signalSpy.count(), 1);
0074     QCOMPARE(signalSpy.first().first().toString(), QLatin1String("remote:/"));
0075     Wallet *wallet = Wallet::openWallet(Wallet::NetworkWallet(), 0, Wallet::Synchronous);
0076     wallet->setFolder("Passwords");
0077 
0078     QVERIFY2(QFile::exists(destPath), "Desktop file has not been created");
0079     QVERIFY2(wallet->hasEntry("webdav-username@host.com:-1-testRealm"), "Wallet realm entry does not exists");
0080     QVERIFY2(wallet->hasEntry("webdav-username@host.com:-1-webdav"), "Wallet schema entry does not exists");
0081 
0082     KConfig _desktopFile(destPath, KConfig::SimpleConfig);
0083     KConfigGroup desktopFile(&_desktopFile, "Desktop Entry");
0084     QCOMPARE(desktopFile.readEntry("Icon", ""), QLatin1String("modem"));
0085     QCOMPARE(desktopFile.readEntry("Name", ""), QLatin1String("test-service"));
0086     QCOMPARE(desktopFile.readEntry("Type", ""), QLatin1String("Link"));
0087     QCOMPARE(desktopFile.readEntry("URL", ""), QLatin1String("webdav://username@host.com/files/webdav.php/"));
0088     QCOMPARE(desktopFile.readEntry("Charset", ""), QLatin1String(""));
0089 
0090     QMap<QString, QString> data;
0091     int result = wallet->readMap("webdav-username@host.com:-1-testRealm", data);
0092     QCOMPARE(result, 0);
0093     result = wallet->readMap("webdav-username@host.com:-1-webdav", data);
0094     QCOMPARE(result, 0);
0095 
0096     QVERIFY2(data.keys().contains("login"), "Login data is not stored in the wallet");
0097     QVERIFY2(data.keys().contains("password"), "Password data is not stored in the wallet");
0098     QCOMPARE(data["login"], QLatin1String("username"));
0099     QCOMPARE(data["password"], QLatin1String("password"));
0100 }
0101 
0102 void testNetAttachJobs::testRemove()
0103 {
0104     QString destPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
0105     destPath.append("test-unique-id.desktop");
0106 
0107     org::kde::KDirNotify *watch = new org::kde::KDirNotify(QDBusConnection::sessionBus().baseService(), QString(), QDBusConnection::sessionBus());
0108     connect(watch, &org::kde::KDirNotify::FilesRemoved, &m_eventLoop, &QEventLoop::quit);
0109 
0110     QSignalSpy signalSpy(watch, &org::kde::KDirNotify::FilesRemoved);
0111 
0112     RemoveNetAttachJob *job = new RemoveNetAttachJob(this);
0113     job->setUniqueId("test-unique-id");
0114     job->exec();
0115 
0116     enterLoop();
0117     QCOMPARE(signalSpy.count(), 1);
0118     QCOMPARE(signalSpy.first().first().toStringList().first(), QLatin1String("remote:/test-unique-id"));
0119     Wallet *wallet = Wallet::openWallet(Wallet::NetworkWallet(), 0, Wallet::Synchronous);
0120     wallet->setFolder("Passwords");
0121 
0122     QVERIFY2(!QFile::exists(destPath), "Desktop file has not been removed");
0123     QVERIFY2(!wallet->hasEntry("webdav-username@host.com:-1-testRealm"), "Wallet realm entry still exists");
0124     QVERIFY2(!wallet->hasEntry("webdav-username@host.com:-1-webdav"), "Wallet schema entry still exists");
0125 }
0126 
0127 void testNetAttachJobs::enterLoop()
0128 {
0129     m_timer.start();
0130     m_eventLoop.exec();
0131 }
0132 
0133 QTEST_MAIN(testNetAttachJobs)
0134 
0135 #include "testnetattachjobs.moc"