自建登录验证码
This commit is contained in:
1 parent
149eb66890
commit
564ffecfed
9 files changed
+75
-49
No files matched your search
@@ -0,0 +1,43 @@
|
||||
using CaptchaKit.AspNetCore;
|
||||
using CaptchaKit;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Jiaowu.Api.Tests;
|
||||
|
||||
public sealed class CaptchaKitIntegrationTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Challenge_is_valid_once_and_the_default_generator_returns_a_png()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddDistributedMemoryCache();
|
||||
services.AddCaptchaKit();
|
||||
services.AddSingleton<ICaptchaGenerator, KnownGenerator>();
|
||||
using var provider = services.BuildServiceProvider();
|
||||
var captcha = provider.GetRequiredService<ICaptchaKitService>();
|
||||
|
||||
var challenge = await captcha.CreateAsync();
|
||||
|
||||
Assert.Equal("image/png", challenge.ContentType);
|
||||
Assert.True(challenge.ImageBytes.AsSpan().StartsWith(new byte[] { 137, 80, 78, 71 }));
|
||||
Assert.True(await captcha.VerifyAsync(challenge.Id, "a1b2"));
|
||||
Assert.False(await captcha.VerifyAsync(challenge.Id, "a1b2"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Default_generator_returns_png_bytes()
|
||||
{
|
||||
var challenge = new SkiaTextCaptchaGenerator().Generate(
|
||||
new CaptchaGenerationRequest("test", DateTimeOffset.UtcNow.AddMinutes(1), 5));
|
||||
|
||||
Assert.Equal("image/png", challenge.Challenge.ContentType);
|
||||
Assert.True(challenge.Challenge.ImageBytes.AsSpan().StartsWith(new byte[] { 137, 80, 78, 71 }));
|
||||
}
|
||||
|
||||
private sealed class KnownGenerator : ICaptchaGenerator
|
||||
{
|
||||
public GeneratedCaptcha Generate(CaptchaGenerationRequest request) => new(
|
||||
new CaptchaChallenge(request.Id, "image/png", new byte[] { 137, 80, 78, 71 }, request.ExpiresAt),
|
||||
"A1B2");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user