~lukas-kde/unity8/dashboard

« back to all changes in this revision

Viewing changes to tests/mocks/liblightdm/MockGreeter.cpp

  • Committer: Lukáš Tinkl
  • Date: 2017-01-26 12:13:17 UTC
  • mfrom: (2749.1.49 unity8)
  • Revision ID: lukas.tinkl@canonical.com-20170126121317-qms39s9pikclidbe
merge trunk, fix conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2014 Canonical, Ltd.
 
2
 * Copyright (C) 2014-2017 Canonical, Ltd.
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or modify
5
5
 * it under the terms of the GNU General Public License as published by
14
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
 */
16
16
 
17
 
#include "Greeter.h"
18
 
#include "GreeterPrivate.h"
19
 
#include <QtCore/QCoreApplication>
 
17
#include "MockController.h"
 
18
#include "MockGreeter.h"
 
19
#include <QDBusInterface>
 
20
#include <QDBusPendingCall>
20
21
#include <QTimer>
21
22
 
22
23
namespace QLightDM
23
24
{
24
25
 
 
26
class GreeterPrivate
 
27
{
 
28
public:
 
29
    bool authenticated = false;
 
30
    QString authenticationUser;
 
31
    bool twoFactorDone = false;
 
32
    QTimer authCallbackTimer;
 
33
};
 
34
 
25
35
Greeter::Greeter(QObject *parent)
26
 
  : QObject(parent),
27
 
    d_ptr(new GreeterPrivate(this))
 
36
  : QObject(parent)
 
37
  , d_ptr(new GreeterPrivate)
28
38
{
 
39
    Q_D(Greeter);
 
40
 
 
41
    d->authCallbackTimer.setSingleShot(true);
 
42
    connect(&d->authCallbackTimer, &QTimer::timeout,
 
43
            this, &QLightDM::Greeter::handleAuthenticate);
29
44
}
30
45
 
31
46
Greeter::~Greeter()
32
47
{
 
48
    delete d_ptr;
33
49
}
34
50
 
35
51
QString Greeter::authenticationUser() const
36
52
{
37
53
    Q_D(const Greeter);
 
54
 
38
55
    return d->authenticationUser;
39
56
}
40
57
 
41
58
bool Greeter::hasGuestAccountHint() const
42
59
{
43
 
    return true;
 
60
    return MockController::instance()->hasGuestAccountHint();
44
61
}
45
62
 
46
63
QString Greeter::getHint(const QString &name) const
56
73
 
57
74
bool Greeter::hideUsersHint() const
58
75
{
59
 
    return false;
 
76
    return MockController::instance()->hideUsersHint();
60
77
}
61
78
 
62
79
bool Greeter::showManualLoginHint() const
63
80
{
64
 
    return true;
 
81
    return MockController::instance()->showManualLoginHint();
65
82
}
66
83
 
67
84
bool Greeter::showRemoteLoginHint() const
68
85
{
69
 
    return true;
 
86
    return false;
70
87
}
71
88
 
72
89
QString Greeter::selectUserHint() const
73
90
{
74
 
    Q_D(const Greeter);
75
 
    return d->selectUserHint;
76
 
}
77
 
 
78
 
void Greeter::setSelectUserHint(const QString &selectUserHint)
79
 
{
80
 
    Q_D(Greeter);
81
 
    d->selectUserHint = selectUserHint;
 
91
    return MockController::instance()->selectUserHint();
82
92
}
83
93
 
84
94
bool Greeter::selectGuestHint() const
85
95
{
86
 
    return false;
 
96
    return MockController::instance()->selectGuestHint();
87
97
}
88
98
 
89
99
QString Greeter::autologinUserHint() const
114
124
bool Greeter::isAuthenticated() const
115
125
{
116
126
    Q_D(const Greeter);
 
127
 
117
128
    return d->authenticated;
118
129
}
119
130
 
129
140
    d->authenticated = false;
130
141
    d->authenticationUser = username;
131
142
    d->twoFactorDone = false;
132
 
    QTimer::singleShot(0, this, &Greeter::delayedAuthentication);
 
143
    d->authCallbackTimer.start();
133
144
}
134
145
 
135
 
void Greeter::delayedAuthentication()
 
146
void Greeter::handleAuthenticate()
136
147
{
137
148
    Q_D(Greeter);
138
 
    d->handleAuthenticate();
 
149
 
 
150
    if (d->authenticated) {
 
151
        // This can happen from the guest authentication flow
 
152
        Q_EMIT authenticationComplete();
 
153
        return;
 
154
    }
 
155
 
 
156
    if (d->authenticationUser.isEmpty()) {
 
157
        Q_EMIT showPrompt("Username: ", Greeter::PromptTypeQuestion);
 
158
        return;
 
159
    }
 
160
 
 
161
    // Send out any messages we need to
 
162
    if (d->authenticationUser == "info-prompt")
 
163
        Q_EMIT showMessage("Welcome to Unity Greeter", Greeter::MessageTypeInfo);
 
164
    else if (d->authenticationUser == "wide-info-prompt")
 
165
        Q_EMIT showMessage("Welcome to Unity Greeter, the greeteriest greeter that ever did appear in these fine lands", Greeter::MessageTypeInfo);
 
166
    else if (d->authenticationUser == "html-info-prompt")
 
167
        Q_EMIT showMessage("<b>&</b>", Greeter::MessageTypeInfo);
 
168
    else if (d->authenticationUser == "long-info-prompt")
 
169
        Q_EMIT showMessage("Welcome to Unity Greeter\n\nWe like to annoy you with super ridiculously long messages.\nLike this one\n\nThis is the last line of a multiple line message.", Greeter::MessageTypeInfo);
 
170
    else if (d->authenticationUser == "multi-info-prompt") {
 
171
        Q_EMIT showMessage("Welcome to Unity Greeter", Greeter::MessageTypeInfo);
 
172
        Q_EMIT showMessage("This is an error", Greeter::MessageTypeError);
 
173
        Q_EMIT showMessage("You should have seen three messages", Greeter::MessageTypeInfo);
 
174
    }
 
175
 
 
176
    // OK, now actually do the prompt
 
177
    if (d->authenticationUser == "no-password") {
 
178
        d->authenticated = true;
 
179
        Q_EMIT authenticationComplete();
 
180
    } else if (d->authenticationUser == "has-pin") {
 
181
        Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
 
182
    } else if (d->authenticationUser == "auth-error") {
 
183
        d->authenticated = false;
 
184
        Q_EMIT authenticationComplete();
 
185
    } else if (d->authenticationUser == "different-prompt") {
 
186
        Q_EMIT showPrompt("Secret word: ", Greeter::PromptTypeSecret);
 
187
    } else if (d->authenticationUser == "question-prompt") {
 
188
        Q_EMIT showPrompt("Favorite Color (blue): ", Greeter::PromptTypeQuestion);
 
189
    } else if (d->authenticationUser == "two-prompts") {
 
190
        Q_EMIT showPrompt("Favorite Color (blue):", Greeter::PromptTypeQuestion);
 
191
        Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
 
192
    } else if (d->authenticationUser == "wacky-prompts") {
 
193
        Q_EMIT showMessage("First message", Greeter::MessageTypeInfo);
 
194
        Q_EMIT showPrompt("Favorite Color (blue)", Greeter::PromptTypeQuestion);
 
195
        Q_EMIT showMessage("Second message", Greeter::MessageTypeError);
 
196
        Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
 
197
        Q_EMIT showMessage("Last message", Greeter::MessageTypeInfo);
 
198
    } else {
 
199
        Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
 
200
    }
139
201
}
140
202
 
141
203
void Greeter::authenticateAsGuest()
142
 
{}
 
204
{
 
205
    Q_D(Greeter);
 
206
 
 
207
    d->authenticated = true;
 
208
    d->authenticationUser = QString(); // this is what the real liblightdm does
 
209
    d->twoFactorDone = false;
 
210
    d->authCallbackTimer.start();
 
211
}
143
212
 
144
213
void Greeter::authenticateAutologin()
145
214
{}
151
220
}
152
221
 
153
222
void Greeter::cancelAuthentication()
154
 
{}
 
223
{
 
224
    Q_D(Greeter);
 
225
    d->authCallbackTimer.stop();
 
226
}
155
227
 
156
228
void Greeter::setLanguage (const QString &language)
157
229
{
161
233
bool Greeter::startSessionSync(const QString &session)
162
234
{
163
235
    Q_UNUSED(session)
 
236
 
 
237
    // Send a request to hide the greeter.  This is normally done by logind,
 
238
    // but when testing, we don't want the bother of mocking that out. Instead,
 
239
    // just send the request directly ourselves.
 
240
    QDBusInterface iface("com.canonical.UnityGreeter",
 
241
                         "/",
 
242
                         "com.canonical.UnityGreeter",
 
243
                         QDBusConnection::sessionBus());
 
244
    iface.asyncCall("HideGreeter");
 
245
 
164
246
    return true;
165
247
}
166
248
 
168
250
{
169
251
    Q_D(Greeter);
170
252
 
171
 
    d->handleRespond(response);
 
253
    if (d->authenticationUser.isEmpty()) {
 
254
        // A manual login, our first question was which username
 
255
        d->authenticationUser = response;
 
256
        handleAuthenticate();
 
257
        return;
 
258
    }
 
259
 
 
260
    if (d->authenticationUser == "no-response") {
 
261
        return;
 
262
    } else if (d->authenticationUser == "locked") {
 
263
        Q_EMIT showMessage("Account is locked", Greeter::MessageTypeError);
 
264
        sendAuthenticationComplete();
 
265
        return;
 
266
    } else if (d->authenticationUser == "two-factor") {
 
267
        if (!d->twoFactorDone) {
 
268
            if (response == "password") {
 
269
                d->twoFactorDone = true;
 
270
                Q_EMIT showPrompt("otp", Greeter::PromptTypeQuestion);
 
271
            } else {
 
272
                d->authenticated = false;
 
273
                sendAuthenticationComplete();
 
274
            }
 
275
        } else {
 
276
            d->authenticated = (response == "otp");
 
277
            sendAuthenticationComplete();
 
278
        }
 
279
        return;
 
280
    } else if (d->authenticationUser == "two-prompts" || d->authenticationUser == "wacky-prompts") {
 
281
        if (!d->twoFactorDone) {
 
282
            d->authenticated = (response == "blue");
 
283
            d->twoFactorDone = true;
 
284
        } else {
 
285
            d->authenticated = d->authenticated && (response == "password");
 
286
            sendAuthenticationComplete();
 
287
        }
 
288
        return;
 
289
    }
 
290
 
 
291
    if (d->authenticationUser == "has-pin") {
 
292
        d->authenticated = (response == "1234");
 
293
    } else if (d->authenticationUser == "question-prompt") {
 
294
        d->authenticated = (response == "blue");
 
295
    } else {
 
296
        d->authenticated = (response == "password");
 
297
    }
 
298
 
 
299
    if (d->authenticationUser == "info-after-login" && d->authenticated) {
 
300
        Q_EMIT showMessage("Congratulations on logging in!", Greeter::MessageTypeInfo);
 
301
    }
 
302
 
 
303
    sendAuthenticationComplete();
172
304
}
173
305
 
174
306
void Greeter::sendAuthenticationComplete()
181
313
    }
182
314
}
183
315
 
184
 
QString Greeter::mockMode() const
185
 
{
186
 
    Q_D(const Greeter);
187
 
    return d->mockMode;
188
 
}
189
 
 
190
 
 
191
 
void Greeter::setMockMode(QString mockMode)
192
 
{
193
 
    Q_D(Greeter);
194
 
 
195
 
    if (d->mockMode != mockMode) {
196
 
        d->mockMode = mockMode;
197
 
        Q_EMIT mockModeChanged(mockMode);
198
 
    }
199
 
}
200
 
 
201
316
}