Beispiel #1
0
    def setUp(self):
        super(ChatTestCase, self).setUp()

        admin = User(email='*****@*****.**', nickname='Admin User', github='https://github.com/greyli')
        admin.set_password('123')
        message1 = Message(body='Test Message 1')
        message2 = Message(body='Test Message 2')
        message3 = Message(body='Test Message 3')
        admin.messages = [message1, message2, message3]
        db.session.add(admin)
        db.session.commit()
Beispiel #2
0
    def setUp(self):
        app = create_app('testing')
        self.context = app.test_request_context()
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()

        db.create_all()
        user = User(nickname='Grey Li', email='*****@*****.**')
        user.set_password('123')
        db.session.add(user)
        db.session.commit()
Beispiel #3
0
    def forge(message):
        """Generate fake data."""
        import random
        from sqlalchemy.exc import IntegrityError

        from faker import Faker

        fake = Faker()

        click.echo('Initializing the database...')
        db.drop_all()
        db.create_all()

        click.echo('Forging the data...')
        admin = User(nickname='Grey Li', email='*****@*****.**')
        admin.set_password('helloflask')
        db.session.add(admin)
        db.session.commit()

        click.echo('Generating users...')
        for i in range(50):
            user = User(nickname=fake.name(),
                        bio=fake.sentence(),
                        github=fake.url(),
                        website=fake.url(),
                        email=fake.email())
            db.session.add(user)
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()

        click.echo('Generating messages...')
        for i in range(message):
            message = Message(
                author=User.query.get(random.randint(1, User.query.count())),
                body=fake.sentence(),
                timestamp=fake.date_time_between('-30d', '-2d'),
            )
            db.session.add(message)

        db.session.commit()
        click.echo('Done.')
Beispiel #4
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('chat.home'))

    if request.method == 'POST':
        email = request.form['email'].lower()

        user = User.query.filter_by(email=email).first()
        if user is not None:
            flash('The email is already registered, please log in.')
            return redirect(url_for('.login'))

        nickname = request.form['nickname']
        password = request.form['password']

        user = User(nickname=nickname, email=email)
        user.set_password(password)
        db.session.add(user)

        # give this user a unknown room
        user = User.query.filter_by(email=email).first()
        user_room = UserRoom(user_id=user.id, room_id=1)
        db.session.add(user_room)
        db.session.commit()

        # incre room 1's total user
        room = Room.query.get(1)
        room.user_total += 1
        db.session.commit()

        # incre 1 for all the user room's cur_user_total
        user_rooms = UserRoom.query.filter(UserRoom.user_id == user.id).all()
        for user_room in user_rooms:
            room = Room.query.get(user_room.room_id)
            room.cur_user_total += 1
            db.session.commit()

        login_user(user, remember=True)
        return redirect(url_for('chat.profile'))

    return render_template('auth/register.html')
Beispiel #5
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))

    if request.method == 'POST':
        email = request.form['email'].lower()

        user = User.query.filter_by(email=email).first()
        if user is not None:
            flash('邮箱已存在,请登陆')
            return redirect(url_for('.login'))

        nickname = request.form['nickname']
        password = request.form['password']

        user = User(nickname=nickname, email=email)
        user.set_password(password)
        db.session.add(user)
        db.session.commit()
        login_user(user, remember=True)
        return redirect(url_for('chat.profile'))
    return render_template('auth/register.html')
Beispiel #6
0
    def setUp(self) -> None:
        app = create_app('testing')
        self.context = app.test_request_context()  # 为了url_for()的正常使用需要注册请求上下文
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()

        db.create_all()
        user = User(email='*****@*****.**', nickname='guoxy2016')
        user.password = '******'
        message = Message(body='Test Message 1', author=user)

        normal_user = User(email='*****@*****.**', nickname='normal_user')
        normal_user.password = '******'
        db.session.add_all([message, normal_user])
        db.session.commit()
Beispiel #7
0
def oauth_callback(provider_name):
    import pdb
    pdb.set_trace()
    if provider_name not in providers.keys():
        abort(404)

    provider = providers[provider_name]
    response = provider.authorized_response()

    if response is not None:
        if provider_name == 'twitter':
            access_token = response.get('oauth_token'), response.get(
                'oauth_token_secret')
        else:
            access_token = response.get('access_token')
    else:
        access_token = None

    if access_token is None:
        flash('Access denied, please try again.')
        return redirect(url_for('auth.login'))

    username, website, github, email, bio = get_social_profile(
        provider, access_token)

    user = User.query.filter_by(email=email).first()
    if user is None:
        user = User(email=email,
                    nickname=username,
                    website=website,
                    github=github,
                    bio=bio)
        db.session.add(user)
        db.session.commit()
        login_user(user, remember=True)
        return redirect(url_for('chat.profile'))
    login_user(user, remember=True)
    return redirect(url_for('chat.home'))
Beispiel #8
0
    def forge(message):
        """Generate fake data."""
        import random
        from sqlalchemy.exc import IntegrityError

        from faker import Faker

        fake = Faker()

        click.echo('Initializing the database...')
        db.drop_all()
        db.create_all()

        click.echo('Forging the data...')
        admin = User(nickname='Grey Li', email='*****@*****.**')
        admin.set_password('helloflask')
        db.session.add(admin)
        db.session.commit()

        click.echo('Generating users...')
        for i in range(2, 52):
            user = User(id=i,
                        nickname=fake.name(),
                        bio=fake.sentence(),
                        github=fake.url(),
                        website=fake.url(),
                        email=fake.email())
            db.session.add(user)
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()

        click.echo("creating a room containing these users.")
        room = Room(id=1,
                    name="Family - All",
                    cur_user_total=0,
                    user_total=0,
                    owner=admin.id)
        for i in range(2, 52):
            user_room = UserRoom(
                user_id=i,
                room_id=1,
            )
            db.session.add(user_room)
            room.user_total += 1
        db.session.add(room)

        db.session.commit()

        click.echo('Generating messages...')
        for i in range(message):
            message = Message(author=User.query.get(
                random.randint(1, User.query.count())),
                              body=fake.sentence(),
                              timestamp=fake.date_time_between('-30d', '-2d'),
                              room_id=1)
            db.session.add(message)

        db.session.commit()
        click.echo('Done.')
Beispiel #9
0
 def setUp(self) -> None:
     super(AuthTestCase, self).setUp()
     user = User(email='*****@*****.**')
     db.session.add(user)
     db.session.commit()
Beispiel #10
0
 def setUp(self):
     super(AdminTestCase, self).setUp()
     admin = User(email='*****@*****.**', nickname='Admin User')
     admin.set_password('123')
     db.session.add(admin)
     db.session.commit()