Skip to content

Commit

Permalink
added basic web-ui integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed Feb 14, 2024
1 parent 696820e commit 881ca9a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pytest
selenium
selenium-wire
chromedriver-autoinstaller
25 changes: 24 additions & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
81 changes: 81 additions & 0 deletions test/integration/webui/main.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 881ca9a

Please sign in to comment.