def test_get_many_wallets(self): self.assertEqual(True, isinstance(Wallet.get(id=1), dict)) self.wallet1.save() self.wallet2.save() self.assertEqual(True, isinstance(Wallet.get_all()[0], Wallet)) self.assertEqual(True, isinstance(Wallet.get_all()[1], Wallet)) self.assertEqual(2, len(Wallet.get_all()))
def test_update_wallet(self): self.wallet1.save() wallet1 = Wallet.get(id=1) wallet1.update({'balance': 1000.0}) self.assertEqual(1000.0, Wallet.get(id=1).balance) self.assertTrue(isinstance( wallet1.update({'random': 'bad field'}), dict))
def setUp(self): """ Set up test application. """ self.app = create_app('testing') self.client = self.app.test_client() self.client.headers = {'Authorization': getenv('TEST_TOKEN')} self.app_context = self.app.app_context() self.app_context.push() db.drop_all() db.create_all() self.board1 = Board() self.board2 = Board() self.conversation1 = Conversation() self.conversation2 = Conversation() self.deposit1 = Deposit() self.deposit2 = Deposit() self.estate1 = Estate(address="Random Address 1") self.estate2 = Estate(address="Random Address 2") self.estate3 = Estate() self.headers = {'Authorization': getenv('TEST_TOKEN')} self.message1 = Message(sender=1, content='Random Content') self.message2 = Message(sender=2, content='Random Content') self.message3 = Message() self.payment1 = Payment() self.payment2 = Payment() self.role1 = Role(title='basic') self.role2 = Role(title='admin') self.role3 = Role(title='super_admin') self.role4 = Role() self.unit1 = Unit(name="Random Unit 1") self.unit2 = Unit(name="Random Unit 2") self.unit3 = Unit() self.user1 = User(name="First1 Middle1 Last1", phone_number="000 12 3456781", email="*****@*****.**", password=digest('ABC123!@#')) self.user2 = User(name="First2 Middle2 Last2", phone_number="000 12 3456782", email="*****@*****.**", password=digest('ABC123!@#')) self.user3 = User() self.user_new_data1 = {'phone_number': "000 12 3456783"} self.user_new_data2 = {'bad_field': "random"} self.wallet1 = Wallet() self.wallet2 = Wallet() self.user1_dict = { "name": "First1 Middle1 Last1", "phone_number": "000 12 3456781", "email": "*****@*****.**", "password": "******" } self.conversation3_dict = {"participants": [1, 2]} self.message4_dict = {"content": "New Message."} self.board3_dict = {'members': [1, 2]}
def test_add_and_remove_payments_from_wallet(self): self.wallet1.save() self.payment1.save() self.payment2.save() wallet1 = Wallet.get(id=1) self.assertEqual(0, len(wallet1.payments)) wallet1.insert('payments', [Payment.get(id=1), Payment.get(id=2)]) wallet1 = Wallet.get(id=1) self.assertEqual(True, isinstance(wallet1.payments[0], Payment)) self.assertEqual(True, isinstance(wallet1.payments[1], Payment)) self.assertEqual(2, len(wallet1.payments)) wallet1.remove('payments', id=1) self.assertEqual(1, len(wallet1.payments))
def create_wallet(obj, info, name, currency, coins): try: wallet = Wallet() wallet.name = name wallet.currency = currency for coin in coins: aux_coin = Coin.query.get(coin) wallet.coins.append(aux_coin) db.session.add(wallet) db.session.commit() payload = {'success': True, 'wallet': wallet.to_dict()} except ValueError as value_error: payload = {'success': False, 'errors': [str(value_error)]} return payload
def test_add_and_remove_deposit_estate_unit_wallet(self): self.deposit1.save() self.deposit2.save() self.estate1.save() self.unit1.save() self.wallet1.save() self.payment1.save() payment1 = Payment.get(id=1) payment1.estate = Estate.get(id=1) payment1.unit = Unit.get(id=1) payment1.wallet = Wallet.get(id=1) payment1.insert('deposits', [Deposit.get(id=1), Deposit.get(id=2)]) self.assertEqual(True, isinstance(payment1.estate, Estate)) self.assertEqual(True, isinstance(payment1.unit, Unit)) self.assertEqual(True, isinstance(payment1.wallet, Wallet)) self.assertEqual(True, isinstance(payment1.deposits[0], Deposit)) self.assertEqual(True, isinstance(payment1.deposits[1], Deposit)) self.assertEqual(2, len(payment1.deposits)) payment1.remove('estate') payment1.remove('unit') payment1.remove('wallet') self.assertEqual(None, payment1.estate) self.assertEqual(None, payment1.unit) self.assertEqual(None, payment1.wallet) payment1.remove('deposits', id=1) self.assertEqual(1, len(payment1.deposits))
def test_add_and_remove_wallet_owner(self): self.user1.save() self.wallet1.save() wallet1 = Wallet.get(id=1) wallet1.insert('owner', User.get(id=1)) self.assertEqual(True, isinstance(wallet1.owner, User)) wallet1.insert('owner', None) self.assertEqual(None, wallet1.owner)
def login(request): form = LoginForm(request.POST) if form.is_valid(): captcha_from_user = form.cleaned_data['captcha'] captcha_from_sess = request.session.get('captcha', '') if captcha_from_sess.lower() != captcha_from_user.lower(): data = {'code': 302, 'message': '验证码不匹配'} else: tel = form.cleaned_data['tel'] user = Users.objects.filter(u_tel=tel).first() if user: code_from_session = request.session.get('tel') code_from_user = request.POST.get('phoneCode') if code_from_session == code_from_user: request.session['user'] = user data = {'code': 200, 'message': '校验成功'} else: hint = '用户名或验证码错误' data = {'code': 300, 'message': hint} else: with atomic(): wallet = Wallet() wallet.save() user = Users() user.id = wallet user.u_tel = tel user.u_nickname = '用户{}'.format(uuid.uuid4()) user.save() role = Roles.objects.filter(id=1).first() user_role = Userrole() user_role.role = role user_role.user = user user_role.save() request.session['user'] = user data = {'code': 201, 'message': '用户注册成功,成功登陆'} else: hint = '请输入有效的登录信息' data = {'code': 301, 'message': hint} return JsonResponse(data)
def post(self): """ Create a new user. """ payload = request.get_json() required = ['email', 'name', 'password', 'phone_number'] result = validate_json(required, payload) if isinstance(result, bool) is True: new_user = User(name=payload['name'], phone_number=payload['phone_number'], email=payload['email'], password=digest(payload['password'])) basic_role = Role.get(title='basic') new_wallet = Wallet() new_user.insert('roles', [basic_role]) new_user.insert('wallet', new_wallet) new_user_id = new_user.save() return { 'status': 'success', 'message': 'User with id {} was created.'.format(new_user_id) }, 201 return {'status': 'fail', 'data': {'missing': result}}, 400
def seed_test_data(): """ Seed data to be used for testing. """ db.drop_all() db.create_all() payment_1 = Payment() payment_2 = Payment() payment_3 = Payment() payment_4 = Payment() payment_1.insert('deposits', [Deposit( amount=1000.0), Deposit(amount=500.0)]) payment_2.insert('deposits', [Deposit( amount=1000.0), Deposit(amount=500.0)]) payment_3.insert('deposits', [Deposit(amount=1000.0)]) payment_4.insert('deposits', [Deposit(amount=1000.0)]) wallet_1 = Wallet() wallet_2 = Wallet() wallet_3 = Wallet() wallet_4 = Wallet() wallet_1.insert('payments', [payment_1, payment_2]) wallet_2.insert('payments', [payment_3]) wallet_3.insert('payments', [payment_4]) estate1 = Estate(address='Casterly Rock') estate2 = Estate(address='Dorne') estate3 = Estate(address='Dragonstone') estate2.insert('payment', wallet_2.payments[0]) estate3.insert('payment', wallet_3.payments[0]) unit1 = Unit(name='No. 1') unit2 = Unit(name='No. 2') unit3 = Unit(name='No. 1') unit4 = Unit(name='No. 2') unit5 = Unit(name='No. 3') unit1.insert('payment', wallet_1.payments[0]) unit2.insert('payment', wallet_1.payments[1]) estate1.insert('estate_units', [unit1, unit2]) estate2.insert('estate_units', [unit3]) estate3.insert('estate_units', [unit4, unit5]) board1 = Board() board2 = Board() board1.insert('estates_owned', [estate1, estate2]) board2.insert('estates_owned', [estate3]) board1.insert('units_owned', [unit5]) user0 = User( name="Test User", phone_number="000 00 0000000", email="*****@*****.**", password=digest('ABC123!@#1')) user1 = User( name="Jaime Lannister", phone_number="000 12 3456781", email="*****@*****.**", password=digest('ABC123!@#1')) user2 = User( name="Oberyn Martell", phone_number="000 12 3456782", email="*****@*****.**", password=digest('ABC123!@#2')) user3 = User( name="Daenerys Targaryen", phone_number="000 12 3456783", email="*****@*****.**", password=digest('ABC123!@#3')) conversation1 = Conversation() conversation2 = Conversation() message1 = Message(sender=user1.name, content='Content 1') message2 = Message(sender=user2.name, content='Content 2') message3 = Message(sender=user3.name, content='Content 3') conversation1.insert('messages', [message1, message2]) conversation2.insert('messages', [message3]) user0.insert('boards', [board1]) user1.insert('boards', [board1]) user2.insert('boards', [board1]) user3.insert('boards', [board2]) user1.insert('units', [unit1, unit2]) user2.insert('units', [unit3]) user3.insert('units', [unit4]) user0.insert('wallet', wallet_4) user1.insert('wallet', wallet_1) user2.insert('wallet', wallet_2) user3.insert('wallet', wallet_3) user1.insert('conversations', [conversation1, conversation2]) user2.insert('conversations', [conversation1, conversation2]) user3.insert('conversations', [conversation1]) role1 = Role(title='admin') role2 = Role(title='basic') role3 = Role(title='super_admin') role3.insert('users', [user0, user1]) role2.insert('users', [user0, user1, user2, user3]) role1.insert('users', [user0, user3]) role1.save() role2.save() role3.save() print('\nTest Data Seeded.\n')
def create(self, validated_data: dict) -> Wallet: wallet = Wallet(user=validated_data['user'], currency=validated_data['currency'], balance=int(validated_data['balance'])) wallet.save() return wallet
def test_wallet_exists(self): self.assertFalse(Wallet.check_exists(id=1)) self.wallet1.save() self.assertTrue(Wallet.check_exists(id=1))
def test_get_wallet(self): self.assertEqual(True, isinstance(Wallet.get(id=1), dict)) self.wallet1.save() self.assertEqual(True, isinstance(Wallet.get(id=1), Wallet))