File indexing completed on 2025-01-05 03:58:12

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2011-03-22
0007  * Description : a MediaWiki C++ interface
0008  *
0009  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2011      by Alexandre Mendes <alex dot mendes1988 at gmail dot com>
0011  * SPDX-FileCopyrightText: 2011      by Hormiere Guillaume <hormiere dot guillaume at gmail dot com>
0012  * SPDX-FileCopyrightText: 2011      by Manuel Campomanes <campomanes dot manuel at gmail dot com>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 // Qt includes
0019 
0020 #include <QObject>
0021 #include <QTest>
0022 
0023 // KDE includes
0024 
0025 #include <kjob.h>
0026 
0027 // Local includes
0028 
0029 #include "mediawiki_iface.h"
0030 #include "mediawiki_login.h"
0031 #include "fakeserver/fakeserver.h"
0032 
0033 using MediaWiki::Iface;
0034 using MediaWiki::Login;
0035 
0036 class Q_DECL_HIDDEN LoginTest : public QObject
0037 {
0038     Q_OBJECT
0039 
0040 public:
0041 
0042     explicit LoginTest(QObject* const parent = nullptr)
0043     {
0044         Q_UNUSED(parent);
0045         loginCount = 0;
0046         m_server   = 0;
0047     }
0048 
0049 public Q_SLOTS:
0050 
0051     void loginHandle(KJob* job)
0052     {
0053         Q_UNUSED(job)
0054         loginCount++;
0055     }
0056 
0057 private Q_SLOTS:
0058 
0059     void initTestCase()
0060     {
0061         loginCount     = 0;
0062         this->m_server = new FakeServer;
0063     }
0064 
0065     // Failing due to https://bugreports.qt.io/browse/QTBUG-44860
0066     void loginTestConnectTrue()
0067     {
0068         loginCount = 0;
0069         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0070         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0071         m_server->setScenario(senario, cookie);
0072         senario = QStringLiteral("<api><login result=\"Success\" lguserid=\"12345\" lgusername=\"MyUsername\" lgtoken=\"b5780b6e2f27e20b450921d9461010b4\" cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>");
0073         m_server->addScenario(senario, cookie);
0074         m_server->startAndWait();
0075 
0076         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0077         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0078 
0079         connect(&login, SIGNAL(result(KJob*)),
0080                 this, SLOT(loginHandle(KJob*)));
0081 
0082         login.exec();   // krazy:exclude=crashy
0083         FakeServer::Request serverrequest = m_server->getRequest()[0];
0084         QCOMPARE(this->loginCount, 1);
0085         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0086         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0087         QCOMPARE(login.error(), (int)Login::NoError);
0088     }
0089 
0090     void loginTestAttribute()
0091     {
0092         loginCount = 0;
0093         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0094         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0095         m_server->setScenario(senario, cookie);
0096         senario = QStringLiteral("<api><login result=\"Success\" lguserid=\"12345\" lgusername=\"MyUsername\" lgtoken=\"b5780b6e2f27e20b450921d9461010b4\" cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /></api>");
0097         m_server->addScenario(senario, cookie);
0098         m_server->startAndWait();
0099 
0100         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));         // krazy:exclude=insecurenet
0101         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0102 
0103         connect(&login, SIGNAL(result(KJob*)),
0104                 this, SLOT(loginHandle(KJob*)));
0105 
0106         login.exec();   // krazy:exclude=crashy
0107 
0108         FakeServer::Request serverrequest = m_server->getRequest()[0];
0109         QCOMPARE(this->loginCount, 1);
0110         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0111         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0112         QCOMPARE(login.error(), (int)Login::NoError);
0113     }
0114 
0115     void loginTestConnectTrueWithoutCookie()
0116     {
0117         loginCount = 0;
0118         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0119         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0120         m_server->setScenario(senario);
0121         senario = QStringLiteral("<api><login result=\"Success\" lguserid=\"12345\" lgusername=\"MyUsername\" lgtoken=\"b5780b6e2f27e20b450921d9461010b4\" cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /></api>");
0122         m_server->addScenario(senario, cookie);
0123         m_server->startAndWait();
0124 
0125         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0126         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0127 
0128         connect(&login, SIGNAL(result(KJob*)),
0129                 this, SLOT(loginHandle(KJob*)));
0130 
0131         login.exec();   // krazy:exclude=crashy
0132         QCOMPARE(this->loginCount, 1);
0133         QCOMPARE(login.error(), (int)Login::NoError);
0134     }
0135 
0136     void loginTestFalseXMLLogin()
0137     {
0138         loginCount = 0;
0139         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" </api>") );
0140         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0141         m_server->setScenario(senario, cookie);
0142         senario = QStringLiteral("<api><login result=\"Success\" lguserid=\"12345\" lgusername=\"MyUsername\" lgtoken=\"b5780b6e2f27e20b450921d9461010b4\" cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /></api>");
0143         m_server->addScenario(senario);
0144         m_server->startAndWait();
0145 
0146         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0147         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0148 
0149         connect(&login, SIGNAL(result(KJob*)),
0150                 this, SLOT(loginHandle(KJob*)));
0151 
0152         login.exec();   // krazy:exclude=crashy
0153         FakeServer::Request serverrequest = m_server->getRequest()[0];
0154         QCOMPARE(this->loginCount, 1);
0155         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0156         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0157         QCOMPARE(login.error(), (int)Login::XmlError);
0158     }
0159 
0160     void loginTestNoNameLogin()
0161     {
0162         loginCount = 0;
0163         QString senario(QStringLiteral("<api><login result=\"NoName\" info=\"\" /> </api>") );
0164         m_server->setScenario(senario);
0165         m_server->startAndWait();
0166 
0167         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0168         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0169 
0170         connect(&login, SIGNAL(result(KJob*)),
0171                 this, SLOT(loginHandle(KJob*)));
0172 
0173         login.exec();   // krazy:exclude=crashy
0174         FakeServer::Request serverrequest = m_server->getRequest()[0];
0175         QCOMPARE(this->loginCount, 1);
0176         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0177         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0178 
0179         QCOMPARE(login.error(), (int)Login::LoginMissing);
0180     }
0181 
0182     void loginTestIllegalLogin()
0183     {
0184         loginCount = 0;
0185         QString senario(QStringLiteral("<api><login result=\"Illegal\" info=\"\" /> </api>") );
0186         m_server->setScenario(senario);
0187         m_server->startAndWait();
0188 
0189         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0190         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0191 
0192         connect(&login, SIGNAL(result(KJob*)),
0193                 this, SLOT(loginHandle(KJob*)));
0194 
0195         login.exec();   // krazy:exclude=crashy
0196         FakeServer::Request serverrequest = m_server->getRequest()[0];
0197         QCOMPARE(this->loginCount, 1);
0198         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0199         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0200 
0201         QCOMPARE(login.error(), (int)Login::IllegalUsername);
0202     }
0203 
0204     void loginTestNotExistsLogin()
0205     {
0206         loginCount = 0;
0207         QString senario(QStringLiteral("<api><login result=\"NotExists\" info=\"\" /> </api>") );
0208         m_server->setScenario(senario);
0209         m_server->startAndWait();
0210 
0211         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0212         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0213 
0214         connect(&login, SIGNAL(result(KJob*)),
0215                 this, SLOT(loginHandle(KJob*)));
0216 
0217         login.exec();   // krazy:exclude=crashy
0218         FakeServer::Request serverrequest = m_server->getRequest()[0];
0219         QCOMPARE(this->loginCount, 1);
0220         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0221         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0222 
0223         QCOMPARE(login.error(), (int)Login::UsernameNotExists);
0224     }
0225 
0226     void loginTestEmptyPassLogin()
0227     {
0228         loginCount = 0;
0229         QString senario(QStringLiteral("<api><login result=\"EmptyPass\" info=\"\" /> </api>") );
0230         m_server->setScenario(senario);
0231         m_server->startAndWait();
0232 
0233         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0234         Login login(site, QStringLiteral("MyUsername"), QString());
0235 
0236         connect(&login, SIGNAL(result(KJob*)),
0237                 this, SLOT(loginHandle(KJob*)));
0238 
0239         login.exec();   // krazy:exclude=crashy
0240         FakeServer::Request serverrequest = m_server->getRequest()[0];
0241         QCOMPARE(this->loginCount, 1);
0242         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0243         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword="));
0244 
0245         QCOMPARE(login.error(), (int)Login::PasswordMissing);
0246     }
0247 
0248     void loginTestWrongPassLogin()
0249     {
0250         loginCount = 0;
0251         QString senario(QStringLiteral("<api><login result=\"WrongPass\" info=\"\" /> </api>") );
0252         m_server->setScenario(senario);
0253         m_server->startAndWait();
0254 
0255         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0256         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0257 
0258         connect(&login, SIGNAL(result(KJob*)),
0259                 this, SLOT(loginHandle(KJob*)));
0260 
0261         login.exec();   // krazy:exclude=crashy
0262         FakeServer::Request serverrequest = m_server->getRequest()[0];
0263         QCOMPARE(this->loginCount, 1);
0264         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0265         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0266 
0267         QCOMPARE(login.error(), (int)Login::WrongPassword);
0268     }
0269 
0270     void loginTestWrongPluginPassLogin()
0271     {
0272         loginCount = 0;
0273         QString senario(QStringLiteral("<api><login result=\"WrongPluginPass\" info=\"\" /> </api>") );
0274         m_server->setScenario(senario);
0275         m_server->startAndWait();
0276 
0277         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0278         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0279 
0280         connect(&login, SIGNAL(result(KJob*)),
0281                 this, SLOT(loginHandle(KJob*)));
0282 
0283         login.exec();   // krazy:exclude=crashy
0284         FakeServer::Request serverrequest = m_server->getRequest()[0];
0285         QCOMPARE(this->loginCount, 1);
0286         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0287         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0288 
0289         QCOMPARE(login.error(), (int)Login::WrongPluginPassword);
0290     }
0291 
0292     void loginTestCreateBlockedLogin()
0293     {
0294         loginCount = 0;
0295         QString senario(QStringLiteral("<api><login result=\"CreateBlocked\" info=\"\" /> </api>") );
0296         m_server->setScenario(senario);
0297         m_server->startAndWait();
0298 
0299         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0300         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0301 
0302         connect(&login, SIGNAL(result(KJob*)),
0303                 this, SLOT(loginHandle(KJob*)));
0304 
0305         login.exec();   // krazy:exclude=crashy
0306         FakeServer::Request serverrequest = m_server->getRequest()[0];
0307         QCOMPARE(this->loginCount, 1);
0308         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0309         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0310 
0311         QCOMPARE(login.error(), (int)Login::IPAddressBlocked);
0312     }
0313 
0314     void loginTestThrottledLogin()
0315     {
0316         loginCount = 0;
0317         QString senario(QStringLiteral("<api><login result=\"Throttled\" info=\"\" /> </api>") );
0318         m_server->setScenario(senario);
0319         m_server->startAndWait();
0320 
0321         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0322         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0323 
0324         connect(&login, SIGNAL(result(KJob*)),
0325                 this, SLOT(loginHandle(KJob*)));
0326 
0327         login.exec();   // krazy:exclude=crashy
0328         FakeServer::Request serverrequest = m_server->getRequest()[0];
0329         QCOMPARE(this->loginCount, 1);
0330         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0331         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0332 
0333         QCOMPARE(login.error(), (int)Login::TooManyConnections);
0334     }
0335 
0336     void loginTestBlockedLogin()
0337     {
0338         loginCount = 0;
0339         QString senario(QStringLiteral("<api><login result=\"Blocked\" info=\"\" /> </api>") );
0340         m_server->setScenario(senario);
0341         m_server->startAndWait();
0342 
0343         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0344         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0345 
0346         connect(&login, SIGNAL(result(KJob*)),
0347                 this, SLOT(loginHandle(KJob*)));
0348 
0349         login.exec();   // krazy:exclude=crashy
0350         FakeServer::Request serverrequest = m_server->getRequest()[0];
0351         QCOMPARE(this->loginCount, 1);
0352         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0353         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0354 
0355         QCOMPARE(login.error(), (int)Login::UserBlocked);
0356     }
0357 
0358     void loginTestFalseXMLToken()
0359     {
0360         loginCount = 0;
0361         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0362         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0363         m_server->setScenario(senario, cookie);
0364         senario = QStringLiteral("<api><login result=\"Success\" lguserid=\"12345\" lgusername=\"MyUsername\" lgtoken=\"b5780b6e2f27e20b450921d9461010b4\" cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\"</api>");
0365         m_server->addScenario(senario);
0366         m_server->startAndWait();
0367 
0368         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0369         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0370 
0371         connect(&login, SIGNAL(result(KJob*)),
0372                 this, SLOT(loginHandle(KJob*)));
0373 
0374         login.exec();   // krazy:exclude=crashy
0375         FakeServer::Request serverrequest = m_server->getRequest()[0];
0376         QCOMPARE(this->loginCount, 1);
0377         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0378         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0379         QCOMPARE(login.error(), (int)Login::XmlError);
0380     }
0381 
0382     void loginTestNoNameToken()
0383     {
0384         loginCount = 0;
0385         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0386         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0387         m_server->setScenario(senario, cookie);
0388         senario = QStringLiteral("<api><login result=\"NoName\" info=\"\" /> </api>") ;
0389         m_server->addScenario(senario);
0390         m_server->startAndWait();
0391 
0392         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0393         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0394 
0395         connect(&login, SIGNAL(result(KJob*)),
0396                 this, SLOT(loginHandle(KJob*)));
0397 
0398         login.exec();   // krazy:exclude=crashy
0399         FakeServer::Request serverrequest = m_server->getRequest()[0];
0400         QCOMPARE(this->loginCount, 1);
0401         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0402         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0403 
0404         QCOMPARE(login.error(), (int)Login::LoginMissing);
0405     }
0406 
0407     void loginTestIllegalToken()
0408     {
0409         loginCount = 0;
0410         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0411         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0412         m_server->setScenario(senario, cookie);
0413         senario = QStringLiteral("<api><login result=\"Illegal\" info=\"\" /> </api>") ;
0414         m_server->addScenario(senario);
0415         m_server->startAndWait();
0416 
0417         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0418         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0419 
0420         connect(&login, SIGNAL(result(KJob*)),
0421                 this, SLOT(loginHandle(KJob*)));
0422 
0423         login.exec();   // krazy:exclude=crashy
0424         FakeServer::Request serverrequest = m_server->getRequest()[0];
0425         QCOMPARE(this->loginCount, 1);
0426         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0427         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0428 
0429         QCOMPARE(login.error(), (int)Login::IllegalUsername);
0430     }
0431 
0432     void loginTestNotExistsToken()
0433     {
0434         loginCount = 0;
0435         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0436         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0437         m_server->setScenario(senario, cookie);
0438         senario = QStringLiteral("<api><login result=\"NotExists\" info=\"\" /> </api>") ;
0439         m_server->addScenario(senario);
0440         m_server->startAndWait();
0441 
0442         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0443         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0444 
0445         connect(&login, SIGNAL(result(KJob*)),
0446                 this, SLOT(loginHandle(KJob*)));
0447 
0448         login.exec();   // krazy:exclude=crashy
0449         FakeServer::Request serverrequest = m_server->getRequest()[0];
0450         QCOMPARE(this->loginCount, 1);
0451         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0452         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0453 
0454         QCOMPARE(login.error(), (int)Login::UsernameNotExists);
0455     }
0456 
0457     void loginTestEmptyPassToken()
0458     {
0459         loginCount = 0;
0460         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0461         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0462         m_server->setScenario(senario, cookie);
0463         senario = QStringLiteral("<api><login result=\"EmptyPass\" info=\"\" /> </api>") ;
0464         m_server->addScenario(senario);
0465         m_server->startAndWait();
0466 
0467         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0468         Login login(site, QStringLiteral("MyUsername"), QString());
0469 
0470         connect(&login, SIGNAL(result(KJob*)),
0471                 this, SLOT(loginHandle(KJob*)));
0472 
0473         login.exec();   // krazy:exclude=crashy
0474         FakeServer::Request serverrequest = m_server->getRequest()[0];
0475         QCOMPARE(this->loginCount, 1);
0476         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0477         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword="));
0478 
0479         QCOMPARE(login.error(), (int)Login::PasswordMissing);
0480     }
0481 
0482     void loginTestWrongPassToken()
0483     {
0484         loginCount = 0;
0485         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0486         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0487         m_server->setScenario(senario, cookie);
0488         senario = QStringLiteral("<api><login result=\"WrongPass\" info=\"\" /> </api>") ;
0489         m_server->addScenario(senario);
0490         m_server->startAndWait();
0491 
0492         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0493         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0494 
0495         connect(&login, SIGNAL(result(KJob*)),
0496                 this, SLOT(loginHandle(KJob*)));
0497 
0498         login.exec();   // krazy:exclude=crashy
0499         FakeServer::Request serverrequest = m_server->getRequest()[0];
0500         QCOMPARE(this->loginCount, 1);
0501         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0502         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0503 
0504         QCOMPARE(login.error(), (int)Login::WrongPassword);
0505     }
0506 
0507     void loginTestWrongPluginPassToken()
0508     {
0509         loginCount = 0;
0510         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0511         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0512         m_server->setScenario(senario, cookie);
0513         senario = QStringLiteral("<api><login result=\"WrongPluginPass\" info=\"\" /> </api>") ;
0514         m_server->addScenario(senario);
0515         m_server->startAndWait();
0516 
0517         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0518         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0519 
0520         connect(&login, SIGNAL(result(KJob*)),
0521                 this, SLOT(loginHandle(KJob*)));
0522 
0523         login.exec();   // krazy:exclude=crashy
0524         FakeServer::Request serverrequest = m_server->getRequest()[0];
0525         QCOMPARE(this->loginCount, 1);
0526         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0527         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0528 
0529         QCOMPARE(login.error(), (int)Login::WrongPluginPassword);
0530     }
0531 
0532     void loginTestCreateBlockedToken()
0533     {
0534         loginCount = 0;
0535         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0536         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0537         m_server->setScenario(senario, cookie);
0538         senario = QStringLiteral("<api><login result=\"CreateBlocked\" info=\"\" /> </api>") ;
0539         m_server->addScenario(senario);
0540         m_server->startAndWait();
0541 
0542         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0543         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0544 
0545         connect(&login, SIGNAL(result(KJob*)),
0546                 this, SLOT(loginHandle(KJob*)));
0547 
0548         login.exec();   // krazy:exclude=crashy
0549         FakeServer::Request serverrequest = m_server->getRequest()[0];
0550         QCOMPARE(this->loginCount, 1);
0551         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0552         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0553 
0554         QCOMPARE(login.error(), (int)Login::IPAddressBlocked);
0555     }
0556 
0557     void loginTestThrottledToken()
0558     {
0559         loginCount = 0;
0560         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0561         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0562         m_server->setScenario(senario, cookie);
0563         senario = QStringLiteral("<api><login result=\"Throttled\" info=\"\" /> </api>") ;
0564         m_server->addScenario(senario);
0565         m_server->startAndWait();
0566 
0567         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0568         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0569 
0570         connect(&login, SIGNAL(result(KJob*)),
0571                 this, SLOT(loginHandle(KJob*)));
0572 
0573         login.exec();   // krazy:exclude=crashy
0574         FakeServer::Request serverrequest = m_server->getRequest()[0];
0575         QCOMPARE(this->loginCount, 1);
0576         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0577         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0578 
0579         QCOMPARE(login.error(), (int)Login::TooManyConnections);
0580     }
0581 
0582     void loginTestBlockedToken()
0583     {
0584         loginCount = 0;
0585         QString senario(QStringLiteral("<api><login result=\"NeedToken\" token=\"b5780b6e2f27e20b450921d9461010b4\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" /> </api>") );
0586         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\""));
0587         m_server->setScenario(senario, cookie);
0588         senario = QStringLiteral("<api><login result=\"Blocked\" info=\"\" /> </api>") ;
0589         m_server->addScenario(senario);
0590         m_server->startAndWait();
0591 
0592         Iface site(QUrl(QStringLiteral("http://127.0.0.1:12566")));     // krazy:exclude=insecurenet
0593         Login login(site, QStringLiteral("MyUsername"), QStringLiteral("test"));
0594 
0595         connect(&login, SIGNAL(result(KJob*)),
0596                 this, SLOT(loginHandle(KJob*)));
0597 
0598         login.exec();   // krazy:exclude=crashy
0599         FakeServer::Request serverrequest = m_server->getRequest()[0];
0600         QCOMPARE(this->loginCount, 1);
0601         QCOMPARE(serverrequest.type, QStringLiteral("POST"));
0602         QCOMPARE(serverrequest.value, QStringLiteral("/?format=xml&action=login&lgname=MyUsername&lgpassword=test"));
0603 
0604         QCOMPARE(login.error(), (int)Login::UserBlocked);
0605     }
0606 
0607     void cleanupTestCase()
0608     {
0609         delete this->m_server;
0610     }
0611 
0612 private:
0613 
0614     int         loginCount;
0615     FakeServer* m_server;
0616 };
0617 
0618 QTEST_MAIN(LoginTest)
0619 
0620 #include "login_utest.moc"