Ejemplo n.º 1
0
 def test_detail_post(self):
     post = Post()
     post.title = 'this is a test'
     post.text = 'test ' * 20
     db.session.add(post)
     db.session.commit()
     response = self.client.post('/post/1/detail')
     self.assertEqual(response.status_code, 302)
     user = User()
     user.username = '******'
     user.password = '******'
     db.session.add(user)
     db.session.commit()
     self.client.post('/login',
                      data={
                          'username': '******',
                          'password': '******'
                      })
     response = self.client.post('/post/1/detail',
                                 data={'comment': ''},
                                 follow_redirects=True)
     self.assertEqual(response.status_code, 403)
     self.assertTrue('评论不能为空' == response.get_data(as_text=True))
     response = self.client.post('/post/1/detail',
                                 data={'comment': 'comment for test'},
                                 follow_redirects=True)
     self.assertEqual(response.status_code, 200)
     self.assertTrue('comment for test' in response.get_data(as_text=True))
Ejemplo n.º 2
0
 def test_post_error(self):
     user = User()
     user.username = '******'
     user.email = '*****@*****.**'
     user.password = '******'
     db.session.add(user)
     db.session.commit()
     response = self.client.post('/login',
                                 data={
                                     'username': '******',
                                     'password': '******'
                                 },
                                 follow_redirects=True)
     self.assertEqual(response.status_code, 404)
Ejemplo n.º 3
0
 def test_login_post_email(self):
     user = User()
     user.username = '******'
     user.password = '******'
     user.email = '*****@*****.**'
     db.session.add(user)
     db.session.commit()
     response = self.client.post('/login',
                                 data={
                                     'username': '******',
                                     'password': '******'
                                 },
                                 follow_redirects=True)
     self.assertEqual(response.status_code, 200)
     self.assertTrue('BUGLAN的个人小站' in response.get_data(as_text=True))
Ejemplo n.º 4
0
 def test_logout_get(self):
     response = self.client.get('/logout')
     self.assertEqual(response.status_code, 302)
     self.assertTrue('BUGLAN的个人小站' not in response.get_data(as_text=True))
     user = User()
     user.username = '******'
     user.password = '******'
     db.session.add(user)
     db.session.commit()
     self.client.post('/login',
                      data={
                          'username': '******',
                          'password': '******'
                      })
     response = self.client.get('/logout', follow_redirects=True)
     self.assertEqual(response.status_code, 200)
     self.assertTrue('BUGLAN的个人小站' in response.get_data(as_text=True))
Ejemplo n.º 5
0
 def test_register_post_error(self):
     response = self.client.post('/register',
                                 data={
                                     'username': '******',
                                     'email': '*****@*****.**',
                                     'password1': '12345',
                                     'password2': '123456'
                                 })
     self.assertEqual(response.status_code, 403)
     user = User()
     user.username = '******'
     db.session.add(user)
     db.session.commit()
     response = self.client.post('/register',
                                 data={
                                     'username': '******',
                                     'email': '*****@*****.**',
                                     'password1': '123456',
                                     'password2': '123456'
                                 })
     self.assertEqual(response.status_code, 400)
     self.assertTrue(response.get_data(as_text=True) == '密码或用户名已被他人所占用')
Ejemplo n.º 6
0
 def test_my_posts_with_login(self):
     user = User()
     user.username = '******'
     user.password = '******'
     user.email = '*****@*****.**'
     db.session.add(user)
     db.session.commit()
     post = Post()
     post.title = 'this is a test'
     post.text = 'test test' * 20
     post.users = user
     db.session.add(post)
     db.session.commit()
     self.client.post('/login',
                      data={
                          'username': '******',
                          'password': '******'
                      },
                      follow_redirects=True)
     response = self.client.get('/my_posts')
     self.assertEqual(response.status_code, 200)
     self.assertTrue('this is a test' in response.get_data(as_text=True))
Ejemplo n.º 7
0
def register():
    if request.method == 'POST':
        user = User()
        if not register_filter(
                request.form.get('username'), request.form.get('email'),
                request.form.get('password1'), request.form.get('password2')):
            abort(403)
        try:
            user.username = request.form.get('username')
            user.email = request.form.get('email')
            user.password = request.form.get('password1')
            user.publish_date = datetime.datetime.now()
            user.modified_date = datetime.datetime.now()
            db.session.add(user)
            db.session.commit()
        except sqlalchemy.exc.IntegrityError:
            logger.error("注册失败-> 密码或用户名已被他人所占用")
            return '密码或用户名已被他人所占用', 400
        else:
            login_user(user)
            logger.info('用户 "{}" 注册成功'.format(user.username))
            return redirect(url_for('main.index'))
    return render_template('blog/register.html')