~ubuntu-branches/ubuntu/utopic/ubuntu-system-settings-online-accounts/utopic-proposed

« back to all changes in this revision

Viewing changes to tests/online-accounts-ui/tst_browser_request.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Ubuntu daily release, Alberto Mardegan
  • Date: 2014-10-01 13:31:34 UTC
  • mfrom: (1.1.36)
  • Revision ID: package-import@ubuntu.com-20141001133134-3fgng1e52axk5d6t
Tags: 0.4+14.10.20141001-0ubuntu1
[ Ubuntu daily release ]
* New rebuild forced

[ Alberto Mardegan ]
* Minor improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Canonical Ltd.
 
3
 *
 
4
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 
5
 *
 
6
 * This file is part of 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 "debug.h"
 
23
#include "globals.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"
 
28
 
 
29
#include <QDebug>
 
30
#include <QNetworkCookie>
 
31
#include <QSignalSpy>
 
32
#include <QTemporaryDir>
 
33
#include <QTest>
 
34
 
 
35
#include <SignOn/uisessiondata.h>
 
36
#include <SignOn/uisessiondata_priv.h>
 
37
 
 
38
using namespace OnlineAccountsUi;
 
39
 
 
40
class TestRequest: public SignOnUi::BrowserRequest
 
41
{
 
42
    Q_OBJECT
 
43
 
 
44
public:
 
45
    TestRequest(const QVariantMap &parameters, QObject *parent = 0):
 
46
        SignOnUi::BrowserRequest(0, "profile", parameters, parent)
 
47
    {
 
48
    }
 
49
};
 
50
 
 
51
class BrowserRequestTest: public QObject
 
52
{
 
53
    Q_OBJECT
 
54
 
 
55
public:
 
56
    BrowserRequestTest();
 
57
 
 
58
private Q_SLOTS:
 
59
    void initTestCase();
 
60
    void testParametersWithHandler_data();
 
61
    void testParametersWithHandler();
 
62
    void testSuccessWithHandler();
 
63
    void testFailureWithHandler();
 
64
    void testCancelWithHandler();
 
65
 
 
66
private:
 
67
    QTemporaryDir m_dataDir;
 
68
    UiServer m_uiServer;
 
69
};
 
70
 
 
71
BrowserRequestTest::BrowserRequestTest():
 
72
    QObject(),
 
73
    m_uiServer("fake")
 
74
{
 
75
}
 
76
 
 
77
void BrowserRequestTest::initTestCase()
 
78
{
 
79
    QVERIFY(m_dataDir.isValid());
 
80
 
 
81
    qputenv("XDG_CACHE_HOME", m_dataDir.path().toUtf8());
 
82
}
 
83
 
 
84
void BrowserRequestTest::testParametersWithHandler_data()
 
85
{
 
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");
 
91
 
 
92
    QString baseCacheDir = QString("file://%1/tst_browser_request").
 
93
        arg(m_dataDir.path());
 
94
 
 
95
    QTest::newRow("empty") <<
 
96
        QVariantMap() <<
 
97
        "DefaultPage.qml" <<
 
98
        QString() <<
 
99
        QString() <<
 
100
        baseCacheDir + "/id-0";
 
101
 
 
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") <<
 
108
        parameters <<
 
109
        "DefaultPage.qml" <<
 
110
        "http://localhost/start.html" <<
 
111
        "http://localhost/end.html" <<
 
112
        baseCacheDir + "/id-4";
 
113
    parameters.clear();
 
114
 
 
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") <<
 
122
        parameters <<
 
123
        "file:///usr/share/signon-ui/MyPage.qml" <<
 
124
        "http://localhost/start.html" <<
 
125
        "http://localhost/end.html" <<
 
126
        baseCacheDir + "/id-4";
 
127
    parameters.clear();
 
128
}
 
129
 
 
130
void BrowserRequestTest::testParametersWithHandler()
 
131
{
 
132
    QFETCH(QVariantMap, parameters);
 
133
    QFETCH(QString, pageComponentUrl);
 
134
    QFETCH(QString, startUrl);
 
135
    QFETCH(QString, finalUrl);
 
136
    QFETCH(QString, rootDir);
 
137
 
 
138
    SignOnUi::RequestHandler handler;
 
139
    QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
 
140
 
 
141
    TestRequest request(parameters);
 
142
    request.setHandler(&handler);
 
143
 
 
144
    request.start();
 
145
 
 
146
    QCOMPARE(requestChanged.count(), 1);
 
147
    QObject *req = handler.request();
 
148
 
 
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);
 
154
}
 
155
 
 
156
void BrowserRequestTest::testSuccessWithHandler()
 
157
{
 
158
    SignOnUi::RequestHandler handler;
 
159
    QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
 
160
 
 
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()));
 
167
 
 
168
    OnlineAccountsUi::RequestPrivate *mockedRequest =
 
169
        OnlineAccountsUi::RequestPrivate::mocked(&request);
 
170
    QSignalSpy setResultCalled(mockedRequest,
 
171
                               SIGNAL(setResultCalled(const QVariantMap &)));
 
172
 
 
173
    request.setHandler(&handler);
 
174
    request.start();
 
175
 
 
176
    QCOMPARE(requestChanged.count(), 1);
 
177
    QObject *req = handler.request();
 
178
    QSignalSpy authenticated(req, SIGNAL(authenticated()));
 
179
 
 
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);
 
185
 
 
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);
 
192
 
 
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);
 
199
 
 
200
    /* Store some cookies */
 
201
    QVariantList cookieList;
 
202
    QList<QNetworkCookie> expectedCookies = QNetworkCookie::parseCookies(
 
203
        "a=1\nb=2");
 
204
    Q_FOREACH(const QNetworkCookie &c, expectedCookies) {
 
205
        cookieList.append(QVariant::fromValue(c));
 
206
    }
 
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);
 
212
 
 
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"));
 
217
}
 
218
 
 
219
void BrowserRequestTest::testFailureWithHandler()
 
220
{
 
221
    SignOnUi::RequestHandler handler;
 
222
    QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
 
223
 
 
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()));
 
230
 
 
231
    OnlineAccountsUi::RequestPrivate *mockedRequest =
 
232
        OnlineAccountsUi::RequestPrivate::mocked(&request);
 
233
    QSignalSpy setResultCalled(mockedRequest,
 
234
                               SIGNAL(setResultCalled(const QVariantMap &)));
 
235
 
 
236
    request.setHandler(&handler);
 
237
    request.start();
 
238
 
 
239
    QCOMPARE(requestChanged.count(), 1);
 
240
    QObject *req = handler.request();
 
241
    QSignalSpy authenticated(req, SIGNAL(authenticated()));
 
242
 
 
243
    /* Fail to load a page */
 
244
    QMetaObject::invokeMethod(req, "onLoadStarted");
 
245
    QMetaObject::invokeMethod(req, "onLoadFinished", Q_ARG(bool, false));
 
246
 
 
247
    QVERIFY(completed.wait());
 
248
    QCOMPARE(completed.count(), 1);
 
249
    QCOMPARE(authenticated.count(), 0);
 
250
 
 
251
    QCOMPARE(setResultCalled.count(), 1);
 
252
    QVariantMap results = setResultCalled.at(0).at(0).toMap();
 
253
    QVERIFY(results.isEmpty());
 
254
}
 
255
 
 
256
void BrowserRequestTest::testCancelWithHandler()
 
257
{
 
258
    SignOnUi::RequestHandler handler;
 
259
    QSignalSpy requestChanged(&handler, SIGNAL(requestChanged()));
 
260
 
 
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()));
 
267
 
 
268
    OnlineAccountsUi::RequestPrivate *mockedRequest =
 
269
        OnlineAccountsUi::RequestPrivate::mocked(&request);
 
270
    QSignalSpy setResultCalled(mockedRequest,
 
271
                               SIGNAL(setResultCalled(const QVariantMap &)));
 
272
 
 
273
    request.setHandler(&handler);
 
274
    request.start();
 
275
 
 
276
    QCOMPARE(requestChanged.count(), 1);
 
277
    requestChanged.clear();
 
278
    QObject *req = handler.request();
 
279
    QSignalSpy authenticated(req, SIGNAL(authenticated()));
 
280
 
 
281
    /* Start loading a page, then cancel */
 
282
    QMetaObject::invokeMethod(req, "onLoadStarted");
 
283
    QMetaObject::invokeMethod(req, "cancel", Qt::QueuedConnection);
 
284
 
 
285
    QVERIFY(requestChanged.wait());
 
286
    QCOMPARE(requestChanged.count(), 1);
 
287
    QVERIFY(!handler.request());
 
288
 
 
289
    QCOMPARE(completed.count(), 1);
 
290
    QCOMPARE(authenticated.count(), 0);
 
291
 
 
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));
 
296
}
 
297
 
 
298
QTEST_MAIN(BrowserRequestTest);
 
299
 
 
300
#include "tst_browser_request.moc"