forked from CTFd/CTFd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test_index test_register_user test_user_isnt_admin
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-r requirements.txt | ||
coverage>=4.1 | ||
mock>=2.0.0 | ||
nose>=1.3.7 | ||
rednose>=1.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[nosetests] | ||
stop=1 | ||
verbosity=2 | ||
with-coverage=1 | ||
cover-package=CTFd |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="[email protected]", 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="[email protected]", 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from helpers import create_ctfd, register_user, login_as_user | ||
from CTFd.models import Teams | ||
|
||
|
||
def test_index(): | ||
app = create_ctfd() | ||
with app.app_context(): | ||
with app.test_client() as client: | ||
r = client.get('/') | ||
assert r.status_code == 200 | ||
|
||
|
||
def test_register_user(): | ||
app = create_ctfd() | ||
with app.app_context(): | ||
register_user(app) | ||
team_count = app.db.session.query(app.db.func.count(Teams.id)).first()[0] | ||
assert team_count == 2 # There's the admin user and the created user | ||
|
||
|
||
def test_user_isnt_admin(): | ||
app = create_ctfd() | ||
with app.app_context(): | ||
register_user(app) | ||
client = login_as_user(app) | ||
r = client.get('/admin/graphs') | ||
assert r.location == "http://localhost/login" | ||
assert r.status_code == 302 |