File indexing completed on 2024-12-22 05:05:17

0001 // SPDX-FileCopyrightText: 2023 g10 Code GmbH
0002 // SPDX-FileContributor: Carl Schwan <carl.schwan@gnupg.com>
0003 // SPDX-License-Identifier: LGPL-2.0-or-later
0004 
0005 #include <QTest>
0006 
0007 #include "attachmentmodel.h"
0008 #include "messageparser.h"
0009 
0010 #include <QAbstractItemModelTester>
0011 #include <QSignalSpy>
0012 #include <QTemporaryFile>
0013 
0014 KMime::Message::Ptr readMailFromFile(const QString &mailFile)
0015 {
0016     QFile file(QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + mailFile);
0017     file.open(QIODevice::ReadOnly);
0018     Q_ASSERT(file.isOpen());
0019     auto mailData = KMime::CRLFtoLF(file.readAll());
0020     KMime::Message::Ptr message(new KMime::Message);
0021     message->setContent(mailData);
0022     message->parse();
0023     return message;
0024 }
0025 
0026 class AttachmentModelTest : public QObject
0027 {
0028     Q_OBJECT
0029 
0030 private Q_SLOTS:
0031 
0032     void openMailWithOneAttachementTest()
0033     {
0034         MessageParser messageParser;
0035         messageParser.setMessage(readMailFromFile(QLatin1StringView("attachment.mbox")));
0036 
0037         auto attachmentModel = messageParser.attachments();
0038         new QAbstractItemModelTester(attachmentModel);
0039 
0040         QCOMPARE(attachmentModel->rowCount(), 1);
0041         QCOMPARE(attachmentModel->data(attachmentModel->index(0, 0), AttachmentModel::TypeRole).toString(), QStringLiteral("image/jpeg"));
0042         QCOMPARE(attachmentModel->data(attachmentModel->index(0, 0), AttachmentModel::NameRole).toString(), QStringLiteral("aqnaozisxya.jpeg"));
0043         QCOMPARE(attachmentModel->data(attachmentModel->index(0, 0), AttachmentModel::SizeRole).toString(), QStringLiteral("100.22 KB"));
0044         QCOMPARE(attachmentModel->data(attachmentModel->index(0, 0), AttachmentModel::IsEncryptedRole).toBool(), false);
0045         QCOMPARE(attachmentModel->data(attachmentModel->index(0, 0), AttachmentModel::IsSignedRole).toBool(), false);
0046         QCOMPARE(attachmentModel->data(attachmentModel->index(0, AttachmentModel::IsEncryptedColumn), Qt::CheckStateRole).value<Qt::CheckState>(),
0047                  Qt::Unchecked);
0048         QCOMPARE(attachmentModel->data(attachmentModel->index(0, AttachmentModel::IsSignedColumn), Qt::CheckStateRole).value<Qt::CheckState>(), Qt::Unchecked);
0049         QCOMPARE(attachmentModel->data(attachmentModel->index(0, AttachmentModel::SizeColumn), Qt::DisplayRole).toString(), QStringLiteral("100.22 KB"));
0050     }
0051 
0052     void saveTest()
0053     {
0054         MessageParser messageParser;
0055         messageParser.setMessage(readMailFromFile(QLatin1StringView("attachment.mbox")));
0056 
0057         auto attachmentModel = messageParser.attachments();
0058         QTemporaryFile file;
0059         QVERIFY(file.open());
0060         const auto fileName = attachmentModel->saveAttachmentToPath(0, file.fileName());
0061         QFile file2(fileName);
0062         QVERIFY(file2.open(QIODevice::ReadOnly | QIODevice::Text));
0063         QVERIFY(!file2.readAll().isEmpty());
0064     }
0065 
0066     void openTest()
0067     {
0068         MessageParser messageParser;
0069         messageParser.setMessage(readMailFromFile(QLatin1StringView("attachment.mbox")));
0070 
0071         auto attachmentModel = messageParser.attachments();
0072         QSignalSpy spy(attachmentModel, &AttachmentModel::errorOccurred);
0073         QVERIFY(spy.isValid());
0074 
0075         attachmentModel->openAttachment(0);
0076 
0077         // Check no error occurred
0078         QCOMPARE(spy.count(), 0);
0079     }
0080 
0081     void saveInvalidPathTest()
0082     {
0083         MessageParser messageParser;
0084         messageParser.setMessage(readMailFromFile(QLatin1StringView("attachment.mbox")));
0085 
0086         auto attachmentModel = messageParser.attachments();
0087         QSignalSpy spy(attachmentModel, &AttachmentModel::errorOccurred);
0088         QVERIFY(spy.isValid());
0089 
0090         const auto fileName = attachmentModel->saveAttachmentToPath(0, QStringLiteral("/does/not/exist"));
0091         QList<QVariant> arguments = spy.takeFirst();
0092         QVERIFY(arguments.at(0).userType() == QMetaType::QString);
0093     }
0094 };
0095 
0096 QTEST_MAIN(AttachmentModelTest)
0097 #include "attachmentmodeltest.moc"