Commit a9717fb6 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Add autotest

parent 401149ab
Pipeline #41478 failed with stage
in 12 minutes and 45 seconds
......@@ -8,5 +8,5 @@ macro(add_sieveutil_test _source _additionalsource)
endmacro()
add_sieveutil_test(sieveaccounttest.cpp "")
add_sieveutil_test(utilmethodtest.cpp "akonadiimapsettinginterfacetest.cpp")
add_sieveutil_test(findaccountinfojobtest.cpp "akonadiimapsettinginterfacetest.cpp")
add_sieveutil_test(regexpconvertertest.cpp "")
......@@ -4,13 +4,16 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "utilmethodtest.h"
#include "findaccountinfojobtest.h"
#include "../util_p.h"
#include "util/findaccountinfojob.h"
#include "imapresourcesettings.h"
#include "akonadiimapsettinginterfacetest.h"
#include "../abstractakonadiimapsettinginterface.h"
#include "../sieveimappasswordprovider.h"
#include <QSignalSpy>
#include <QTest>
QTEST_MAIN(FindAccountInfoJobTest)
class DataImapPasswordProvider : public KSieveUi::SieveImapPasswordProvider
{
......@@ -30,22 +33,28 @@ public:
return mData->sieveCustomPassword;
}
AkonadiImapSettingInterfaceDataTest *mData = nullptr;
AkonadiImapSettingInterfaceDataTest *const mData;
};
UtilMethodTest::UtilMethodTest(QObject *parent)
FindAccountInfoJobTest::FindAccountInfoJobTest(QObject *parent)
: QObject(parent)
{
}
void UtilMethodTest::shouldReturnEmptyInfo()
void FindAccountInfoJobTest::shouldReturnEmptyInfo()
{
std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> interface(new KSieveUi::AbstractAkonadiImapSettingInterface);
KSieveUi::Util::AccountInfo info = KSieveUi::Util::findAccountInfo(QStringLiteral("dummy"), {}, false, interface);
auto *job = new KSieveUi::FindAccountInfoJob;
job->setIdentifier(QStringLiteral("dummy"));
job->setProvider({});
QSignalSpy spy(job, &KSieveUi::FindAccountInfoJob::findAccountInfoFinished);
job->start();
spy.wait(1000);
QCOMPARE(spy.count(), 1);
const KSieveUi::Util::AccountInfo info = spy.at(0).at(0).value<KSieveUi::Util::AccountInfo>();
QVERIFY(!info.sieveImapAccountSettings.isValid());
}
void UtilMethodTest::shouldAssignValue_data()
void FindAccountInfoJobTest::shouldAssignValue_data()
{
QTest::addColumn<AkonadiImapSettingInterfaceDataTest>("data");
QTest::addColumn<KSieveUi::Util::AccountInfo>("accountInfo");
......@@ -521,7 +530,7 @@ void UtilMethodTest::shouldAssignValue_data()
}
}
void UtilMethodTest::shouldAssignValue()
void FindAccountInfoJobTest::shouldAssignValue()
{
QFETCH(AkonadiImapSettingInterfaceDataTest, data);
QFETCH(KSieveUi::Util::AccountInfo, accountInfo);
......@@ -530,7 +539,17 @@ void UtilMethodTest::shouldAssignValue()
std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> interface(new AkonadiImapSettingInterfaceTest(data));
std::unique_ptr<DataImapPasswordProvider> provider(new DataImapPasswordProvider(&data));
const KSieveUi::Util::AccountInfo info = KSieveUi::Util::findAccountInfo(QStringLiteral("foo"), provider.get(), useVacationFile, interface);
auto *job = new KSieveUi::FindAccountInfoJob;
job->setIdentifier(QStringLiteral("foo"));
job->setProvider(provider.get());
job->setCustomImapSettingsInterface(interface.get());
job->setWithVacationFileName(useVacationFile);
QSignalSpy spy(job, &KSieveUi::FindAccountInfoJob::findAccountInfoFinished);
job->start();
spy.wait(1000);
QCOMPARE(spy.count(), 1);
const KSieveUi::Util::AccountInfo info = spy.at(0).at(0).value<KSieveUi::Util::AccountInfo>();
QCOMPARE(info.sieveImapAccountSettings.isValid(), sieveImapAccountValid);
const bool equal = (info == accountInfo);
if (!equal) {
......@@ -540,4 +559,4 @@ void UtilMethodTest::shouldAssignValue()
QVERIFY(equal);
}
QTEST_MAIN(UtilMethodTest)
......@@ -4,20 +4,20 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef UTILMETHODTEST_H
#define UTILMETHODTEST_H
#ifndef FINDACCOUNTINFOJOBTEST_H
#define FINDACCOUNTINFOJOBTEST_H
#include <QObject>
class UtilMethodTest : public QObject
class FindAccountInfoJobTest : public QObject
{
Q_OBJECT
public:
explicit UtilMethodTest(QObject *parent = nullptr);
~UtilMethodTest() = default;
explicit FindAccountInfoJobTest(QObject *parent = nullptr);
~FindAccountInfoJobTest() = default;
private Q_SLOTS:
void shouldReturnEmptyInfo();
void shouldAssignValue_data();
void shouldAssignValue();
};
#endif // UTILMETHODTEST_H
#endif // FINDACCOUNTINFOJOBTEST_H
......@@ -27,7 +27,12 @@ FindAccountInfoJob::~FindAccountInfoJob()
bool FindAccountInfoJob::canStart() const
{
return !mIdentifier.isEmpty() && mProvider;
return !mIdentifier.isEmpty();
}
void FindAccountInfoJob::setCustomImapSettingsInterface(AbstractAkonadiImapSettingInterface *newCustomImapSettingsInterface)
{
mCustomImapSettingsInterface = newCustomImapSettingsInterface;
}
void FindAccountInfoJob::sendAccountInfo()
......@@ -43,24 +48,28 @@ void FindAccountInfoJob::start()
sendAccountInfo();
return;
}
std::unique_ptr<OrgKdeAkonadiImapSettingsInterface> interfaceImap(PimCommon::Util::createImapSettingsInterface(mIdentifier));
std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> interface(new KSieveUi::AkonadiImapSettingInterface(interfaceImap));
if (!interface) {
if (!mProvider) {
sendAccountInfo();
return;
}
if (!interface->sieveSupport()) {
std::unique_ptr<OrgKdeAkonadiImapSettingsInterface> interfaceImap(PimCommon::Util::createImapSettingsInterface(mIdentifier));
std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> interface(new KSieveUi::AkonadiImapSettingInterface(interfaceImap));
if (!mCustomImapSettingsInterface) {
mCustomImapSettingsInterface = interface.get();
}
if (!mCustomImapSettingsInterface->sieveSupport()) {
sendAccountInfo();
return;
}
if (interface->sieveReuseConfig()) {
if (mCustomImapSettingsInterface->sieveReuseConfig()) {
// assemble Sieve url from the settings of the account:
QUrl u;
u.setScheme(QStringLiteral("sieve"));
QString server;
const QString reply = interface->imapServer();
const QString reply = mCustomImapSettingsInterface->imapServer();
if (!reply.isEmpty()) {
server = reply;
server = server.section(QLatin1Char(':'), 0, 0);
......@@ -68,7 +77,7 @@ void FindAccountInfoJob::start()
sendAccountInfo();
return;
}
const QString userName = interface->userName();
const QString userName = mCustomImapSettingsInterface->userName();
mAccountInfo.sieveImapAccountSettings.setServerName(server);
mAccountInfo.sieveImapAccountSettings.setUserName(userName);
......@@ -78,11 +87,11 @@ void FindAccountInfoJob::start()
const QString pwd = mProvider->password(mIdentifier);
u.setPassword(pwd);
mAccountInfo.sieveImapAccountSettings.setPassword(pwd);
mAccountInfo.sieveImapAccountSettings.setPort(interface->imapPort());
u.setPort(interface->sievePort());
mAccountInfo.sieveImapAccountSettings.setPort(mCustomImapSettingsInterface->imapPort());
u.setPort(mCustomImapSettingsInterface->sievePort());
QString authStr;
mAccountInfo.sieveImapAccountSettings.setAuthenticationType(static_cast<SieveImapAccountSettings::AuthenticationMode>((int)interface->authentication()));
switch (interface->authentication()) {
mAccountInfo.sieveImapAccountSettings.setAuthenticationType(static_cast<SieveImapAccountSettings::AuthenticationMode>((int)mCustomImapSettingsInterface->authentication()));
switch (mCustomImapSettingsInterface->authentication()) {
case MailTransport::Transport::EnumAuthenticationType::CLEAR:
case MailTransport::Transport::EnumAuthenticationType::PLAIN:
authStr = QStringLiteral("PLAIN");
......@@ -108,7 +117,7 @@ void FindAccountInfoJob::start()
}
QUrlQuery query;
query.addQueryItem(QStringLiteral("x-mech"), authStr);
const QString resultSafety = interface->safety();
const QString resultSafety = mCustomImapSettingsInterface->safety();
if (resultSafety == QLatin1String("None")) {
mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
query.addQueryItem(QStringLiteral("x-allow-unencrypted"), QStringLiteral("true"));
......@@ -122,13 +131,13 @@ void FindAccountInfoJob::start()
u.setQuery(query);
u = u.adjusted(QUrl::RemoveFilename);
if (mWithVacationFileName) {
u.setPath(u.path() + QLatin1Char('/') + QString(interface->sieveVacationFilename()));
u.setPath(u.path() + QLatin1Char('/') + QString(mCustomImapSettingsInterface->sieveVacationFilename()));
}
mAccountInfo.sieveUrl = u;
sendAccountInfo();
} else {
QString server;
const QString reply = interface->imapServer();
const QString reply = mCustomImapSettingsInterface->imapServer();
if (!reply.isEmpty()) {
server = reply;
server = server.section(QLatin1Char(':'), 0, 0);
......@@ -137,21 +146,21 @@ void FindAccountInfoJob::start()
return;
}
const QString userName = interface->userName();
const QString userName = mCustomImapSettingsInterface->userName();
mAccountInfo.sieveImapAccountSettings.setServerName(server);
mAccountInfo.sieveImapAccountSettings.setUserName(userName);
mAccountInfo.sieveImapAccountSettings.setAuthenticationType(static_cast<SieveImapAccountSettings::AuthenticationMode>((int)interface->authentication()));
mAccountInfo.sieveImapAccountSettings.setAuthenticationType(static_cast<SieveImapAccountSettings::AuthenticationMode>((int)mCustomImapSettingsInterface->authentication()));
const QString pwd = mProvider->password(mIdentifier);
mAccountInfo.sieveImapAccountSettings.setPassword(pwd);
mAccountInfo.sieveImapAccountSettings.setPort(interface->imapPort());
mAccountInfo.sieveImapAccountSettings.setPort(mCustomImapSettingsInterface->imapPort());
QUrl u;
u.setHost(interface->sieveAlternateUrl());
u.setHost(mCustomImapSettingsInterface->sieveAlternateUrl());
u.setScheme(QStringLiteral("sieve"));
u.setPort(interface->sievePort());
u.setPort(mCustomImapSettingsInterface->sievePort());
QString authStr;
const QString resultSafety = interface->safety();
switch (interface->alternateAuthentication()) {
const QString resultSafety = mCustomImapSettingsInterface->safety();
switch (mCustomImapSettingsInterface->alternateAuthentication()) {
case MailTransport::Transport::EnumAuthenticationType::CLEAR:
case MailTransport::Transport::EnumAuthenticationType::PLAIN:
authStr = QStringLiteral("PLAIN");
......@@ -191,21 +200,21 @@ void FindAccountInfoJob::start()
u.setQuery(query);
const QString resultCustomAuthentication = interface->sieveCustomAuthentification();
const QString resultCustomAuthentication = mCustomImapSettingsInterface->sieveCustomAuthentification();
if (resultCustomAuthentication == QLatin1String("ImapUserPassword")) {
u.setUserName(interface->userName());
u.setUserName(mCustomImapSettingsInterface->userName());
const QString imapPwd = pwd;
u.setPassword(imapPwd);
} else if (resultCustomAuthentication == QLatin1String("CustomUserPassword")) {
const QString customPwd = mProvider->sieveCustomPassword(mIdentifier);
u.setPassword(customPwd);
u.setUserName(interface->sieveCustomUsername());
u.setUserName(mCustomImapSettingsInterface->sieveCustomUsername());
} else {
qCWarning(LIBKSIEVE_LOG) << "resultCustomAuthentication undefined " << resultCustomAuthentication;
}
u = u.adjusted(QUrl::RemoveFilename);
if (mWithVacationFileName) {
u.setPath(u.path() + QLatin1Char('/') + interface->sieveVacationFilename());
u.setPath(u.path() + QLatin1Char('/') + mCustomImapSettingsInterface->sieveVacationFilename());
}
mAccountInfo.sieveUrl = u;
sendAccountInfo();
......
......@@ -8,11 +8,12 @@
#define FINDACCOUNTINFOJOB_H
#include "util_p.h"
#include "ksieveui_export.h" //TODO use specific export for testing ?
#include <QObject>
namespace KSieveUi {
class SieveImapPasswordProvider;
class FindAccountInfoJob : public QObject
class KSIEVEUI_EXPORT FindAccountInfoJob : public QObject
{
Q_OBJECT
public:
......@@ -32,6 +33,8 @@ public:
Q_REQUIRED_RESULT bool canStart() const;
void setCustomImapSettingsInterface(KSieveUi::AbstractAkonadiImapSettingInterface *newCustomImapSettingsInterface);
Q_SIGNALS:
void findAccountInfoFinished(const KSieveUi::Util::AccountInfo &info);
......@@ -39,6 +42,7 @@ private:
void sendAccountInfo();
KSieveUi::Util::AccountInfo mAccountInfo;
QString mIdentifier;
KSieveUi::AbstractAkonadiImapSettingInterface *mCustomImapSettingsInterface = nullptr;
SieveImapPasswordProvider *mProvider = nullptr;
bool mWithVacationFileName = true;
};
......
......@@ -21,180 +21,6 @@
using namespace KSieveUi;
KSieveUi::Util::AccountInfo KSieveUi::Util::fullAccountInfo(const QString &identifier, SieveImapPasswordProvider *provider, bool withVacationFileName)
{
std::unique_ptr<OrgKdeAkonadiImapSettingsInterface> interfaceImap(PimCommon::Util::createImapSettingsInterface(identifier));
std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> interface(new KSieveUi::AkonadiImapSettingInterface(interfaceImap));
KSieveUi::Util::AccountInfo accountInfo = KSieveUi::Util::findAccountInfo(identifier, provider, withVacationFileName, interface);
return accountInfo;
}
KSieveUi::Util::AccountInfo KSieveUi::Util::findAccountInfo(const QString &identifier, SieveImapPasswordProvider *provider, bool withVacationFileName, std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> &interface)
{
KSieveUi::Util::AccountInfo accountInfo;
if (!interface) {
return accountInfo;
}
if (!interface->sieveSupport()) {
return accountInfo;
}
if (interface->sieveReuseConfig()) {
// assemble Sieve url from the settings of the account:
QUrl u;
u.setScheme(QStringLiteral("sieve"));
QString server;
QString reply = interface->imapServer();
if (!reply.isEmpty()) {
server = reply;
server = server.section(QLatin1Char(':'), 0, 0);
} else {
return accountInfo;
}
const QString userName = interface->userName();
accountInfo.sieveImapAccountSettings.setServerName(server);
accountInfo.sieveImapAccountSettings.setUserName(userName);
u.setHost(server);
u.setUserName(userName);
const QString pwd = provider->password(identifier);
u.setPassword(pwd);
accountInfo.sieveImapAccountSettings.setPassword(pwd);
accountInfo.sieveImapAccountSettings.setPort(interface->imapPort());
u.setPort(interface->sievePort());
QString authStr;
accountInfo.sieveImapAccountSettings.setAuthenticationType(static_cast<SieveImapAccountSettings::AuthenticationMode>((int)interface->authentication()));
switch (interface->authentication()) {
case MailTransport::Transport::EnumAuthenticationType::CLEAR:
case MailTransport::Transport::EnumAuthenticationType::PLAIN:
authStr = QStringLiteral("PLAIN");
break;
case MailTransport::Transport::EnumAuthenticationType::LOGIN:
authStr = QStringLiteral("LOGIN");
break;
case MailTransport::Transport::EnumAuthenticationType::CRAM_MD5:
authStr = QStringLiteral("CRAM-MD5");
break;
case MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5:
authStr = QStringLiteral("DIGEST-MD5");
break;
case MailTransport::Transport::EnumAuthenticationType::GSSAPI:
authStr = QStringLiteral("GSSAPI");
break;
case MailTransport::Transport::EnumAuthenticationType::ANONYMOUS:
authStr = QStringLiteral("ANONYMOUS");
break;
default:
authStr = QStringLiteral("PLAIN");
break;
}
QUrlQuery query;
query.addQueryItem(QStringLiteral("x-mech"), authStr);
const QString resultSafety = interface->safety();
if (resultSafety == QLatin1String("None")) {
accountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
query.addQueryItem(QStringLiteral("x-allow-unencrypted"), QStringLiteral("true"));
} else if (resultSafety == QLatin1String("SSL")) {
accountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::SSLorTLS);
} else if (resultSafety == QLatin1String("STARTTLS")) {
accountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::STARTTLS);
} else {
accountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
}
u.setQuery(query);
u = u.adjusted(QUrl::RemoveFilename);
if (withVacationFileName) {
u.setPath(u.path() + QLatin1Char('/') + QString(interface->sieveVacationFilename()));
}
accountInfo.sieveUrl = u;
return accountInfo;
} else {
QString server;
QString reply = interface->imapServer();
if (!reply.isEmpty()) {
server = reply;
server = server.section(QLatin1Char(':'), 0, 0);
} else {
return accountInfo;
}
const QString userName = interface->userName();
accountInfo.sieveImapAccountSettings.setServerName(server);
accountInfo.sieveImapAccountSettings.setUserName(userName);
accountInfo.sieveImapAccountSettings.setAuthenticationType(static_cast<SieveImapAccountSettings::AuthenticationMode>((int)interface->authentication()));
const QString pwd = provider->password(identifier);
accountInfo.sieveImapAccountSettings.setPassword(pwd);
accountInfo.sieveImapAccountSettings.setPort(interface->imapPort());
QUrl u;
u.setHost(interface->sieveAlternateUrl());
u.setScheme(QStringLiteral("sieve"));
u.setPort(interface->sievePort());
QString authStr;
const QString resultSafety = interface->safety();
switch (interface->alternateAuthentication()) {
case MailTransport::Transport::EnumAuthenticationType::CLEAR:
case MailTransport::Transport::EnumAuthenticationType::PLAIN:
authStr = QStringLiteral("PLAIN");
break;
case MailTransport::Transport::EnumAuthenticationType::LOGIN:
authStr = QStringLiteral("LOGIN");
break;
case MailTransport::Transport::EnumAuthenticationType::CRAM_MD5:
authStr = QStringLiteral("CRAM-MD5");
break;
case MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5:
authStr = QStringLiteral("DIGEST-MD5");
break;
case MailTransport::Transport::EnumAuthenticationType::GSSAPI:
authStr = QStringLiteral("GSSAPI");
break;
case MailTransport::Transport::EnumAuthenticationType::ANONYMOUS:
authStr = QStringLiteral("ANONYMOUS");
break;
default:
authStr = QStringLiteral("PLAIN");
break;
}
QUrlQuery query;
query.addQueryItem(QStringLiteral("x-mech"), authStr);
if (resultSafety == QLatin1String("None")) {
accountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
query.addQueryItem(QStringLiteral("x-allow-unencrypted"), QStringLiteral("true"));
} else if (resultSafety == QLatin1String("SSL")) {
accountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::SSLorTLS);
} else if (resultSafety == QLatin1String("STARTTLS")) {
accountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::STARTTLS);
} else {
accountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
}
u.setQuery(query);
const QString resultCustomAuthentication = interface->sieveCustomAuthentification();
if (resultCustomAuthentication == QLatin1String("ImapUserPassword")) {
u.setUserName(interface->userName());
const QString imapPwd = provider->password(identifier);
u.setPassword(imapPwd);
} else if (resultCustomAuthentication == QLatin1String("CustomUserPassword")) {
const QString customPwd = provider->sieveCustomPassword(identifier);
u.setPassword(customPwd);
u.setUserName(interface->sieveCustomUsername());
} else {
qCWarning(LIBKSIEVE_LOG) << "resultCustomAuthentication undefined " << resultCustomAuthentication;
}
u = u.adjusted(QUrl::RemoveFilename);
if (withVacationFileName) {
u.setPath(u.path() + QLatin1Char('/') + interface->sieveVacationFilename());
}
accountInfo.sieveUrl = u;
return accountInfo;
}
}
QStringList KSieveUi::Util::sieveImapResourceNames()
{
const QVector<KSieveUi::SieveImapInstance> lst = KSieveUi::Util::sieveImapInstances();
......
......@@ -33,27 +33,21 @@ KSIEVEUI_EXPORT QDebug operator <<(QDebug d, const Util::AccountInfo &info);
/**
* Returns the list of configured IMAP agent instances.
*/
QVector<KSieveUi::SieveImapInstance> sieveImapInstances();
QStringList sieveImapResourceNames();
Q_REQUIRED_RESULT QVector<KSieveUi::SieveImapInstance> sieveImapInstances();
Q_REQUIRED_RESULT QStringList sieveImapResourceNames();
/**
* Checks if a server has KEP:14 support
*/
bool hasKep14Support(const QStringList &sieveCapabilities, const QStringList &availableScripts, const QString &activeScript);
Q_REQUIRED_RESULT bool hasKep14Support(const QStringList &sieveCapabilities, const QStringList &availableScripts, const QString &activeScript);
bool hasKep14CapabilitySupport(const QStringList &sieveCapabilities);
Q_REQUIRED_RESULT bool hasKep14CapabilitySupport(const QStringList &sieveCapabilities);
/**
* Is the given scriptName a protected KEP:14 name, that a normal user should not touch directly.
* it tests against MASTER, USER and MANAGEMENT script
*/
bool isKep14ProtectedName(const QString &scriptName);
KSIEVEUI_EXPORT KSieveUi::Util::AccountInfo findAccountInfo(const QString &identifier, SieveImapPasswordProvider *provider, bool withVacationFileName, std::unique_ptr<KSieveUi::AbstractAkonadiImapSettingInterface> &interface);
/**
* Returns all sieve account info with the given @p identifier.
*/
KSIEVEUI_EXPORT KSieveUi::Util::AccountInfo fullAccountInfo(const QString &identifier, SieveImapPasswordProvider *provider, bool withVacationFileName = true);
Q_REQUIRED_RESULT bool isKep14ProtectedName(const QString &scriptName);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment