2
* Copyright (C) 2014 Canonical Ltd.
4
* Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
6
* This file is part of online-accounts-ui
8
* This program is free software: you can redistribute it and/or modify it
9
* under the terms of the GNU General Public License version 3, as published
10
* by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranties of
14
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15
* PURPOSE. See the GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License along
18
* with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "browser-request.h"
24
#include "request-handler.h"
25
#include "mock/request-mock.h"
26
#include "mock/signonui-request-mock.h"
27
#include "mock/ui-server-mock.h"
30
#include <QNetworkCookie>
32
#include <QTemporaryDir>
35
#include <SignOn/uisessiondata.h>
36
#include <SignOn/uisessiondata_priv.h>
38
using namespace OnlineAccountsUi;
40
class TestRequest: public SignOnUi::BrowserRequest
45
TestRequest(const QVariantMap ¶meters, QObject *parent = 0):
46
SignOnUi::BrowserRequest(0, "profile", parameters, parent)
51
class BrowserRequestTest: public QObject
60
void testParametersWithHandler_data();
61
void testParametersWithHandler();
62
void testSuccessWithHandler();
63
void testFailureWithHandler();
64
void testCancelWithHandler();
67
QTemporaryDir m_dataDir;
71
BrowserRequestTest::BrowserRequestTest():
77
void BrowserRequestTest::initTestCase()
79
QVERIFY(m_dataDir.isValid());
81
qputenv("XDG_CACHE_HOME", m_dataDir.path().toUtf8());
84
void BrowserRequestTest::testParametersWithHandler_data()
86
QTest::addColumn<QVariantMap>("parameters");
87
QTest::addColumn<QString>("pageComponentUrl");
88
QTest::addColumn<QString>("startUrl");
89
QTest::addColumn<QString>("finalUrl");
90
QTest::addColumn<QString>("rootDir");
92
QString baseCacheDir = QString("file://%1/tst_browser_request").
93
arg(m_dataDir.path());
95
QTest::newRow("empty") <<
100
baseCacheDir + "/id-0";
102
QVariantMap parameters;
103
QVariantMap clientData;
104
parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
105
parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
106
parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
107
QTest::newRow("with URLs and ID") <<
110
"http://localhost/start.html" <<
111
"http://localhost/end.html" <<
112
baseCacheDir + "/id-4";
115
clientData.insert("X-PageComponent",
116
QUrl("file:///usr/share/signon-ui/MyPage.qml"));
117
parameters.insert(SSOUI_KEY_CLIENT_DATA, clientData);
118
parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
119
parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
120
parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
121
QTest::newRow("with page component") <<
123
"file:///usr/share/signon-ui/MyPage.qml" <<
124
"http://localhost/start.html" <<
125
"http://localhost/end.html" <<
126
baseCacheDir + "/id-4";
130
void BrowserRequestTest::testParametersWithHandler()
132
QFETCH(QVariantMap, parameters);
133
QFETCH(QString, pageComponentUrl);
134
QFETCH(QString, startUrl);
135
QFETCH(QString, finalUrl);
136
QFETCH(QString, rootDir);
138
SignOnUi::RequestHandler handler;
139
QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
141
TestRequest request(parameters);
142
request.setHandler(&handler);
146
QCOMPARE(requestChanged.count(), 1);
147
QObject *req = handler.request();
149
QCOMPARE(req->property("pageComponentUrl").toUrl().toString(), pageComponentUrl);
150
QCOMPARE(req->property("currentUrl").toUrl().toString(), QString());
151
QCOMPARE(req->property("startUrl").toUrl().toString(), startUrl);
152
QCOMPARE(req->property("finalUrl").toUrl().toString(), finalUrl);
153
QCOMPARE(req->property("rootDir").toString(), rootDir);
156
void BrowserRequestTest::testSuccessWithHandler()
158
SignOnUi::RequestHandler handler;
159
QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
161
QVariantMap parameters;
162
parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
163
parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
164
parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
165
TestRequest request(parameters);
166
QSignalSpy completed(&request, SIGNAL(completed()));
168
OnlineAccountsUi::RequestPrivate *mockedRequest =
169
OnlineAccountsUi::RequestPrivate::mocked(&request);
170
QSignalSpy setResultCalled(mockedRequest,
171
SIGNAL(setResultCalled(const QVariantMap &)));
173
request.setHandler(&handler);
176
QCOMPARE(requestChanged.count(), 1);
177
QObject *req = handler.request();
178
QSignalSpy authenticated(req, SIGNAL(authenticated()));
180
/* Go through a couple of pages */
181
QMetaObject::invokeMethod(req, "onLoadStarted");
182
QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, true));
183
req->setProperty("currentUrl", QUrl("http://localhost/somewhere"));
184
QCOMPARE(authenticated.count(), 0);
186
/* Pretend a page is failing, but then go to the destination URL before the
187
* fail timer is triggered */
188
QMetaObject::invokeMethod(req, "onLoadStarted");
189
QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, false));
190
req->setProperty("currentUrl", QUrl("http://localhost/somewhere-else"));
191
QCOMPARE(authenticated.count(), 0);
193
/* Finally, arrive at the destination URL */
194
QMetaObject::invokeMethod(req, "onLoadStarted");
195
QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, true));
196
req->setProperty("currentUrl", QUrl("http://localhost/end.html?code=ciao"));
197
QCOMPARE(authenticated.count(), 1);
198
QCOMPARE(completed.count(), 0);
200
/* Store some cookies */
201
QVariantList cookieList;
202
QList<QNetworkCookie> expectedCookies = QNetworkCookie::parseCookies(
204
Q_FOREACH(const QNetworkCookie &c, expectedCookies) {
205
cookieList.append(QVariant::fromValue(c));
207
QVariant cookies(cookieList);
208
QMetaObject::invokeMethod(req, "setCookies", Qt::QueuedConnection,
209
Q_ARG(QVariant, cookies));
210
QVERIFY(completed.wait());
211
QCOMPARE(completed.count(), 1);
213
QCOMPARE(setResultCalled.count(), 1);
214
QVariantMap results = setResultCalled.at(0).at(0).toMap();
215
QCOMPARE(results.value(SSOUI_KEY_URLRESPONSE).toString(),
216
QString("http://localhost/end.html?code=ciao"));
219
void BrowserRequestTest::testFailureWithHandler()
221
SignOnUi::RequestHandler handler;
222
QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
224
QVariantMap parameters;
225
parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
226
parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
227
parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
228
TestRequest request(parameters);
229
QSignalSpy completed(&request, SIGNAL(completed()));
231
OnlineAccountsUi::RequestPrivate *mockedRequest =
232
OnlineAccountsUi::RequestPrivate::mocked(&request);
233
QSignalSpy setResultCalled(mockedRequest,
234
SIGNAL(setResultCalled(const QVariantMap &)));
236
request.setHandler(&handler);
239
QCOMPARE(requestChanged.count(), 1);
240
QObject *req = handler.request();
241
QSignalSpy authenticated(req, SIGNAL(authenticated()));
243
/* Fail to load a page */
244
QMetaObject::invokeMethod(req, "onLoadStarted");
245
QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, false));
247
QVERIFY(completed.wait());
248
QCOMPARE(completed.count(), 1);
249
QCOMPARE(authenticated.count(), 0);
251
QCOMPARE(setResultCalled.count(), 1);
252
QVariantMap results = setResultCalled.at(0).at(0).toMap();
253
QVERIFY(results.isEmpty());
256
void BrowserRequestTest::testCancelWithHandler()
258
SignOnUi::RequestHandler handler;
259
QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
261
QVariantMap parameters;
262
parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
263
parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
264
parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
265
TestRequest request(parameters);
266
QSignalSpy completed(&request, SIGNAL(completed()));
268
OnlineAccountsUi::RequestPrivate *mockedRequest =
269
OnlineAccountsUi::RequestPrivate::mocked(&request);
270
QSignalSpy setResultCalled(mockedRequest,
271
SIGNAL(setResultCalled(const QVariantMap &)));
273
request.setHandler(&handler);
276
QCOMPARE(requestChanged.count(), 1);
277
requestChanged.clear();
278
QObject *req = handler.request();
279
QSignalSpy authenticated(req, SIGNAL(authenticated()));
281
/* Start loading a page, then cancel */
282
QMetaObject::invokeMethod(req, "onLoadStarted");
283
QMetaObject::invokeMethod(req, "cancel", Qt::QueuedConnection);
285
QVERIFY(requestChanged.wait());
286
QCOMPARE(requestChanged.count(), 1);
287
QVERIFY(!handler.request());
289
QCOMPARE(completed.count(), 1);
290
QCOMPARE(authenticated.count(), 0);
292
QCOMPARE(setResultCalled.count(), 1);
293
QVariantMap results = setResultCalled.at(0).at(0).toMap();
294
QCOMPARE(results.value(SSOUI_KEY_ERROR).toInt(),
295
int(SignOn::QUERY_ERROR_CANCELED));
298
QTEST_MAIN(BrowserRequestTest);
300
#include "tst_browser_request.moc"