| 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 <online-accounts-common/globals.h> |
| 22 | #include "signonui-request.h" |
| 23 | #include "mock/request-mock.h" |
| 24 | #include "mock/ui-server-mock.h" |
| 25 | |
| 26 | #include <Accounts/Account> |
| 27 | #include <Accounts/Manager> |
| 28 | |
| 29 | #include <LomiriOnlineAccountsPlugin/request-handler.h> |
| 30 | |
| 31 | #include <QDebug> |
| 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::Request |
| 42 | { |
| 43 | Q_OBJECT |
| 44 | |
| 45 | public: |
| 46 | TestRequest(const QString &clientProfile, |
| 47 | const QVariantMap ¶meters, |
| 48 | QObject *parent = 0): |
| 49 | Request(0, clientProfile, parameters, parent) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | void setWindow(QWindow *window) { Request::setWindow(window); } |
| 54 | |
| 55 | void sendResult(const QVariantMap &result) { setResult(result); } |
| 56 | }; |
| 57 | |
| 58 | class SignonuiRequestTest: public QObject |
| 59 | { |
| 60 | Q_OBJECT |
| 61 | |
| 62 | public: |
| 63 | SignonuiRequestTest(); |
| 64 | |
| 65 | private Q_SLOTS: |
| 66 | void initTestCase(); |
| 67 | void testParameters_data(); |
| 68 | void testParameters(); |
| 69 | void testHandler(); |
| 70 | |
| 71 | private: |
| 72 | bool mustCreateAccount(uint credentialsId) { return credentialsId > 10; } |
| 73 | |
| 74 | private: |
| 75 | QTemporaryDir m_accountsDir; |
| 76 | UiServer m_uiServer; |
| 77 | }; |
| 78 | |
| 79 | SignonuiRequestTest::SignonuiRequestTest(): |
| 80 | QObject(), |
| 81 | m_uiServer("fake") |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | void SignonuiRequestTest::initTestCase() |
| 86 | { |
| 87 | QVERIFY(m_accountsDir.isValid()); |
| 88 | |
| 89 | qputenv("ACCOUNTS", m_accountsDir.path().toUtf8()); |
| 90 | qputenv("AG_APPLICATIONS", TEST_DATA_DIR); |
| 91 | qputenv("AG_PROVIDERS", TEST_DATA_DIR); |
| 92 | qputenv("AG_SERVICES", TEST_DATA_DIR); |
| 93 | qputenv("XDG_DATA_HOME", TEST_DATA_DIR); |
| 94 | } |
| 95 | |
| 96 | void SignonuiRequestTest::testParameters_data() |
| 97 | { |
| 98 | QTest::addColumn<QVariantMap>("parameters"); |
| 99 | QTest::addColumn<uint>("identity"); |
| 100 | QTest::addColumn<QString>("method"); |
| 101 | QTest::addColumn<QString>("mechanism"); |
| 102 | QTest::addColumn<QString>("ssoId"); |
| 103 | QTest::addColumn<QVariantMap>("clientData"); |
| 104 | |
| 105 | QTest::newRow("empty") << |
| 106 | QVariantMap() << |
| 107 | uint(0) << |
| 108 | QString() << |
| 109 | QString() << |
| 110 | QString() << |
| 111 | QVariantMap(); |
| 112 | |
| 113 | QVariantMap clientData; |
| 114 | clientData.insert("some number", 4); |
| 115 | |
| 116 | QVariantMap parameters; |
| 117 | parameters.insert(SSOUI_KEY_CLIENT_DATA, clientData); |
| 118 | parameters.insert(SSOUI_KEY_IDENTITY, uint(45)); |
| 119 | parameters.insert(SSOUI_KEY_METHOD, QString("a method")); |
| 120 | parameters.insert(SSOUI_KEY_MECHANISM, QString("a mechanism")); |
| 121 | parameters.insert(SSOUI_KEY_REQUESTID, QString("/id-4")); |
| 122 | |
| 123 | |
| 124 | QTest::newRow("basic") << |
| 125 | parameters << |
| 126 | uint(45) << |
| 127 | "a method" << |
| 128 | "a mechanism" << |
| 129 | "/id-4" << |
| 130 | clientData; |
| 131 | } |
| 132 | |
| 133 | void SignonuiRequestTest::testParameters() |
| 134 | { |
| 135 | QFETCH(QVariantMap, parameters); |
| 136 | QFETCH(QString, method); |
| 137 | QFETCH(QString, mechanism); |
| 138 | QFETCH(QString, ssoId); |
| 139 | QFETCH(QVariantMap, clientData); |
| 140 | |
| 141 | TestRequest request("unconfined", parameters); |
| 142 | QCOMPARE(request.method(), method); |
| 143 | QCOMPARE(request.mechanism(), mechanism); |
| 144 | QCOMPARE(request.ssoId(), ssoId); |
| 145 | QCOMPARE(request.clientData(), clientData); |
| 146 | } |
| 147 | |
| 148 | void SignonuiRequestTest::testHandler() |
| 149 | { |
| 150 | QVariantMap parameters; |
| 151 | TestRequest request("unconfined", parameters); |
| 152 | QVERIFY(request.handler() == 0); |
| 153 | |
| 154 | SignOnUi::RequestHandler *handler = new SignOnUi::RequestHandler; |
| 155 | request.setHandler(handler); |
| 156 | QCOMPARE(request.handler(), handler); |
| 157 | |
| 158 | /* Try to set another handler; this won't be allowed */ |
| 159 | SignOnUi::RequestHandler *handler2 = new SignOnUi::RequestHandler; |
| 160 | request.setHandler(handler2); |
| 161 | QCOMPARE(request.handler(), handler); |
| 162 | |
| 163 | delete handler2; |
| 164 | delete handler; |
| 165 | } |
| 166 | |
| 167 | QTEST_MAIN(SignonuiRequestTest); |
| 168 | |
| 169 | #include "tst_signonui_request.moc" |