SignInResultTest.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System.Security.Claims;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Authentication;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Logging.Abstractions;
  9. using Moq;
  10. using Xunit;
  11. namespace Microsoft.AspNetCore.Http.Result
  12. {
  13. public class SignInResultTest
  14. {
  15. [Fact]
  16. public async Task ExecuteAsync_InvokesSignInAsyncOnAuthenticationManager()
  17. {
  18. // Arrange
  19. var principal = new ClaimsPrincipal();
  20. var auth = new Mock<IAuthenticationService>();
  21. auth
  22. .Setup(c => c.SignInAsync(It.IsAny<HttpContext>(), "", principal, null))
  23. .Returns(Task.CompletedTask)
  24. .Verifiable();
  25. var httpContext = GetHttpContext(auth.Object);
  26. var result = new SignInResult("", principal, null);
  27. // Act
  28. await result.ExecuteAsync(httpContext);
  29. // Assert
  30. auth.Verify();
  31. }
  32. [Fact]
  33. public async Task ExecuteAsync_InvokesSignInAsyncOnAuthenticationManagerWithDefaultScheme()
  34. {
  35. // Arrange
  36. var principal = new ClaimsPrincipal();
  37. var auth = new Mock<IAuthenticationService>();
  38. auth
  39. .Setup(c => c.SignInAsync(It.IsAny<HttpContext>(), null, principal, null))
  40. .Returns(Task.CompletedTask)
  41. .Verifiable();
  42. var httpContext = GetHttpContext(auth.Object);
  43. var result = new SignInResult(principal);
  44. // Act
  45. await result.ExecuteAsync(httpContext);
  46. // Assert
  47. auth.Verify();
  48. }
  49. [Fact]
  50. public async Task ExecuteAsync_InvokesSignInAsyncOnConfiguredScheme()
  51. {
  52. // Arrange
  53. var principal = new ClaimsPrincipal();
  54. var authProperties = new AuthenticationProperties();
  55. var auth = new Mock<IAuthenticationService>();
  56. auth
  57. .Setup(c => c.SignInAsync(It.IsAny<HttpContext>(), "Scheme1", principal, authProperties))
  58. .Returns(Task.CompletedTask)
  59. .Verifiable();
  60. var httpContext = GetHttpContext(auth.Object);
  61. var result = new SignInResult("Scheme1", principal, authProperties);
  62. // Act
  63. await result.ExecuteAsync(httpContext);
  64. // Assert
  65. auth.Verify();
  66. }
  67. private static DefaultHttpContext GetHttpContext(IAuthenticationService auth)
  68. {
  69. var httpContext = new DefaultHttpContext();
  70. httpContext.RequestServices = CreateServices()
  71. .AddSingleton(auth)
  72. .BuildServiceProvider();
  73. return httpContext;
  74. }
  75. private static IServiceCollection CreateServices()
  76. {
  77. var services = new ServiceCollection();
  78. services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
  79. return services;
  80. }
  81. }
  82. }