public ActionResult BulkCreate(int? templateId, int? callForProposalId, string bulkLoadEmails) { var emailsCreatedCount = 0; var notAddedCount = 0; Template template = null; CallForProposal callforProposal = null; var existingList = new List<string>(); var notAddedSb = new StringBuilder(); if (!_accessService.HasAccess(templateId, callForProposalId, CurrentUser.Identity.Name)) { Message = string.Format(StaticValues.Message_NoAccess, "that"); return this.RedirectToAction<HomeController>(a => a.Index()); } if (templateId.HasValue && templateId != 0) { template = Repository.OfType<Template>().GetNullableById(templateId.Value); existingList = _emailsforcallRepository.Queryable.Where(a => a.Template != null && a.Template == template).Select(a => a.Email).ToList(); } else if (callForProposalId.HasValue && callForProposalId != 0) { callforProposal = Repository.OfType<CallForProposal>().GetNullableById(callForProposalId.Value); existingList = _emailsforcallRepository.Queryable.Where(a => a.CallForProposal != null && a.CallForProposal == callforProposal).Select(a => a.Email).ToList(); } const string regexPattern = @"\b[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}\b"; // Find matches System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(bulkLoadEmails, regexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); // add each match foreach (System.Text.RegularExpressions.Match match in matches) { if (existingList.Contains(match.ToString().ToLower())) { notAddedCount++; notAddedSb.AppendLine(match.ToString().ToLower() + " == Email already exists in list"); } else { ModelState.Clear(); var emailsforcallToCreate = new EmailsForCall(match.ToString().ToLower()); emailsforcallToCreate.Template = template; emailsforcallToCreate.CallForProposal = callforProposal; emailsforcallToCreate.TransferValidationMessagesTo(ModelState); if (ModelState.IsValid) { _emailsforcallRepository.EnsurePersistent(emailsforcallToCreate); emailsCreatedCount++; existingList.Add(emailsforcallToCreate.Email); } else { notAddedCount++; var sbErr = new StringBuilder(); foreach (var result in ModelState.Values) { foreach (var errs in result.Errors) { sbErr.Append(errs.ErrorMessage); } } notAddedSb.AppendLine(string.Format("{0} == {1} \n", match.ToString().ToLower(), sbErr)); } } } if (notAddedCount > 0) { ModelState.Clear(); Message = string.Format("{0} EmailsForCall Created Successfully == {1} EmailsForCall Not Created", emailsCreatedCount, notAddedCount); var viewModel = EmailsForCallViewModel.Create(Repository, template, callforProposal); viewModel.BulkLoadEmails = notAddedSb.ToString(); return View(viewModel); } else { Message = string.Format("{0} EmailsForCall Created Successfully", emailsCreatedCount); return this.RedirectToAction(a => a.Index(templateId, callForProposalId)); } }
public ActionResult Edit(int id, int? templateId, int? callForProposalId, EmailsForCall emailsforcall) { Template template = null; CallForProposal callforProposal = null; if (!_accessService.HasAccess(templateId, callForProposalId, CurrentUser.Identity.Name)) { Message = string.Format(StaticValues.Message_NoAccess, "that"); return this.RedirectToAction<HomeController>(a => a.Index()); } var emailsforcallToEdit = _emailsforcallRepository.GetNullableById(id); if (emailsforcallToEdit == null) { Message = string.Format(StaticValues.Message_NotFound, "Emails For Call"); return this.RedirectToAction(a => a.Index(templateId, callForProposalId)); } if (!_accessService.HasSameId(emailsforcallToEdit.Template, emailsforcallToEdit.CallForProposal, templateId, callForProposalId)) { Message = string.Format(StaticValues.Message_NoAccess, "that"); return this.RedirectToAction<HomeController>(a => a.Index()); } //TransferValues(emailsforcall, emailsforcallToEdit); emailsforcallToEdit.Email = emailsforcall.Email.ToLower(); if (callForProposalId.HasValue && callForProposalId != 0) { emailsforcallToEdit.HasBeenEmailed = emailsforcall.HasBeenEmailed; } emailsforcallToEdit.TransferValidationMessagesTo(ModelState); if (templateId.HasValue && templateId != 0) { template = Repository.OfType<Template>().GetNullableById(templateId.Value); if (_emailsforcallRepository.Queryable.Where(a => a.Template != null && a.Template == template && a.Id != emailsforcallToEdit.Id && a.Email == emailsforcallToEdit.Email).Any()) { ModelState.AddModelError("Email", "Email already exists"); } } else if (callForProposalId.HasValue && callForProposalId != 0) { callforProposal = Repository.OfType<CallForProposal>().GetNullableById(callForProposalId.Value); if (_emailsforcallRepository.Queryable.Where(a => a.CallForProposal != null && a.CallForProposal == callforProposal && a.Id != emailsforcallToEdit.Id && a.Email == emailsforcallToEdit.Email).Any()) { ModelState.AddModelError("Email", "Email already exists"); } } if (ModelState.IsValid) { _emailsforcallRepository.EnsurePersistent(emailsforcallToEdit); Message = string.Format(StaticValues.Message_EditedSuccessfully, "Emails For Call"); return this.RedirectToAction(a => a.Index(templateId, callForProposalId)); } else { var viewModel = EmailsForCallViewModel.Create(Repository, emailsforcallToEdit.Template, emailsforcallToEdit.CallForProposal); viewModel.EmailsForCall = emailsforcallToEdit; return View(viewModel); } }
public void TestEditPostWithInvalidEmailDoesNotSave2() { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me"); AccessService.Expect(a => a.HasAccess(4, null, "Me")).Return(true).Repeat.Any(); SetupDataForTests1(); AccessService.Expect(a => a.HasSameId(EmailsForCallRepository.GetNullableById(10).Template, null, 4, null)).Return(true).Repeat.Any(); var updatedEmail = new EmailsForCall(); updatedEmail.Email = "blah@[email protected]"; updatedEmail.HasBeenEmailed = true; #endregion Arrange #region Act var result = Controller.Edit(10, 4, null, updatedEmail) .AssertViewRendered() .WithViewData<EmailsForCallViewModel>(); #endregion Act #region Assert Assert.IsNotNull(result); Assert.IsFalse(result.IsCallForProposal); Assert.AreEqual(0, result.CallForProposalId); Assert.IsTrue(result.IsTemplate); Assert.AreEqual(4, result.TemplateId); Assert.AreEqual(10, result.EmailsForCall.Id); Assert.AreEqual("blah@[email protected]".ToLower(), result.EmailsForCall.Email); Assert.IsFalse(result.EmailsForCall.HasBeenEmailed); //Template does not update this //Assert.AreEqual("", Controller.Message); Controller.ModelState.AssertErrorsAre("Email: not a well-formed email address"); AccessService.AssertWasCalled(a => a.HasAccess(Arg<int?>.Is.Anything, Arg<int?>.Is.Anything, Arg<string>.Is.Anything)); var args = AccessService.GetArgumentsForCallsMadeOn(a => a.HasAccess( Arg<int?>.Is.Anything, Arg<int?>.Is.Anything, Arg<string>.Is.Anything))[0]; Assert.AreEqual(4, args[0]); Assert.AreEqual(null, args[1]); Assert.AreEqual("Me", args[2]); AccessService.AssertWasCalled(a => a.HasSameId(Arg<Template>.Is.Anything, Arg<CallForProposal>.Is.Anything, Arg<int?>.Is.Anything, Arg<int?>.Is.Anything)); var args1 = AccessService.GetArgumentsForCallsMadeOn(a => a.HasSameId( Arg<Template>.Is.Anything, Arg<CallForProposal>.Is.Anything, Arg<int?>.Is.Anything, Arg<int?>.Is.Anything))[0]; Assert.AreEqual(4, ((Template)args1[0]).Id); Assert.AreEqual(null, args1[1]); Assert.AreEqual(4, args1[2]); Assert.AreEqual(null, args1[3]); #endregion Assert }
public void TestEditPostWithSameEmailDoesSave2() { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me"); AccessService.Expect(a => a.HasAccess(4, null, "Me")).Return(true).Repeat.Any(); SetupDataForTests1(); AccessService.Expect(a => a.HasSameId(EmailsForCallRepository.GetNullableById(10).Template, null, 4, null)).Return(true).Repeat.Any(); var updatedEmail = new EmailsForCall(); updatedEmail.Email = "*****@*****.**"; updatedEmail.HasBeenEmailed = true; #endregion Arrange #region Act var result = Controller.Edit(10, 4, null, updatedEmail) .AssertActionRedirect() .ToAction<EmailsForCallController>(a => a.Index(null, null)); #endregion Act #region Assert Assert.IsNotNull(result); Assert.AreEqual("templateId", result.RouteValues.ElementAt(2).Key); Assert.AreEqual(4, result.RouteValues.ElementAt(2).Value); Assert.AreEqual("callForProposalId", result.RouteValues.ElementAt(3).Key); Assert.AreEqual(null, result.RouteValues.ElementAt(3).Value); Assert.AreEqual("Emails For Call Edited Successfully.", Controller.Message); EmailsForCallRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<EmailsForCall>.Is.Anything)); var args2 = (EmailsForCall) EmailsForCallRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<EmailsForCall>.Is.Anything))[0][0]; Assert.IsNotNull(args2); Assert.IsFalse(args2.HasBeenEmailed); Assert.AreEqual("*****@*****.**".ToLower(), args2.Email); Assert.AreEqual(null, args2.CallForProposal); Assert.AreEqual(4, args2.Template.Id); AccessService.AssertWasCalled(a => a.HasAccess(Arg<int?>.Is.Anything, Arg<int?>.Is.Anything, Arg<string>.Is.Anything)); var args = AccessService.GetArgumentsForCallsMadeOn(a => a.HasAccess( Arg<int?>.Is.Anything, Arg<int?>.Is.Anything, Arg<string>.Is.Anything))[0]; Assert.AreEqual(4, args[0]); Assert.AreEqual(null, args[1]); Assert.AreEqual("Me", args[2]); AccessService.AssertWasCalled(a => a.HasSameId(Arg<Template>.Is.Anything, Arg<CallForProposal>.Is.Anything, Arg<int?>.Is.Anything, Arg<int?>.Is.Anything)); var args1 = AccessService.GetArgumentsForCallsMadeOn(a => a.HasSameId( Arg<Template>.Is.Anything, Arg<CallForProposal>.Is.Anything, Arg<int?>.Is.Anything, Arg<int?>.Is.Anything))[0]; Assert.AreEqual(4, ((Template)args1[0]).Id); Assert.AreEqual(null, args1[1]); Assert.AreEqual(4, args1[2]); Assert.AreEqual(null, args1[3]); #endregion Assert }
public void TestEditPostWithDuplicateEmailDoesNotSave1() { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me"); AccessService.Expect(a => a.HasAccess(null, 3, "Me")).Return(true).Repeat.Any(); SetupDataForTests1(); AccessService.Expect(a => a.HasSameId(null, EmailsForCallRepository.GetNullableById(5).CallForProposal, null, 3)).Return(true).Repeat.Any(); var updatedEmail = new EmailsForCall(); updatedEmail.Email = "*****@*****.**"; updatedEmail.HasBeenEmailed = true; #endregion Arrange #region Act var result = Controller.Edit(5, null, 3, updatedEmail) .AssertViewRendered() .WithViewData<EmailsForCallViewModel>(); #endregion Act #region Assert Assert.IsNotNull(result); Assert.IsTrue(result.IsCallForProposal); Assert.AreEqual(3, result.CallForProposalId); Assert.IsFalse(result.IsTemplate); Assert.AreEqual(0, result.TemplateId); Assert.AreEqual(5, result.EmailsForCall.Id); Assert.AreEqual("*****@*****.**".ToLower(), result.EmailsForCall.Email); Assert.IsTrue(result.EmailsForCall.HasBeenEmailed); //Assert.AreEqual("", Controller.Message); Controller.ModelState.AssertErrorsAre("Email already exists"); AccessService.AssertWasCalled(a => a.HasAccess(Arg<int?>.Is.Anything, Arg<int?>.Is.Anything, Arg<string>.Is.Anything)); var args = AccessService.GetArgumentsForCallsMadeOn(a => a.HasAccess( Arg<int?>.Is.Anything, Arg<int?>.Is.Anything, Arg<string>.Is.Anything))[0]; Assert.AreEqual(null, args[0]); Assert.AreEqual(3, args[1]); Assert.AreEqual("Me", args[2]); AccessService.AssertWasCalled(a => a.HasSameId(Arg<Template>.Is.Anything, Arg<CallForProposal>.Is.Anything, Arg<int?>.Is.Anything, Arg<int?>.Is.Anything)); var args1 = AccessService.GetArgumentsForCallsMadeOn(a => a.HasSameId( Arg<Template>.Is.Anything, Arg<CallForProposal>.Is.Anything, Arg<int?>.Is.Anything, Arg<int?>.Is.Anything))[0]; Assert.AreEqual(null, args1[0]); Assert.AreEqual(3, ((CallForProposal)args1[1]).Id); Assert.AreEqual(null, args1[2]); Assert.AreEqual(3, args1[3]); #endregion Assert }