Exemple #1
0
        private void JpegImgQualityUpDown_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            model.JpegImageQuality = Convert.ToInt32(JpegImgQualityUpDown.Value);
            bool valid = _validator.ValidateModel();

            FocusControlToolTip.Active = !valid;
            e.Cancel = !valid;
        }
Exemple #2
0
        public void ValidateModel_ModelIsNull_ExpectArgumentException()
        {
            model = null;

            var ex = Assert.Throws <ArgumentException>(() =>
            {
                validator.ValidateModel(model);
            });

            Assert.Equal("Model to validate is missing", ex.Message);
            Assert.Null(model);
        }
Exemple #3
0
        public void Returns_ValidationSuccess_If_Not_Required_And_File_Empty()
        {
            var model = new Model()
            {
                NotRequiredFile = null
            };

            Assert.True(ModelValidator.ValidateModel(model).Count == 0);
        }
        public void Returns_ValidationResult_If_Leading_Decimal(string fileName)
        {
            var justExtensions = IFormFileBuilder.CreateMockFormFile(fileName);
            var model          = new Model()
            {
                RequiredFile = justExtensions
            };

            Assert.True(ModelValidator.ValidateModel(model).Count > 0);
        }
Exemple #5
0
        public void JpegImageQualityTestBelow()
        {
            const int ImageQuality             = 49;
            ApplicationSettingsModel model     = CreateModel(ImageQuality);
            ModelValidator           validator = new ModelValidator(model);

            bool valid = validator.ValidateModel();

            Assert.IsFalse(valid, " validation of " + nameof(model.JpegImageQuality) + " failed!");
        }
Exemple #6
0
        public void Returns_ValidationResult_If_Not_Required_But_Still_Invalid()
        {
            var invalidFile = IFormFileBuilder.CreateMockFormFile("hi.exe");
            var model       = new Model()
            {
                NotRequiredFile = invalidFile
            };

            Assert.True(ModelValidator.ValidateModel(model).Count > 0);
        }
        public void Returns_ValidationResult_If_No_Extension()
        {
            var noExtFile = IFormFileBuilder.CreateMockFormFile("sneaky");
            var model     = new Model()
            {
                RequiredFile = noExtFile
            };

            Assert.True(ModelValidator.ValidateModel(model).Count > 0);
        }
        public void Returns_ValidationResult_If_Invalid_Extension(string fileName)
        {
            var invalidFile = IFormFileBuilder.CreateMockFormFile(fileName);
            var model       = new Model()
            {
                RequiredFile = invalidFile
            };

            Assert.True(ModelValidator.ValidateModel(model).Count > 0);
        }
        public void Returns_ValidationSuccess_If_Extensions_Match()
        {
            var validFile = IFormFileBuilder.CreateMockFormFile("valid.jpg");
            var model     = new Model()
            {
                RequiredFile = validFile
            };

            Assert.True(ModelValidator.ValidateModel(model).Count == 0);
        }
        public void CreateTodoViewModel_AllFieldsValid_NoErrors()
        {
            // Arrange
            var model = MockCreateTodoViewModel.Get(1);

            // Act
            var validation = ModelValidator.ValidateModel(model);

            // Assert
            Assert.Equal(0, validation.Count);
        }
Exemple #11
0
        public void NoPropertiesSet_ReturnsIssue()
        {
            // Arrange
            PartyLookup target = new PartyLookup();

            // Act
            IList <ValidationResult> issues = ModelValidator.ValidateModel(target);

            // Assert
            Assert.True(issues.All(i => i.MemberNames.Contains("OrgNo") && i.ErrorMessage.Contains("At least one")));
        }
Exemple #12
0
        public void Returns_ValidationResult_If_Size_Exceeds_Spec()
        {
            var oneByteOver5Mb = 5242881;
            var largeFile      = IFormFileBuilder.CreateMockFormFile("Foo", oneByteOver5Mb);

            var invalidModel = new Model()
            {
                File = largeFile
            };

            Assert.True(ModelValidator.ValidateModel(invalidModel).Count > 0);
        }
Exemple #13
0
        public IActionResult Post([FromBody] Object obj)
        {
            var resource = shaper.FormatObject(obj);

            if (checker.ValidateModel(resource))
            {
                repo.Create(resource);
                return(Created(resource));
            }

            return(BadRequest());
        }
Exemple #14
0
        public void TwoPropertiesSet_ReturnsIssue()
        {
            // Arrange
            PartyLookup target = new PartyLookup {
                Ssn = "09054300139", OrgNo = "910072218"
            };

            // Act
            IList <ValidationResult> issues = ModelValidator.ValidateModel(target);

            // Assert
            Assert.True(issues.All(i => i.MemberNames.Contains("OrgNo") && i.ErrorMessage.Contains("With Ssn already")));
        }
Exemple #15
0
        public void SsnInvalid(string ssn)
        {
            // Arrange
            PartyLookup target = new PartyLookup {
                Ssn = ssn
            };

            // Act
            IList <ValidationResult> issues = ModelValidator.ValidateModel(target);

            // Assert
            Assert.True(issues.All(i => i.MemberNames.Contains("Ssn") && i.ErrorMessage.Contains("exactly 11 digits")));
        }
Exemple #16
0
        public void OrgNoInvalid(string orgNo)
        {
            // Arrange
            PartyLookup target = new PartyLookup {
                OrgNo = orgNo
            };

            // Act
            IList <ValidationResult> issues = ModelValidator.ValidateModel(target);

            // Assert
            Assert.True(issues.All(i => i.MemberNames.Contains("OrgNo") && i.ErrorMessage.Contains("exactly 9 digits")));
        }
Exemple #17
0
        public void SsnIsValid(string ssn)
        {
            // Arrange
            PartyLookup target = new PartyLookup {
                Ssn = ssn
            };

            // Act
            IList <ValidationResult> issues = ModelValidator.ValidateModel(target);

            // Assert
            Assert.Empty(issues);
        }
Exemple #18
0
        public void OrgNoIsValid(string orgNo)
        {
            // Arrange
            PartyLookup target = new PartyLookup {
                OrgNo = orgNo
            };

            // Act
            IList <ValidationResult> issues = ModelValidator.ValidateModel(target);

            // Assert
            Assert.Empty(issues);
        }
Exemple #19
0
        public void LoginViewModel_EmailMissing_Error()
        {
            // Arrange
            var model = new LoginViewModel
            {
                Password = "******"
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.AccountEmailIsRequired, errorMessage);
        }
Exemple #20
0
        public void LoginViewModel_PasswordMissing_Error()
        {
            // Arrange
            var model = new LoginViewModel
            {
                Email = "*****@*****.**",
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.AccountPasswordIsRequired, errorMessage);
        }
        public IActionResult Register([FromBody] Object obj)
        {
            // Retrieve actual class of the model
            var resource = shaper.FormatObject(obj);

            // Encrypt the user's credentials for security
            resource = crypto.EncryptUserPassword(resource);

            if (checker.ValidateModel(resource))
            {
                repo.Create(resource);
                return(Ok("Registration successful."));
            }

            return(BadRequest("Registration failed."));
        }
        public void CreateTodoViewModel_DescriptionMissing_Error()
        {
            // Arrange
            var model = new CreateTodoViewModel
            {
                Due = DateTime.Now
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.TodoDescriptionRequired, errorMessage);
        }
        public void EditTodoViewModel_IdMissing_Error()
        {
            // Arrange
            var model = new EditTodoViewModel
            {
                Description = "Build a ladder to heaven",
                Due         = DateTime.Now
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.TodoIdRequired, errorMessage);
        }
        public void EditTodoViewModel_DueMissing_Error()
        {
            // Arrange
            var model = new EditTodoViewModel
            {
                Description = "Give an angry speech before being killed by a shark",
                Id          = 5555
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.TodoDueRequired, errorMessage);
        }
        public void RegisterViewModel_EmailMissing_Error()
        {
            // Arrange
            var model = new RegisterViewModel
            {
                Password = "******",
                Name     = "Curts"
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.AccountEmailIsRequired, errorMessage);
        }
        public void RegisterViewModel_PasswordMissing_Error()
        {
            // Arrange
            var model = new RegisterViewModel
            {
                Name  = "Curtis",
                Email = "*****@*****.**",
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.AccountPasswordIsRequired, errorMessage);
        }
        public void RegisterViewModel_NameMissing_Error()
        {
            // Arrange
            var model = new RegisterViewModel
            {
                Email    = "*****@*****.**",
                Password = "******",
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.UserNameRequired, errorMessage);
        }
Exemple #28
0
        public void LoginViewModel_PasswordTooShort_Error()
        {
            // Arrange
            var model = new LoginViewModel
            {
                Email    = "*****@*****.**",
                Password = "******"
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.AccountPasswordInvalidLength, errorMessage);
        }
Exemple #29
0
        public void LoginViewModel_EmailInvalid_Error()
        {
            // Arrange
            var model = new LoginViewModel
            {
                Email    = "This is not a email",
                Password = "******"
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal("The Email field is not a valid e-mail address.", errorMessage);
        }
        public void CreateTodoViewModel_DescriptionTooLong_Error()
        {
            // Arrange
            var len   = TodoLimits.DescriptionMaxLength + 1;
            var model = new CreateTodoViewModel
            {
                Description = new StringBuilder(len).Insert(0, "A", len).ToString(),
                Due         = DateTime.Now
            };

            // Act
            var validation   = ModelValidator.ValidateModel(model);
            var errorMessage = validation.Select(x => x.ErrorMessage).SingleOrDefault();

            // Assert
            Assert.Single(validation);
            Assert.Equal(ErrorMessages.TodoDescriptionMaxLength, errorMessage);
        }