diff --git a/tests/browser/__init__.py b/tests/browser/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/browser/test_headless.py b/tests/browser/test_headless.py new file mode 100644 index 00000000..af92373c --- /dev/null +++ b/tests/browser/test_headless.py @@ -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) \ No newline at end of file diff --git a/tests/helpers.py b/tests/helpers.py index 72e61e6e..4eb2a6ea 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -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 ):