def init_real_data_v1(): ''' - Fills the system by real data to test the interface by real users. ''' rebuild_db() data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'real_data_v1') print 'The process "init_real_data_v1" has been started.' print 'path - %s' % data_dir # User creation. users = json.load(open(os.path.join(data_dir, 'users.json'))) for user in users: user['obj'] = UserModel.register(user['email'], user['password'], user['name']) user['id'] = user['obj'].id # Budget creation. budget = json.load(open(os.path.join(data_dir, 'budget.json'))) budget['obj'] = BudgetModel.create(budget['title'], users[0]['id']) budget['id'] = budget['obj'].id # Users are attached to the budget. for user in users[1:]: budget['obj'].attach_user(user['obj'].id) # Tag creation. with open(os.path.join(data_dir, 'tags.json')) as json_file: tags = json.load(json_file) flat_tags = {} def go_over(tags, parent=None): for tag_name, tag in tags.items(): parent_id = parent['obj']['id'] if parent else None tag['obj'] = TagModel.create(tag_name, budget['id'], parent_id) flat_tags[tag_name] = tag if '_' in tag: go_over(tag['_'], parent) go_over(tags) # Contribution creation. # @todo - Fill contributions. # Expense creation. with open(os.path.join(data_dir, 'expenses.csv')) as csv_file: reader = csv.reader(csv_file) for row in reader: tag_list = [flat_tags[row[2]]['obj']] user = users[0]; # @todo - pass expenses expenses. ExpenseModel.create(budget['id'], user['id'], row[1], tag_list, '')
def test_budget(self): # Creates a test user. name = 'user_1' self.test_user = UserModel.register('*****@*****.**' % name, name, name) self.test_budget = BudgetModel.create(self.test_budget_data['title'], self.test_user.id) self.test_tag = TagModel.create(self.test_tag_data['title'], self.test_budget.id) assert self.test_tag.title == self.test_tag_data['title'] assert self.test_tag.budget_id == self.test_budget.id tag_id = self.test_tag.id another_tag_object = TagModel.load_by_id(tag_id) assert another_tag_object.id == tag_id TagModel.delete_by_id(tag_id) another_tag_object = TagModel.load_by_id(tag_id) assert another_tag_object is None
def test_budget(self): # Creates a test user. name = 'user_1' user = UserModel.register('*****@*****.**' % name, name, name) # Creates a budget, attaches an owner. budget_title = 'budget_title' budget = BudgetModel.create(budget_title, user.id) # Creates a budget, attaches an owner. another_budget_title = 'another_budget_title' another_budget = BudgetModel.create(another_budget_title, user.id) tag_1 = TagModel.create('tag_1', budget.id) tag_2 = TagModel.create('tag_2', budget.id) tag_3 = TagModel.create('tag_3', budget.id) tag_4 = TagModel.create('tag_4', budget.id) another_budget_tag_5 = TagModel.create('another_budget_tag_5', another_budget.id) # Registers an expense. expense_dict = dict( user_id=user.id, amount=40, description='Bought bananas at the market.' ) # Tags from another budget are not allowed. expense = self.assertRaises(Exception, ExpenseModel.create, (budget.id, expense_dict['user_id'], expense_dict['amount'], [tag_1, another_budget_tag_5], expense_dict['description'])) # Creates and attaches an expense. expense = ExpenseModel.create(budget.id, expense_dict['user_id'], expense_dict['amount'], [tag_1, tag_3], expense_dict['description']) assert expense.id > 0 expense_dict['id'] = expense.id # Loads the expense. another_expense = ExpenseModel.load_by_id(expense_dict['id']) assert another_expense.id > 0 and another_expense.id == expense_dict['id'] # Deletes the expense. ExpenseModel.remove_by_id(expense_dict['id']) another_expense = ExpenseModel.load_by_id(expense_dict['id']) assert another_expense is None
def init_test_db(test_data): ''' - Fills the system by test data. ''' rebuild_db() # User creation. for name in test_data['users']: test_data['user_objects'][name] = UserModel.register('*****@*****.**' % name, name, name) # Budget creation. for budget_dict in test_data['budgets']: # Budget creation. owner_name = test_data['users'][0] user_id = test_data['user_objects'][owner_name].id budget_object = BudgetModel.create(budget_dict['title'], user_id) budget_dict['budget_object'] = budget_object # Users are attached to the budget. for name in test_data['users'][1:]: user_object = test_data['user_objects'][name] budget_object.attach_user(user_object.id) # Tag creation. for tag_name in budget_dict['tags']: tag = TagModel.create(tag_name, budget_object.id) budget_dict['tag_objects'][tag_name] = tag # Contribution creation. for contribution_dict in budget_dict['contributions']: user_object = test_data['user_objects'][contribution_dict['user']] contribution_object = budget_object.add_contribution(user_object.id, contribution_dict['amount']) contribution_dict['object'] = contribution_object # Expense creation. for expense_dict in budget_dict['expenses']: user_object = test_data['user_objects'][expense_dict['user']] tag_list = [ budget_dict['tag_objects'][tag_name] for tag_name in expense_dict['tags']] expense_dict['object'] = ExpenseModel.create(budget_object.id, user_object.id, expense_dict['amount'], tag_list, expense_dict['description'])
def test_budget(self): # Creates a test user. name = 'user_1' user = UserModel.register('*****@*****.**' % name, name, name) # Creates a budget, attaches an owner. budget_title = 'budget_title' budget = BudgetModel.create(budget_title, user.id) assert budget.title == budget_title # Loads a budget by id. budget_id = budget.id another_budget_object = BudgetModel.load_by_id(budget_id) assert another_budget_object.id == budget_id # Attaches another user to the budget. name = 'user_2' another_user = UserModel.register('*****@*****.**' % name, name, name) budget_user = budget.attach_user(another_user.id, 'watcher') assert budget_user.user_id == another_user.id and budget_user.budget_id == budget.id # Attaches a user at the second time. self.assertRaises(LogicException, budget.attach_user, another_user.id, 'watcher') # Checks if the user is attached. assert budget.user_is_attached(user.id) # Deattaches the single owner from the budget. self.assertRaises(Exception, budget.deattach_user, user.id) # Deattaches the user. budget.deattach_user(another_user.id) assert not budget.user_is_attached(another_user.id) # Registers a contribution. contribution_dict = dict( user_id=user.id, amount=40, description='A month income from the salary.', obj_id=None, obj=None ) contribution = budget.add_contribution(contribution_dict['user_id'], contribution_dict['amount'], contribution_dict['description']) contribution_dict['obj'] = contribution contribution_dict['obj_id'] = contribution.id assert (contribution.id > 0\ and contribution.user_id == contribution_dict['user_id']\ and contribution.amount == contribution_dict['amount']\ and contribution.description == contribution_dict['description']) # Loads the contribution. another_contribution = budget.load_contribution_by_id(contribution_dict['obj_id']) assert another_contribution.id > 0 and another_contribution.id == contribution_dict['obj_id'] tag_1 = TagModel.create('tag_1', budget.id) tag_2 = TagModel.create('tag_2', budget.id) tag_3 = TagModel.create('tag_3', budget.id) tag_4 = TagModel.create('tag_4', budget.id) # Registers an expense. expense_dict_list = [ dict( user_id=user.id, amount=40, tags=[tag_1, tag_3], description='Bought bananas at the market.', obj=None ), dict( user_id=user.id, amount=80, tags=[tag_2, tag_4], description='Taxi.', obj=None ), dict( user_id=another_user.id, amount=80, tags=[tag_3, tag_4], description='Apples.', obj=None ) ] for expense_dict in expense_dict_list: expense_dict['obj'] = ExpenseModel.create(budget.id, expense_dict['user_id'], expense_dict['amount'], [tag_1, tag_3], expense_dict['description']) expense_dict['obj_id'] = expense_dict['obj'].id # Deletes a budget by id, checks related data. BudgetModel.delete_by_id(budget_id) another_budget_object = BudgetModel.load_by_id(budget_id) assert another_budget_object is None # Expenses are deleted. for expense_dict in expense_dict_list: assert None == ExpenseModel.load_by_id(expense_dict_list[0]['obj_id']) # Contributions are deleted. contribution = budget.load_contribution_by_id(contribution_dict['obj_id']) assert None == contribution
def test_user(self): user_dict = dict( name='user_1', password='******', email='*****@*****.**' ) user_obj = UserModel.register(user_dict['email'], user_dict['password'], user_dict['name']) @app.route('/test_auth_route/') @requires_auth def requires_auth_route(): return 'text' @app.route('/test_anonym_route/') @requires_anonym def requires_anonym_route(): return 'text' # It tests the auth lib. with app.test_request_context(): # The user is not authenticated yet. assert not is_authenticated() # Default user state. logged_user_dict = get_current_user() assert None is logged_user_dict # The user is logged in. login(user_obj) logged_user_dict = get_current_user() assert logged_user_dict['id'] > 0 and logged_user_dict['id'] == user_obj.id # The user is authenticated. assert is_authenticated() # It tests the main with app.test_client() as client: # The stricted page. It is forbidden. response = client.get('/test_auth_route/') assert response.status_code == 401 # Anonymous only. It is allowed. response = client.get('/test_anonym_route/') assert response.status_code == 200 # It makes an user authenticated. with client.session_transaction() as session: session['user'] = dict( id=user_obj.id, name=user_obj.name ) # It test the restricted route. response = client.get('/test_auth_route/') assert response.status_code == 200 # Anonymous only. It is forbidden. response = client.get('/test_anonym_route/') assert response.status_code == 401