~lukas-kde/unity8/dashboard

« back to all changes in this revision

Viewing changes to tests/plugins/LightDM/IntegratedLightDM/promptsmodel.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
/*
 
2
 * Copyright (C) 2015 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include "Greeter.h"
 
18
#include "PromptsModel.h"
 
19
 
 
20
#include <QtTest>
 
21
 
 
22
class GreeterPromptsModelTest : public QObject
 
23
{
 
24
    Q_OBJECT
 
25
 
 
26
private:
 
27
    class PromptInfo {
 
28
    public:
 
29
        PromptInfo(PromptsModel::PromptType t, const QString &p)
 
30
            : type(t), text(p) {}
 
31
        PromptsModel::PromptType type;
 
32
        QString text;
 
33
    };
 
34
 
 
35
    void selectUser(const QString &user)
 
36
    {
 
37
        Greeter::instance()->authenticate(user);
 
38
        QCOMPARE(Greeter::instance()->authenticationUser(), user);
 
39
    }
 
40
 
 
41
    void respond(const QString &response, bool authenticated)
 
42
    {
 
43
        Greeter::instance()->respond(response);
 
44
        QCOMPARE(Greeter::instance()->isAuthenticated(), authenticated);
 
45
    }
 
46
 
 
47
    void comparePrompts(const QList<PromptInfo> &expected)
 
48
    {
 
49
        QTRY_COMPARE(prompts->rowCount(), expected.size());
 
50
        for (int i = 0; i < prompts->rowCount(); i++) {
 
51
            QCOMPARE(prompts->data(prompts->index(i, 0), PromptsModel::TypeRole).toInt(), (int)expected[i].type);
 
52
            QCOMPARE(prompts->data(prompts->index(i, 0), PromptsModel::TextRole).toString(), expected[i].text);
 
53
        }
 
54
    }
 
55
 
 
56
private Q_SLOTS:
 
57
 
 
58
    void initTestCase()
 
59
    {
 
60
        prompts = Greeter::instance()->promptsModel();
 
61
        QVERIFY(prompts);
 
62
    }
 
63
 
 
64
    void init()
 
65
    {
 
66
        QCOMPARE(prompts->rowCount(), 0);
 
67
    }
 
68
 
 
69
    void cleanup()
 
70
    {
 
71
        prompts->clear();
 
72
    }
 
73
 
 
74
    void testSimpleFailure()
 
75
    {
 
76
        selectUser("has-password");
 
77
        comparePrompts({{PromptsModel::Secret, ""}});
 
78
        respond("nope", false);
 
79
        comparePrompts({{PromptsModel::Secret, ""}}); // prompts don't change immediately
 
80
        selectUser("has-password");
 
81
        comparePrompts({{PromptsModel::Error, "Invalid password, please try again"},
 
82
                        {PromptsModel::Secret, ""}});
 
83
    }
 
84
 
 
85
    void testSimpleSuccess()
 
86
    {
 
87
        selectUser("has-password");
 
88
        comparePrompts({{PromptsModel::Secret, ""}});
 
89
        respond("password", true);
 
90
        comparePrompts({{PromptsModel::Secret, ""}}); // prompts don't change
 
91
    }
 
92
 
 
93
    void testHasPassword()
 
94
    {
 
95
        selectUser("has-password");
 
96
        comparePrompts({{PromptsModel::Secret, ""}});
 
97
    }
 
98
 
 
99
    void testDifferentPrompt()
 
100
    {
 
101
        selectUser("different-prompt");
 
102
        comparePrompts({{PromptsModel::Secret, "Secret word"}});
 
103
    }
 
104
 
 
105
    void testNoPassword()
 
106
    {
 
107
        selectUser("no-password");
 
108
        comparePrompts({{PromptsModel::Button, "Log In"}});
 
109
    }
 
110
 
 
111
    void testAuthError()
 
112
    {
 
113
        selectUser("auth-error");
 
114
        comparePrompts({{PromptsModel::Error, "Failed to authenticate"},
 
115
                        {PromptsModel::Button, "Retry"}});
 
116
    }
 
117
 
 
118
    void testTwoFactor()
 
119
    {
 
120
        selectUser("two-factor");
 
121
        comparePrompts({{PromptsModel::Secret, ""}});
 
122
        respond("password", false);
 
123
        comparePrompts({{PromptsModel::Question, "otp"}});
 
124
    }
 
125
 
 
126
    void testTwoPrompts()
 
127
    {
 
128
        selectUser("two-prompts");
 
129
        comparePrompts({{PromptsModel::Question, "Favorite Color (blue)"},
 
130
                        {PromptsModel::Secret, ""}});
 
131
    }
 
132
 
 
133
    void testWackyPrompts()
 
134
    {
 
135
        selectUser("wacky-prompts");
 
136
        comparePrompts({{PromptsModel::Message, "First message"},
 
137
                        {PromptsModel::Question, "Favorite Color (blue)"},
 
138
                        {PromptsModel::Error, "Second message"},
 
139
                        {PromptsModel::Secret, ""},
 
140
                        {PromptsModel::Message, "Last message"}});
 
141
    }
 
142
 
 
143
    void testHtmlInfoPrompts()
 
144
    {
 
145
        selectUser("html-info-prompt");
 
146
        comparePrompts({{PromptsModel::Message, "<b>&</b>"},
 
147
                        {PromptsModel::Secret, ""}});
 
148
    }
 
149
 
 
150
    void testInfoAfterLogin()
 
151
    {
 
152
        selectUser("info-after-login");
 
153
        comparePrompts({{PromptsModel::Secret, ""}});
 
154
        respond("password", true);
 
155
        comparePrompts({{PromptsModel::Message, "Congratulations on logging in!"},
 
156
                        {PromptsModel::Button, "Log In"}});
 
157
    }
 
158
 
 
159
    void testLocked()
 
160
    {
 
161
        selectUser("locked");
 
162
        comparePrompts({{PromptsModel::Secret, ""}});
 
163
        respond("nope", false);
 
164
        selectUser("locked");
 
165
        comparePrompts({{PromptsModel::Error, "Account is locked"},
 
166
                        {PromptsModel::Secret, ""}});
 
167
    }
 
168
 
 
169
private:
 
170
    PromptsModel *prompts;
 
171
};
 
172
 
 
173
QTEST_GUILESS_MAIN(GreeterPromptsModelTest)
 
174
 
 
175
#include "promptsmodel.moc"