Search in sources :

Example 1 with Assessment

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.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) Assessment(com.google.recaptchaenterprise.v1.Assessment) AccountDefenderAssessment(com.google.recaptchaenterprise.v1.AccountDefenderAssessment) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 2 with Assessment

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()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Assessment(com.google.recaptchaenterprise.v1beta1.Assessment) ArrayList(java.util.ArrayList) CreateAssessmentRequest(com.google.recaptchaenterprise.v1beta1.CreateAssessmentRequest) Test(org.junit.Test)

Example 3 with Assessment

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()));
}
Also used : AnnotateAssessmentRequest(com.google.recaptchaenterprise.v1beta1.AnnotateAssessmentRequest) AbstractMessage(com.google.protobuf.AbstractMessage) AnnotateAssessmentResponse(com.google.recaptchaenterprise.v1beta1.AnnotateAssessmentResponse) AssessmentName(com.google.recaptchaenterprise.v1beta1.AssessmentName) Test(org.junit.Test)

Example 4 with Assessment

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.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) ProjectName(com.google.recaptchaenterprise.v1.ProjectName) Assessment(com.google.recaptchaenterprise.v1.Assessment) AccountDefenderAssessment(com.google.recaptchaenterprise.v1.AccountDefenderAssessment) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 5 with Assessment

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;
    }
}
Also used : DistributionSummary(io.micrometer.core.instrument.DistributionSummary) Assessment(com.google.recaptchaenterprise.v1.Assessment) Event(com.google.recaptchaenterprise.v1.Event)

Aggregations

Test (org.junit.Test)10 Assessment (com.google.recaptchaenterprise.v1beta1.Assessment)8 Assessment (com.google.recaptchaenterprise.v1.Assessment)6 CreateAssessmentRequest (com.google.recaptchaenterprise.v1beta1.CreateAssessmentRequest)6 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)5 AbstractMessage (com.google.protobuf.AbstractMessage)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 AccountDefenderAssessment (com.google.recaptchaenterprise.v1.AccountDefenderAssessment)4 CreateAssessmentRequest (com.google.recaptchaenterprise.v1.CreateAssessmentRequest)3 ProjectName (com.google.recaptchaenterprise.v1beta1.ProjectName)3 Test (org.junit.jupiter.api.Test)3 ByteString (com.google.protobuf.ByteString)2 Event (com.google.recaptchaenterprise.v1.Event)2 ProjectName (com.google.recaptchaenterprise.v1.ProjectName)2 AnnotateAssessmentRequest (com.google.recaptchaenterprise.v1beta1.AnnotateAssessmentRequest)2 AssessmentName (com.google.recaptchaenterprise.v1beta1.AssessmentName)2 ArrayList (java.util.ArrayList)2 RecaptchaEnterpriseServiceClient (com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)1 RecaptchaEnterpriseServiceV1Beta1Client (com.google.cloud.recaptchaenterprise.v1beta1.RecaptchaEnterpriseServiceV1Beta1Client)1 ClassificationReason (com.google.recaptchaenterprise.v1.RiskAnalysis.ClassificationReason)1