testnetattachjobs.cpp 4.78 KB
Newer Older
1
2
3
4
5
/*
 *  SPDX-FileCopyrightText: 2013 Alejandro Fiestas Olivares <afiestas@kde.org>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */
6
7

#include "../src/daemon/kio/createnetattachjob.h"
Àlex Fiestas's avatar
Àlex Fiestas committed
8
#include "../src/daemon/kio/removenetattachjob.h"
9

Yuri Chornoivan's avatar
Yuri Chornoivan committed
10
#include <QTest>
11

Martin Klapetek's avatar
Martin Klapetek committed
12
#include <KWallet/KWallet>
13
#include <KDirNotify>
Martin Klapetek's avatar
Martin Klapetek committed
14
#include <KConfig>
15
#include <KConfigGroup>
Martin Klapetek's avatar
Martin Klapetek committed
16

17
18
#include <QDBusConnection>
#include <QDBusAbstractAdaptor>
Martin Klapetek's avatar
Martin Klapetek committed
19
20
21
#include <QStandardPaths>
#include <QTimer>
#include <QSignalSpy>
22

23
using namespace KWallet;
24
class testNetAttachJobs : public QObject
25
26
27
{
    Q_OBJECT

28
    public:
29
        explicit testNetAttachJobs(QObject* parent = 0);
30
31
    private Q_SLOTS:
        void testCreate();
Àlex Fiestas's avatar
Àlex Fiestas committed
32
        void testRemove();
33
34
35
36
37
    private:
        void enterLoop();

        QTimer m_timer;
        QEventLoop m_eventLoop;
38
39
};

40
testNetAttachJobs::testNetAttachJobs(QObject* parent): QObject(parent)
41
42
43
44
{
    m_timer.setSingleShot(true);
    m_timer.setInterval(10000);// 10 seconds timeout for eventloop

Nicolas Fella's avatar
Nicolas Fella committed
45
    connect(&m_timer, &QTimer::timeout, &m_eventLoop, &QEventLoop::quit);
46
}
47

48
void testNetAttachJobs::testCreate()
49
{
Martin Klapetek's avatar
Martin Klapetek committed
50
51
52
53
    QString destPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
    destPath.append("/remoteview/test-unique-id.desktop");

    qDebug() << destPath;
54

55
56
    org::kde::KDirNotify *watch = new org::kde::KDirNotify(
    QDBusConnection::sessionBus().baseService(), QString(), QDBusConnection::sessionBus());
Nicolas Fella's avatar
Nicolas Fella committed
57
    connect(watch, &org::kde::KDirNotify::FilesAdded, &m_eventLoop, &QEventLoop::quit);
58

Nicolas Fella's avatar
Nicolas Fella committed
59
    QSignalSpy signalSpy(watch, &org::kde::KDirNotify::FilesAdded);
60

Nicolas Fella's avatar
Nicolas Fella committed
61
    CreateNetAttachJob *job = new CreateNetAttachJob(this)
62
63
64
65
    job->setHost("host.com");
    job->setUsername("username");
    job->setPassword("password");
    job->setIcon("modem");
66
    job->setUniqueId("test-unique-id");
67
68
    job->setPath("files/webdav.php/");
    job->setName("test-service");
69
    job->setRealm("testRealm");
70
    job->exec();
71

72
73
74
    enterLoop();

    QCOMPARE(signalSpy.count(), 1);
75
    QCOMPARE(signalSpy.first().first().toString(), QLatin1String("remote:/"));
76
77
78
79
    Wallet *wallet = Wallet::openWallet(Wallet::NetworkWallet(), 0, Wallet::Synchronous);
    wallet->setFolder("Passwords");

    QVERIFY2(QFile::exists(destPath), "Desktop file has not been created");
Àlex Fiestas's avatar
Àlex Fiestas committed
80
81
    QVERIFY2(wallet->hasEntry("webdav-username@host.com:-1-testRealm"), "Wallet realm entry does not exists");
    QVERIFY2(wallet->hasEntry("webdav-username@host.com:-1-webdav"), "Wallet schema entry does not exists");
82

83
84
85
86
87
88
89
90
    KConfig _desktopFile(destPath, KConfig::SimpleConfig );
    KConfigGroup desktopFile(&_desktopFile, "Desktop Entry");
    QCOMPARE(desktopFile.readEntry("Icon", ""), QLatin1String("modem"));
    QCOMPARE(desktopFile.readEntry("Name", ""), QLatin1String("test-service"));
    QCOMPARE(desktopFile.readEntry("Type", ""), QLatin1String("Link"));
    QCOMPARE(desktopFile.readEntry("URL", ""), QLatin1String("webdav://username@host.com/files/webdav.php/"));
    QCOMPARE(desktopFile.readEntry("Charset", ""), QLatin1String(""));

91
    QMap<QString, QString> data;
92
    int result = wallet->readMap("webdav-username@host.com:-1-testRealm", data);
93
    QCOMPARE(result, 0);
94
95
    result = wallet->readMap("webdav-username@host.com:-1-webdav", data);
    QCOMPARE(result, 0);
96
97
98
99
100

    QVERIFY2(data.keys().contains("login"), "Login data is not stored in the wallet");
    QVERIFY2(data.keys().contains("password"), "Password data is not stored in the wallet");
    QCOMPARE(data["login"], QLatin1String("username"));
    QCOMPARE(data["password"], QLatin1String("password"));
101

Àlex Fiestas's avatar
Àlex Fiestas committed
102
103
}

104
void testNetAttachJobs::testRemove()
Àlex Fiestas's avatar
Àlex Fiestas committed
105
{
106
    QString destPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
Àlex Fiestas's avatar
Àlex Fiestas committed
107
108
    destPath.append("test-unique-id.desktop");

109
110
    org::kde::KDirNotify *watch = new org::kde::KDirNotify(
    QDBusConnection::sessionBus().baseService(), QString(), QDBusConnection::sessionBus());
Nicolas Fella's avatar
Nicolas Fella committed
111
    connect(watch, &org::kde::KDirNotify::FilesRemoved, &m_eventLoop, &QEventLoop::quit);
112

Nicolas Fella's avatar
Nicolas Fella committed
113
    QSignalSpy signalSpy(watch, &org::kde::KDirNotify::FilesRemoved);
114

Àlex Fiestas's avatar
Àlex Fiestas committed
115
116
117
118
    RemoveNetAttachJob *job = new RemoveNetAttachJob(this);
    job->setUniqueId("test-unique-id");
    job->exec();

119
120
    enterLoop();
    QCOMPARE(signalSpy.count(), 1);
121
    QCOMPARE(signalSpy.first().first().toStringList().first(), QLatin1String("remote:/test-unique-id"));
Àlex Fiestas's avatar
Àlex Fiestas committed
122
123
    Wallet *wallet = Wallet::openWallet(Wallet::NetworkWallet(), 0, Wallet::Synchronous);
    wallet->setFolder("Passwords");
124

Àlex Fiestas's avatar
Àlex Fiestas committed
125
126
127
    QVERIFY2(!QFile::exists(destPath), "Desktop file has not been removed");
    QVERIFY2(!wallet->hasEntry("webdav-username@host.com:-1-testRealm"), "Wallet realm entry still exists");
    QVERIFY2(!wallet->hasEntry("webdav-username@host.com:-1-webdav"), "Wallet schema entry still exists");
128
129
}

130
void testNetAttachJobs::enterLoop()
131
132
133
134
135
{
    m_timer.start();
    m_eventLoop.exec();
}

Martin Klapetek's avatar
Martin Klapetek committed
136
QTEST_MAIN(testNetAttachJobs)
137

138
#include "testnetattachjobs.moc"