From b1dbd94b0059bc81e31278da28485769b76d8515 Mon Sep 17 00:00:00 2001 From: Lan Hui Date: Sun, 22 Sep 2024 10:50:49 +0800 Subject: [PATCH] Add a regression test script and fix bug --- Script.php | 16 ++++---- test/SeleniumHui/helper.py | 33 ++++++++++++++++ test/SeleniumHui/test_lrr.py | 73 ++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 test/SeleniumHui/helper.py create mode 100644 test/SeleniumHui/test_lrr.py diff --git a/Script.php b/Script.php index 3bff38d..9329adc 100644 --- a/Script.php +++ b/Script.php @@ -170,8 +170,8 @@ if (!empty($_POST["form_signup"])) { // apply password_hash() $password_hash = password_hash($password, PASSWORD_DEFAULT); - $sql = "INSERT INTO `users_table`(`Email`, `Password`, `Full_Name`, `UserType`, `Student_ID`) VALUES " - . "('$email','$password_hash','$fullname','Student','$student_id')"; + $sql = "INSERT INTO `users_table`(`Email`, `Password`, `HashPassword`, `Full_Name`, `UserType`, `Student_ID`) VALUES " + . "('$email','$password_hash','','$fullname','Student','$student_id')"; $_SESSION['user_fullname'] =$_SESSION['user_fullname_temp']; @@ -356,17 +356,15 @@ if (!empty($_POST["form_createlecturrer"])){ exit; } $password_hash = password_hash("$password", PASSWORD_DEFAULT); - $sql = "INSERT INTO `users_table`(`Email`, `Password`, `Full_Name`, `UserType`) VALUES " - . "('$email','$password_hash','$fullname','$type')"; + $sql = "INSERT INTO `users_table`(`Email`, `Password`, `HashPassword`, `Full_Name`, `UserType`) VALUES ('$email','$password_hash','','$fullname','$type')"; - if ($con->query($sql) === TRUE) { + try { + $result = mysqli_query($con, $sql); $_SESSION["info_Admin_Users"] = $type . " user created successfully. Use email " . $email . " as account name and ". $password ." as password."; header("Location: Admin.php"); - - } else { - alert("Error: " . $sql . "
" . $con->error); + } catch (Exception $ex) { + echo "$ex"; } - } // ### FUNCTION TO GENERATE INITIAL PASSWORDS ###// diff --git a/test/SeleniumHui/helper.py b/test/SeleniumHui/helper.py new file mode 100644 index 0000000..bea59f9 --- /dev/null +++ b/test/SeleniumHui/helper.py @@ -0,0 +1,33 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.common.exceptions import NoSuchElementException, UnexpectedAlertPresentException + + +def login(driver, url, username, password): + try: + driver.get(url) + + # Fill in the login form + user_input = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "user_name")) + ) + user_input.send_keys(username) + + password_input = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "user_password")) + ) + password_input.send_keys(password) + + # Click the login button + login_button = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "login_btn")) + ) + login_button.click() + + # Wait for the admin_tab to become clickable + admin_tab = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "admin_tab")) + ) + except (NoSuchElementException, UnexpectedAlertPresentException) as e: + return f"Error: {str(e)}" diff --git a/test/SeleniumHui/test_lrr.py b/test/SeleniumHui/test_lrr.py new file mode 100644 index 0000000..b5f3db1 --- /dev/null +++ b/test/SeleniumHui/test_lrr.py @@ -0,0 +1,73 @@ +from helper import login +from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +import time + +def test_admin_can_create_lecturer_account(driver, url, admin_username, admin_password, restore_database): + # Administrator (admin@qq.com, password 123) logs in + driver.maximize_window() + login(driver, url, admin_username, admin_password) + + # Create a Lecture account for mrlan@qq.com, password [123Abc!] + tab = driver.find_element(By.ID, 'tab_ins_accounts') + tab.click() + elem = driver.find_element(By.NAME, 'fullname') + elem.send_keys('Mr Lan') + elem = driver.find_element(By.NAME, 'email') + elem.send_keys('mrlan@qq.com') + elem = driver.find_element(By.NAME, 'password') + elem.send_keys('123Abc!!') + radio_button = driver.find_element(By.NAME, 'type') + radio_button.click() + button = driver.find_element(By.NAME, 'create_btn') + button.click() + time.sleep(1) + + # Log out Admin + logout_button = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable( + (By.XPATH, "//a[contains(@class, 'nav-link') and contains(@href, 'logout.php')]")) + ) + logout_button.click() + time.sleep(1) + + # Log in Lecture account + login(driver, url, 'mrlan@qq.com', '123Abc!!') + elems = driver.find_elements(By.CLASS_NAME, 'nav-link') + assert '(Lecturer)' in elems[0].text + driver.quit() + + + +def test_lecturer_can_create_course(): + # Lecturer mrlan@qq.com logs in + # Create a course called CSC1001 Advanced Software Engineering, 2024 + assert True + + +def test_lecturer_can_post_assignment(): + # Lecturer mrlan@qq.com logs in + # Create an assignment called Take-home quiz 1 for course CSC1001 + assert True + + +def test_lecturer_can_add_student_numbers(): + # Lecturer mrlan@qq.com logs in + # Add 6 ASE student numbers + assert True + + +def test_student_can_sign_up(): + # Student with recognizable student number can sign up an account + assert True + + +def test_student_can_join_course(): + # Student can join CSC1001 Advanced Software Engineering + assert True + + +def test_student_can_submit_assignment(): + # Student can submit Take-home quiz 1 for CSC1001 + assert True