Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 76 additions & 6 deletions CapMonsterCloud.Client.IntegrationTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ public async Task RecaptchaV2_ShouldSolve()
status = "ready",
solution = new
{
gRecaptchaResponse = expectedResult.Solution.Value
gRecaptchaResponse = expectedResult.Solution.Value,
userAgent = expectedResult.Solution.UserAgent,
cookies = expectedResult.Solution.Cookies
},
errorId = 0,
errorCode = (string)null!
Expand Down Expand Up @@ -206,7 +208,8 @@ public async Task RecaptchaV3Proxyless_ShouldSolve()
status = "ready",
solution = new
{
gRecaptchaResponse = expectedResult.Solution.Value
gRecaptchaResponse = expectedResult.Solution.Value,
userAgent = expectedResult.Solution.UserAgent
},
errorId = 0,
errorCode = (string)null!
Expand All @@ -222,6 +225,69 @@ public async Task RecaptchaV3Proxyless_ShouldSolve()
actual.Should().BeEquivalentTo(expectedResult);
}

[Test]
public async Task RecaptchaV3Enterprise_ShouldSolve()
{
var clientKey = Gen.RandomString();
var taskId = Gen.RandomInt();

var captchaRequest = ObjectGen.RecaptchaV3Enterprise.CreateTask();
var expectedResult = ObjectGen.RecaptchaV3Enterprise.CreateSolution();

var expectedRequests = new List<(RequestType Type, string ExpectedRequest)>
{
(
Type: RequestType.CreateTask,
ExpectedRequest: JsonConvert.SerializeObject(new
{ clientKey = clientKey, task = captchaRequest, softId = 53 })
),
(
Type: RequestType.GetTaskResult,
ExpectedRequest: JsonConvert.SerializeObject(new { clientKey = clientKey, taskId = taskId })
),
};

var captchaResults = new List<object>
{
new { taskId = taskId, errorId = 0, errorCode = (string)null! },
new
{
status = "ready",
solution = new
{
gRecaptchaResponse = expectedResult.Solution.Value,
userAgent = expectedResult.Solution.UserAgent
},
errorId = 0,
errorCode = (string)null!
}
};

var sut = new Sut(clientKey);
sut.SetupHttpServer(captchaResults);

var actual = await sut.SolveAsync(captchaRequest);

sut.GetActualRequests().Should().BeEquivalentTo(expectedRequests);
actual.Should().BeEquivalentTo(expectedResult);
}

[Test]
public async Task RecaptchaV3Enterprise_IncorrectMinScore_ShouldThrowValidationException()
{
var clientKey = Gen.RandomString();
var captchaRequest = ObjectGen.RecaptchaV3Enterprise.CreateTask(
minScore: Gen.RandomBool() ? Gen.RandomDouble(0, 0.09) : Gen.RandomDouble(0.91, 1.5));

var sut = new Sut(clientKey);
sut.SetupHttpServer(new List<object>());

Func<Task> actual = () => sut.SolveAsync(captchaRequest);

_ = await actual.Should().ThrowAsync<System.ComponentModel.DataAnnotations.ValidationException>()
.WithMessage("*The field MinScore must be between 0?1 and 0?9*");
}

[Test]
public async Task FunCaptcha_ShouldSolve()
{
Expand Down Expand Up @@ -252,7 +318,8 @@ public async Task FunCaptcha_ShouldSolve()
status = "ready",
solution = new
{
token = expectedResult.Solution.Value
token = expectedResult.Solution.Value,
userAgent = expectedResult.Solution.UserAgent
},
errorId = 0,
errorCode = (string)null!
Expand Down Expand Up @@ -400,7 +467,8 @@ public async Task Turnstile_ShouldSolve()
solution = new
{
token = expectedResult.Solution.Value,
cf_clearance = expectedResult.Solution.Clearance
cf_clearance = expectedResult.Solution.Clearance,
userAgent = expectedResult.Solution.UserAgent
},
errorId = 0,
errorCode = (string)null!
Expand Down Expand Up @@ -541,7 +609,8 @@ public async Task RecaptchaComplexImageTask_ShouldSolve()
status = "ready",
solution = new
{
answer = expectedResult.Solution.Answer
answer = expectedResult.Solution.Answer,
metadata = new { AnswerType = expectedResult.Solution.Metadata.AnswerType }
},
errorId = 0,
errorCode = (string)null!
Expand Down Expand Up @@ -681,7 +750,8 @@ public async Task FunCaptchaComplexImageTask_ShouldSolve()
status = "ready",
solution = new
{
answer = expectedResult.Solution.Answer
answer = expectedResult.Solution.Answer,
metadata = new { AnswerType = expectedResult.Solution.Metadata.AnswerType }
},
errorId = 0,
errorCode = (string)null!
Expand Down
57 changes: 53 additions & 4 deletions CapMonsterCloud.Client.IntegrationTests/ObjectGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static RecaptchaV2Request CreateTask(
UserAgent = Gen.UserAgent(),
Cookies = Gen.ListOfValues(Gen.RandomString).ToDictionary(_ => Gen.RandomString(), value => value),
NoCache = Gen.RandomBool(),
IsInvisible = Gen.RandomBool(),
Proxy = new ProxyContainer(Gen.RandomString(), proxyPort ?? Gen.RandomInt(0, 65535), Gen.RandomEnum<ProxyType>(), Gen.RandomString(), Gen.RandomString())
};
}
Expand All @@ -64,6 +65,8 @@ public static CaptchaResult<RecaptchaV2Response> CreateSolution()
Solution = new RecaptchaV2Response
{
Value = Gen.RandomString(),
UserAgent = Gen.UserAgent(),
Cookies = new Dictionary<string, string> { { Gen.RandomString(), Gen.RandomString() } }
}
};
}
Expand All @@ -80,7 +83,8 @@ public static RecaptchaV3ProxylessRequest CreateTask(
WebsiteKey = Gen.RandomGuid(),
MinScore = minScore ?? Gen.RandomDouble(0.1, 0.9),
PageAction = Gen.RandomString(),
NoCache = Gen.RandomBool()
NoCache = Gen.RandomBool(),
IsEnterprise = Gen.RandomBool()
};
}

Expand All @@ -92,11 +96,41 @@ public static CaptchaResult<RecaptchaV3Response> CreateSolution()
Solution = new RecaptchaV3Response
{
Value = Gen.RandomString(),
UserAgent = Gen.UserAgent()
}
};
}
}

public static class RecaptchaV3Enterprise
{
public static RecaptchaV3EnterpriseRequest CreateTask(
double? minScore = null)
{
return new RecaptchaV3EnterpriseRequest
{
WebsiteUrl = Gen.RandomUri().ToString(),
WebsiteKey = Gen.RandomGuid(),
MinScore = minScore ?? Gen.RandomDouble(0.1, 0.9),
PageAction = Gen.RandomString(),
NoCache = Gen.RandomBool()
};
}

public static CaptchaResult<RecaptchaV3Response> CreateSolution()
{
return new CaptchaResult<RecaptchaV3Response>
{
Error = null,
Solution = new RecaptchaV3Response
{
Value = Gen.RandomString(),
UserAgent = Gen.UserAgent()
}
};
}
}

public static class FunCaptcha
{
public static FunCaptchaRequest CreateTask()
Expand All @@ -122,6 +156,7 @@ public static CaptchaResult<FunCaptchaResponse> CreateSolution()
Solution = new FunCaptchaResponse
{
Value = Gen.RandomString(),
UserAgent = Gen.UserAgent()
}
};
}
Expand Down Expand Up @@ -211,6 +246,10 @@ public static RecaptchaV2EnterpriseRequest CreateTask()
WebsiteKey = Gen.RandomString(),
EnterprisePayload = Gen.RandomString(),
DataSValue = Gen.RandomString(),
ApiDomain = Gen.RandomString(),
PageAction = Gen.RandomString(),
Cookies = Gen.ListOfValues(Gen.RandomString).ToDictionary(_ => Gen.RandomString(), value => value),
UserAgent = Gen.UserAgent(),
Proxy = new ProxyContainer(Gen.RandomString(), Gen.RandomInt(0, 65535), Gen.RandomEnum<ProxyType>(), Gen.RandomString(), Gen.RandomString())
};
}
Expand Down Expand Up @@ -243,6 +282,7 @@ public static TurnstileRequest CreateTask()
HtmlPageBase64 = Gen.RandomString(),
UserAgent = Gen.UserAgent(),
ApiJsUrl = Gen.RandomUri().ToString(),
Action = Gen.RandomString(),
NoCache = Gen.RandomBool(),
Proxy = new ProxyContainer(Gen.RandomString(), Gen.RandomInt(0, 65535), Gen.RandomEnum<ProxyType>(), Gen.RandomString(), Gen.RandomString())
};
Expand All @@ -256,7 +296,8 @@ public static CaptchaResult<TurnstileResponse> CreateSolution()
Solution = new TurnstileResponse
{
Value = Gen.RandomString(),
Clearance = Gen.RandomString()
Clearance = Gen.RandomString(),
UserAgent = Gen.UserAgent()
}
};
}
Expand All @@ -277,6 +318,8 @@ public static RecaptchaComplexImageTaskRequest CreateRecaptchaComplexImageTask()
},
ImageUrls = Gen.ListOfValues(Gen.RandomUri().ToString),
ImagesBase64 = Gen.ListOfValues(Gen.RandomString),
ExampleImageUrls = Gen.ListOfValues(Gen.RandomUri().ToString),
ExampleImagesBase64 = Gen.ListOfValues(Gen.RandomString),
UserAgent = Gen.UserAgent()
};
}
Expand Down Expand Up @@ -322,10 +365,13 @@ public static RecognitionComplexImageTaskRequest CreateRecognitionComplexImageTa
Metadata = new RecognitionComplexImageTaskRequest.RecognitionMetadata
{
Task = Gen.RandomString(),
TaskArgument = Gen.RandomString()
TaskArgument = Gen.RandomString(),
PayloadType = Gen.RandomString()
},
ImageUrls = Gen.ListOfValues(Gen.RandomUri().ToString),
ImagesBase64 = Gen.ListOfValues(Gen.RandomString),
ExampleImageUrls = Gen.ListOfValues(Gen.RandomUri().ToString),
ExampleImagesBase64 = Gen.ListOfValues(Gen.RandomString),
UserAgent = Gen.UserAgent()
};
}
Expand All @@ -338,6 +384,7 @@ public static CaptchaResult<GridComplexImageTaskResponse> CreateGridComplexImage
Solution = new GridComplexImageTaskResponse
{
Answer = Gen.ListOfValues(Gen.RandomBool),
Metadata = new DynamicComplexImageTaskResponse.RecognitionMetadata { AnswerType = "Grid" }
}
};
}
Expand Down Expand Up @@ -374,7 +421,8 @@ public static class AlibabaTask
public static AlibabaCustomTaskRequest CreateTask()
{
return new AlibabaCustomTaskRequest(Gen.RandomString(), Gen.RandomString(),
Gen.RandomGuid(), Gen.RandomGuid(), Gen.RandomString(), Gen.RandomString(), Gen.RandomGuid(), Gen.RandomString())
Gen.RandomGuid(), Gen.RandomGuid(), Gen.RandomString(), Gen.RandomString(), Gen.RandomGuid(), Gen.RandomString(),
Gen.RandomUri().ToString(), Gen.RandomBool())
{
WebsiteUrl = Gen.RandomUri().ToString(),
UserAgent = Gen.UserAgent(),
Expand Down Expand Up @@ -838,6 +886,7 @@ public static ProsopoTaskRequest CreateTask()
{
WebsiteUrl = Gen.RandomUri().ToString(),
WebsiteKey = Gen.RandomString(),
UserAgent = Gen.UserAgent(),
Proxy = new ProxyContainer(Gen.RandomString(), Gen.RandomInt(0, 65535), Gen.RandomEnum<ProxyType>(), Gen.RandomString(), Gen.RandomString())
};
}
Expand Down
3 changes: 3 additions & 0 deletions CapMonsterCloud.Client.IntegrationTests/Sut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public async Task<CaptchaResult<RecaptchaV2Response>> SolveAsync (

public async Task<CaptchaResult<RecaptchaV3Response>> SolveAsync (
RecaptchaV3ProxylessRequest request) => await _cloudClient.SolveAsync(request);

public async Task<CaptchaResult<RecaptchaV3Response>> SolveAsync (
RecaptchaV3EnterpriseRequest request) => await _cloudClient.SolveAsync(request);

public async Task<CaptchaResult<FunCaptchaResponse>> SolveAsync (
FunCaptchaRequest request) => await _cloudClient.SolveAsync(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="FluentAssertions" Version="8.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.8.1" />
<PackageReference Include="NUnit" Version="4.6.1" />
<PackageReference Include="NUnit3TestAdapter" Version="6.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading