File indexing completed on 2024-11-24 04:44:00

0001 /*
0002    SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003    SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
0004 
0005    SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "imaptestbase.h"
0009 
0010 #include "additemtask.h"
0011 
0012 #include "uidnextattribute.h"
0013 
0014 #include <KMime/Message>
0015 
0016 #include <QTest>
0017 
0018 class TestAddItemTask : public ImapTestBase
0019 {
0020     Q_OBJECT
0021 
0022 private Q_SLOTS:
0023     void shouldAppendMessage_data()
0024     {
0025         QTest::addColumn<Akonadi::Item>("item");
0026         QTest::addColumn<Akonadi::Collection>("collection");
0027         QTest::addColumn<QList<QByteArray>>("scenario");
0028         QTest::addColumn<QStringList>("callNames");
0029 
0030         Akonadi::Collection collection;
0031         Akonadi::Item item;
0032         QString messageContent;
0033         QList<QByteArray> scenario;
0034         QStringList callNames;
0035 
0036         collection = createCollectionChain(QStringLiteral("/INBOX/Foo"));
0037         auto uidNext = new UidNextAttribute;
0038         uidNext->setUidNext(63);
0039         collection.addAttribute(uidNext);
0040 
0041         item = Akonadi::Item(2);
0042         item.setParentCollection(collection);
0043 
0044         KMime::Message::Ptr message(new KMime::Message);
0045 
0046         messageContent = QStringLiteral("From: ervin\nTo: someone\nSubject: foo\n\nSpeechless...");
0047 
0048         message->setContent(messageContent.toUtf8());
0049         message->parse();
0050         item.setPayload(message);
0051 
0052         scenario.clear();
0053         scenario << defaultPoolConnectionScenario() << "C: A000003 APPEND \"INBOX/Foo\" {55}\r\n" + message->encodedContent(true)
0054                  << "S: A000003 OK append done [ APPENDUID 1239890035 66 ]";
0055 
0056         callNames.clear();
0057         callNames << QStringLiteral("itemChangeCommitted");
0058 
0059         QTest::newRow("trivial case") << item << collection << scenario << callNames;
0060 
0061         message = KMime::Message::Ptr(new KMime::Message);
0062 
0063         messageContent = QStringLiteral("From: ervin\nTo: someone\nSubject: foo\nMessage-ID: <42.4242.foo@bar.org>\n\nSpeechless...");
0064 
0065         message->setContent(messageContent.toUtf8());
0066         message->parse();
0067         item.setPayload(message);
0068 
0069         scenario.clear();
0070         scenario << defaultPoolConnectionScenario() << "C: A000003 APPEND \"INBOX/Foo\" {90}\r\n" + message->encodedContent(true) << "S: A000003 OK append done"
0071                  << "C: A000004 SELECT \"INBOX/Foo\""
0072                  << "S: A000004 OK select done"
0073                  << "C: A000005 UID SEARCH HEADER Message-ID \"<42.4242.foo@bar.org>\""
0074                  << "S: * SEARCH 66"
0075                  << "S: A000005 OK search done";
0076 
0077         callNames.clear();
0078         callNames << QStringLiteral("itemChangeCommitted");
0079 
0080         QTest::newRow("no APPENDUID, message contained Message-ID") << item << collection << scenario << callNames;
0081 
0082         scenario.clear();
0083         scenario << defaultPoolConnectionScenario() << "C: A000003 APPEND \"INBOX/Foo\" {90}\r\n" + message->encodedContent(true) << "S: A000003 OK append done"
0084                  << "C: A000004 SELECT \"INBOX/Foo\""
0085                  << "S: A000004 OK select done"
0086                  << "C: A000005 UID SEARCH HEADER Message-ID \"<42.4242.foo@bar.org>\""
0087                  << "S: * SEARCH 65 66"
0088                  << "S: A000005 OK search done";
0089         callNames.clear();
0090         callNames << QStringLiteral("itemChangeCommitted");
0091         QTest::newRow("no APPENDUID, message contained non-unique Message-ID") << item << collection << scenario << callNames;
0092 
0093         message = KMime::Message::Ptr(new KMime::Message);
0094 
0095         messageContent = QStringLiteral("From: ervin\nTo: someone\nSubject: foo\n\nSpeechless...");
0096 
0097         message->setContent(messageContent.toUtf8());
0098         message->parse();
0099         item.setPayload(message);
0100 
0101         scenario.clear();
0102         scenario << defaultPoolConnectionScenario() << "C: A000003 APPEND \"INBOX/Foo\" {55}\r\n" + message->encodedContent(true) << "S: A000003 OK append done"
0103                  << "C: A000004 SELECT \"INBOX/Foo\""
0104                  << "S: A000004 OK select done"
0105                  << "C: A000005 UID SEARCH NEW UID 63:*"
0106                  << "S: * SEARCH 66"
0107                  << "S: A000005 OK search done";
0108 
0109         callNames.clear();
0110         callNames << QStringLiteral("itemChangeCommitted");
0111 
0112         QTest::newRow("no APPENDUID, message didn't contain Message-ID") << item << collection << scenario << callNames;
0113     }
0114 
0115     void shouldAppendMessage()
0116     {
0117         QFETCH(Akonadi::Item, item);
0118         QFETCH(Akonadi::Collection, collection);
0119         QFETCH(QList<QByteArray>, scenario);
0120         QFETCH(QStringList, callNames);
0121 
0122         FakeServer server;
0123         server.setScenario(scenario);
0124         server.startAndWait();
0125 
0126         SessionPool pool(1);
0127 
0128         pool.setPasswordRequester(createDefaultRequester());
0129         QVERIFY(pool.connect(createDefaultAccount()));
0130         QVERIFY(waitForSignal(&pool, SIGNAL(connectDone(int, QString))));
0131 
0132         DummyResourceState::Ptr state = DummyResourceState::Ptr(new DummyResourceState);
0133         state->setCollection(collection);
0134         state->setItem(item);
0135         auto task = new AddItemTask(state);
0136         task->start(&pool);
0137 
0138         QTRY_COMPARE(state->calls().count(), callNames.size());
0139         for (int i = 0; i < callNames.size(); i++) {
0140             QString command = QString::fromUtf8(state->calls().at(i).first);
0141             QVariant parameter = state->calls().at(i).second;
0142 
0143             if (command == QLatin1StringView("cancelTask") && callNames[i] != QLatin1StringView("cancelTask")) {
0144                 qDebug() << "Got a cancel:" << parameter.toString();
0145             }
0146 
0147             QCOMPARE(command, callNames[i]);
0148 
0149             if (command == QLatin1StringView("cancelTask")) {
0150                 QVERIFY(!parameter.toString().isEmpty());
0151             }
0152         }
0153 
0154         QVERIFY(server.isAllScenarioDone());
0155 
0156         server.quit();
0157     }
0158 };
0159 
0160 QTEST_GUILESS_MAIN(TestAddItemTask)
0161 
0162 #include "testadditemtask.moc"