forked from okta/okta-oidc-ios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOktaScenarios.swift
269 lines (208 loc) · 8.32 KB
/
OktaScenarios.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
/*
* Copyright (c) 2017-Present, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/
import XCTest
import OktaOidc
final class OktaScenarios: XCTestCase {
// Update these values along with your Plist config
var username = ProcessInfo.processInfo.environment["USERNAME"]!
var password = ProcessInfo.processInfo.environment["PASSWORD"]!
var issuer = ProcessInfo.processInfo.environment["ISSUER"]!
var redirectURI = ProcessInfo.processInfo.environment["REDIRECT_URI"]!
var logoutRedirectURI = ProcessInfo.processInfo.environment["LOGOUT_REDIRECT_URI"]!
var clientID = ProcessInfo.processInfo.environment["CLIENT_ID"]!
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
private var browserContinueButton: XCUIElement {
springboard.buttons["Continue"]
}
private var toolbarDoneButton: XCUIElement {
app.toolbars["Toolbar"].buttons["Done"]
}
private var app: XCUIApplication!
private var tokenTextView: XCUIElement {
app.textViews["tokenView"]
}
private var signInButton: XCUIElement {
app.buttons["SignIn"]
}
private var sessionTokenTextView: XCUIElement {
app.textViews["SessionTokenTextView"]
}
private var getUserButton: XCUIElement {
app.buttons["GetUser"]
}
private var introspectButton: XCUIElement {
app.buttons["Introspect"]
}
private var revokeButton: XCUIElement {
app.buttons["Revoke"]
}
private var clearButton: XCUIElement {
app.buttons["Clear"]
}
private var signOutButton: XCUIElement {
app.buttons["SignOutOkta"]
}
private var continueSpringboardButton: XCUIElement {
springboard.buttons["Continue"]
}
private var authenticateButton: XCUIElement {
app.buttons["Authenticate"]
}
// MARK: -
override func setUpWithError() throws {
try super.setUpWithError()
try XCTSkipIf(clientID.count == 0 || password.count == 0,
"Cannot run UI tests without CLIENT_ID or PASSWORD environment variables")
app = XCUIApplication()
app.launchEnvironment = [
"UITEST": "1",
"ISSUER": issuer,
"CLIENT_ID": clientID,
"REDIRECT_URI": redirectURI,
"LOGOUT_REDIRECT_URI" : logoutRedirectURI
]
continueAfterFailure = false
app.launch()
if clearButton.exists {
clearButton.tap()
}
}
func testAuthCodeFlow() {
// given
signInAndWait()
waitForText(predicate: "CONTAINS 'Access Token'", object: tokenTextView, timeout: .testing)
// when
app.terminate()
app.launch()
// then
waitForText(predicate: "CONTAINS 'Access Token'", object: tokenTextView, timeout: .testing)
// then
pressGetUserButton()
pressIntrospectButton(expectedValue: "true")
pressRevokeButton()
}
func testAuthCodeFlowAndUserInfo() throws {
// given
signInAndWait()
// then
pressGetUserButton()
}
func testAuthCodeFlowIntrospectAndRevoke() throws {
// given
signInAndWait()
// then
pressIntrospectButton(expectedValue: "true")
}
func testAuthCodeFlowRevokeAndIntrospect() throws {
// given
signInAndWait()
// then
pressRevokeButton()
pressIntrospectButton(expectedValue: "false")
}
func testSignOutFlow() {
// given
signInAndWait()
// when
signOutButton.tap()
// then
XCTAssertTrue(continueSpringboardButton.waitForExistence(timeout: .minimal))
continueSpringboardButton.tap()
XCTAssertTrue(app.webViews.firstMatch.waitForExistence(timeout: .testing))
XCTAssertTrue(tokenTextView.waitForExistence(timeout: .testing))
waitForText(predicate: "== ''", object: tokenTextView, timeout: .minimal)
}
func testAuthenticateWithInvalidSessionToken() throws {
// given
authenticateButton.tap()
XCTAssertTrue(sessionTokenTextView.waitForExistence(timeout: .minimal))
sessionTokenTextView.clearText(app: app)
sessionTokenTextView.tap()
sessionTokenTextView.typeText("Some_Invalid_Token")
// when
let authbutton = app.buttons["AuthenticateWithSessionToken"]
authbutton.tap()
// then
let messageView = app.textViews["MessageView"]
XCTAssertTrue(messageView.waitForExistence(timeout: .testing))
waitForText(predicate: "CONTAINS 'Error'", object: messageView, timeout: .minimal)
}
// MARK: - Private
private func signInAndWait() {
XCTAssertTrue(tokenTextView.waitForExistence(timeout: .minimal))
XCTAssertNotEqual(tokenTextView.value as? String, "SDK is not configured!")
// Sign In
signIn(username: username, password: password)
XCTAssertTrue(tokenTextView.waitForExistence(timeout: .minimal))
}
private func pressGetUserButton() {
getUserButton.tap()
// then
waitForText(predicate: "CONTAINS '\(username)'", object: tokenTextView, timeout: .testing)
}
private func pressIntrospectButton(expectedValue: String) {
introspectButton.tap()
// then
waitForText(predicate: "CONTAINS '\(expectedValue)'", object: tokenTextView, timeout: .testing)
}
private func pressRevokeButton() {
// when
revokeButton.tap()
// then
waitForText(predicate: "CONTAINS 'AccessToken was revoked'", object: tokenTextView, timeout: .testing)
}
private func signIn(username: String, password: String) {
signInButton.tap()
allowBrowserLaunch()
let webView = app.webViews
let usernameWebField = webView.textFields.element(boundBy: 0)
XCTAssertTrue(usernameWebField.waitForExistence(timeout: .testing))
usernameWebField.tap()
// If the "Swipe-to-type" keyboard is shown (e.g. this is the first launch of the
// device after an Erase & Restart), dismiss the keyboard onboarding view to reveal
// the regular software keyboard.
let continueButton = app.buttons["Continue"]
if continueButton.exists {
continueButton.tap()
}
usernameWebField.clearText(app: app)
usernameWebField.typeText(username)
if toolbarDoneButton.exists {
toolbarDoneButton.tap()
}
let passwordWebField = webView.secureTextFields.allElementsBoundByIndex.first { $0.frame.width >= usernameWebField.frame.width }!
XCTAssertTrue(passwordWebField.waitForExistence(timeout: .testing))
passwordWebField.tap()
UIPasteboard.general.string = password
passwordWebField.doubleTap()
app.menuItems["Paste"].tap()
// We would dismiss keyboard by entering return key ('\n').
// But there's 'Go' button instead of return.
if toolbarDoneButton.exists {
toolbarDoneButton.tap()
}
let verifyWebButton = webView.buttons["Verify"]
if verifyWebButton.exists {
verifyWebButton.tap()
} else {
webView.buttons["Sign In"].tap()
}
}
private func allowBrowserLaunch() {
XCTAssertTrue(browserContinueButton.waitForExistence(timeout: .minimal))
browserContinueButton.tap()
}
}
extension TimeInterval {
static let testing: TimeInterval = 30
static let minimal: TimeInterval = testing / 2
}