24
24
import java .io .IOException ;
25
25
import java .io .PrintStream ;
26
26
import java .net .URI ;
27
+ import java .time .Duration ;
27
28
import java .util .concurrent .ExecutionException ;
28
29
import java .util .concurrent .TimeUnit ;
29
30
import java .util .concurrent .TimeoutException ;
30
31
import org .json .JSONException ;
32
+ import org .json .JSONObject ;
31
33
import org .junit .After ;
32
34
import org .junit .AfterClass ;
33
35
import org .junit .Assert ;
47
49
import org .springframework .boot .web .server .LocalServerPort ;
48
50
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
49
51
import org .springframework .web .util .UriComponentsBuilder ;
52
+ import recaptcha .AnnotateAssessment ;
50
53
51
54
@ RunWith (SpringJUnit4ClassRunner .class )
52
55
@ EnableAutoConfiguration
@@ -55,6 +58,7 @@ public class SnippetsIT {
55
58
56
59
private static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
57
60
private static final String DOMAIN_NAME = "localhost" ;
61
+ private static String ASSESSMENT_NAME = "" ;
58
62
private static String RECAPTCHA_SITE_KEY_1 = "recaptcha-site-key1" ;
59
63
private static String RECAPTCHA_SITE_KEY_2 = "recaptcha-site-key2" ;
60
64
private static WebDriver browser ;
@@ -69,7 +73,7 @@ public static void requireEnvVar(String envVarName) {
69
73
}
70
74
71
75
@ BeforeClass
72
- public static void setUp () throws IOException , InterruptedException {
76
+ public static void setUp () throws IOException , InterruptedException , JSONException {
73
77
requireEnvVar ("GOOGLE_APPLICATION_CREDENTIALS" );
74
78
requireEnvVar ("GOOGLE_CLOUD_PROJECT" );
75
79
@@ -144,11 +148,59 @@ public void testDeleteSiteKey()
144
148
}
145
149
146
150
@ 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 {
148
201
// Construct the URL to call for validating the assessment.
149
- String assessURL = "http://localhost:" + randomServerPort + "/" ;
150
202
URI url =
151
- UriComponentsBuilder .fromUriString (assessURL )
203
+ UriComponentsBuilder .fromUriString (testURL )
152
204
.queryParam ("recaptchaSiteKey" , RECAPTCHA_SITE_KEY_1 )
153
205
.build ()
154
206
.encode ()
@@ -158,7 +210,7 @@ public void testCreateAssessment() throws IOException, JSONException, Interrupte
158
210
159
211
// Wait until the page is loaded.
160
212
JavascriptExecutor js = (JavascriptExecutor ) browser ;
161
- new WebDriverWait (browser , 10 )
213
+ new WebDriverWait (browser , Duration . ofSeconds ( 10 ) )
162
214
.until (webDriver -> js .executeScript ("return document.readyState" ).equals ("complete" ));
163
215
164
216
// Pass the values to be entered.
@@ -175,21 +227,10 @@ public void testCreateAssessment() throws IOException, JSONException, Interrupte
175
227
String token = element .getAttribute ("data-token" );
176
228
String action = element .getAttribute ("data-action" );
177
229
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 );
184
231
}
185
232
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 ();
194
235
}
195
236
}
0 commit comments