File indexing completed on 2025-01-05 04:58:40

0001 #include <QTest>
0002 
0003 #include "../mailtransport.h"
0004 
0005 #include "common/test.h"
0006 #include "common/store.h"
0007 #include "common/resourcecontrol.h"
0008 #include "common/domain/applicationdomaintype.h"
0009 #include "common/log.h"
0010 #include "common/secretstore.h"
0011 
0012 using namespace Sink;
0013 using namespace Sink::ApplicationDomain;
0014 
0015 class MailtransportTest : public QObject
0016 {
0017     Q_OBJECT
0018 
0019     Sink::ApplicationDomain::SinkResource createResource()
0020     {
0021         auto resource = ApplicationDomain::MailtransportResource::create("account1");
0022         resource.setProperty("server", "localhost");
0023         // resource.setProperty("port", 993);
0024         resource.setProperty("user", "doe");
0025         Sink::SecretStore::instance().insert(resource.identifier(), "doe");
0026         resource.setProperty("testmode", true);
0027         return resource;
0028     }
0029     QByteArray mResourceInstanceIdentifier;
0030     QByteArray mStorageResource;
0031 
0032 private slots:
0033 
0034     void initTestCase()
0035     {
0036         Test::initTest();
0037         auto resource = createResource();
0038         QVERIFY(!resource.identifier().isEmpty());
0039         VERIFYEXEC(Store::create(resource));
0040         mResourceInstanceIdentifier = resource.identifier();
0041 
0042         auto dummyResource = ApplicationDomain::DummyResource::create("account1");
0043         VERIFYEXEC(Store::create(dummyResource));
0044         mStorageResource = dummyResource.identifier();
0045         QVERIFY(!mStorageResource.isEmpty());
0046     }
0047 
0048     void cleanup()
0049     {
0050         VERIFYEXEC(Store::removeDataFromDisk(mResourceInstanceIdentifier));
0051         VERIFYEXEC(Store::removeDataFromDisk(mStorageResource));
0052     }
0053 
0054     void init()
0055     {
0056         VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier));
0057     }
0058 
0059     void testSendMail()
0060     {
0061         auto message = KMime::Message::Ptr::create();
0062         message->messageID(true)->generate("foo.com");
0063         message->subject(true)->fromUnicodeString(QString::fromLatin1("send: Foobar"), "utf8");
0064         message->assemble();
0065 
0066         auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier);
0067         mail.setMimeMessage(message->encodedContent(true));
0068 
0069         VERIFYEXEC(Store::create(mail));
0070         VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
0071 
0072         //FIXME the email is sent already because changereplay kicks of automatically
0073         //Ensure the mail is queryable in the outbox
0074         // auto mailInOutbox = Store::readOne<ApplicationDomain::Mail>(Query().resourceFilter(mResourceInstanceIdentifier).filter<Mail::Sent>(false).request<Mail::Subject>().request<Mail::Folder>().request<Mail::MimeMessage>().request<Mail::Sent>());
0075         // QVERIFY(!mailInOutbox.identifier().isEmpty());
0076 
0077         //Ensure the mail is sent and moved to the sent mail folder on sync
0078         VERIFYEXEC(Store::synchronize(Query().resourceFilter(mResourceInstanceIdentifier)));
0079         QTest::qWait(100);
0080         VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mStorageResource));
0081         auto mailInSentMailFolder = Store::readOne<ApplicationDomain::Mail>(Query().resourceFilter(mStorageResource).filter<Mail::Sent>(true).request<Mail::Subject>().request<Mail::Folder>().request<Mail::MimeMessage>().request<Mail::Sent>());
0082         //Check that the mail has been moved to the sent mail folder
0083         QVERIFY(mailInSentMailFolder.getSent());
0084         QVERIFY(!mailInSentMailFolder.getSubject().isEmpty());
0085     }
0086 
0087     void testSendFailure()
0088     {
0089         auto message = KMime::Message::Ptr::create();
0090         message->messageID(true)->generate("foo.com");
0091         message->subject(true)->fromUnicodeString(QString::fromLatin1("error: Foobar"), "utf8");
0092         message->assemble();
0093 
0094         auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier);
0095         mail.setMimeMessage(message->encodedContent(true));
0096 
0097         VERIFYEXEC(Store::create(mail));
0098         VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
0099 
0100         //Ensure the mail is queryable in the outbox
0101         auto mailInOutbox = Store::readOne<ApplicationDomain::Mail>(Query().resourceFilter(mResourceInstanceIdentifier).filter<Mail::Sent>(false));
0102         QVERIFY(!mailInOutbox.identifier().isEmpty());
0103 
0104         //Modify back to drafts
0105         auto modifiedMail = mailInOutbox;
0106         modifiedMail.setDraft(true);
0107         VERIFYEXEC(Store::modify(modifiedMail));
0108         VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
0109 
0110         QTRY_COMPARE(Store::read<ApplicationDomain::Mail>(Query().resourceFilter(mStorageResource)).size(), 1);
0111         QTRY_COMPARE(Store::read<ApplicationDomain::Mail>(Query().resourceFilter(mResourceInstanceIdentifier)).size(), 0);
0112     }
0113 
0114 };
0115 
0116 
0117 QTEST_MAIN(MailtransportTest)
0118 
0119 #include "mailtransporttest.moc"