Example #1
0
        public async void AddAccessRequestAsync_InvalidAgencies_BadRequest()
        {
            // Arrange
            var user       = PrincipalHelper.CreateForRole();
            var helper     = new TestHelper();
            var controller = helper.CreateController <UserController>(user);

            var service = helper.GetService <Mock <IPimsService> >();
            var mapper  = helper.GetService <IMapper>();

            service.Setup(m => m.User.AddAccessRequest(It.IsAny <Entity.AccessRequest>()));

            var accessRequest = new Model.AccessRequestModel()
            {
                Agencies = new List <Model.AccessRequestAgencyModel>(),
                Roles    = new List <Model.AccessRequestRoleModel>(new[] { new Model.AccessRequestRoleModel() })
            };
            var model = mapper.Map <Model.AccessRequestModel>(accessRequest);

            // Act
            // Assert
            await Assert.ThrowsAsync <BadRequestException>(async() => await controller.AddAccessRequestAsync(model));

            service.Verify(m => m.User.AddAccessRequest(It.IsAny <Entity.AccessRequest>()), Times.Never());
        }
Example #2
0
        public IActionResult UpdateAccessRequest(int id, [FromBody] Model.AccessRequestModel model)
        {
            if (model == null || model.Agencies == null || model.Roles == null)
            {
                throw new BadRequestException("Invalid access request specified");
            }
            if (model.Agencies.Count() != 1)
            {
                throw new BadRequestException("Each access request can only contain one agency.");
            }
            if (model.Roles.Count() != 1)
            {
                throw new BadRequestException("Each access request can only contain one role.");
            }
            var accessRequest = _mapper.Map <Entity.AccessRequest>(model);

            _pimsService.User.UpdateAccessRequest(accessRequest);
            return(new JsonResult(_mapper.Map <Model.AccessRequestModel>(accessRequest)));
        }
Example #3
0
        public async Task <IActionResult> AddAccessRequestAsync([FromBody] Model.AccessRequestModel model)
        {
            if (model == null || model.Agencies == null || model.Roles == null)
            {
                throw new BadRequestException("Invalid access request specified");
            }
            if (model.Agencies.Count() != 1)
            {
                throw new BadRequestException("Each access request can only contain one agency.");
            }
            if (model.Roles.Count() != 1)
            {
                throw new BadRequestException("Each access request can only contain one role.");
            }
            var accessRequest = _mapper.Map <Entity.AccessRequest>(model);

            _pimsService.User.AddAccessRequest(accessRequest);

            // Send notification to administrators and RPD mailbox
            try
            {
                var template       = _pimsService.NotificationTemplate.Get(_options.AccessRequest.NotificationTemplate);
                var templateForRPD = _pimsService.NotificationTemplate.Get(_options.AccessRequest.RPDNotificationTemplate);
                var administrators = _pimsService.User.GetAdmininstrators(accessRequest.Agencies.Select(a => a.AgencyId).ToArray());
                var notification   = _pimsService.NotificationQueue.GenerateNotification(
                    String.Join(";", administrators.Select(a => a.Email).Concat(new[] { _options.AccessRequest.SendTo }).Where(e => !String.IsNullOrWhiteSpace(e))),
                    template,
                    new AccessRequestNotificationModel(accessRequest, _options));
                await _pimsService.NotificationQueue.SendNotificationsAsync(new[] { notification });

                var notificationForRPD = _pimsService.NotificationQueue.GenerateNotification(templateForRPD, new AccessRequestNotificationModel(accessRequest, _options));
                await _pimsService.NotificationQueue.SendNotificationsAsync(new[] { notificationForRPD });
            }
            catch (Exception ex)
            {
                // Ignore email errors.
                _logger.LogError(ex, "Error occurred while attempting to send an access request notification to administrators.");
            }

            return(CreatedAtAction(nameof(GetAccessRequest), new { id = accessRequest.Id }, _mapper.Map <Model.AccessRequestModel>(accessRequest)));
        }
Example #4
0
        public void UpdateAccessRequest_NullRoles_BadRequest()
        {
            // Arrange
            var user       = PrincipalHelper.CreateForRole();
            var helper     = new TestHelper();
            var controller = helper.CreateController <UserController>(user);

            var service = helper.GetService <Mock <IPimsService> >();
            var mapper  = helper.GetService <IMapper>();

            service.Setup(m => m.User.UpdateAccessRequest(It.IsAny <Entity.AccessRequest>()));

            var accessRequest = new Model.AccessRequestModel()
            {
                Agencies = new List <Model.AccessRequestAgencyModel>(),
                Roles    = null
            };
            var model = mapper.Map <Model.AccessRequestModel>(accessRequest);

            // Act
            // Assert
            Assert.Throws <BadRequestException>(() => controller.UpdateAccessRequest(model.Id, model));
            service.Verify(m => m.User.UpdateAccessRequest(It.IsAny <Entity.AccessRequest>()), Times.Never());
        }