public IntegrationTestBase()
        {
            BuildTestConfiguration();
            _dbContext      = GetDbContext();
            _eventDbContext = GetEventDbContext();

            _testHttpContext                 = new DefaultHttpContext();
            _httpContextAccessor             = new HttpContextAccessor();
            _httpContextAccessor.HttpContext = _testHttpContext;
            _fakeHttpContextItems            = new Dictionary <object, object>();

            var memoryCache = new MemoryCache(new MemoryCacheOptions()
            {
            });
            var accessTokenRetriever = new TestAccessTokenRetriever();

            _graphService               = new MockGraphService();
            _encryptionService          = new EncryptionService();
            _applicationIdentityService = new ApplicationIdentityService(_dbContext, _graphService, _httpContextAccessor, _fakeHttpContextItems);
            _eventService               = new EventService(_eventDbContext, _applicationIdentityService);
            _permissionService          = new PermissionService(_dbContext, _graphService, _eventService, _applicationIdentityService);
            _projectsService            = new ProjectsService(_dbContext, _encryptionService, _eventService, _applicationIdentityService, _permissionService);
            _assetService               = new AssetService(_dbContext, _projectsService, _encryptionService, _eventService, _applicationIdentityService);

            _testUser = _applicationIdentityService.FindUserAsync(u => u.AzureAdObjectIdentifier == "TestAdObjectId11234567890").Result;
            if (_testUser == null)
            {
                _testUser = new ApplicationUser()
                {
                    DisplayName             = "Test User 123456789",
                    AzureAdObjectIdentifier = "TestAdObjectId11234567890",
                    TenantId              = "1234-12345-123",
                    AzureAdName           = "Test User Name",
                    AzureAdNameIdentifier = "123123kl21j3lk12j31",
                    Upn = "*****@*****.**",
                };

                _dbContext.ApplicationIdentities.Add(_testUser);
                _dbContext.SaveChanges();
                _testUser = _applicationIdentityService.FindUserAsync(u => u.AzureAdObjectIdentifier == "TestAdObjectId11234567890").Result;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Discards all tracked changes to the entity and marks it as archived in the database
        /// </summary>
        /// <param name="decryptedProject">The Project entity to archive.</param>
        /// <param name="remoteIpAddress">The IP address of the request causing the event</param>
        /// <returns>A Task result</returns>
        public async Task ArchiveProject(Project decryptedProject, string remoteIpAddress)
        {
            // Validation
            if (decryptedProject == null)
            {
                throw new ArgumentException("You must pass a valid project.");
            }

            // TODO: ensure the current user has access to archive this project
            var currentUser = await _applicationIdentityService.GetCurrentUser();

            var currentRetrievedUser = await _applicationIdentityService.FindUserAsync(u => u.AzureAdObjectIdentifier == currentUser.AzureAdObjectIdentifier);

            if (currentRetrievedUser == null)
            {
                throw new Exception("Unauthorised requests are not allowed.");                               // we fail on no current user
            }
            if (await _applicationIdentityService.IsCurrentUserAdmin() == false && await _permissionService.CheckAccessAsync(decryptedProject.Id, currentRetrievedUser, Enums.Role.Owner, this) == false)
            {
                // CR: this should rather return a failed result
                throw new Exception("The current user does not have enough permissions to archive this project.");
            }

            this.EncryptProject(decryptedProject);

            _dbContext.Entry(decryptedProject).Collection(x => x.Assets).Load();
            _dbContext.Entry(currentUser).State          = EntityState.Detached;
            _dbContext.Entry(currentRetrievedUser).State = EntityState.Detached;

            // Refresh the entity to discard changes and avoid saving a decrypted project
            //_dbContext.Entry(decryptedProject).State = EntityState.Unchanged;

            // Refresh assets and set to archived
            foreach (var asset in decryptedProject.Assets)
            {
                //_dbContext.Entry(asset.CreatedBy).State = EntityState.Unchanged;
                //_dbContext.Entry(asset).State = EntityState.Unchanged;
                asset.IsArchived = true;
                asset.Modified   = DateTime.UtcNow;

                currentRetrievedUser = await _applicationIdentityService.FindUserAsync(u => u.AzureAdObjectIdentifier == currentUser.AzureAdObjectIdentifier);

                asset.ModifiedBy = currentRetrievedUser;

                // LOG asset archive event
                await _eventService.LogArchiveAssetEventAsync(
                    decryptedProject.Id,
                    asset.Project.Title,
                    remoteIpAddress,
                    currentUser.Id,
                    currentUser.Upn,
                    asset.Id,
                    asset.Title,
                    asset.GetType() == typeof(Credential)?(asset as Credential).Login : string.Empty
                    );
            }

            decryptedProject.IsArchived = true; // set archive status

            await _eventService.LogArchiveProjectEventAsync(decryptedProject.Id, currentRetrievedUser.Id, remoteIpAddress);

            var entries = _dbContext.ChangeTracker.Entries()
                          .Where(e =>
                                 e.Entity.GetType() == typeof(ApplicationUser) &&
                                 e.State == EntityState.Added);

            foreach (var item in entries)
            {
                // CR : Fix this or leave a comment on why it is needed
                try
                {
                    _dbContext.Entry(item).State = EntityState.Detached;
                }
                catch
                {
                }
            }

            var modifiedRows = await _dbContext.SaveChangesAsync(); // save to db
        }