on 7b68b97 help sign in
175 lines 5.1 kB history raw
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 */
17#include "Greeter.h"
18#include "PromptsModel.h"
20#include <QtTest>
22class GreeterPromptsModelTest : public QObject
24 Q_OBJECT
26private:
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 };
35 void selectUser(const QString &user)
36 {
37 Greeter::instance()->authenticate(user);
38 QCOMPARE(Greeter::instance()->authenticationUser(), user);
39 }
41 void respond(const QString &response, bool authenticated)
42 {
43 Greeter::instance()->respond(response);
44 QCOMPARE(Greeter::instance()->isAuthenticated(), authenticated);
45 }
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 }
56private Q_SLOTS:
58 void initTestCase()
59 {
60 prompts = Greeter::instance()->promptsModel();
61 QVERIFY(prompts);
62 }
64 void init()
65 {
66 QCOMPARE(prompts->rowCount(), 0);
67 }
69 void cleanup()
70 {
71 prompts->clear();
72 }
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 }
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 }
93 void testHasPassword()
94 {
95 selectUser("has-password");
96 comparePrompts({{PromptsModel::Secret, ""}});
97 }
99 void testDifferentPrompt()
100 {
101 selectUser("different-prompt");
102 comparePrompts({{PromptsModel::Secret, "Secret word"}});
103 }
105 void testNoPassword()
106 {
107 selectUser("no-password");
108 comparePrompts({{PromptsModel::Button, "Log In"}});
109 }
111 void testAuthError()
112 {
113 selectUser("auth-error");
114 comparePrompts({{PromptsModel::Error, "Failed to authenticate"},
115 {PromptsModel::Button, "Retry"}});
116 }
118 void testTwoFactor()
119 {
120 selectUser("two-factor");
121 comparePrompts({{PromptsModel::Secret, ""}});
122 respond("password", false);
123 comparePrompts({{PromptsModel::Question, "otp"}});
124 }
126 void testTwoPrompts()
127 {
128 selectUser("two-prompts");
129 comparePrompts({{PromptsModel::Question, "Favorite Color (blue)"},
130 {PromptsModel::Secret, ""}});
131 }
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 }
143 void testHtmlInfoPrompts()
144 {
145 selectUser("html-info-prompt");
146 comparePrompts({{PromptsModel::Message, "<b>&</b>"},
147 {PromptsModel::Secret, ""}});
148 }
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 }
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 }
169private:
170 PromptsModel *prompts;
171};
173QTEST_GUILESS_MAIN(GreeterPromptsModelTest)
175#include "promptsmodel.moc"