2424import java .io .IOException ;
2525import java .io .PrintStream ;
2626import java .net .URI ;
27+ import java .time .Duration ;
2728import java .util .concurrent .ExecutionException ;
2829import java .util .concurrent .TimeUnit ;
2930import java .util .concurrent .TimeoutException ;
3031import org .json .JSONException ;
32+ import org .json .JSONObject ;
3133import org .junit .After ;
3234import org .junit .AfterClass ;
3335import org .junit .Assert ;
4749import org .springframework .boot .web .server .LocalServerPort ;
4850import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
4951import org .springframework .web .util .UriComponentsBuilder ;
52+ import recaptcha .AnnotateAssessment ;
5053
5154@ RunWith (SpringJUnit4ClassRunner .class )
5255@ EnableAutoConfiguration
@@ -55,6 +58,7 @@ public class SnippetsIT {
5558
5659 private static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
5760 private static final String DOMAIN_NAME = "localhost" ;
61+ private static String ASSESSMENT_NAME = "" ;
5862 private static String RECAPTCHA_SITE_KEY_1 = "recaptcha-site-key1" ;
5963 private static String RECAPTCHA_SITE_KEY_2 = "recaptcha-site-key2" ;
6064 private static WebDriver browser ;
@@ -69,7 +73,7 @@ public static void requireEnvVar(String envVarName) {
6973 }
7074
7175 @ BeforeClass
72- public static void setUp () throws IOException , InterruptedException {
76+ public static void setUp () throws IOException , InterruptedException , JSONException {
7377 requireEnvVar ("GOOGLE_APPLICATION_CREDENTIALS" );
7478 requireEnvVar ("GOOGLE_CLOUD_PROJECT" );
7579
@@ -144,11 +148,59 @@ public void testDeleteSiteKey()
144148 }
145149
146150 @ Test
147- public void testCreateAssessment () throws IOException , JSONException , InterruptedException {
151+ public void testCreateAnnotateAssessment ()
152+ throws JSONException , IOException , InterruptedException {
153+ // Create an assessment.
154+ String testURL = "http://localhost:" + randomServerPort + "/" ;
155+ JSONObject createAssessmentResult = createAssessment (testURL );
156+ ASSESSMENT_NAME = createAssessmentResult .getString ("assessmentName" );
157+ // Verify that the assessment name has been modified post the assessment creation.
158+ assertThat (ASSESSMENT_NAME ).isNotEmpty ();
159+
160+ // Annotate the assessment.
161+ AnnotateAssessment .annotateAssessment (PROJECT_ID , ASSESSMENT_NAME );
162+ assertThat (stdOut .toString ()).contains ("Annotated response sent successfully ! " );
163+ }
164+
165+ public JSONObject createAssessment (String testURL )
166+ throws IOException , JSONException , InterruptedException {
167+
168+ // Setup the automated browser test and retrieve the token and action.
169+ JSONObject tokenActionPair = initiateBrowserTest (testURL );
170+
171+ // Send the token for analysis. The analysis score ranges from 0.0 to 1.0
172+ recaptcha .CreateAssessment .createAssessment (
173+ PROJECT_ID ,
174+ RECAPTCHA_SITE_KEY_1 ,
175+ tokenActionPair .getString ("token" ),
176+ tokenActionPair .getString ("action" ));
177+
178+ // Analyse the response.
179+ String response = stdOut .toString ();
180+ assertThat (response ).contains ("Assessment name: " );
181+ assertThat (response ).contains ("The reCAPTCHA score is: " );
182+ float recaptchaScore = 0 ;
183+ String assessmentName = "" ;
184+ for (String line : response .split ("\n " )) {
185+ if (line .contains ("The reCAPTCHA score is: " )) {
186+ recaptchaScore = Float .parseFloat (substr (line ));
187+ } else if (line .contains ("Assessment name: " )) {
188+ assessmentName = substr (line );
189+ }
190+ }
191+
192+ // Set the score.
193+ browser .findElement (By .cssSelector ("#assessment" )).sendKeys (String .valueOf (recaptchaScore ));
194+ return new JSONObject ()
195+ .put ("recaptchaScore" , recaptchaScore )
196+ .put ("assessmentName" , assessmentName );
197+ }
198+
199+ public JSONObject initiateBrowserTest (String testURL )
200+ throws IOException , JSONException , InterruptedException {
148201 // Construct the URL to call for validating the assessment.
149- String assessURL = "http://localhost:" + randomServerPort + "/" ;
150202 URI url =
151- UriComponentsBuilder .fromUriString (assessURL )
203+ UriComponentsBuilder .fromUriString (testURL )
152204 .queryParam ("recaptchaSiteKey" , RECAPTCHA_SITE_KEY_1 )
153205 .build ()
154206 .encode ()
@@ -158,7 +210,7 @@ public void testCreateAssessment() throws IOException, JSONException, Interrupte
158210
159211 // Wait until the page is loaded.
160212 JavascriptExecutor js = (JavascriptExecutor ) browser ;
161- new WebDriverWait (browser , 10 )
213+ new WebDriverWait (browser , Duration . ofSeconds ( 10 ) )
162214 .until (webDriver -> js .executeScript ("return document.readyState" ).equals ("complete" ));
163215
164216 // Pass the values to be entered.
@@ -175,21 +227,10 @@ public void testCreateAssessment() throws IOException, JSONException, Interrupte
175227 String token = element .getAttribute ("data-token" );
176228 String action = element .getAttribute ("data-action" );
177229
178- // The obtained token must be further analyzed to get the score.
179- float recaptchaScore = assessToken (token , action );
180-
181- // Set the score.
182- browser .findElement (By .cssSelector ("#assessment" )).sendKeys (String .valueOf (recaptchaScore ));
183- return ;
230+ return new JSONObject ().put ("token" , token ).put ("action" , action );
184231 }
185232
186- public float assessToken (String token , String action ) throws IOException {
187- // Send the token for analysis. The analysis score ranges from 0.0 to 1.0
188- recaptcha .CreateAssessment .createAssessment (PROJECT_ID , RECAPTCHA_SITE_KEY_1 , token , action );
189- String response = stdOut .toString ();
190- assertThat (response ).contains ("The reCAPTCHA score is: " );
191- float recaptchaScore =
192- Float .parseFloat (response .substring (response .lastIndexOf (":" ) + 1 ).trim ());
193- return recaptchaScore ;
233+ public String substr (String line ) {
234+ return line .substring ((line .lastIndexOf (":" ) + 1 )).trim ();
194235 }
195236}
0 commit comments