150
163
queryAuthWithDialog(server, info, filledInfo, result);
153
// TODO test cancelling
154
// TODO test more concurrent requests
155
// TODO set info.verifyPath
166
void testRetryDialog()
168
KPasswdServer server(this);
169
server.setWalletDisabled(true);
171
// What the app would ask
173
info.url = KUrl("http://www.example.com");
175
// What the user would type
176
KIO::AuthInfo filledInfo(info);
177
filledInfo.username = "username";
178
filledInfo.password = "password";
180
KIO::AuthInfo result;
181
queryAuthWithDialog(server, info, filledInfo, result);
183
// Pretend that the returned credentials failed and initiate a retry,
184
// but cancel the retry dialog.
185
info.password.clear();
186
result = KIO::AuthInfo();
187
queryAuthWithDialog(server, info, filledInfo, result, QDialog::Rejected, QLatin1String("Invalid username or password"));
189
// Pretend that the returned credentials failed and initiate a retry,
190
// but this time continue the retry.
191
info.password.clear();
192
result = KIO::AuthInfo();
193
queryAuthWithDialog(server, info, filledInfo, result, QDialog::Accepted, QLatin1String("Invalid username or password"));
196
void testUsernameMistmatch()
198
KPasswdServer server(this);
199
server.setWalletDisabled(true);
201
// What the app would ask. Note the username in the URL.
203
info.url = KUrl("http://foo@www.example.com");
205
// What the user would type
206
KIO::AuthInfo filledInfo(info);
207
filledInfo.username = "bar";
208
filledInfo.password = "blah";
210
KIO::AuthInfo result;
211
queryAuthWithDialog(server, info, filledInfo, result);
213
// Check the returned url does not match the request url because of the
214
// username mismatch between the request URL and the filled in one.
215
QVERIFY(result.url != filledInfo.url);
217
// Verify there is NO cached auth data if the request URL contains the
218
// original user name (foo).
219
QVERIFY(noCheckAuth(server, info));
221
// Verify there is a cached auth data if the request URL contains the
222
// new user name (bar).
223
filledInfo.url = KUrl("http://bar@www.example.com");
224
QVERIFY(successCheckAuth(server, filledInfo, result));
226
// Now the URL check should be valid too.
227
QCOMPARE(result.url, filledInfo.url);
230
void testCancelPasswordDialog()
232
KPasswdServer server(this);
233
server.setWalletDisabled(true);
235
// What the app would ask.
237
info.url = KUrl("http://www.example.com");
238
info.username = info.url.user();
240
KIO::AuthInfo result;
241
queryAuthWithDialog(server, info, KIO::AuthInfo(), result, QDialog::Rejected);
244
void testVerifyPath()
246
KPasswdServer server(this);
247
server.setWalletDisabled(true);
249
// Add auth to the cache
250
const qlonglong windowId = 42;
251
KIO::AuthInfo authInfo;
252
authInfo.url = KUrl("http://www.example.com/test/test.html");
253
authInfo.username = "toto";
254
authInfo.password = "foobar";
255
server.addAuthInfo(authInfo, windowId);
257
KIO::AuthInfo queryAuthInfo;
258
queryAuthInfo.url = KUrl("http://www.example.com/test/test2/test.html");
259
queryAuthInfo.verifyPath = true;
261
KIO::AuthInfo expectedAuthInfo;
262
expectedAuthInfo.username = "toto";
263
expectedAuthInfo.password = "foobar";
265
QVERIFY(successCheckAuth(server, queryAuthInfo, expectedAuthInfo));
268
void testConcurrentQueryAuth()
270
KPasswdServer server(this);
271
server.setWalletDisabled(true);
273
QList<KIO::AuthInfo> authInfos;
274
for (int i=0; i < 10; ++i) {
276
info.url = KUrl("http://www.example.com/test" + QString::number(i) + ".html");
280
// What the user would type
281
KIO::AuthInfo filledInfo;
282
filledInfo.username = "bar";
283
filledInfo.password = "blah";
285
QList<KIO::AuthInfo> results;
286
concurrentQueryAuthWithDialog(server, authInfos, filledInfo, results);
289
void testConcurrentCheckAuth()
291
KPasswdServer server(this);
292
server.setWalletDisabled(true);
294
QList<KIO::AuthInfo> authInfos;
295
for (int i=0; i < 10; ++i) {
297
info.url = KUrl("http://www.example.com/test" + QString::number(i) + ".html");
301
// What the user would type
302
KIO::AuthInfo filledInfo;
303
filledInfo.username = "bar";
304
filledInfo.password = "blah";
306
QList<KIO::AuthInfo> results;
307
concurrentQueryAuthWithDialog(server, authInfos, filledInfo, results);
158
311
// Checks that no auth is available for @p info
209
362
result = spy[0][2].value<KIO::AuthInfo>();
212
void queryAuthWithDialog(KPasswdServer& server, const KIO::AuthInfo& info, const KIO::AuthInfo& filledInfo, KIO::AuthInfo& result)
365
void queryAuthWithDialog(KPasswdServer& server, const KIO::AuthInfo& info,
366
const KIO::AuthInfo& filledInfo, KIO::AuthInfo& result,
367
int code = QDialog::Accepted, const QString& errMsg = QString())
214
369
QSignalSpy spy(&server, sigQueryAuthInfoResult);
215
370
const qlonglong windowId = 42;
216
371
const qlonglong seqNr = 2;
217
372
const qlonglong id = server.queryAuthInfoAsync(
219
QString("KPasswdServerTest"),
220
375
windowId, seqNr, 16 /*usertime*/);
221
QVERIFY(id > 0); // requestId, ever increasing
376
QVERIFY(id >= 0); // requestId, ever increasing
222
377
QVERIFY(spy.isEmpty());
223
QMetaObject::invokeMethod(this, "checkAndFillDialog", Qt::QueuedConnection, Q_ARG(KIO::AuthInfo, info), Q_ARG(KIO::AuthInfo, filledInfo));
379
const bool hasErrorMessage = (!errMsg.isEmpty());
380
const bool isCancelRetryDialogTest = (hasErrorMessage && code == QDialog::Rejected);
382
if (hasErrorMessage) {
383
QMetaObject::invokeMethod(this, "checkRetryDialog",
384
Qt::QueuedConnection, Q_ARG(int, code));
387
if (!isCancelRetryDialogTest) {
388
QMetaObject::invokeMethod(this, "checkAndFillDialog", Qt::QueuedConnection,
389
Q_ARG(KIO::AuthInfo, info),
390
Q_ARG(KIO::AuthInfo, filledInfo),
224
393
// Force KPasswdServer to process the request now, otherwise the checkAndFillDialog needs a timer too...
225
394
server.processRequest();
396
QVERIFY(QTest::kWaitForSignal(&server, sigQueryAuthInfoResult, 1000));
226
397
QCOMPARE(spy.count(), 1);
227
398
QCOMPARE(spy[0][0].toLongLong(), id);
228
399
//QCOMPARE(spy[0][1].toLongLong(), 3LL); // seqNr
229
400
result = spy[0][2].value<KIO::AuthInfo>();
230
QCOMPARE(result.username, filledInfo.username);
231
QCOMPARE(result.password, filledInfo.password);
232
QCOMPARE(result.isModified(), true);
401
QCOMPARE(result.username, (isCancelRetryDialogTest ? QString() : filledInfo.username));
402
QCOMPARE(result.password, (isCancelRetryDialogTest ? QString() : filledInfo.password));
403
QCOMPARE(result.isModified(), (code == QDialog::Accepted ? true : false));
406
void concurrentQueryAuthWithDialog(KPasswdServer& server, const QList<KIO::AuthInfo>& infos,
407
const KIO::AuthInfo& filledInfo, QList<KIO::AuthInfo>& results,
408
int code = QDialog::Accepted)
410
QSignalSpy spy(&server, sigQueryAuthInfoResult);
411
const qlonglong windowId = 42;
413
QList<qlonglong> idList;
415
Q_FOREACH(const KIO::AuthInfo& info, infos) {
416
const qlonglong id = server.queryAuthInfoAsync(
419
windowId, seqNr, 16 /*usertime*/);
420
QVERIFY(id >= 0); // requestId, ever increasing
424
QVERIFY(spy.isEmpty());
425
QMetaObject::invokeMethod(this, "checkAndFillDialog", Qt::QueuedConnection,
426
Q_ARG(KIO::AuthInfo, infos.first()),
427
Q_ARG(KIO::AuthInfo,filledInfo),
430
// Force KPasswdServer to process the request now, otherwise the checkAndFillDialog needs a timer too...
431
server.processRequest();
432
while (spy.count() < infos.count())
433
QVERIFY(QTest::kWaitForSignal(&server, sigQueryAuthInfoResult, 1000));
435
QCOMPARE(spy.count(), infos.count());
437
for(int i = 0, count = spy.count(); i < count; ++i) {
438
QCOMPARE(spy[i][0].toLongLong(), idList.at(i));
439
//QCOMPARE(spy[0][1].toLongLong(), 3LL); // seqNr
440
KIO::AuthInfo result = spy[i][2].value<KIO::AuthInfo>();
441
QCOMPARE(result.username, filledInfo.username);
442
QCOMPARE(result.password, filledInfo.password);
443
QCOMPARE(result.isModified(), (code == QDialog::Accepted ? true : false));
448
void concurrentCheckAuthWithDialog(KPasswdServer& server, const QList<KIO::AuthInfo>& infos,
449
const KIO::AuthInfo& filledInfo, QList<KIO::AuthInfo>& results,
450
int code = QDialog::Accepted)
452
QSignalSpy spy(&server, sigQueryAuthInfoResult);
453
const qlonglong windowId = 42;
455
QList<qlonglong> idList;
457
QListIterator<KIO::AuthInfo> it (infos);
459
const qlonglong id = server.queryAuthInfoAsync(
462
windowId, seqNr, 16 /*usertime*/);
463
QVERIFY(id >= 0); // requestId, ever increasing
467
while (it.hasNext()) {
468
const qlonglong id = server.checkAuthInfoAsync(it.next(), windowId,16 /*usertime*/);
469
QVERIFY(id >= 0); // requestId, ever increasing
473
QVERIFY(spy.isEmpty());
474
QMetaObject::invokeMethod(this, "checkAndFillDialog", Qt::QueuedConnection,
475
Q_ARG(KIO::AuthInfo, infos.first()),
476
Q_ARG(KIO::AuthInfo,filledInfo),
479
// Force KPasswdServer to process the request now, otherwise the checkAndFillDialog needs a timer too...
480
server.processRequest();
482
QVERIFY(QTest::kWaitForSignal(&server, sigQueryAuthInfoResult, 1000));
484
while ((spy.count()-1) < infos.count()) {
485
QVERIFY(QTest::kWaitForSignal(&server, sigCheckAuthInfoResult, 1000));
488
for(int i = 0, count = spy.count(); i < count; ++i) {
489
QCOMPARE(spy[i][0].toLongLong(), idList.at(i));
490
//QCOMPARE(spy[0][1].toLongLong(), 3LL); // seqNr
491
KIO::AuthInfo result = spy[i][2].value<KIO::AuthInfo>();
492
QCOMPARE(result.username, filledInfo.username);
493
QCOMPARE(result.password, filledInfo.password);
494
QCOMPARE(result.isModified(), (code == QDialog::Accepted ? true : false));
235
499
protected Q_SLOTS:
236
void checkAndFillDialog(const KIO::AuthInfo& info, const KIO::AuthInfo& filledInfo)
500
void checkAndFillDialog(const KIO::AuthInfo& info, const KIO::AuthInfo& filledInfo, int code = QDialog::Accepted)
238
502
Q_FOREACH(QWidget *widget, QApplication::topLevelWidgets()) {
240
503
if (KPasswordDialog* dialog = qobject_cast<KPasswordDialog *>(widget)) {
241
QCOMPARE(dialog->username(), info.username);
242
QCOMPARE(dialog->password(), info.password);
243
dialog->setUsername(filledInfo.username);
244
dialog->setPassword(filledInfo.password);
245
dialog->done(QDialog::Accepted);
504
if (code == QDialog::Accepted) {
505
QCOMPARE(dialog->username(), getUserNameFrom(info));
506
QCOMPARE(dialog->password(), info.password);
507
dialog->setUsername(filledInfo.username);
508
dialog->setPassword(filledInfo.password);
249
514
kWarning() << "No KPasswordDialog found!";
517
void checkRetryDialog(int code = QDialog::Accepted)
519
Q_FOREACH(QWidget *widget, QApplication::topLevelWidgets()) {
520
KDialog* dialog = qobject_cast<KDialog*>(widget);
521
if (dialog && !dialog->inherits("KPasswordDialog")) {
253
529
QTEST_KDEMAIN( KPasswdServerTest, GUI )