Block new user registration if registering via MLC (#840)

* Block new user registration if registering via MLC
* Allow login with MLC while registration is disabled
This commit is contained in:
Kevin Chung
2019-01-19 16:00:29 -05:00
committed by GitHub
parent f8607c3d5c
commit 3af036b4b2
4 changed files with 182 additions and 8 deletions

View File

@@ -7,9 +7,11 @@ from CTFd.cache import cache
from sqlalchemy_utils import database_exists, create_database, drop_database
from sqlalchemy.engine.url import make_url
from collections import namedtuple
from mock import Mock, patch
import datetime
import six
import gc
import requests
if six.PY2:
text_type = unicode
@@ -130,6 +132,59 @@ def login_as_user(app, name="user", password="password", raise_for_error=True):
return client
def login_with_mlc(app, name='user', scope='profile%20team', email='user@ctfd.io', oauth_id=1337, team_name='TestTeam', team_oauth_id=1234, raise_for_error=True):
with app.test_client() as client, \
patch.object(requests, 'get') as fake_get_request, \
patch.object(requests, 'post') as fake_post_request:
client.get('/login')
with client.session_transaction() as sess:
nonce = sess['nonce']
redirect_url = "{endpoint}?response_type=code&client_id={client_id}&scope={scope}&state={state}".format(
endpoint=app.config['OAUTH_AUTHORIZATION_ENDPOINT'],
client_id=app.config['OAUTH_CLIENT_ID'],
scope=scope,
state=nonce
)
r = client.get('/oauth', follow_redirects=False)
assert r.location == redirect_url
fake_post_response = Mock()
fake_post_request.return_value = fake_post_response
fake_post_response.status_code = 200
fake_post_response.json = lambda: {
'access_token': 'fake_mlc_access_token'
}
fake_get_response = Mock()
fake_get_request.return_value = fake_get_response
fake_get_response.status_code = 200
fake_get_response.json = lambda: {
'id': oauth_id,
'name': name,
'email': email,
'team': {
'id': team_oauth_id,
'name': team_name
}
}
client.get('/redirect?code={code}&state={state}'.format(
code='mlc_test_code',
state=nonce
), follow_redirects=False)
if raise_for_error:
with client.session_transaction() as sess:
assert sess['id']
assert sess['name']
assert sess['type']
assert sess['email']
assert sess['nonce']
return client
def get_scores(user):
r = user.get('/api/v1/scoreboard')
scores = r.get_json()