#!/usr/bin/env python # -*- coding: utf-8 -*- from unittest.mock import patch from freezegun import freeze_time from CTFd.models import Users, db from CTFd.utils import get_config, set_config from CTFd.utils.crypto import verify_password from tests.helpers import create_ctfd, destroy_ctfd, login_as_user, register_user def test_register_user(): """Can a user be registered""" app = create_ctfd() with app.app_context(): register_user(app) user_count = Users.query.count() assert user_count == 2 # There's the admin user and the created user destroy_ctfd(app) def test_register_unicode_user(): """Can a user with a unicode name be registered""" app = create_ctfd() with app.app_context(): register_user(app, name="你好") user_count = Users.query.count() assert user_count == 2 # There's the admin user and the created user destroy_ctfd(app) def test_register_duplicate_username(): """A user shouldn't be able to use an already registered team name""" app = create_ctfd() with app.app_context(): register_user( app, name="user1", email="user1@examplectf.com", password="password", raise_for_error=False, ) register_user( app, name="user1", email="user2@examplectf.com", password="password", raise_for_error=False, ) register_user( app, name="admin ", email="admin2@examplectf.com", password="password", raise_for_error=False, ) user_count = Users.query.count() assert user_count == 2 # There's the admin user and the first created user destroy_ctfd(app) def test_register_duplicate_email(): """A user shouldn't be able to use an already registered email address""" app = create_ctfd() with app.app_context(): register_user( app, name="user1", email="user1@examplectf.com", password="password", raise_for_error=False, ) register_user( app, name="user2", email="user1@examplectf.com", password="password", raise_for_error=False, ) user_count = Users.query.count() assert user_count == 2 # There's the admin user and the first created user destroy_ctfd(app) def test_register_whitelisted_email(): """A user shouldn't be able to register with an email that isn't on the whitelist""" app = create_ctfd() with app.app_context(): set_config( "domain_whitelist", "whitelisted.com, whitelisted.org, whitelisted.net" ) register_user( app, name="not_whitelisted", email="user@nope.com", raise_for_error=False ) assert Users.query.count() == 1 register_user(app, name="user1", email="user@whitelisted.com") assert Users.query.count() == 2 register_user(app, name="user2", email="user@whitelisted.org") assert Users.query.count() == 3 register_user(app, name="user3", email="user@whitelisted.net") assert Users.query.count() == 4 destroy_ctfd(app) def test_register_blacklisted_email(): """A user shouldn't be able to register with an email that is on the blacklist""" app = create_ctfd() with app.app_context(): set_config( "domain_blacklist", "blacklisted.com, blacklisted.org, blacklisted.net" ) register_user( app, name="blacklisted", email="user@blacklisted.com", raise_for_error=False ) assert Users.query.count() == 1 register_user(app, name="user1", email="user@yep.com") assert Users.query.count() == 2 register_user(app, name="user2", email="user@yay.org") assert Users.query.count() == 3 register_user(app, name="user3", email="user@yipee.net") assert Users.query.count() == 4 destroy_ctfd(app) def test_user_bad_login(): """A user should not be able to login with an incorrect password""" app = create_ctfd() with app.app_context(): register_user(app) client = login_as_user( app, name="user", password="wrong_password", raise_for_error=False ) with client.session_transaction() as sess: assert sess.get("id") is None r = client.get("/profile") assert r.location.startswith("/login") # We got redirected to login destroy_ctfd(app) def test_user_login(): """Can a registered user can login""" app = create_ctfd() with app.app_context(): register_user(app) client = login_as_user(app) r = client.get("/profile") assert r.location is None # We didn't get redirected to login assert r.status_code == 200 destroy_ctfd(app) def test_user_login_with_email(): """Can a registered user can login with an email address instead of a team name""" app = create_ctfd() with app.app_context(): register_user(app) client = login_as_user(app, name="user@examplectf.com", password="password") r = client.get("/profile") assert r.location is None # We didn't get redirected to login assert r.status_code == 200 destroy_ctfd(app) def test_user_get_logout(): """Can a registered user load /logout""" app = create_ctfd() with app.app_context(): register_user(app) client = login_as_user(app) client.get("/logout", follow_redirects=True) r = client.get("/challenges") assert r.location == "/login?next=%2Fchallenges%3F" assert r.status_code == 302 destroy_ctfd(app) def test_user_isnt_admin(): """A registered user cannot access admin pages""" app = create_ctfd() with app.app_context(): register_user(app) client = login_as_user(app) for page in [ "pages", "users", "teams", "scoreboard", "challenges", "statistics", "config", ]: r = client.get("/admin/{}".format(page)) assert r.location.startswith("/login?next=") assert r.status_code == 302 destroy_ctfd(app) def test_expired_confirmation_links(): """Test that expired confirmation links are reported to the user""" app = create_ctfd() with app.app_context(): set_config("mail_server", "localhost") set_config("mail_port", 25) set_config("mail_useauth", True) set_config("mail_username", "username") set_config("mail_password", "password") set_config("verify_emails", True) register_user(app, email="user@user.com") client = login_as_user(app, name="user", password="password") # user@user.com "2012-01-14 03:21:34" confirm_link = ( "http://localhost/confirm/bb8a8526146e50778b211ae63074595880edbc0b" ) r = client.get(confirm_link) assert ( "Your confirmation link is invalid, please generate a new one" in r.get_data(as_text=True) ) user = Users.query.filter_by(email="user@user.com").first() assert user.verified is not True destroy_ctfd(app) def test_invalid_confirmation_links(): """Test that invalid confirmation links are reported to the user""" app = create_ctfd() with app.app_context(): set_config("mail_server", "localhost") set_config("mail_port", 25) set_config("mail_useauth", True) set_config("mail_username", "username") set_config("mail_password", "password") set_config("verify_emails", True) register_user(app, email="user@user.com") client = login_as_user(app, name="user", password="password") # user@user.com "2012-01-14 03:21:34" confirm_link = "http://localhost/confirm/a8375iyu