use of com.google.recaptchaenterprise.v1beta1.Assessment in project java-recaptchaenterprise by googleapis.
the class RecaptchaEnterpriseServiceClientTest method createAssessmentExceptionTest2.
@Test
public void createAssessmentExceptionTest2() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockRecaptchaEnterpriseService.addException(exception);
try {
String parent = "parent-995424086";
Assessment assessment = Assessment.newBuilder().build();
client.createAssessment(parent, assessment);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.recaptchaenterprise.v1beta1.Assessment in project java-recaptchaenterprise by googleapis.
the class RecaptchaEnterpriseServiceV1Beta1ClientTest method createAssessmentTest2.
@Test
public void createAssessmentTest2() throws Exception {
Assessment expectedResponse = Assessment.newBuilder().setName(AssessmentName.of("[PROJECT]", "[ASSESSMENT]").toString()).setEvent(Event.newBuilder().build()).setScore(109264530).setTokenProperties(TokenProperties.newBuilder().build()).addAllReasons(new ArrayList<Assessment.ClassificationReason>()).build();
mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse);
String parent = "parent-995424086";
Assessment assessment = Assessment.newBuilder().build();
Assessment actualResponse = client.createAssessment(parent, assessment);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateAssessmentRequest actualRequest = ((CreateAssessmentRequest) actualRequests.get(0));
Assert.assertEquals(parent, actualRequest.getParent());
Assert.assertEquals(assessment, actualRequest.getAssessment());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.recaptchaenterprise.v1beta1.Assessment in project java-recaptchaenterprise by googleapis.
the class RecaptchaEnterpriseServiceV1Beta1ClientTest method annotateAssessmentTest.
@Test
public void annotateAssessmentTest() throws Exception {
AnnotateAssessmentResponse expectedResponse = AnnotateAssessmentResponse.newBuilder().build();
mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse);
AssessmentName name = AssessmentName.of("[PROJECT]", "[ASSESSMENT]");
AnnotateAssessmentRequest.Annotation annotation = AnnotateAssessmentRequest.Annotation.forNumber(0);
AnnotateAssessmentResponse actualResponse = client.annotateAssessment(name, annotation);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests();
Assert.assertEquals(1, actualRequests.size());
AnnotateAssessmentRequest actualRequest = ((AnnotateAssessmentRequest) actualRequests.get(0));
Assert.assertEquals(name.toString(), actualRequest.getName());
Assert.assertEquals(annotation, actualRequest.getAnnotation());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.recaptchaenterprise.v1beta1.Assessment in project java-recaptchaenterprise by googleapis.
the class RecaptchaEnterpriseServiceClientTest method createAssessmentExceptionTest.
@Test
public void createAssessmentExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockRecaptchaEnterpriseService.addException(exception);
try {
ProjectName parent = ProjectName.of("[PROJECT]");
Assessment assessment = Assessment.newBuilder().build();
client.createAssessment(parent, assessment);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.recaptchaenterprise.v1beta1.Assessment in project Signal-Server by signalapp.
the class RecaptchaClient method verify.
public boolean verify(final String input, final String ip) {
final String[] parts = parseInputToken(input);
final String sitekey = parts[0];
final String expectedAction = parts[1];
final String token = parts[2];
Event.Builder eventBuilder = Event.newBuilder().setSiteKey(sitekey).setToken(token).setUserIpAddress(ip);
if (expectedAction != null) {
eventBuilder.setExpectedAction(expectedAction);
}
final Event event = eventBuilder.build();
final Assessment assessment = client.createAssessment(projectPath, Assessment.newBuilder().setEvent(event).build());
Metrics.counter(ASSESSMENTS_COUNTER_NAME, "action", String.valueOf(expectedAction), "valid", String.valueOf(assessment.getTokenProperties().getValid())).increment();
if (assessment.getTokenProperties().getValid()) {
final float score = assessment.getRiskAnalysis().getScore();
final DistributionSummary.Builder distributionSummaryBuilder = DistributionSummary.builder(SCORE_DISTRIBUTION_NAME).scale(100).maximumExpectedValue(100.0d).tags("action", String.valueOf(expectedAction));
distributionSummaryBuilder.register(Metrics.globalRegistry).record(score);
return score >= dynamicConfigurationManager.getConfiguration().getCaptchaConfiguration().getScoreFloor().floatValue();
} else {
return false;
}
}
Aggregations