def registration_process(): user_id = request.form.get("id") email = request.form.get("email") password = request.form.get("password") age = request.form.get("age") if age: age = int(age) else: age = None zipcode = request.form.get("zipcode") user = User.query.filter_by(email=email).first() if user: flash( "You're already in our system? Please login to your account instead." ) return redirect("/login") else: user = User(user_id=user_id, email=email, password=password, age=age, zipcode=zipcode) db.session.add(user) db.session.commit() session['user_id'] = user.user_id flash( "Registration Successful. Thank You! Look through our Ratings! In the End, PLEASE Rate some of these Movies too!!" ) return redirect("/")
def setUp(self): db.create_all() new_user = User( wallet_address='0xc9cFdce5F5DF8e07443Ac2117D462050C8B9d225', fname='nathan', lname='plow', username='******', email='*****@*****.**', registered_on=datetime.now()) self.test_user = new_user new_miner = Miner( name='money-printer', user_id=new_user.id, user=new_user, ) self.test_miner = new_miner new_miner.gpus.append( Gpu(gpu_no=0, miner_id=new_miner.id, miner=new_miner)) db.session.add(new_user) db.session.add(new_miner) db.session.commit()
def test_add_user(self): user = User(username='******', email='*****@*****.**', password='******') db.session.add(user) db.session.commit() assert len(User.query.all()) == 1
def test_check_password(self): pw_hash = bcrypt.generate_password_hash('password') user = User(username='******', email='*****@*****.**', password=pw_hash) db.session.add(user) db.session.commit() user = User.query.filter_by(username='******').first() assert bcrypt.check_password_hash(user.password, 'password')
def reset_password(token): if current_user.is_authenticated: return redirect(url_for('main.index')) form = ResetPasswordForm(request.form) if form.validate_on_submit(): user = User.verify_reset_password_token(token) user.password = bcrypt.generate_password_hash(form.password.data) db.session.commit() flash('Your password has been reset.') return redirect(url_for('main.login')) return render_template('reset_password.html', form=form)
def register(): """Function for handling requests to the localhost:5000/register endpoint """ if current_user.is_authenticated: return redirect(url_for('main.index')) form = RegisterForm(request.form) if form.validate_on_submit(): username = form.username.data password = bcrypt.generate_password_hash(form.password.data) email = form.email.data user = User(username=username, password=password, email=email) db.session.add(user) db.session.commit() flash('Congrats, register success. You can log in now.', category='info') return redirect(url_for('main.login')) return render_template('register.html', form=form)
def test_encode_token(self): user = User( email_on="*****@*****.**", registered_on=datetime.datetime.utcnow(), ) db.session.add(user)
class TestShareSocket_EverythingExists(BaseTestCase): test_user = User() test_miner = Miner() def setUp(self): db.create_all() new_user = User( wallet_address='0xc9cFdce5F5DF8e07443Ac2117D462050C8B9d225', fname='nathan', lname='plow', username='******', email='*****@*****.**', registered_on=datetime.now()) self.test_user = new_user new_miner = Miner( name='money-printer', user_id=new_user.id, user=new_user, ) self.test_miner = new_miner new_miner.gpus.append( Gpu(gpu_no=0, miner_id=new_miner.id, miner=new_miner)) db.session.add(new_user) db.session.add(new_miner) db.session.commit() def tearDown(self): db.session.remove() db.drop_all() def test_add_one_invalid_share(self): """Test adding one new invalid share data for a miners gpu""" share_data = { 'gpu_no': '0', 'share_type': 'invalid', 'timestamp': '1621540877' } response = update_shares(self.test_miner.id, share_data) status_code = response[1] body = response[0] # test the response for errors in updating the db self.assertTrue("SUCCESS" in body.get('message').upper(), body.get('message')) self.assertEqual(body.get('status'), 'success') self.assertEqual(status_code, 201) # query the db to make sure the changes are reflected/correct exp_share_type = ShareType.invalid exp_num_entries = 1 query = db.session.query(Share).filter_by(miner_id=self.test_miner.id, gpu_no=0) # query.all() removes duplicates so use count() to test properly act_num_entries = query.count() self.assertEqual( act_num_entries, exp_num_entries, f"expected {exp_num_entries} entries, got {act_num_entries}") act_share_type = query.first().type self.assertEqual(act_share_type, exp_share_type, f"expected {exp_share_type}, got {act_share_type}") def test_add_multiple_invalid_share_same_gpu(self): """Test adding a lot of invalid shares for the same gpu""" share_data = [{ 'gpu_no': '0', 'share_type': 'invalid', 'timestamp': '1621540817' }, { 'gpu_no': '0', 'share_type': 'invalid', 'timestamp': '1621540870' }, { 'gpu_no': '0', 'share_type': 'invalid', 'timestamp': '1621540877' }] for datum in share_data: response = update_shares(self.test_miner.id, datum) status_code = response[1] body = response[0] # test the response for errors in updating the db self.assertTrue("SUCCESS" in body.get('message').upper(), body.get('message')) self.assertEqual(body.get('status'), 'success') self.assertEqual(status_code, 201) # query the db to make sure the changes are reflected/correct exp_share_type = ShareType.invalid exp_num_entries = len(share_data) query = db.session.query(Share).filter_by(miner_id=self.test_miner.id, gpu_no=0) # query.all() removes duplicates so use count() to test properly act_num_entries = query.count() self.assertEqual( act_num_entries, exp_num_entries, f"expected {exp_num_entries} entries, got {act_num_entries}") # go through all entries in the db for the specified gpu and map them to their PK -> timestamp ts_to_share = {} for share in query.all(): ts = int(share.time.timestamp()) ts_to_share[ts] = share # check through exp items and compare them against the map for datum in share_data: exp_share_type = ShareType[datum['share_type']] act_share_type = ts_to_share[int(datum['timestamp'])].type self.assertEqual( exp_share_type, act_share_type, f"expected {exp_share_type}, got {act_share_type}") def test_add_multiple_invalid_and_valid_share_same_gpu(self): """Test adding a lot of invalid and valid shares for the same gpu""" share_data = [{ 'gpu_no': '0', 'share_type': 'invalid', 'timestamp': '1621540817' }, { 'gpu_no': '0', 'share_type': 'invalid', 'timestamp': '1621540870' }, { 'gpu_no': '0', 'share_type': 'invalid', 'timestamp': '1621540877' }, { 'gpu_no': '0', 'share_type': 'valid', 'timestamp': '1621540872' }, { 'gpu_no': '0', 'share_type': 'valid', 'timestamp': '1621540873' }] for datum in share_data: response = update_shares(self.test_miner.id, datum) status_code = response[1] body = response[0] # test the response for errors in updating the db self.assertTrue("SUCCESS" in body.get('message').upper(), body.get('message')) self.assertEqual(body.get('status'), 'success') self.assertEqual(status_code, 201) # query the db to make sure the changes are reflected/correct exp_share_type = ShareType.invalid exp_num_entries = len(share_data) query = db.session.query(Share).filter_by(miner_id=self.test_miner.id, gpu_no=0) # query.all() removes duplicates so use count() to test properly act_num_entries = query.count() self.assertEqual( act_num_entries, exp_num_entries, f"expected {exp_num_entries} entries, got {act_num_entries}") # go through all entries in the db for the specified gpu and map them to their PK -> timestamp ts_to_share = {} for share in query.all(): ts = int(share.time.timestamp()) ts_to_share[ts] = share # check through exp items and compare them against the map for datum in share_data: exp_share_type = ShareType[datum['share_type']] act_share_type = ts_to_share[int(datum['timestamp'])].type self.assertEqual( exp_share_type, act_share_type, f"expected {exp_share_type}, got {act_share_type}")