class Test_ValidationNonScalar(HTMLCheck): """ each <li> is going to be checked against a regex """ name = "Pham" template = """ <html><head><title>Greetings<title></head> <body>Hi <span id="name" >{{ name }}</span>! It is now <span class="timestamp">{{ now }}</span>. <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> </body></html> """ cls_validators = [ # ValidationDirective("list", exp=re.compile("Item"), validator=CSSValidator("li")) ]
class Test_ValidationNonScalarFail(Test_ValidationNonScalar): """ the regex will fail """ fail_on_validate = "Not Item" cls_validators = [ValidationDirective("list", exp=re.compile("Not Item"))]
class Test_Features(Helper, HTMLValidationMixin, LazyMixinBasic, unittest.TestCase): """ this is the test we are running here """ cls_filters = dict(html=filter_variables) # 👈 This is how we add the filters name = "Mr. Rabbit" line1 = "Item 1" line2 = "Item 2" line3 = "Item 3" # 👇 setting up the validations cls_validators = [ ValidationDirective("title", exp="Your order"), ValidationDirective("name", exp=AutoExp, validator=CSSValidator("#name")), ] # the template used to generate the fake html template = """
class Test_Validation(Test_Basic): template = """ <html><head><title>Greetings<title></head> <body>Hi <span id="name" >{{ name }}</span>! It is now <span class="timestamp">{{ now }}</span>. </body></html> """ cls_validators = [ ValidationDirective("name", exp=AutoExp, validator=CSSValidator("#name", "name")), # we have a title now ValidationDirective("title", active=True, exp=AutoExp), ] name = "Pham Nuwen" title = "Greetings"
class Test_Features(Helper, HTMLValidationMixin, LazyMixinBasic, unittest.TestCase): cls_filters = dict(html=filter_variables) # 👈 This is how we add the filters name = "Mr. Rabbit" # picked up by `AutoExp` below # 👇 setting up the validations 🥦🥦 cls_validators = [ ValidationDirective("title", exp="Your order"), ValidationDirective("name", exp=AutoExp, validator=CSSValidator("#name")), ] @mock.patch.dict(os.environ, di_mock_env) def test_it(self): try: # this comes from `requests`, could be a Django testserver, your code... http_response = get_fake_response_from_template(self) # if we re-use filters and validations, yes, we use a lot less than # 10 lines of code 👇 🍰🍰🍰🍰 # "adapt" standard http_response by tracking content_type, status_code, headers... response = ResponseHTML(http_response) # Check validation such as content_type and status code self.check_expectations(response=response) # Regression test - did we get the same contents as the last time? tmp = self.assert_exp(response.content, "html") except (Exception,) as e: raise ################################################################# # template and configuration to fake an http response ################################################################# line1 = "Item 1" line2 = "Item 2" line3 = "Item 3" # the template used to generate the fake html template = """
class Test_Features(Helper2, LazyMixinBasic): name = "Mr. Rabbit" line1 = "Item 1" line2 = "Item 2" line3 = "Item 3" template = """ <title>Greetings</title> <script> const csrf_token = '{{csrf}}'; </script> <body> Hi <span id="name">{{ name }}</span> ! It is now<span class="timestamp">{{ timestamp }}</span>. Your order is: <ul> <li>{{line1}}</li> <li>{{line2}}</li> <li>{{line3}}</li> </ul> </body> """ cls_validators = [ ValidationDirective("title", exp="Greetings"), ValidationDirective("name", exp=AutoExp, validator=CSSValidator("#name")), ] cls_filters = dict(html=[ FilterDirective( "timestamp", filter_=CSSRemoveFilter("span.timestamp", "timestamp", scalar=True), ), FilterDirective("csrf", filter_=RegexRemoveSaver("csrf_token", "csrf_token")), ])
class Test_Features_CustomLineValidation(Test_Features_Regex): """ This should pass, we are re-using the CSSValidation lookup for `item`""" # 👇 cls_validators = [ValidationDirective("item", exp=check_lineitems)] # 👇 name = "Mr. Rabbit" line1 = "Item 1" line2 = "Item 2" line3 = "Bad line 3" def test_it(self): Test_Features.test_it(self)
class Test_Features_Regex(Test_Features): """ This should fail """ cls_validators = [ # 👇 #👇 ValidationDirective( "item", exp=re.compile("^Item"), validator=CSSValidator("li.orderline") ) ] name = "Mr. Rabbit" line1 = "Item 1" line2 = "Item 2" line3 = "Bad line 3" def test_it(self): try: Test_Features.test_it(self) # pragma: no cover pylint: disable=unused-variable except (AssertionError,) as e: self.assertTrue("Bad" in str(e))
class Test_ValidationFailLine3(Test_ValidationNonScalar): cls_validators = [ # ValidationDirective("list", exp=re.compile("Item"), validator=CSSValidator("li"), active=True) ] name = "Ezr Vinh" line3 = "Line3" data = dict(line1="Item 11", line2="Item 12", line3=line3, name=name, now="today") fail_on_validate = "Line3" template = """
class Test_Validation_Fail(HTMLBase): cls_validators = [ ValidationDirective("name", exp=AutoExp, validator=CSSValidator("#name", "name")), # we have a title now ValidationDirective("title", active=True, exp=AutoExp), ] template = """ <html><head><title>Greetings<title></head><body>Hi <span id="name" >{{ name }}?</span>! It is now <span class="timestamp">{{ now }}</span>.</body></html> """ title = "Greetings" name = "Robert Gu" @mock.patch.dict(os.environ, di_mock_env) def test_001_fail_name(self): """get response""" try: data = dict(name="Woodcarver", now=datetime.datetime.now()) response = self.get(data=data) try: self.check_expectations(response=response) self.fail("should have failed name validation") except (AssertionError, ) as e: for text in ["name", "Gu", "Woodcarver"]: self.assertTrue(text in str(e)) tmp = self.assert_exp(response.content, "html") except (Exception, ) as e: if cpdb(): pdb.set_trace() raise @mock.patch.dict(os.environ, di_mock_env) def test_002_fail_title(self): """get response""" try: data = dict(name=self.name, now=datetime.datetime.now()) self.template = self.template.replace("Greetings", "Bonjour") response = self.get(data=data) try: self.check_expectations(response=response) self.fail("should have failed title validation") except (AssertionError, ) as e: for text in ["title"]: self.assertTrue(text in str(e)) tmp = self.assert_exp(response.content, "html") except (Exception, ) as e: if cpdb(): pdb.set_trace() raise
class HTMLBase(HTMLValidationMixin, LazyMixinBasic): cls_validators = [ValidationDirective("title", active=False)] cls_filters = dict(html=FilterDirective( "timestamp", filter_=CSSRemoveFilter("span.timestamp", "timestamp", scalar=True), ))