public void Create_EffectiveAuthorization_Without_EffectiveAuthorizationEvent_InStorage_Returns_Timeline_With_EffectiveAuthorization()
        {
            //Arrange
            var eaEventsStorate  = new RawEventInMemoryStorage();
            var eaHandlerFactory = new EffectiveAuthorizationHandlerFactory();

            eaHandlerFactory.RegisterHandler(typeof(EffectiveAuthorizationGrantedEvent), new PermissionGrantedHandler());

            var owner = new ExternalId()
            {
                Id = _defaultId, Context = _defaultContext
            };
            var permission = new Permission()
            {
                Application = _defaultApplication, Id = _defaultPermissionId, Description = _defaultDescription
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = owner
            };

            var eaTimelineFactory = new EffectiveAuthorizationTimelineFactory(eaEventsStorate, eaHandlerFactory);

            //Act
            var eaTimeline = eaTimelineFactory.Create(effectiveAuthorization).Result;

            //Assert
            Assert.IsNotNull(eaTimeline);
            Assert.AreEqual(effectiveAuthorization, eaTimeline.EffectiveAuthorization);
        }
示例#2
0
        public Task <IList <EffectiveAuthorizationInterval> > GetIntervals(EffectiveAuthorization effectiveAuthorization)
        {
            //The reason for composing the full SQL statement instead of using the Equals operation as part of a linq "where" is because "Equals" operator is not supported
            var resourceResponse = _client.CreateDocumentQuery <EffectiveAuthorizationInterval>(UriFactory.CreateDocumentCollectionUri(_dbConfig.Database, _dbConfig.Collection), GetSQLQueryByEffectiveAuthorisation(effectiveAuthorization), _queryOptions).ToList();

            return(Task.FromResult <IList <EffectiveAuthorizationInterval> >(resourceResponse));
        }
示例#3
0
        public async Task DeleteIntervals(EffectiveAuthorization effectiveAuthorization)
        {
            try
            {
                var resourceResponse = _client
                                       .CreateDocumentQuery <Document>(
                    UriFactory.CreateDocumentCollectionUri(_dbConfig.Database, _dbConfig.Collection),
                    GetSQLQueryByEffectiveAuthorisation(effectiveAuthorization), _queryOptions).ToList();
                var tasks          = new List <Task>();
                var requestOptions = new RequestOptions
                {
                    PartitionKey = new PartitionKey(effectiveAuthorization.TenantId)
                };
                foreach (Document doc in resourceResponse)
                {
                    tasks.Add(_client.DeleteDocumentAsync(doc.SelfLink, requestOptions));
                }

                Task.WaitAll(tasks.ToArray());
            }
            catch (Exception e)
            {
                _logger.LogCritical($"Storage failure causes process interruption due to :{e}");
                throw new ReportingStorageException(e.Message, e);
            }
        }
        public async Task RawEventStorageRepository_GetRawEventsAsync_ExistingRevokedEvent_Returns_EffectiveAuthorizationRevokedEvent_With_Right_EffectiveAuthorization()
        {
            //Arrange: create event data to be stored
            var owner = new ExternalId()
            {
                Context = _ownerContext, Id = _ownerId
            };
            var permission = new HAS.Core.Domain.Permission()
            {
                Application = _applicationName, Id = _Identifier, Description = _Identifier
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = owner
            };
            var eaEvent = new EffectiveAuthorizationRevokedEvent()
            {
                Until = _until, EffectiveAuthorization = effectiveAuthorization, DateCreated = DateTime.Now
            };

            //Act: Attempt to write event to repository
            _eventIdList.Add(await _repository.WriteRawEventAsync(eaEvent));

            //Act: Attempt to get nonexisting event
            var rawEventList = await _repository.GetRawEventsAsync(effectiveAuthorization);

            var revokedRawEventList = rawEventList.Where(e => e.GetType() == typeof(EffectiveAuthorizationRevokedEvent));

            //Assert: check that there is a returned Id with some data
            Assert.IsNotNull(rawEventList);
            Assert.IsTrue(rawEventList.Count > 0);
            Assert.IsNotNull(revokedRawEventList);
            Assert.AreEqual(rawEventList[0].EffectiveAuthorization, effectiveAuthorization);
        }
        public async Task RawEventStorageRepository_WriteRawEventAsync_OnSuccessfulWritingRevokedEvent_Returns_Event_Id()
        {
            //Arrange: create event data to be stored
            var owner = new ExternalId()
            {
                Context = _ownerContext, Id = _ownerId
            };
            var permission = new HAS.Core.Domain.Permission()
            {
                Application = _applicationName, Id = _Identifier, Description = _Identifier
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = owner
            };
            var eaEvent = new EffectiveAuthorizationRevokedEvent()
            {
                Until = _until, EffectiveAuthorization = effectiveAuthorization, DateCreated = DateTime.Now
            };

            //Act: Attempt to write event to repository
            _eventIdList.Add(await _repository.WriteRawEventAsync(eaEvent));

            //Assert: check that there is a returned Id with some data
            Assert.IsNotNull(_eventIdList[0]);
            Assert.IsTrue(_eventIdList[0].Length > 0);
        }
示例#6
0
        public async Task ReportingStorageRepository_GetIntervals_NonExistingEffectiveAuthorization_Returns_EmptyList()
        {
            //Arrange: create Non-existing EffectiveAuthorization
            var owner = new ExternalId()
            {
                Context = "NonExisting", Id = "NonExisting"
            };
            var permission = new HAS.Core.Domain.Permission()
            {
                Application = "NonExisting", Id = "NonExisting", Description = "NonExisting"
            };
            var target = new ExternalId()
            {
                Id = "NonExisting", Context = "NonExisting"
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = "NonExisting" + DateTime.Now.Ticks, Permission = permission, User = owner, Target = target
            };

            //Act: Attempt to get EffectiveAuthorization intervals
            var intervals = await _repository.GetIntervals(effectiveAuthorization);

            //Assert: check that there is no returned data
            Assert.IsNotNull(intervals);
            Assert.AreEqual(0, intervals.Count);
        }
示例#7
0
        public async Task ReportingStorageRepository_GetIntervals_ExistingEffectiveAuthorizationInterval_With_TargetPerson_Returns_EffectiveAuthorizationGrantedEvent_With_Right_EffectiveAuthorization()
        {
            //Arrange: create EffectiveAuthorizationInterval data to be stored
            var owner = new ExternalId()
            {
                Context = _ownerContext, Id = _ownerId
            };
            var user = new Person(owner, new PersonalInfo()
            {
                BirthDate             = _birthDate1,
                Initials              = _initials1,
                LastNameAtBirth       = _lastNameAtBirth1,
                LastNameAtBirthPrefix = _lastNameAtBirthPrefix1
            });

            var target = new ExternalId()
            {
                Context = _ownerContext, Id = _targetId
            };
            var targetPerson = new Person(target,
                                          new PersonalInfo()
            {
                BirthDate             = _birthDate2,
                Initials              = _initials2,
                LastNameAtBirth       = _lastNameAtBirth2,
                LastNameAtBirthPrefix = _lastNameAtBirthPrefix2
            });

            var permission = new HAS.Core.Domain.Permission()
            {
                Application = _applicationName,
                Id          = _Identifier,
                Description = _Identifier
            };
            var interval = new Interval(_dateTimeStart1, null);
            var effectiveAuthorizationInterval = new EffectiveAuthorizationInterval(interval, user, targetPerson, permission, _tenantId);

            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = owner, Target = target
            };

            var intervalList = new List <EffectiveAuthorizationInterval>();

            intervalList.Add(effectiveAuthorizationInterval);

            //Act: Attempt to write effectiveAuthorizationInterval to repository and getting it back
            _idToDeleteList.AddRange((await _repository.SaveAsync(intervalList)).Split(","));

            var effectiveAuthorizationIntervals = await _repository.GetIntervals(effectiveAuthorization);

            //Assert: check that there is a returned Id with some data
            Assert.IsNotNull(effectiveAuthorizationIntervals);
            Assert.IsTrue(effectiveAuthorizationIntervals.Count > 0);
            Assert.IsInstanceOfType(effectiveAuthorizationIntervals[0], typeof(EffectiveAuthorizationInterval));
            Assert.AreEqual(effectiveAuthorizationIntervals[0].TenantId, effectiveAuthorization.TenantId);
            Assert.AreEqual(effectiveAuthorizationIntervals[0].User.Key, effectiveAuthorization.User);
            Assert.AreEqual(effectiveAuthorizationIntervals[0].TargetPerson.Key, effectiveAuthorization.Target);
            Assert.AreEqual(effectiveAuthorizationIntervals[0].Permission, effectiveAuthorization.Permission);
        }
示例#8
0
        public void MapEffectiveAuthorization_DTO_With_Target_Returns_Domain_EffectiveAuthorization_With_Valid_Values()
        {
            //Arrange
            var dtoEffectiveAuthorization = new EffectiveAuthorization(string1, new ExternalId(string2, string3), new Permission(string4, string5, string6), new ExternalId(string7, string8));

            //Act
            var domainEffectiveAuthorization = DomainAdapter.MapEffectiveAuthorization(dtoEffectiveAuthorization);

            //Assert
            Assert.IsNotNull(domainEffectiveAuthorization);
            Assert.IsNotNull(domainEffectiveAuthorization.Permission);
            Assert.IsNotNull(domainEffectiveAuthorization.User);
            Assert.IsNotNull(domainEffectiveAuthorization.Target);

            Assert.AreEqual(string1, domainEffectiveAuthorization.TenantId);

            Assert.AreEqual(dtoEffectiveAuthorization.Permission.Application, domainEffectiveAuthorization.Permission.Application);
            Assert.AreEqual(dtoEffectiveAuthorization.Permission.Description, domainEffectiveAuthorization.Permission.Description);
            Assert.AreEqual(dtoEffectiveAuthorization.Permission.Id, domainEffectiveAuthorization.Permission.Id);

            Assert.AreEqual(dtoEffectiveAuthorization.Target.Context, domainEffectiveAuthorization.Target.Context);
            Assert.AreEqual(dtoEffectiveAuthorization.Target.Id, domainEffectiveAuthorization.Target.Id);

            Assert.AreEqual(dtoEffectiveAuthorization.User.Context, domainEffectiveAuthorization.User.Context);
            Assert.AreEqual(dtoEffectiveAuthorization.User.Id, domainEffectiveAuthorization.User.Id);
        }
示例#9
0
        public async Task AddEffectiveAuthorizationAsync_Event_Stored_With_Invalid_Authorization_No_Permission_Throws_NullReferenceException()
        {
            //Arrange
            var eaEventsStorate = new RawEventInMemoryStorage();

            var eaHandlerFactory = new EffectiveAuthorizationHandlerFactory();

            eaHandlerFactory.RegisterHandler(typeof(EffectiveAuthorizationGrantedEvent), new PermissionGrantedHandler());

            var eatimelineFactory = new EffectiveAuthorizationTimelineFactory(eaEventsStorate, eaHandlerFactory);

            var prService = new PersonalInfoEnrichmentService(new MockPersonalLocalStorage(), new MockPersonalInfoExternalServiceFactory(), new MockInMemoryLogger());
            var rpStorage = new ReportingInMemoryStorage();

            var permission = new Permission()
            {
                Application = _defaultApplication, Id = _defaultPermissionId, Description = _defaultDescription
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission
            };
            var eaEvent = new EffectiveAuthorizationGrantedEvent()
            {
                From = new DateTime(2018, 1, 1), EffectiveAuthorization = effectiveAuthorization
            };

            await eaEventsStorate.WriteRawEventAsync(eaEvent); //event needs to be previously writteng to storage.

            var dataEnrichmentService = new DataEnrichmentService(eatimelineFactory, prService, rpStorage);

            //Act & Assert
            await Assert.ThrowsExceptionAsync <NullReferenceException>(() => dataEnrichmentService.AddEffectiveAuthorizationAsync(eaEvent));
        }
示例#10
0
        public IEventPushResponse PushCompanyWideEffectiveAuthorizationRevoked(string targetCompanyId, ExternalId user, Permission permission, DateTime effectiveDate)
        {
            var effectiveAuthorization = new EffectiveAuthorization(targetCompanyId, user, permission);

            effectiveAuthorization.AddTargetCompany(targetCompanyId);

            return(PushEffectiveAuthorizationRevoked(effectiveAuthorization, effectiveDate));
        }
示例#11
0
        public async Task <IEventPushResponse> PushCompanyWideEffectiveAuthorizationGrantedAsync(string targetCompanyId, ExternalId user, Permission permission, DateTime effectiveDate)
        {
            var effectiveAuthorization = new EffectiveAuthorization(targetCompanyId, user, permission);

            effectiveAuthorization.AddTargetCompany(targetCompanyId);

            return(await PushEffectiveAuthorizationGrantedAsync(effectiveAuthorization, effectiveDate));
        }
示例#12
0
        public async Task AddEffectiveAuthorizationAsync_GrantedAndRevokedEventsForUnexistingUserContextAndWithoutTarget_ClosedIntervalIsCreatedInReadModelWithoutEnrichedPersonalInfo()
        {
            //Create a raw event
            var user = new ExternalId()
            {
                Context = "unexistingContext", Id = _userId
            };
            var permission = new Permission()
            {
                Application = "YOUFORCE", Id = "HomeAccess", Description = ""
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = user
            };

            var eaGrantedEvent = new EffectiveAuthorizationGrantedEvent()
            {
                From = _from, EffectiveAuthorization = effectiveAuthorization, DateCreated = DateTime.Now
            };
            var eaRevokedEvent = new EffectiveAuthorizationRevokedEvent()
            {
                Until = _until, EffectiveAuthorization = effectiveAuthorization, DateCreated = DateTime.Now
            };


            _eventIdList.Add(await _repository.WriteRawEventAsync(eaGrantedEvent));
            _eventIdList.Add(await _repository.WriteRawEventAsync(eaRevokedEvent));

            //Data Enrichment
            var eaHandlerFactory = new EffectiveAuthorizationHandlerFactory();

            eaHandlerFactory.RegisterHandler(typeof(EffectiveAuthorizationRevokedEvent), new PermissionRevokedHandler());
            eaHandlerFactory.RegisterHandler(typeof(EffectiveAuthorizationGrantedEvent), new PermissionGrantedHandler());
            var eatimelineFactory = new EffectiveAuthorizationTimelineFactory(_repository, eaHandlerFactory);

            var logger    = new MockInMemoryLogger();
            var prService = PersonalInfoEnrichmentServiceHelper.BuildService(logger);
            var readModel = new ReportingInMemoryStorage();

            var dataEnrichmentService = new DataEnrichmentService(eatimelineFactory, prService, readModel);

            await dataEnrichmentService.AddEffectiveAuthorizationAsync(eaRevokedEvent);

            //Assertions
            var expectedUser = new Person(user, null);
            var intervals    = readModel.GetIntervals(effectiveAuthorization).Result;

            Assert.AreEqual(1, intervals.Count);
            Assert.AreEqual(_tenantId, intervals[0].TenantId);
            Assert.AreEqual(user, intervals[0].User.Key);
            Assert.IsNull(intervals[0].User.PersonalInfo);
            Assert.AreEqual(permission, intervals[0].Permission);
            Assert.AreEqual(null, intervals[0].TargetPerson);
            Assert.AreEqual(_from, intervals[0].EffectiveInterval.Start);
            Assert.AreEqual(_until, intervals[0].EffectiveInterval.End);
        }
示例#13
0
        private void ValidateContent(EffectiveAuthorization authorizationToValidate)
        {
            Validator.ValidateObject(authorizationToValidate, new ValidationContext(authorizationToValidate), true);
            Validator.ValidateObject(authorizationToValidate.User, new ValidationContext(authorizationToValidate.User), true);
            Validator.ValidateObject(authorizationToValidate.Permission, new ValidationContext(authorizationToValidate.Permission), true);

            if (authorizationToValidate.Target != null)
            {
                Validator.ValidateObject(authorizationToValidate.Target, new ValidationContext(authorizationToValidate.Target), true);
            }
        }
示例#14
0
        public IEventPushResponse PushEffectiveAuthorizationRevoked(EffectiveAuthorization authorization, DateTime effectiveDate)
        {
            ValidateContent(authorization);

            var eventToPush = new EffectiveAuthorizationRevokedEvent()
            {
                UntilDateTime = effectiveDate, EffectiveAuthorization = authorization
            };

            return(_eventPusher.PushRevoked(eventToPush));
        }
示例#15
0
        public async Task AddEffectiveAuthorizationAsync_GrantedEventForExistingUserAndTarget_OpenIntervalIsCreatedInReadModelWithEnrichedPersonalInfo()
        {
            //Create a raw event
            var user = new ExternalId()
            {
                Context = _context, Id = _userId
            };
            var permission = new Permission()
            {
                Application = "YOUFORCE", Id = "HomeAccess", Description = "Home Access"
            };
            var target = new ExternalId()
            {
                Id = _targetId, Context = _context
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = user, Target = target
            };
            var eaEvent = new EffectiveAuthorizationGrantedEvent()
            {
                From = _from, EffectiveAuthorization = effectiveAuthorization, DateCreated = DateTime.Now
            };

            //Add event to the memory event store
            _eventIdList.Add(await _repository.WriteRawEventAsync(eaEvent));

            //Data Enrichment
            var eaHandlerFactory = new EffectiveAuthorizationHandlerFactory();

            eaHandlerFactory.RegisterHandler(typeof(EffectiveAuthorizationGrantedEvent), new PermissionGrantedHandler());
            var eatimelineFactory = new EffectiveAuthorizationTimelineFactory(_repository, eaHandlerFactory);

            var logger    = new MockInMemoryLogger();
            var prService = PersonalInfoEnrichmentServiceHelper.BuildService(logger);
            var readModel = new ReportingInMemoryStorage();

            var dataEnrichmentService = new DataEnrichmentService(eatimelineFactory, prService, readModel);

            await dataEnrichmentService.AddEffectiveAuthorizationAsync(eaEvent);

            //Assertions
            var expectedUser   = new Person(user, _userPersonalInfo);
            var expectedTarget = new Person(target, _targetPersonalInfo);
            var intervals      = readModel.GetIntervals(effectiveAuthorization).Result;

            Assert.AreEqual(1, intervals.Count);
            Assert.AreEqual(_tenantId, intervals[0].TenantId);
            Assert.AreEqual(expectedUser, intervals[0].User);
            Assert.AreEqual(permission, intervals[0].Permission);
            Assert.AreEqual(expectedTarget, intervals[0].TargetPerson);
            Assert.AreEqual(_from, intervals[0].EffectiveInterval.Start);
        }
        public void ExtensionMethod_AddTargetCompany_Adds_Target_With_Right_Company_Data()
        {
            //Arrange
            var user          = new ExternalId(_defaultContext, _defaultUserId1);
            var permission    = new Permission(_defaultIdentifier, _defaultApplication);
            var authorization = new EffectiveAuthorization(_defaultTenant, user, permission);

            //Assert
            authorization.AddTargetCompany(_defaultTenant);

            Assert.IsNotNull(authorization.Target);
            Assert.AreEqual(_companyContext, authorization.Target.Context);
            Assert.AreEqual(_defaultTenant, authorization.Target.Id);
        }
示例#17
0
        public async Task <EffectiveAuthorizationTimeline> Create(EffectiveAuthorization effectiveAuthorization)
        {
            var events = await eventStore.GetRawEventsAsync(effectiveAuthorization);

            var effectiveAuthorizationTimeline = new EffectiveAuthorizationTimeline(effectiveAuthorization);

            foreach (var authorizationEvent in events)
            {
                eventHandlerFactory.GetHandler(authorizationEvent)
                .ApplyEvent(authorizationEvent, effectiveAuthorizationTimeline);
            }

            return(effectiveAuthorizationTimeline);
        }
示例#18
0
        public async Task ReportingStorageRepository_DeleteIntervals_ExistingEffectiveAuthorizationIntervals_Deletes_All_With_Same_Effective_Authorization_Values()
        {
            //Arrange: create EffectiveAuthorizationInterval data to be stored
            var owner = new ExternalId()
            {
                Context = _ownerContext, Id = _ownerId
            };
            var user = new Person(owner, new PersonalInfo()
            {
                BirthDate = _birthDate1, Initials = _initials1, LastNameAtBirth = _lastNameAtBirth1, LastNameAtBirthPrefix = _lastNameAtBirthPrefix1
            });

            var target = new ExternalId()
            {
                Context = _ownerContext, Id = _targetId
            };
            var targetPerson = new Person(target, new PersonalInfo()
            {
                BirthDate = _birthDate2, Initials = _initials2, LastNameAtBirth = _lastNameAtBirth2, LastNameAtBirthPrefix = _lastNameAtBirthPrefix2
            });

            var permission = new HAS.Core.Domain.Permission()
            {
                Application = _applicationName, Id = _Identifier, Description = _Identifier
            };
            var interval = new Interval(_dateTimeStart1, null);
            var effectiveAuthorizationInterval = new EffectiveAuthorizationInterval(interval, user, targetPerson, permission, _tenantId);

            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = owner, Target = target
            };

            var intervalList = new List <EffectiveAuthorizationInterval>();

            intervalList.Add(effectiveAuthorizationInterval);

            //Act: Attempt to write effectiveAuthorizationInterval to repository, delete it and try to retrieve it back
            _idToDeleteList.AddRange((await _repository.SaveAsync(intervalList)).Split(",")); //In case test fails

            await _repository.DeleteIntervals(effectiveAuthorization);

            var effectiveAuthorizationIntervals = await _repository.GetIntervals(effectiveAuthorization);

            //Assert: check that there is no intervals
            Assert.IsNotNull(effectiveAuthorizationIntervals);
            Assert.IsTrue(effectiveAuthorizationIntervals.Count == 0);
        }
        public void Contstructor_With_No_Target_Returns_EffectiveAuthorization_Without_Target()
        {
            //Arrange
            var user       = new ExternalId(_defaultContext, _defaultUserId1);
            var permission = new Permission(_defaultIdentifier, _defaultApplication);

            //Act
            var authorization = new EffectiveAuthorization(_defaultTenant, user, permission);

            //Assert
            Assert.IsInstanceOfType(authorization, typeof(EffectiveAuthorization));
            Assert.AreEqual(_defaultTenant, authorization.TenantId);
            Assert.AreEqual(user, authorization.User);
            Assert.AreEqual(permission, authorization.Permission);
            Assert.AreEqual(null, authorization.Target);
        }
示例#20
0
        public static Core.Domain.EffectiveAuthorization MapEffectiveAuthorization(EffectiveAuthorization dtoEffectiveAuthorization)
        {
            var output = new Core.Domain.EffectiveAuthorization()
            {
                TenantId   = dtoEffectiveAuthorization.TenantId.Trim(),
                User       = MapExternalId(dtoEffectiveAuthorization.User),
                Permission = MapPermission(dtoEffectiveAuthorization.Permission)
            };

            if (dtoEffectiveAuthorization.Target != null)
            {
                output.Target = MapExternalId(dtoEffectiveAuthorization.Target);
            }

            return(output);
        }
示例#21
0
        public async Task AddEffectiveAuthorizationAsync_Event_Stored_With_Valid_Authorization_Without_Target_Stores_Correct_Information()
        {
            //Arrange
            var eaEventsStorate = new RawEventInMemoryStorage();

            var eaHandlerFactory = new EffectiveAuthorizationHandlerFactory();

            eaHandlerFactory.RegisterHandler(typeof(EffectiveAuthorizationGrantedEvent), new PermissionGrantedHandler());

            var eatimelineFactory = new EffectiveAuthorizationTimelineFactory(eaEventsStorate, eaHandlerFactory);

            var prService = new PersonalInfoEnrichmentService(new MockPersonalLocalStorage(), new MockPersonalInfoExternalServiceFactory(), new MockInMemoryLogger());

            var rpStorage = new ReportingInMemoryStorage();
            var owner     = new ExternalId()
            {
                Id = _defaultId, Context = _defaultContext
            };
            var permission = new Permission()
            {
                Application = _defaultApplication, Id = _defaultPermissionId, Description = _defaultDescription
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = owner
            };
            var eaEvent = new EffectiveAuthorizationGrantedEvent()
            {
                From = new DateTime(2018, 1, 1), EffectiveAuthorization = effectiveAuthorization
            };

            await eaEventsStorate.WriteRawEventAsync(eaEvent); //event needs to be previously writteng to storage.

            var dataEnrichmentService = new DataEnrichmentService(eatimelineFactory, prService, rpStorage);

            //Act
            await dataEnrichmentService.AddEffectiveAuthorizationAsync(eaEvent);

            var intervals = await rpStorage.GetIntervals(effectiveAuthorization);

            //Assert
            Assert.AreEqual(1, intervals.Count);
            Assert.AreEqual(_tenantId, intervals[0].TenantId);
            Assert.AreEqual(owner, intervals[0].User.Key);
            Assert.AreEqual(permission, intervals[0].Permission);
            Assert.AreEqual(null, intervals[0].TargetPerson);
        }
        public void Contructor_Returns_EffectiveAuthorization_With_Values_And_Properties()
        {
            //Arrange
            var user       = new ExternalId(_defaultContext, _defaultUserId1);
            var permission = new Permission(_defaultIdentifier, _defaultApplication);
            var target     = new ExternalId(_defaultContext, _defaultPersonId1);

            //Act
            var authorization = new EffectiveAuthorization(_defaultTenant, user, permission, target);

            //Assert
            Assert.IsInstanceOfType(authorization, typeof(EffectiveAuthorization));
            Assert.AreEqual(_defaultTenant, authorization.TenantId);
            Assert.AreEqual(user, authorization.User);
            Assert.AreEqual(permission, authorization.Permission);
            Assert.AreEqual(target, authorization.Target);
        }
示例#23
0
        public async Task ReportingStorageRepository_GetIntervals_Null_Permission_Returns_NullReferenceException()
        {
            //Arrange: create EffectiveAuthorization without user
            var owner = new ExternalId()
            {
                Context = "NonExisting", Id = "NonExisting"
            };
            var target = new ExternalId()
            {
                Id = "NonExisting", Context = "NonExisting"
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = "NonExisting" + DateTime.Now.Ticks, Permission = null, User = owner, Target = target
            };

            //Act: Attempt to get EffectiveAuthorization intervals
            var intervals = await _repository.GetIntervals(effectiveAuthorization);
        }
        public async Task RawEventStorageRepository_GetRawEventsAsync_Null_User_Returns_NullReferenceException()
        {
            //Arrange: create EffectiveAuthorization without user
            var permission = new HAS.Core.Domain.Permission()
            {
                Application = "NonExisting", Id = "NonExisting", Description = "NonExisting"
            };
            var target = new ExternalId()
            {
                Id = "NonExisting", Context = "NonExisting"
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = "NonExisting" + DateTime.Now.Ticks, Permission = permission, User = null, Target = target
            };

            //Act: Attempt to get related events
            var rawEvent = await _repository.GetRawEventsAsync(effectiveAuthorization);
        }
        public void ReactiveEffectiveAuthorizationLoggingTestAsync()
        {
            //Arrange

            var serviceCollection = new ServiceCollection();
            var appStartup        = new MockAppStartupFactory("reactive").GetAppStartup();

            appStartup.ConfigureServices(serviceCollection, null);
            var provider = serviceCollection.BuildServiceProvider();

            appStartup.ConfigureStartup(provider);

            var effAuthLoginService = provider.GetService <IEffectiveAuthorizationLogging>();

            var grantTime1 = DateTime.UtcNow - TimeSpan.FromDays(4);

            var effectiveAuthorization = new EffectiveAuthorization
            {
                User = new ExternalId {
                    Context = "Youforce", Id = "IC000001"
                },
                Target = new ExternalId {
                    Context = "Youforce", Id = "IC000002"
                },
                TenantId   = "tenant1",
                Permission = new Permission {
                    Application = "A1", Id = "p1"
                }
            };

            var effAuthzGrantedEvent = new EffectiveAuthorizationGrantedEvent
            {
                EffectiveAuthorization = effectiveAuthorization,
                From = grantTime1
            };

            //Act
            var result = effAuthLoginService.AddAuthLogAsync(effAuthzGrantedEvent);

            //Assert
            Assert.AreEqual("RanToCompletion", result.Status.ToString());
        }
示例#26
0
        public async Task AddEffectiveAuthorizationAsync_Event_Stored_With_Valid_Authorization_Without_Target_Calls_IReportingStorage_SaveAsyc()
        {
            //Arrange
            var eaEventsStorate = new RawEventInMemoryStorage();

            var eaHandlerFactory = new EffectiveAuthorizationHandlerFactory();

            eaHandlerFactory.RegisterHandler(typeof(EffectiveAuthorizationGrantedEvent), new PermissionGrantedHandler());

            var eatimelineFactory = new EffectiveAuthorizationTimelineFactory(eaEventsStorate, eaHandlerFactory);

            var prService = new PersonalInfoEnrichmentService(new MockPersonalLocalStorage(), new MockPersonalInfoExternalServiceFactory(), new MockInMemoryLogger());

            var rpStorageMock = new Mock <IReportingStorage>();
            var owner         = new ExternalId()
            {
                Id = _defaultId, Context = _defaultContext
            };
            var permission = new Permission()
            {
                Application = _defaultApplication, Id = _defaultPermissionId, Description = _defaultDescription
            };
            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = owner
            };
            var eaEvent = new EffectiveAuthorizationGrantedEvent()
            {
                From = new DateTime(2018, 1, 1), EffectiveAuthorization = effectiveAuthorization
            };

            await eaEventsStorate.WriteRawEventAsync(eaEvent); //event needs to be previously writteng to storage.

            var dataEnrichmentService = new DataEnrichmentService(eatimelineFactory, prService, rpStorageMock.Object);

            //Act
            await dataEnrichmentService.AddEffectiveAuthorizationAsync(eaEvent);

            //Assert
            rpStorageMock.Verify(v => v.SaveAsync(It.IsAny <IList <EffectiveAuthorizationInterval> >()));
        }
示例#27
0
        private string GetSQLQueryByEffectiveAuthorisation(EffectiveAuthorization ea)
        {
            var queryBuilder = new List <string>();

            queryBuilder.Add($@"c.TenantId = ""{ea.TenantId}""");

            queryBuilder.Add($@"c.User.Key.Context = ""{ea.User.Context}""");
            queryBuilder.Add($@"c.User.Key.Id = ""{ea.User.Id}""");

            queryBuilder.Add($@"c.Permission.Application = ""{ea.Permission.Application}""");
            queryBuilder.Add($@"c.Permission.Id = ""{ea.Permission.Id}""");

            if (ea.Target != null)
            {
                queryBuilder.Add($@"c.TargetPerson.Key.Context = ""{ea.Target.Context}""");
                queryBuilder.Add($@"c.TargetPerson.Key.Id = ""{ea.Target.Id}""");
            }

            var whereClause = String.Join(" AND ", queryBuilder.ToArray());

            return($"SELECT * FROM c WHERE {whereClause}");
        }
        public Task <string> SaveAsync(IList <EffectiveAuthorizationInterval> effectiveAuthorizationIntervals)
        {
            if (effectiveAuthorizationIntervals.Count == 0)
            {
                return(Task.FromResult <string>(string.Empty));
            }

            // Asumming all intervals belong to same effective authorization (it is a mock!)
            var effectiveAuthorizationIntervalRef = effectiveAuthorizationIntervals[0];
            var key = new EffectiveAuthorization
            {
                User       = effectiveAuthorizationIntervalRef.User.Key,
                Target     = effectiveAuthorizationIntervalRef.TargetPerson?.Key,
                Permission = effectiveAuthorizationIntervalRef.Permission,
                TenantId   = effectiveAuthorizationIntervalRef.TenantId
            };

            effectiveAuthorizationIntervalStore.Remove(key);

            effectiveAuthorizationIntervalStore.Add(key, effectiveAuthorizationIntervals);
            return(Task.FromResult <string>(string.Empty));
        }
示例#29
0
        public async Task <string> SaveAsync(IList <EffectiveAuthorizationInterval> effectiveAuthorizationIntervals)
        {
            //FIXME this code is a fudge to be kept until a decision is made about the effectiveAuthorizationIntervals saving individually or in collection
            string[] output = new string[effectiveAuthorizationIntervals.Count];

            try
            {
                //FIXME because there is no way to identify the document in DB matching the Domain entity, the Upsert will always insert. That's why previous deletion is necessary.
                //FIXME some decision needs to be taken about this.

                var effectiveAuthorization = new EffectiveAuthorization
                {
                    TenantId   = effectiveAuthorizationIntervals[0].TenantId,
                    User       = effectiveAuthorizationIntervals[0].User.Key,
                    Permission = effectiveAuthorizationIntervals[0].Permission,
                    Target     = effectiveAuthorizationIntervals[0].TargetPerson?.Key ?? null
                };

                await DeleteIntervals(effectiveAuthorization);

                var resourceResponses = new List <ResourceResponse <Document> >();
                foreach (var t in effectiveAuthorizationIntervals)
                {
                    var response = _client.CreateDocumentAsync(
                        UriFactory.CreateDocumentCollectionUri(_dbConfig.Database, _dbConfig.Collection),
                        t).Result;
                    resourceResponses.Add(response);
                }
                return(string.Join(",", resourceResponses.Select(x => x.Resource.Id)));
            }
            catch (Exception e)
            {
                _logger.LogCritical($"Storage failure causes process interruption due to :{e}");
                throw new ReportingStorageException(e.Message, e);
            }
        }
示例#30
0
        public async Task ReportingStorageRepository_SaveAsync_IntervalList_Only_Saved_Ones_Remain_Stored()
        {
            //Arrange: create and insert interval list
            var owner = new ExternalId()
            {
                Context = _ownerContext, Id = _ownerId
            };
            var user = new Person(owner, new PersonalInfo()
            {
                BirthDate = _birthDate1, Initials = _initials1, LastNameAtBirth = _lastNameAtBirth1, LastNameAtBirthPrefix = _lastNameAtBirthPrefix1
            });

            var target = new ExternalId()
            {
                Context = _ownerContext, Id = _targetId
            };
            var targetPerson = new Person(target, new PersonalInfo()
            {
                BirthDate = _birthDate2, Initials = _initials2, LastNameAtBirth = _lastNameAtBirth2, LastNameAtBirthPrefix = _lastNameAtBirthPrefix2
            });

            var permission = new HAS.Core.Domain.Permission()
            {
                Application = _applicationName, Id = _Identifier, Description = _Identifier
            };
            var intervalList = new List <EffectiveAuthorizationInterval>();

            EffectiveAuthorizationInterval auxInterval;

            auxInterval = new EffectiveAuthorizationInterval(new Interval(_dateTimeStart1, _dateTimeEnd1), user, targetPerson, permission, _tenantId);
            intervalList.Add(auxInterval);
            auxInterval = new EffectiveAuthorizationInterval(new Interval(_dateTimeStart1.AddDays(1), _dateTimeEnd1.AddDays(1)), user, targetPerson, permission, _tenantId);
            intervalList.Add(auxInterval);
            auxInterval = new EffectiveAuthorizationInterval(new Interval(_dateTimeStart1.AddDays(2), _dateTimeEnd1.AddDays(2)), user, targetPerson, permission, _tenantId);
            intervalList.Add(auxInterval);

            _idToDeleteList.AddRange((await _repository.SaveAsync(intervalList)).Split(",")); //In case test fails

            var effectiveAuthorization = new EffectiveAuthorization()
            {
                TenantId = _tenantId, Permission = permission, User = owner, Target = target
            };

            //assert: check that the retrieved intervals from BBDD match
            var retrievedIntervals = (await _repository.GetIntervals(effectiveAuthorization)).OrderBy(x => x.EffectiveInterval.Start).ToList <EffectiveAuthorizationInterval>();

            Assert.IsNotNull(retrievedIntervals);
            Assert.AreEqual(3, retrievedIntervals.Count);

            Assert.AreEqual(_tenantId, retrievedIntervals[0].TenantId);
            Assert.AreEqual(owner, retrievedIntervals[0].User.Key);
            Assert.AreEqual(permission, retrievedIntervals[0].Permission);
            Assert.AreEqual(target, retrievedIntervals[0].TargetPerson.Key);
            Assert.AreEqual(_dateTimeStart1, retrievedIntervals[0].EffectiveInterval.Start);
            Assert.AreEqual(_dateTimeEnd1, retrievedIntervals[0].EffectiveInterval.End);

            Assert.AreEqual(_tenantId, retrievedIntervals[1].TenantId);
            Assert.AreEqual(owner, retrievedIntervals[1].User.Key);
            Assert.AreEqual(permission, retrievedIntervals[1].Permission);
            Assert.AreEqual(target, retrievedIntervals[1].TargetPerson.Key);
            Assert.AreEqual(_dateTimeStart1.AddDays(1), retrievedIntervals[1].EffectiveInterval.Start);
            Assert.AreEqual(_dateTimeEnd1.AddDays(1), retrievedIntervals[1].EffectiveInterval.End);

            Assert.AreEqual(_tenantId, retrievedIntervals[2].TenantId);
            Assert.AreEqual(owner, retrievedIntervals[2].User.Key);
            Assert.AreEqual(permission, retrievedIntervals[2].Permission);
            Assert.AreEqual(target, retrievedIntervals[2].TargetPerson.Key);
            Assert.AreEqual(_dateTimeStart1.AddDays(2), retrievedIntervals[2].EffectiveInterval.Start);
            Assert.AreEqual(_dateTimeEnd1.AddDays(2), retrievedIntervals[2].EffectiveInterval.End);
        }