File indexing completed on 2024-04-28 04:59:30

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include <QObject>
0005 #include <QTest>
0006 
0007 #include "linkpreviewer.h"
0008 
0009 #include <Quotient/events/roommessageevent.h>
0010 #include <Quotient/quotient_common.h>
0011 #include <Quotient/syncdata.h>
0012 
0013 #include "utils.h"
0014 
0015 #include "testutils.h"
0016 
0017 using namespace Quotient;
0018 
0019 class LinkPreviewerTest : public QObject
0020 {
0021     Q_OBJECT
0022 
0023 private:
0024     Connection *connection = nullptr;
0025     TestUtils::TestRoom *room = nullptr;
0026 
0027 private Q_SLOTS:
0028     void initTestCase();
0029 
0030     void linkPreviewsMatch_data();
0031     void linkPreviewsMatch();
0032 
0033     void linkPreviewsReject_data();
0034     void linkPreviewsReject();
0035 
0036     void editedLink();
0037 };
0038 
0039 void LinkPreviewerTest::initTestCase()
0040 {
0041     connection = Connection::makeMockConnection(QStringLiteral("@bob:example.org"));
0042     room = new TestUtils::TestRoom(connection, QStringLiteral("!test:example.org"));
0043 }
0044 
0045 void LinkPreviewerTest::linkPreviewsMatch_data()
0046 {
0047     QTest::addColumn<QString>("eventSource");
0048     QTest::addColumn<QUrl>("testOutputLink");
0049 
0050     QTest::newRow("plainHttps") << QStringLiteral("test-validplainlink-event.json") << QUrl("https://kde.org"_ls);
0051     QTest::newRow("richHttps") << QStringLiteral("test-validrichlink-event.json") << QUrl("https://kde.org"_ls);
0052     QTest::newRow("plainWww") << QStringLiteral("test-validplainwwwlink-event.json") << QUrl("www.example.org"_ls);
0053     QTest::newRow("multipleHttps") << QStringLiteral("test-multiplelink-event.json") << QUrl("www.example.org"_ls);
0054 }
0055 
0056 void LinkPreviewerTest::linkPreviewsMatch()
0057 {
0058     QFETCH(QString, eventSource);
0059     QFETCH(QUrl, testOutputLink);
0060 
0061     auto event = TestUtils::loadEventFromFile<RoomMessageEvent>(eventSource);
0062     auto linkPreviewer = LinkPreviewer(room, event.get());
0063 
0064     QCOMPARE(linkPreviewer.empty(), false);
0065     QCOMPARE(linkPreviewer.url(), testOutputLink);
0066 }
0067 
0068 void LinkPreviewerTest::linkPreviewsReject_data()
0069 {
0070     QTest::addColumn<QString>("eventSource");
0071 
0072     QTest::newRow("mxc") << QStringLiteral("test-invalidmxclink-event.json");
0073     QTest::newRow("matrixTo") << QStringLiteral("test-invalidmatrixtolink-event.json");
0074     QTest::newRow("noSpace") << QStringLiteral("test-invalidnospacelink-event.json");
0075 }
0076 
0077 void LinkPreviewerTest::linkPreviewsReject()
0078 {
0079     QFETCH(QString, eventSource);
0080 
0081     auto event = TestUtils::loadEventFromFile<RoomMessageEvent>(eventSource);
0082     auto linkPreviewer = LinkPreviewer(room, event.get());
0083 
0084     QCOMPARE(linkPreviewer.empty(), true);
0085     QCOMPARE(linkPreviewer.url(), QUrl());
0086 }
0087 
0088 void LinkPreviewerTest::editedLink()
0089 {
0090     room->syncNewEvents(QStringLiteral("test-linkpreviewerintial-sync.json"));
0091     auto event = eventCast<const RoomMessageEvent>(room->messageEvents().at(0).get());
0092     auto linkPreviewer = LinkPreviewer(room, event);
0093 
0094     QCOMPARE(linkPreviewer.empty(), false);
0095     QCOMPARE(linkPreviewer.url(), QUrl("https://kde.org"_ls));
0096 
0097     room->syncNewEvents(QStringLiteral("test-linkpreviewerreplace-sync.json"));
0098 
0099     QCOMPARE(linkPreviewer.empty(), true);
0100     QCOMPARE(linkPreviewer.url(), QUrl());
0101 }
0102 
0103 QTEST_MAIN(LinkPreviewerTest)
0104 #include "linkpreviewertest.moc"