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

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "importimapsettingsakonadicheckjobtest.h"
0008 #include "../checks/importimapsettingsakonadicheckjob.h"
0009 #include "importimapsettingspasswordtest.h"
0010 
0011 #include <QSignalSpy>
0012 #include <QStandardPaths>
0013 #include <QTest>
0014 
0015 Q_DECLARE_METATYPE(SieveEditorUtil::SieveServerConfig)
0016 ImportImapSettingsAkonadiCheckJobTest::ImportImapSettingsAkonadiCheckJobTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019     qRegisterMetaType<SieveEditorUtil::SieveServerConfig>();
0020 }
0021 
0022 ImportImapSettingsAkonadiCheckJobTest::~ImportImapSettingsAkonadiCheckJobTest() = default;
0023 
0024 void ImportImapSettingsAkonadiCheckJobTest::initTestCase()
0025 {
0026     QStandardPaths::setTestModeEnabled(true);
0027 }
0028 
0029 void ImportImapSettingsAkonadiCheckJobTest::shouldHaveSettingsFiles()
0030 {
0031     qputenv("XDG_CONFIG_DIRS", QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + QStringLiteral("/config/config1")).toLatin1().constData());
0032     ImportImapSettingsAkonadiCheckJob job;
0033     job.setImapSettingsPassword(new ImportImapSettingsPasswordTest);
0034     QVERIFY(job.settingsCanBeImported());
0035 }
0036 
0037 void ImportImapSettingsAkonadiCheckJobTest::shouldHaveNotSettingsFiles()
0038 {
0039     qputenv("XDG_CONFIG_DIRS", QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + QStringLiteral("/config/noconfig")).toLatin1().constData());
0040     ImportImapSettingsAkonadiCheckJob job;
0041     job.setImapSettingsPassword(new ImportImapSettingsPasswordTest);
0042     QVERIFY(!job.settingsCanBeImported());
0043 }
0044 
0045 void ImportImapSettingsAkonadiCheckJobTest::shouldHaveSettingsFilesImap()
0046 {
0047     qputenv("XDG_CONFIG_DIRS", QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + QStringLiteral("/config/config2")).toLatin1().constData());
0048     ImportImapSettingsAkonadiCheckJob job;
0049     job.setImapSettingsPassword(new ImportImapSettingsPasswordTest);
0050     QVERIFY(job.settingsCanBeImported());
0051 }
0052 
0053 void ImportImapSettingsAkonadiCheckJobTest::shouldHaveSettingsFilesMbox()
0054 {
0055     qputenv("XDG_CONFIG_DIRS", QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + QStringLiteral("/config/config3")).toLatin1().constData());
0056     ImportImapSettingsAkonadiCheckJob job;
0057     job.setImapSettingsPassword(new ImportImapSettingsPasswordTest);
0058     QVERIFY(!job.settingsCanBeImported());
0059 }
0060 
0061 void ImportImapSettingsAkonadiCheckJobTest::shouldHaveImportSettings_data()
0062 {
0063     QTest::addColumn<QString>("directory");
0064     QTest::addColumn<int>("nbsignals");
0065     QTest::addColumn<int>("nbSignalsNoSettingsFound");
0066     QTest::newRow("reuseconfig") << QStringLiteral("/config/reuseconfig") << 1 << 0;
0067     QTest::newRow("reuseconfigtwiceconfig") << QStringLiteral("/config/reuseconfigtwiceconfig") << 2 << 0;
0068 
0069     QTest::newRow("imapconfig") << QStringLiteral("/config/imapconfig") << 0 << 1;
0070     QTest::newRow("imapconfigwithsieveconfig") << QStringLiteral("/config/imapconfigwithsieveconfig") << 1 << 0;
0071 }
0072 
0073 void ImportImapSettingsAkonadiCheckJobTest::shouldHaveImportSettings()
0074 {
0075     QFETCH(QString, directory);
0076     QFETCH(int, nbsignals);
0077     QFETCH(int, nbSignalsNoSettingsFound);
0078     qputenv("XDG_CONFIG_DIRS", QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + directory).toLatin1().constData());
0079     ImportImapSettingsAkonadiCheckJob job;
0080     job.setImapSettingsPassword(new ImportImapSettingsPasswordTest);
0081     QVERIFY(job.settingsCanBeImported());
0082     QSignalSpy spy(&job, &ImportImapSettingsAkonadiCheckJob::importSetting);
0083     QSignalSpy spy2(&job, &ImportImapSettingsAkonadiCheckJob::noSettingsImported);
0084 
0085     job.start();
0086     QCOMPARE(spy.count(), nbsignals);
0087     QCOMPARE(spy2.count(), nbSignalsNoSettingsFound);
0088 }
0089 
0090 void ImportImapSettingsAkonadiCheckJobTest::shouldImportSieveSettings_data()
0091 {
0092     QTest::addColumn<QString>("directory");
0093     QTest::addColumn<QString>("name");
0094     QTest::addColumn<SieveEditorUtil::SieveServerConfig>("settings");
0095 
0096     SieveEditorUtil::SieveServerConfig settings;
0097     settings.sieveImapAccountSettings.setUserName(QStringLiteral("foo@foo.com"));
0098     settings.sieveImapAccountSettings.setServerName(QStringLiteral("bla.foo.com"));
0099     settings.sieveImapAccountSettings.setPort(143);
0100     settings.sieveImapAccountSettings.setPassword(QStringLiteral("password_imap"));
0101     settings.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::STARTTLS);
0102 
0103     settings.sieveSettings.userName = QStringLiteral("foo@foo.com");
0104     settings.sieveSettings.serverName = QStringLiteral("bla.foo.com");
0105     settings.sieveSettings.password = QStringLiteral("password_imap");
0106     settings.sieveSettings.port = 4190;
0107 
0108     QTest::newRow("reuseconfig") << QStringLiteral("/config/reuseconfig") << QStringLiteral("akonadi_kolab_resource_0rc") << settings;
0109 
0110     SieveEditorUtil::SieveServerConfig settings2;
0111     settings2.sieveImapAccountSettings.setUserName(QStringLiteral("foo@foo.com"));
0112     settings2.sieveImapAccountSettings.setServerName(QStringLiteral("bla.foo.com"));
0113     settings2.sieveImapAccountSettings.setPort(143);
0114     settings2.sieveImapAccountSettings.setPassword(QStringLiteral("password_imap"));
0115     settings2.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::STARTTLS);
0116     settings2.sieveSettings.userName = QStringLiteral("b");
0117     settings2.sieveSettings.serverName = QStringLiteral("bla.foo.com"); // TODO
0118     settings2.sieveSettings.password = QStringLiteral("password_custom_sieve_imap");
0119     settings2.sieveSettings.port = 150;
0120 
0121     QTest::newRow("usecustomsieveconfig") << QStringLiteral("/config/usecustomsieveconfig") << QStringLiteral("akonadi_kolab_resource_0rc") << settings2;
0122 
0123     SieveEditorUtil::SieveServerConfig settings3;
0124     settings3.sieveImapAccountSettings.setUserName(QStringLiteral("foo@foo.com"));
0125     settings3.sieveImapAccountSettings.setServerName(QStringLiteral("bla.foo.com"));
0126     settings3.sieveImapAccountSettings.setPort(143);
0127     settings3.sieveImapAccountSettings.setAuthenticationType(static_cast<KSieveCore::SieveImapAccountSettings::AuthenticationMode>(3));
0128     settings3.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::Unencrypted);
0129     settings3.sieveImapAccountSettings.setPassword(QStringLiteral("password_imap"));
0130     settings3.sieveSettings.userName = QStringLiteral("b");
0131     settings3.sieveSettings.password = QStringLiteral("password_custom_sieve_imap");
0132     settings3.sieveSettings.serverName = QStringLiteral("bla.foo.com"); // TODO
0133     settings3.sieveSettings.port = 150;
0134 
0135     QTest::newRow("usecustomsieveconfig2") << QStringLiteral("/config/usecustomsieveconfig2") << QStringLiteral("akonadi_kolab_resource_0rc") << settings3;
0136 
0137     SieveEditorUtil::SieveServerConfig settings4;
0138     settings4.sieveImapAccountSettings.setUserName(QStringLiteral("foo@foo.com"));
0139     settings4.sieveImapAccountSettings.setServerName(QStringLiteral("bla.foo.com"));
0140     settings4.sieveImapAccountSettings.setPort(143);
0141     settings4.sieveImapAccountSettings.setAuthenticationType(static_cast<KSieveCore::SieveImapAccountSettings::AuthenticationMode>(3));
0142     settings4.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::STARTTLS);
0143     settings4.sieveImapAccountSettings.setPassword(QStringLiteral("password_imap"));
0144     settings4.sieveSettings.userName = QStringLiteral("b");
0145     settings4.sieveSettings.password = QStringLiteral("password_custom_sieve_imap");
0146     settings4.sieveSettings.serverName = QStringLiteral("bla.foo.com"); // TODO
0147     settings4.sieveSettings.port = 150;
0148 
0149     QTest::newRow("usecustomsieveconfig3") << QStringLiteral("/config/usecustomsieveconfig3") << QStringLiteral("akonadi_kolab_resource_0rc") << settings4;
0150 
0151     SieveEditorUtil::SieveServerConfig settings5;
0152     settings5.sieveImapAccountSettings.setUserName(QStringLiteral("foo@foo.com"));
0153     settings5.sieveImapAccountSettings.setServerName(QStringLiteral("bla.foo.com"));
0154     settings5.sieveImapAccountSettings.setPort(143);
0155     settings5.sieveImapAccountSettings.setAuthenticationType(static_cast<KSieveCore::SieveImapAccountSettings::AuthenticationMode>(3));
0156     settings5.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::SSLorTLS);
0157     settings5.sieveImapAccountSettings.setPassword(QStringLiteral("password_imap"));
0158     settings5.sieveSettings.userName = QStringLiteral("b");
0159     settings5.sieveSettings.serverName = QStringLiteral("bla.foo.com"); // TODO
0160     settings5.sieveSettings.password = QStringLiteral("password_custom_sieve_imap");
0161     settings5.sieveSettings.port = 150;
0162 
0163     QTest::newRow("usecustomsieveconfig4") << QStringLiteral("/config/usecustomsieveconfig4") << QStringLiteral("akonadi_kolab_resource_0rc") << settings5;
0164 }
0165 
0166 void ImportImapSettingsAkonadiCheckJobTest::shouldImportSieveSettings()
0167 {
0168     QFETCH(QString, directory);
0169     QFETCH(QString, name);
0170     QFETCH(SieveEditorUtil::SieveServerConfig, settings);
0171     qputenv("XDG_CONFIG_DIRS", QString(QLatin1StringView(IMPORTWIZARD_DATA_DIR) + directory).toLatin1().constData());
0172     ImportImapSettingsAkonadiCheckJob job;
0173     job.setImapSettingsPassword(new ImportImapSettingsPasswordTest);
0174     QVERIFY(job.settingsCanBeImported());
0175     QSignalSpy spy(&job, &ImportImapSettingsAkonadiCheckJob::importSetting);
0176     job.start();
0177     QCOMPARE(spy.count(), 1);
0178     QCOMPARE(spy.at(0).at(0).toString(), name);
0179     auto importSettings = spy.at(0).at(1).value<SieveEditorUtil::SieveServerConfig>();
0180     QCOMPARE(importSettings, settings);
0181 }
0182 
0183 QTEST_MAIN(ImportImapSettingsAkonadiCheckJobTest)
0184 
0185 #include "moc_importimapsettingsakonadicheckjobtest.cpp"