File indexing completed on 2024-05-12 16:59:02

0001 /*
0002     SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include <QDebug>
0010 
0011 #include "../clients/bugclient.h"
0012 #include "../clients/productclient.h"
0013 
0014 #include "jobdouble.h"
0015 
0016 namespace Bugzilla
0017 {
0018 static void compareNewBugHash(const QVariantHash &hash, bool *ok)
0019 {
0020     *ok = false;
0021     QCOMPARE(hash["product"].toString(), QLatin1String("aproduct"));
0022     QCOMPARE(hash["component"].toString(), QLatin1String("acomp"));
0023     QCOMPARE(hash["summary"].toString(), QLatin1String("asummary"));
0024     QCOMPARE(hash["version"].toString(), QLatin1String("aversion"));
0025     QCOMPARE(hash["description"].toString(), QLatin1String("adescription"));
0026     QCOMPARE(hash["op_sys"].toString(), QLatin1String("asys"));
0027     QCOMPARE(hash["platform"].toString(), QLatin1String("aplatform"));
0028     QCOMPARE(hash["priority"].toString(), QLatin1String("apriority"));
0029     QCOMPARE(hash["severity"].toString(), QLatin1String("aseverity"));
0030     QCOMPARE(hash["keywords"].toStringList(), QStringList({"aword", "anotherword"}));
0031     *ok = true;
0032 }
0033 
0034 static void compareUpdateBugHash(const QVariantHash &hash, bool *ok)
0035 {
0036     *ok = false;
0037     QCOMPARE(hash["cc"].toHash()["add"].toStringList(), QStringList({"me@host.com"}));
0038     QCOMPARE(hash["cc"].toHash()["remove"].toStringList(), QStringList({"you@host.com"}));
0039     *ok = true;
0040 }
0041 
0042 class ConnectionDouble : public Connection
0043 {
0044 public:
0045     using Connection::Connection;
0046 
0047     void setToken(const QString &) override
0048     {
0049         Q_UNREACHABLE();
0050     }
0051 
0052     [[nodiscard]] APIJob *get(const QString &path, const Query &query = Query()) const override
0053     {
0054         if (path == "/bug" && query.toString() == "product=dragonplayer") {
0055             return new JobDouble{QFINDTESTDATA("data/bugs.dragonplayer.json")};
0056         }
0057         if (path == "/bug" && query.toString() == "product=dragonplayer2") {
0058             return new JobDouble{QFINDTESTDATA("data/bugs.unresolved.json")};
0059         }
0060         if (path == "/bug" && query.toString() == "product=dragonplayerSecondProduct&product=dragonplayerFirstProduct") {
0061             // simply to test the query params. returns regular unresolved result
0062             return new JobDouble{QFINDTESTDATA("data/bugs.unresolved.json")};
0063         }
0064         Q_ASSERT_X(false, "get", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0065         return nullptr;
0066     }
0067 
0068     [[nodiscard]] APIJob *post(const QString &path, const QByteArray &data, const Query &query = Query()) const override
0069     {
0070         qDebug() << path << query.toString();
0071         if (path == "/bug" && query.isEmpty()) {
0072             QJsonParseError e;
0073             auto doc = QJsonDocument::fromJson(data, &e);
0074             Q_ASSERT(e.error == QJsonParseError::NoError);
0075             auto hash = doc.object().toVariantHash();
0076             bool ok;
0077             compareNewBugHash(hash, &ok);
0078             Q_ASSERT(ok);
0079 
0080             return new JobDouble{QFINDTESTDATA("data/bugs.new.json")};
0081         }
0082         Q_ASSERT_X(false, "post", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0083         return nullptr;
0084     }
0085 
0086     [[nodiscard]] APIJob *put(const QString &path, const QByteArray &data, const Query &query = Query()) const override
0087     {
0088         if (path == "/bug/54321" && query.isEmpty()) {
0089             QJsonParseError e;
0090             auto doc = QJsonDocument::fromJson(data, &e);
0091             Q_ASSERT(e.error == QJsonParseError::NoError);
0092             auto hash = doc.object().toVariantHash();
0093             bool ok;
0094             compareUpdateBugHash(hash, &ok);
0095             Q_ASSERT(ok);
0096 
0097             return new JobDouble{QFINDTESTDATA("data/bugs.update.json")};
0098         }
0099         Q_ASSERT_X(false, "put", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0100         return nullptr;
0101     }
0102 };
0103 
0104 class BugTest : public QObject
0105 {
0106     Q_OBJECT
0107 private Q_SLOTS:
0108 
0109     void initTestCase()
0110     {
0111         Bugzilla::setConnection(m_doubleConnection);
0112     }
0113 
0114     void testSearch()
0115     {
0116         Bugzilla::BugSearch search;
0117         search.products = QStringList{"dragonplayer"};
0118         auto job = Bugzilla::BugClient().search(search);
0119         job->start();
0120         const QList<Bug::Ptr> bugs = Bugzilla::BugClient().search(job);
0121         QCOMPARE(bugs.size(), 2);
0122         Bug::Ptr bug;
0123         for (const auto &b : bugs) {
0124             if (b->id() == 156514) {
0125                 bug = b;
0126             }
0127         }
0128 
0129         QCOMPARE(bug.isNull(), false);
0130         QCOMPARE(bug->id(), 156514);
0131         QCOMPARE(bug->product(), "dragonplayer");
0132         QCOMPARE(bug->component(), "general");
0133         QCOMPARE(bug->summary(), "Supported filetypes not shown in Play File.. Dialog");
0134         QCOMPARE(bug->version(), "unspecified");
0135         QCOMPARE(bug->op_sys(), "Linux");
0136         QCOMPARE(bug->priority(), "NOR");
0137         QCOMPARE(bug->severity(), "normal");
0138         QCOMPARE(bug->status(), Bug::Status::RESOLVED);
0139         QCOMPARE(bug->resolution(), Bug::Resolution::FIXED);
0140         QCOMPARE(bug->dupe_of(), -1);
0141         QCOMPARE(bug->is_open(), false);
0142         QCOMPARE(bug->customField("cf_versionfixedin"), "5.0");
0143     }
0144 
0145     void testSearchUnresolved()
0146     {
0147         Bugzilla::BugSearch search;
0148         search.products = QStringList{"dragonplayer2"};
0149         auto job = Bugzilla::BugClient().search(search);
0150         job->start();
0151         QList<Bug::Ptr> bugs = Bugzilla::BugClient().search(job);
0152         QCOMPARE(bugs.size(), 1);
0153         // resolution:"" maps to NONE
0154         QCOMPARE(bugs.at(0)->resolution(), Bug::Resolution::NONE);
0155         // None of the above should fail assertions or exception tests.
0156     }
0157 
0158     void testSearchMultipleProducts()
0159     {
0160         // Queries support the same argument more than once and indeed the API does too and we rely on this behavior.
0161         // Make sure multiple keys are properly sent in the request.
0162         Bugzilla::BugSearch search;
0163         search.products = QStringList{"dragonplayerFirstProduct", "dragonplayerSecondProduct"};
0164         auto job = Bugzilla::BugClient().search(search);
0165         job->start();
0166         QList<Bug::Ptr> bugs = Bugzilla::BugClient().search(job);
0167         QCOMPARE(bugs.size(), 1);
0168         // None of the above should fail assertions or exception tests.
0169     }
0170 
0171     void testNewBug()
0172     {
0173         Bugzilla::NewBug bug;
0174 
0175         bug.product = "aproduct";
0176         bug.component = "acomp";
0177         bug.summary = "asummary";
0178         bug.version = "aversion";
0179         bug.description = "adescription";
0180         bug.op_sys = "asys";
0181         bug.platform = "aplatform";
0182         bug.priority = "apriority";
0183         bug.severity = "aseverity";
0184         bug.keywords = QStringList{"aword", "anotherword"};
0185 
0186         auto job = Bugzilla::BugClient().create(bug);
0187         job->start();
0188         qint64 id = Bugzilla::BugClient().create(job);
0189         QCOMPARE(id, 12345);
0190     }
0191 
0192     void testUpdateBug()
0193     {
0194         Bugzilla::BugUpdate bug;
0195         bug.cc->add << "me@host.com";
0196         bug.cc->remove << "you@host.com";
0197 
0198         auto job = Bugzilla::BugClient().update(54321, bug);
0199         job->start();
0200         qint64 id = Bugzilla::BugClient().update(job);
0201         QCOMPARE(id, 54321);
0202     }
0203 
0204 private:
0205     Bugzilla::ConnectionDouble *m_doubleConnection = new Bugzilla::ConnectionDouble;
0206 };
0207 
0208 } // namespace Bugzilla
0209 
0210 QTEST_MAIN(Bugzilla::BugTest)
0211 
0212 #include "bugtest.moc"