@@ -88,9 +88,10 @@ class RealBackendTestCase: ShellTestCase {
8888 let totpSecret : Data
8989 }
9090
91- func googleInputs ( ) throws -> GoogleInputs {
92- guard ProcessInfo . processInfo . environment [ " OPENBOT_SHELL_RUN_GOOGLE " ] == " true " else {
93- throw XCTSkip ( " the Google sign-in runs only when the workflow is started by hand with google_sign_in: each attempt sends a sign-in prompt to the account's phone " )
91+ /// `gate`: the dispatch input that must be "true" (each attempt sends a prompt to the phone).
92+ func googleInputs ( gate : String = " OPENBOT_SHELL_RUN_GOOGLE " ) throws -> GoogleInputs {
93+ guard ProcessInfo . processInfo . environment [ gate ] == " true " else {
94+ throw XCTSkip ( " this Google sign-in runs only when the workflow is started by hand with its input on: each attempt sends a sign-in prompt to the account's phone " )
9495 }
9596 let names = [ " OPENBOT_SHELL_GOOGLE_EMAIL " , " OPENBOT_SHELL_GOOGLE_PASSWORD " , " OPENBOT_SHELL_GOOGLE_TOTP_SECRET " ]
9697 note ( " secret-inputs: \( names . allSatisfy { secret ( $0 ) != nil } ? " present " : " absent " ) " )
@@ -103,20 +104,25 @@ class RealBackendTestCase: ShellTestCase {
103104 return GoogleInputs ( email : email , password : password , totpSecret : totpSecret )
104105 }
105106
106- /// Google's email, password and authenticator pages, wherever they show (`roots`). One attempt.
107- /// Stops (XCTSkip with the reason) at any CAPTCHA, "verify it's you" or other challenge: nothing
108- /// here tries to get past one. Screenshots only before a value goes in and after the page is left.
109- func googleSteps ( app : XCUIApplication , roots : [ XCUIApplication ] , inputs : GoogleInputs ) throws {
107+ /// Google's email, password and 2-Step Verification pages, wherever they show (`roots`). One
108+ /// attempt. On the 2-Step page it answers only with the account's own authenticator code: when
109+ /// `tryAnotherWay`, it may pick that option under "Try another way" (the user decided so). It stops
110+ /// (XCTSkip with the reason) at any CAPTCHA, bot check or anything else Google asks for: nothing
111+ /// here tries to get past one. Screenshots only before a value goes in and after a page is left.
112+ func googleSteps ( app : XCUIApplication , roots : [ XCUIApplication ] , inputs : GoogleInputs , tryAnotherWay : Bool = false ) throws {
110113 func live ( ) -> [ XCUIApplication ] { roots . filter { $0 . state != . notRunning } }
111114 func pageText ( ) -> String {
112115 live ( ) . flatMap { labels ( in : $0 , limit : 200 ) } . joined ( separator : " \n " ) . lowercased ( )
113116 }
114- func stopIfChallenged ( _ step : String ) throws {
117+ func stopIfChallenged ( _ step : String , allowSecondStep : Bool = false ) throws {
115118 let text = pageText ( )
116- let signs = [ " captcha " , " verify it \u{2019} s you " , " verify it's you " , " confirm it \u{2019} s you " , " confirm it's you " ,
117- " unusual activity " , " not a robot " , " type the text you hear or see " , " check your phone " ,
119+ var signs = [ " captcha " , " unusual activity " , " not a robot " , " type the text you hear or see " ,
118120 " this browser or app may not be secure " , " couldn \u{2019} t sign you in " , " couldn't sign you in " ,
119121 " try again later " , " suspicious " , " disallowed_useragent " ]
122+ // The 2-Step Verification page itself says "verify it's you"; anywhere else that is a challenge.
123+ if ! ( allowSecondStep && text . contains ( " 2-step verification " ) ) {
124+ signs += [ " verify it \u{2019} s you " , " verify it's you " , " confirm it \u{2019} s you " , " confirm it's you " , " check your phone " ]
125+ }
120126 if let sign = signs . first ( where : { text . contains ( $0 ) } ) {
121127 screenshot ( " challenge-at- \( step . replacingOccurrences ( of : " " , with : " - " ) ) " )
122128 note ( " challenged by Google at \( step ) (page says ' \( sign ) ') " )
@@ -181,10 +187,36 @@ class RealBackendTestCase: ShellTestCase {
181187 submit ( passwordField )
182188 sleep ( 10 )
183189 screenshot ( " google-after-password " )
184- try stopIfChallenged ( " after password " )
190+ try stopIfChallenged ( " after password " , allowSecondStep : tryAnotherWay )
185191
186- let text = pageText ( )
187- if text . contains ( " authenticator " ) || text . contains ( " enter code " ) || text . contains ( " 2-step verification " ) {
192+ var text = pageText ( )
193+ let codePage = { ( t : String ) in t . contains ( " google authenticator " ) || t . contains ( " authenticator app " ) || t . contains ( " enter code " ) }
194+ if text . contains ( " 2-step verification " ) && ! codePage ( text ) {
195+ guard tryAnotherWay else { throw XCTSkip ( " Google asked for its 2-Step Verification phone prompt " ) }
196+ let another = NSPredicate ( format : " label CONTAINS[c] 'Try another way' " )
197+ guard let tryAnother = first ( { $0 . buttons . matching ( another ) . firstMatch } , timeout : 8 )
198+ ?? first ( { $0 . links . matching ( another ) . firstMatch } , timeout : 2 ) else {
199+ screenshot ( " google-no-try-another-way " )
200+ throw XCTSkip ( " the 2-Step page offered no 'Try another way' " )
201+ }
202+ tryAnother . tap ( )
203+ sleep ( 6 )
204+ screenshot ( " google-other-ways " )
205+ let options = pageText ( )
206+ note ( " other ways offered: authenticator \( options . contains ( " authenticator " ) ) , backup codes \( options . contains ( " backup code " ) ) , text message \( options . contains ( " text message " ) || options . contains ( " sms " ) ) " )
207+ let authenticator = NSPredicate ( format : " label CONTAINS[c] 'Authenticator' " )
208+ guard let option = first ( { $0 . links . matching ( authenticator ) . firstMatch } , timeout : 5 )
209+ ?? first ( { $0 . buttons . matching ( authenticator ) . firstMatch } , timeout : 2 )
210+ ?? first ( { $0 . staticTexts . matching ( authenticator ) . firstMatch } , timeout : 2 ) else {
211+ throw XCTSkip ( " the authenticator app is not among Google's other ways " )
212+ }
213+ option . tap ( )
214+ sleep ( 6 )
215+ screenshot ( " google-authenticator-page " )
216+ try stopIfChallenged ( " authenticator page " , allowSecondStep : true )
217+ text = pageText ( )
218+ }
219+ if codePage ( text ) || text . contains ( " 2-step verification " ) {
188220 guard let codeField = first ( { $0 . webViews . textFields . firstMatch } , timeout : 10 )
189221 ?? first ( { $0 . textFields . firstMatch } , timeout : 3 ) else {
190222 throw XCTSkip ( " 2-Step Verification page without a code field reachable " )
@@ -194,6 +226,7 @@ class RealBackendTestCase: ShellTestCase {
194226 sleep ( 12 )
195227 screenshot ( " google-after-code " )
196228 try stopIfChallenged ( " after code " )
229+ note ( " the authenticator code was accepted: \( ! pageText ( ) . contains ( " wrong code " ) && ! pageText ( ) . contains ( " 2-step verification " ) ) " )
197230 } else {
198231 note ( " no authenticator page after the password " )
199232 }
@@ -347,7 +380,7 @@ final class RealBackendUITests: RealBackendTestCase {
347380 /// The real sign-in with Google inside the app's web view, then (if it lets the person in) the
348381 /// whole signed-in journey in that same session, with no hook at all.
349382 func testRealGoogleSignInInWebViewAndJourney ( ) throws {
350- let inputs = try googleInputs ( )
383+ let inputs = try googleInputs ( gate : " OPENBOT_SHELL_RUN_GOOGLE_WEBVIEW " )
351384 try requireProduction ( )
352385 note ( " video-mark start \( Date ( ) . timeIntervalSince1970 ) " )
353386 defer { note ( " video-mark end \( Date ( ) . timeIntervalSince1970 ) " ) }
@@ -412,10 +445,15 @@ final class RealBackendDebugUITests: RealBackendTestCase {
412445 google . tap ( )
413446 sleep ( 10 )
414447 screenshot ( " 2-google " )
415- try googleSteps ( app : app , roots : [ app , service ] , inputs : inputs )
416- let landed = firstExisting ( [ app . staticTexts . matching ( NSPredicate ( format : " label CONTAINS 'Start a conversation' " ) ) . firstMatch ,
417- app . textViews . firstMatch ] , timeout : 20 ) != nil
418- note ( " the sheet ended on OpenBot signed in: \( landed ) " )
448+ try googleSteps ( app : app , roots : [ app , service ] , inputs : inputs , tryAnotherWay : true )
449+ // Production has no hand-off yet: the sheet should end on my.open.bot, signed in, inside the sheet.
450+ sleep ( 8 )
451+ screenshot ( " 3-where-the-sheet-ended " )
452+ let home = NSPredicate ( format : " label CONTAINS 'Start a conversation' OR label CONTAINS 'Ask anything' " )
453+ let landed = firstExisting ( [ app . staticTexts . matching ( home ) . firstMatch , service . staticTexts . matching ( home ) . firstMatch ,
454+ app . textViews . matching ( home ) . firstMatch ] , timeout : 20 ) != nil
455+ let ended = labels ( in : app , limit : 200 ) . joined ( separator : " \n " )
456+ note ( " the sheet ended on OpenBot signed in: \( landed ) ; OpenBot sign-in page \( ended . contains ( " Sign in to OpenBot " ) ) ; Google page \( ended . contains ( " Google " ) ) " )
419457 guard landed else { throw XCTSkip ( " Google's sign-in in the sheet did not end on OpenBot signed in " ) }
420458 }
421459
0 commit comments