1- using EssentialCSharp . Web . Extensions ;
2- using EssentialCSharp . Web . Models ;
3- using EssentialCSharp . Web . Services ;
4- using Microsoft . Extensions . Configuration ;
1+ using EssentialCSharp . Web . Extensions ;
2+ using EssentialCSharp . Web . Models ;
3+ using EssentialCSharp . Web . Services ;
4+ using Microsoft . Extensions . Configuration ;
55using Microsoft . Extensions . DependencyInjection ;
66
7- namespace EssentialCSharp . Web . Tests . Integration ;
8-
9- [ ClassDataSource < CaptchaServiceProvider > ( Shared = SharedType . PerClass ) ]
10- public class CaptchaTests ( CaptchaServiceProvider serviceProvider )
11- {
12- [ Test ]
13- [ Timeout ( 30_000 ) ]
14- public async Task CaptchaService_Verify_Success ( CancellationToken cancellationToken )
15- {
16- ICaptchaService captchaService = serviceProvider . ServiceProvider . GetRequiredService < ICaptchaService > ( ) ;
17-
18- // From https://docs.hcaptcha.com/#integration-testing-test-keys
19- string hCaptchaSecret = "0x0000000000000000000000000000000000000000" ;
20- string hCaptchaToken = "10000000-aaaa-bbbb-cccc-000000000001" ;
21- string hCaptchaSiteKey = "10000000-ffff-ffff-ffff-000000000001" ;
22- HCaptchaResult ? response = await captchaService . VerifyAsync ( hCaptchaSecret , hCaptchaToken , hCaptchaSiteKey , cancellationToken ) ;
23-
24- await Assert . That ( response ) . IsNotNull ( ) ;
25- await Assert . That ( response . Success ) . IsTrue ( ) ;
26- }
27- }
28-
29- public class CaptchaServiceProvider : IDisposable , IAsyncDisposable
30- {
31- public ServiceProvider ServiceProvider { get ; } = CreateServiceProvider ( ) ;
32- public static ServiceProvider CreateServiceProvider ( )
33- {
34- IServiceCollection services = new ServiceCollection ( ) ;
35-
36- IConfigurationRoot configuration = new ConfigurationBuilder ( )
37- . SetBasePath ( IntelliTect . Multitool . RepositoryPaths . GetDefaultRepoRoot ( ) )
38- . AddJsonFile ( $ "{ nameof ( EssentialCSharp ) } .{ nameof ( Web ) } /appsettings.json")
39- . Build ( ) ;
40- services . AddCaptchaService ( configuration . GetSection ( CaptchaOptions . CaptchaSender ) ) ;
41- // Add other necessary services here
42-
43- return services . BuildServiceProvider ( ) ;
44- }
45- public void Dispose ( )
46- {
47- ServiceProvider . Dispose ( ) ;
48- GC . SuppressFinalize ( this ) ;
49- }
50-
51- public async ValueTask DisposeAsync ( )
52- {
53- await ServiceProvider . DisposeAsync ( ) ;
54- GC . SuppressFinalize ( this ) ;
55- }
7+ namespace EssentialCSharp . Web . Tests . Integration ;
8+
9+ [ ClassDataSource < CaptchaServiceProvider > ( Shared = SharedType . PerClass ) ]
10+ public class CaptchaTests ( CaptchaServiceProvider serviceProvider )
11+ {
12+ [ Test ]
13+ public async Task CaptchaService_Verify_Success ( CancellationToken cancellationToken )
14+ {
15+ ICaptchaService captchaService = serviceProvider . ServiceProvider . GetRequiredService < ICaptchaService > ( ) ;
16+
17+ // From https://docs.hcaptcha.com/#integration-testing-test-keys
18+ string hCaptchaSecret = "0x0000000000000000000000000000000000000000" ;
19+ string hCaptchaToken = "10000000-aaaa-bbbb-cccc-000000000001" ;
20+ string hCaptchaSiteKey = "10000000-ffff-ffff-ffff-000000000001" ;
21+ HCaptchaResult ? response = await captchaService . VerifyAsync ( hCaptchaSecret , hCaptchaToken , hCaptchaSiteKey , cancellationToken ) ;
22+
23+ await Assert . That ( response ) . IsNotNull ( ) ;
24+ await Assert . That ( response . Success ) . IsTrue ( ) ;
25+ }
26+ }
27+
28+ public class CaptchaServiceProvider : IDisposable , IAsyncDisposable
29+ {
30+ public ServiceProvider ServiceProvider { get ; } = CreateServiceProvider ( ) ;
31+ public static ServiceProvider CreateServiceProvider ( )
32+ {
33+ IServiceCollection services = new ServiceCollection ( ) ;
34+
35+ IConfigurationRoot configuration = new ConfigurationBuilder ( )
36+ . SetBasePath ( IntelliTect . Multitool . RepositoryPaths . GetDefaultRepoRoot ( ) )
37+ . AddJsonFile ( $ "{ nameof ( EssentialCSharp ) } .{ nameof ( Web ) } /appsettings.json")
38+ . Build ( ) ;
39+ services . AddCaptchaService ( configuration . GetSection ( CaptchaOptions . CaptchaSender ) ) ;
40+ // Add other necessary services here
41+
42+ return services . BuildServiceProvider ( ) ;
43+ }
44+ public void Dispose ( )
45+ {
46+ ServiceProvider . Dispose ( ) ;
47+ GC . SuppressFinalize ( this ) ;
48+ }
49+
50+ public async ValueTask DisposeAsync ( )
51+ {
52+ await ServiceProvider . DisposeAsync ( ) ;
53+ GC . SuppressFinalize ( this ) ;
54+ }
5655}
0 commit comments