Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

64 строки
2.4KB

  1. using System.Net;
  2. using System.Text.Json;
  3. using Campaign_Tracker.Server.Authentication;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.Extensions.Options;
  6. namespace Campaign_Tracker.Server.Tests;
  7. public sealed class KeycloakTokenClientTests
  8. {
  9. [Fact]
  10. public async Task ExchangeAuthorizationCodeAsync_PostsClientSecretToKeycloakTokenEndpoint()
  11. {
  12. using var handler = new CapturingMessageHandler();
  13. using var httpClient = new HttpClient(handler);
  14. var client = new KeycloakTokenClient(
  15. httpClient,
  16. Options.Create(new KeycloakOptions
  17. {
  18. Authority = "http://localhost:8180/realms/KCI",
  19. PublicAuthority = "http://localhost:8180/realms/KCI",
  20. ClientId = "canopy-web",
  21. ClientSecret = "secret-from-env",
  22. }),
  23. NullLogger<KeycloakTokenClient>.Instance);
  24. var tokens = await client.ExchangeAuthorizationCodeAsync(
  25. "auth-code",
  26. "http://kci-app01.ntp.kentcommunications.com/auth/callback",
  27. CancellationToken.None);
  28. Assert.Equal("http://localhost:8180/realms/KCI/protocol/openid-connect/token", handler.RequestUri);
  29. Assert.Equal("access-token", tokens.AccessToken);
  30. Assert.Contains("client_id=canopy-web", handler.FormBody);
  31. Assert.Contains("client_secret=secret-from-env", handler.FormBody);
  32. Assert.Contains("code=auth-code", handler.FormBody);
  33. Assert.Contains("grant_type=authorization_code", handler.FormBody);
  34. }
  35. private sealed class CapturingMessageHandler : HttpMessageHandler, IDisposable
  36. {
  37. public string RequestUri { get; private set; } = string.Empty;
  38. public string FormBody { get; private set; } = string.Empty;
  39. protected override async Task<HttpResponseMessage> SendAsync(
  40. HttpRequestMessage request,
  41. CancellationToken cancellationToken)
  42. {
  43. RequestUri = request.RequestUri?.ToString() ?? string.Empty;
  44. FormBody = await request.Content!.ReadAsStringAsync(cancellationToken);
  45. return new HttpResponseMessage(HttpStatusCode.OK)
  46. {
  47. Content = new StringContent(JsonSerializer.Serialize(new
  48. {
  49. access_token = "access-token",
  50. refresh_token = "refresh-token",
  51. expires_in = 300,
  52. })),
  53. };
  54. }
  55. }
  56. }

Powered by TurnKey Linux.