File indexing completed on 2024-04-14 14:55:13

0001 /** ===========================================================
0002  * @file
0003  *
0004  * This file is a part of KDE project
0005  * <a href="https://commits.kde.org/libmediawiki">libmediawiki</a>
0006  *
0007  * @date   2011-03-22
0008  * @brief  a MediaWiki C++ interface for KDE
0009  *
0010  * @author Copyright (C) 2010 by Alexandre Mendes
0011  *         <a href="mailto:alex dot mendes1988 at gmail dot com">alex dot mendes1988 at gmail dot com</a>
0012  *
0013  * This program is free software; you can redistribute it
0014  * and/or modify it under the terms of the GNU General
0015  * Public License as published by the Free Software Foundation;
0016  * either version 2, or (at your option)
0017  * any later version.
0018  *
0019  * This program is distributed in the hope that it will be useful,
0020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0022  * GNU General Public License for more details.
0023  *
0024  * ============================================================ */
0025 
0026 #ifndef TEST_LOGOUT_H
0027 #define TEST_LOGOUT_H
0028 
0029 #include <QObject>
0030 #include <QtTest>
0031 
0032 #include <KJob>
0033 
0034 #include "mediawiki.h"
0035 #include "logout.h"
0036 #include "libmediawikitest/fakeserver.h"
0037 
0038 using mediawiki::MediaWiki;
0039 using mediawiki::Logout;
0040 
0041 class LogoutTest : public QObject
0042 {
0043     Q_OBJECT
0044 
0045 public Q_SLOTS:
0046 
0047     void logoutHandle(KJob* job) {
0048         Q_UNUSED(job)
0049         logoutCount++;
0050     }
0051 
0052 private Q_SLOTS:
0053 
0054     void initTestCase()
0055     {
0056         logoutCount = 0;
0057         this->m_mediaWiki = new MediaWiki(QUrl(QStringLiteral("http://127.0.0.1:12566")));
0058         this->m_server = new FakeServer;
0059         this->request = QStringLiteral("/?format=xml&action=logout");
0060     }
0061 
0062     void logoutTestConnectTrue()
0063     {
0064         QString senario(QStringLiteral("<api />") );
0065         QString cookie( QStringLiteral("cookieprefix=\"enwiki\" sessionid=\"17ab96bd8ffbe8ca58a78657a918558e\" expires=\"Sat, 12-Feb-2011 21:39:30 GMT\""));
0066         m_server->setScenario(senario, cookie);
0067         m_server->startAndWait();
0068 
0069         logoutCount = 0;
0070         Logout logout(*m_mediaWiki);
0071         connect(&logout, SIGNAL(result(KJob*)),this, SLOT(logoutHandle(KJob*)));
0072         logout.exec();
0073         QCOMPARE(this->logoutCount, 1);
0074         QCOMPARE(logout.error(), (int)Logout::NoError);
0075 
0076         QList<FakeServer::Request> requests = m_server->getRequest();
0077         QCOMPARE(requests.size(), 1);
0078 
0079         FakeServer::Request request = requests[0];
0080         QCOMPARE(request.agent, m_mediaWiki->userAgent());
0081         QCOMPARE(request.type, QStringLiteral("GET"));
0082         QCOMPARE(request.value, QStringLiteral("/?format=xml&action=logout"));
0083     }
0084 
0085     void cleanupTestCase()
0086     {
0087         delete this->m_mediaWiki;
0088         delete this->m_server;
0089     }
0090 
0091 private:
0092 
0093     int         logoutCount;
0094     QString     request;
0095     MediaWiki*  m_mediaWiki;
0096     FakeServer* m_server;
0097 };
0098 
0099 QTEST_MAIN(LogoutTest)
0100 
0101 #include "logouttest.moc"
0102 
0103 #endif // TEST_LOGOUT_H
0104