test: tests for checking links from rendered templetes

!33 #15
This commit is contained in:
2024-06-20 01:54:12 +09:30
parent 58b134ae30
commit 6d3984f6e1
5 changed files with 272 additions and 4 deletions

View File

@ -0,0 +1,86 @@
from app.urls import urlpatterns
class Data:
def parse_urls(self, patterns, parent_route = None) -> list:
urls = []
root_paths = [
'access',
# 'account',
# 'api',
'config_management',
'history',
'itam',
'organization',
'settings'
]
for url in patterns:
if hasattr(url, 'pattern'):
route = None
if hasattr(url.pattern, '_route'):
if parent_route:
route = parent_route + url.pattern._route
route = str(route).replace('<int:device_id>', '1')
route = str(route).replace('<int:group_id>', '1')
route = str(route).replace('<int:operating_system_id>', '1')
route = str(route).replace('<int:organization_id>', '1')
route = str(route).replace('<int:pk>', '1')
route = str(route).replace('<int:software_id>', '1')
route = str(route).replace('<int:team_id>', '1')
if route != '' and route not in urls:
urls += [ route ]
else:
route = url.pattern._route
route = str(route).replace('<int:device_id>', '1')
route = str(route).replace('<int:group_id>', '1')
route = str(route).replace('<int:operating_system_id>', '1')
route = str(route).replace('<int:organization_id>', '1')
route = str(route).replace('<int:pk>', '1')
route = str(route).replace('<int:software_id>', '1')
route = str(route).replace('<int:team_id>', '1')
if str(url.pattern._route).replace('/', '') in root_paths:
if route != '' and route not in urls:
urls += [ route ]
if hasattr(url, 'url_patterns'):
if str(url.pattern._route).replace('/', '') in root_paths:
urls += self.parse_urls(patterns=url.url_patterns, parent_route=url.pattern._route)
return urls
def __init__(self):
urls = []
patterns = urlpatterns
urls_found = self.parse_urls(patterns=patterns)
for url in urls_found:
if url not in urls:
urls += [ url ]
self.urls = urls

View File

@ -0,0 +1,141 @@
import pytest
import re
import requests
import unittest
from django.test import LiveServerTestCase
from app.urls import urlpatterns
from conftest import Data
@pytest.mark.skip(reason="test server required to be setup so tests work.")
class TestRenderedTemplateLinks:
"""UI Links tests """
server_host: str = '127.0.0.1'
# server_host: str = '192.168.1.172'
server_url: str = 'http://' + server_host + ':8002/'
data = Data()
driver = None
""" Chrome webdriver """
session = None
""" Client session that is logged into the dejango site """
def setup_class(self):
""" Set up the test
1. fetch session cookie
2. login to site
3. save session for use in tests
"""
self.session = requests.Session()
# fetch the csrf token
self.session.get(
url = self.server_url + 'account/login/',
)
# login
self.client = self.session.post(
url = self.server_url + 'account/login/',
data = {
'username': 'admin',
'password': 'admin',
'csrfmiddlewaretoken': self.session.cookies._cookies[self.server_host]['/']['csrftoken'].value
}
)
@pytest.mark.parametrize(
argnames='url',
argvalues=[link for link in data.urls],
ids=[link for link in data.urls]
)
def test_ui_no_http_forbidden(self, url):
""" Test Page Links
Scrape the page for links and ensure none return HTTP/403.
Test failure denotes a link on a page that should have been filtered out by testing for user
permissions within the template.
Args:
url (str): Page to test
"""
response = self.session.get(
url = str(self.server_url + url)
)
# Failsafe to ensure no redirection and that page exists
assert len(response.history) == 0
assert response.status_code == 200
page_urls = []
page = str(response.content)
links = re.findall('href=\"([a-z\/0-9]+)\"', page)
for link in links:
page_link_response = self.session.get(
url = str(self.server_url + link)
)
# Failsafe to ensure no redirection
assert len(response.history) == 0
assert page_link_response.status_code != 403
@pytest.mark.parametrize(
argnames='url',
argvalues=[link for link in data.urls],
ids=[link for link in data.urls]
)
def test_ui_no_http_not_found(self, url):
""" Test Page Links
Scrape the page for links and ensure none return HTTP/404.
Test failure denotes a link on a page that should not exist within the template.
Args:
url (str): Page to test
"""
response = self.session.get(
url = str(self.server_url + url)
)
# Failsafe to ensure no redirection and that page exists
assert len(response.history) == 0
assert response.status_code == 200
page_urls = []
page = str(response.content)
links = re.findall('href=\"([a-z\/0-9]+)\"', page)
for link in links:
page_link_response = self.session.get(
url = str(self.server_url + link)
)
# Failsafe to ensure no redirection
assert len(response.history) == 0
assert page_link_response.status_code != 404