File indexing completed on 2024-12-01 04:35:25

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "notificationinfotest.h"
0007 #include "notificationinfo.h"
0008 #include <QJsonDocument>
0009 #include <QJsonObject>
0010 #include <QTest>
0011 QTEST_GUILESS_MAIN(NotificationInfoTest)
0012 
0013 NotificationInfoTest::NotificationInfoTest(QObject *parent)
0014     : QObject{parent}
0015 {
0016 }
0017 
0018 void NotificationInfoTest::shouldHaveDefaultValues()
0019 {
0020     NotificationInfo t;
0021     QVERIFY(t.accountName().isEmpty());
0022     QVERIFY(t.message().isEmpty());
0023     QVERIFY(t.title().isEmpty());
0024     QVERIFY(t.senderId().isEmpty());
0025     QVERIFY(t.senderName().isEmpty());
0026     QVERIFY(t.senderUserName().isEmpty());
0027     QVERIFY(t.roomName().isEmpty());
0028     QVERIFY(t.roomId().isEmpty());
0029     QVERIFY(t.channelType().isEmpty());
0030     QVERIFY(t.tmId().isEmpty());
0031     QVERIFY(t.pixmap().isNull());
0032     QVERIFY(t.messageId().isEmpty());
0033     QCOMPARE(t.notificationType(), NotificationInfo::NotificationType::StandardMessage);
0034 }
0035 
0036 void NotificationInfoTest::shouldParseNotification_data()
0037 {
0038     QTest::addColumn<QString>("fileName");
0039     QTest::addColumn<QString>("title");
0040     QTest::addColumn<QString>("message");
0041     QTest::addColumn<QString>("sender");
0042     QTest::addColumn<QString>("roomName");
0043     QTest::addColumn<QString>("channelType");
0044     QTest::addColumn<QString>("senderName");
0045     QTest::addColumn<QString>("senderUserName");
0046     QTest::addColumn<QString>("roomId");
0047     QTest::addColumn<QString>("tmId");
0048     QTest::addColumn<QString>("messageId");
0049     QTest::newRow("notification1") << QStringLiteral("notification") << QStringLiteral("title") << QStringLiteral("pong") << QStringLiteral("tgrk5CZKgYGiSSqXp")
0050                                    << QString() << QStringLiteral("d") << QStringLiteral("Laurent") << QStringLiteral("laurent")
0051                                    << QStringLiteral("tgrk5CZKgYGiSSqXpuKK39zoewTkdacidH") << QString() << QStringLiteral("yfitiEkpGyDAAJ6Eo");
0052     // TODO
0053     QTest::newRow("notificationencrypted") << QStringLiteral("notificationencrypted") << QStringLiteral("title") << QStringLiteral("pong")
0054                                            << QStringLiteral("tgrk5CZKgYGiSSqXp") << QString() << QStringLiteral("d") << QStringLiteral("foo")
0055                                            << QStringLiteral("bla") << QStringLiteral("XQv6u7Kyb4pfDhS4wuKK39zoewTkdacidH") << QString()
0056                                            << QStringLiteral("PuKLfJfd5r5DrZBdN");
0057 
0058     QTest::newRow("notification2") << QStringLiteral("notification1") << QStringLiteral("my title") << QStringLiteral("@here")
0059                                    << QStringLiteral("Gsvg6BGoBfmPLoFie") << QStringLiteral("roomname example") << QStringLiteral("c") << QStringLiteral("foo")
0060                                    << QStringLiteral("foo.bla") << QStringLiteral("Fc4EWcQXCkJkJ7QEQ") << QString() << QStringLiteral("dNYhFAYBhqPFnBwRy");
0061 
0062     QTest::newRow("notification-tmid1") << QStringLiteral("notification-tmid1") << QStringLiteral("my title") << QStringLiteral("@here")
0063                                         << QStringLiteral("Gsvg6BGoBfmPLoFie") << QStringLiteral("roomname example") << QStringLiteral("c")
0064                                         << QStringLiteral("foo") << QStringLiteral("foo.bla") << QStringLiteral("Fc4EWcQXCkJkJ7QEQ")
0065                                         << QStringLiteral("QLNcu8TnRi8Ld57Pk") << QStringLiteral("dNYhFAYBhqPFnBwRy");
0066 }
0067 
0068 void NotificationInfoTest::shouldParseNotification()
0069 {
0070     QFETCH(QString, fileName);
0071     QFETCH(QString, title);
0072     QFETCH(QString, message);
0073     QFETCH(QString, sender);
0074     QFETCH(QString, roomName);
0075     QFETCH(QString, channelType);
0076     QFETCH(QString, senderName);
0077     QFETCH(QString, senderUserName);
0078     QFETCH(QString, roomId);
0079     QFETCH(QString, tmId);
0080     QFETCH(QString, messageId);
0081     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/") + fileName + QLatin1String(".json");
0082     QFile f(originalJsonFile);
0083     QVERIFY(f.open(QIODevice::ReadOnly));
0084     const QByteArray content = f.readAll();
0085     f.close();
0086     const QJsonDocument doc = QJsonDocument::fromJson(content);
0087     const QJsonObject fields = doc.object().value(QLatin1String("fields")).toObject();
0088     const QJsonArray contents = fields.value(QLatin1String("args")).toArray();
0089 
0090     NotificationInfo info;
0091     info.parseNotification(contents);
0092     QEXPECT_FAIL("notificationencrypted", "Encrypted message not supported yet", Continue);
0093     QCOMPARE(info.message(), message);
0094     QCOMPARE(info.title(), title);
0095     QEXPECT_FAIL("notificationencrypted", "Encrypted message not supported yet", Continue);
0096     QCOMPARE(info.senderId(), sender);
0097     QCOMPARE(info.roomName(), roomName);
0098     QCOMPARE(info.roomId(), roomId);
0099     QCOMPARE(info.channelType(), channelType);
0100     QCOMPARE(info.senderUserName(), senderUserName);
0101     QCOMPARE(info.senderName(), senderName);
0102     QCOMPARE(info.tmId(), tmId);
0103     QCOMPARE(info.messageId(), messageId);
0104 
0105     QVERIFY(info.isValid());
0106 }
0107 
0108 #include "moc_notificationinfotest.cpp"