File indexing completed on 2025-02-02 04:51:23

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "createdmjobtest.h"
0008 #include "directmessage/createdmjob.h"
0009 #include "ruqola_restapi_helper.h"
0010 #include <QJsonDocument>
0011 QTEST_GUILESS_MAIN(CreateDmJobTest)
0012 using namespace RocketChatRestApi;
0013 CreateDmJobTest::CreateDmJobTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 void CreateDmJobTest::shouldHaveDefaultValue()
0019 {
0020     CreateDmJob job;
0021     verifyDefaultValue(&job);
0022     QVERIFY(job.requireHttpAuthentication());
0023     QVERIFY(job.userNames().isEmpty());
0024     QVERIFY(!job.hasQueryParameterSupport());
0025 }
0026 
0027 void CreateDmJobTest::shouldGenerateRequest()
0028 {
0029     CreateDmJob job;
0030     QNetworkRequest request = QNetworkRequest(QUrl());
0031     verifyAuthentication(&job, request);
0032     QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/im.create")));
0033     QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json"));
0034 }
0035 
0036 void CreateDmJobTest::shouldGenerateJson()
0037 {
0038     CreateDmJob job;
0039     {
0040         const QString channelname = QStringLiteral("foo1");
0041         job.setUserNames({channelname});
0042         QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"username":"%1"})").arg(channelname).toLatin1());
0043     }
0044     {
0045         const QStringList userNames = {QStringLiteral("foo1"), QStringLiteral("bla"), QStringLiteral("bli")};
0046         job.setUserNames(userNames);
0047         QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"usernames":"foo1,bla,bli"})").toLatin1());
0048     }
0049 }
0050 
0051 void CreateDmJobTest::shouldNotStarting()
0052 {
0053     CreateDmJob job;
0054 
0055     RestApiMethod method;
0056     method.setServerUrl(QStringLiteral("http://www.kde.org"));
0057     job.setRestApiMethod(&method);
0058 
0059     QNetworkAccessManager mNetworkAccessManager;
0060     job.setNetworkAccessManager(&mNetworkAccessManager);
0061     QVERIFY(!job.canStart());
0062     const QString auth = QStringLiteral("foo");
0063     const QString userId = QStringLiteral("foo");
0064     job.setAuthToken(auth);
0065     QVERIFY(!job.canStart());
0066     job.setUserId(userId);
0067     QVERIFY(!job.canStart());
0068     const QString username = QStringLiteral("foo1");
0069     job.setUserNames({username});
0070     QVERIFY(job.canStart());
0071 }
0072 
0073 #include "moc_createdmjobtest.cpp"