def send_challonge_request(challonge_path, current_user): data = {} # needed so the Request object is a "POST" request req = Request(challonge_path, data) # use basic authentication user, api_key = current_user.challonge_username, xor_crypt_string(current_user.api_key, decode=True) auth_handler = HTTPBasicAuthHandler() auth_handler.add_password( realm="Application", uri=req.get_full_url(), user=user, passwd=api_key ) opener = build_opener(auth_handler) try: response = opener.open(req) except URLLibHTTPError as e: if e.code != 422: raise # wrap up application-level errors doc = ElementTree.parse(e).getroot() if doc.tag != "errors": raise errors = [e.text for e in doc] raise ChallongeException(*errors) return response
def setUp(self): password = generate_password_hash('password') challonge_api_key = xor_crypt_string('challonge123', encode=True) test_user = User('testuser', password, '*****@*****.**', 'testuser', challonge_api_key) self.test_user = test_user db.drop_all() db.create_all() db.session.add(self.test_user) db.session.commit() valid_credentials = base64.b64encode(b'testuser:password').decode( 'utf-8') response = self.client.post( LOGIN_URL, headers={'Authorization': 'Basic ' + valid_credentials}) returned = json.loads(response.data) tk_valid_user = returned['token'] tk_invalid = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTV9.LZaaRkg7THSCD-8VDtjX43Wxn5gKktR6m8DJQDH2SpM' self.headers = { 'Content-Type': 'application/json', 'x-access-token': tk_valid_user } self.badheaders = { 'Content-Type': 'application/json', 'x-access-token': tk_invalid }
def get(self): current_user = get_user_from_auth_header(request, api) try: challonge.set_credentials(current_user.challonge_username, xor_crypt_string(current_user.api_key, decode=True)) tournaments = challonge.tournaments.index() return jsonify({'tournaments' : tournaments}) except HTTPError as e: api.abort(401, 'Invalid credentials.')
def setUp(self): password = generate_password_hash('password') test_user = User('testuser', password, '*****@*****.**', 'matchuptesting', challonge_api_key) self.test_user = test_user db.drop_all() db.create_all() db.session.add(self.test_user) db.session.commit() valid_credentials = base64.b64encode(b'testuser:password').decode( 'utf-8') response = self.client.post( LOGIN_URL, headers={'Authorization': 'Basic ' + valid_credentials}) returned = json.loads(response.data) self.tk_valid_user = returned['token'] self.headers = { 'Content-Type': 'application/json', 'x-access-token': self.tk_valid_user } challonge.set_credentials( self.test_user.challonge_username, xor_crypt_string(self.test_user.api_key, decode=True)) challonge.tournaments.reset(bracket_1_id) challonge.tournaments.start(bracket_1_id) event_data = { 'event_name': 'Test Event', 'brackets': [{ 'bracket_id': bracket_1_id, 'number_of_setups': 0 }, { 'bracket_id': bracket_2_id, 'number_of_setups': 0 }] } response = self.client.post(EVENT_URL, json=event_data, headers=self.headers) self.event = Event.query.get(json.loads(response.data)['id']) self.matches_available_bracket_1 = challonge.matches.index( bracket_1_id, state='open') self.match_to_test = self.matches_available_bracket_1[0]
def put(self): user = get_user_from_auth_header(request, api) try: user.username = request.json['username'] user.challonge_username = request.json['challonge_username'] user.email = request.json['email'] user.api_key = xor_crypt_string(request.json['api_key'], encode=True) db.session.commit() return user_schema.jsonify(user) except KeyError as e: message = f'Missing field on user entity: {e.args[0]}' api.abort(400, message) except IntegrityError as e: message = e.args[0].split('\n')[1] api.abort(409, message)
def post(self): try: username = request.json['username'] password = request.json['password'] hashed_password = generate_password_hash(password, method='sha256') challonge_username = request.json['challonge_username'] email = request.json['email'] api_key = xor_crypt_string(request.json['api_key'], encode=True) new_user = User(username, hashed_password, email, challonge_username, api_key) db.session.add(new_user) db.session.commit() return user_schema.jsonify(new_user) except KeyError as e: message = f'Missing field on user entity: {e.args[0]}' api.abort(400, message) except IntegrityError as e: message = e.args[0].split('\n')[1] api.abort(409, message)
def get(self, event_id): current_user = get_user_from_auth_header(request, api) event = Event.query.get(event_id) if not event: api.abort(404, 'Event not found') try: challonge.set_credentials(current_user.challonge_username, xor_crypt_string(current_user.api_key, decode=True)) average_bracket_size = calculate_mean_bracket_size(event.brackets) available_matches = [] bracket_setups = {} matches_in_progress = [] for bracket in event.brackets: list_of_matches = challonge.matches.index(bracket.bracket_id, state='open') matches_not_in_progress = list(filter(lambda match: match['underway_at'] == None, list_of_matches)) number_of_setups_in_use = len(list_of_matches) - len(matches_not_in_progress) bracket_matches_in_progress = list(filter(lambda match: match['underway_at'] is not None, list_of_matches)) get_player_data_for_matches(bracket_matches_in_progress, bracket) matches_in_progress = matches_in_progress + bracket_matches_in_progress bracket.bracket_size_ratio = bracket.number_of_players / average_bracket_size matches_for_bracket = determine_priority_for_matches(matches_not_in_progress, bracket) available_matches = available_matches + matches_for_bracket bracket_setups[bracket.id] = bracket.number_of_setups - number_of_setups_in_use # sort matches by priority (descending) sorted_matches = sorted(available_matches, key=lambda match: match['priority'], reverse=True) matches_called = get_highest_priority_matches(sorted_matches, bracket_setups, matches_in_progress) return jsonify(matches_called) except HTTPError as e: api.abort(401, 'Invalid credentials.')
from app.src.config import basedir from app.src.model.user import User from app.src.model.event import Event from werkzeug.security import generate_password_hash from app.src.controller import xor_crypt_string import json import base64 BASE_URL = 'http://localhost:5000/challonge' BRACKET_URL = BASE_URL + '/brackets' MATCHES_URL = BASE_URL + '/matches' VALIDATION_URL = BASE_URL + '/verify' MATCH_START_URL = BASE_URL + '/match/start' LOGIN_URL = 'http://localhost:5000/auth' EVENT_URL = 'http://localhost:5000/event' challonge_api_key = xor_crypt_string( 'lDV85oOJLqA1ySxegdJQQcVghlA1bgWi3tUyOGNN', encode=True) b1_id = 8176881 b2_id = 8176886 b3_id = 8176890 b4_id = 8177036 b5_id = 8177044 b6_id = 8182048 class BaseTestCase(TestCase): def create_app(self): app.config.from_object('app.src.config.TestingConfig') return app def setUp(self):
def get(self): user = get_user_from_auth_header(request, api) api_key = xor_crypt_string(user.api_key, decode=True) user.api_key = api_key return user_schema.jsonify(user)