diff options
author | Dario Freddi <dario.freddi@collabora.com> | 2012-02-22 18:49:15 +0100 |
---|---|---|
committer | Dario Freddi <dario.freddi@collabora.com> | 2012-03-13 14:32:22 +0100 |
commit | 80b718632470a88c62d200ac5c1d3e472e9ea1eb (patch) | |
tree | bb65ab8d47e2fb5902d80841756eb1e3aac16f1c /tests | |
parent | 30d8ff444735fee65173f9ef2a635507a124eaeb (diff) |
captcha-authentication: Add a basic test case
Diffstat (limited to 'tests')
-rw-r--r-- | tests/dbus/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/dbus/captcha-authentication.cpp | 193 |
2 files changed, 194 insertions, 0 deletions
diff --git a/tests/dbus/CMakeLists.txt b/tests/dbus/CMakeLists.txt index b13d15d3..6236955e 100644 --- a/tests/dbus/CMakeLists.txt +++ b/tests/dbus/CMakeLists.txt @@ -37,6 +37,7 @@ if(ENABLE_TP_GLIB_TESTS) endif(HAVE_TEST_PYTHON) tpqt_add_dbus_unit_test(AccountConnectionFactory account-connection-factory tp-glib-tests tp-qt-tests-glib-helpers) + tpqt_add_dbus_unit_test(CaptchaAuthentication captcha-authentication tp-glib-tests tp-qt-tests-glib-helpers) tpqt_add_dbus_unit_test(ChannelBasics chan-basics tp-glib-tests tp-qt-tests-glib-helpers) tpqt_add_dbus_unit_test(ChannelConference chan-conference tp-glib-tests future-example-cm-conference tp-qt-tests-glib-helpers) tpqt_add_dbus_unit_test(ConnectionManagerBasics cm-basics tp-glib-tests) diff --git a/tests/dbus/captcha-authentication.cpp b/tests/dbus/captcha-authentication.cpp new file mode 100644 index 00000000..ab16b481 --- /dev/null +++ b/tests/dbus/captcha-authentication.cpp @@ -0,0 +1,193 @@ +#include <tests/lib/test.h> + +#include <tests/lib/glib-helpers/test-conn-helper.h> + +#include <tests/lib/glib/simple-conn.h> +#include <tests/lib/glib/captcha-chan.h> + +#include <TelepathyQt/Captcha> +#include <TelepathyQt/CaptchaAuthentication> +#include <TelepathyQt/Connection> +#include <TelepathyQt/PendingReady> +#include <TelepathyQt/PendingCaptchas> +#include <TelepathyQt/ReferencedHandles> + +#include <telepathy-glib/telepathy-glib.h> + +#include <stdio.h> + +using namespace Tp; + +class TestCaptchaAuthentication : public Test +{ + Q_OBJECT + +public: + TestCaptchaAuthentication(QObject *parent = 0) + : Test(parent), + mConn(0), mChanService(0) + { } + +protected Q_SLOTS: + void onStatusChanged(Tp::CaptchaStatus status); + +private Q_SLOTS: + void initTestCase(); + void init(); + + void testCreation(); + void testCaptchaSuccessful(); + + void cleanup(); + void cleanupTestCase(); + +private: + void testCheckRemoteConnectionsCommon(); + + void createCaptchaChannel(); + + TestConnHelper *mConn; + TpTestsCaptchaChannel *mChanService; + ChannelPtr mChan; +}; + +void TestCaptchaAuthentication::onStatusChanged(Tp::CaptchaStatus status) +{ + mLoop->exit(0); +} + +void TestCaptchaAuthentication::createCaptchaChannel() +{ + mChan.reset(); + mLoop->processEvents(); + tp_clear_object(&mChanService); + + /* Create service-side tube channel object */ + QString chanPath = QString(QLatin1String("%1/Channel")).arg(mConn->objectPath()); + + mChanService = TP_TESTS_CAPTCHA_CHANNEL(g_object_new( + TP_TESTS_TYPE_CAPTCHA_CHANNEL, + "connection", mConn->service(), + "requested", FALSE, + "object-path", chanPath.toLatin1().constData(), + NULL)); + + /* Create client-side tube channel object */ + mChan = Channel::create(mConn->client(), chanPath, QVariantMap()); +} + +void TestCaptchaAuthentication::initTestCase() +{ + initTestCaseImpl(); + + g_type_init(); + g_set_prgname("captcha-authentication"); + tp_debug_set_flags("all"); + dbus_g_bus_get(DBUS_BUS_STARTER, 0); + + mConn = new TestConnHelper(this, + TP_TESTS_TYPE_SIMPLE_CONNECTION, + "account", "me@example.com", + "protocol", "example", + NULL); + QCOMPARE(mConn->connect(), true); +} + +void TestCaptchaAuthentication::init() +{ + initImpl(); +} + +void TestCaptchaAuthentication::testCreation() +{ + createCaptchaChannel(); + QVERIFY(connect(mChan->becomeReady(Channel::FeatureCaptcha), + SIGNAL(finished(Tp::PendingOperation *)), + SLOT(expectSuccessfulCall(Tp::PendingOperation *)))); + QCOMPARE(mLoop->exec(), 0); + + QCOMPARE(mChan->isReady(Channel::FeatureCaptcha), true); + + CaptchaAuthenticationPtr captcha = mChan->captchaAuthentication(); + + QCOMPARE(captcha.isNull(), false); + QCOMPARE(captcha->status(), Tp::CaptchaStatusLocalPending); + QCOMPARE(captcha->canRetry(), false); + QVERIFY(captcha->lastError().isEmpty()); + QVERIFY(captcha->lastErrorDetails().allDetails().isEmpty()); +} + +void TestCaptchaAuthentication::testCaptchaSuccessful() +{ + createCaptchaChannel(); + QVERIFY(connect(mChan->becomeReady(Channel::FeatureCaptcha), + SIGNAL(finished(Tp::PendingOperation *)), + SLOT(expectSuccessfulCall(Tp::PendingOperation *)))); + QCOMPARE(mLoop->exec(), 0); + + QCOMPARE(mChan->isReady(Channel::FeatureCaptcha), true); + + CaptchaAuthenticationPtr captcha = mChan->captchaAuthentication(); + + QCOMPARE(captcha.isNull(), false); + + PendingCaptchas *pendingCaptchas = captcha->requestCaptchas(QStringList() << QLatin1String("image/png")); + QVERIFY(connect(pendingCaptchas, + SIGNAL(finished(Tp::PendingOperation *)), + SLOT(expectSuccessfulCall(Tp::PendingOperation *)))); + QCOMPARE(mLoop->exec(), 0); + + QCOMPARE(pendingCaptchas->requiresMultipleCaptchas(), false); + QCOMPARE(pendingCaptchas->captchaList().size(), 1); + + Captcha captchaData = pendingCaptchas->captcha(); + + QCOMPARE(captchaData.mimeType(), QLatin1String("image/png")); + QCOMPARE(captchaData.label(), QLatin1String("Enter the text displayed")); + QCOMPARE(captchaData.data(), QByteArray("This is a fake payload")); + QCOMPARE(captchaData.type(), CaptchaAuthentication::OCRChallenge); + QCOMPARE((int)captchaData.id(), 42); + + QVERIFY(connect(captcha->answer(42, QLatin1String("This is the right answer")), + SIGNAL(finished(Tp::PendingOperation *)), + SLOT(expectSuccessfulCall(Tp::PendingOperation *)))); + QCOMPARE(mLoop->exec(), 0); + + QCOMPARE(captcha->status(), CaptchaStatusSucceeded); +} + +void TestCaptchaAuthentication::cleanup() +{ + cleanupImpl(); + + if (mChan && mChan->isValid()) { + qDebug() << "waiting for the channel to become invalidated"; + + QVERIFY(connect(mChan.data(), + SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)), + mLoop, + SLOT(quit()))); + tp_base_channel_close(TP_BASE_CHANNEL(mChanService)); + QCOMPARE(mLoop->exec(), 0); + } + + mChan.reset(); + + if (mChanService != 0) { + g_object_unref(mChanService); + mChanService = 0; + } + + mLoop->processEvents(); +} + +void TestCaptchaAuthentication::cleanupTestCase() +{ + QCOMPARE(mConn->disconnect(), true); + delete mConn; + + cleanupTestCaseImpl(); +} + +QTEST_MAIN(TestCaptchaAuthentication) +#include "_gen/captcha-authentication.cpp.moc.hpp" |