File indexing completed on 2024-05-05 05:29:37

0001 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0002 // SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>
0003 
0004 #include <QJsonDocument>
0005 #include <QSignalSpy>
0006 #include <QStandardPaths>
0007 #include <QTest>
0008 
0009 #include <sentrypostbox.h>
0010 
0011 #include "sentryfilereply.h"
0012 
0013 using namespace Qt::StringLiterals;
0014 
0015 class FileConnection : public SentryConnection
0016 {
0017 public:
0018     using SentryConnection::SentryConnection;
0019 
0020     SentryReply *get(const QNetworkRequest &request) override
0021     {
0022         if (request.url() == QUrl("https://autoconfig.kde.org/drkonqi/sentry/0/dsns.json"_L1)) {
0023             return new FileReply(QFINDTESTDATA("data/dsns.json"));
0024         }
0025 
0026         qWarning() << "unhandled request" << request.url();
0027         Q_ASSERT(false);
0028         return nullptr;
0029     }
0030 
0031     SentryReply *post(const QNetworkRequest &request, const QByteArray &) override
0032     {
0033         if (request.url() == QUrl("https://456f53a71a074438bbb786d6add63241@crash-reports.kde.org/api/11/envelope/"_L1)) {
0034             return new FileReply("/dev/null"_L1);
0035         }
0036 
0037         qWarning() << "unhandled request" << request.url();
0038         Q_ASSERT(false);
0039         return nullptr;
0040     }
0041 };
0042 
0043 class SentryPostboxTest : public QObject
0044 {
0045     Q_OBJECT
0046 private Q_SLOTS:
0047     void initTestCase()
0048     {
0049         QStandardPaths::setTestModeEnabled(true);
0050     }
0051 
0052     void testKWrite()
0053     {
0054         QFile eventPayloadFile(QFINDTESTDATA("data/sentry-event.json"));
0055         QVERIFY(eventPayloadFile.open(QFile::ReadOnly));
0056 
0057         SentryPostbox box("kwrite"_L1, std::make_shared<FileConnection>());
0058         box.addEventPayload(QJsonDocument::fromJson(eventPayloadFile.readAll()));
0059         QSignalSpy spy(&box, &SentryPostbox::hasDeliveredChanged);
0060         box.deliver();
0061         spy.wait();
0062         QCOMPARE(spy.count(), 1);
0063         QVERIFY(box.hasDelivered());
0064     }
0065 
0066     void testFallthrough()
0067     {
0068         QFile eventPayloadFile(QFINDTESTDATA("data/sentry-event.json"));
0069         QVERIFY(eventPayloadFile.open(QFile::ReadOnly));
0070 
0071         SentryPostbox box("foobar_does_not_exist_in_dsns"_L1, std::make_shared<FileConnection>());
0072         box.addEventPayload(QJsonDocument::fromJson(eventPayloadFile.readAll()));
0073         QSignalSpy spy(&box, &SentryPostbox::hasDeliveredChanged);
0074         box.deliver();
0075         spy.wait();
0076         QCOMPARE(spy.count(), 1);
0077         QVERIFY(box.hasDelivered());
0078     }
0079 };
0080 
0081 QTEST_GUILESS_MAIN(SentryPostboxTest)
0082 
0083 #include "sentrypostboxtest.moc"