Add code for playwright based testing

This commit is contained in:
Kevin Chung
2020-08-13 16:14:52 -04:00
parent 92a40b6eff
commit cb7f559d5b
3 changed files with 63 additions and 0 deletions

View File

View 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)

View File

@@ -5,12 +5,14 @@ import string
import uuid import uuid
from collections import namedtuple from collections import namedtuple
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
import threading
import requests import requests
from flask.testing import FlaskClient from flask.testing import FlaskClient
from sqlalchemy.engine.url import make_url from sqlalchemy.engine.url import make_url
from sqlalchemy_utils import drop_database from sqlalchemy_utils import drop_database
from werkzeug.datastructures import Headers from werkzeug.datastructures import Headers
from wsgiref.simple_server import make_server, WSGIRequestHandler
from CTFd import create_app from CTFd import create_app
from CTFd.cache import cache, clear_standings from CTFd.cache import cache, clear_standings
@@ -70,6 +72,7 @@ def create_ctfd(
setup=True, setup=True,
enable_plugins=False, enable_plugins=False,
application_root="/", application_root="/",
server_name="localhost",
config=TestingConfig, config=TestingConfig,
): ):
if enable_plugins: if enable_plugins:
@@ -77,6 +80,8 @@ def create_ctfd(
else: else:
config.SAFE_MODE = True config.SAFE_MODE = True
config.SERVER_NAME = server_name
config.APPLICATION_ROOT = application_root config.APPLICATION_ROOT = application_root
url = make_url(config.SQLALCHEMY_DATABASE_URI) url = make_url(config.SQLALCHEMY_DATABASE_URI)
if url.database: if url.database:
@@ -132,6 +137,26 @@ def destroy_ctfd(app):
drop_database(app.config["SQLALCHEMY_DATABASE_URI"]) 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( def register_user(
app, name="user", email="user@ctfd.io", password="password", raise_for_error=True app, name="user", email="user@ctfd.io", password="password", raise_for_error=True
): ):