File indexing completed on 2024-12-15 04:55:35

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "importimapsettingsthunderbirdcheckjobtest.h"
0008 #include "importwizard/checks/importimapsettingsthunderbirdcheckjob.h"
0009 #include "libsieveeditor_export.h"
0010 #include <QSignalSpy>
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 Q_DECLARE_METATYPE(SieveEditorUtil::SieveServerConfig)
0015 extern LIBSIEVEEDITOR_EXPORT QString sieveeditor_thunderbird_default_toplevel_path;
0016 ImportImapSettingsThunderbirdCheckJobTest::ImportImapSettingsThunderbirdCheckJobTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019     qRegisterMetaType<SieveEditorUtil::SieveServerConfig>();
0020     QStandardPaths::setTestModeEnabled(true);
0021     sieveeditor_thunderbird_default_toplevel_path = QString();
0022 }
0023 
0024 ImportImapSettingsThunderbirdCheckJobTest::~ImportImapSettingsThunderbirdCheckJobTest() = default;
0025 
0026 void ImportImapSettingsThunderbirdCheckJobTest::shouldHaveDefaultValue()
0027 {
0028     sieveeditor_thunderbird_default_toplevel_path = QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + QStringLiteral("/empty2"));
0029     ImportImapSettingsThunderbirdCheckJob check;
0030     QVERIFY(!check.settingsCanBeImported());
0031 }
0032 
0033 void ImportImapSettingsThunderbirdCheckJobTest::shouldHaveDefaultValueWithEmptyDirectory()
0034 {
0035     sieveeditor_thunderbird_default_toplevel_path = QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + QStringLiteral("/empty"));
0036     ImportImapSettingsThunderbirdCheckJob check;
0037     QVERIFY(check.settingsCanBeImported());
0038     check.start();
0039 }
0040 
0041 void ImportImapSettingsThunderbirdCheckJobTest::shouldBeAbleToImportSettings()
0042 {
0043     sieveeditor_thunderbird_default_toplevel_path = QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + QStringLiteral("/thunderbird1"));
0044     ImportImapSettingsThunderbirdCheckJob check;
0045     QVERIFY(check.settingsCanBeImported());
0046     check.start();
0047 }
0048 
0049 void ImportImapSettingsThunderbirdCheckJobTest::shouldBeAbleToImportSettings2()
0050 {
0051     sieveeditor_thunderbird_default_toplevel_path = QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + QStringLiteral("/thunderbird2"));
0052     ImportImapSettingsThunderbirdCheckJob check;
0053     QVERIFY(check.settingsCanBeImported());
0054     check.start();
0055 }
0056 
0057 void ImportImapSettingsThunderbirdCheckJobTest::shouldHaveImportSettings_data()
0058 {
0059     QTest::addColumn<QString>("directory");
0060     QTest::addColumn<int>("nbsignals");
0061     QTest::addColumn<int>("nbSignalsNoSettingsFound");
0062     QTest::newRow("thunderbird1-withoutsieve") << QStringLiteral("/thunderbird1") << 0 << 1;
0063     QTest::newRow("empty") << QStringLiteral("/empty") << 0 << 1;
0064     QTest::newRow("thunderbird2-withoutsieve") << QStringLiteral("/thunderbird2") << 0 << 1;
0065     QTest::newRow("thunderbirdencryptionnone") << QStringLiteral("/thunderbirdencryptionnone") << 2 << 0;
0066     QTest::newRow("thunderbirdencryptionssl") << QStringLiteral("/thunderbirdencryptionssl") << 2 << 0;
0067     QTest::newRow("thunderbirdencryptionstarttls") << QStringLiteral("/thunderbirdencryptionstarttls") << 2 << 0;
0068 
0069     QTest::newRow("thunderbirdencryptionnoneoneaccount") << QStringLiteral("/thunderbirdencryptionnoneoneaccount") << 1 << 0;
0070     QTest::newRow("thunderbirdencryptionssloneaccount") << QStringLiteral("/thunderbirdencryptionssloneaccount") << 1 << 0;
0071     QTest::newRow("thunderbirdencryptionstarttlsoneaccount") << QStringLiteral("/thunderbirdencryptionstarttlsoneaccount") << 1 << 0;
0072 }
0073 
0074 void ImportImapSettingsThunderbirdCheckJobTest::shouldHaveImportSettings()
0075 {
0076     QFETCH(QString, directory);
0077     QFETCH(int, nbsignals);
0078     QFETCH(int, nbSignalsNoSettingsFound);
0079     sieveeditor_thunderbird_default_toplevel_path = QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + directory);
0080     ImportImapSettingsThunderbirdCheckJob job;
0081     QVERIFY(job.settingsCanBeImported());
0082     QSignalSpy spy(&job, &ImportImapSettingsThunderbirdCheckJob::importSetting);
0083     QSignalSpy spy2(&job, &ImportImapSettingsThunderbirdCheckJob::noSettingsImported);
0084 
0085     job.start();
0086     QCOMPARE(spy.count(), nbsignals);
0087     QCOMPARE(spy2.count(), nbSignalsNoSettingsFound);
0088 }
0089 
0090 void ImportImapSettingsThunderbirdCheckJobTest::shouldImportSieveSettings_data()
0091 {
0092     QTest::addColumn<QString>("directory");
0093     QTest::addColumn<QString>("name");
0094     QTest::addColumn<SieveEditorUtil::SieveServerConfig>("settings");
0095 
0096     {
0097         SieveEditorUtil::SieveServerConfig settings;
0098         settings.sieveImapAccountSettings.setUserName(QStringLiteral("bla@kde.org"));
0099         settings.sieveImapAccountSettings.setServerName(QStringLiteral("sever.foo.org"));
0100         settings.sieveImapAccountSettings.setPort(993);
0101         settings.sieveImapAccountSettings.setAuthenticationType(KSieveCore::SieveImapAccountSettings::NTLM);
0102         settings.sieveSettings.userName = QStringLiteral("bla@kde.org");
0103         settings.sieveSettings.serverName = QStringLiteral("sever.foo.org");
0104         settings.sieveSettings.port = 4190;
0105 
0106         QTest::newRow("thunderbirdencryptionnoneoneaccount")
0107             << QStringLiteral("/thunderbirdencryptionnoneoneaccount") << QStringLiteral("bla@kde.org") << settings;
0108     }
0109     {
0110         SieveEditorUtil::SieveServerConfig settings;
0111         settings.sieveImapAccountSettings.setUserName(QStringLiteral("bla@kde.org"));
0112         settings.sieveImapAccountSettings.setServerName(QStringLiteral("sever.foo.org"));
0113         settings.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::SSLorTLS);
0114         settings.sieveImapAccountSettings.setAuthenticationType(KSieveCore::SieveImapAccountSettings::Plain);
0115         settings.sieveImapAccountSettings.setPort(993);
0116         settings.sieveSettings.userName = QStringLiteral("bla@kde.org");
0117         settings.sieveSettings.serverName = QStringLiteral("sever.foo.org");
0118         settings.sieveSettings.port = 4190;
0119 
0120         QTest::newRow("thunderbirdencryptionssloneaccount")
0121             << QStringLiteral("/thunderbirdencryptionssloneaccount") << QStringLiteral("bla@kde.org") << settings;
0122     }
0123 
0124     {
0125         SieveEditorUtil::SieveServerConfig settings;
0126         settings.sieveImapAccountSettings.setUserName(QStringLiteral("bla@kde.org"));
0127         settings.sieveImapAccountSettings.setServerName(QStringLiteral("sever.foo.org"));
0128         settings.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::STARTTLS);
0129         settings.sieveImapAccountSettings.setAuthenticationType(KSieveCore::SieveImapAccountSettings::GSSAPI);
0130         settings.sieveImapAccountSettings.setPort(993);
0131         settings.sieveSettings.userName = QStringLiteral("bla@kde.org");
0132         settings.sieveSettings.serverName = QStringLiteral("sever.foo.org");
0133         settings.sieveSettings.port = 4190;
0134 
0135         QTest::newRow("thunderbirdencryptionstarttlsoneaccount")
0136             << QStringLiteral("/thunderbirdencryptionstarttlsoneaccount") << QStringLiteral("bla@kde.org") << settings;
0137     }
0138 }
0139 
0140 void ImportImapSettingsThunderbirdCheckJobTest::shouldImportSieveSettings()
0141 {
0142     QFETCH(QString, directory);
0143     QFETCH(QString, name);
0144     QFETCH(SieveEditorUtil::SieveServerConfig, settings);
0145     sieveeditor_thunderbird_default_toplevel_path = QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + directory);
0146     ImportImapSettingsThunderbirdCheckJob job;
0147     QVERIFY(job.settingsCanBeImported());
0148     QSignalSpy spy(&job, &ImportImapSettingsThunderbirdCheckJob::importSetting);
0149     job.start();
0150     QCOMPARE(spy.count(), 1);
0151     QCOMPARE(spy.at(0).at(0).toString(), name);
0152     auto importSettings = spy.at(0).at(1).value<SieveEditorUtil::SieveServerConfig>();
0153     QCOMPARE(importSettings, settings);
0154 }
0155 
0156 QTEST_MAIN(ImportImapSettingsThunderbirdCheckJobTest)
0157 
0158 #include "moc_importimapsettingsthunderbirdcheckjobtest.cpp"