1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-29 08:56:23 -07:00

Use staff board from options and create roles for testing moderator and janitor stuff

This commit is contained in:
Eggbertx 2024-12-14 13:21:43 -08:00
parent 23a35d7cfd
commit d8cbf4dfa5
4 changed files with 93 additions and 54 deletions

View file

@ -18,8 +18,13 @@ default_post_password = "12345"
default_board1 = "test"
default_board2 = "test2"
default_staff_board = "selenium"
default_staff_username = "admin"
default_staff_password = "password"
default_admin_username = "selenium_admin"
default_admin_password = "password"
default_moderator_username = "selenium_mod"
default_moderator_password = "password"
default_janitor_username = "selenium_janitor"
default_janitor_password = "password"
class TestingOptions:
browser: str
@ -36,8 +41,12 @@ class TestingOptions:
message: str
upload_path: str
post_password: str
staff_username: str
staff_password: str
admin_username: str
admin_password: str
moderator_username: str
moderator_password: str
janitor_username: str
janitor_password: str
@property
def keep_open(self):
@ -51,7 +60,7 @@ class TestingOptions:
@staticmethod
def from_dict(src_dict:dict[str,object]):
options = TestingOptions(src_dict.get("browser", ""), src_dict.get("headless", False), src_dict.get("keepopen"))
options = TestingOptions(src_dict.get("browser", ""), src_dict.get("headless", False), src_dict.get("keep_open", False))
options.site = src_dict.get("site", default_site)
options.board1 = src_dict.get("board1", default_board1)
options.board2 = src_dict.get("board2", default_board2)
@ -62,8 +71,12 @@ class TestingOptions:
options.message = src_dict.get("message", default_message)
options.upload_path = src_dict.get("upload", default_upload)
options.post_password = src_dict.get("post_password", default_post_password)
options.staff_username = src_dict.get("staff_username", default_staff_username)
options.staff_password = src_dict.get("staff_password", default_staff_password)
options.admin_username = src_dict.get("admin_username", default_admin_username)
options.admin_password = src_dict.get("admin_password", default_admin_password)
options.moderator_username = src_dict.get("moderator_username", default_moderator_username)
options.moderator_password = src_dict.get("moderator_password", default_moderator_password)
options.janitor_username = src_dict.get("janitor_username", default_janitor_username)
options.janitor_password = src_dict.get("janitor_password", default_janitor_password)
return options
@ -81,8 +94,8 @@ class TestingOptions:
self.message = default_message
self.upload_path = default_upload
self.post_password = default_post_password
self.staff_username = default_staff_username
self.staff_password = default_staff_password
self.admin_username = default_admin_username
self.admin_password = default_admin_password
match browser:
case "firefox":
@ -117,7 +130,10 @@ class TestingOptions:
def board_exists(self, board: str):
req = urlopen(urljoin(default_site, "boards.json")) # skipcq: BAN-B310
boards_json_url = urljoin(self.site, "boards.json")
if not boards_json_url.lower().startswith("http"):
raise ValueError(f"Invalid site URL (expected http): {self.site}")
req = urlopen(boards_json_url)
boards = json.load(req)['boards']
for entry in boards:
if entry['board'] == board:

View file

@ -8,8 +8,8 @@ from argparse import ArgumentParser
import unittest
from .options import (TestingOptions, default_site, default_name, default_email, default_message, default_subject,
default_upload, default_post_password, default_board1, default_board2, default_staff_board, default_staff_username,
default_staff_password)
default_upload, default_post_password, default_board1, default_board2, default_staff_board, default_admin_username,
default_admin_password, default_moderator_username, default_moderator_password, default_janitor_username, default_janitor_password)
from .tests import SeleniumTestCase
from .tests.test_mgmt import TestManageActions
from .tests.test_posting import TestPosting
@ -85,10 +85,18 @@ def setup_selenium_args(parser:ArgumentParser):
help="Sets the file to be used when posting")
parser.add_argument("--post_password", default=default_post_password,
help="Sets the post password")
parser.add_argument("--staff_username", default=default_staff_username,
help="Sets the staff username to be used when logging in")
parser.add_argument("--staff_password", default=default_staff_password,
help="Sets the staff password to be used when logging in")
parser.add_argument("--admin_username", default=default_admin_username,
help="Sets the username to be used when logging in as an admin. Admin tests will fail if this does not exist")
parser.add_argument("--admin_password", default=default_admin_password,
help="Sets the password to be used when logging in as an admin. Admin tests will fail if this does not exist")
parser.add_argument("--moderator_username", default=default_moderator_username,
help="Sets the username to be used when logging in as a moderator. Moderator tests will fail if this does not exist")
parser.add_argument("--moderator_password", default=default_moderator_password,
help="Sets the password to be used when logging in as a moderator. Moderator tests will fail if this does not exist")
parser.add_argument("--janitor_username", default=default_janitor_username,
help="Sets the username to be used when logging in as a janitor. Janitor tests will fail if this does not exist")
parser.add_argument("--janitor_password", default=default_janitor_password,
help="Sets the password to be used when logging in as a janitor. Janitor tests will fail if this does not exist")
parser.add_argument("--single-test", default="",
help="If specified, only the test method with this name will be run")

View file

@ -8,36 +8,26 @@ from selenium.webdriver.support.select import Select
from . import SeleniumTestCase
from ..util.posting import make_post, delete_post
import random
from ..util.manage import staff_login
from ..util.manage import staff_login, StaffRole, staff_logout
class TestManageActions(SeleniumTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
def setUp(self):
staff_login(self.options)
return super().setUp()
staff_login(self.options, StaffRole.Admin)
def tearDown(self):
staff_logout(self.options, True)
def get_recent_post_link(self, msg_text: str):
trs = self.driver.find_elements(by=By.CSS_SELECTOR, value="#content table tr")
for tr in trs:
tds = tr.find_elements(by=By.TAG_NAME, value="td")
for c, item in enumerate(tds):
if item.text == msg_text:
# found the post we made
link = tds[c-2].find_element(by=By.LINK_TEXT, value="Post")
return link
def test_login(self):
self.assertEqual(
self.driver.find_element(by=By.CSS_SELECTOR, value="header h1").text,
"Dashboard",
"Testing staff login")
self.options.goto_page("manage/recentposts")
tds = self.driver.find_elements(by=By.CSS_SELECTOR, value="table#recentposts td")
for c, item in enumerate(tds):
if item.text == msg_text:
# found the post we made
link = tds[c-2].find_element(by=By.LINK_TEXT, value="Post")
return link
def test_logoutEverywhere(self):
@ -59,9 +49,9 @@ class TestManageActions(SeleniumTestCase):
new_msg = f"test_recentPosts {random.randint(0, 9999)}"
old_msg = self.options.message
self.options.message = new_msg
make_post(self.options, "test", self)
make_post(self.options, self.options.board1, self)
self.options.message = old_msg
staff_login(self.options)
staff_login(self.options, StaffRole.Admin)
self.driver.find_element(by=By.LINK_TEXT, value="Recent posts").click()
WebDriverWait(self.driver, 10).until(
EC.url_contains("/manage/recentposts"))
@ -71,7 +61,7 @@ class TestManageActions(SeleniumTestCase):
self.assertIsNotNone(post_link, "Found recent post in recent posts list")
post_link.click()
WebDriverWait(self.driver, 10).until(
EC.url_contains(link_href)) # link_href should be something like "/seleniumtesting/ref/<threadOP>.html#<postID>"
EC.url_contains(link_href)) # link_href should be something like "/selenium/ref/<threadOP>.html#<postID>"
fragment = urllib.parse.urldefrag(self.driver.current_url).fragment
delete_post(self.options, fragment, self.options.post_password)
@ -82,13 +72,13 @@ class TestManageActions(SeleniumTestCase):
def test_makeBoard(self):
if self.options.board_exists("seleniumtesting"):
raise Exception("Board /seleniumtests/ already exists")
if self.options.board_exists(self.options.staff_board):
raise Exception(f"Board /{self.options.staff_board}/ already exists")
self.options.goto_page("manage/boards")
# fill out the board creation form
self.driver.find_element(by=By.NAME, value="dir").\
send_keys("seleniumtesting")
send_keys(self.options.staff_board)
self.driver.find_element(by=By.NAME, value="title").\
send_keys("Selenium testing")
self.driver.find_element(by=By.NAME, value="subtitle").\
@ -100,15 +90,15 @@ class TestManageActions(SeleniumTestCase):
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((
By.CSS_SELECTOR,
'div#topbar a[href="/seleniumtesting/"]')))
make_post(self.options, "seleniumtesting", self)
f'div#topbar a[href="/{self.options.staff_board}/"]')))
make_post(self.options, self.options.staff_board, self)
self.options.goto_page("manage/boards")
sel = Select(self.driver.find_element(by=By.ID, value="modifyboard"))
sel.select_by_visible_text("/seleniumtesting/ - Selenium testing")
sel.select_by_visible_text(f"/{self.options.staff_board}/ - Selenium testing")
self.driver.find_element(by=By.NAME, value="dodelete").click()
self.driver.switch_to.alert.accept()
WebDriverWait(self.driver, 10).until_not(
EC.presence_of_element_located((
By.CSS_SELECTOR,
'div#topbar a[href="/seleniumtesting/"]')))
f'div#topbar a[href="/{self.options.staff_board}/"]')))

View file

@ -1,17 +1,42 @@
from enum import Enum, auto
from selenium.webdriver.common.by import By
from ..options import TestingOptions
class StaffRole(Enum):
Janitor = auto()
Moderator = auto()
Admin = auto()
def is_logged_in(options: TestingOptions):
options.goto_page("manage/login")
return options.driver.find_element(by=By.CSS_SELECTOR, value="h1#board-title").text == "Dashboard"
def staff_login(options: TestingOptions):
if is_logged_in(options):
return
def staff_login(options: TestingOptions, role: StaffRole):
options.goto_page("manage/logout")
options.goto_page("manage")
options.driver.find_element(by=By.NAME, value="username").send_keys(options.staff_username)
options.driver.find_element(by=By.NAME, value="password").send_keys(options.staff_password)
username = ""
password = ""
match role:
case StaffRole.Janitor:
username = options.janitor_username
password = options.janitor_password
case StaffRole.Moderator:
username = options.moderator_username
password = options.moderator_password
case StaffRole.Admin:
username = options.admin_username
password = options.admin_password
case _:
raise ValueError(f"Invalid role: {role}")
options.driver.find_element(by=By.NAME, value="username").send_keys(username)
options.driver.find_element(by=By.NAME, value="password").send_keys(password)
options.driver.find_element(by=By.CSS_SELECTOR, value="input[value=Login]").click()
def staff_logout(options: TestingOptions, clear_sessions:bool = False):
options.goto_page("manage/clearmysessions" if clear_sessions else "manage/logout")