class UtilAuthTD(unittest.TestCase): def setUp(self): self.app = Flask("dummy") self.app.secret_key = 'poop' LoginManager().init_app(self.app) self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) self.app.tables = self.tables self.user_with_roles = self.tables.User() self.role = self.tables.Role() self.role.name = "new_role" self.user_with_roles.roles = [self.role] self.id = Identity('dummy') def test_on_identity_loaded_callback(self): with self.app.test_client() as c: with self.app.test_request_context('/') as req_c: login_user(self.user_with_roles) self.assertIsNone(getattr(self.id, 'user', None)) on_identity_loaded_callback = auth.generate_identity_loaded( self.app) self.assertEqual(len(self.id.provides), 0) on_identity_loaded_callback(None, self.id) self.assertEqual(len(self.id.provides), 2) def test_generate_user_loader(self): self.app.tables = MagicMock() self.app.tables.User.query.get.return_value = self.user_with_roles self.app.td_config = {'PLAYER_LOGIN': 0} with self.app.test_client() as c: with self.app.test_request_context('/') as req_c: user_loader = auth.generate_user_loader(self.app) returned_user = user_loader(1) self.assertEquals(returned_user, self.user_with_roles) self.app.td_config = {'PLAYER_LOGIN': 1} with self.app.test_client() as c: with self.app.test_request_context('/') as req_c: user_loader = auth.generate_user_loader(self.app) returned_user = user_loader(1) self.assertEquals(returned_user, self.user_with_roles)
class ModelUserTD(unittest.TestCase): def setUp(self): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) self.user_with_roles = self.tables.User( username='******') self.role = self.tables.Role() self.role.name = "new_role" self.role.role_id = 1 self.user_with_roles.roles = [self.role] self.user = self.tables.User(username='******') self.user_with_roles.user_id = 1 self.user.user_id = 2 def test_crypt_password(self): password = self.user.crypt_password('password_test') self.assertTrue(password != "password_test") self.user.password = self.user.crypt_password('password_test') self.assertTrue(self.user.verify_password('password_test')) self.assertFalse(self.user.verify_password('password_not_test')) def test_user_auth(self): self.assertTrue(self.user.is_authenticated()) self.assertTrue(self.user.is_active()) self.assertFalse(self.user.is_anonymous()) def test_to_dict_simple(self): self.user.password = self.user.crypt_password('password_two') simple_dict_with_roles = self.user_with_roles.to_dict_simple() for value in self.user.__table__.columns: key = str(value)[str(value).index('.') + 1:] if key != 'password_crypt': self.assertTrue(key in simple_dict_with_roles, "oops - did not find %s" % key) self.assertTrue('password_crypt' not in simple_dict_with_roles) self.assertTrue('roles' in simple_dict_with_roles) self.assertEquals(len(simple_dict_with_roles['roles']), 1) simple_dict_no_roles = self.user.to_dict_simple() self.assertTrue('roles' not in simple_dict_no_roles)
class TdUnitTestBase(unittest.TestCase): def __init__(self,*args, **kwargs): super(TdUnitTestBase, self).__init__(*args, **kwargs) self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) def create_division_final_players(self,num_final_players=2): players=[] division_final_players = [] for num in range(1,num_final_players+1): players.append(self.create_player(player_id=num)) division_final_players.append(self.tables.DivisionFinalPlayer( final_player_id=num, division_final_id=1, player_id=num, adjusted_seed=num, initial_seed=num, overall_rank=num )) division_final_players[num-1].player=players[num-1] return division_final_players def create_division_final(self,use_division_final_players=True): tournament = self.create_single_division_tournament() division_final = self.tables.DivisionFinal( division_final_id=1, division_id=1, ) division_final.division=tournament.divisions[0] if use_division_final_players: division_final.qualifiers=self.create_division_final_players() return division_final def create_entry(self): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) self.entry = self.tables.Entry( entry_id=1, player_id=1, division_id=1 ) self.score = self.tables.Score( score_id=1, score=12345, entry_id=1, division_machine_id=1 ) if hasattr(self,'division_machine'): self.score.division_machine=self.division_machine self.entry.scores=[self.score] def create_queue(self): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) self.child_queue = self.tables.Queue( queue_id=1, bumped=False, division_machine_id=1, parent_id=1, queue_child=[], division_machine=self.division_machine ) self.queue = self.tables.Queue( queue_id=2, bumped=False, division_machine_id=1, queue_child=[self.child_queue], division_machine=self.division_machine ) if hasattr(self,'division_machine'): self.division_machine.queue = self.queue def create_division_machine(self): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) self.machine = self.tables.Machine( machine_name='test_machine', abbreviation='AAA' ) self.division_machine = self.tables.DivisionMachine( division_machine_id=1, removed=False, machine_id=self.machine.machine_id, division_id=self.division.division_id, player_id=1, team_id=1 ) self.division_machine.machine = self.machine def create_player(self,player_id=1): player = self.tables.Player( player_id=player_id, first_name="test", last_name="player %s" % player_id ) if hasattr(self,"division_machine"): player.division_machine=self.division_machine self.division_machine.player = player if hasattr(self,"role"): player.roles=[self.role] if hasattr(self,"division"): player.linked_division_id=self.division.division_id player.linked_division=self.division return player def create_role(self,role_name=None): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) role = self.tables.Role() role.name = role_name role.role_id = 1 return role def create_team(self, team_name=None): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) if team_name is None: team_name='test_test' self.team = self.tables.Team( team_name=team_name, players=[self.player], team_id=1 ) if hasattr(self,"division_machine"): self.division_machine.team = self.team def create_audit_log(self): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) self.create_division_machine() self.audit_log = self.tables.AuditLog( audit_log_id=1, player_id=1, entry_id=1, token_id=1, division_machine_id=1, deskworker_id=1, scorekeeper_id=1, purchase_date=1, game_started_date=None, used_date=None, voided_date=None, voided=False, used=False, remaining_tokens="", description="", action="" ) self.audit_log.division_machine=self.division_machine self.audit_log.token=self.token def create_token(self): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) self.token = self.tables.Token( token_id=1, player_id=1, division_id=1, team_id=1, metadivision_id=1, paid_for=False, used=False ) def create_multiple_single_div_tournaments(self,tournament_name,num): tournaments=[] for i in range(num): tournament = self.tables.Tournament( tournament_id=1, tournament_name='%s%s' % (tournament_name,num), single_division=True ) division = self.tables.Division( division_id=i, division_name='all %s' % num, number_of_scores_per_entry=1, tournament_id=1, tournament=tournament, active=True ) tournament.divisions=[division] tournaments.append(tournament) return tournaments def create_single_division_tournament(self, tournament_name=None): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) if tournament_name is None: tournament_name='test_tournament' self.tournament = self.tables.Tournament( tournament_id=1, tournament_name=tournament_name, single_division=True ) self.division = self.tables.Division( division_id=1, division_name='all', number_of_scores_per_entry=1, tournament_id=1, tournament=self.tournament, active=True ) self.tournament.divisions=[self.division] return self.tournament def create_multi_division_tournament(self, tournament_name=None): self.db_handle = db_util.create_db_handle_no_app() self.tables = ImportedTables(self.db_handle) if tournament_name is None: tournament_name='test_tournament' self.multi_div_tournament = self.tables.Tournament( tournament_id=1, tournament_name=tournament_name, single_division=False ) self.multi_div_tournament_division = self.tables.Division( division_id=1, division_name='all', number_of_scores_per_entry=1, tournament_id=1, tournament=self.multi_div_tournament, active=True ) self.multi_div_tournament.divisions=[self.multi_div_tournament_division]