File indexing completed on 2025-01-05 04:58:19

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "baloocompletionemailtest.h"
0009 #include "addressline/addresslineedit/baloocompletionemail.h"
0010 #include <QTest>
0011 BalooCompletionEmailTest::BalooCompletionEmailTest(QObject *parent)
0012     : QObject(parent)
0013 {
0014 }
0015 
0016 BalooCompletionEmailTest::~BalooCompletionEmailTest() = default;
0017 
0018 void BalooCompletionEmailTest::returnEmptyListWhenEmailListIsEmpty()
0019 {
0020     PimCommon::BalooCompletionEmail completion;
0021 
0022     QVERIFY(completion.cleanupEmailList().isEmpty());
0023     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0024     QVERIFY(info.mListEmail.isEmpty());
0025     QVERIFY(info.mBlackList.isEmpty());
0026     QVERIFY(info.mExcludeEmailsRegularExpressions.isEmpty());
0027     QVERIFY(info.mListEmail.isEmpty());
0028 }
0029 
0030 void BalooCompletionEmailTest::shouldReturnSameListWhenNotExclude()
0031 {
0032     PimCommon::BalooCompletionEmail completion;
0033     QStringList emailList;
0034     emailList << QStringLiteral("foo");
0035     emailList << QStringLiteral("foo2");
0036     emailList << QStringLiteral("foo3");
0037     emailList << QStringLiteral("foo4");
0038     emailList << QStringLiteral("foo5");
0039     emailList << QStringLiteral("foo6");
0040     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0041     info.mListEmail = emailList;
0042     completion.setBalooCompletionEmailInfo(info);
0043     QCOMPARE(completion.cleanupEmailList(), emailList);
0044 }
0045 
0046 void BalooCompletionEmailTest::shouldExcludeEmails()
0047 {
0048     {
0049         PimCommon::BalooCompletionEmail completion;
0050         QStringList emailList;
0051         emailList << QStringLiteral("foo@kde.org");
0052         emailList << QStringLiteral("foo2@kde.org");
0053         emailList << QStringLiteral("foo3@bli.com");
0054         emailList << QStringLiteral("foo4@blo.com");
0055         emailList << QStringLiteral("foo5@bli.com");
0056         emailList << QStringLiteral("foo6@ff.com");
0057         PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0058         info.mListEmail = emailList;
0059         QStringList excludeEmailsRegularExpressionsList;
0060         excludeEmailsRegularExpressionsList << QStringLiteral("foo6@ff\\.com");
0061         info.mExcludeEmailsRegularExpressions = excludeEmailsRegularExpressionsList;
0062         completion.setBalooCompletionEmailInfo(info);
0063 
0064         QStringList returnList;
0065         returnList << QStringLiteral("foo2@kde.org");
0066         returnList << QStringLiteral("foo3@bli.com");
0067         returnList << QStringLiteral("foo4@blo.com");
0068         returnList << QStringLiteral("foo5@bli.com");
0069         returnList << QStringLiteral("foo@kde.org");
0070         QCOMPARE(completion.cleanupEmailList(), returnList);
0071     }
0072 
0073     {
0074         PimCommon::BalooCompletionEmail completion;
0075         QStringList emailList;
0076         emailList << QStringLiteral("foo@kde.org");
0077         emailList << QStringLiteral("foo2@kde.org");
0078         emailList << QStringLiteral("foo3@bli.com");
0079         emailList << QStringLiteral("foo4@blo.com");
0080         emailList << QStringLiteral("foo5@bli.com");
0081         emailList << QStringLiteral("foo6@ff.com");
0082         PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0083         info.mListEmail = emailList;
0084         QStringList excludeEmailsRegularExpressionsList;
0085         excludeEmailsRegularExpressionsList << QStringLiteral("foo.*@kde\\.org");
0086         info.mExcludeEmailsRegularExpressions = excludeEmailsRegularExpressionsList;
0087         completion.setBalooCompletionEmailInfo(info);
0088 
0089         QStringList returnList;
0090         returnList << QStringLiteral("foo3@bli.com");
0091         returnList << QStringLiteral("foo4@blo.com");
0092         returnList << QStringLiteral("foo5@bli.com");
0093         returnList << QStringLiteral("foo6@ff.com");
0094         QCOMPARE(completion.cleanupEmailList(), returnList);
0095     }
0096     {
0097         PimCommon::BalooCompletionEmail completion;
0098         QStringList emailList;
0099         emailList << QStringLiteral("foo@kde.org");
0100         emailList << QStringLiteral("foo2@kde.org");
0101         emailList << QStringLiteral("foo3@bli.com");
0102         emailList << QStringLiteral("foo4@blo.com");
0103         emailList << QStringLiteral("foo5@bli.com");
0104         emailList << QStringLiteral("foo6@ff.com");
0105         PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0106         info.mListEmail = emailList;
0107         QStringList excludeEmailsRegularExpressionsList;
0108         excludeEmailsRegularExpressionsList << QStringLiteral("foo.*@kde\\.org");
0109         excludeEmailsRegularExpressionsList << QStringLiteral("foo6@ff\\.com");
0110         info.mExcludeEmailsRegularExpressions = excludeEmailsRegularExpressionsList;
0111         completion.setBalooCompletionEmailInfo(info);
0112 
0113         QStringList returnList;
0114         returnList << QStringLiteral("foo3@bli.com");
0115         returnList << QStringLiteral("foo4@blo.com");
0116         returnList << QStringLiteral("foo5@bli.com");
0117         QCOMPARE(completion.cleanupEmailList(), returnList);
0118     }
0119     {
0120         PimCommon::BalooCompletionEmail completion;
0121         QStringList emailList;
0122         emailList << QStringLiteral("foo@kde.org");
0123         emailList << QStringLiteral("foo2@kde.org");
0124         emailList << QStringLiteral("foo3@bli.com");
0125         emailList << QStringLiteral("foo4@blo.com");
0126         emailList << QStringLiteral("foo5@bli.com");
0127         emailList << QStringLiteral("foo6@ff.com");
0128         PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0129         info.mListEmail = emailList;
0130         QStringList excludeEmailsRegularExpressionsList;
0131         excludeEmailsRegularExpressionsList << QStringLiteral("foo.*@kde\\.org");
0132         excludeEmailsRegularExpressionsList << QStringLiteral("foo6@ff\\.com");
0133         info.mExcludeEmailsRegularExpressions = excludeEmailsRegularExpressionsList;
0134 
0135         info.mExcludeDomains = QStringList() << QStringLiteral("bli.com");
0136         completion.setBalooCompletionEmailInfo(info);
0137 
0138         QStringList returnList;
0139         returnList << QStringLiteral("foo4@blo.com");
0140         QCOMPARE(completion.cleanupEmailList(), returnList);
0141     }
0142     {
0143         PimCommon::BalooCompletionEmail completion;
0144         QStringList emailList;
0145         emailList << QStringLiteral("foo@kde.org");
0146         emailList << QStringLiteral("foo2@kde.org");
0147         emailList << QStringLiteral("foo3@bli.com");
0148         emailList << QStringLiteral("foo4@blo.com");
0149         emailList << QStringLiteral("foo5@bli.com");
0150         emailList << QStringLiteral("foo6@ff.com");
0151         emailList << QStringLiteral("notifications@github.com");
0152         emailList << QStringLiteral("incoming+000554@gitlab.com");
0153         emailList << QStringLiteral("blabla@kde.org");
0154         PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0155         info.mListEmail = emailList;
0156         QStringList excludeEmailsRegularExpressionsList;
0157         excludeEmailsRegularExpressionsList << QStringLiteral("foo.*@kde\\.org");
0158         excludeEmailsRegularExpressionsList << QStringLiteral("foo6@ff\\.com");
0159         excludeEmailsRegularExpressionsList << QStringLiteral("notifications@github\\.com");
0160         excludeEmailsRegularExpressionsList << QStringLiteral("incoming\\+.+@gitlab\\.com");
0161         info.mExcludeEmailsRegularExpressions = excludeEmailsRegularExpressionsList;
0162 
0163         info.mExcludeDomains = QStringList() << QStringLiteral("bli.com");
0164         completion.setBalooCompletionEmailInfo(info);
0165 
0166         QStringList returnList;
0167         returnList << QStringLiteral("blabla@kde.org") << QStringLiteral("foo4@blo.com");
0168         QCOMPARE(completion.cleanupEmailList(), returnList);
0169     }
0170 }
0171 
0172 void BalooCompletionEmailTest::shouldReturnSameListIfBlackListInterceptEmail()
0173 {
0174     PimCommon::BalooCompletionEmail completion;
0175     QStringList emailList;
0176     emailList << QStringLiteral("foo");
0177     emailList << QStringLiteral("foo2");
0178     emailList << QStringLiteral("foo3");
0179     emailList << QStringLiteral("foo4");
0180     emailList << QStringLiteral("foo5");
0181     emailList << QStringLiteral("foo6");
0182     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0183     info.mListEmail = emailList;
0184     completion.setBalooCompletionEmailInfo(info);
0185 
0186     QStringList blackList;
0187     blackList << QStringLiteral("foo");
0188     blackList << QStringLiteral("bla2");
0189     blackList << QStringLiteral("bla3");
0190     blackList << QStringLiteral("bla4");
0191     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info2;
0192     info2.mListEmail = emailList;
0193     info2.mBlackList = blackList;
0194     completion.setBalooCompletionEmailInfo(info2);
0195     QStringList returnList;
0196     returnList << QStringLiteral("foo2");
0197     returnList << QStringLiteral("foo3");
0198     returnList << QStringLiteral("foo4");
0199     returnList << QStringLiteral("foo5");
0200     returnList << QStringLiteral("foo6");
0201     QCOMPARE(completion.cleanupEmailList(), returnList);
0202 }
0203 
0204 void BalooCompletionEmailTest::shouldReturnSameListIfBlackListDoesntInterceptEmail()
0205 {
0206     PimCommon::BalooCompletionEmail completion;
0207     QStringList emailList;
0208     emailList << QStringLiteral("foo");
0209     emailList << QStringLiteral("foo2");
0210     emailList << QStringLiteral("foo3");
0211     emailList << QStringLiteral("foo4");
0212     emailList << QStringLiteral("foo5");
0213     emailList << QStringLiteral("foo6");
0214     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0215     info.mListEmail = emailList;
0216     completion.setBalooCompletionEmailInfo(info);
0217 
0218     QStringList blackList;
0219     blackList << QStringLiteral("bla");
0220     blackList << QStringLiteral("bla2");
0221     blackList << QStringLiteral("bla3");
0222     blackList << QStringLiteral("bla4");
0223     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info2;
0224     info2.mListEmail = emailList;
0225     info2.mBlackList = blackList;
0226     completion.setBalooCompletionEmailInfo(info2);
0227     QCOMPARE(completion.cleanupEmailList(), emailList);
0228 }
0229 
0230 void BalooCompletionEmailTest::shouldReturnUniqueEmail()
0231 {
0232     PimCommon::BalooCompletionEmail completion;
0233     QStringList emailList;
0234     emailList << QStringLiteral("foo");
0235     emailList << QStringLiteral("foo");
0236     emailList << QStringLiteral("foo1");
0237     emailList << QStringLiteral("foo");
0238     emailList << QStringLiteral("foo1");
0239     emailList << QStringLiteral("foo2");
0240     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0241     info.mListEmail = emailList;
0242     completion.setBalooCompletionEmailInfo(info);
0243     QCOMPARE(completion.cleanupEmailList(), (QStringList() << QStringLiteral("foo") << QStringLiteral("foo1") << QStringLiteral("foo2")));
0244 }
0245 
0246 void BalooCompletionEmailTest::shouldReturnEmptyListWhenAllBlackListed()
0247 {
0248     PimCommon::BalooCompletionEmail completion;
0249     QStringList emailList;
0250     emailList << QStringLiteral("foo");
0251     emailList << QStringLiteral("foo2");
0252     emailList << QStringLiteral("foo3");
0253     emailList << QStringLiteral("foo4");
0254     emailList << QStringLiteral("foo5");
0255     emailList << QStringLiteral("foo6");
0256     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0257     info.mListEmail = emailList;
0258     info.mBlackList = emailList;
0259     completion.setBalooCompletionEmailInfo(info);
0260     QVERIFY(completion.cleanupEmailList().isEmpty());
0261 }
0262 
0263 void BalooCompletionEmailTest::shouldExcludeDomain()
0264 {
0265     PimCommon::BalooCompletionEmail completion;
0266     QStringList emailList;
0267     emailList << QStringLiteral("foo@kde.org");
0268     emailList << QStringLiteral("foo2@kde.org");
0269     emailList << QStringLiteral("foo3@kde.org");
0270     emailList << QStringLiteral("foo4@kde.org");
0271     emailList << QStringLiteral("foo5@kde.org");
0272     emailList << QStringLiteral("foo6@kde.org");
0273     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0274     info.mListEmail = emailList;
0275     info.mExcludeDomains = QStringList() << QStringLiteral("kde.org");
0276     completion.setBalooCompletionEmailInfo(info);
0277     QVERIFY(completion.cleanupEmailList().isEmpty());
0278 
0279     const QString newAddress = QStringLiteral("foo6@linux.org");
0280     emailList << newAddress;
0281     info.mListEmail = emailList;
0282     completion.setBalooCompletionEmailInfo(info);
0283     QCOMPARE(completion.cleanupEmailList(), (QStringList() << newAddress));
0284 
0285     info.mExcludeDomains = QStringList() << QStringLiteral("kde.org") << QStringLiteral("linux.org");
0286     completion.setBalooCompletionEmailInfo(info);
0287     QVERIFY(completion.cleanupEmailList().isEmpty());
0288 }
0289 
0290 void BalooCompletionEmailTest::shouldReturnEmailListWhenDomainListIsNotNull()
0291 {
0292     PimCommon::BalooCompletionEmail completion;
0293     QStringList emailList;
0294     emailList << QStringLiteral("foo@kde.org");
0295     emailList << QStringLiteral("foo2@kde.org");
0296     emailList << QStringLiteral("foo3@kde.org");
0297     emailList << QStringLiteral("foo4@kde.org");
0298     emailList << QStringLiteral("foo5@kde.org");
0299     emailList << QStringLiteral("foo6@kde.org");
0300     emailList.sort();
0301     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0302     info.mListEmail = emailList;
0303     info.mExcludeDomains = QStringList() << QString();
0304     completion.setBalooCompletionEmailInfo(info);
0305     QCOMPARE(completion.cleanupEmailList(), emailList);
0306 }
0307 
0308 void BalooCompletionEmailTest::shouldDontDuplicateEmailWhenUseCase()
0309 {
0310     PimCommon::BalooCompletionEmail completion;
0311     QStringList emailList;
0312     emailList << QStringLiteral("foo");
0313     emailList << QStringLiteral("foo2");
0314     emailList << QStringLiteral("foo3");
0315     emailList << QStringLiteral("foo4");
0316     emailList << QStringLiteral("foo5");
0317     emailList << QStringLiteral("foo6");
0318 
0319     QStringList caseEmailList;
0320     caseEmailList << QStringLiteral("Foo");
0321     caseEmailList << QStringLiteral("fOo2");
0322     caseEmailList << QStringLiteral("FOo3");
0323     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0324     info.mListEmail = emailList;
0325     completion.setBalooCompletionEmailInfo(info);
0326     QCOMPARE(completion.cleanupEmailList(), emailList);
0327 }
0328 
0329 void BalooCompletionEmailTest::shouldExcludeDuplicateEntryWithDisplayName()
0330 {
0331     PimCommon::BalooCompletionEmail completion;
0332     QStringList emailList;
0333     emailList << QStringLiteral("John Doe <doe@example.com>");
0334     emailList << QStringLiteral("\"John Doe\" <doe@example.com>");
0335     emailList << QStringLiteral("\"\'John Doe\'\" <doe@example.com>");
0336 
0337     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0338     info.mListEmail = emailList;
0339     completion.setBalooCompletionEmailInfo(info);
0340     QCOMPARE(completion.cleanupEmailList().count(), 1);
0341 }
0342 
0343 void BalooCompletionEmailTest::shouldExcludeDuplicateEntryWithDisplayNameAddAddressWithDifferentCase()
0344 {
0345     PimCommon::BalooCompletionEmail completion;
0346     QStringList emailList;
0347     emailList << QStringLiteral("John Doe <doe@example.com>");
0348     emailList << QStringLiteral("\"John Doe\" <doe@example.com>");
0349     emailList << QStringLiteral("\"\'John Doe\'\" <doe@example.com>");
0350     emailList << QStringLiteral("\"\"\"\"\'John Doe\'\"\"\"\" <doe@example.com>");
0351     emailList << QStringLiteral("John Doe <Doe@example.com>");
0352     emailList << QStringLiteral("John Doe <DOE@example.com>");
0353     emailList << QStringLiteral("John Doe <dOE@example.com>");
0354     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0355     info.mListEmail = emailList;
0356     completion.setBalooCompletionEmailInfo(info);
0357     QCOMPARE(completion.cleanupEmailList().count(), 1);
0358 }
0359 
0360 void BalooCompletionEmailTest::shouldExcludeDuplicateEntryWithDifferentDisplayNameAddAddressWithDifferentCase()
0361 {
0362     PimCommon::BalooCompletionEmail completion;
0363     QStringList emailList;
0364     emailList << QStringLiteral("John Doe <doe@example.com>");
0365     emailList << QStringLiteral("\"John Doe\" <doe@example.com>");
0366     emailList << QStringLiteral("\"\'John Doe\'\" <doe@example.com>");
0367     emailList << QStringLiteral("John Doe <doe@example.com>");
0368     emailList << QStringLiteral("Doe John <Doe@example.com>");
0369     emailList << QStringLiteral("John <DOE@example.com>");
0370     emailList << QStringLiteral("Doe <dOE@example.com>");
0371     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0372     info.mListEmail = emailList;
0373     completion.setBalooCompletionEmailInfo(info);
0374     QCOMPARE(completion.cleanupEmailList().count(), 1);
0375 }
0376 
0377 void BalooCompletionEmailTest::shouldExcludeEmptyNameWithDisplayNameForSameAddress()
0378 {
0379     PimCommon::BalooCompletionEmail completion;
0380     QStringList emailList;
0381     emailList << QStringLiteral("doe@example.com");
0382     emailList << QStringLiteral("Doe John <Doe@example.com>");
0383     emailList << QStringLiteral("John <DOE@example.com>");
0384     emailList << QStringLiteral("Doe <dOE@example.com>");
0385     PimCommon::BalooCompletionEmail::BalooCompletionEmailInfo info;
0386     info.mListEmail = emailList;
0387     completion.setBalooCompletionEmailInfo(info);
0388     QCOMPARE(completion.cleanupEmailList().count(), 1);
0389 }
0390 
0391 QTEST_MAIN(BalooCompletionEmailTest)
0392 
0393 #include "moc_baloocompletionemailtest.cpp"