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 "createchanneljobtest.h"
0008 #include "channels/createchanneljob.h"
0009 #include "ruqola_restapi_helper.h"
0010 #include <QJsonDocument>
0011 QTEST_GUILESS_MAIN(CreateChannelJobTest)
0012 using namespace RocketChatRestApi;
0013 CreateChannelJobTest::CreateChannelJobTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 void CreateChannelJobTest::shouldHaveDefaultValue()
0019 {
0020     CreateChannelJob job;
0021     verifyDefaultValue(&job);
0022     QVERIFY(job.requireHttpAuthentication());
0023     CreateChannelTeamInfo info = job.createChannelInfo();
0024     QVERIFY(!info.readOnly);
0025     QVERIFY(info.name.isEmpty());
0026     QVERIFY(info.members.isEmpty());
0027     QVERIFY(!job.hasQueryParameterSupport());
0028 }
0029 
0030 void CreateChannelJobTest::shouldGenerateRequest()
0031 {
0032     CreateChannelJob job;
0033     QNetworkRequest request = QNetworkRequest(QUrl());
0034     verifyAuthentication(&job, request);
0035     QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/channels.create")));
0036     QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json"));
0037 }
0038 
0039 void CreateChannelJobTest::shouldGenerateJson()
0040 {
0041     CreateChannelJob job;
0042     const QString channelname = QStringLiteral("foo1");
0043     CreateChannelTeamInfo info;
0044     info.name = channelname;
0045     job.setCreateChannelInfo(info);
0046     QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"extraData":{},"name":"%1"})").arg(channelname).toLatin1());
0047 
0048     bool readOnly = false;
0049     info.readOnly = readOnly;
0050     job.setCreateChannelInfo(info);
0051     QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"extraData":{},"name":"%1"})").arg(channelname).toLatin1());
0052 
0053     readOnly = true;
0054     info.readOnly = readOnly;
0055     job.setCreateChannelInfo(info);
0056     QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"extraData":{},"name":"%1","readOnly":true})").arg(channelname).toLatin1());
0057 
0058     const QStringList members = {QStringLiteral("foo"), QStringLiteral("bla")};
0059     info.members = members;
0060     job.setCreateChannelInfo(info);
0061     QCOMPARE(job.json().toJson(QJsonDocument::Compact),
0062              QStringLiteral(R"({"extraData":{},"members":["foo","bla"],"name":"%1","readOnly":true})").arg(channelname).toLatin1());
0063 
0064     const QString teamId = {QStringLiteral("foo")};
0065     info.teamId = teamId;
0066     job.setCreateChannelInfo(info);
0067     QCOMPARE(job.json().toJson(QJsonDocument::Compact),
0068              QStringLiteral(R"({"extraData":{"teamId":"foo"},"members":["foo","bla"],"name":"%1","readOnly":true})").arg(channelname).toLatin1());
0069 }
0070 
0071 void CreateChannelJobTest::shouldNotStarting()
0072 {
0073     CreateChannelJob job;
0074 
0075     RestApiMethod method;
0076     method.setServerUrl(QStringLiteral("http://www.kde.org"));
0077     job.setRestApiMethod(&method);
0078 
0079     QNetworkAccessManager mNetworkAccessManager;
0080     job.setNetworkAccessManager(&mNetworkAccessManager);
0081     QVERIFY(!job.canStart());
0082     const QString auth = QStringLiteral("foo");
0083     const QString userId = QStringLiteral("foo");
0084     job.setAuthToken(auth);
0085     QVERIFY(!job.canStart());
0086     job.setUserId(userId);
0087     QVERIFY(!job.canStart());
0088     const QString channelName = QStringLiteral("foo1");
0089     CreateChannelTeamInfo info;
0090     info.name = channelName;
0091     job.setCreateChannelInfo(info);
0092     QVERIFY(job.canStart());
0093 }
0094 
0095 #include "moc_createchanneljobtest.cpp"