def callback(): if session.get('error', None): return redirect('/') if session['state'] != request.args.get('state', None): return redirect('/') tokens = google_api.get_oauth_tokens( url_for('server.routes.auth.callback', _external=True), request.args['code']) email = google_api.get_user_email(tokens['access_token']) user = User.get(email) if not user: user = User(_id=email, subscriptions=[], collections={}, credentials=google_api.create_credentials(tokens), job_id=None, last_updated=datetime.utcnow()) job = queue.enqueue('server.utils.google_api.build_collections', access_token=tokens['access_token'], username=email) user.job_id = job.id user.insert() else: user.update({'credentials': google_api.create_credentials(tokens)}) login_user(user, remember=True, duration=timedelta(30)) return redirect('/')
def test_get_by_id(self): """Get user by ID.""" user = User('*****@*****.**') user.save() retrieved = User.get_by_id(user.id) assert retrieved == user
def post(self): parser = reqparse.RequestParser() parser.add_argument('email', required=True, help='email field is required') parser.add_argument('full_name', required=True, help='full_name field is required') parser.add_argument('username', required=True, help='username field is required') parser.add_argument('password', required=True, help='password field is required') parser.parse_args() full_name = request.form['full_name'] email = request.form['email'] password = request.form['password'] username = request.form['username'] user = User(full_name, username, email, password) check_user = user.check_username_email(username, email) if check_user == None: user.save() user_details = { 'id': user.id, 'username': user.username, 'email': user.email, 'full_name': user.full_name } token = create_access_token(identity=user.id) return { 'message': 'User created successfully', 'user': user_details, 'token': token } else: if check_user.username == username: return {'message': 'Username already in use'}, 409 else: return {'message': 'Email already in use'}, 409
def test_password_is_nullable(self): """Test null password.""" user = User(email='*****@*****.**') user.save() assert user.password is None
def test_created_at_defaults_to_datetime(self): """Test creation date.""" user = User(email='*****@*****.**') user.save() assert bool(user.created_at) assert isinstance(user.created_at, dt.datetime)