public void WhenNewMailHaveNoTopic_ShouldHaveError()
        {
            var validator = new NewMailValidator();

            #region Init variables
            List <string> CCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            List <string> BCCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            string topic = "";
            string text  = "testtext";
            #endregion
            NewMail newMail = new NewMail
            {
                CCRecipientsAddresses  = CCRecipientsAddresses,
                BCCRecipientsAddresses = BCCRecipientsAddresses,
                Topic = topic,
                Text  = text,
            };
            var result = validator.TestValidate(newMail);
            #region Tests
            result.ShouldHaveValidationErrorFor(x => x.Topic);
            #endregion
        }
        public void WhenNewMailHaveToLongText_ShouldHaveError()
        {
            var validator = new NewMailValidator();

            #region Init variables
            List <string> CCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            List <string> BCCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            string        topic         = "testtopic";
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < validator.textMaxLength + 1; i++)
            {
                stringBuilder.Append('x');
            }
            string text = stringBuilder.ToString();
            #endregion
            NewMail newMail = new NewMail
            {
                CCRecipientsAddresses  = CCRecipientsAddresses,
                BCCRecipientsAddresses = BCCRecipientsAddresses,
                Topic = topic,
                Text  = text,
            };
            var result = validator.TestValidate(newMail);
            #region Tests
            result.ShouldHaveValidationErrorFor(x => x.Text);
            #endregion
        }
Beispiel #3
0
        public async Task <IActionResult> Create(string topic, string text, List <string> ccRecipientsAddresses, List <string> bccRecipientsAddresses, List <IFormFile> files)
        {
            int           userID        = int.Parse(User.Claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value);
            ErrorResponse errorResponse = new ErrorResponse();

            NewMail newMail = new NewMail
            {
                Topic = topic,
                Text  = text,
                CCRecipientsAddresses  = ccRecipientsAddresses,
                BCCRecipientsAddresses = bccRecipientsAddresses,
                Files = files
            };
            var newMailValidator = new NewMailValidator().Validate(newMail);

            if (!newMailValidator.IsValid)
            {
                foreach (var error in newMailValidator.Errors)
                {
                    errorResponse.Errors.Add(new ErrorModel {
                        FieldName = error.PropertyName, Message = error.ErrorMessage
                    });
                }
                Response.StatusCode = 400;
                return(new JsonResult(errorResponse));
            }

            try
            {
                await _mailService.AddMail(userID, newMail);
            }
            catch (Exception ex)
            {
                errorResponse.Errors.Add(new ErrorModel {
                    FieldName = ex.Message, Message = ex.InnerException.Message
                });
                Response.StatusCode = 400;
            }

            return(new JsonResult(errorResponse));
        }
        public void WhenNewMailHaveAtLeastOneBCCRecipients_ShouldNotHaveAnyError()
        {
            var validator = new NewMailValidator();

            #region Init variables
            List <string> CCRecipientsAddresses  = new List <string>();
            List <string> BCCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            string topic = "testtopic";
            string text  = "testtext";
            #endregion
            NewMail newMail = new NewMail
            {
                CCRecipientsAddresses  = CCRecipientsAddresses,
                BCCRecipientsAddresses = BCCRecipientsAddresses,
                Topic = topic,
                Text  = text,
            };
            var result = validator.TestValidate(newMail);
            #region Tests
            result.ShouldNotHaveAnyValidationErrors();
            #endregion
        }