Files
quayso.sisvietnam.vn/tests/e2e/test_backend.py
T

157 lines
7.5 KiB
Python

from playwright.sync_api import Page, expect
def test_admin_login_failures(page: Page, base_url: str):
# Lỗi: Để trống
page.goto(f"{base_url}/admin")
page.evaluate("document.querySelectorAll('form').forEach(f => f.setAttribute('novalidate', 'novalidate'))")
page.locator('input[type="submit"][value="Login"]').click()
# Laravel sẽ validation form
# Lỗi: Sai mật khẩu
page.goto(f"{base_url}/admin")
page.fill("#email", "admin")
page.fill("#pass", "wrongpassword123")
page.locator('input[type="submit"][value="Login"]').click()
expect(page.locator("text=Email hoặc Password không chính xác")).to_be_visible()
def test_admin_login_success(page: Page, base_url: str):
page.goto(f"{base_url}/admin")
page.fill("#email", "admin")
page.fill("#pass", "123456")
page.locator('input[type="submit"][value="Login"]').click()
expect(page).to_have_url(f"{base_url}/admin/account")
def test_admin_add_prize_validation(page: Page, base_url: str):
# Yêu cầu đã đăng nhập
page.goto(f"{base_url}/admin")
page.fill("#email", "admin")
page.fill("#pass", "123456")
page.locator('input[type="submit"][value="Login"]').click()
# Truy cập trang thêm giải thưởng
page.goto(f"{base_url}/admin/prize/update")
# Để trống tên giải
page.evaluate("document.querySelectorAll('form').forEach(f => f.setAttribute('novalidate', 'novalidate'))")
page.locator('button.btn-custom[type="submit"]').first.click()
expect(page.locator("text=Nhập tên giải thưởng").first).to_be_visible()
def test_admin_advanced_operations(page: Page, base_url: str):
# Yêu cầu đã đăng nhập
page.goto(f"{base_url}/admin")
page.fill("#email", "admin")
page.fill("#pass", "123456")
page.locator('input[type="submit"][value="Login"]').click()
# TC_B08: Upload Ảnh sai định dạng
page.goto(f"{base_url}/admin/prize/update")
page.locator("input[name='prize_image']").set_input_files("tests/e2e/dummy.txt")
page.locator("input[name='prize_name']").fill("Giải test upload")
page.locator("input[name='prize_number']").fill("10")
page.locator("input[name='prize_order']").fill("1")
page.locator("select[name='prize_status']").select_option("1")
page.evaluate("document.querySelectorAll('form').forEach(f => f.setAttribute('novalidate', 'novalidate'))")
page.locator('button.btn-custom[type="submit"]').first.click()
# Laravel sẽ báo lỗi hình ảnh phải là image
expect(page.locator("text=Có lỗi xảy ra").first).to_be_visible()
# TC_B09: Cập nhật Giải thưởng
# Giả sử có sẵn giải thưởng ở dòng đầu tiên của danh sách
page.goto(f"{base_url}/admin/prizes")
edit_link = page.locator("table tbody tr:first-child td a:has-text('Sửa')").first
if edit_link.is_visible():
edit_link.click()
page.locator("input[name='prize_number']").fill("100")
page.locator('button.btn-custom[type="submit"]:has-text("Cập nhật")').first.click()
expect(page.locator("text=Cập nhật thành công")).to_be_visible()
# TC_B10: Xóa Giải thưởng
page.goto(f"{base_url}/admin/prizes")
delete_link = page.locator("table tbody tr:first-child td a.delete-prize").first
if delete_link.is_visible():
page.on("dialog", lambda dialog: dialog.accept()) # Auto accept confirm box
delete_link.click()
expect(page.locator("text=Xóa thành công")).to_be_visible()
# TC_B11: Cập nhật Cấu hình hệ thống
page.goto(f"{base_url}/admin/parameter")
# Sử dụng JS để set data cho CKEditor thay vì fill textarea bị ẩn
page.evaluate("if(typeof CKEDITOR !== 'undefined') { CKEDITOR.instances.game_rule.setData('Nội dung thể lệ game mới'); } else { document.querySelector('textarea[name=\"game_rule\"]').value = 'Nội dung thể lệ game mới'; }")
page.locator('button[type="submit"]:has-text("Cập nhật")').first.click()
expect(page.locator("text=Cập nhật thành công")).to_be_visible()
def test_admin_prizeuser_filter_error(page: Page, base_url: str):
# Đăng nhập
page.goto(f"{base_url}/admin")
page.fill("#email", "admin")
page.fill("#pass", "123456")
page.locator('input[type="submit"][value="Login"]').click()
page.goto(f"{base_url}/admin/prizeuser")
# Cột hiển thị
expect(page.locator("th:has-text('EMAIL')")).to_be_visible()
expect(page.locator("th:has-text('NĂM SINH')")).to_be_visible()
# TC_B12: Lọc và Xuất Excel
with page.expect_download() as download_info:
page.locator('a#excel_export').click()
download = download_info.value
# Xác nhận file tải về
assert download.suggested_filename.endswith(".xlsx")
def test_backend_v3_comprehensive(page: Page, base_url: str):
import time
dynamic_email = f"admin_test_{int(time.time())}@example.com"
# Đăng nhập Admin
page.goto(f"{base_url}/admin")
page.fill("#email", "admin")
page.fill("#pass", "123456")
page.locator('input[type="submit"][value="Login"]').click()
# TC_B13: Quản lý Người chơi - Thêm mới User
page.goto(f"{base_url}/admin/users/update?id=0")
page.locator("input[name='name']").fill("Admin Test User")
page.locator("input[name='email']").fill(dynamic_email)
page.locator("input[name='password']").fill("password123")
page.locator("select[name='status']").select_option("1")
page.locator('button[type="submit"]:has-text("Thêm mới")').click()
# Laravel redirects back with success or to the new user's edit page
expect(page.locator("text=Cập nhật người dùng").first).to_be_visible() # Redirects to edit page of the new user
# Wait, the add user redirects to /admin/users/update?id=X.
# The actual success message or page content will tell us.
expect(page.locator("input[name='email']")).to_have_value(dynamic_email)
# TC_B14: Cập nhật User
page.locator("select[name='status']").select_option("0")
page.locator('button[type="submit"]:has-text("Cập nhật")').click()
expect(page.locator("text=Cập nhật thành công")).to_be_visible()
# TC_B15: Cấu hình Hệ thống - Đổi Logo/Banner
page.goto(f"{base_url}/admin/parameter")
page.locator("input[name='banner_image']").set_input_files("tests/e2e/dummy.txt")
# Actually the backend does not validate image extension for parameters!
page.locator('button[type="submit"]:has-text("Cập nhật")').first.click()
expect(page.locator("text=Cập nhật thành công")).to_be_visible()
# TC_B16: Reset Lượt Quay (AJAX update_spined)
page.goto(f"{base_url}/admin/prizeuser")
# The button to reset spin is likely missing or using a specific class.
# The function update_spined(user_id) exists in JS. We can call it directly or find the button.
# We will invoke the JS directly to test the API endpoint
page.evaluate("update_spined(1)")
# Since it triggers an alert and reloads, we need to handle the dialog
page.once("dialog", lambda dialog: dialog.accept())
# After accepting, page reloads.
page.wait_for_load_state('networkidle')
# TC_B17: Đăng xuất Admin và Kiểm tra Middleware
page.goto(f"{base_url}/admin/logout")
# We are redirected to /wp-admin (based on BackendController)
# Now try to access admin account page
page.goto(f"{base_url}/admin/account")
# Middleware should block and redirect to /admin login
expect(page).to_have_url(f"{base_url}/admin")