File indexing completed on 2024-05-26 05:28:44

0001 /* Copyright (C) 2006 - 2017 Jan Kundrát <jkt@kde.org>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include <QtTest>
0024 #include "test_Composer_Existing.h"
0025 #include "Utils/FakeCapabilitiesInjector.h"
0026 #include "Imap/data.h"
0027 #include "Imap/Model/ItemRoles.h"
0028 #include "Streams/FakeSocket.h"
0029 
0030 ComposerExistingTest::ComposerExistingTest()
0031 {
0032 }
0033 
0034 void ComposerExistingTest::init()
0035 {
0036     LibMailboxSync::init();
0037 
0038     // There's a default delay of 50ms which made the cEmpty() and justKeepTask() ignore these pending actions...
0039     model->setProperty("trojita-imap-delayed-fetch-part", QVariant(0u));
0040 
0041     existsA = 5;
0042     uidValidityA = 333666;
0043     uidMapA << 10 << 11 << 12 << 13 << 14;
0044     uidNextA = 15;
0045     helperSyncAWithMessagesEmptyState();
0046 
0047     QCOMPARE(msgListA.model()->index(0, 0, msgListA).data(Imap::Mailbox::RoleMessageSubject), QVariant());
0048     cClient(t.mk("UID FETCH 10:14 (" FETCH_METADATA_ITEMS ")\r\n"));
0049     cServer("* 1 FETCH (BODYSTRUCTURE "
0050             "(\"text\" \"plain\" (\"charset\" \"UTF-8\" \"format\" \"flowed\") NIL NIL \"8bit\" 362 15 NIL NIL NIL)"
0051             " ENVELOPE (NIL \"subj\" NIL NIL NIL NIL NIL NIL NIL \"<msgid>\")"
0052             ")\r\n" +
0053             t.last("OK fetched\r\n"));
0054 }
0055 
0056 /** @short Not requesting anything should not result in network activity */
0057 void ComposerExistingTest::testDoNothing()
0058 {
0059     Composer::ExistingMessageComposer composer(msgListA.model()->index(0, 0, msgListA));
0060     QVERIFY(!composer.isReadyForSerialization());
0061     cEmpty();
0062 }
0063 
0064 /** @short A successful and simple composing */
0065 void ComposerExistingTest::testSimpleCompose()
0066 {
0067     Composer::ExistingMessageComposer composer(msgListA.model()->index(0, 0, msgListA));
0068     QVERIFY(!composer.isReadyForSerialization());
0069     QString errorMessage;
0070     QByteArray data;
0071     QBuffer buf(&data);
0072     buf.open(QIODevice::WriteOnly);
0073     QCOMPARE(composer.asRawMessage(&buf, &errorMessage), false);
0074     QVERIFY(data.isEmpty());
0075     cClientRegExp(t.mk("UID FETCH 10 \\(BODY\\.PEEK\\["
0076                        "("
0077                        "TEXT\\] BODY\\.PEEK\\[HEADER"
0078                        "|"
0079                        "HEADER\\] BODY\\.PEEK\\[TEXT"
0080                        ")"
0081                        "\\]\\)"));
0082     QByteArray headers("Date: Wed, 12 Apr 2017 09:28:35 +0200\r\n\r\n");
0083     QByteArray body("this is the body!");
0084     cServer("* 1 FETCH (BODY[TEXT] {" + QByteArray::number(body.size()) + "}\r\n" + body + ")\r\n");
0085     cServer("* 1 FETCH (BODY[HEADER] {" + QByteArray::number(headers.size()) + "}\r\n" + headers + ")\r\n");
0086     cServer(t.last("OK fetched\r\n"));
0087     QCOMPARE(composer.asRawMessage(&buf, &errorMessage), true);
0088     QVERIFY(data.startsWith("Resent-Date: "));
0089     QVERIFY(data.contains("Resent-Message-ID: <"));
0090     QVERIFY(data.endsWith("\r\n" + headers + body));
0091     QCOMPARE(errorMessage, QString());
0092     QVERIFY(composer.isReadyForSerialization());
0093     cEmpty();
0094 }
0095 
0096 void ComposerExistingTest::testSimpleNo()
0097 {
0098     Composer::ExistingMessageComposer composer(msgListA.model()->index(0, 0, msgListA));
0099     QString errorMessage;
0100     QByteArray data;
0101     QBuffer buf(&data);
0102     buf.open(QIODevice::WriteOnly);
0103     QCOMPARE(composer.asRawMessage(&buf, &errorMessage), false);
0104     QVERIFY(data.isEmpty());
0105     cClientRegExp(t.mk("UID FETCH 10 \\(BODY\\.PEEK\\["
0106                        "("
0107                        "TEXT\\] BODY\\.PEEK\\[HEADER"
0108                        "|"
0109                        "HEADER\\] BODY\\.PEEK\\[TEXT"
0110                        ")"
0111                        "\\]\\)"));
0112     cServer(t.last("NO go away\r\n"));
0113     QCOMPARE(composer.asRawMessage(&buf, &errorMessage), false);
0114     QCOMPARE(data, QByteArray());
0115     QCOMPARE(errorMessage, QStringLiteral("Offline mode: uncached message data not available"));
0116     QVERIFY(!composer.isReadyForSerialization());
0117     cEmpty();
0118 }
0119 
0120 void ComposerExistingTest::testCatenate()
0121 {
0122     FakeCapabilitiesInjector injector(model);
0123     injector.injectCapability(QStringLiteral("CATENATE"));
0124     Composer::ExistingMessageComposer composer(msgListA.model()->index(0, 0, msgListA));
0125     QString errorMessage;
0126     QVERIFY(!composer.isReadyForSerialization());
0127     QList<Imap::Mailbox::CatenatePair> data;
0128     QVERIFY(composer.asCatenateData(data, &errorMessage));
0129     QCOMPARE(data.size(), 2);
0130     QCOMPARE(data[0].first, Imap::Mailbox::CATENATE_TEXT);
0131     QVERIFY(data[0].second.startsWith("Resent-Date: "));
0132     QVERIFY(data[0].second.endsWith("\r\n"));
0133     QCOMPARE(data[1].first, Imap::Mailbox::CATENATE_URL);
0134     QCOMPARE(data[1].second, QByteArrayLiteral("/a;UIDVALIDITY=333666/;UID=10"));
0135     QVERIFY(!composer.isReadyForSerialization()); // FIXME: this is actually a bug, but one which is hard to fix without changing the API
0136     cEmpty();
0137 }
0138 
0139 QTEST_GUILESS_MAIN(ComposerExistingTest)