public async Task GetAggregateReportSummaryFillsInHolesInDataset()
        {
            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) },
                { 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(0));
            Assert.That(aggregateSummary.Results.Values.ElementAt(2).FullyTrusted, Is.EqualTo(0));
            Assert.That(aggregateSummary.Results.Values.ElementAt(3).FullyTrusted, Is.EqualTo(4));
        }
Exemplo n.º 2
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));
        }