#!/usr/bin/env python # -*- coding: utf-8 -*- from CTFd.models import Users from CTFd.utils import set_config, get_config from CTFd.utils.security.signing import serialize from freezegun import freeze_time from tests.helpers import * from mock import patch 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@ctfd.io", password="password") register_user(app, name="user1", email="user2@ctfd.io", password="password") 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@ctfd.io", password="password") register_user(app, name="user2", email="user1@ctfd.io", password="password") 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') 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_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") with client.session_transaction() as sess: assert sess.get('id') is None r = client.get('/profile') assert r.location.startswith("http://localhost/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 != "http://localhost/login" # 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@ctfd.io", password="password") r = client.get('/profile') assert r.location != "http://localhost/login" # 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 == "http://localhost/login?next=%2Fchallenges" 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("http://localhost/login?next=") assert r.status_code == 302 destroy_ctfd(app) @freeze_time("2019-02-24 03:21:34") def test_expired_confirmation_links(): """Test that expired confirmation links are reported to the user""" app = create_ctfd() with app.app_context(): 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/InVzZXJAdXNlci5jb20i.TxD0vg.cAGwAy8cK1T0saEEbrDEBVF2plI' r = client.get(confirm_link) assert "Your confirmation link has expired" 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('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