From 881ca9a2371dcf3c496ba8b2e0028e00af09513e Mon Sep 17 00:00:00 2001 From: AnsibleGuy Date: Wed, 14 Feb 2024 21:33:08 +0100 Subject: [PATCH] added basic web-ui integration tests --- .github/workflows/release.yml | 2 +- .github/workflows/test.yml | 2 +- README.md | 2 + requirements_test.txt | 3 ++ scripts/test.sh | 25 ++++++++++- test/integration/webui/main.py | 81 ++++++++++++++++++++++++++++++++++ 6 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 test/integration/webui/main.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0fbda08..c8f2e04 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -45,7 +45,7 @@ jobs: set +e echo 'RUNNING APP' export AW_DB="/tmp/$(date +%s).aw.db" - timeout 5 python3 -m ansible-webui + timeout 20 python3 -m ansible-webui ec="$?" echo 'CLEANUP' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index da78f79..f5cc9ca 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -84,7 +84,7 @@ jobs: run: | set +e export AW_DB="/tmp/$(date +%s).aw.db" - timeout 2 python3 src/ansible-webui + timeout 10 python3 src/ansible-webui ec="$?" if [[ "$ec" != "124" ]] then diff --git a/README.md b/README.md index fc0cc02..f9ae254 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,8 @@ There are multiple Ansible WebUI products - how do they compare to this product? - [ ] Integration Tests + - [x] Basic WebUI checks + - [ ] API Endpoints - [ ] Permission system diff --git a/requirements_test.txt b/requirements_test.txt index e079f8a..6828c66 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1 +1,4 @@ pytest +selenium +selenium-wire +chromedriver-autoinstaller diff --git a/scripts/test.sh b/scripts/test.sh index 85cdaad..5e11d79 100644 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -5,7 +5,30 @@ set -e cd "$(dirname "$0")/.." echo '' -echo 'TESTING Python' +echo 'UNIT TESTS' echo '' python3 -m pytest + +echo '' +echo 'INTEGRATION TESTS WEB-UI' +echo '' + +if pgrep -f 'ansible-webui' +then + echo 'An instance of Ansible-WebUI is already running! Stop it first (pkill -f ansible-webui)' + exit 1 +fi + +# shellcheck disable=SC2155 +export AW_DB="/tmp/$(date +%s).aw.db" +export AW_ADMIN='tester' +export AW_ADMIN_PWD='someSecret!Pwd' +python3 src/ansible-webui/ >/dev/null & +sleep 5 + +python3 test/integration/webui/main.py + +sleep 1 + +pkill -f 'ansible-webui' diff --git a/test/integration/webui/main.py b/test/integration/webui/main.py new file mode 100644 index 0000000..2aa6aa6 --- /dev/null +++ b/test/integration/webui/main.py @@ -0,0 +1,81 @@ +from os import environ +from time import sleep + +from seleniumwire import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.common.keys import Keys +import chromedriver_autoinstaller + +BASE_URL = 'http://127.0.0.1:8000' +options = webdriver.ChromeOptions() +options.add_argument('--headless') +options.add_argument('--disable-extensions') +options.add_argument('--remote-debugging-port=9222') +chromedriver_autoinstaller.install() +DRIVER = webdriver.Chrome(options=options) + + +def _response_code(url: str) -> (int, None): + for request in DRIVER.requests: + if request.response and request.url == url: + return request.response.status_code + + return None + + +def _response_ok(url: str) -> bool: + return _response_code(url) in [200, 302] + + +def login(user: str, pwd: str): + print('TESTING LOGIN') + login_url = f'{BASE_URL}/a/login/' + DRIVER.get(login_url) + DRIVER.find_element(By.ID, 'id_username').send_keys(user) + DRIVER.find_element(By.ID, 'id_password').send_keys(pwd) + DRIVER.find_element(By.ID, 'id_password').send_keys(Keys.RETURN) + assert _response_ok(login_url) + + login_redirect = f'{BASE_URL}/ui/jobs/manage' + assert DRIVER.current_url == login_redirect + assert _response_ok(login_redirect) + + +def test_get_locations(locations: list): + for location in locations: + print(f'TESTING GET {location}') + url = f'{BASE_URL}/{location}' + sleep(1) + DRIVER.get(url) + assert _response_ok(url) + + +def test_main_pages(): + test_get_locations([ + 'ui/jobs/manage', 'ui/jobs/log', 'ui/jobs/credentials', + 'ui/settings/api_keys', 'ui/settings/permissions', + 'ui/admin/', 'ui/api_docs', 'ui/system/environment', + ]) + + +def test_actions_views(): + test_get_locations([ + 'ui/jobs/manage/job', + 'ui/jobs/credentials/0?global=false', 'ui/jobs/credentials/0?global=true', + 'ui/settings/permissions/0', + ]) + + +def main(): + try: + login(user=environ['AW_ADMIN'], pwd=environ['AW_ADMIN_PWD']) + test_main_pages() + test_actions_views() + # todo: add action post variants + + finally: + DRIVER.quit() + + +if __name__ == '__main__': + main()