def test_get_by_id(self): """Get user by ID.""" user = User('foo', '*****@*****.**') user.save() retrieved = User.get_by_id(user.id) assert retrieved == user
def test_user_token_auth(self): user = User.create(username='******', email='*****@*****.**', password='******') auth_token = user.generate_auth_token() invalid_token = "" assert User.verify_auth_token(invalid_token) is None assert User.verify_auth_token(auth_token) is user
def register(): """Register new user.""" form = RegisterForm(request.form, csrf_enabled=False) if form.validate_on_submit(): User.create(username=form.username.data, email=form.email.data, password=form.password.data, active=True) flash('Thank you for registering. You can now log in.', 'success') return redirect(url_for('public.home')) else: flash_errors(form) return render_template('public/register.html', form=form)
def test_check_password(self): """Check password.""" user = User.create(username='******', email='*****@*****.**', password='******') assert user.check_password('foobarbaz123') is True assert user.check_password('barfoobaz') is False
def get(self): args = recommend_get_parse.parse_args() user = User.verify_auth_token(args.access_token) if user: return make_beers_arr(user.ratings), 200 else: return None, 401
def post(self): create_user_parse = reqparse.RequestParser() create_user_parse.add_argument('username', dest='username', required=True, help="Username of user to be created") create_user_parse.add_argument('password', dest='password', required=True, help="Password of user to be created") create_user_parse.add_argument('password_confirm', dest='password_confirm', required=True, help="Password confirmation of user to be created") create_user_parse.add_argument('email', dest='email', required=True, help="Email address of user to be created") args = create_user_parse.parse_args() user = User.query.filter(User.username == args.username).first() emailuser = User.query.filter(User.email == args.email).first() if user: return {"message": "username already exists"}, 400 elif emailuser: return {"message": "email already registered"}, 400 elif args.password != args.password_confirm: return {"message": "passwords do not match"}, 400 elif len(args.password) < 6 or len(args.password) > 128: return {"message": "invalid password length"}, 400 elif not args.username or len(args.username) > 80: return {"message": "invalid username length"}, 400 else: valid_user = User.create(username=args.username, password=args.password, email=args.email, active=True) return {'access_token': valid_user.generate_auth_token()}, 201
def get(self): args = recommend_get_parse.parse_args() user = User.verify_auth_token(args.access_token) if user: return {"username": user.username, "taste_profile": user.get_profile(), "initial_profile": user.get_taste_profile(), "ratings": make_beers_arr(user.ratings)}
def get(self): args = recommend_get_parse.parse_args() user = User.verify_auth_token(args.access_token) if user: #lol for now beer = user.reccommend() return beer.to_data() else: return None, 401
def get(self): profile_get_parser = reqparse.RequestParser() profile_get_parser.add_argument('access_token', dest='access_token', type=str, required=True, help='Access token of user profile') args = profile_get_parser.parse_args() user = User.verify_auth_token(args.access_token) if user: return user.get_taste_profile(), 201 else: return None, 400
def post(self): args = rate_post_parser.parse_args() user = User.verify_auth_token(args.access_token) rating_val = args.rating beer_id = args.beer_id if user: rating = Rating.query.filter(Rating.user_id == user.id, Rating.beer_id == beer_id).first() if rating: rating = rating.update_time(beer_id=beer_id, user_id=user.id, rating=rating_val) else: rating = Rating.create(beer_id=beer_id, user_id=user.id, rating=rating_val) return rating.id, 201 else: return None, 401
def post(self): args = profile_set_parser.parse_args() user = User.verify_auth_token(args.access_token) if user: print(args) print(user.taste_profile) user.update_taste_profile(malty=args.malty, sour=args.sour, wood=args.wood, spice=args.spice, fruit=args.fruit, sweet=args.sweet, roasty=args.roasty, bitter=args.bitter, smoke=args.smoke, hoppy=args.hoppy) print(user.taste_profile) return user.get_taste_profile(), 201 else: return None, 400
def test_recommend_returns_closest(self): user = User.create(username='******', email='*****@*****.**', password='******'tcare') beer = Beer.create(beer_name='testbeer', abv=2.3, bitter=2.3, color=4, fruit=3, hoppy=4, malty=3, roasty=2, smoke=1, sour=3, spice=4.5, family=1, sweet=2, wood=3) beer1 = Beer.create(beer_name='testbeer1', abv=5.3, bitter=6, color=3, fruit=7, hoppy=8, malty=3, roasty=2, smoke=1, sour=3, spice=4.5, family=1, sweet=2, wood=3) user.update_taste_profile(malty=0, bitter=7, fruit=6.5, hoppy=7, wood=2, sweet=3, spice=1.4, sour=3.4, smoke=4, roasty=3) assert user.reccommend() is beer1 assert user.reccommend() is not beer
def test_recommend_returns(self): user = User.create(username='******', email='*****@*****.**', password='******'tcare') beer = Beer.create(beer_name='testbeer', abv=2.3, bitter=2.3, color=4, fruit=5, hoppy=4, malty=3, roasty=2, smoke=1, sour=3, spice=4.5, family=1, sweet=2, wood=3) beer_rec = user.reccommend() assert beer_rec == beer
def test_rating_beer(self): user = User.create(username='******', email='*****@*****.**', password='******'tcare') beer = Beer.create(beer_name='testbeer', abv=2.3, bitter=2.3, color=4, fruit=3, hoppy=4, malty=3, roasty=2, smoke=1, sour=3, spice=4.5, family=1, sweet=2, wood=3) Rating.create(user_id=user.id, beer_id=beer.id, rating=4) assert [x for x in user.ratings] is not [] assert [x for x in user.ratings][0].rating is 4
def load_user(user_id): """Load user by ID.""" return User.get_by_id(int(user_id))
def test_password_is_nullable(self): """Test null password.""" user = User(username='******', email='*****@*****.**') user.save() assert user.password is None
def test_created_at_defaults_to_datetime(self): """Test creation date.""" user = User(username='******', email='*****@*****.**') user.save() assert bool(user.created_at) assert isinstance(user.created_at, dt.datetime)
def get(self): args = recommend_get_parse.parse_args() user = User.verify_auth_token(args.access_token) x = grab_matching_ratings(user, lambda x: x < 3) return {'beers': x, 'total': len(x)}