示例#1
0
        public void Process_ShouldNotAdd_DataTabIndexAttribute_WhenTabIndexNull()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.IsFalse(_tagHelperOutputStub.Attributes.ContainsName("data-tabindex"));
        }
示例#2
0
        public void Process_ShouldThrow_ArgumentNullException()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            // Act


            // Assert
            Assert.Throws <ArgumentNullException>(() => scriptTagHelper.Process(_contextStub, null));
        }
示例#3
0
        public void Process_ShouldReady_FromSettings_OnlyOnce()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            _settingsMock.Verify(mock => mock.CurrentValue, Times.Once);
        }
示例#4
0
        public void Process_ShouldChangeTagTo_DivTag()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.AreEqual("div", _tagHelperOutputStub.TagName);
        }
示例#5
0
        public void Process_ShouldNotAdd_DataErrorCallbackAttribute_WhenErrorCallbackNullOrEmpty()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.IsFalse(_tagHelperOutputStub.Attributes.ContainsName("data-error-callback"));
        }
示例#6
0
        public void Process_ShouldAdd_SiteKeyAttribute()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.IsTrue(_tagHelperOutputStub.Attributes.ContainsName("data-sitekey"));
            Assert.AreEqual(SiteKey, _tagHelperOutputStub.Attributes["data-sitekey"].Value.ToString());
        }
示例#7
0
        public void Process_ShouldAdd_RecaptchaClass()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.IsTrue(_tagHelperOutputStub.Attributes.ContainsName("class"));
            Assert.AreEqual("g-recaptcha", _tagHelperOutputStub.Attributes["class"].Value);
        }
示例#8
0
        public void Process_ShouldAdd_DataSizeAttribute()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.IsTrue(_tagHelperOutputStub.Attributes.ContainsName("data-size"));
            Assert.AreEqual(Size.Normal.ToString().ToLowerInvariant(), _tagHelperOutputStub.Attributes["data-size"].Value.ToString());
        }
示例#9
0
        public void Process_ShouldChange_TagModeTo_StartTagAndEndTag()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            _tagHelperOutputStub.TagMode = TagMode.SelfClosing;

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.AreEqual(TagMode.StartTagAndEndTag, _tagHelperOutputStub.TagMode);
        }
示例#10
0
        public void Process_ShouldOverrideExisting_SiteKeyAttribute()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            _tagHelperOutputStub.Attributes.Add("data-sitekey", "false-key");

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.AreEqual(1, _tagHelperOutputStub.Attributes.Count(attribute => attribute.Name == "data-sitekey"));
            Assert.AreEqual(SiteKey, _tagHelperOutputStub.Attributes["data-sitekey"].Value.ToString());
        }
示例#11
0
        public void Process_ShouldAdd_DataErrorCallbackAttribute()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object)
            {
                ErrorCallback = "myCallback"
            };

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.IsTrue(_tagHelperOutputStub.Attributes.ContainsName("data-error-callback"));
            Assert.AreEqual("myCallback", _tagHelperOutputStub.Attributes["data-error-callback"].Value.ToString());
        }
示例#12
0
        public void Process_ShouldAdd_DataTabindexAttribute()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object)
            {
                TabIndex = 2
            };

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.IsTrue(_tagHelperOutputStub.Attributes.ContainsName("data-tabindex"));
            Assert.AreEqual(2, (int)_tagHelperOutputStub.Attributes["data-tabindex"].Value);
        }
示例#13
0
        public void Process_ShouldOverrideExisting_DataErrorCallbackAttribute()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object)
            {
                ErrorCallback = "myCallback"
            };

            _tagHelperOutputStub.Attributes.Add("data-error-callback", "fake-callback");

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.AreEqual(1, _tagHelperOutputStub.Attributes.Count(attribute => attribute.Name == "data-error-callback"));
            Assert.AreEqual("myCallback", _tagHelperOutputStub.Attributes["data-error-callback"].Value.ToString());
        }
示例#14
0
        public void Process_ShouldAdd_RecaptchaClassAnd_KeepExistingClasses()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object);

            _tagHelperOutputStub.Attributes.Add("class", "container text-center");

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);
            var classes = _tagHelperOutputStub.Attributes["class"].Value.ToString().Split(" ", StringSplitOptions.RemoveEmptyEntries);

            // Assert
            Assert.AreEqual(3, classes.Length);
            Assert.IsTrue(classes.Contains("g-recaptcha"));
            Assert.IsTrue(classes.Contains("text-center"));
            Assert.IsTrue(classes.Contains("container"));
        }
示例#15
0
        public void Process_ShouldOverrideExisting_DataTabIndexAttribute()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object)
            {
                TabIndex = 3
            };

            _tagHelperOutputStub.Attributes.Add("data-tabindex", "1");

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.AreEqual(1, _tagHelperOutputStub.Attributes.Count(attribute => attribute.Name == "data-tabindex"));
            Assert.AreEqual(3, (int)_tagHelperOutputStub.Attributes["data-tabindex"].Value);
        }
示例#16
0
        public void Process_ShouldOverrideExisting_DataThemeAttribute()
        {
            // Arrange
            var scriptTagHelper = new RecaptchaTagHelper(_settingsMock.Object, _optionsMock.Object)
            {
                Theme = Theme.Dark
            };

            _tagHelperOutputStub.Attributes.Add("data-theme", "light");

            // Act
            scriptTagHelper.Process(_contextStub, _tagHelperOutputStub);

            // Assert
            Assert.AreEqual(1, _tagHelperOutputStub.Attributes.Count(attribute => attribute.Name == "data-theme"));
            Assert.AreEqual(Theme.Dark.ToString().ToLowerInvariant(), _tagHelperOutputStub.Attributes["data-theme"].Value.ToString());
        }
        private TagHelperOutput ProcessTagHelper(RecaptchaService service, TagHelperAttributeList attributes, Action <RecaptchaTagHelper> config = null)
        {
            var tagHelper = new RecaptchaTagHelper(service);

            config?.Invoke(tagHelper);

            var tagHelperContext = new TagHelperContext(
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                tagName: "doesntmatter",
                attributes: attributes,
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                return(Task.FromResult <TagHelperContent>(new DefaultTagHelperContent()));
            });

            tagHelper.Process(tagHelperContext, output);

            return(output);
        }