/
AuthenticationTest.swift
353 lines (289 loc) · 14 KB
/
AuthenticationTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import XCTest
extension String {
subscript (i: Int) -> Character {
return self[self.characters.index(self.startIndex, offsetBy: i)]
}
subscript (i: Int) -> String {
return String(self[i] as Character)
}
subscript (r: Range<Int>) -> String {
let start = characters.index(startIndex, offsetBy: r.lowerBound)
let end = self.index(start, offsetBy: r.upperBound - r.lowerBound)
return self[Range(start ..< end)]
}
}
class AuthenticationTest: BaseTestCase {
var navigator: Navigator!
var app: XCUIApplication!
override func setUp() {
super.setUp()
app = XCUIApplication()
navigator = createScreenGraph(app).navigator(self)
continueAfterFailure = false
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
closeAuthenticationManager()
super.tearDown()
}
fileprivate func typePasscode(_ passCode: String) {
app.keys[passCode[0]].tap()
app.keys[passCode[1]].tap()
app.keys[passCode[2]].tap()
app.keys[passCode[3]].tap()
}
fileprivate func closeAuthenticationManager() {
navigator.goto(NewTabScreen)
}
fileprivate func disablePasscode(_ passCode: String) {
navigator.goto(PasscodeSettings)
app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode Off"].tap()
waitforExistence(app.staticTexts["Enter passcode"])
typePasscode(passCode)
waitforExistence(app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode On"])
}
fileprivate func enablePasscode(_ passCode: String, interval: String = "Immediately") {
let authenticationmanagerSettingstableviewTable = app.tables["AuthenticationManager.settingsTableView"]
navigator.goto(PasscodeSettings)
app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode On"].tap()
waitforExistence(app.staticTexts["Enter a passcode"])
typePasscode(passCode)
waitforExistence(app.staticTexts["Re-enter passcode"])
typePasscode(passCode)
waitforExistence(app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode Off"])
navigator.goto(PasscodeIntervalSettings)
typePasscode(passCode)
app.staticTexts[interval].tap()
navigator.goto(PasscodeSettings)
waitforExistence(authenticationmanagerSettingstableviewTable.staticTexts[interval])
}
// Sets the passcode and interval (set to immediately)
func testTurnOnOff() {
enablePasscode("1337")
XCTAssertTrue(app.staticTexts["Immediately"].exists)
disablePasscode("1337")
}
func testChangePassCode() {
enablePasscode("1337")
app.staticTexts["Change Passcode"].tap()
waitforExistence(app.staticTexts["Enter passcode"])
typePasscode("1337")
waitforExistence(app.staticTexts["Enter a new passcode"])
typePasscode("2337")
waitforExistence(app.staticTexts["Re-enter passcode"])
typePasscode("2337")
waitforExistence(app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode Off"])
disablePasscode("2337")
}
func testPromptPassCodeUponReentry() {
enablePasscode("1337")
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.tables["Login List"])
//send app to background, and re-enter
XCUIDevice.shared().press(.home)
app.activate()
let contentView = app.navigationBars["Client.FxAContentView"]
if contentView.exists {
app.navigationBars["Client.FxAContentView"].buttons["Settings"].tap()
}
navigator.nowAt("SettingsScreen")
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
}
func testPromptPassCodeUponReentryWithDelay() {
enablePasscode("1337", interval: "After 5 minutes")
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.tables["Login List"])
//send app to background, and re-enter
XCUIDevice.shared().press(.home)
app.activate()
let contentView = app.navigationBars["Client.FxAContentView"]
if contentView.exists {
app.navigationBars["Client.FxAContentView"].buttons["Settings"].tap()
}
navigator.nowAt("SettingsScreen")
navigator.goto(LoginsSettings)
waitforExistence(app.tables["Login List"])
}
func testChangePasscodeShowsErrorStates() {
enablePasscode("1337")
app.staticTexts["Change Passcode"].tap()
waitforExistence(app.staticTexts["Enter passcode"])
typePasscode("2337")
waitforExistence(app.staticTexts["Incorrect passcode. Try again (Attempts remaining: 2)."])
typePasscode("3337")
waitforExistence(app.staticTexts["Incorrect passcode. Try again (Attempts remaining: 1)."])
typePasscode("1337")
waitforExistence(app.staticTexts["Enter a new passcode"])
// Enter same passcode as new one
typePasscode("1337")
waitforExistence(app.staticTexts["New passcode must be different than existing code."])
// Enter mismatched passcode
typePasscode("2337")
waitforExistence(app.staticTexts["Re-enter passcode"])
typePasscode("3337")
waitforExistence(app.staticTexts["Passcodes didn't match. Try again."])
// Put proper password
XCTAssertTrue(app.staticTexts["Enter a new passcode"].exists)
typePasscode("2337")
waitforExistence(app.staticTexts["Re-enter passcode"])
typePasscode("2337")
waitforExistence(app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode Off"])
disablePasscode("2337")
}
func testChangeRequirePasscodeInterval() {
enablePasscode("1337")
let authenticationmanagerSettingstableviewTable = app.tables["AuthenticationManager.settingsTableView"]
navigator.goto(PasscodeIntervalSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.staticTexts["Immediately"])
XCTAssertTrue(app.staticTexts["After 1 minute"].exists)
XCTAssertTrue(app.staticTexts["After 5 minutes"].exists)
XCTAssertTrue(app.staticTexts["After 10 minutes"].exists)
XCTAssertTrue(app.staticTexts["After 15 minutes"].exists)
XCTAssertTrue(app.staticTexts["After 1 hour"].exists)
app.staticTexts["After 15 minutes"].tap()
navigator.goto(PasscodeSettings)
waitforExistence(authenticationmanagerSettingstableviewTable.staticTexts["After 15 minutes"])
// Since we set to 15 min, it shouldn't ask for password again, but it skips verification
// only when timing isn't changed. (could be due to timer reset?)
// For clarification, raised Bug 1325439
navigator.goto(PasscodeIntervalSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
navigator.goto(PasscodeSettings)
waitforExistence(authenticationmanagerSettingstableviewTable.staticTexts["After 15 minutes"])
disablePasscode("1337")
}
func testEnteringLoginsUsingPasscode() {
enablePasscode("1337")
// Enter login
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.tables["Login List"])
navigator.goto(SettingsScreen)
// Trying again should display passcode screen since we've set the interval to be immediately.
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.tables["Login List"])
disablePasscode("1337")
}
func testEnteringLoginsUsingPasscodeWithFiveMinutesInterval() {
enablePasscode("1337", interval: "After 5 minutes")
// now we've changed the timeout, we should prompt next time for passcode.
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.tables["Login List"])
// Trying again should not display the passcode screen since the interval is 5 minutes
navigator.goto(SettingsScreen)
navigator.goto(LoginsSettings)
waitforExistence(app.tables["Login List"])
navigator.goto(PasscodeIntervalSettings)
waitforExistence(app.staticTexts["After 5 minutes"])
disablePasscode("1337")
}
func testEnteringLoginsWithNoPasscode() {
// it is disabled
navigator.goto(PasscodeSettings)
waitforExistence(app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode On"])
navigator.goto(LoginsSettings)
waitforExistence(app.tables["Login List"])
}
func testWrongPasscodeDisplaysAttemptsAndMaxError() {
enablePasscode("1337")
app.tables["AuthenticationManager.settingsTableView"].staticTexts["Require Passcode"].tap()
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.staticTexts["After 5 minutes"])
app.staticTexts["After 5 minutes"].tap()
app.navigationBars["Require Passcode"].buttons["Passcode"].tap()
waitforExistence(app.tables["AuthenticationManager.settingsTableView"].staticTexts["After 5 minutes"])
// Enter wrong passcode
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("2337")
waitforExistence(app.staticTexts["Incorrect passcode. Try again (Attempts remaining: 2)."])
typePasscode("3337")
waitforExistence(app.staticTexts["Incorrect passcode. Try again (Attempts remaining: 1)."])
typePasscode("3337")
waitforExistence(app.staticTexts["Maximum attempts reached. Please try again later."])
}
func testWrongPasscodeAttemptsPersistAcrossEntryAndConfirmation() {
enablePasscode("1337")
// Enter wrong passcode on Logins
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("2337")
waitforExistence(app.staticTexts["Incorrect passcode. Try again (Attempts remaining: 2)."])
// Go back to Passcode, and enter a wrong passcode, notice the error count
navigator.goto(PasscodeSettings)
app.staticTexts["Change Passcode"].tap()
waitforExistence(app.staticTexts["Enter passcode"])
typePasscode("2337")
waitforExistence(app.staticTexts["Incorrect passcode. Try again (Attempts remaining: 1)."])
app.buttons["Cancel"].tap()
disablePasscode("1337")
}
func testChangedPasswordMustBeNew() {
enablePasscode("1337")
app.staticTexts["Change Passcode"].tap()
waitforExistence(app.staticTexts["Enter passcode"])
typePasscode("1337")
waitforExistence(app.staticTexts["Enter a new passcode"])
typePasscode("1337")
waitforExistence(app.staticTexts["New passcode must be different than existing code."])
app.navigationBars["Change Passcode"].buttons["Cancel"].tap()
disablePasscode("1337")
}
func testPasscodesMustMatchWhenCreating() {
navigator.goto(PasscodeSettings)
app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode On"].tap()
waitforExistence(app.staticTexts["Enter a passcode"])
typePasscode("1337")
waitforExistence(app.staticTexts["Re-enter passcode"])
typePasscode("2337")
waitforExistence(app.staticTexts["Passcodes didn't match. Try again."])
waitforExistence(app.staticTexts["Enter a passcode"])
app.buttons["Cancel"].tap()
}
func testPasscodeMustBeCorrectWhenRemoving() {
enablePasscode("1337")
XCTAssertTrue(app.staticTexts["Immediately"].exists)
app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode Off"].tap()
waitforExistence(app.staticTexts["Enter passcode"])
typePasscode("2337")
waitforExistence(app.staticTexts["Incorrect passcode. Try again (Attempts remaining: 2)."])
typePasscode("1337")
waitforExistence(app.tables["AuthenticationManager.settingsTableView"].staticTexts["Turn Passcode On"])
}
func testChangingIntervalResetsValidationTimer() {
enablePasscode("1337")
// Enter login, since the default is 'set immediately,' it will ask for passcode
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.tables["Login List"])
navigator.goto(PasscodeIntervalSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
typePasscode("1337")
waitforExistence(app.staticTexts["Immediately"])
app.staticTexts["After 15 minutes"].tap()
navigator.goto(PasscodeSettings)
waitforExistence(app.tables["AuthenticationManager.settingsTableView"].staticTexts["After 15 minutes"])
// Enter login, since the interval is reset, it will ask for password again
navigator.goto(LoginsSettings)
waitforExistence(app.navigationBars["Enter Passcode"])
disablePasscode("1337")
}
}