def test_expects_to_filter_by_audit_type(self): # Arrange domain = 'sub.domain.com' site = Site.from_domain_or_url(domain) page = Page(site) test_axe_report_path = helper.fixture_file_path( 'httpbin-org-page-all-violations.json') with open(test_axe_report_path, "r") as f: data = json.loads(f.read()) axe_errors = data["violations"] test_cases = [ # audit_type expected_violations_length ("design", 2), ("code", 3), (None, 5) ] for audit_type, expected_violations_length in test_cases: audit = AxePageAudit(page, audit_type) sorted_violations = [] # Act for error in axe_errors: sorted_violations += Violation.s_from_audit_axe_error( audit, error) # Assert self.assertEqual(expected_violations_length, len(sorted_violations))
def test_expect_successful_page_audit(self, webmock): # Arrange domain = 'httpbin.org' test_axe_report_path = helper.fixture_file_path('httpbin-org-page-all-violations.json') webmock.get(requests_mock.ANY, text='ok') site = Site.from_domain_or_url(domain) page = Page(site) audit_type = None # Assume self.assertIsNone(page.audit) self.assertPathExists(test_axe_report_path) # Act # Mock the AxeAudit generate_report method to return our test fixture file # path when page.axe_audit called. with patch.object(AxePageAudit, 'generate_report') as mocked_method: mocked_method.return_value = test_axe_report_path page.axe_audit(audit_type) # Assert self.assertIsNotNone(page.audit) self.assertEqual(page.site, site) self.assertIn(domain, page.url) self.assertEqual(5, len(page.audit.violations)) self.assertEqual(5, len(page.audit.errors)) self.assertEqual(0, len(page.audit.warnings))
def test_expects_to_validate_internal_urls(self, webmock): # Arrange webmock.get(requests_mock.ANY, text='ok') domain = 'sub.domain.com' site = Site.from_domain_or_url(domain) test_cases = [ # url, is_valid ['https://sub.domain.com/', True], ['https://sub.domain.com/foo', True], ['https://sub.domain.com/foo?q=foo', True], ['https://domain.com/', False], ['foo/bar', False], ['https://google.com', False], ['https://google.com/', False], ['https://sub.domain.com/mailto:[email protected]', False], ['https://sub.domain.com/tel:555-555-5555', False], ['https://sub.domain.com/fax:555-555-5555', False], ['https://sub.domain.com/foo#header', False], ['https://sub.domain.com/javascript: openMarker(1)', False], ['https://sub.domain.com/foo.jpg', False], ['https://sub.domain.com/foo.mp3', False], ['https://sub.domain.com/foo.mov', False], ['https://sub.domain.com/foo.pdf', False] ] # Act / Assert for url, expected in test_cases: is_valid = site.is_valid_internal_url(url) self.assertEqual(expected, is_valid, url)
def test_expects_page_instance(self): # Arrange domain = 'sub.domain.com' site = Site.from_domain_or_url(domain) # Act page = Page(site) # Assert self.assertIsInstance(page, Page) self.assertEqual(page.site, site) self.assertIn(domain, page.url)
def test_expects_spider_instance(self, webmock): # Arrange domain = 'sub.domain.com' site = Site.from_domain_or_url(domain) webmock.get(requests_mock.ANY, text='ok') # Act spider = SitemapSpider(site) # Assert self.assertIsInstance(spider, SitemapSpider) self.assertEqual(site, spider.site) self.assertIn(site.base_url, spider.base_url)
def test_expects_site_for_a_localhost_url(self): # Arrange url = 'http://localhost:3000/' # Act site = Site.from_domain_or_url(url) # Assert self.assertIsInstance(site, Site) self.assertEqual('http', site.scheme) self.assertEqual('', site.subdomain) self.assertEqual('localhost', site.domain) self.assertEqual('', site.tld) self.assertEqual('localhost', site.fqdn) self.assertEqual('http://localhost:3000/', site.url) self.assertEqual('http://localhost:3000', site.base_url)
def test_expects_templates(self): # Arrange url = 'https://sub.domain.com/path/subpath/subsubpath/index.html' site = Site.from_domain_or_url(url) page = Page(site) expected_templates = [ 'path/subpath/subsubpath/index.html', 'path/subpath/subsubpath', 'path/subpath', 'path' ] # Act templates = page.templates # Assert self.assertEqual(expected_templates, templates)
def audit(self): # Command-line options domain_or_url = self.app.pargs.domain_or_url[0] audit_type = self.app.pargs.audit_type use_templates = not self.app.pargs.no_templates site = Site.from_domain_or_url(domain_or_url, audit_type=audit_type, templates=use_templates) if self.app.pargs.crawl: audit = site.audit() else: audit = Page.audit(site, audit_type=audit_type) audit.write_violations_to_csv() print(audit.summary)
def test_expects_subtemplate(self): # Arrange test_cases = [ # url, expected_subtemplate ('https://sub.domain.com/path/subpath/subsubpath/index.html', 'path/subpath'), ('https://sub.domain.com/path/subpath/', 'path/subpath'), ('https://sub.domain.com/path/subpath', 'path/subpath'), ('https://sub.domain.com/path/', None), ('https://sub.domain.com/', None) ] # Act / Assert for url, expected_subtemplate in test_cases: # Act site = Site.from_domain_or_url(url) page = Page(site) # Assert self.assertEqual(expected_subtemplate, page.subtemplate, url)
def test_expects_paths(self): # Arrange test_cases = [ # url, expected_path ('https://sub.domain.com', ''), ('https://sub.domain.com/', ''), ('https://sub.domain.com/path', 'path'), ('https://sub.domain.com/path/', 'path'), ('https://sub.domain.com/path/subpath/', 'path/subpath'), ] # Act / Assert for url, expected_path in test_cases: # Act site = Site.from_domain_or_url(url) page = Page(site) # Assert self.assertEqual(expected_path, page.path, url) self.assertEqual(url, page.url)
def test_expects_violation_string_to_have_correct_information(self): # Arrange domain = 'sub.domain.com' site = Site.from_domain_or_url(domain) page = Page(site) source = 'test' identifier = 'test-error' severity = 'low' expected_output = 'test reported a low test-error error on http://sub.domain.com' # Act violation = Violation(page=page, source=source, identifier=identifier, severity=severity) # Same as __str__ magic method in Violation violation_str = str(violation) # Assert self.assertEqual(expected_output, violation_str)
def test_expects_violation_instance(self): # Arrange domain = 'sub.domain.com' site = Site.from_domain_or_url(domain) page = Page(site) source = 'test' identifier = 'test-error' severity = 'low' # Act violation = Violation(page=page, source=source, identifier=identifier, severity=severity) # Assert self.assertIsInstance(violation, Violation) self.assertEqual(page, violation.page) self.assertEqual(source, violation.source) self.assertEqual(identifier, violation.identifier) self.assertEqual(severity, violation.severity)
def test_expects_to_normalize_urls(self, webmock): # Arrange domain = 'sub.domain.com' site = Site.from_domain_or_url(domain) webmock.get(requests_mock.ANY, text='ok') test_cases = [ # link, expected_url ['http://sub.domain.com/', 'http://sub.domain.com'], ['http://sub.domain.com', 'http://sub.domain.com'], ['/', 'http://sub.domain.com'], ['/foo', 'http://sub.domain.com/foo'], ['foo', 'http://sub.domain.com/foo'], ['foo/bar', 'http://sub.domain.com/foo/bar'], ['https://google.com', 'https://google.com'], ['https://google.com/', 'https://google.com/'], ['http://localhost/', 'http://localhost/'], ['http://localhost:3000/', 'http://localhost:3000/'] ] # Act / Assert for url, expected_url in test_cases: normalized_url = site.normalize_url(url) self.assertEqual(expected_url, normalized_url)
def sitemap(self): site = Site.from_domain_or_url(self.app.pargs.domain_or_url[0]) sitemap_path = site.generate_sitemap() print("Generated sitemap: {}\nRuntime: {}".format( sitemap_path, site.runtime))