示例#1
0
        public void TestTextCheckRestrictedSymbols()
        {
            const string AllChars = "abcdefghijklmnopqrstuvwxyz \n\t абвгдеёжзийклмнопрстуфхцчшщьыъэюя 1234567890 \\ \" .,;:~'`!? №@#$%^&_ []{}()<> /*-+=";
            var          value    = new TextElementValue {
                Raw = AllChars
            };
            var constraints = new PlainTextElementConstraints();

            var errorSpace = TestHelpers.MakeValidationCheck <TextElementValue, NonBreakingSpaceSymbolError>(
                value,
                constraints,
                PlainTextValidator.CheckRestrictedSymbols,
                val => val.Raw = "\x00A0");

            Assert.Equal(nameof(constraints.WithoutNonBreakingSpace), errorSpace.ErrorType);

            value.Raw = AllChars.ToUpper();
            var errorControlChars = TestHelpers.MakeValidationCheck <TextElementValue, ControlCharactersInTextError>(
                value,
                constraints,
                PlainTextValidator.CheckRestrictedSymbols,
                val => val.Raw = "\r");

            Assert.Equal(nameof(constraints.WithoutControlChars), errorControlChars.ErrorType);
        }
示例#2
0
        public async Task ObjectElementValueShouldBeSet()
        {
            const int      TemplateCode         = 100;
            const Language Language             = Language.Ru;
            var            plainTextConstraints = new PlainTextElementConstraints {
                MaxSymbols = 100
            };

            var templateDescriptor = new TemplateDescriptor
            {
                Id         = TemplateId,
                VersionId  = TemplateVersionId,
                Properties = new JObject(),
                Elements   = new[]
                {
                    new ElementDescriptor(
                        ElementDescriptorType.PlainText,
                        TemplateCode,
                        new JObject(),
                        new ConstraintSet(new[]
                    {
                        new ConstraintSetItem(Language, plainTextConstraints)
                    }))
                }
            };

            _objectsStorageReaderMock.Setup(m => m.IsObjectExists(It.IsAny <long>()))
            .ReturnsAsync(() => false);
            _templatesStorageReaderMock.Setup(m => m.GetTemplateDescriptor(It.IsAny <long>(), It.IsAny <string>()))
            .ReturnsAsync(() => templateDescriptor);
            _templatesStorageReaderMock.Setup(m => m.GetTemplateLatestVersion(It.IsAny <long>()))
            .ReturnsAsync(() => TemplateVersionId);

            var objectDescriptor = new ObjectDescriptor
            {
                Language          = Language,
                TemplateId        = TemplateId,
                TemplateVersionId = TemplateVersionId,
                Properties        = new JObject(),
                Elements          = new[]
                {
                    new ObjectElementDescriptor
                    {
                        Type         = ElementDescriptorType.PlainText,
                        TemplateCode = TemplateCode,
                        Constraints  = new ConstraintSet(new[]
                        {
                            new ConstraintSetItem(Language, plainTextConstraints)
                        })
                    }
                }
            };

            await Assert.ThrowsAsync <NullReferenceException>(
                async() => await _objectsManagementService.Create(ObjectId, AuthorInfo, objectDescriptor));
        }
示例#3
0
        public void TestFasCommentCheckRestrictedSymbols()
        {
            const string AllChars = "abcdefghijklmnopqrstuvwxyz \n\t абвгдеёжзийклмнопрстуфхцчшщьыъэюя 1234567890 \\ \" .,;:~'`!? №@#$%^&|_ []{}()<> /*-+=";
            var          value    = new FasElementValue {
                Raw = "custom", Text = AllChars
            };
            var constraints = new PlainTextElementConstraints();

            TestHelpers.MakeValidationCheck <FasElementValue, NonBreakingSpaceSymbolError>(value, constraints, PlainTextValidator.CheckRestrictedSymbols, val => val.Text = "\x00A0");

            value.Text = AllChars.ToUpper();
            TestHelpers.MakeValidationCheck <FasElementValue, ControlCharactersInTextError>(value, constraints, PlainTextValidator.CheckRestrictedSymbols, val => val.Text = "\r");
        }
示例#4
0
        public void TestAllChecks(string text, int?maxLength, int?maxWordLength, int?maxLines, bool containsRestrictedSymbols = true, int expectedErrorsCount = 5)
        {
            IObjectElementValue value = new TextElementValue {
                Raw = text
            };
            var constraints = new PlainTextElementConstraints {
                MaxLines = maxLines, MaxSymbols = maxLength, MaxSymbolsPerWord = maxWordLength
            };

            TestHelpers.InternalTextChecksTest(AllChecks, containsRestrictedSymbols, expectedErrorsCount, value, constraints);

            value = new FasElementValue {
                Raw = "custom", Text = text
            };
            TestHelpers.InternalTextChecksTest(AllChecks, containsRestrictedSymbols, expectedErrorsCount, value, constraints);
        }
示例#5
0
        public void TestFasCommentCheckLength()
        {
            var value = new FasElementValue {
                Raw = "custom", Text = "text"
            };
            var constraints = new PlainTextElementConstraints {
                MaxSymbols = 5
            };

            var error = TestHelpers.MakeValidationCheck <FasElementValue, ElementTextTooLongError>(
                value,
                constraints,
                PlainTextValidator.CheckLength,
                val => val.Text = "long text");

            Assert.Equal(constraints.MaxSymbols, error.MaxLength);
            Assert.Equal(value.Text.Length, error.ActualLength);
            Assert.Equal(nameof(constraints.MaxSymbols), error.ErrorType);
        }
示例#6
0
        public void TestTextCheckMaxLines()
        {
            const int MaxLines = 10;
            var       value    = new TextElementValue {
                Raw = new string('\n', MaxLines - 1)
            };
            var constraints = new PlainTextElementConstraints {
                MaxLines = MaxLines
            };

            var error = TestHelpers.MakeValidationCheck <TextElementValue, TooManyLinesError>(
                value,
                constraints,
                PlainTextValidator.CheckLinesCount,
                val => val.Raw = new string('\n', MaxLines));

            Assert.Equal(MaxLines, error.MaxLinesCount);
            Assert.Equal(MaxLines + 1, error.ActualLinesCount);
            Assert.Equal(nameof(constraints.MaxLines), error.ErrorType);
        }
示例#7
0
        public void TestTextCheckLength()
        {
            const int MaxSymbols = 50;
            var       value      = new TextElementValue {
                Raw = new string('a', MaxSymbols)
            };
            var constraints = new PlainTextElementConstraints {
                MaxSymbols = MaxSymbols
            };

            var error = TestHelpers.MakeValidationCheck <TextElementValue, ElementTextTooLongError>(
                value,
                constraints,
                PlainTextValidator.CheckLength,
                val => val.Raw = new string('b', MaxSymbols + 1));

            Assert.Equal(MaxSymbols, error.MaxLength);
            Assert.Equal(MaxSymbols + 1, error.ActualLength);
            Assert.Equal(nameof(constraints.MaxSymbols), error.ErrorType);
        }
示例#8
0
        public void TestTextCheckLongWords()
        {
            const int MaxSymbols = 10;
            var       value      = new TextElementValue {
                Raw = new string('a', MaxSymbols)
            };
            var constraints = new PlainTextElementConstraints {
                MaxSymbolsPerWord = MaxSymbols
            };

            var error = TestHelpers.MakeValidationCheck <TextElementValue, ElementWordsTooLongError>(
                value,
                constraints,
                PlainTextValidator.CheckWordsLength,
                val => val.Raw = new string('b', MaxSymbols + 1));

            Assert.Equal(MaxSymbols, error.MaxWordLength);
            Assert.Equal(1, error.TooLongWords.Count);
            Assert.Equal(value.Raw, error.TooLongWords.First());
            Assert.Equal(nameof(constraints.MaxSymbolsPerWord), error.ErrorType);
        }
示例#9
0
        public async Task CorrectJsonShouldBePassedToS3PutObjectForTextElement()
        {
            const int      TemplateCode         = 100;
            const Language Language             = Language.Ru;
            var            plainTextConstraints = new PlainTextElementConstraints {
                MaxSymbols = 100
            };

            var templateDescriptor = new TemplateDescriptor
            {
                Id         = TemplateId,
                VersionId  = TemplateVersionId,
                Properties = new JObject(),
                Elements   = new[]
                {
                    new ElementDescriptor(
                        ElementDescriptorType.PlainText,
                        TemplateCode,
                        new JObject(),
                        new ConstraintSet(new[]
                    {
                        new ConstraintSetItem(Language, plainTextConstraints)
                    }))
                }
            };

            _objectsStorageReaderMock.Setup(m => m.IsObjectExists(It.IsAny <long>()))
            .ReturnsAsync(() => false);
            _templatesStorageReaderMock.Setup(m => m.GetTemplateDescriptor(It.IsAny <long>(), It.IsAny <string>()))
            .ReturnsAsync(() => templateDescriptor);
            _templatesStorageReaderMock.Setup(m => m.GetTemplateLatestVersion(It.IsAny <long>()))
            .ReturnsAsync(() => TemplateVersionId);
            _objectsStorageReaderMock.Setup(m => m.GetObjectLatestVersions(It.IsAny <long>()))
            .ReturnsAsync(() => new[]
            {
                new VersionedObjectDescriptor <string>(
                    ObjectId.AsS3ObjectKey(Tokens.ObjectPostfix),
                    ObjectVersionId,
                    DateTime.UtcNow)
            });

            var requests = new List <PutObjectRequest>();

            _s3ClientMock.Setup(m => m.PutObjectAsync(It.IsAny <PutObjectRequest>()))
            .Callback <PutObjectRequest>(request => requests.Add(request))
            .ReturnsAsync(new PutObjectResponse());

            var objectDescriptor = new ObjectDescriptor
            {
                Language          = Language,
                TemplateId        = TemplateId,
                TemplateVersionId = TemplateVersionId,
                Properties        = new JObject(),
                Elements          = new[]
                {
                    new ObjectElementDescriptor
                    {
                        Type         = ElementDescriptorType.PlainText,
                        TemplateCode = TemplateCode,
                        Constraints  = new ConstraintSet(new[]
                        {
                            new ConstraintSetItem(Language, plainTextConstraints)
                        }),
                        Value = new TextElementValue {
                            Raw = "Text"
                        }
                    }
                }
            };

            await _objectsManagementService.Create(ObjectId, AuthorInfo, objectDescriptor);

            var elementContent = requests[0].ContentBody;
            var elementJson    = JObject.Parse(elementContent);

            Assert.Equal("Text", elementJson["value"]["raw"]);
        }
示例#10
0
        public async Task S3PutObjectShouldBeCalledWhileCreation()
        {
            const int      TemplateCode         = 100;
            const Language Language             = Language.Ru;
            var            plainTextConstraints = new PlainTextElementConstraints {
                MaxSymbols = 100
            };

            var templateDescriptor = new TemplateDescriptor
            {
                Id         = TemplateId,
                VersionId  = TemplateVersionId,
                Properties = new JObject(),
                Elements   = new[]
                {
                    new ElementDescriptor(
                        ElementDescriptorType.PlainText,
                        TemplateCode,
                        new JObject(),
                        new ConstraintSet(new[]
                    {
                        new ConstraintSetItem(Language, plainTextConstraints)
                    }))
                }
            };

            _objectsStorageReaderMock.Setup(m => m.IsObjectExists(It.IsAny <long>()))
            .ReturnsAsync(() => false);
            _templatesStorageReaderMock.Setup(m => m.GetTemplateDescriptor(It.IsAny <long>(), It.IsAny <string>()))
            .ReturnsAsync(() => templateDescriptor);
            _templatesStorageReaderMock.Setup(m => m.GetTemplateLatestVersion(It.IsAny <long>()))
            .ReturnsAsync(() => TemplateVersionId);
            _objectsStorageReaderMock.Setup(m => m.GetObjectLatestVersions(It.IsAny <long>()))
            .ReturnsAsync(() => new[]
            {
                new VersionedObjectDescriptor <string>(
                    ObjectId.AsS3ObjectKey(Tokens.ObjectPostfix),
                    ObjectVersionId,
                    DateTime.UtcNow)
            });

            var objectDescriptor = new ObjectDescriptor
            {
                Language          = Language,
                TemplateId        = TemplateId,
                TemplateVersionId = TemplateVersionId,
                Properties        = new JObject(),
                Elements          = new[]
                {
                    new ObjectElementDescriptor
                    {
                        Type         = ElementDescriptorType.PlainText,
                        TemplateCode = TemplateCode,
                        Constraints  = new ConstraintSet(new[]
                        {
                            new ConstraintSetItem(Language, plainTextConstraints)
                        }),
                        Value = new TextElementValue {
                            Raw = "Text"
                        }
                    }
                }
            };

            await _objectsManagementService.Create(ObjectId, AuthorInfo, objectDescriptor);

            _s3ClientMock.Verify(m => m.PutObjectAsync(It.IsAny <PutObjectRequest>()), Times.Exactly(2));
        }
        public async Task CorrectJsonShouldBePassedToS3PutObjectForTextElement()
        {
            const int      TemplateCode         = 100;
            const Language Language             = Language.Ru;
            var            plainTextConstraints = new PlainTextElementConstraints {
                MaxSymbols = 100
            };

            var templateDescriptor = new TemplateDescriptor
            {
                Id         = TemplateId,
                VersionId  = TemplateVersionId,
                Properties = new JObject(),
                Elements   = new[]
                {
                    new ElementDescriptor(
                        ElementDescriptorType.PlainText,
                        TemplateCode,
                        new JObject(),
                        new ConstraintSet(new[]
                    {
                        new ConstraintSetItem(Language, plainTextConstraints)
                    }))
                }
            };

            _objectsStorageReaderMock.Setup(m => m.IsObjectExists(It.IsAny <long>()))
            .ReturnsAsync(() => false);
            _templatesStorageReaderMock.Setup(m => m.GetTemplateDescriptor(It.IsAny <long>(), It.IsAny <string>()))
            .ReturnsAsync(() => templateDescriptor);
            _objectsStorageReaderMock.Setup(m => m.GetObjectLatestVersion(It.IsAny <long>()))
            .ReturnsAsync(new VersionedObjectDescriptor <long>(ObjectId, ObjectVersionId, DateTime.UtcNow));

            var objectDescriptor = new ObjectDescriptor
            {
                Language          = Language,
                TemplateId        = TemplateId,
                TemplateVersionId = TemplateVersionId,
                Properties        = new JObject(),
                Elements          = new[]
                {
                    new ObjectElementDescriptor
                    {
                        Type         = ElementDescriptorType.PlainText,
                        TemplateCode = TemplateCode,
                        Constraints  = new ConstraintSet(new[]
                        {
                            new ConstraintSetItem(Language, plainTextConstraints)
                        }),
                        Value = new TextElementValue {
                            Raw = "Text"
                        }
                    }
                }
            };

            await _objectsManagementService.Create(ObjectId, AuthorInfo, objectDescriptor);

            Assert.True(await _inMemoryContext.Objects.AnyAsync());
            var element = await _inMemoryContext.ObjectElements.FirstOrDefaultAsync();

            Assert.NotNull(element);
            var elementJson = JObject.Parse(element.Data);

            Assert.Equal("Text", elementJson["value"]["raw"]);
        }
        public async Task S3PutObjectShouldNotBeCalledWhileCreation()
        {
            const int      TemplateCode         = 100;
            const Language Language             = Language.Ru;
            var            plainTextConstraints = new PlainTextElementConstraints {
                MaxSymbols = 100
            };

            var templateDescriptor = new TemplateDescriptor
            {
                Id         = TemplateId,
                VersionId  = TemplateVersionId,
                Properties = new JObject(),
                Elements   = new[]
                {
                    new ElementDescriptor(
                        ElementDescriptorType.PlainText,
                        TemplateCode,
                        new JObject(),
                        new ConstraintSet(new[]
                    {
                        new ConstraintSetItem(Language, plainTextConstraints)
                    }))
                }
            };

            _objectsStorageReaderMock.Setup(m => m.IsObjectExists(It.IsAny <long>()))
            .ReturnsAsync(() => false);
            _templatesStorageReaderMock.Setup(m => m.GetTemplateDescriptor(It.IsAny <long>(), It.IsAny <string>()))
            .ReturnsAsync(() => templateDescriptor);
            _objectsStorageReaderMock.Setup(m => m.GetObjectLatestVersion(It.IsAny <long>()))
            .ReturnsAsync(new VersionedObjectDescriptor <long>(ObjectId, ObjectVersionId, DateTime.UtcNow));

            var objectDescriptor = new ObjectDescriptor
            {
                Language          = Language,
                TemplateId        = TemplateId,
                TemplateVersionId = TemplateVersionId,
                Properties        = new JObject(),
                Elements          = new[]
                {
                    new ObjectElementDescriptor
                    {
                        Type         = ElementDescriptorType.PlainText,
                        TemplateCode = TemplateCode,
                        Constraints  = new ConstraintSet(new[]
                        {
                            new ConstraintSetItem(Language, plainTextConstraints)
                        }),
                        Value = new TextElementValue {
                            Raw = "Text"
                        }
                    }
                }
            };

            Assert.False(await _inMemoryContext.Objects.AnyAsync());
            await _objectsManagementService.Create(ObjectId, AuthorInfo, objectDescriptor);

            Assert.True(await _inMemoryContext.Objects.AnyAsync());
        }