File indexing completed on 2024-11-10 04:50:00

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "filteractionremoveheadertest.h"
0008 #include "../filteractions/filteractionremoveheader.h"
0009 #include <QComboBox>
0010 #include <QTest>
0011 
0012 FilterActionRemoveHeaderTest::FilterActionRemoveHeaderTest(QObject *parent)
0013     : QObject(parent)
0014 {
0015 }
0016 
0017 FilterActionRemoveHeaderTest::~FilterActionRemoveHeaderTest() = default;
0018 
0019 void FilterActionRemoveHeaderTest::shouldHaveDefaultValue()
0020 {
0021     MailCommon::FilterActionRemoveHeader filter;
0022     QWidget *w = filter.createParamWidget(nullptr);
0023     const auto comboBox = dynamic_cast<QComboBox *>(w);
0024     QVERIFY(comboBox);
0025     QVERIFY(comboBox->isEditable());
0026     QVERIFY(comboBox->count() > 0);
0027 }
0028 
0029 void FilterActionRemoveHeaderTest::shouldHaveSieveRequires()
0030 {
0031     MailCommon::FilterActionRemoveHeader filter;
0032     QCOMPARE(filter.sieveRequires(), QStringList() << QStringLiteral("editheader"));
0033 }
0034 
0035 void FilterActionRemoveHeaderTest::shouldBeEmpty()
0036 {
0037     MailCommon::FilterActionRemoveHeader filter;
0038     QVERIFY(filter.isEmpty());
0039     filter.argsFromString(QStringLiteral("bla"));
0040     QVERIFY(!filter.isEmpty());
0041 }
0042 
0043 void FilterActionRemoveHeaderTest::shouldNotExecuteActionWhenParameterIsEmpty()
0044 {
0045     MailCommon::FilterActionRemoveHeader filter(this);
0046     KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
0047     Akonadi::Item item;
0048     item.setPayload<KMime::Message::Ptr>(msgPtr);
0049     MailCommon::ItemContext context(item, true);
0050 
0051     filter.argsFromString(QString());
0052     QCOMPARE(filter.process(context, false), MailCommon::FilterAction::ErrorButGoOn);
0053     QCOMPARE(context.needsPayloadStore(), false);
0054 }
0055 
0056 void FilterActionRemoveHeaderTest::shouldRemoveHeader()
0057 {
0058     const QByteArray data =
0059         "From: foo@kde.org\n"
0060         "To: foo@kde.org\n"
0061         "Subject: test\n"
0062         "testheader: foo\n"
0063         "Date: Wed, 01 Apr 2015 09:33:01 +0200\n"
0064         "MIME-Version: 1.0\n"
0065         "Content-Type: text/plain; charset=\"us-ascii\"\n"
0066         "\n"
0067         "test";
0068     const QByteArray output =
0069         "From: foo@kde.org\n"
0070         "To: foo@kde.org\n"
0071         "Subject: test\n"
0072         "Date: Wed, 01 Apr 2015 09:33:01 +0200\n"
0073         "MIME-Version: 1.0\n"
0074         "Content-Type: text/plain; charset=\"us-ascii\"\n"
0075         "\n"
0076         "test";
0077 
0078     MailCommon::FilterActionRemoveHeader filter(this);
0079     KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
0080     msgPtr->setContent(data);
0081     msgPtr->parse();
0082     Akonadi::Item item;
0083     item.setPayload<KMime::Message::Ptr>(msgPtr);
0084     MailCommon::ItemContext context(item, true);
0085 
0086     filter.argsFromString(QStringLiteral("testheader"));
0087     QCOMPARE(filter.process(context, false), MailCommon::FilterAction::GoOn);
0088     QCOMPARE(context.needsPayloadStore(), true);
0089     QCOMPARE(msgPtr->encodedContent(), output);
0090 }
0091 
0092 void FilterActionRemoveHeaderTest::shouldNotTryToRemoveHeaderWhenItDoesntExist()
0093 {
0094     const QByteArray data =
0095         "From: foo@kde.org\n"
0096         "To: foo@kde.org\n"
0097         "Subject: test\n"
0098         "testheader2: foo\n"
0099         "Date: Wed, 01 Apr 2015 09:33:01 +0200\n"
0100         "MIME-Version: 1.0\n"
0101         "Content-Type: text/plain; charset=\"us-ascii\"\n"
0102         "\n"
0103         "test";
0104 
0105     MailCommon::FilterActionRemoveHeader filter(this);
0106     KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
0107     msgPtr->setContent(data);
0108     msgPtr->parse();
0109     Akonadi::Item item;
0110     item.setPayload<KMime::Message::Ptr>(msgPtr);
0111     MailCommon::ItemContext context(item, true);
0112 
0113     filter.argsFromString(QStringLiteral("testheader"));
0114     QCOMPARE(filter.process(context, false), MailCommon::FilterAction::GoOn);
0115     QCOMPARE(context.needsPayloadStore(), false);
0116     QCOMPARE(msgPtr->encodedContent(), data);
0117 }
0118 
0119 void FilterActionRemoveHeaderTest::shouldRemoveMultiHeader()
0120 {
0121     const QByteArray data =
0122         "From: foo@kde.org\n"
0123         "To: foo@kde.org\n"
0124         "Subject: test\n"
0125         "testheader: foo\n"
0126         "testheader: foo\n"
0127         "testheader: bla\n"
0128         "Date: Wed, 01 Apr 2015 09:33:01 +0200\n"
0129         "MIME-Version: 1.0\n"
0130         "Content-Type: text/plain; charset=\"us-ascii\"\n"
0131         "\n"
0132         "test";
0133     const QByteArray output =
0134         "From: foo@kde.org\n"
0135         "To: foo@kde.org\n"
0136         "Subject: test\n"
0137         "Date: Wed, 01 Apr 2015 09:33:01 +0200\n"
0138         "MIME-Version: 1.0\n"
0139         "Content-Type: text/plain; charset=\"us-ascii\"\n"
0140         "\n"
0141         "test";
0142 
0143     MailCommon::FilterActionRemoveHeader filter(this);
0144     KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
0145     msgPtr->setContent(data);
0146     msgPtr->parse();
0147     Akonadi::Item item;
0148     item.setPayload<KMime::Message::Ptr>(msgPtr);
0149     MailCommon::ItemContext context(item, true);
0150 
0151     filter.argsFromString(QStringLiteral("testheader"));
0152     QCOMPARE(filter.process(context, false), MailCommon::FilterAction::GoOn);
0153     QCOMPARE(context.needsPayloadStore(), true);
0154     QCOMPARE(msgPtr->encodedContent(), output);
0155 }
0156 
0157 void FilterActionRemoveHeaderTest::shouldHaveRequiredPart()
0158 {
0159     MailCommon::FilterActionRemoveHeader filter;
0160     QCOMPARE(filter.requiredPart(), MailCommon::SearchRule::CompleteMessage);
0161 }
0162 
0163 QTEST_MAIN(FilterActionRemoveHeaderTest)
0164 
0165 #include "moc_filteractionremoveheadertest.cpp"