def seed(dbpath=DBPATH): ORM.dbpath = dbpath default = Account(username='******', balance=10000.00, first='sami', last='s', api_key="12345678912345678902") default.set_password('1234') default.save() default_buy = Trade(buy_sell='Buy', username='******', ticker='tsla', price='100', shares='10', time=time()) default_buy.save() default_sale = Trade(buy_sell='Sell', username='******', ticker='tsla', price='110', shares='5', time=time()) default_sale.save() default_position = Position(username='******', ticker='tsla', shares='5') default_position.save()
def create_account(): if not request.json: return jsonify({'error': 'bad request'}), 400 if 'username' not in request.json or 'password' not in request.json or 'firstname' not in request.json or 'lastname' not in request.json: return jsonify({'error': 'bad request'}), 400 username = request.json['username'] password = request.json['password'] first = request.json['firstname'] last = request.json['lastname'] uniqueUsername = Account().username_preventduplicate(username) if uniqueUsername == None: new_account = Account(username=username, balance=0, first=first, last=last) new_account.set_password(password) new_account.generate_api_key() new_account.save() return jsonify( { "Your account was created successfully.": "You start with a balance of $0.00" }, {"Your api key is": new_account.values['api_key']}) else: return jsonify({"error": "bad username"})
def test_encode_auth_token(self): account = Account(email="*****@*****.**", password="******") account.save() auth_token = account.encode_auth_token() assert isinstance(auth_token, bytes)
def post(self): # get the post data post_data = request.get_json() # check if account already exists account = Account.query.filter_by( email=post_data.get('email')).one_or_none() if not account: try: account = Account(email=post_data.get('email'), password=post_data.get('password')) account.save() # generate auth token aut_token = account.encode_auth_token() response = { 'status': 'success', 'message': 'Successfully registered.', 'auth_token': aut_token.decode() } return make_response(jsonify(response)), 201 except Exception as e: response = { 'status': 'fail', 'message': 'Some error occurred. Please try again.' } return make_response(jsonify(response)), 401 else: response = { 'status': 'fail', 'message': 'User already exists. Please log in.' } return make_response(jsonify(response)), 202
def test_generate_api_key(self): user = Account(username='******', balance=10000) user.set_password('1234') user.save() user.generate_api_key() reloaded = Account.login('some_user', '1234') self.assertEqual( user.values['api_key'], reloaded.values['api_key'], "check that the user's api_key is the same as the one generated")
def test_register_with_already_registered_account(self): """Test registration with already registered email.""" account = Account( email='*****@*****.**', password='******' ) account.save() with self.client: response = register_account(self, '*****@*****.**', '123456') assert response.content_type == 'application/json' assert response.status_code == 202 data = json.loads(response.data.decode()) assert data['status'] == 'fail' assert data['message'] == 'User already exists. Please log in.'
def test_save_and_pk_load(self): user = Account(username="******") user.save() self.assertIsInstance(user.values['pk'], int, 'save sets pk') pk = user.values['pk'] same_user = Account.one_from_pk(pk) self.assertIsInstance(same_user, Account, "one_from_pk loads an Account object") self.assertEqual(same_user.values['username'], "Greg", "save creates database row") same_user.values['username'] = "******" same_user.save() same_again = Account.one_from_pk(pk) self.assertEqual(same_again.values['username'], "Gregory", "save updates an existing row")
def test_save_and_pk_load(self): user = Account(username='******') user.save() self.assertIsInstance( user.values['pk'], int, 'save sets pk' ) #assert will always return a boolean - if we get false back, something is broken pk = user.values['pk'] same_user = Account.one_from_pk(pk) self.assertIsInstance(same_user, Account, 'one_from_pk loads an Account object') self.assertEqual(same_user.values['username'], 'Sami', 'save creates database row') same_user.values['username'] = '******' same_user.save() same_again = Account.one_from_pk(pk) self.assertEqual(same_again.values['username'], 'Gregory', 'save updates an exisiting row')
def create_account(): if not request.json: return jsonify({'error': 'bad request'}), 400 if 'username' not in request.json or 'password' not in request.json or 'first_name' not in request.json or 'last_name' not in request.json: return jsonify({'error': 'bad request'}), 400 username = request.json['username'] password = request.json['password'] first = request.json['first_name'] last = request.json['last_name'] pword_hash = hash_password(password) new_account = Account(username=username, password_hash=pword_hash, balance=10000, first=first, last=last) new_account.generate_api_key new_account.save() return jsonify( { "Your account was created successfully.": "You start with a balance of $10,000." }, {"Your api key is": new_account.values['api_key']})