Exemple #1
0
        protected override async Task <Response> CreateInternalResponseAsync(
            DateRangeDomainRequest request)
        {
            AggregatedStatistics aggregatedStatistics = await AggregateReportApiDao
                                                        .GetAggregatedComplianceStatisticsAsync(request.BeginDateUtc.Value, request.EndDateUtc.Value,
                                                                                                request.DomainId);

            return(new AggregatedStatisticsResponse(aggregatedStatistics.Values));
        }
        protected override async Task <Response> CreateInternalResponseAsync(
            DateRangeDomainRequest request)
        {
            DailyStatistics dailyStatistics = await AggregateReportApiDao
                                              .GetDailyDispositionStatisticsAsync(request.BeginDateUtc.Value, request.EndDateUtc.Value,
                                                                                  request.DomainId);

            return(new DailyStatisticsResponse(dailyStatistics.Values));
        }
Exemple #3
0
        public async Task GetAggregateReportSummaryReturnsForbidWhenUnknownUser()
        {
            DateRangeDomainRequest request = new DateRangeDomainRequest();

            ValidationResult validationResult = new ValidationResult(new List <ValidationFailure>());

            A.CallTo(() => _dateRangeDomainRequestValidator.ValidateAsync(request, CancellationToken.None)).Returns(Task.FromResult(validationResult));

            IActionResult result = await _domainStatusController.GetAggregateReportSummary(request);

            Assert.That(result, Is.TypeOf <ForbidResult>());
        }
        public async Task GetAggregateReportSummaryReturnsResults()
        {
            DateTime now     = DateTime.Now.Date;
            DateTime dayOne  = now.AddDays(-3);
            DateTime dayFour = now;

            DateRangeDomainRequest request = new DateRangeDomainRequest {
                Id = 1, StartDate = dayOne, EndDate = dayFour
            };

            ValidationResult validationResult = new ValidationResult(new List <ValidationFailure>());

            A.CallTo(() => _dateRangeDomainRequestValidator.ValidateAsync(request, CancellationToken.None))
            .Returns(Task.FromResult(validationResult));

            SetSid("1", _domainStatusController);

            A.CallTo(() => _permissionDao.GetPermissions(A <int> ._, A <int> ._))
            .Returns(Task.FromResult(new DomainPermissions(1, true, true)));

            A.CallTo(() => _domainStatusDao.GetAggregateReportSummary(A <int> ._, A <DateTime> ._, A <DateTime> ._, false))
            .Returns(Task.FromResult(new SortedDictionary <DateTime, AggregateSummaryItem>
            {
                { dayOne, new AggregateSummaryItem(1, 1, 1, 1, 1) },
                { dayOne.AddDays(1), new AggregateSummaryItem(2, 2, 2, 2, 2) },
                { dayOne.AddDays(2), new AggregateSummaryItem(3, 3, 3, 3, 3) },
                { dayFour, new AggregateSummaryItem(4, 4, 4, 4, 4) }
            }));

            IActionResult result = await _domainStatusController.GetAggregateReportSummary(request);

            Assert.That(result, Is.TypeOf <ObjectResult>());

            ObjectResult objectResult = result as ObjectResult;

            Assert.That(objectResult.Value, Is.TypeOf <AggregateSummary>());

            AggregateSummary aggregateSummary = objectResult.Value as AggregateSummary;

            Assert.That(aggregateSummary.Results.Count, Is.EqualTo(4));

            Assert.That(aggregateSummary.Results.Keys.ElementAt(0), Is.EqualTo(dayOne));
            Assert.That(aggregateSummary.Results.Keys.ElementAt(1), Is.EqualTo(dayOne.AddDays(1)));
            Assert.That(aggregateSummary.Results.Keys.ElementAt(2), Is.EqualTo(dayOne.AddDays(2)));
            Assert.That(aggregateSummary.Results.Keys.ElementAt(3), Is.EqualTo(dayFour));

            Assert.That(aggregateSummary.Results.Values.ElementAt(0).FullyTrusted, Is.EqualTo(1));
            Assert.That(aggregateSummary.Results.Values.ElementAt(1).FullyTrusted, Is.EqualTo(2));
            Assert.That(aggregateSummary.Results.Values.ElementAt(2).FullyTrusted, Is.EqualTo(3));
            Assert.That(aggregateSummary.Results.Values.ElementAt(3).FullyTrusted, Is.EqualTo(4));
        }
Exemple #5
0
        public async Task GetAggregateReportSummaryReturnsBadRequestWhenRequestIsInvalid()
        {
            DateRangeDomainRequest request = new DateRangeDomainRequest();

            ValidationResult validationResult = new ValidationResult(new List <ValidationFailure> {
                new ValidationFailure(string.Empty, string.Empty)
            });

            A.CallTo(() => _dateRangeDomainRequestValidator.ValidateAsync(request, CancellationToken.None)).Returns(Task.FromResult(validationResult));

            IActionResult result = await _domainStatusController.GetAggregateReportSummary(request);

            Assert.That(result, Is.TypeOf <BadRequestObjectResult>());
        }
Exemple #6
0
        public async Task GetAggregateReportSummaryReturnsNotFoundWhenUserCantViewDomain()
        {
            DateRangeDomainRequest request = new DateRangeDomainRequest();

            ValidationResult validationResult = new ValidationResult(new List <ValidationFailure>());

            A.CallTo(() => _dateRangeDomainRequestValidator.ValidateAsync(request, CancellationToken.None)).Returns(Task.FromResult(validationResult));

            SetSid("1", _domainStatusController);

            A.CallTo(() => _permissionDao.GetPermissions(A <int> ._, A <int> ._)).Returns(Task.FromResult(new DomainPermissions(1, false, false)));

            IActionResult result = await _domainStatusController.GetAggregateReportSummary(request);

            Assert.That(result, Is.TypeOf <NotFoundObjectResult>());
        }
        public async Task <IActionResult> GetDateRangeDomainResult <T>(DateRangeDomainRequest dateRangeDomainRequest,
                                                                       Func <int, DateTime, DateTime, int?, Task <T> > resultGetter)
        {
            ValidationResult validationResult = await _dateRangeDomainValidator.ValidateAsync(dateRangeDomainRequest);

            if (!validationResult.IsValid)
            {
                _log.LogWarning($"Bad request: {validationResult.GetErrorString()}");
                return(BadRequest(new ErrorResponse(validationResult.GetErrorString())));
            }

            Claim roleClaim = User.FindFirst(_ => _.Type == ClaimTypes.Role);

            if (roleClaim.Value == RoleType.Unauthorised)
            {
                return(Forbid());
            }

            int userId = GetUserId(User);

            if (dateRangeDomainRequest.DomainId.HasValue)
            {
                bool domainExists = await _domainsDao.DomainExists(userId, dateRangeDomainRequest.DomainId.Value);

                if (!domainExists)
                {
                    _log.LogWarning($"No domain exists with id for user {userId}: {dateRangeDomainRequest.DomainId.Value}");
                    return(NotFound(new ErrorResponse("Domain not found.", ErrorStatus.Information)));
                }
            }

            T result = await resultGetter(userId, dateRangeDomainRequest.BeginDateUtc.Value,
                                          dateRangeDomainRequest.EndDateUtc.Value, dateRangeDomainRequest.DomainId);

            return(new ObjectResult(result));
        }
Exemple #8
0
 public Task <IActionResult> GetDisposition(DateRangeDomainRequest dateRangeDomainRequest)
 {
     return(GetDateRangeDomainResult(dateRangeDomainRequest,
                                     _aggregatedStatisticsDao.GetAggregatedDispositionStatisticsAsync));
 }
Exemple #9
0
 public Task <IActionResult> GetCompliance(DateRangeDomainRequest dateRangeDomainRequest)
 {
     return(GetDateRangeDomainResult(dateRangeDomainRequest,
                                     _aggregatedStatisticsDao.GetAggregatedComplianceStatisticsAsync));
 }
        public async Task Test(DateRangeDomainRequest dateRangeDomainRequest, bool expectedToBeValid)
        {
            ValidationResult validationResult = await _dateRangeRequestValidator.ValidateAsync(dateRangeDomainRequest);

            Assert.That(validationResult.IsValid, Is.EqualTo(expectedToBeValid));
        }
 public Task <IActionResult> GetDisposition(DateRangeDomainRequest dateRangeDomainRequest)
 {
     return(GetDateRangeDomainResult(dateRangeDomainRequest, _dailyStatisticsDao.GetDailyDispositionStatisticsAsync));
 }
 public Task <IActionResult> GetTrust(DateRangeDomainRequest dateRangeDomainRequest)
 {
     return(GetDateRangeDomainResult(dateRangeDomainRequest, _dailyStatisticsDao.GetDailyTrustStatisticsAsync));
 }
Exemple #13
0
        public async Task <IActionResult> GetAggregateReportSummary(DateRangeDomainRequest request)
        {
            ValidationResult validationResult = await _dateRangeDomainRequestValidator.ValidateAsync(request);

            if (!validationResult.IsValid)
            {
                _log.LogWarning($"Bad request: {validationResult.GetErrorString()}.");
                return(BadRequest(new ErrorResponse(validationResult.GetErrorString())));
            }

            int?userId = User.GetId();

            if (!userId.HasValue)
            {
                _log.LogWarning("Forbid unknown user.");
                return(Forbid());
            }

            DomainPermissions domainPermissions = await _permissionDao.GetPermissions(userId.Value, request.Id);

            if (!domainPermissions.DomainPermission)
            {
                _log.LogWarning($"Domain {request.Id} not found for user {userId}.");
                return(NotFound(new ErrorResponse($"No domain found for ID {request.Id}.")));
            }

            if (!domainPermissions.AggregatePermission)
            {
                _log.LogWarning($"Forbid user {userId}");
                return(Forbid());
            }

            bool.TryParse(HttpContext.Request.Query["includeSubdomains"].ToString(), out bool includeSubdomains);

            Task <SortedDictionary <DateTime, AggregateSummaryItem> > getInfos = _domainStatusDao.GetAggregateReportSummary(request.Id, request.StartDate, request.EndDate, includeSubdomains);
            Task <int> getEmailCount = _domainStatusDao.GetAggregateReportTotalEmailCount(request.Id, request.StartDate, request.EndDate, includeSubdomains);

            await Task.WhenAll(getInfos, getEmailCount);

            SortedDictionary <DateTime, AggregateSummaryItem> infoResults = getInfos.Result;

            if (!infoResults.Any())
            {
                return(new ObjectResult(null));
            }

            int days = request.EndDate.Subtract(request.StartDate).Days + 1;

            if (days > infoResults.Count)
            {
                List <DateTime> datesRange = Enumerable.Range(0, days).Select(_ => new DateTime(request.StartDate.Ticks).AddDays(_)).ToList();
                foreach (DateTime date in datesRange)
                {
                    if (!infoResults.TryGetValue(date, out AggregateSummaryItem value))
                    {
                        _log.LogDebug($"Added empty entry for date {date}");
                        infoResults[date] = new AggregateSummaryItem(0, 0, 0, 0, 0);
                    }
                }
            }

            AggregateSummary aggregateSummary = new AggregateSummary(infoResults, getEmailCount.Result);

            return(new ObjectResult(aggregateSummary));
        }
Exemple #14
0
        public void Test(DateRangeDomainRequest request, bool valid)
        {
            ValidationResult result = _dateRangeDomainRequestValidator.Validate(request);

            Assert.That(result.IsValid, Is.EqualTo(valid));
        }
 public Task <IActionResult> GetUntrustedSenders(DateRangeDomainRequest dateRangeDomainRequest)
 {
     return(GetDateRangeDomainResult(dateRangeDomainRequest, _senderStatisticsDao.GetUntrustedSenderStatistics));
 }