mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-17 05:54:19 +01:00
Add code for playwright based testing
This commit is contained in:
0
tests/browser/__init__.py
Normal file
0
tests/browser/__init__.py
Normal file
38
tests/browser/test_headless.py
Normal file
38
tests/browser/test_headless.py
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
|
||||
from flask import url_for
|
||||
from freezegun import freeze_time
|
||||
|
||||
from CTFd.cache import clear_pages
|
||||
from CTFd.utils import set_config
|
||||
from CTFd.utils.config.pages import get_pages
|
||||
from CTFd.utils.encoding import hexencode
|
||||
from tests.helpers import (
|
||||
create_ctfd,
|
||||
destroy_ctfd,
|
||||
gen_challenge,
|
||||
gen_file,
|
||||
gen_page,
|
||||
login_as_user,
|
||||
register_user,
|
||||
LiveServer,
|
||||
)
|
||||
from playwright import sync_playwright
|
||||
import time
|
||||
|
||||
|
||||
def test_browser_index():
|
||||
"""Does the index page return a 200 by default"""
|
||||
app = create_ctfd(server_name="localhost:8943")
|
||||
with LiveServer(app):
|
||||
with sync_playwright() as p:
|
||||
for browser_type in [p.chromium, p.firefox]:
|
||||
browser = browser_type.launch()
|
||||
page = browser.newPage()
|
||||
page.goto('http://localhost:8943/')
|
||||
page.screenshot(path=f'example-{browser_type.name}.png')
|
||||
browser.close()
|
||||
destroy_ctfd(app)
|
||||
@@ -5,12 +5,14 @@ import string
|
||||
import uuid
|
||||
from collections import namedtuple
|
||||
from unittest.mock import Mock, patch
|
||||
import threading
|
||||
|
||||
import requests
|
||||
from flask.testing import FlaskClient
|
||||
from sqlalchemy.engine.url import make_url
|
||||
from sqlalchemy_utils import drop_database
|
||||
from werkzeug.datastructures import Headers
|
||||
from wsgiref.simple_server import make_server, WSGIRequestHandler
|
||||
|
||||
from CTFd import create_app
|
||||
from CTFd.cache import cache, clear_standings
|
||||
@@ -70,6 +72,7 @@ def create_ctfd(
|
||||
setup=True,
|
||||
enable_plugins=False,
|
||||
application_root="/",
|
||||
server_name="localhost",
|
||||
config=TestingConfig,
|
||||
):
|
||||
if enable_plugins:
|
||||
@@ -77,6 +80,8 @@ def create_ctfd(
|
||||
else:
|
||||
config.SAFE_MODE = True
|
||||
|
||||
config.SERVER_NAME = server_name
|
||||
|
||||
config.APPLICATION_ROOT = application_root
|
||||
url = make_url(config.SQLALCHEMY_DATABASE_URI)
|
||||
if url.database:
|
||||
@@ -132,6 +137,26 @@ def destroy_ctfd(app):
|
||||
drop_database(app.config["SQLALCHEMY_DATABASE_URI"])
|
||||
|
||||
|
||||
class LiveServer(object):
|
||||
def __init__(self, app, host="localhost", port=8943):
|
||||
self.app = app
|
||||
self.server = make_server(host, port, app, handler_class=WSGIRequestHandler)
|
||||
self.thread = threading.Thread(target=self.server.serve_forever)
|
||||
self.thread.start()
|
||||
|
||||
def __enter__(self):
|
||||
return self.server
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
if self.thread is not None:
|
||||
self.server.shutdown()
|
||||
self.server.server_close()
|
||||
self.thread.join()
|
||||
del self.server
|
||||
|
||||
|
||||
|
||||
|
||||
def register_user(
|
||||
app, name="user", email="user@ctfd.io", password="password", raise_for_error=True
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user