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

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 "savenotificationjobtest.h"
0008 #include "rooms/savenotificationjob.h"
0009 #include "ruqola_restapi_helper.h"
0010 #include <QJsonDocument>
0011 QTEST_GUILESS_MAIN(SaveNotificationJobTest)
0012 using namespace RocketChatRestApi;
0013 SaveNotificationJobTest::SaveNotificationJobTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 void SaveNotificationJobTest::shouldHaveDefaultValue()
0019 {
0020     SaveNotificationJob job;
0021     verifyDefaultValue(&job);
0022 
0023     QVERIFY(job.roomId().isEmpty());
0024     QVERIFY(!job.disableNotifications());
0025     QVERIFY(!job.muteGroupMentions());
0026     QVERIFY(!job.hideUnreadStatus());
0027     QVERIFY(!job.hideMentionStatus());
0028     QVERIFY(job.emailNotifications().isEmpty());
0029     QVERIFY(job.desktopNotifications().isEmpty());
0030     QVERIFY(job.mobilePushNotifications().isEmpty());
0031     QVERIFY(job.audioNotificationValue().isEmpty());
0032     QCOMPARE(job.desktopNotificationDuration(), 0);
0033     QVERIFY(job.unreadAlert().isEmpty());
0034 
0035     QVERIFY(job.requireHttpAuthentication());
0036     QVERIFY(job.roomId().isEmpty());
0037     QVERIFY(!job.hasQueryParameterSupport());
0038     QVERIFY(!job.requireTwoFactorAuthentication());
0039 }
0040 
0041 void SaveNotificationJobTest::shouldGenerateRequest()
0042 {
0043     SaveNotificationJob job;
0044     QNetworkRequest request = QNetworkRequest(QUrl());
0045     verifyAuthentication(&job, request);
0046     QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/rooms.saveNotification")));
0047     QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json"));
0048 }
0049 
0050 void SaveNotificationJobTest::shouldGenerateJson()
0051 {
0052     SaveNotificationJob job;
0053     const QString roomId = QStringLiteral("foo1");
0054     job.setRoomId(roomId);
0055     QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"notifications":{},"roomId":"%1"})").arg(roomId).toLatin1());
0056 
0057     // Add settings
0058     const bool hideUnread = true;
0059     job.setHideUnreadStatus(hideUnread);
0060     QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"notifications":{"hideUnreadStatus":"1"},"roomId":"%1"})").arg(roomId).toLatin1());
0061     const QString mobilePushNotifications = QStringLiteral("all");
0062     job.setMobilePushNotifications(mobilePushNotifications);
0063     QCOMPARE(job.json().toJson(QJsonDocument::Compact),
0064              QStringLiteral("{\"notifications\":{"
0065                             "\"hideUnreadStatus\":\"1\","
0066                             "\"mobilePushNotifications\":\"%3\"},\"roomId\":\"%1\"}")
0067                  .arg(roomId, mobilePushNotifications)
0068                  .toLatin1());
0069 
0070     const bool disableNotifications = true;
0071     job.setDisableNotifications(disableNotifications);
0072     QCOMPARE(job.json().toJson(QJsonDocument::Compact),
0073              QStringLiteral("{\"notifications\":{"
0074                             "\"disableNotifications\":\"1\","
0075                             "\"hideUnreadStatus\":\"1\","
0076                             "\"mobilePushNotifications\":\"%3\"},\"roomId\":\"%1\"}")
0077                  .arg(roomId, mobilePushNotifications)
0078                  .toLatin1());
0079 
0080     const bool muteMentionGroups = true;
0081     job.setMuteGroupMentions(muteMentionGroups);
0082     QCOMPARE(job.json().toJson(QJsonDocument::Compact),
0083              QStringLiteral("{\"notifications\":{"
0084                             "\"disableNotifications\":\"1\","
0085                             "\"hideUnreadStatus\":\"1\","
0086                             "\"mobilePushNotifications\":\"%3\","
0087                             "\"muteGroupMentions\":\"1\"},\"roomId\":\"%1\"}")
0088                  .arg(roomId, mobilePushNotifications)
0089                  .toLatin1());
0090 
0091     // TODO add more settings
0092 }
0093 
0094 void SaveNotificationJobTest::shouldNotStarting()
0095 {
0096     SaveNotificationJob job;
0097 
0098     RestApiMethod method;
0099     method.setServerUrl(QStringLiteral("http://www.kde.org"));
0100     job.setRestApiMethod(&method);
0101 
0102     QNetworkAccessManager mNetworkAccessManager;
0103     job.setNetworkAccessManager(&mNetworkAccessManager);
0104     QVERIFY(!job.canStart());
0105     const QString auth = QStringLiteral("foo");
0106     const QString userId = QStringLiteral("foo");
0107     job.setAuthToken(auth);
0108     QVERIFY(!job.canStart());
0109     job.setUserId(userId);
0110     QVERIFY(!job.canStart());
0111     const QString roomId = QStringLiteral("foo1");
0112     job.setRoomId(roomId);
0113     QVERIFY(!job.canStart());
0114 
0115     // We need to change a settings
0116     job.setAudioNotificationValue(QStringLiteral("foo"));
0117     QVERIFY(job.canStart());
0118 }
0119 
0120 #include "moc_savenotificationjobtest.cpp"