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 "../bugzilla.h"
0010 
0011 #include "jobdouble.h"
0012 
0013 namespace Bugzilla
0014 {
0015 class ConnectionDouble : public Connection
0016 {
0017 public:
0018     using Connection::Connection;
0019 
0020     void setToken(const QString &) override
0021     {
0022         Q_UNREACHABLE();
0023     }
0024 
0025     [[nodiscard]] APIJob *get(const QString &path, const Query &query = Query()) const override
0026     {
0027         if (path == "/version") {
0028             return new JobDouble{QFINDTESTDATA("data/bugzilla.version.json")};
0029         } else if (path == "/login" && query.toString() == "login=auser&password=apass&restrict_login=true") {
0030             return new JobDouble{QFINDTESTDATA("data/bugzilla.login.json")};
0031         }
0032         Q_ASSERT_X(false, "get", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0033         return nullptr;
0034     }
0035 
0036     [[nodiscard]] APIJob *post(const QString &path, const QByteArray &, const Query &query = Query()) const override
0037     {
0038         Q_ASSERT_X(false, "post", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0039         return nullptr;
0040     }
0041 
0042     [[nodiscard]] APIJob *put(const QString &path, const QByteArray &, const Query &query = Query()) const override
0043     {
0044         Q_ASSERT_X(false, "put", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0045         return nullptr;
0046     }
0047 };
0048 
0049 } // namespace Bugzilla
0050 
0051 class BugzillaTest : public QObject
0052 {
0053     Q_OBJECT
0054 private Q_SLOTS:
0055     void initTestCase()
0056     {
0057         Bugzilla::setConnection(m_doubleConnection);
0058     }
0059 
0060     void testVersion()
0061     {
0062         KJob *job = Bugzilla::version();
0063         job->start();
0064         QCOMPARE(Bugzilla::version(job), "5.0.0");
0065     }
0066 
0067     void testLogin()
0068     {
0069         KJob *job = Bugzilla::login("auser", "apass");
0070         job->start();
0071         auto details = Bugzilla::login(job);
0072         QCOMPARE(details.id, 52960);
0073         QCOMPARE(details.token, "52960-aaaaaaaaaa");
0074     }
0075 
0076 private:
0077     Bugzilla::ConnectionDouble *m_doubleConnection = new Bugzilla::ConnectionDouble;
0078 };
0079 
0080 QTEST_GUILESS_MAIN(BugzillaTest)
0081 
0082 #include "bugzillatest.moc"