…lines 1–8 unchanged (8 lines)
9 */
10
11#include <QtTest>
12#include <QDebug>
13
14#include "accessmanager.h"
15#include "creds/webflowcredentials.h"
16#include "propagatedownload.h"
17#include "owncloudpropagator_p.h"
18#include "syncenginetestutils.h"
19
20using namespace Qt::StringLiterals;
21
…lines 22–194 unchanged (173 lines)
195 QCOMPARE(job.reply()->error(), QNetworkReply::NoError);
196 // the download succeeded
197 QCOMPARE(receivedContent.size(), decompressedContent.size());
198 }
199 }
200
201 void testDirectUrlCredentials()
202 {
203 // for this one we need two HTTP servers:
204 // - one that acts as a host for direct downloads
205 // - and another one that's our pretend-Nextcloud server
206 //
207 // in any case the requests made to the direct download server should
208 // never contain the `Authorization` header
209
210 QHttpServer directDownloadServer;
211 directDownloadServer.route("/data/directData", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
212 QVERIFY(!request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));
213
214 QHttpHeaders headers;
215 headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain"_ba);
216 responder.write("OK directData"_ba, headers);
217 });
218 directDownloadServer.route("/data/redirectToOtherUrl", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
219 QVERIFY(!request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));
220
221 QHttpHeaders headers;
222 headers.append(QHttpHeaders::WellKnownHeader::Location, "/someOtherPath/redirectTarget"_ba);
223 responder.write(""_ba, headers, QHttpServerResponder::StatusCode::Found);
224 });
225 directDownloadServer.route("/someOtherPath/redirectTarget", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
226 QVERIFY(!request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));
227
228 QHttpHeaders headers;
229 headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain"_ba);
230 headers.append("OC-ETag"_ba, "0123456789abcdef"_ba);
231 headers.append("ETag"_ba, "0123456789abcdef"_ba);
232 responder.write("OK redirectTarget"_ba, headers);
233 });
234
235 QTcpServer directDownloadTcpServer;
236 QVERIFY(directDownloadTcpServer.listen(QHostAddress::LocalHost));
237 QVERIFY(directDownloadServer.bind(&directDownloadTcpServer));
238 const QString directDownloadBaseUrl = "http://%1:%2"_L1.arg(directDownloadTcpServer.serverAddress().toString(), QString::number(directDownloadTcpServer.serverPort()));
239 qInfo() << "Listening on" << directDownloadBaseUrl;
240
241 QHttpServer nextcloudServer;
242 nextcloudServer.route("/remote.php/dav/files/propagatortest/someFile", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
243 QVERIFY(request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));
244
245 QHttpHeaders headers;
246 headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain"_ba);
247 headers.append("OC-ETag"_ba, "0123456789abcdef"_ba);
248 headers.append("ETag"_ba, "0123456789abcdef"_ba);
249 responder.write("OK someFile"_ba, headers);
250 });
251 nextcloudServer.route("/remote.php/dav/files/propagatortest/redirectedFile", [&directDownloadBaseUrl](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
252 QVERIFY(request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));
253
254 QHttpHeaders headers;
255 headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain"_ba);
256 headers.append(QHttpHeaders::WellKnownHeader::Location, directDownloadBaseUrl + "/someOtherPath/redirectTarget"_L1);
257 responder.write(""_ba, headers, QHttpServerResponder::StatusCode::Found);
258 });
259
260 QTcpServer nextcloudTcpServer;
261 QVERIFY(nextcloudTcpServer.listen(QHostAddress::LocalHost));
262 QVERIFY(nextcloudServer.bind(&nextcloudTcpServer));
263 const QString nextcloudBaseUrl = "http://%1:%2"_L1.arg(nextcloudTcpServer.serverAddress().toString(), QString::number(nextcloudTcpServer.serverPort()));
264 qInfo() << "Listening on" << nextcloudBaseUrl;
265
266
267 auto account = OCC::Account::create();
268 // using WebFlowCredentials here to to make sure it works as expected for real
269 account->setCredentials(new WebFlowCredentials(u"propagatortest"_s, u"invalid"_s));
270 account->setDavUser(u"propagatortest"_s);
271 account->setUrl(nextcloudBaseUrl);
272
273
274 {
275 qInfo() << "Test: direct URL";
276 QBuffer receivedContent;
277 receivedContent.open(QIODevice::ReadWrite);
278
279 auto job = GETFileJob(account, QUrl{directDownloadBaseUrl + "/data/directData"_L1}, &receivedContent, {}, {}, 0);
280 QSignalSpy spy(&job, &GETFileJob::finishedSignal);
281
282 job.start();
283 spy.wait(1000);
284
285 // The temporary web servers verify the received headers
286 QCOMPARE(receivedContent.data(), "OK directData"_ba);
287 }
288
289 {
290 qInfo() << "Test: direct URL with redirect";
291 QBuffer receivedContent;
292 receivedContent.open(QIODevice::ReadWrite);
293
294 auto job = GETFileJob(account, QUrl{directDownloadBaseUrl + "/data/redirectToOtherUrl"_L1}, &receivedContent, {}, {}, 0);
295 QSignalSpy spy(&job, &GETFileJob::finishedSignal);
296
297 job.start();
298 spy.wait(1000);
299
300 // The temporary web servers verify the received headers
301 QCOMPARE(receivedContent.data(), "OK redirectTarget"_ba);
302 }
303
304 {
305 qInfo() << "Test: standard dav path";
306 QBuffer receivedContent;
307 receivedContent.open(QIODevice::ReadWrite);
308
309 auto job = GETFileJob(account, "/someFile"_L1, &receivedContent, {}, {}, 0);
310 QSignalSpy spy(&job, &GETFileJob::finishedSignal);
311
312 job.start();
313 spy.wait(1000);
314
315 // The temporary web servers verify the received headers
316 QCOMPARE(receivedContent.data(), "OK someFile"_ba);
317 }
318
319 {
320 qInfo() << "Test: standard dav path with a redirect to a different origin";
321 QBuffer receivedContent;
322 receivedContent.open(QIODevice::ReadWrite);
323
324 auto job = GETFileJob(account, "/redirectedFile"_L1, &receivedContent, {}, {}, 0);
325 QSignalSpy spy(&job, &GETFileJob::finishedSignal);
326
327 job.start();
328 spy.wait(1000);
329
330 // The temporary web servers verify the received headers
331 QCOMPARE(receivedContent.data(), "OK redirectTarget"_ba);
332 }
333 }
334#endif
335};
336
337QTEST_GUILESS_MAIN(TestNextcloudPropagator)
338#include "testnextcloudpropagator.moc"
339