summaryrefslogtreecommitdiffstats
path: root/examples/mailtransportresource/tests/mailtransporttest.cpp
blob: e4cc4479ffc44a2f6ecc8fa448a94b1d781fd903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <QtTest>

#include "tests/testutils.h"
#include "../mailtransport.h"

#include "common/test.h"
#include "common/store.h"
#include "common/resourcecontrol.h"
#include "common/domain/applicationdomaintype.h"
#include "common/log.h"

using namespace Sink;
using namespace Sink::ApplicationDomain;

class MailtransportTest : public QObject
{
    Q_OBJECT

    Sink::ApplicationDomain::SinkResource createResource()
    {
        auto resource = ApplicationDomain::MailtransportResource::create("account1");
        resource.setProperty("server", "localhost");
        // resource.setProperty("port", 993);
        resource.setProperty("user", "doe");
        resource.setProperty("password", "doe");
        resource.setProperty("testmode", true);
        return resource;
    }
    QByteArray mResourceInstanceIdentifier;
    QByteArray mStorageResource;

private slots:

    void initTestCase()
    {
        Test::initTest();
        auto resource = createResource();
        QVERIFY(!resource.identifier().isEmpty());
        VERIFYEXEC(Store::create(resource));
        mResourceInstanceIdentifier = resource.identifier();

        auto dummyResource = ApplicationDomain::DummyResource::create("account1");
        VERIFYEXEC(Store::create(dummyResource));
        mStorageResource = dummyResource.identifier();
        QVERIFY(!mStorageResource.isEmpty());
    }

    void cleanup()
    {
        VERIFYEXEC(Store::removeDataFromDisk(mResourceInstanceIdentifier));
        VERIFYEXEC(Store::removeDataFromDisk(mStorageResource));
    }

    void init()
    {
        VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier));
    }

    void testSendMail()
    {
        auto message = KMime::Message::Ptr::create();
        message->messageID(true)->generate("foo.com");
        message->subject(true)->fromUnicodeString(QString::fromLatin1("send: Foobar"), "utf8");
        message->assemble();

        auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier);
        mail.setMimeMessage(message->encodedContent());

        VERIFYEXEC(Store::create(mail));
        VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));

        //FIXME the email is sent already because changereplay kicks of automatically
        //Ensure the mail is queryable in the outbox
        // 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>());
        // QVERIFY(!mailInOutbox.identifier().isEmpty());

        //Ensure the mail is sent and moved to the sent mail folder on sync
        VERIFYEXEC(Store::synchronize(Query().resourceFilter(mResourceInstanceIdentifier)));
        QTest::qWait(100);
        VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mStorageResource));
        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>());
        //Check that the mail has been moved to the sent mail folder
        QVERIFY(mailInSentMailFolder.getSent());
        QVERIFY(!mailInSentMailFolder.getSubject().isEmpty());
    }

    void testSendFailure()
    {
        auto message = KMime::Message::Ptr::create();
        message->messageID(true)->generate("foo.com");
        message->subject(true)->fromUnicodeString(QString::fromLatin1("error: Foobar"), "utf8");
        message->assemble();

        auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier);
        mail.setMimeMessage(message->encodedContent());

        VERIFYEXEC(Store::create(mail));
        VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));

        //Ensure the mail is queryable in the outbox
        auto mailInOutbox = Store::readOne<ApplicationDomain::Mail>(Query().resourceFilter(mResourceInstanceIdentifier).filter<Mail::Sent>(false));
        QVERIFY(!mailInOutbox.identifier().isEmpty());

        //Modify back to drafts
        auto modifiedMail = mailInOutbox;
        modifiedMail.setDraft(true);
        VERIFYEXEC(Store::modify(modifiedMail));
        VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));

        QTest::qWait(100);
        auto mailsInOutbox = Store::read<ApplicationDomain::Mail>(Query().resourceFilter(mResourceInstanceIdentifier));
        QCOMPARE(mailsInOutbox.size(), 0);

        auto mailsInDrafts = Store::read<ApplicationDomain::Mail>(Query().resourceFilter(mStorageResource));
        QCOMPARE(mailsInDrafts.size(), 1);

    }

};


QTEST_MAIN(MailtransportTest)

#include "mailtransporttest.moc"