public void ErrorsController_Post_Roundtrip() { var errorReport = new ErrorReport { AppId = "apps/1", Category = "category", Detail = "detail", Message = "message", Source = "source", ReportedTimestampUtc = DateTime.UtcNow, UserEmail = "*****@*****.**" }; var mockErrorService = new Mock<IErrorService>(); mockErrorService .Setup( s => s.ReportError(It.IsAny<ErrorReport>(), "api-key")) .Returns(errorReport); var controller = new ErrorsController(mockErrorService.Object); WebApiControllerHelper.MakeTestable(controller, "errors"); var response = controller.Post(errorReport, "api-key"); var responseErrorTask = response.Content.ReadAsAsync<ErrorReport>(); responseErrorTask.Wait(); var responseError = responseErrorTask.Result; Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.AreEqual("apps/1", responseError.AppId); }
public ErrorReport ReportError(ErrorReport errorReport, string apiKey) { using (var session = _documentStore.OpenSession()) { // load app App app = null; if (errorReport != null && !String.IsNullOrWhiteSpace(errorReport.AppId)) { app = session .Include<App>(a => a.ContactEmailIds) .Load<App>(errorReport.AppId); } // validate ThrowOnInvalidErrorReport(errorReport, apiKey, app); // store var errorReportToStore = new ErrorReport { AppId = errorReport.AppId, Category = errorReport.Category, Detail = errorReport.Detail, Message = errorReport.Message, Source = errorReport.Source, ReportedTimestampUtc = GetReportedTimestampUtc(errorReport.ReportedTimestampUtc), UserEmail = errorReport.UserEmail }; session.Store(errorReportToStore); session.SaveChanges(); // email var emailsToContact = session .Load<ContactEmail>(app.ContactEmailIds) .Where(ce => ce.Confirmed) .Select(ce => ce.EmailAddress) .ToList(); if (emailsToContact.Count > 0) { _emailService.SendErrorReports(app, errorReportToStore, emailsToContact); } else { LoggingService.LogInfo("No emails to contact for " + app.Id); } return errorReportToStore; } }
public void ErrorService_EmptyModelValidation() { var testError = new ErrorReport(); try { new ErrorService(DocumentStore, _MockEmailService.Object).ReportError(testError, null); } catch (ServiceValidationException serviceEx) { Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.ErrorReport_MissingAppId)); Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.ErrorReport_AppDoesNotExist)); Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.ErrorReport_MissingMessage)); Assert.AreEqual(3, serviceEx.ErrorCodes.Count); } }
public void ErrorService_AppDoesNotExistValidation() { var testError = new ErrorReport { AppId = "bad_app_id", Message = "test report", UserEmail = "*****@*****.**" }; try { new ErrorService(DocumentStore, _MockEmailService.Object).ReportError(testError, null); } catch (ServiceValidationException serviceEx) { Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.ErrorReport_AppDoesNotExist)); Assert.AreEqual(1, serviceEx.ErrorCodes.Count); } }
public void SendErrorReports(App app, ErrorReport errorReport, IEnumerable<string> emailAddresses) { string from = app.Name.Replace(' ', '_') + "-" + app.Id.Replace("/", String.Empty) + "@" + _MailgunDomain; string replyTo = errorReport.UserEmail; string subject = "ErrorGun Error Report for " + app.Name; string body = String.Format( "Id: {0}, Category: {1}, Source: {2}, Reported At: {3} UTC, User Email: {4}\r\nMessage: {5}\r\nDetail: {6}", errorReport.Id, errorReport.Category.PlaceholderOrTrim(), errorReport.Source.PlaceholderOrTrim(), errorReport.ReportedTimestampUtc, errorReport.UserEmail.PlaceholderOrTrim(), errorReport.Message.PlaceholderOrTrim(), errorReport.Detail.PlaceholderOrTrim()); foreach (var emailAddress in emailAddresses) { SendEmail(from, replyTo, emailAddress, subject, body); } }
public void ErrorService_InvalidApiKeyValidation() { var testError = new ErrorReport { AppId = _TestAppId, Message = "test report", UserEmail = "*****@*****.**" }; try { new ErrorService(DocumentStore, _MockEmailService.Object).ReportError(testError, "bad api key"); } catch (ServiceValidationException serviceEx) { Assert.IsTrue(serviceEx.ErrorCodes.Contains(ErrorCode.ErrorReport_InvalidApiKey)); Assert.AreEqual(1, serviceEx.ErrorCodes.Count); } }
public void ErrorService_ReportError() { var error = new ErrorReport { AppId = _TestAppId, Category = "category", Detail = "detail", Message = "message", Source = "source", ReportedTimestampUtc = DateTime.UtcNow, UserEmail = "*****@*****.**" }; var service = new ErrorService(DocumentStore, _MockEmailService.Object); var reportedError = service.ReportError(error, "apikey"); Assert.IsFalse(String.IsNullOrEmpty(reportedError.Id)); _MockEmailService.Verify(s => s.SendErrorReports(It.IsAny<App>(), It.IsAny<ErrorReport>(), It.IsAny<IEnumerable<string>>())); }
private static void ThrowOnInvalidErrorReport(ErrorReport errorReport, string apiKey, App app) { var errorCodes = new List<ErrorCode>(); if (errorReport == null) { errorCodes.Add(ErrorCode.ErrorReport_MissingErrorReport); } else { if (String.IsNullOrWhiteSpace(errorReport.AppId)) errorCodes.Add(ErrorCode.ErrorReport_MissingAppId); if (app == null) { errorCodes.Add(ErrorCode.ErrorReport_AppDoesNotExist); } else { if (!String.Equals(app.ApiKey, apiKey, StringComparison.Ordinal)) errorCodes.Add(ErrorCode.ErrorReport_InvalidApiKey); } if (String.IsNullOrWhiteSpace(errorReport.Message)) errorCodes.Add(ErrorCode.ErrorReport_MissingMessage); if (!String.IsNullOrWhiteSpace(errorReport.UserEmail) && !EmailValidator.Validate(errorReport.UserEmail)) { errorCodes.Add(ErrorCode.ErrorReport_InvalidUserEmail); } } if (errorCodes.Count > 0) throw new ServiceValidationException(errorCodes); }