public async void VerifyAndCheck_ValidSecretKey_ValidToken(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var recaptchaService = CreateService(secretKey);
        var response         = await recaptchaService.VerifyAndCheckAsync(tokenFixture.Token);

        Assert.True(response.Success);
    }
    public async void VerifyAndCheck_InvalidSecretKey(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var recaptchaService = CreateService(secretKey);
        var response         = await recaptchaService.VerifyAndCheckAsync(tokenFixture.Token, tokenFixture.Action);

        Assert.False(response.Success);
    }
Example #3
0
    public async void Verify_ValidSecretKey_InvalidToken(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var assertErrors = tokenFixture.ErrorsItems
                           .Select(e => (Action <VerifyError>)(item => Assert.Equal(e, item)))
                           .ToArray();

        var recaptchaService = CreateService(secretKey);
        var response         = await recaptchaService.VerifyAsync(tokenFixture.Token);

        Assert.False(response.Success);
        Assert.Collection(response.Errors, assertErrors);

        recaptchaService = CreateService();
        response         = await recaptchaService.VerifyAsync(tokenFixture.Token, secretKey);

        Assert.False(response.Success);
        Assert.Collection(response.Errors, assertErrors);
    }
    public async void VerifyAndCheck_ValidSecretKey_InvalidToken_UnknownErrorKey(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var recaptchaService = CreateService(secretKey);
        var response         = await recaptchaService.VerifyAndCheckAsync(tokenFixture.Token);

        Assert.False(response.Response.Success);
        Assert.False(response.Success);
        var e = Assert.Throws <UnknownErrorKeyException>(() => response.Response.Errors);

        Assert.Equal(tokenFixture.Errors?.First(), e.Key);
    }
    public async void VerifyAndCheck_ValidSecretKey_ValidToken_WithAction(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var recaptchaService = CreateService(secretKey);
        var noScoreException = await Assert.ThrowsAsync <MinScoreNotSpecifiedException>(
            () => recaptchaService.VerifyAndCheckAsync(tokenFixture.Token, tokenFixture.Action));

        Assert.Equal(tokenFixture.Action, noScoreException.Action);

        recaptchaService = CreateService(secretKey, null, null, tokenFixture.Action);
        noScoreException = await Assert.ThrowsAsync <MinScoreNotSpecifiedException>(
            () => recaptchaService.VerifyAndCheckAsync(tokenFixture.Token));

        Assert.Equal(tokenFixture.Action, noScoreException.Action);
    }
    public async void VerifyAndCheck_ValidSecretKey_ValidToken_NoAction(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var recaptchaService = CreateService(secretKey);
        await Assert.ThrowsAsync <EmptyActionException>(
            () => recaptchaService.VerifyAndCheckAsync(tokenFixture.Token));

        foreach (var emptyAction in EmptyStrings)
        {
            await Assert.ThrowsAsync <EmptyActionException>(
                () => recaptchaService.VerifyAndCheckAsync(tokenFixture.Token, emptyAction));
        }
    }
    public async void VerifyAndCheck_ValidSecretKey_ValidToken_WithScore_DirectlyOverInOptions(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var recaptchaService = CreateService(secretKey, RecaptchaServiceFixture.Score, RecaptchaServiceFixture.ScoreMap);
        var response         = await recaptchaService.VerifyAndCheckAsync(
            tokenFixture.Token, tokenFixture.Action, RecaptchaServiceFixture.ScoreUnsuccessful);

        Assert.False(response.Success);
    }
    public async void VerifyAndCheck_ValidSecretKey_ValidToken_WithScore_Directly(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var recaptchaService = CreateService(secretKey);
        var response         = await recaptchaService.VerifyAndCheckAsync(
            tokenFixture.Token, tokenFixture.Action, RecaptchaServiceFixture.Score);

        var success = tokenFixture.Score.HasValue && tokenFixture.Score.Value >= RecaptchaServiceFixture.Score;

        Assert.Equal(success, response.Success);
    }
    public async void VerifyAndCheck_ValidSecretKey_ValidToken_WithScoreAndScoreMap(string secretKey, ResponseTokenFixture tokenFixture)
    {
        var recaptchaService = CreateService(secretKey, RecaptchaServiceFixture.Score, RecaptchaServiceFixture.ScoreMap);
        var response         = await recaptchaService.VerifyAndCheckAsync(tokenFixture.Token, tokenFixture.Action);

        var success = tokenFixture.Score.HasValue &&
                      (RecaptchaServiceFixture.ScoreMap.TryGetValue(tokenFixture.Action !, out var score) ?
                       tokenFixture.Score.Value >= score :
                       tokenFixture.Score.Value >= RecaptchaServiceFixture.Score);

        Assert.Equal(success, response.Success);
    }