def using(name, all): credentials: UserCredentials = UserCredentials.instance() facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() username = credentials['username'] context = { 'name': name, 'count': 1, 'description': facade.CONFIG.REWARDS[name]['description'], } try: if all: name_rewards_map = facade.user_get_rewards_by_name(username) reward_count = len(name_rewards_map[name]) for i in range(reward_count): facade.user_use_reward(username, name) context.update({'count': reward_count}) else: facade.user_use_reward(username, name) templater.echo_template('reward_used.jinja2', context) except LookupError: templater.echo_template('permission_use_error.jinja2', context) except Exception as e: click.echo(e)
def test_user_get_gold(self): self.setup_sample() self.create_sample_user() facade: Rewardify = Rewardify.instance() gold = facade.user_get_gold(self.SAMPLE_USER_PARAMETERS['name']) self.assertEqual(gold, 0)
def test_recycle_reward(self): reward_parameters = [ { 'name': 'Gold Reward', 'description': 'for testing', 'rarity': 'common', 'effect': 'gold(100)', 'cost': 100, 'recycle': 100 }, ] with MockConfigContext( self, rewards=reward_parameters ) as mock_context, StandardUserContext() as user_context: facade: Rewardify = Rewardify.instance() facade.user_add_dust(user_context.username, 100) facade.user_buy_reward(user_context.username, 'Gold Reward') self.RUNNER.invoke(login, [user_context.username, user_context.password]) result = self.RUNNER.invoke(rewards, ['recycle', 'Gold Reward']) user_context.update() self.assertEqual(result.exit_code, 0) self.assertIn('REWARD RECYCLED', result.output) self.assertEqual(user_context.user.dust, 100)
def test_listing_available_packs(self): pack_parameters = [{ 'name': 'Buy Me Pack', 'cost': 100, 'description': 'For testing', 'slot1': [1, 0, 0, 0], 'slot2': [1, 0, 0, 0], 'slot3': [0, 1, 0, 0], 'slot4': [0, 0, 1, 0], 'slot5': [0, 0, 0, 1], }, { 'name': 'Other Pack', 'cost': 1000, 'description': 'For testing', 'slot1': [1, 0, 0, 0], 'slot2': [1, 0, 0, 0], 'slot3': [0, 1, 0, 0], 'slot4': [0, 0, 1, 0], 'slot5': [0, 0, 0, 1], }] with MockConfigContext( self, packs=pack_parameters ) as mock_context, StandardUserContext() as user_context: facade: Rewardify = Rewardify.instance() result = self.RUNNER.invoke(packs, ['list']) self.assertEqual(result.exit_code, 0) print(result.output) self.assertTrue('AVAILABLE PACKS' in result.output) self.assertTrue('Buy Me Pack' in result.output) self.assertTrue('Other Pack' in result.output)
def test_buying_without_gold_not_working(self): pack_parameters = [{ 'name': 'Buy Me Pack', 'cost': 100, 'description': 'For testing', 'slot1': [1, 0, 0, 0], 'slot2': [1, 0, 0, 0], 'slot3': [0, 1, 0, 0], 'slot4': [0, 0, 1, 0], 'slot5': [0, 0, 0, 1], }] with MockConfigContext( self, packs=pack_parameters ) as mock_context, StandardUserContext() as user_context: facade: Rewardify = Rewardify.instance() # If the user does not have gold, the buying process should not work, instead the permission denied # template is to be used. self.assertEqual(user_context.user.gold, 0) self.RUNNER.invoke(login, [user_context.username, user_context.password]) result = self.RUNNER.invoke(packs, ['buy', 'Buy Me Pack']) self.assertEqual(result.exit_code, 1) self.assertTrue('YOU CANNOT BUY THAT' in result.output)
def wrapper(*args, **kwargs): # UserCredentials is a singleton, that provides access to the credentials of the currently logged in user, # which are being saved in a temporary file credentials: UserCredentials = UserCredentials.instance() # The Rewardify object is the main facade to the rewardify system. It provides the methods for checking user # existence and the password. facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() # Some of the templates user the username to display the error context = {'username': credentials['username']} if credentials.is_default(): templater.echo_template('no_user.jinja2', context) raise click.Abort() if not facade.exists_user(credentials['username']): templater.echo_template('user_not_exists.jinja2', context) raise click.Abort() if not facade.user_check_password(credentials['username'], credentials['password']): templater.echo_template('wrong_password.jinja2', context) raise click.Abort() # Only if the user really truely is valid, the command is being executed return func(*args, **kwargs)
def test_test_case_working(self): self.setup_sample() # Now if everything was setup correctly the config instance from within the facade object should contain all # the sample values facade = Rewardify.instance() self.assertTrue(hasattr(facade.CONFIG, "BACKEND")) self.assertEqual(facade.CONFIG.BACKEND, MockBackend)
def test_checking_password_to_username(self): # Setting up the environment and Inserting a new user into the database self.setup_sample() self.create_sample_user() facade: Rewardify = Rewardify.instance() facade.user_check_password(self.SAMPLE_USER_PARAMETERS['name'], self.SAMPLE_USER_PARAMETERS['password'])
def test_getting_user_model_from_facade(self): # Setting up the environment and Inserting a new user into the database self.setup_sample() self.create_sample_user() facade: Rewardify = Rewardify.instance() user = facade.get_user(self.SAMPLE_USER_PARAMETERS['name']) self.assertIsInstance(user, User)
def listing(): facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() available_packs = facade.available_packs() context = {'packs': available_packs} templater.echo_template('pack_list.jinja2', context)
def listing(): facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() rarity_rewards_map = facade.available_rewards_by_rarity() context = defaultdict(list) context.update(rarity_rewards_map) templater.echo_template('reward_list.jinja2', context)
def test_opening_multiple_packs_at_once(self): pack_parameters = [{ 'name': 'Buy Me Pack', 'cost': 100, 'description': 'For testing', 'slot1': [1, 0, 0, 0], 'slot2': [1, 0, 0, 0], 'slot3': [0, 1, 0, 0], 'slot4': [0, 0, 1, 0], 'slot5': [0, 0, 0, 1], }] # Since the pack is configured in a way, that it will return at least one reward of each rarity, rewards with # these rarities HAVE TO EXIST reward_parameters = [{ 'name': 'Common Reward', 'description': 'for testing', 'rarity': 'common', 'cost': 100, 'recycle': 100 }, { 'name': 'Uncommon Reward', 'description': 'for testing', 'rarity': 'uncommon', 'cost': 100, 'recycle': 100 }, { 'name': 'Rare Reward', 'description': 'for testing', 'rarity': 'rare', 'cost': 100, 'recycle': 100 }, { 'name': 'Legendary Reward', 'description': 'for testing', 'rarity': 'legendary', 'cost': 100, 'recycle': 100 }] with MockConfigContext(self, packs=pack_parameters, rewards=reward_parameters) as mock_context, \ StandardUserContext() as user_context: facade: Rewardify = Rewardify.instance() facade.user_add_gold(user_context.username, 200) facade.user_buy_pack(user_context.username, 'Buy Me Pack') facade.user_buy_pack(user_context.username, 'Buy Me Pack') self.RUNNER.invoke(login, [user_context.username, user_context.password]) result = self.RUNNER.invoke(packs, ['open', '--all', 'Buy Me Pack']) user_context.update() self.assertEqual(result.exit_code, 0) self.assertEqual(len(user_context.user.packs), 0) self.assertEqual(len(user_context.user.rewards), 10) self.assertTrue('PACK OPENING' in result.output)
def update(): credentials: UserCredentials = UserCredentials.instance() facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() context = { 'backend': facade.CONFIG.BACKEND } facade.backend_update() templater.echo_template('updated.jinja2', context)
def test_user_exists(self): self.setup_sample() facade: Rewardify = Rewardify.instance() # Of course from the get go the user is not in the database, which means the exists call should return false user_exists = facade.exists_user(self.SAMPLE_USER_PARAMETERS['name']) self.assertFalse(user_exists) # After we have inserted the user though the exists call should return true self.create_sample_user() user_exists = facade.exists_user(self.SAMPLE_USER_PARAMETERS['name']) self.assertTrue(user_exists)
def test_create_user(self): with MockConfigContext(self) as mock_context: username = '******' password = '******' facade: Rewardify = Rewardify.instance() self.assertFalse(facade.exists_user(username)) result = self.RUNNER.invoke(users, ['create', username, password]) self.assertEqual(result.exit_code, 0) self.assertIn('USER CREATED', result.output) self.assertTrue(facade.exists_user(username))
def test_creating_new_user(self): facade: Rewardify = Rewardify.instance() # Creating the user username = '******' password = '******' facade.create_user(username, password) # Querying for the user to check if it exists query = User.select().where(User.name == username) self.assertTrue(len(query) >= 1) user = query[0] self.assertTrue(user.password.check(password))
def test_backend_update(self): self.setup_sample() self.create_sample_user() user = self.get_sample_user() self.assertEqual(user.gold, 0) facade: Rewardify = Rewardify.instance() facade.backend_update() # The mock backend gives every user specified in MOCK_BACKEND_USERS exactly 100 gold on every call of the # update, which means, that after the call the user should have been granted 100 gold from the mock backend. user = self.get_sample_user() self.assertEqual(user.gold, 100)
def create(username, password): credentials: UserCredentials = UserCredentials.instance() facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() context = { 'username': username, } try: facade.create_user(username, password) templater.echo_template('user_created.jinja2', context) except Exception as e: click.echo(e) raise click.Abort()
def inventory(): credentials: UserCredentials = UserCredentials.instance() facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() username = credentials['username'] context = { 'name': username, 'dust': facade.user_get_dust(username), 'gold': facade.user_get_gold(username), 'rewards': facade.user_get_rewards_by_name(username), 'packs': facade.user_get_packs_by_name(username) } templater.echo_template('inventory.jinja2', context)
def test_user_open_pack(self): self.setup_sample() self.create_sample_user() facade: Rewardify = Rewardify.instance() facade.user_add_gold(self.SAMPLE_USER_PARAMETERS['name'], 100) facade.user_buy_pack(self.SAMPLE_USER_PARAMETERS['name'], 'Sample Pack') user = self.get_sample_user() self.assertEqual(len(user.packs), 1) facade.user_open_pack(self.SAMPLE_USER_PARAMETERS['name'], 'Sample Pack') user = self.get_sample_user() self.assertEqual(len(user.packs), 0) self.assertEqual(len(user.rewards), 5)
def recycling(name): credentials: UserCredentials = UserCredentials.instance() facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() username = credentials['username'] context = { 'name': username, 'recycle': '{} dust'.format(facade.CONFIG.REWARDS[name]['recycle']), } try: facade.user_recycle_reward(username, name) templater.echo_template('reward_recycled.jinja2', context) except LookupError: templater.echo_template('permission_use_error.jinja2', context) raise click.Abort()
def test_user_use_reward_with_effect(self): self.setup_sample() self.create_sample_user() facade: Rewardify = Rewardify.instance() facade.user_add_dust(self.SAMPLE_USER_PARAMETERS['name'], 100) facade.user_buy_reward(self.SAMPLE_USER_PARAMETERS['name'], 'Gold Reward') user = self.get_sample_user() self.assertEqual(len(user.rewards), 1) self.assertEqual(user.gold, 0) facade.user_use_reward(self.SAMPLE_USER_PARAMETERS['name'], 'Gold Reward') user = User.select().where( User.name == self.SAMPLE_USER_PARAMETERS['name'])[0] self.assertEqual(len(user.rewards), 0) print(user.gold) self.assertEqual(user.gold, 100)
def buy(name): credentials: UserCredentials = UserCredentials.instance() facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() username = credentials['username'] context = { 'name': username, 'cost': '{} gold'.format(facade.CONFIG.PACKS[name]['cost']), 'balance': '{} gold'.format(facade.user_get_gold(username)) } try: facade.user_buy_pack(username, name) templater.echo_template('pack_bought.jinja2', context) except PermissionError: templater.echo_template('permission_buy_error.jinja2', context) raise click.Abort()
def test_using_single_reward(self): with MockConfigContext( self) as mock_context, StandardUserContext() as user_context: facade: Rewardify = Rewardify.instance() facade.user_add_dust(user_context.username, 100) facade.user_buy_reward(user_context.username, 'Standard Reward') user_context.update() self.assertEqual(len(user_context.user.rewards), 1) self.RUNNER.invoke(login, [user_context.username, user_context.password]) result = self.RUNNER.invoke(rewards, ['use', 'Standard Reward']) print(result.output) self.assertEqual(result.exit_code, 0) self.assertIn('REWARD USED', result.output)
def test_buying_rewards_works(self): with MockConfigContext( self) as mock_context, StandardUserContext() as user_context: facade: Rewardify = Rewardify.instance() credentials: UserCredentials = UserCredentials.instance() self.assertEqual(len(user_context.user.rewards), 0) facade.user_add_dust(user_context.username, 100) self.RUNNER.invoke(login, [user_context.username, user_context.password]) result = self.RUNNER.invoke(rewards, ['buy', 'Standard Reward']) user_context.update() self.assertEqual(result.exit_code, 0) self.assertIn('REWARD PURCHASE COMPLETED', result.output) self.assertEqual(user_context.user.dust, 0) self.assertEqual(len(user_context.user.rewards), 1)
def opening(all, name): credentials: UserCredentials = UserCredentials.instance() facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() username = credentials['username'] context = { 'name': username, 'count': 1, } name_rewards_map_before = defaultdict(list) name_rewards_map_before.update(facade.user_get_rewards_by_name(username)) try: if all: name_pack_map = facade.user_get_packs_by_name(username) pack_count = len(name_pack_map[name]) for i in range(pack_count): facade.user_open_pack(username, name) context.update({'count': pack_count}) else: facade.user_open_pack(username, name) name_rewards_map_after = facade.user_get_rewards_by_name(username) rarity_rewards_map = defaultdict(list) for name, rewards in name_rewards_map_after.items(): rewards_before = name_rewards_map_before[name] new_reward_count = len(rewards) - len(rewards_before) rarity = str(rewards[0].rarity) for i in range(new_reward_count): rarity_rewards_map[rarity].append(rewards[0]) context.update(rarity_rewards_map) templater.echo_template('pack_opened.jinja2', context) except LookupError: templater.echo_template('permission_use_error.jinja2', context) except Exception as e: click.echo(e)
def login(password, username): facade: Rewardify = Rewardify.instance() templater: Templater = Templater.instance() # The templates will display the username for more personalized messages context = {'username': username} if not facade.exists_user(username): templater.echo_template('user_not_exists.jinja2', context) raise click.Abort() if not facade.user_check_password(username, password): templater.echo_template('wrong_password.jinja2', context) raise click.Abort() # The UserCredentials singleton provides easy access to the credentials saved in the temp file. After the username # and password have been checked the new credentials can be saved persistently, so that every command after this # one has access to them credentials: UserCredentials = UserCredentials.instance() credentials.save(username, password) templater.echo_template('login_success.jinja2', context)
def test_buy_pack_working(self): pack_parameters = [{ 'name': 'Buy Me Pack', 'cost': 100, 'description': 'For testing', 'slot1': [1, 0, 0, 0], 'slot2': [1, 0, 0, 0], 'slot3': [0, 1, 0, 0], 'slot4': [0, 0, 1, 0], 'slot5': [0, 0, 0, 1], }] with MockConfigContext( self, packs=pack_parameters ) as mock_context, StandardUserContext() as user_context: facade: Rewardify = Rewardify.instance() credentials: UserCredentials = UserCredentials.instance() self.assertEqual(len(user_context.user.packs), 0) self.assertEqual(user_context.user.gold, 0) # After adding the necessary gold to the user, the buying operation should succeed, which means a success # exit code and the corresponding template being used facade.user_add_gold(user_context.username, 100) # First we need to login the user and then we double check if the new credentials are actually saved into # the credentials object self.RUNNER.invoke(login, [user_context.username, user_context.password]) self.assertEqual(credentials['username'], user_context.username) result = self.RUNNER.invoke(packs, ['buy', 'Buy Me Pack']) self.assertEqual(result.exit_code, 0) self.assertTrue('PACK PURCHASE COMPLETED' in result.output) # If the pack purchase has worked, the user now has one pack more and no gold anymore user_context.update() self.assertEqual(len(user_context.user.packs), 1) self.assertEqual(user_context.user.gold, 0)
def test_listing_rewards(self): reward_parameters = [{ 'name': 'Common Reward', 'description': 'for testing', 'rarity': 'common', 'cost': 100, 'recycle': 100 }, { 'name': 'Uncommon Reward', 'description': 'for testing', 'rarity': 'uncommon', 'cost': 100, 'recycle': 100 }, { 'name': 'Rare Reward', 'description': 'for testing', 'rarity': 'rare', 'cost': 100, 'recycle': 100 }, { 'name': 'Legendary Reward', 'description': 'for testing', 'rarity': 'legendary', 'cost': 100, 'recycle': 100 }] with MockConfigContext( self, rewards=reward_parameters ) as mock_context, StandardUserContext() as user_context: facade: Rewardify = Rewardify.instance() result = self.RUNNER.invoke(rewards, ['list']) print(result.output) self.assertEqual(result.exit_code, 0) self.assertIn('AVAILABLE REWARDS', result.output) self.assertIn('Common Reward', result.output)
def test_populated_inventory_working(self): with MockConfigContext( self) as mock_context, StandardUserContext() as user_context: self.RUNNER.invoke(login, [user_context.username, user_context.password]) facade: Rewardify = Rewardify.instance() facade.user_add_gold(user_context.username, 2000) facade.user_add_dust(user_context.username, 2000) facade.user_buy_pack(user_context.username, 'Standard Pack') facade.user_buy_pack(user_context.username, 'Standard Pack') facade.user_buy_reward(user_context.username, 'Standard Reward') result = self.RUNNER.invoke(inventory) print(result.output) self.assertEqual(result.exit_code, 0) self.assertIn('YOUR INVENTORY', result.output) self.assertIn('Standard Pack', result.output)