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 "filteractionrewriteheadertest.h" 0008 #include "../filteractions/filteractionrewriteheader.h" 0009 #include <KLineEdit> 0010 #include <QComboBox> 0011 #include <QLabel> 0012 #include <QTest> 0013 #include <QWidget> 0014 0015 FilterActionRewriteHeaderTest::FilterActionRewriteHeaderTest(QObject *parent) 0016 : QObject(parent) 0017 { 0018 } 0019 0020 FilterActionRewriteHeaderTest::~FilterActionRewriteHeaderTest() = default; 0021 0022 void FilterActionRewriteHeaderTest::shouldHaveDefaultValue() 0023 { 0024 MailCommon::FilterActionRewriteHeader filter; 0025 QWidget *w = filter.createParamWidget(nullptr); 0026 auto combo = w->findChild<QComboBox *>(QStringLiteral("combo")); 0027 QVERIFY(combo); 0028 QVERIFY(combo->count() > 0); 0029 0030 auto label = w->findChild<QLabel *>(QStringLiteral("label_replace")); 0031 QVERIFY(label); 0032 0033 auto regExpLineEdit = w->findChild<KLineEdit *>(QStringLiteral("search")); 0034 QVERIFY(regExpLineEdit); 0035 QVERIFY(regExpLineEdit->text().isEmpty()); 0036 0037 label = w->findChild<QLabel *>(QStringLiteral("label_with")); 0038 QVERIFY(label); 0039 0040 auto lineEdit = w->findChild<KLineEdit *>(QStringLiteral("replace")); 0041 QVERIFY(lineEdit); 0042 QVERIFY(lineEdit->text().isEmpty()); 0043 } 0044 0045 void FilterActionRewriteHeaderTest::shouldBeEmpty() 0046 { 0047 MailCommon::FilterActionRewriteHeader filter; 0048 QVERIFY(filter.isEmpty()); 0049 filter.argsFromString(QStringLiteral("foo\tbla")); 0050 QVERIFY(filter.isEmpty()); 0051 filter.argsFromString(QStringLiteral("foo\tbla\tkde")); 0052 QVERIFY(!filter.isEmpty()); 0053 } 0054 0055 void FilterActionRewriteHeaderTest::shouldNotExecuteActionWhenParameterIsEmpty() 0056 { 0057 MailCommon::FilterActionRewriteHeader filter(this); 0058 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0059 Akonadi::Item item; 0060 item.setPayload<KMime::Message::Ptr>(msgPtr); 0061 MailCommon::ItemContext context(item, true); 0062 0063 filter.argsFromString(QString()); 0064 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::ErrorButGoOn); 0065 QCOMPARE(context.needsPayloadStore(), false); 0066 } 0067 0068 void FilterActionRewriteHeaderTest::shouldNotExecuteActionWhenValueIsEmpty() 0069 { 0070 MailCommon::FilterActionRewriteHeader filter(this); 0071 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0072 Akonadi::Item item; 0073 item.setPayload<KMime::Message::Ptr>(msgPtr); 0074 MailCommon::ItemContext context(item, true); 0075 0076 filter.argsFromString(QStringLiteral("foo")); 0077 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::ErrorButGoOn); 0078 QCOMPARE(context.needsPayloadStore(), false); 0079 0080 filter.argsFromString(QStringLiteral("foo\tbla")); 0081 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::ErrorButGoOn); 0082 QCOMPARE(context.needsPayloadStore(), false); 0083 } 0084 0085 void FilterActionRewriteHeaderTest::shouldRewriteHeader() 0086 { 0087 const QByteArray data = 0088 "From: foo@kde.org\n" 0089 "To: foo@kde.org\n" 0090 "Subject: test\n" 0091 "testheader: foo\n" 0092 "Date: Wed, 01 Apr 2015 09:33:01 +0200\n" 0093 "MIME-Version: 1.0\n" 0094 "Content-Type: text/plain; charset=\"us-ascii\"\n" 0095 "\n" 0096 "test"; 0097 const QByteArray output = 0098 "From: foo@kde.org\n" 0099 "To: foo@kde.org\n" 0100 "Subject: test\n" 0101 "Date: Wed, 01 Apr 2015 09:33:01 +0200\n" 0102 "MIME-Version: 1.0\n" 0103 "Content-Type: text/plain; charset=\"us-ascii\"\n" 0104 "testheader: bla\n" 0105 "\n" 0106 "test"; 0107 0108 MailCommon::FilterActionRewriteHeader filter(this); 0109 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0110 msgPtr->setContent(data); 0111 msgPtr->parse(); 0112 Akonadi::Item item; 0113 item.setPayload<KMime::Message::Ptr>(msgPtr); 0114 MailCommon::ItemContext context(item, true); 0115 0116 filter.argsFromString(QStringLiteral("testheader\tfoo\tbla")); 0117 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::GoOn); 0118 QCOMPARE(context.needsPayloadStore(), true); 0119 QCOMPARE(msgPtr->encodedContent(), output); 0120 } 0121 0122 void FilterActionRewriteHeaderTest::shouldNotRewriteHeaderWhenHeaderNotFound() 0123 { 0124 const QByteArray data = 0125 "From: foo@kde.org\n" 0126 "To: foo@kde.org\n" 0127 "Subject: test\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 0134 MailCommon::FilterActionRewriteHeader filter(this); 0135 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0136 msgPtr->setContent(data); 0137 msgPtr->parse(); 0138 Akonadi::Item item; 0139 item.setPayload<KMime::Message::Ptr>(msgPtr); 0140 MailCommon::ItemContext context(item, true); 0141 0142 filter.argsFromString(QStringLiteral("testheader\tfoo\tbla")); 0143 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::GoOn); 0144 QCOMPARE(context.needsPayloadStore(), false); 0145 QCOMPARE(msgPtr->encodedContent(), data); 0146 } 0147 0148 void FilterActionRewriteHeaderTest::shouldNotRewriteHeaderWhenRegexpNotFound() 0149 { 0150 const QByteArray data = 0151 "From: foo@kde.org\n" 0152 "To: foo@kde.org\n" 0153 "Subject: test\n" 0154 "Date: Wed, 01 Apr 2015 09:33:01 +0200\n" 0155 "MIME-Version: 1.0\n" 0156 "Content-Type: text/plain; charset=\"us-ascii\"\n" 0157 "testheader: bla\n" 0158 "\n" 0159 "test"; 0160 0161 MailCommon::FilterActionRewriteHeader filter(this); 0162 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0163 msgPtr->setContent(data); 0164 msgPtr->parse(); 0165 Akonadi::Item item; 0166 item.setPayload<KMime::Message::Ptr>(msgPtr); 0167 MailCommon::ItemContext context(item, true); 0168 0169 filter.argsFromString(QStringLiteral("testheader\tfoo\tbla")); 0170 QCOMPARE(filter.process(context, false), MailCommon::FilterAction::GoOn); 0171 QCOMPARE(context.needsPayloadStore(), false); 0172 QCOMPARE(msgPtr->encodedContent(), data); 0173 } 0174 0175 void FilterActionRewriteHeaderTest::shouldHaveRequiredPart() 0176 { 0177 MailCommon::FilterActionRewriteHeader filter; 0178 QCOMPARE(filter.requiredPart(), MailCommon::SearchRule::CompleteMessage); 0179 } 0180 0181 QTEST_MAIN(FilterActionRewriteHeaderTest) 0182 0183 #include "moc_filteractionrewriteheadertest.cpp"