def test_import_ctf(): """Test that CTFd can import a CTF""" app = create_ctfd() if not app.config.get("SQLALCHEMY_DATABASE_URI").startswith("sqlite"): with app.app_context(): base_user = "******" for x in range(10): user = base_user + str(x) user_email = user + "@ctfd.io" gen_user(app.db, name=user, email=user_email) base_team = "team" for x in range(5): team = base_team + str(x) team_email = team + "@ctfd.io" gen_team(app.db, name=team, email=team_email) for x in range(9): chal = gen_challenge(app.db, name="chal_name{}".format(x)) gen_flag(app.db, challenge_id=chal.id, content="flag") chal = gen_challenge(app.db, name="chal_name10", requirements={"prerequisites": [1]}) gen_flag(app.db, challenge_id=chal.id, content="flag") app.db.session.commit() backup = export_ctf() with open("export.test_import_ctf.zip", "wb") as f: f.write(backup.read()) destroy_ctfd(app) app = create_ctfd() # TODO: These databases should work but they don't... if not app.config.get("SQLALCHEMY_DATABASE_URI").startswith("sqlite"): with app.app_context(): import_ctf("export.test_import_ctf.zip") if not app.config.get("SQLALCHEMY_DATABASE_URI").startswith( "postgres"): # TODO: Dig deeper into why Postgres fails here assert Users.query.count() == 31 assert Teams.query.count() == 5 assert Challenges.query.count() == 10 assert Flags.query.count() == 10 chal = Challenges.query.filter_by(name="chal_name10").first() assert chal.requirements == {"prerequisites": [1]} destroy_ctfd(app)
def test_import_ctf(): """Test that CTFd can import a CTF""" app = create_ctfd() if not app.config.get('SQLALCHEMY_DATABASE_URI').startswith('sqlite'): with app.app_context(): base_user = '******' for x in range(10): user = base_user + str(x) user_email = user + "@ctfd.io" gen_user(app.db, name=user, email=user_email) for x in range(9): chal = gen_challenge(app.db, name='chal_name{}'.format(x)) gen_flag(app.db, challenge_id=chal.id, content='flag') chal = gen_challenge(app.db, name='chal_name10', requirements={"prerequisites": [1]}) gen_flag(app.db, challenge_id=chal.id, content='flag') app.db.session.commit() backup = export_ctf() with open('export.test_import_ctf.zip', 'wb') as f: f.write(backup.read()) destroy_ctfd(app) app = create_ctfd() # TODO: These databases should work but they don't... if not app.config.get('SQLALCHEMY_DATABASE_URI').startswith('sqlite'): with app.app_context(): import_ctf('export.test_import_ctf.zip') if not app.config.get('SQLALCHEMY_DATABASE_URI').startswith( 'postgres'): # TODO: Dig deeper into why Postgres fails here assert Users.query.count() == 11 assert Challenges.query.count() == 10 assert Flags.query.count() == 10 chal = Challenges.query.filter_by(name='chal_name10').first() assert chal.requirements == {"prerequisites": [1]} destroy_ctfd(app)
""" python import.py export.zip """ from CTFd import create_app from CTFd.utils.exports import import_ctf import sys app = create_app() with app.app_context(): print( "This file will be deleted in CTFd v4.0. Switch to using `python manage.py import_ctf`" ) import_ctf(sys.argv[1])