def seed_db(): """ Flask command to seed the database with some user. Run this command when you have a new database so that you can seed some data to prevent READ ALL APIs to fail. """ db.session.add( User(username="******", email="*****@*****.**")) db.session.add( User(username="******", email="*****@*****.**")) db.session.commit()
def test_mark_in_progress_user_does_not_own_event(self): password = generate_password_hash('password') extra_test_user = User('extratestuser', password, '*****@*****.**', 'matchup', challonge_api_key) db.session.add(extra_test_user) db.session.commit() valid_credentials = base64.b64encode(b'extratestuser:password').decode( 'utf-8') login_response = self.client.post( LOGIN_URL, headers={'Authorization': 'Basic ' + valid_credentials}) returned = json.loads(login_response.data) match_data = { 'event_id': self.event.id, 'bracket_id': self.event.brackets[0].id, 'match_id': self.match_to_test['id'] } response = self.client.put(MATCH_URL, json=match_data, headers={ 'Content-Type': 'application/json', 'x-access-token': returned['token'] }) self.assert401(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 create_new_user(data): """ Service function to add a user to the database. This should be called by the POST /users route. Parameters: data (dict): user data from request object Returns: int: public_id of the new user created Raises: ValueError """ username = data.get("username") email = data.get("email") if utils.user_not_exists(email): new_user = User(email=email, username=username) db.session.add(new_user) db.session.commit() return new_user.public_id raise exc.UserExistsError(email, "User with email {} already exists")
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()
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 add_user(data): """ Utility function to add a user to the database. Use this when you need to create a user but not while in `test_new_user` scope. Parameters: data (dict): user data from request object Returns: int: public_id of the new user created """ user = User(username=data.get("username"), email=data.get("email")) db.session.add(user) current_app.logger.debug(f"Added {user.to_json()} to db session") db.session.commit() current_app.logger.debug(f"Session comitted") return user.public_id
def setUp(self): db.drop_all() db.create_all() password = generate_password_hash('password') test_user = User('testuser', password, '*****@*****.**', 'matchuptesting', challonge_api_key) self.test_user = test_user 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 } event_data = { 'event_name': 'Test Event', 'brackets': [{ 'bracket_id': bracket_1_id, 'number_of_setups': 4 }, { 'bracket_id': bracket_2_id, 'number_of_setups': 5 }] } response = self.client.post(CREATE_EVENT_URL, json=event_data, headers=self.headers) self.test_event = Event.query.get(json.loads(response.data)['id'])