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

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 "channelclosejobtest.h"
0008 #include "channels/channelclosejob.h"
0009 #include "ruqola_restapi_helper.h"
0010 #include <QJsonDocument>
0011 QTEST_GUILESS_MAIN(ChannelCloseJobTest)
0012 using namespace RocketChatRestApi;
0013 ChannelCloseJobTest::ChannelCloseJobTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 void ChannelCloseJobTest::shouldHaveDefaultValue()
0019 {
0020     ChannelCloseJob job;
0021     verifyDefaultValue(&job);
0022     QVERIFY(job.requireHttpAuthentication());
0023     QVERIFY(!job.hasIdentifier());
0024     QCOMPARE(job.channelType(), ChannelCloseJob::ChannelType::Unknown);
0025     QVERIFY(!job.hasQueryParameterSupport());
0026 }
0027 
0028 void ChannelCloseJobTest::shouldGenerateRequest()
0029 {
0030     ChannelCloseJob job;
0031     job.setChannelType(ChannelCloseJob::Channel);
0032     QNetworkRequest request = QNetworkRequest(QUrl());
0033     verifyAuthentication(&job, request);
0034     QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/channels.close")));
0035     QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json"));
0036 
0037     job.setChannelType(ChannelCloseJob::Direct);
0038     verifyAuthentication(&job, request);
0039     QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/im.close")));
0040     QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json"));
0041 
0042     job.setChannelType(ChannelCloseJob::Groups);
0043     verifyAuthentication(&job, request);
0044     QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/groups.close")));
0045     QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json"));
0046 }
0047 
0048 void ChannelCloseJobTest::shouldGenerateJson()
0049 {
0050     ChannelCloseJob job;
0051     const QString roomId = QStringLiteral("foo1");
0052     ChannelGroupBaseJob::ChannelGroupInfo info;
0053     info.channelGroupInfoType = ChannelGroupBaseJob::ChannelGroupInfoType::Identifier;
0054     info.identifier = roomId;
0055     job.setChannelGroupInfo(info);
0056     QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"roomId":"%1"})").arg(roomId).toLatin1());
0057 }
0058 
0059 void ChannelCloseJobTest::shouldNotStarting()
0060 {
0061     ChannelCloseJob job;
0062 
0063     RestApiMethod method;
0064     method.setServerUrl(QStringLiteral("http://www.kde.org"));
0065     job.setRestApiMethod(&method);
0066 
0067     QNetworkAccessManager mNetworkAccessManager;
0068     job.setNetworkAccessManager(&mNetworkAccessManager);
0069     QVERIFY(!job.canStart());
0070     const QString auth = QStringLiteral("foo");
0071     const QString userId = QStringLiteral("foo");
0072     job.setAuthToken(auth);
0073     QVERIFY(!job.canStart());
0074     job.setUserId(userId);
0075     QVERIFY(!job.canStart());
0076     const QString roomId = QStringLiteral("foo1");
0077     ChannelGroupBaseJob::ChannelGroupInfo info;
0078     info.channelGroupInfoType = ChannelGroupBaseJob::ChannelGroupInfoType::Identifier;
0079     info.identifier = roomId;
0080     job.setChannelGroupInfo(info);
0081 
0082     QVERIFY(!job.canStart());
0083     job.setChannelType(ChannelCloseJob::ChannelType::Channel);
0084     QVERIFY(job.canStart());
0085 }
0086 
0087 #include "moc_channelclosejobtest.cpp"