private ShareNotificationViewModel PrepareReturnModel(ShareNotificationViewModel model)
        {
            model.EmailAddress = string.Empty;
            model.SetSharedUsers(model.SelectedSharedUsers);

            ModelState.Clear();
            return(model);
        }
        public void Add_Email_EmailNotValidFormat()
        {
            var model = new ShareNotificationViewModel(this.notificationId);

            model.EmailAddress = "fake";

            Assert.True(ViewModelValidator.ValidateViewModel(model).Count > 0);
        }
        public void Add_Email_MaximumEmailCountReached()
        {
            var model = new ShareNotificationViewModel(this.notificationId);

            model.SelectedSharedUsers = this.CreateSharedUserList(5);

            Assert.True(ViewModelValidator.ValidateViewModel(model).Count > 0);
        }
        public ActionResult ShareNotification(Guid id)
        {
            this.CheckSharedDataIsValid();

            var sharedUsers = (List <NotificationSharedUser>)TempData["SharedUsers"];
            var model       = new ShareNotificationViewModel(id, sharedUsers);

            this.PrepareReturnModel(model);
            return(View(model));
        }
        public async Task Add_Email_SuccessfullyAddsToList()
        {
            var model = new ShareNotificationViewModel(this.notificationId);

            model.EmailAddress = this.externalEmail;

            var result = await shareNotificationOptionController.ShareNotification(this.notificationId, model, "addshareduser", string.Empty) as ViewResult;

            var resultModel = result.Model as ShareNotificationViewModel;

            Assert.Single(resultModel.SelectedSharedUsers);
        }
        public async Task Remove_Email_SuccessfullyRemovesFromList()
        {
            var model = new ShareNotificationViewModel(this.notificationId);

            model.SelectedSharedUsers = this.CreateSharedUserList(1);
            model.EmailAddress        = model.SelectedSharedUsers[0].Email;

            var result = await shareNotificationOptionController.ShareNotification(this.notificationId, model, string.Empty, model.SelectedSharedUsers[0].UserId.ToString()) as ViewResult;

            var resultModel = result.Model as ShareNotificationViewModel;

            Assert.Empty(resultModel.SelectedSharedUsers);
        }
        public async Task Add_Email_AlreadyInList_DoesntAdd()
        {
            var model = new ShareNotificationViewModel(this.notificationId);

            model.SelectedSharedUsers = this.CreateSharedUserList(1);

            model.EmailAddress = model.SelectedSharedUsers[0].Email;

            var result = await shareNotificationOptionController.ShareNotification(this.notificationId, model, "addshareduser", string.Empty) as ViewResult;

            var resultModel = result.Model as ShareNotificationViewModel;

            Assert.Single(resultModel.SelectedSharedUsers);
        }
        public async Task Add_UnknownEmail_ReturnsError()
        {
            A.CallTo(
                () =>
                mediator.SendAsync(A <GetUserId> .That.Matches(p => p.EmailAddress == "*****@*****.**")))
            .Throws(new Exception());

            var model = new ShareNotificationViewModel(this.notificationId);

            model.EmailAddress = "*****@*****.**";

            var result = await shareNotificationOptionController.ShareNotification(this.notificationId, model, "addshareduser", string.Empty) as ViewResult;

            Assert.True(result.ViewData.ModelState.Count == 1, "Enter a valid email address");
        }
        public async Task Add_InternalEmail_ReturnsError()
        {
            A.CallTo(
                () =>
                mediator.SendAsync(A <ExternalUserExists> .That.Matches(p => p.EmailAddress == internalEmail)))
            .Returns(false);

            var model = new ShareNotificationViewModel(this.notificationId);

            model.EmailAddress = this.internalEmail;

            var result = await shareNotificationOptionController.ShareNotification(this.notificationId, model, "addshareduser", string.Empty) as ViewResult;

            Assert.True(result.ViewData.ModelState.Count == 1, "Email address can't be an internal user");
        }
        public async Task Add_UserAlreadyShared_ReturnsError()
        {
            List <NotificationSharedUser> users = new List <NotificationSharedUser>()
            {
                new NotificationSharedUser()
                {
                    UserId = this.userId.ToString()
                }
            };

            A.CallTo(
                () =>
                mediator.SendAsync(A <GetSharedUsersByNotificationId> .That.Matches(p => p.NotificationId == this.notificationId)))
            .Returns(users);

            var model = new ShareNotificationViewModel(this.notificationId, users);

            var result = await shareNotificationOptionController.ShareNotification(this.notificationId, model, "addshareduser", null) as ViewResult;

            Assert.True(result.ViewData.ModelState.Count == 1, "This email address has already been added as a shared user");
        }
        public async Task <ActionResult> ShareNotification(Guid id, ShareNotificationViewModel model, string command, string removeId)
        {
            // User is removing a user from the list
            if (removeId != null)
            {
                model.SelectedSharedUsers.RemoveAll(c => c.UserId.ToString() == removeId);
            }

            // Use is finished adding emails and proceeding to next page
            if (command == "continue")
            {
                SharedUserListConfirmViewModel confirmViewModel = new SharedUserListConfirmViewModel(id, model.SelectedSharedUsers);

                if (confirmViewModel.SharedUsers.Count == 0)
                {
                    ModelState.Clear();
                    ModelState.AddModelError("Continue", "Please enter at least 1 email address to continue");
                    return(View(model));
                }

                TempData["SharedUsers"] = confirmViewModel.SharedUsers;
                return(RedirectToAction("Confirm", "ShareNotification", new { id = id }));
            }

            // User has tried to add an email address
            if (command == "addshareduser")
            {
                // Check validation of model for correct number of email addresses and email address in correct format
                if (!ModelState.IsValid)
                {
                    model.SetSharedUsers(model.SelectedSharedUsers);
                    return(View(model));
                }

                // Check that the owner of notification isn't trying to share it with themselves
                if (User.GetEmailAddress() == model.EmailAddress)
                {
                    model = this.PrepareModelWithErrors("Email Address", "Cannot share notification with your email address", model);
                    return(View(model));
                }

                Guid userId;
                // This method doesn't handle nicely so if a user doesn't exist, it throws an exception.
                try
                {
                    userId = await mediator.SendAsync(new GetUserId(model.EmailAddress));
                }
                catch (Exception)
                {
                    model = this.PrepareModelWithErrors("Email Address", "Enter a valid email address", model);
                    return(View(model));
                }

                var isInternalUser = await mediator.SendAsync(new ExternalUserExists(model.EmailAddress));

                if (!isInternalUser)
                {
                    model = this.PrepareModelWithErrors("Email Address", "You cannot share this notification with a competent authority user", model);
                    return(View(model));
                }

                var existingSharedUsers = await mediator.SendAsync(new GetSharedUsersByNotificationId(id));

                if (existingSharedUsers.Count(p => p.UserId == userId.ToString()) > 0)
                {
                    model = this.PrepareModelWithErrors("Email Address", "This email address has already been added as a shared user", model);

                    return(View(model));
                }

                // If user id is not already in list then add it to the list
                if (userId != null && model.SelectedSharedUsers.Count(p => p.UserId == userId.ToString()) == 0)
                {
                    model.SelectedSharedUsers.Add(new NotificationSharedUser {
                        Email = model.EmailAddress, UserId = userId.ToString(), NotificationId = model.NotificationId
                    });
                }
            }

            this.PrepareReturnModel(model);
            return(View(model));
        }
        private ShareNotificationViewModel PrepareModelWithErrors(string area, string errorMessage, ShareNotificationViewModel model)
        {
            model.SetSharedUsers(model.SelectedSharedUsers);
            ModelState.AddModelError(area, errorMessage);

            return(model);
        }