def client(app): with app.app_context(): client = app.test_client() db.create_all() # Add Users password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='TestA', last_name='UserOne', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='TestB', last_name='UserTwo', email='*****@*****.**', password=password)) db.session.commit() # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() yield client db.drop_all()
def entry_client(app): """ Fixture with more test data in it """ with app.app_context(): entry_client = app.test_client() db.create_all() # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() # Add some entries db.session.add( Entry(id=1, game_session=1, description='Wand', amount=10.00, character_id=2)) db.session.add( Entry(id=2, game_session=1, description='Sword', amount=20.00, character_id=2)) db.session.add( Entry(id=3, game_session=2, description='Potion', amount=30.00, character_id=2)) db.session.add( Entry(id=4, game_session=1, description='Crossbow', amount=40.00, character_id=3)) db.session.add( Entry(id=5, game_session=2, description='Spear', amount=50.00, character_id=3)) db.session.add( Entry(id=6, game_session=3, description='Backpack', amount=60.00, character_id=3)) db.session.commit() yield entry_client db.drop_all()
def save_setting(setting_name, value): """ Add or update a setting value """ setting_exists = Setting.query.filter_by(key=setting_name).first() if setting_exists: setting_exists.value = value else: new_setting = Setting(user_id=current_user.id, key=setting_name, value=value) db.session.add(new_setting) db.session.commit() # Save the value into session session[setting_name] = value
def test_get_current_character_id(client): with client: # arrange client.post('/login', data=dict(email='*****@*****.**', password='******', remember_me=False)) db.session.add(Setting(key='current_character', value='2')) db.session.commit() # act rv = rv = client.get('/entry', follow_redirects=True) # assert assert b'Rogue' in rv.data
def client(app): """ Fixture with basic data in it """ with app.app_context(): client = app.test_client() db.create_all() # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() yield client db.drop_all()
def test_check_setting_saved(client): """ Check that the setting is saved to the table when it's changed """ with client: # arrange client.post('/login', data=dict(email='*****@*****.**', password='******', remember_me=False)) db.session.add(Setting(key='current_character', value='2')) db.session.commit() # act character_id = 3 client.post('/current_character', data=dict(selected_character=character_id), follow_redirects=True) # assert current_character = Setting.query.filter_by( key='current_character').first() assert current_character.value == '3'
def get_current_character_id(): """ Check the database for a previously set active character if one isn't set then set the 'first one in the database. """ # A filtered query # current = db.session.query(Setting).filter(Setting.key == 'current_character').all() # or another, shorter way # current = db.session.query(Setting).filter_by(key='current_character').all() # Scalar value, single value if it exists or None # current = db.session.scalar(Setting).filter_by(key='current_character') # current = Setting.scalar(Setting).filter_by(key='current_character') # see if their is a character id in session if 'current_character' in session: return session['current_character'] # See if there is a saved character id on the database, if so set it to current current_id = Setting.query.filter_by(key='current_character').first() char = Character() if current_id is None: # no current_id has been saved try and get the first Character on the Character table and use that. char = Character.query.first() if char is None: return None else: # Ok we have a character on the database, pull it up char = Character.query.filter_by(id=current_id.value).first() # now we can save the right data db.session.add(Setting(key='current_character', value=str(char.id))) db.session.commit # set the character we found up in session session['current_character'] = char.id return char.id
def test_handle_character_no_entries(empty_client): """ confirm the application can start up correctly without any characters or entries in the db """ with empty_client: # arrange empty_client.post('/login', data=dict(email='*****@*****.**', password='******', remember_me=False)) # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() # act result = empty_client.get('/', follow_redirects=True) # assert assert b'Entries' in result.data
def __init_test_db(): ''' Add mock db data ''' db.create_all() # Add Users password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='TestA', last_name='UserOne', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='TestB', last_name='UserTwo', email='*****@*****.**', password=password)) db.session.commit() # Add sessions db.session.add(Setting(id=1, user_id=1, key='test_session', value='42')) db.session.add(Setting(id=2, user_id=2, key='test_session', value='99')) # Add some Characters db.session.add(Character(id=1, user_id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, user_id=1, name='Rogue', is_dead=False)) db.session.add(Character(id=3, user_id=2, name='Fighter', is_dead=False)) db.session.commit() # Add some entries db.session.add( Entry(id=1, game_session=1, description='Wand', amount=10.00, character_id=1)) db.session.add( Entry(id=2, game_session=1, description='Sword', amount=20.00, character_id=1)) db.session.add( Entry(id=3, game_session=2, description='Potion', amount=30.00, character_id=1)) db.session.add( Entry(id=4, game_session=1, description='Crossbow', amount=40.00, character_id=2)) db.session.add( Entry(id=5, game_session=1, description='Spear', amount=50.00, character_id=2)) db.session.add( Entry(id=6, game_session=2, description='Backpack', amount=60.00, character_id=2)) db.session.commit()
def entry_client(app): with app.app_context(): entry_client = app.test_client() db.create_all() # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() # Add Users password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='TestA', last_name='UserOne', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='TestB', last_name='UserTwo', email='*****@*****.**', password=password)) db.session.commit() # Add some entries db.session.add( Entry(id=1, game_session=1, description='Wand', amount=10.00, character_id=2)) db.session.add( Entry(id=2, game_session=1, description='Sword', amount=20.00, character_id=2)) db.session.add( Entry(id=3, game_session=2, description='Potion', amount=30.00, character_id=2)) db.session.add( Entry(id=4, game_session=1, description='Crossbow', amount=40.00, character_id=3)) db.session.add( Entry(id=5, game_session=1, description='Spear', amount=50.00, character_id=3)) db.session.add( Entry(id=6, game_session=2, description='Backpack', amount=60.00, character_id=3)) db.session.commit() yield entry_client db.drop_all()
def run(*script_args): print('Loading application settings...') settings_list = [] if not Setting.objects.filter(name='Core Run Times').exists(): print('"Core Run Times" not configured. Inserting...') settings_list.append(Setting(name='Core Run Times', value1=RUN_TIMES)) if not Setting.objects.filter(name='Core Weekly Tasks Days').exists(): print('"Core Weekly Tasks Days" not configured. Inserting...') settings_list.append(Setting(name='Core Weekly Tasks Days', value1=WEEKLY_TASKS_DAYS)) if not Setting.objects.filter(name='Days to Display').exists(): print('"Days to Display" not configured. Inserting...') settings_list.append(Setting(name='Days to Display', value1=DAYS_TO_DISPLAY)) if not Setting.objects.filter(name='VirusTotal User').exists(): print('"VirusTotal User" not configured. Inserting...') settings_list.append(Setting(name='VirusTotal User', value1=VT_USER)) if not Setting.objects.filter(name='VirusTotal API Key').exists(): print('"VirusTotal API Key" not configured. Inserting...') settings_list.append(Setting(name='VirusTotal API Key', value1=VT_API_KEY)) if not Setting.objects.filter(name='VirusTotal Requests Per Minute').exists(): print('"VirusTotal Requests Per Minute" not configured. Inserting...') settings_list.append(Setting(name='VirusTotal Requests Per Minute', value1=VT_REQ_MIN)) if not Setting.objects.filter(name='BinaryEdge API Key').exists(): print('"BinaryEdge API Key" not configured. Inserting...') settings_list.append(Setting(name='BinaryEdge API Key', value1=BE_API_KEY)) if not Setting.objects.filter(name='Shodan API Key').exists(): print('"Shodan API Key" not configured. Inserting...') settings_list.append(Setting(name='Shodan API Key', value1=SHODAN_API_KEY)) if not Setting.objects.filter(name='Twitter Consumer Key').exists(): print('"Twitter Consumer Key" not configured. Inserting...') settings_list.append(Setting(name='Twitter Consumer Key', value1=TWITTER_CONSUMER_KEY)) if not Setting.objects.filter(name='Twitter Consumer Secret').exists(): print('"Twitter Consumer Secret" not configured. Inserting...') settings_list.append(Setting(name='Twitter Consumer Secret', value1=TWITTER_CONSUMER_SECRET)) if not Setting.objects.filter(name='Twitter Access Token').exists(): print('"Twitter Access Token" not configured. Inserting...') settings_list.append(Setting(name='Twitter Access Token', value1=TWITTER_ACCESS_TOKEN)) if not Setting.objects.filter(name='Twitter Access Token Secret').exists(): print('"Twitter Access Token Secret" not configured. Inserting...') settings_list.append(Setting(name='Twitter Access Token Secret', value1=TWITTER_ACCESS_TOKEN_SECRET)) if not Setting.objects.filter(name='Twitter Username List').exists(): print('"Twitter Username List" not configured. Inserting...') settings_list.append(Setting(name='Twitter Username List', value1=TWITTER_USERNAME_LIST)) if not Setting.objects.filter(name='Twitter Search List').exists(): print('"Twitter Search List" not configured. Inserting...') settings_list.append(Setting(name='Twitter Search List', value1=TWITTER_SEARCH_LIST)) if not Setting.objects.filter(name='MaxMind City Database Path').exists(): print('"MaxMind City Database Path" not configured. Inserting...') settings_list.append(Setting(name='MaxMind City Database Path', value1=MAXMIND_CITY_DB_PATH)) if not Setting.objects.filter(name='MaxMind ASN Database Path').exists(): print('"MaxMind ASN Database Path" not configured. Inserting...') settings_list.append(Setting(name='MaxMind ASN Database Path', value1=MAXMIND_ASN_DB_PATH)) if not Setting.objects.filter(name='Home Country').exists(): print('"Home Country" not configured. Inserting...') settings_list.append(Setting(name='Home Country', value1=HOME_COUNTRY_NAME, value2=HOME_COUNTRY_CODE)) if len(settings_list) > 0: print('Saving new values...') Setting.objects.bulk_create(settings_list) else: print('There are no new items to save.')