public HttpResponseMessage Post(AppModel app) { var savedApp = _appService.CreateApp(app); var response = Request.CreateResponse(HttpStatusCode.Created, savedApp); string link = Url.Link(RouteNames.DefaultApi, new { controller = "apps", id = 1 }); response.Headers.Location = new Uri(link); return response; }
public void AppService_EmptyModelValidation() { var badApp = new AppModel(); try { var service = new AppService(DocumentStore, _MockEmailService.Object); service.CreateApp(badApp); } catch (ServiceValidationException serviceEx) { Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.App_MissingName)); Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.App_MissingContactEmail)); throw; } }
public void AppService_DuplicateEmailValidation() { var badApp = new AppModel { Name = "MyApp", ContactEmails = {"*****@*****.**", "*****@*****.**"} }; try { var service = new AppService(DocumentStore, _MockEmailService.Object); service.CreateApp(badApp); } catch (ServiceValidationException serviceEx) { Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.App_DuplicateContactEmails)); throw; } }
public AppModel CreateApp(AppModel appModel) { ThrowOnInvalidAppModel(appModel); using (var session = _documentStore.OpenSession()) { var contactEmails = TrimmedEmails(appModel.ContactEmails) .Select( address => new ContactEmail { EmailAddress = address, ConfirmationCode = KeyGenerator.Generate(), Confirmed = false }) .ToList(); foreach (var contactEmail in contactEmails) session.Store(contactEmail); var newApp = new App { Name = appModel.Name.Trim(), ApiKey = KeyGenerator.Generate(), CreatedTimestampUtc = DateTime.UtcNow, ContactEmailIds = contactEmails.Select(ce => ce.Id).ToList() }; session.Store(newApp); session.SaveChanges(); // send confirmation emails _emailService.SendConfirmationEmails(contactEmails); // output new model return new AppModel { Id = newApp.Id, Name = newApp.Name, ContactEmails = contactEmails.Select(ce => ce.EmailAddress).ToList(), ApiKey = newApp.ApiKey }; } }
public void HomeRender_Post_Create_Success() { // Arrange var testModel = new AppModel { Name = "App Name", ContactEmails = {"*****@*****.**", "*****@*****.**"} }; var mockAppService = new Mock<IAppService>(); mockAppService .Setup(service => service.CreateApp(testModel)) .Returns(() => new AppModel { Id = "apps/1", Name = "App Name", ContactEmails = {"*****@*****.**", "*****@*****.**"}, ApiKey = "apiKey" }); var appsController = new AppsController(mockAppService.Object); WebApiControllerHelper.MakeTestable(appsController, "apps"); // Act var response = appsController.Post(testModel); //WebViewPage<AppModel> complete = new ErrorGun.Web.Views.App.Complete(); //var doc = complete.RenderAsHtml((AppModel) response.Content.ReadAsStringAsync()); //// Assert //var h1 = doc.DocumentNode.SelectSingleNode("//h1"); //Assert.AreEqual("App Created", h1.InnerHtml.Trim()); // TODO: assert model properties correctly rendered }
public void AppService_OneGoodOneBadEmailValidation() { var badApp = new AppModel { Name = "MyApp", ContactEmails = {"*****@*****.**", "not_an_email_address"} }; try { var service = new AppService(DocumentStore, _MockEmailService.Object); service.CreateApp(badApp); } catch (ServiceValidationException serviceEx) { Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.App_InvalidEmailFormat)); throw; } }
public void AppService_ZZCompleteRoundTripWithConfirmation() { var appService = new AppService(DocumentStore, _MockEmailService.Object); // Save via service var validApp = new AppModel { Name = "MyApp", ContactEmails = {"*****@*****.**", "*****@*****.**"} }; var savedApp = appService.CreateApp(validApp); string savedAppId = savedApp.Id; // Verify app and contact emails are saved List<string> emailConfirmCodes; using (var session = DocumentStore.OpenSession()) { App loadedApp = session .Include<App>(a => a.ContactEmailIds) .Load<App>(savedAppId); Assert.IsNotNull(loadedApp); Assert.AreEqual("MyApp", loadedApp.Name); Assert.IsNotNull(loadedApp.ApiKey); Assert.AreNotEqual(default(DateTime), loadedApp.CreatedTimestampUtc); var loadedEmails = session.Load<ContactEmail>(loadedApp.ContactEmailIds); foreach (var email in loadedEmails) { TestContext.WriteLine("Loaded email with confirm code " + email.ConfirmationCode); } Assert.AreEqual(2, loadedEmails.Length); Assert.IsTrue(loadedEmails.Any(ce => ce.EmailAddress == "*****@*****.**")); Assert.IsTrue(loadedEmails.Any(ce => ce.EmailAddress == "*****@*****.**")); Assert.IsFalse(loadedEmails.Any(ce => ce.Confirmed)); Assert.IsFalse(loadedEmails.Any(ce => String.IsNullOrEmpty(ce.ConfirmationCode))); // Verify confirmation emails were sent _MockEmailService.Verify(s => s.SendConfirmationEmails(It.IsAny<IEnumerable<ContactEmail>>())); emailConfirmCodes = loadedEmails.Select(e => e.ConfirmationCode).ToList(); } // Confirm the contact emails: should not be already confirmed foreach (var confirmCode in emailConfirmCodes) { TestContext.WriteLine("Confirming email with code " + confirmCode); var confirmModel = appService.ConfirmEmail(confirmCode); Assert.IsTrue(confirmModel.Confirmed); Assert.IsFalse(confirmModel.AlreadyConfirmed); } // Re-confirm the contact emails: should be already confirmed foreach (var confirmCode in emailConfirmCodes) { var confirmModel = appService.ConfirmEmail(confirmCode); Assert.IsTrue(confirmModel.Confirmed); Assert.IsTrue(confirmModel.AlreadyConfirmed); } }
private static void ThrowOnInvalidAppModel(AppModel appModel) { var errorCodes = new List<ErrorCode>(); if (appModel == null) { errorCodes.Add(ErrorCode.App_MissingAppModel); } else { if (string.IsNullOrWhiteSpace(appModel.Name)) errorCodes.Add(ErrorCode.App_MissingName); var emails = TrimmedEmails(appModel.ContactEmails); if (emails.Count == 0) { errorCodes.Add(ErrorCode.App_MissingContactEmail); } else { if (emails.Any(email => !EmailValidator.Validate(email))) errorCodes.Add(ErrorCode.App_InvalidEmailFormat); if (emails.HasDuplicate()) errorCodes.Add(ErrorCode.App_DuplicateContactEmails); } } if (errorCodes.Count > 0) throw new ServiceValidationException(errorCodes); }