Debian Codegraph @

tests/online-accounts-ui/tst_browser_request/tst_browser_request.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This file is part of lomiri-online-accounts-ui
7 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 #include "browser-request.h"
22 #include <online-accounts-common/debug.h>
23 #include <online-accounts-common/globals.h>
24 #include "mock/request-mock.h"
25 #include "mock/signonui-request-mock.h"
26 #include "mock/ui-server-mock.h"
27
28 #include <LomiriOnlineAccountsPlugin/request-handler.h>
29
30 #include <QDebug>
31 #include <QNetworkCookie>
32 #include <QSignalSpy>
33 #include <QTemporaryDir>
34 #include <QTest>
35
36 #include <SignOn/uisessiondata.h>
37 #include <SignOn/uisessiondata_priv.h>
38
39 using namespace OnlineAccountsUi;
40
41 class TestRequest: public SignOnUi::BrowserRequest
42 {
43 Q_OBJECT
44
45 public:
46 TestRequest(const QVariantMap &parameters, QObject *parent = 0):
47 SignOnUi::BrowserRequest(0, "profile", parameters, parent)
48 {
49 }
50 };
51
52 class BrowserRequestTest: public QObject
53 {
54 Q_OBJECT
55
56 public:
57 BrowserRequestTest();
58
59 private Q_SLOTS:
60 void initTestCase();
61 void testParametersWithHandler_data();
62 void testParametersWithHandler();
63 void testSuccessWithHandler();
64 void testFailureWithHandler();
65 void testCancelWithHandler();
66
67 private:
68 QTemporaryDir m_dataDir;
69 UiServer m_uiServer;
70 };
71
72 BrowserRequestTest::BrowserRequestTest():
73 QObject(),
74 m_uiServer("fake")
75 {
76 }
77
78 void BrowserRequestTest::initTestCase()
79 {
80 QVERIFY(m_dataDir.isValid());
81
82 qputenv("XDG_CACHE_HOME", m_dataDir.path().toUtf8());
83 }
84
85 void BrowserRequestTest::testParametersWithHandler_data()
86 {
87 QTest::addColumn<QVariantMap>("parameters");
88 QTest::addColumn<QString>("providerId");
89 QTest::addColumn<QString>("pageComponentUrl");
90 QTest::addColumn<QString>("startUrl");
91 QTest::addColumn<QString>("finalUrl");
92 QTest::addColumn<QString>("rootDir");
93
94 QString baseCacheDir = QString("%1/tst_browser_request").
95 arg(m_dataDir.path());
96
97 QTest::newRow("empty") <<
98 QVariantMap() <<
99 QString() <<
100 "DefaultPage.qml" <<
101 QString() <<
102 QString() <<
103 baseCacheDir + "/id-0-";
104
105 QVariantMap parameters;
106 QVariantMap clientData;
107 parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
108 parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
109 parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
110 QTest::newRow("with URLs and ID") <<
111 parameters <<
112 "google" <<
113 "DefaultPage.qml" <<
114 "http://localhost/start.html" <<
115 "http://localhost/end.html" <<
116 baseCacheDir + "/id-4-google";
117 parameters.clear();
118
119 clientData.insert("X-PageComponent",
120 QUrl("file:///usr/share/signon-ui/MyPage.qml"));
121 parameters.insert(SSOUI_KEY_CLIENT_DATA, clientData);
122 parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
123 parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
124 parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
125 QTest::newRow("with page component") <<
126 parameters <<
127 "com.lomiri.app_plugin" <<
128 "file:///usr/share/signon-ui/MyPage.qml" <<
129 "http://localhost/start.html" <<
130 "http://localhost/end.html" <<
131 baseCacheDir + "/id-4-com.lomiri.app_plugin";
132 parameters.clear();
133 }
134
135 void BrowserRequestTest::testParametersWithHandler()
136 {
137 QFETCH(QVariantMap, parameters);
138 QFETCH(QString, providerId);
139 QFETCH(QString, pageComponentUrl);
140 QFETCH(QString, startUrl);
141 QFETCH(QString, finalUrl);
142 QFETCH(QString, rootDir);
143
144 SignOnUi::RequestHandler handler;
145 QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
146
147 TestRequest request(parameters);
148 SignOnUi::RequestPrivate *mockedRequest =
149 SignOnUi::RequestPrivate::mocked(&request);
150 mockedRequest->setProviderId(providerId);
151 QString title = providerId.toUpper();
152 mockedRequest->setWindowTitle(title);
153 request.setHandler(&handler);
154
155 request.start();
156
157 QCOMPARE(requestChanged.count(), 1);
158 QObject *req = handler.request();
159
160 QCOMPARE(req->property("title").toString(), title);
161 QCOMPARE(req->property("pageComponentUrl").toUrl().toString(), pageComponentUrl);
162 QCOMPARE(req->property("currentUrl").toUrl().toString(), QString());
163 QCOMPARE(req->property("startUrl").toUrl().toString(), startUrl);
164 QCOMPARE(req->property("finalUrl").toUrl().toString(), finalUrl);
165 QCOMPARE(req->property("rootDir").toString(), rootDir);
166 }
167
168 void BrowserRequestTest::testSuccessWithHandler()
169 {
170 SignOnUi::RequestHandler handler;
171 QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
172
173 QVariantMap parameters;
174 parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
175 parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
176 parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
177 TestRequest request(parameters);
178 QSignalSpy completed(&request, SIGNAL(completed()));
179
180 OnlineAccountsUi::RequestPrivate *mockedRequest =
181 OnlineAccountsUi::RequestPrivate::mocked(&request);
182 QSignalSpy setResultCalled(mockedRequest,
183 SIGNAL(setResultCalled(const QVariantMap &)));
184
185 request.setHandler(&handler);
186 request.start();
187
188 QCOMPARE(requestChanged.count(), 1);
189 QObject *req = handler.request();
190 QSignalSpy authenticated(req, SIGNAL(authenticated()));
191
192 /* Go through a couple of pages */
193 QMetaObject::invokeMethod(req, "onLoadStarted");
194 QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, true));
195 req->setProperty("currentUrl", QUrl("http://localhost/somewhere"));
196 QCOMPARE(authenticated.count(), 0);
197
198 /* Pretend a page is failing, but then go to the destination URL before the
199 * fail timer is triggered */
200 QMetaObject::invokeMethod(req, "onLoadStarted");
201 QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, false));
202 req->setProperty("currentUrl", QUrl("http://localhost/somewhere-else"));
203 QCOMPARE(authenticated.count(), 0);
204
205 /* Finally, arrive at the destination URL */
206 QMetaObject::invokeMethod(req, "onLoadStarted");
207 QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, true));
208 req->setProperty("currentUrl", QUrl("http://localhost/end.html?code=ciao"));
209 QCOMPARE(authenticated.count(), 1);
210 QCOMPARE(completed.count(), 0);
211
212 /* Store some cookies */
213 QVariantList cookieList;
214 QList<QNetworkCookie> expectedCookies = QNetworkCookie::parseCookies(
215 "a=1\nb=2");
216 Q_FOREACH(const QNetworkCookie &c, expectedCookies) {
217 cookieList.append(QVariant::fromValue(c));
218 }
219 QVariant cookies(cookieList);
220 QMetaObject::invokeMethod(req, "setCookies", Qt::QueuedConnection,
221 Q_ARG(QVariant, cookies));
222 QVERIFY(completed.wait());
223 QCOMPARE(completed.count(), 1);
224
225 QCOMPARE(setResultCalled.count(), 1);
226 QVariantMap results = setResultCalled.at(0).at(0).toMap();
227 QCOMPARE(results.value(SSOUI_KEY_URLRESPONSE).toString(),
228 QString("http://localhost/end.html?code=ciao"));
229 }
230
231 void BrowserRequestTest::testFailureWithHandler()
232 {
233 SignOnUi::RequestHandler handler;
234 QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
235
236 QVariantMap parameters;
237 parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
238 parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
239 parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
240 TestRequest request(parameters);
241 QSignalSpy completed(&request, SIGNAL(completed()));
242
243 OnlineAccountsUi::RequestPrivate *mockedRequest =
244 OnlineAccountsUi::RequestPrivate::mocked(&request);
245 QSignalSpy setResultCalled(mockedRequest,
246 SIGNAL(setResultCalled(const QVariantMap &)));
247
248 request.setHandler(&handler);
249 request.start();
250
251 QCOMPARE(requestChanged.count(), 1);
252 QObject *req = handler.request();
253 QSignalSpy authenticated(req, SIGNAL(authenticated()));
254
255 /* Fail to load a page */
256 QMetaObject::invokeMethod(req, "onLoadStarted");
257 QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, false));
258
259 QVERIFY(completed.wait());
260 QCOMPARE(completed.count(), 1);
261 QCOMPARE(authenticated.count(), 0);
262
263 QCOMPARE(setResultCalled.count(), 1);
264 QVariantMap results = setResultCalled.at(0).at(0).toMap();
265 QVERIFY(results.contains(SSOUI_KEY_ERROR));
266 }
267
268 void BrowserRequestTest::testCancelWithHandler()
269 {
270 SignOnUi::RequestHandler handler;
271 QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
272
273 QVariantMap parameters;
274 parameters.insert(SSOUI_KEY_OPENURL, "http://localhost/start.html");
275 parameters.insert(SSOUI_KEY_FINALURL, "http://localhost/end.html");
276 parameters.insert(SSOUI_KEY_IDENTITY, uint(4));
277 TestRequest request(parameters);
278 QSignalSpy completed(&request, SIGNAL(completed()));
279
280 OnlineAccountsUi::RequestPrivate *mockedRequest =
281 OnlineAccountsUi::RequestPrivate::mocked(&request);
282 QSignalSpy setResultCalled(mockedRequest,
283 SIGNAL(setResultCalled(const QVariantMap &)));
284
285 request.setHandler(&handler);
286 request.start();
287
288 QCOMPARE(requestChanged.count(), 1);
289 requestChanged.clear();
290 QObject *req = handler.request();
291 QSignalSpy authenticated(req, SIGNAL(authenticated()));
292
293 /* Start loading a page, then cancel */
294 QMetaObject::invokeMethod(req, "onLoadStarted");
295 QMetaObject::invokeMethod(req, "cancel", Qt::QueuedConnection);
296
297 QVERIFY(requestChanged.wait());
298 QCOMPARE(requestChanged.count(), 1);
299 QVERIFY(!handler.request());
300
301 QCOMPARE(completed.count(), 1);
302 QCOMPARE(authenticated.count(), 0);
303
304 QCOMPARE(setResultCalled.count(), 1);
305 QVariantMap results = setResultCalled.at(0).at(0).toMap();
306 QCOMPARE(results.value(SSOUI_KEY_ERROR).toInt(),
307 int(SignOn::QUERY_ERROR_CANCELED));
308 }
309
310 QTEST_MAIN(BrowserRequestTest);
311
312 #include "tst_browser_request.moc"