Commit e8a66e1e authored by David Faure's avatar David Faure
Browse files

Add unittest for DavCollectionsMultiFetchJob

Summary:
This also tests the XQuery and the expected XML response from the
server.

Test Plan: Passes (finally)

Reviewers: vkrause, mlaurent, dvratil, ervin

Reviewed By: vkrause

Subscribers: kde-pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D28568
parent 7c8355c8
......@@ -19,6 +19,12 @@ ecm_add_test(davurltest.cpp
LINK_LIBRARIES KF5::DAV Qt5::Test Qt5::Core
)
ecm_add_test(davcollectionsmultifetchjobtest.cpp fakeserver.cpp
TEST_NAME davcollectionsmultifetchjobtest
NAME_PREFIX "kdav-"
LINK_LIBRARIES KF5::DAV Qt5::Test Qt5::Core Qt5::Network
)
ecm_add_test(davitemfetchjobtest.cpp fakeserver.cpp
TEST_NAME davitemfetchjob
NAME_PREFIX "kdav-"
......
C: PROPFIND /caldav/dfaure%40example.com/ HTTP/1.1
S: HTTP/1.1 207 Multi-Status
S: Date: Wed, 04 Jan 2017 18:26:48 GMT
S: Last-Modified: Wed, 04 Jan 2017 18:26:47 GMT
S: DAV: 1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, calendar-access, calendar-proxy, calendar-schedule, calendar-auto-schedule, addressbook, 2
S: Content-Type: application/xml; charset=utf-8
D: <?xml version="1.0" encoding="utf-8" ?>
D: <multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
D: <response xmlns="DAV:">
D: <href xmlns="DAV:">/caldav.php/test1.user/home/</href>
D: <propstat xmlns="DAV:">
D: <prop xmlns="DAV:">
D: <C:supported-calendar-component-set xmlns:C="urn:ietf:params:xml:ns:caldav">
D: <C:comp xmlns:C="urn:ietf:params:xml:ns:caldav" name="VEVENT"/>
D: <C:comp xmlns:C="urn:ietf:params:xml:ns:caldav" name="VTODO"/>
D: <C:comp xmlns:C="urn:ietf:params:xml:ns:caldav" name="VJOURNAL"/>
D: <C:comp xmlns:C="urn:ietf:params:xml:ns:caldav" name="VTIMEZONE"/>
D: <C:comp xmlns:C="urn:ietf:params:xml:ns:caldav" name="VFREEBUSY"/>
D: </C:supported-calendar-component-set>
D: <resourcetype xmlns="DAV:">
D: <collection xmlns="DAV:"/>
D: <C:calendar xmlns:C="urn:ietf:params:xml:ns:caldav"/>
D: <C:schedule-calendar xmlns:C="urn:ietf:params:xml:ns:caldav"/>
D: </resourcetype>
D: <displayname xmlns="DAV:">Test1 User</displayname>
D: <current-user-privilege-set xmlns="DAV:">
D: <privilege xmlns="DAV:">
D: <read xmlns="DAV:"/>
D: </privilege>
D: </current-user-privilege-set>
D: <getctag xmlns="http://calendarserver.org/ns/">12345</getctag>
D: </prop>
D: <status xmlns="DAV:">HTTP/1.1 200 OK</status>
D: </propstat>
D: </response>
D: </multistatus>
X
C: PROPFIND /caldav HTTP/1.1
S: HTTP/1.1 207 Multi-Status
S: Date: Wed, 04 Jan 2017 18:26:48 GMT
S: Last-Modified: Wed, 04 Jan 2017 18:26:47 GMT
S: DAV: 1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, calendar-access, calendar-proxy, calendar-schedule, calendar-auto-schedule, addressbook, 2
S: Content-Type: application/xml; charset=utf-8
D: <?xml version="1.0" encoding="utf-8" ?>
D: <multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
D: <response>
D: <href>/principals/users/dfaure%40example.com/</href>
D: <propstat>
D: <prop>
D: <C:calendar-home-set>
D: <href>/caldav/dfaure%40example.com/</href>
D: </C:calendar-home-set>
D: </prop>
D: <status>HTTP/1.1 200 OK</status>
D: </propstat>
D: </response>
D: </multistatus>
X
C: PROPFIND /carddav/dfaure%40example.com/ HTTP/1.1
S: HTTP/1.1 207 Multi-Status
S: Date: Wed, 04 Jan 2017 18:26:48 GMT
S: Last-Modified: Wed, 04 Jan 2017 18:26:47 GMT
S: DAV: 1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, calendar-access, calendar-proxy, calendar-schedule, calendar-auto-schedule, addressbook, 2
S: Content-Type: application/xml; charset=utf-8
D: <?xml version="1.0" encoding="utf-8" ?>
D: <multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav">
D: <response xmlns="DAV:">
D: <href xmlns="DAV:">/carddav.php/test1.user/home/</href>
D: <propstat xmlns="DAV:">
D: <prop xmlns="DAV:">
D: <resourcetype xmlns="DAV:">
D: <collection xmlns="DAV:"/>
D: <C:addressbook xmlns:C="urn:ietf:params:xml:ns:carddav"/>
D: </resourcetype>
D: <displayname xmlns="DAV:">My Address Book</displayname>
D: <getctag xmlns="http://calendarserver.org/ns/">3145</getctag>
D: </prop>
D: <status xmlns="DAV:">HTTP/1.1 200 OK</status>
D: </propstat>
D: </response>
D: </multistatus>
X
C: PROPFIND /carddav HTTP/1.1
S: HTTP/1.1 207 Multi-Status
S: Date: Wed, 04 Jan 2017 18:26:48 GMT
S: Last-Modified: Wed, 04 Jan 2017 18:26:47 GMT
S: DAV: 1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, calendar-access, calendar-proxy, calendar-schedule, calendar-auto-schedule, addressbook, 2
S: Content-Type: application/xml; charset=utf-8
D: <?xml version="1.0" encoding="utf-8" ?>
D: <multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav">
D: <response>
D: <href>/principals/users/dfaure%40example.com/</href>
D: <propstat>
D: <prop>
D: <C:addressbook-home-set>
D: <href>/carddav/dfaure%40example.com/</href>
D: </C:addressbook-home-set>
D: </prop>
D: <status>HTTP/1.1 200 OK</status>
D: </propstat>
D: </response>
D: </multistatus>
X
/*
SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "davcollectionsmultifetchjobtest.h"
#include "fakeserver.h"
#include <KDAV/DavCollectionsMultiFetchJob>
#include <QTest>
using KDAV::DavCollection;
void DavCollectionsMultiFetchJobTest::initTestCase()
{
// To avoid a runtime dependency on klauncher
qputenv("KDE_FORK_SLAVES", "yes");
// To let ctest exit, we shouldn't start kio_http_cache_cleaner
qputenv("KIO_DISABLE_CACHE_CLEANER", "yes");
}
void DavCollectionsMultiFetchJobTest::runSuccessfullTest()
{
FakeServer fakeServer;
QUrl url(QStringLiteral("http://localhost/caldav"));
url.setPort(fakeServer.port());
KDAV::DavUrl davUrl1(url, KDAV::CalDav);
QUrl url2(url);
url2.setPath(QStringLiteral("/carddav"));
KDAV::DavUrl davUrl2(url2, KDAV::CardDav);
auto job = new KDAV::DavCollectionsMultiFetchJob({davUrl1, davUrl2});
// TODO QSignalSpy spy(job, &KDAV::DavCollectionsMultiFetchJob::collectionDiscovered);
fakeServer.addScenarioFromFile(QLatin1String(AUTOTEST_DATA_DIR)+QStringLiteral("/dataitemmultifetchjob-caldav.txt"));
fakeServer.addScenarioFromFile(QLatin1String(AUTOTEST_DATA_DIR)+QStringLiteral("/dataitemmultifetchjob-caldav-collections.txt"));
fakeServer.addScenarioFromFile(QLatin1String(AUTOTEST_DATA_DIR)+QStringLiteral("/dataitemmultifetchjob-carddav.txt"));
fakeServer.addScenarioFromFile(QLatin1String(AUTOTEST_DATA_DIR)+QStringLiteral("/dataitemmultifetchjob-carddav-collections.txt"));
fakeServer.startAndWait();
job->exec();
fakeServer.quit();
QVERIFY(fakeServer.isAllScenarioDone());
QCOMPARE(job->error(), 0);
const KDAV::DavCollection::List collections = job->collections();
QCOMPARE(collections.count(), 2);
const KDAV::DavCollection calendar = collections.at(0);
QCOMPARE(calendar.displayName(), QStringLiteral("Test1 User"));
QCOMPARE(calendar.contentTypes(), DavCollection::Events | DavCollection::Todos | DavCollection::FreeBusy | DavCollection::Journal);
QCOMPARE(calendar.url().url().path(), QStringLiteral("/caldav.php/test1.user/home/"));
QCOMPARE(calendar.CTag(), QStringLiteral("12345"));
QCOMPARE(calendar.privileges(), KDAV::Read);
const KDAV::DavCollection addressbook = collections.at(1);
QCOMPARE(addressbook.displayName(), QStringLiteral("My Address Book"));
QCOMPARE(addressbook.contentTypes(), DavCollection::Contacts);
QCOMPARE(addressbook.url().url().path(), QStringLiteral("/carddav.php/test1.user/home/"));
QCOMPARE(addressbook.CTag(), QStringLiteral("3145"));
QCOMPARE(addressbook.privileges(), KDAV::All);
}
QTEST_MAIN(DavCollectionsMultiFetchJobTest)
/*
SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef DAVITEMFETCHJOB_TEST_H
#define DAVITEMFETCHJOB_TEST_H
#include <QObject>
class DavCollectionsMultiFetchJobTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void runSuccessfullTest();
};
#endif
......@@ -45,6 +45,10 @@ void FakeServer::dataAvailable()
int scenarioNumber = m_clientSockets.indexOf(socket);
if (scenarioNumber >= m_scenarios.size()) {
qWarning() << "There is no scenario for socket" << scenarioNumber << ", we got more connections than expected";
}
readClientPart(scenarioNumber);
writeServerPart(scenarioNumber);
}
......@@ -178,10 +182,16 @@ void FakeServer::readClientPart(int scenarioNumber)
QList<QByteArray> scenario = m_scenarios[scenarioNumber];
QTcpSocket *socket = m_clientSockets[scenarioNumber];
QByteArray line = socket->readLine();
qDebug() << "Read client request" << line;
QVector<QByteArray> header;
while(line != "\r\n") {
while (line != "\r\n") {
header << line;
if (socket->bytesAvailable() == 0 && !socket->waitForReadyRead()) {
qDebug() << header;
QFAIL("could not read all headers");
return;
}
line = socket->readLine();
}
......@@ -190,7 +200,8 @@ void FakeServer::readClientPart(int scenarioNumber)
QByteArray expected = scenario.takeFirst().mid(3) + "\r\n";
if (!header.contains(expected)) {
qWarning() << expected << "not found in header";
qWarning() << expected << "not found in header. Here's what we got:";
qWarning() << header;
QVERIFY(false);
break;
}
......
......@@ -98,7 +98,7 @@ void DavCollectionsFetchJob::principalFetchFinished(KJob *job)
}
const QStringList homeSets = davJob->homeSets();
qCDebug(KDAV_LOG) << "Found " << homeSets.size() << " homesets";
qCDebug(KDAV_LOG) << "Found" << homeSets.size() << "homesets";
qCDebug(KDAV_LOG) << homeSets;
if (homeSets.isEmpty()) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment