mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-17 14:04:20 +01:00
Starting to write tests
test_index test_register_user test_user_isnt_admin
This commit is contained in:
61
tests/helpers.py
Normal file
61
tests/helpers.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from CTFd import create_app
|
||||
from sqlalchemy_utils import database_exists, create_database, drop_database
|
||||
from sqlalchemy.engine.url import make_url
|
||||
|
||||
def create_ctfd(ctf_name="CTFd", name="admin", email="admin@ctfd.io", password="password"):
|
||||
app = create_app()
|
||||
app.config['PRESERVE_CONTEXT_ON_EXCEPTION'] = False
|
||||
app.config['TESTING'] = True
|
||||
app.config['DEBUG'] = True
|
||||
|
||||
url = make_url(app.config['SQLALCHEMY_DATABASE_URI'])
|
||||
if url.drivername == 'postgres':
|
||||
url.drivername = 'postgresql'
|
||||
|
||||
if database_exists(url):
|
||||
drop_database(url)
|
||||
create_database(url)
|
||||
app.db.create_all()
|
||||
|
||||
with app.app_context():
|
||||
with app.test_client() as client:
|
||||
data = {}
|
||||
r = client.get('/setup') # Populate session with nonce
|
||||
with client.session_transaction() as sess:
|
||||
data = {
|
||||
"ctf_name": ctf_name,
|
||||
"name": name,
|
||||
"email": email,
|
||||
"password": password,
|
||||
"nonce": sess.get('nonce')
|
||||
}
|
||||
client.post('/setup', data=data)
|
||||
return app
|
||||
|
||||
|
||||
def register_user(app, name="user", email="user@ctfd.io", password="password"):
|
||||
with app.app_context():
|
||||
with app.test_client() as client:
|
||||
r = client.get('/register')
|
||||
with client.session_transaction() as sess:
|
||||
data = {
|
||||
"name": name,
|
||||
"email": email,
|
||||
"password": password,
|
||||
"nonce": sess.get('nonce')
|
||||
}
|
||||
client.post('/register', data=data)
|
||||
|
||||
|
||||
def login_as_user(app, name="user", password="password"):
|
||||
with app.app_context():
|
||||
with app.test_client() as client:
|
||||
r = client.get('/login')
|
||||
with client.session_transaction() as sess:
|
||||
data = {
|
||||
"name": name,
|
||||
"password": password,
|
||||
"nonce": sess.get('nonce')
|
||||
}
|
||||
client.post('/login', data=data)
|
||||
return client
|
||||
Reference in New Issue
Block a user