File indexing completed on 2024-12-22 05:00:59
0001 /* 0002 SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kmcommandstest.h" 0008 #include "kmcommands.h" 0009 #include "kmkernel.h" 0010 #include <KIdentityManagementCore/Identity> 0011 #include <KIdentityManagementCore/IdentityManager> 0012 0013 #include <Akonadi/Collection> 0014 #include <Akonadi/Item> 0015 0016 #include <KMime/Message> 0017 0018 #include <QEventLoop> 0019 #include <QLabel> 0020 #include <QStandardPaths> 0021 #include <QTemporaryDir> 0022 #include <QTest> 0023 0024 Akonadi::Item createItem(const KIdentityManagementCore::Identity &ident) 0025 { 0026 QByteArray data 0027 = "From: Konqui <konqui@kde.org>\n" 0028 "To: Friends <friends@kde.org>\n" 0029 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0030 "Subject: Sample message\n" 0031 "MIME-Version: 1.0\n" 0032 "X-KMail-Identity: " + QByteArray::number(ident.uoid()) + "\n" 0033 "Content-type: text/plain; charset=us-ascii\n" 0034 "\n" 0035 "\n" 0036 "This is explicitly typed plain US-ASCII text.\n" 0037 "It DOES end with a linebreak.\n" 0038 "\n"; 0039 0040 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0041 Akonadi::Item item; 0042 Akonadi::Collection col(0); 0043 msgPtr->setContent(data); 0044 msgPtr->parse(); 0045 item.setPayload<KMime::Message::Ptr>(msgPtr); 0046 item.setParentCollection(col); 0047 0048 return item; 0049 } 0050 0051 KMCommandsTest::KMCommandsTest(QObject *parent) 0052 : QObject(parent) 0053 , mKernel(new KMKernel(parent)) 0054 { 0055 } 0056 0057 KMCommandsTest::~KMCommandsTest() 0058 { 0059 delete mKernel; 0060 } 0061 0062 void KMCommandsTest::initTestCase() 0063 { 0064 const KIdentityManagementCore::Identity &def = mKernel->identityManager()->defaultIdentity(); 0065 KIdentityManagementCore::Identity &i1 = mKernel->identityManager()->modifyIdentityForUoid(def.uoid()); 0066 i1.setIdentityName(QStringLiteral("default")); 0067 mKernel->identityManager()->newFromScratch(QStringLiteral("test2")); 0068 mKernel->identityManager()->newFromScratch(QStringLiteral("test3")); 0069 mKernel->identityManager()->commit(); 0070 } 0071 0072 void KMCommandsTest::resetIdentities() 0073 { 0074 KIdentityManagementCore::Identity &i1 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("default")); 0075 i1.setFullName(QStringLiteral("default")); 0076 i1.setPrimaryEmailAddress(QStringLiteral("firstname.lastname@example.com")); 0077 i1.setPGPSigningKey("0x123456789"); 0078 i1.setPgpAutoSign(true); 0079 KIdentityManagementCore::Identity &i2 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("test2")); 0080 i2.setFullName(QStringLiteral("second")); 0081 i2.setPrimaryEmailAddress(QStringLiteral("secundus@example.com")); 0082 i2.setPGPSigningKey("0x234567890"); 0083 i2.setPgpAutoSign(false); 0084 KIdentityManagementCore::Identity &i3 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("test3")); 0085 i3.setFullName(QStringLiteral("third")); 0086 i3.setPrimaryEmailAddress(QStringLiteral("drei@example.com")); 0087 i3.setPGPSigningKey("0x345678901"); 0088 i3.setPgpAutoSign(true); 0089 mKernel->identityManager()->commit(); 0090 } 0091 0092 void KMCommandsTest::verifyEncryption(bool encrypt) 0093 { 0094 const KMainWindow *w = mKernel->mainWin(); 0095 auto encryption = w->findChild<QLabel *>(QStringLiteral("encryptionindicator")); 0096 QVERIFY(encryption); 0097 QCOMPARE(encryption->isVisible(), encrypt); 0098 } 0099 0100 void KMCommandsTest::verifySignature(bool sign) 0101 { 0102 const KMainWindow *w = mKernel->mainWin(); 0103 auto signature = w->findChild<QLabel *>(QStringLiteral("signatureindicator")); 0104 QVERIFY(signature); 0105 QCOMPARE(signature->isVisible(), sign); 0106 } 0107 0108 void KMCommandsTest::testMailtoReply() 0109 { 0110 resetIdentities(); 0111 { 0112 // default has auto sign set -> verifySignature = true 0113 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->defaultIdentity(); 0114 Akonadi::Item item(createItem(ident)); 0115 0116 auto cmd(new KMMailtoReplyCommand(nullptr, QUrl(QStringLiteral("mailto:test@example.com")), item, QString())); 0117 cmd->start(); 0118 verifySignature(true); 0119 waitForMainWindowToClose(); 0120 } 0121 { 0122 // secundus has no auto sign set -> verifySignature = false 0123 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("secundus@example.com")); 0124 Akonadi::Item item(createItem(ident)); 0125 0126 auto cmd(new KMMailtoReplyCommand(nullptr, QUrl(QStringLiteral("mailto:test@example.com")), item, QString())); 0127 cmd->start(); 0128 verifySignature(false); 0129 waitForMainWindowToClose(); 0130 } 0131 { 0132 // drei has auto sign set -> verifySignature = true 0133 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("drei@example.com")); 0134 Akonadi::Item item(createItem(ident)); 0135 0136 auto cmd(new KMMailtoReplyCommand(nullptr, QUrl(QStringLiteral("mailto:test@example.com")), item, QString())); 0137 cmd->start(); 0138 verifySignature(true); 0139 waitForMainWindowToClose(); 0140 } 0141 } 0142 0143 void KMCommandsTest::testReply() 0144 { 0145 resetIdentities(); 0146 { 0147 // default has auto sign set -> verifySignature = true 0148 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->defaultIdentity(); 0149 Akonadi::Item item(createItem(ident)); 0150 0151 auto cmd(new KMReplyCommand(nullptr, item, MessageComposer::ReplyAll)); 0152 cmd->start(); 0153 verifySignature(true); 0154 waitForMainWindowToClose(); 0155 } 0156 { 0157 // secundus has no auto sign set -> verifySignature = false 0158 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("secundus@example.com")); 0159 Akonadi::Item item(createItem(ident)); 0160 0161 auto cmd(new KMReplyCommand(nullptr, item, MessageComposer::ReplyAll)); 0162 cmd->start(); 0163 verifySignature(false); 0164 waitForMainWindowToClose(); 0165 } 0166 { 0167 // drei has auto sign set -> verifySignature = true 0168 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("drei@example.com")); 0169 Akonadi::Item item(createItem(ident)); 0170 0171 auto cmd(new KMReplyCommand(nullptr, item, MessageComposer::ReplyAll)); 0172 cmd->start(); 0173 verifySignature(true); 0174 waitForMainWindowToClose(); 0175 } 0176 } 0177 0178 void KMCommandsTest::testReplyWithoutDefaultGPGSign() 0179 { 0180 resetIdentities(); 0181 KIdentityManagementCore::Identity &i1 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("default")); 0182 i1.setPgpAutoSign(false); 0183 mKernel->identityManager()->commit(); 0184 0185 { 0186 // default has auto sign set -> verifySignature = true 0187 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->defaultIdentity(); 0188 Akonadi::Item item(createItem(ident)); 0189 0190 auto cmd(new KMReplyCommand(nullptr, item, MessageComposer::ReplyAll)); 0191 cmd->start(); 0192 verifySignature(false); 0193 waitForMainWindowToClose(); 0194 } 0195 { 0196 // secundus has no auto sign set -> verifySignature = false 0197 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("secundus@example.com")); 0198 Akonadi::Item item(createItem(ident)); 0199 0200 auto cmd(new KMReplyCommand(nullptr, item, MessageComposer::ReplyAll)); 0201 cmd->start(); 0202 verifySignature(false); 0203 waitForMainWindowToClose(); 0204 } 0205 { 0206 // drei has auto sign set -> verifySignature = true 0207 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("drei@example.com")); 0208 Akonadi::Item item(createItem(ident)); 0209 0210 auto cmd(new KMReplyCommand(nullptr, item, MessageComposer::ReplyAll)); 0211 cmd->start(); 0212 verifySignature(true); 0213 waitForMainWindowToClose(); 0214 } 0215 } 0216 0217 void KMCommandsTest::testSendAgain() 0218 { 0219 resetIdentities(); 0220 { 0221 const KIdentityManagementCore::Identity &ident = mKernel->identityManager()->defaultIdentity(); 0222 Akonadi::Item item(createItem(ident)); 0223 0224 auto cmd(new KMResendMessageCommand(nullptr, item)); 0225 cmd->start(); 0226 QVERIFY(!cmd->retrievedMsgs().isEmpty()); 0227 waitForMainWindowToClose(); 0228 } 0229 } 0230 0231 void KMCommandsTest::waitForMainWindowToClose() 0232 { 0233 KMainWindow *w = mKernel->mainWin(); 0234 QEventLoop loop; 0235 loop.connect(w, &QMainWindow::destroyed, &loop, &QEventLoop::quit); 0236 w->close(); 0237 loop.exec(); 0238 } 0239 0240 int main(int argc, char *argv[]) 0241 { 0242 QTemporaryDir config; 0243 qputenv("LC_ALL", "C"); 0244 qputenv("XDG_CONFIG_HOME", config.path().toUtf8()); 0245 QApplication app(argc, argv); 0246 app.setAttribute(Qt::AA_Use96Dpi, true); 0247 QTEST_DISABLE_KEYPAD_NAVIGATION 0248 KMCommandsTest tc; 0249 return QTest::qExec(&tc, argc, argv); 0250 } 0251 0252 #include "moc_kmcommandstest.cpp"