Example #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()
Example #2
0
    def test_delete_message(self):
        self.login(email='*****@*****.**', password='******')
        response = self.client.delete(url_for('chat.delete_message', message_id=1))
        self.assertEqual(response.status_code, 204)

        response = self.client.get(url_for('chat.home'))
        data = response.get_data(as_text=True)
        self.assertNotIn('Test Message 1', data)
        self.assertIn('Test Message 2', data)
        self.assertIn('Test Message 3', data)

        user = User.query.get(1)
        message4 = Message(body='Test Message 4')
        user.messages.append(message4)
        db.session.commit()

        response = self.client.delete(url_for('chat.delete_message', message_id=4))
        self.assertEqual(response.status_code, 204)

        response = self.client.get(url_for('chat.home'))
        data = response.get_data(as_text=True)
        self.assertNotIn('Test Message 4', data)
        self.assertIn('Test Message 2', data)
        self.assertIn('Test Message 3', data)

        self.logout()
        self.login()

        response = self.client.delete(url_for('chat.delete_message', message_id=2))
        self.assertEqual(response.status_code, 403)
Example #3
0
def new_message(message_body):
    message = Message(author=current_user._get_current_object(),
                      body=message_body)
    db.session.add(message)
    db.session.commit()
    emit('new message', {'message_html': render_template('', message=message)},
         broadcast=True)
Example #4
0
def new_message(message_body):
    html_message = to_html(message_body)
    message = Message(author=current_user._get_current_object(), body=html_message, room_id=escape(session['room_id']))
    db.session.add(message)
    db.session.commit()
    emit('new message',
         {'message_html': render_template('chat/_message.html', message=message),
          'message_body': html_message,
          'gravatar': current_user.gravatar,
          'nickname': current_user.nickname,
          'user_id': current_user.id},
         broadcast=True)
Example #5
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()
Example #6
0
def test():
    for i in range(10):
        data = '服务端发来的测试数据'
        message = Message(author=current_user._get_current_object(), body=data)
        emit('new message', {
            'message_html':
            render_template('chat/_message.html', message=message),
            'message_body':
            data,
            'gravatar':
            current_user.gravatar,
            'nickname':
            current_user.nickname,
            'user_id':
            current_user.id
        },
             broadcast=True)
Example #7
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.')
Example #8
0
def new_message(message_body):
    # 将HTML传过来的格式数据包转为Python格式
    # html_message = to_html(message_body)
    html_message = '测试数据'
    # 将数据包存储在数据库
    print(current_user)
    message = Message(author=current_user._get_current_object(),
                      body=html_message)
    db.session.add(message)
    db.session.commit()
    # 发送数据包,message作为参数传入
    emit('new message', {
        'message_html': render_template('chat/_message.html', message=message),
        'message_body': html_message,
        'gravatar': current_user.gravatar,
        'nickname': current_user.nickname,
        'user_id': current_user.id
    },
         broadcast=True)
Example #9
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.')