public async void GetCurrentUser_ShouldReturnUser() { // Arrange var newApplicationUser = new ApplicationUser(); newApplicationUser.AzureAdNameIdentifier = "Unit Tests 1234 - Name ID"; newApplicationUser.AzureAdObjectIdentifier = "Unit Test 1234 - Object ID"; newApplicationUser.TenantId = "Unit Test 1234 - Tenant ID"; newApplicationUser.Upn = "*****@*****.**"; _testHttpContext.Items[ApplicationIdentityService.CURRENTUSERKEY] = newApplicationUser; ApplicationUser returnedUser; ApplicationIdentityService applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, _httpContextAccessor); // Act returnedUser = await applicationIdentityService.GetCurrentUser(); // Assert Assert.NotNull(returnedUser); Assert.Equal(newApplicationUser.AzureAdNameIdentifier, returnedUser.AzureAdNameIdentifier); Assert.Equal(newApplicationUser.AzureAdObjectIdentifier, returnedUser.AzureAdObjectIdentifier); Assert.Equal(newApplicationUser.TenantId, returnedUser.TenantId); Assert.Equal(newApplicationUser.Upn, returnedUser.Upn); }
public async void GetCurrentUser_NullIdentityShouldReturnNull() { // Arrange ApplicationUser nullUser; ApplicationIdentityService applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, new HttpContextAccessor()); // Act nullUser = await applicationIdentityService.GetCurrentUser(null); // Assert Assert.Null(nullUser); }
public async void GetCurrentUser_UnauthenticatedIdentityShouldReturnNull() { // Arrange ApplicationUser nullUser; ApplicationIdentityService applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, new HttpContextAccessor()); _testHttpContext.Items[ApplicationIdentityService.CURRENTUSERKEY] = null; // need to clear singleton context // Act nullUser = await applicationIdentityService.GetCurrentUser(); // Assert Assert.Null(nullUser); }
public async void GetCurrentUser_ShouldReturnCorrectClaimsIdentity() { // Arrange List <Claim> claimsList = new List <Claim>(); claimsList.Add(new Claim( "http://schemas.microsoft.com/identity/claims/objectidentifier", "my unit test object id")); claimsList.Add(new Claim(ClaimTypes.NameIdentifier, "Name Identifier Test")); claimsList.Add(new Claim(ClaimTypes.Name, "Display Name Claim Test")); claimsList.Add(new Claim("name", "Simple Name Claim Test")); claimsList.Add(new Claim("ipaddr", "1.2.3.4")); claimsList.Add(new Claim(ClaimTypes.Upn, "*****@*****.**")); claimsList.Add(new Claim("http://schemas.microsoft.com/identity/claims/tenantid", "12345678-1234-1234-1234-123982828122")); var mockContext = new Mock <ClaimsIdentity>(); mockContext.SetupGet(p => p.IsAuthenticated).Returns(true); mockContext.SetupGet(p => p.Claims).Returns(claimsList); ApplicationIdentityService applicationIdentityService; ApplicationUser retrievedUser; // Act applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, _httpContextAccessor); retrievedUser = await applicationIdentityService.GetCurrentUser(mockContext.Object); // Assert Assert.NotNull(retrievedUser); Assert.Equal("my unit test object id", retrievedUser.AzureAdObjectIdentifier); Assert.Equal("Name Identifier Test", retrievedUser.AzureAdNameIdentifier); Assert.Equal("Display Name Claim Test", retrievedUser.AzureAdName); Assert.Equal("Simple Name Claim Test", retrievedUser.DisplayName); Assert.Equal("*****@*****.**", retrievedUser.Upn); Assert.Equal("12345678-1234-1234-1234-123982828122", retrievedUser.TenantId); }