Created
March 15, 2019 16:21
-
-
Save Stayrony/b8c3b89c6684dfd2494d3677040ca870 to your computer and use it in GitHub Desktop.
Unit testing ASP.NET Core Identity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CollectionDefinition("Test")] | |
| public class AccountServiceTests | |
| { | |
| private readonly InjectFixture _injectFixture; | |
| public AccountServiceTests() | |
| { | |
| _injectFixture = new InjectFixture(); | |
| } | |
| [Theory] | |
| [InlineData("test@test.it", "Bred", "Apollo", 90)] | |
| public async Task ConfirmEmail_ShouldThrowException_IfUserIdIsNullAsync(string email, string firstName, | |
| string lastName, int passwordExpireInDays) | |
| { | |
| var user = new ApplicationUser() | |
| { | |
| Email = email, | |
| FirstName = firstName, | |
| LastName = lastName, | |
| PasswordExpiredDate = _injectFixture.Generator.UtcNow().AddDays(passwordExpireInDays), | |
| }; | |
| Task Act() => _injectFixture.AccountService.ConfirmEmailAsync(null, user.Email, "test"); | |
| await Assert.ThrowsAsync<ArgumentNullException>(Act); | |
| Assert.Contains("userId", Act().Exception.Message); | |
| } | |
| [Theory] | |
| [InlineData("test@test.it", "Bred", "Apollo", 90)] | |
| public async Task ConfirmEmail_ShouldThrowException_IfCodeIsNullAsync(string email, string firstName, | |
| string lastName, int passwordExpireInDays) | |
| { | |
| var user = new ApplicationUser() | |
| { | |
| Email = email, | |
| FirstName = firstName, | |
| LastName = lastName, | |
| PasswordExpiredDate = _injectFixture.Generator.UtcNow().AddDays(passwordExpireInDays), | |
| }; | |
| Task Act() => _injectFixture.AccountService.ConfirmEmailAsync(user.Id, user.Email, null); | |
| await Assert.ThrowsAsync<ArgumentNullException>(Act); | |
| Assert.Contains("code", Act().Exception.Message); | |
| } | |
| [Theory] | |
| [InlineData("test@test.it", "Bred", "Apollo", 90, "wertyuiolkjmnbvcfdew3456789")] | |
| public async Task ConfirmEmail_ShouldThrowException_IfUserIsNullAsync(string email, string firstName, | |
| string lastName, int passwordExpireInDays, string code) | |
| { | |
| var user = new ApplicationUser() | |
| { | |
| Email = email, | |
| FirstName = firstName, | |
| LastName = lastName, | |
| PasswordExpiredDate = _injectFixture.Generator.UtcNow().AddDays(passwordExpireInDays), | |
| }; | |
| Task Act() => _injectFixture.AccountService.ConfirmEmailAsync(user.Id, user.Email, code); | |
| await Assert.ThrowsAsync<ArgumentNullException>(Act); | |
| Assert.Contains("user", Act().Exception.Message); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class FakeSignInManager : SignInManager<ApplicationUser> | |
| { | |
| public FakeSignInManager() | |
| : base(new Mock<FakeUserManager>().Object, | |
| new HttpContextAccessor(), | |
| new Mock<IUserClaimsPrincipalFactory<ApplicationUser>>().Object, | |
| new Mock<IOptions<IdentityOptions>>().Object, | |
| new Mock<ILogger<SignInManager<ApplicationUser>>>().Object, | |
| new Mock<IAuthenticationSchemeProvider>().Object) | |
| { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class FakeUserManager : UserManager<ApplicationUser> | |
| { | |
| public FakeUserManager() | |
| : base(new Mock<IUserStore<ApplicationUser>>().Object, | |
| new Mock<IOptions<IdentityOptions>>().Object, | |
| new Mock<IPasswordHasher<ApplicationUser>>().Object, | |
| new IUserValidator<ApplicationUser>[0], | |
| new IPasswordValidator<ApplicationUser>[0], | |
| new Mock<ILookupNormalizer>().Object, | |
| new Mock<IdentityErrorDescriber>().Object, | |
| new Mock<IServiceProvider>().Object, | |
| new Mock<ILogger<UserManager<ApplicationUser>>>().Object) | |
| { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CollectionDefinition("Test")] | |
| public class FixtureCollection : ICollectionFixture<InjectFixture> | |
| { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class InjectFixture : IDisposable | |
| { | |
| public readonly UserManager<ApplicationUser> UserManager; | |
| public readonly SignInManager<ApplicationUser> SignInManager; | |
| public readonly IAccountService AccountService; | |
| public readonly ApplicationDbContext DbContext; | |
| public readonly IGenerator Generator; | |
| public InjectFixture() | |
| { | |
| var options = new DbContextOptionsBuilder<ApplicationDbContext>() | |
| .UseInMemoryDatabase(databaseName: "FakeDatabase") | |
| .Options; | |
| DbContext = new ApplicationDbContext(options); | |
| var users = new List<ApplicationUser> | |
| { | |
| new ApplicationUser | |
| { | |
| UserName = "Test", | |
| Id = Guid.NewGuid().ToString(), | |
| Email = "test@test.it" | |
| } | |
| }.AsQueryable(); | |
| var fakeUserManager = new Mock<FakeUserManager>(); | |
| fakeUserManager.Setup(x => x.Users) | |
| .Returns(users); | |
| fakeUserManager.Setup(x => x.DeleteAsync(It.IsAny<ApplicationUser>())) | |
| .ReturnsAsync(IdentityResult.Success); | |
| fakeUserManager.Setup(x => x.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>())) | |
| .ReturnsAsync(IdentityResult.Success); | |
| fakeUserManager.Setup(x => x.UpdateAsync(It.IsAny<ApplicationUser>())) | |
| .ReturnsAsync(IdentityResult.Success); | |
| fakeUserManager.Setup(x => | |
| x.ChangeEmailAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<string>())) | |
| .ReturnsAsync(IdentityResult.Success); | |
| var signInManager = new Mock<FakeSignInManager>(); | |
| signInManager.Setup( | |
| x => x.PasswordSignInAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<bool>(), | |
| It.IsAny<bool>())) | |
| .ReturnsAsync(SignInResult.Success); | |
| UserManager = fakeUserManager.Object; | |
| SignInManager = signInManager.Object; | |
| AccountService = new AccountService(UserManager); | |
| Generator = new Generator(); | |
| } | |
| public void Dispose() | |
| { | |
| UserManager?.Dispose(); | |
| DbContext?.Dispose(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment