Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Fix test_welcome_practice #621

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 69 additions & 72 deletions test/test_welcome.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import contextlib
import time
import os

import pytest
from selenium.webdriver import Firefox, FirefoxOptions
Expand All @@ -13,143 +12,141 @@


@pytest.fixture
def random_user_webdriver(random_user):
def random_user_browser(random_user):
random_id, random_session = random_user

options = FirefoxOptions()
options.add_argument("--headless")
wd = Firefox(options=options)
browser = Firefox(options=options)

wd.get(f"{DOJO_URL}/login")
wd.find_element("id", "name").send_keys(random_id)
wd.find_element("id", "password").send_keys(random_id)
wd.find_element("id", "_submit").click()
return random_id, random_session, wd
browser.get(f"{DOJO_URL}/login")
browser.find_element("id", "name").send_keys(random_id)
browser.find_element("id", "password").send_keys(random_id)
browser.find_element("id", "_submit").click()
return random_id, random_session, browser


@contextlib.contextmanager
def vscode_terminal(wd):
module_window = wd.current_window_handle
def vscode_terminal(browser):
module_window = browser.current_window_handle

wd.switch_to.new_window("tab")
wd.get(f"{DOJO_URL}/workspace/code")
browser.switch_to.new_window("tab")
browser.get(f"{DOJO_URL}/workspace/code")

wait = WebDriverWait(wd, 30)
wait = WebDriverWait(browser, 30)
workspace_iframe = wait.until(EC.presence_of_element_located((By.ID, "workspace_iframe")))
wd.switch_to.frame(workspace_iframe)
browser.switch_to.frame(workspace_iframe)


def wait_for_active(selector):
def wait_for_selector(selector):
try:
element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, selector)))
wait.until(lambda _: wd.switch_to.active_element == element)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, selector)))
except Exception as e:
print(wd.get_full_page_screenshot_as_base64())
print(wd.switch_to.active_element.get_attribute("outerHTML"))
print(browser.get_full_page_screenshot_as_base64())
print(browser.switch_to.active_element.get_attribute("outerHTML"))
raise e

wait_for_active("button.getting-started-step")
wd.switch_to.active_element.send_keys(Keys.CONTROL, Keys.SHIFT, "`") # Shortcut to open terminal
wait_for_active("textarea.xterm-helper-textarea")
wait_for_selector("button.getting-started-step")
browser.switch_to.active_element.send_keys(Keys.CONTROL, Keys.SHIFT, "`") # Shortcut to open terminal
wait_for_selector("textarea.xterm-helper-textarea")

yield wd.switch_to.active_element
yield browser.switch_to.active_element

wd.close()
wd.switch_to.window(module_window)
browser.close()
browser.switch_to.window(module_window)


@contextlib.contextmanager
def desktop_terminal(wd, user_id):
module_window = wd.current_window_handle
def desktop_terminal(browser, user_id):
module_window = browser.current_window_handle

wd.switch_to.new_window("tab")
wd.get(f"{DOJO_URL}/workspace/desktop")
browser.switch_to.new_window("tab")
browser.get(f"{DOJO_URL}/workspace/desktop")
time.sleep(2)
workspace_run("DISPLAY=:0 xfce4-terminal &", user=user_id)
wd.switch_to.frame("workspace")
e = wd.find_element("id", "noVNC_keyboardinput")
browser.switch_to.frame("workspace")
e = browser.find_element("id", "noVNC_keyboardinput")
time.sleep(2)

yield e

wd.close()
wd.switch_to.window(module_window)
browser.close()
browser.switch_to.window(module_window)


# Expands the accordion entry of the challenge
def challenge_expand(wd, idx):
wd.refresh()
wd.find_element("id", f"challenges-header-button-{idx}").click()
def challenge_expand(browser, idx):
browser.refresh()
browser.find_element("id", f"challenges-header-button-{idx}").click()
time.sleep(0.5)


def challenge_start(wd, idx, practice=False):
challenge_expand(wd, idx)
body = wd.find_element("id", f"challenges-body-{idx}")
def challenge_start(browser, idx, practice=False):
challenge_expand(browser, idx)
body = browser.find_element("id", f"challenges-body-{idx}")
body.find_element("id", "challenge-practice" if practice else "challenge-start").click()
while "started" not in body.find_element("id", "result-message").text:
time.sleep(0.5)
time.sleep(1)


def challenge_submit(wd, idx, flag):
challenge_expand(wd, idx)
body = wd.find_element("id", f"challenges-body-{idx}")
def challenge_submit(browser, idx, flag):
challenge_expand(browser, idx)
body = browser.find_element("id", f"challenges-body-{idx}")
body.find_element("id", "challenge-input").send_keys(flag)
body.find_element("id", "challenge-submit").click()
while "Correct" not in body.find_element("id", "result-message").text:
time.sleep(0.5)

# Gets the accordion entry index
def challenge_idx(wd, name):
num_challenges = len(wd.find_elements("id", "challenge-start"))
idx = next(n for n in range(num_challenges) if wd.find_element("id", f"challenges-header-button-{n+1}").text.split("\n")[0] == name)
def challenge_idx(browser, name):
num_challenges = len(browser.find_elements("id", "challenge-start"))
idx = next(n for n in range(num_challenges) if browser.find_element("id", f"challenges-header-button-{n+1}").text.split("\n")[0] == name)
return idx+1


def test_welcome_desktop(random_user_webdriver, welcome_dojo):
random_id, _, wd = random_user_webdriver
wd.get(f"{DOJO_URL}/welcome/welcome")
idx = challenge_idx(wd, "The Flag File")
def test_welcome_desktop(random_user_browser, welcome_dojo):
random_id, _, browser = random_user_browser
browser.get(f"{DOJO_URL}/welcome/welcome")
idx = challenge_idx(browser, "The Flag File")

challenge_start(wd, idx)
with desktop_terminal(wd, random_id) as vs:
challenge_start(browser, idx)
with desktop_terminal(browser, random_id) as vs:
vs.send_keys("/challenge/solve; cat /flag | tee /tmp/out\n")
time.sleep(5)
flag = workspace_run("tail -n1 /tmp/out", user=random_id).stdout.split()[-1]
challenge_submit(wd, idx, flag)
wd.close()
challenge_submit(browser, idx, flag)
browser.close()


def test_welcome_vscode(random_user_webdriver, welcome_dojo):
random_id, _, wd = random_user_webdriver
wd.get(f"{DOJO_URL}/welcome/welcome")
idx = challenge_idx(wd, "Challenge Programs")
def test_welcome_vscode(random_user_browser, welcome_dojo):
random_id, _, browser = random_user_browser
browser.get(f"{DOJO_URL}/welcome/welcome")
idx = challenge_idx(browser, "Challenge Programs")

challenge_start(wd, idx)
with vscode_terminal(wd) as vs:
challenge_start(browser, idx)
with vscode_terminal(browser) as vs:
vs.send_keys("/challenge/solve | tee /tmp/out\n")
time.sleep(5)
flag = workspace_run("tail -n1 /tmp/out", user=random_id).stdout.split()[-1]
challenge_submit(wd, idx, flag)
wd.close()
challenge_submit(browser, idx, flag)
browser.close()


def test_welcome_practice(random_user_webdriver, welcome_dojo):
random_id, _, wd = random_user_webdriver
wd.get(f"{DOJO_URL}/welcome/welcome")
idx = challenge_idx(wd, "Using Practice Mode")
def test_welcome_practice(random_user_browser, welcome_dojo):
random_id, _, browser = random_user_browser
browser.get(f"{DOJO_URL}/welcome/welcome")
idx = challenge_idx(browser, "Using Practice Mode")

challenge_start(wd, idx, practice=True)
with vscode_terminal(wd) as vs:
challenge_start(browser, idx, practice=True)
with vscode_terminal(browser) as vs:
vs.send_keys("sudo chmod 644 /challenge/secret\n")
vs.send_keys("cp /challenge/secret /home/hacker/\n")
time.sleep(1)

challenge_start(wd, idx, practice=False)
with vscode_terminal(wd) as vs:
challenge_start(browser, idx, practice=False)
with vscode_terminal(browser) as vs:
vs.send_keys("/challenge/solve < secret | tee /tmp/out\n")
time.sleep(5)
flag = workspace_run("tail -n1 /tmp/out", user=random_id).stdout.split()[-1]
challenge_submit(wd, idx, flag)
wd.close()
challenge_submit(browser, idx, flag)
browser.close()
Loading