public void GetCurrentUser_ShouldReturnArgumentNullException() { Assert.Throws <ArgumentNullException>(() => { ApplicationIdentityService applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, null); }); }
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 FindUserByRandomString_ShouldReturnNull() { // Arrange ApplicationIdentityService applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, _httpContextAccessor); // Act ApplicationUser retrievedUser = await applicationIdentityService.FindUserAsync(u => u.AzureAdObjectIdentifier == Guid.NewGuid().ToString()); // Assert Assert.Null(retrievedUser); }
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 LoadAssets_ShouldNotReturnArchivedAssets() { // Arrange var newProjectId = await _projectsService.CreateProject(CreateTestProject(), "127.0.1.1"); var newCredential = CreateTestCredential(); var newCredentialArchived = CreateTestCredential(); // Act - get and add assets var retrievedProject = await _projectsService.GetProject(newProjectId); await _assetService.AddAssetToProjectAsync(newProjectId, newCredential, "127.0.1.1"); await _assetService.AddAssetToProjectAsync(newProjectId, newCredentialArchived, "127.0.1.1"); // Act - archive and retrieve await _assetService.ArchiveAssetAsync(newProjectId, newCredentialArchived.Id, "127.0.1.1"); var archivedAsset = await _assetService.GetAssetAsync(newProjectId, newCredentialArchived.Id, "127.0.1.1"); // Act - reload project (it is already populated) by refreshing the context and load assets (should exlude archived) _dbContext.Dispose(); _dbContext = GetDbContext(); _applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, _httpContextAccessor, _fakeHttpContextItems); _projectsService = new ProjectsService(_dbContext, _encryptionService, _eventService, _applicationIdentityService, _permissionService); _assetService = new AssetService(_dbContext, _projectsService, _encryptionService, _eventService, _applicationIdentityService); _testUser = _applicationIdentityService.FindUserAsync(u => u.AzureAdObjectIdentifier == "TestAdObjectId11234567890").Result; var retrievedProjectAfterArchive = await _projectsService.GetProject(newProjectId); await _assetService.LoadAssetsAsync(retrievedProjectAfterArchive); // Assert Assert.Null(archivedAsset); Assert.Equal(1, retrievedProjectAfterArchive.Assets.Count()); Assert.Equal(false, retrievedProjectAfterArchive.Assets.Any(a => a.Id == newCredentialArchived.Id && a.IsArchived == true)); // Cleanup //_dbContext = GetDbContext(); //_applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, _httpContextAccessor, _fakeHttpContextItems); //_projectsService = new ProjectsService(_dbContext, _encryptionService, _eventService, _applicationIdentityService, _permissionService); var rr = _dbContext.Entry(_testUser).State; var retrievedProjectAfterArchiveForCleanup = await _projectsService.GetProject(newProjectId); await _projectsService.ArchiveProject(retrievedProjectAfterArchiveForCleanup, "127.0.1.1", _testUser); var archivedProject = await _projectsService.GetProject(newProjectId); Assert.Null(archivedProject); }
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); }