File indexing completed on 2024-05-12 05:22:06

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@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 <QObject>
0008 #include <QTest>
0009 
0010 #include "fakenetworkaccessmanagerfactory.h"
0011 #include "testutils.h"
0012 
0013 #include "account.h"
0014 #include "accountinfo/accountinfo.h"
0015 #include "accountinfo/accountinfofetchjob.h"
0016 #include "types.h"
0017 
0018 using namespace KGAPI2;
0019 
0020 Q_DECLARE_METATYPE(QList<FakeNetworkAccessManager::Scenario>)
0021 
0022 class AccountInfoFetchJobTest : public QObject
0023 {
0024     Q_OBJECT
0025 private Q_SLOTS:
0026     void initTestCase()
0027     {
0028         NetworkAccessManagerFactory::setFactory(new FakeNetworkAccessManagerFactory);
0029     }
0030 
0031     void testFetch()
0032     {
0033         QFile f(QFINDTESTDATA("data/accountinfo.json"));
0034         QVERIFY(f.open(QIODevice::ReadOnly));
0035         auto accountInfo = AccountInfo::fromJSON(f.readAll());
0036         QVERIFY(accountInfo);
0037 
0038         FakeNetworkAccessManagerFactory::get()->setScenarios(
0039             {scenarioFromFile(QFINDTESTDATA("data/accountinfo_fetch_request.txt"), QFINDTESTDATA("data/accountinfo_fetch_response.txt"))});
0040 
0041         auto account = AccountPtr::create(QStringLiteral("MockAccount"), QStringLiteral("MockToken"));
0042         auto job = new AccountInfoFetchJob(account);
0043         QVERIFY(execJob(job));
0044         const auto items = job->items();
0045         QCOMPARE(items.count(), 1);
0046         QCOMPARE(*items.at(0).dynamicCast<AccountInfo>(), *accountInfo);
0047     }
0048 };
0049 
0050 QTEST_GUILESS_MAIN(AccountInfoFetchJobTest)
0051 
0052 #include "accountinfofetchjobtest.moc"