def seed_db(): """ Seed the database """ db.session.add( User(username='******', email='*****@*****.**', password='******')) db.session.add( User(username='******', email='*****@*****.**', password='******')) db.session.commit()
def get_user_status(): # get auth tokne auth_header = request.headers.get('Authorization') if auth_header: auth_token = auth_header.split(" ")[1] response = User.decode_auth_token(auth_token) if not isinstance(response, str): user = User.query.filter_by(id=response).first() response_object = { 'status': 'success', 'data': { 'id': user.id, 'username': user.username, 'email': user.email, 'active': user.active, 'created_at': user.created_at } } return jsonify(response_object), 200 response_object = { 'status': 'fail', 'message': response } return jsonify(response_object),401 else: response_object = { 'status': 'fail', 'message': 'Provide a valid token.' } return jsonify(response_object), 401
def add_user(username, email, password, created_at=datetime.datetime.utcnow()): user = User(username=username, email=email, password=password, created_at=created_at) db.session.add(user) db.session.commit() return user
def register_user(): post_data = request.get_json() if not post_data: response_object = { 'status': 'fail', 'message': 'Invalid payload.' } return jsonify(response_object), 400 username = post_data.get('username') email = post_data.get('email') password = post_data.get('password') try: # check for existing user user = User.query.filter( or_(User.username == username, User.email == email)).first() if not user: new_user = User( username=username, email=email, password=password ) db.session.add(new_user) db.session.commit() # generate auth token auth_token = new_user.encode_auth_token(new_user.id) response_object = { 'status': 'success', 'message': 'Successfully registered.', 'auth_token': auth_token.decode() } return jsonify(response_object), 201 else: response_object = { 'status': 'fail', 'message': 'Sorry. That user already exists.' } return jsonify(response_object),400 # handle errors except (exc.IntegrityError, ValueError) as err: db.session.rollback() response_object = { 'status': 'fail', 'message': 'Invalid payload' } return jsonify(response_object), 400
def test_add_user_duplicate_email(self): add_user('curtis', '*****@*****.**', 'password1234') duplicate_email = User(username='******', email='*****@*****.**', password='******') db.session.add(duplicate_email) # self.assertRaises(IntegrityError, db.session.commit()) with self.assertRaises(IntegrityError) as cm: db.session.commit()
def index(): if request.method == 'POST': username = request.form['username'] email = request.form['email'] password = request.form['password'] db.session.add(User(username=username,email=email, password=password)) db.session.commit() users = User.query.order_by(User.created_at.desc()).all() return render_template('index.html', users=users)
def add_users(): post_data = request.get_json() if not post_data: response_object = { 'status': 'fail', 'message': 'Invalid payload' } return jsonify(response_object), 400 username = post_data.get('username') email = post_data.get('email') password = post_data.get('password') try: user = User.query.filter_by(email=email).first() if not user: db.session.add(User( username=username, email=email, password=password)) db.session.commit() response_object = { 'status': 'success', 'message': f'{email} was added!' } return jsonify(response_object), 201 else: response_object = { 'status': 'fail', 'message': 'Sorry. That email already exists.' } return jsonify(response_object), 400 except (exc.IntegrityError, ValueError) as e: db.session.rollback() response_object = { 'status': 'fail', 'message': 'Invalid payload.' } return jsonify(response_object), 400
def logout_user(): # get auth token auth_header = request.headers.get('Authorization') if auth_header: auth_token = auth_header.split(" ")[1] response = User.decode_auth_token(auth_token) if not isinstance(response, str): response_object = { 'status': 'success', 'message': 'Successfully logged out.' } return jsonify(response_object), 200 else: response_object = { 'status': 'fail', 'message': response } return jsonify(response_object), 403 else: response_object = { 'status': 'fail', 'message': 'Provide a valid auth token.' } return jsonify(response_object), 401
def test_decode_auth_token(self): user = add_user('mans', '*****@*****.**', '4321drowssap') auth_token = user.encode_auth_token(user.id) self.assertTrue(isinstance(auth_token, bytes)) self.assertEqual(User.decode_auth_token(auth_token), user.id)