Skip to content

Commit

Permalink
Unit test with unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli committed Aug 26, 2018
1 parent 8063d41 commit 3d6bbd9
Show file tree
Hide file tree
Showing 9 changed files with 594 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
@@ -0,0 +1,3 @@
[run]
source = bluelog
branch = true
4 changes: 4 additions & 0 deletions .flake8
@@ -0,0 +1,4 @@
[flake8]
exclude = .git,*migrations*
max-line-length = 119
max-complexity = 7
Empty file added tests/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions tests/base.py
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
"""
:author: Grey Li (李辉)
:url: http://greyli.com
:copyright: © 2018 Grey Li <withlihui@gmail.com>
:license: MIT, see LICENSE for more details.
"""
import unittest

from flask import url_for

from bluelog import create_app
from bluelog.extensions import db
from bluelog.models import Admin


class BaseTestCase(unittest.TestCase):

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 = Admin(name='Grey Li', username='grey', about='I am test', blog_title='Testlog', blog_sub_title='a test')
user.set_password('123')
db.session.add(user)
db.session.commit()

def tearDown(self):
db.drop_all()
self.context.pop()

def login(self, username=None, password=None):
if username is None and password is None:
username = 'grey'
password = '123'

return self.client.post(url_for('auth.login'), data=dict(
username=username,
password=password
), follow_redirects=True)

def logout(self):
return self.client.get(url_for('auth.logout'), follow_redirects=True)
275 changes: 275 additions & 0 deletions tests/test_admin.py
@@ -0,0 +1,275 @@
# -*- coding: utf-8 -*-
"""
:author: Grey Li (李辉)
:url: http://greyli.com
:copyright: © 2018 Grey Li <withlihui@gmail.com>
:license: MIT, see LICENSE for more details.
"""
from flask import url_for

from bluelog.models import Post, Category, Link, Comment
from bluelog.extensions import db

from tests.base import BaseTestCase


class AdminTestCase(BaseTestCase):

def setUp(self):
super(AdminTestCase, self).setUp()
self.login()

category = Category(name='Default')
post = Post(title='Hello', category=category, body='Blah...')
comment = Comment(body='A comment', post=post, from_admin=True)
link = Link(name='GitHub', url='https://github.com/greyli')
db.session.add_all([category, post, comment, link])
db.session.commit()

def test_new_post(self):
response = self.client.get(url_for('admin.new_post'))
data = response.get_data(as_text=True)
self.assertIn('New Post', data)

response = self.client.post(url_for('admin.new_post'), data=dict(
title='Something',
category=1,
body='Hello, world.'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Post created.', data)
self.assertIn('Something', data)
self.assertIn('Hello, world.', data)

def test_edit_post(self):
response = self.client.get(url_for('admin.edit_post', post_id=1))
data = response.get_data(as_text=True)
self.assertIn('Edit Post', data)
self.assertIn('Hello', data)
self.assertIn('Blah...', data)

response = self.client.post(url_for('admin.edit_post', post_id=1), data=dict(
title='Something Edited',
category=1,
body='New post body.'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Post updated.', data)
self.assertIn('New post body.', data)
self.assertNotIn('Blah...', data)

def test_delete_post(self):
response = self.client.get(url_for('admin.delete_post', post_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Post deleted.', data)
self.assertIn('405 Method Not Allowed', data)

response = self.client.post(url_for('admin.delete_post', post_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Post deleted.', data)

def test_delete_comment(self):
response = self.client.get(url_for('admin.delete_comment', comment_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Comment deleted.', data)
self.assertIn('405 Method Not Allowed', data)

response = self.client.post(url_for('admin.delete_comment', comment_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Comment deleted.', data)
self.assertNotIn('A comment', data)

def test_enable_comment(self):
post = Post.query.get(1)
post.can_comment = False
db.session.commit()

response = self.client.post(url_for('admin.set_comment', post_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Comment enabled.', data)

response = self.client.post(url_for('blog.show_post', post_id=1))
data = response.get_data(as_text=True)
self.assertIn('<div id="comment-form">', data)

def test_disable_comment(self):
response = self.client.post(url_for('admin.set_comment', post_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Comment disabled.', data)

response = self.client.post(url_for('blog.show_post', post_id=1))
data = response.get_data(as_text=True)
self.assertNotIn('<div id="comment-form">', data)

def test_approve_comment(self):
self.logout()
response = self.client.post(url_for('blog.show_post', post_id=1), data=dict(
author='Guest',
email='a@b.com',
site='http://greyli.com',
body='I am a guest comment.',
post=Post.query.get(1),
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Thanks, your comment will be published after reviewed.', data)
self.assertNotIn('I am a guest comment.', data)

self.login()
response = self.client.post(url_for('admin.approve_comment', comment_id=2), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Comment published.', data)

response = self.client.post(url_for('blog.show_post', post_id=1))
data = response.get_data(as_text=True)
self.assertIn('I am a guest comment.', data)

def test_new_category(self):
response = self.client.get(url_for('admin.new_category'))
data = response.get_data(as_text=True)
self.assertIn('New Category', data)

response = self.client.post(url_for('admin.new_category'), data=dict(name='Tech'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Category created.', data)
self.assertIn('Tech', data)

response = self.client.post(url_for('admin.new_category'), data=dict(name='Tech'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Name already in use.', data)

category = Category.query.get(1)
post = Post(title='Post Title', category=category)
db.session.add(post)
db.session.commit()
response = self.client.get(url_for('blog.show_category', category_id=1))
data = response.get_data(as_text=True)
self.assertIn('Post Title', data)

def test_edit_category(self):
response = self.client.post(url_for('admin.edit_category', category_id=1),
data=dict(name='Default edited'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Category updated.', data)
self.assertIn('Default', data)
self.assertNotIn('Default edited', data)
self.assertIn('You can not edit the default category', data)

response = self.client.post(url_for('admin.new_category'), data=dict(name='Tech'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Category created.', data)
self.assertIn('Tech', data)

response = self.client.get(url_for('admin.edit_category', category_id=2))
data = response.get_data(as_text=True)
self.assertIn('Edit Category', data)
self.assertIn('Tech', data)

response = self.client.post(url_for('admin.edit_category', category_id=2),
data=dict(name='Life'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Category updated.', data)
self.assertIn('Life', data)
self.assertNotIn('Tech', data)

def test_delete_category(self):
category = Category(name='Tech')
post = Post(title='test', category=category)
db.session.add(category)
db.session.add(post)
db.session.commit()

response = self.client.get(url_for('admin.delete_category', category_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Category deleted.', data)
self.assertIn('405 Method Not Allowed', data)

response = self.client.post(url_for('admin.delete_category', category_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('You can not delete the default category.', data)
self.assertNotIn('Category deleted.', data)
self.assertIn('Default', data)

response = self.client.post(url_for('admin.delete_category', category_id=2), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Category deleted.', data)
self.assertIn('Default', data)
self.assertNotIn('Tech', data)

def test_new_link(self):
response = self.client.get(url_for('admin.new_link'))
data = response.get_data(as_text=True)
self.assertIn('New Link', data)

response = self.client.post(url_for('admin.new_link'), data=dict(
name='HelloFlask',
url='http://helloflask.com'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Link created.', data)
self.assertIn('HelloFlask', data)

def test_edit_link(self):
response = self.client.get(url_for('admin.edit_link', link_id=1))
data = response.get_data(as_text=True)
self.assertIn('Edit Link', data)
self.assertIn('GitHub', data)
self.assertIn('https://github.com/greyli', data)

response = self.client.post(url_for('admin.edit_link', link_id=1), data=dict(
name='Github',
url='https://github.com/helloflask'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Link updated.', data)
self.assertIn('https://github.com/helloflask', data)

def test_delete_link(self):
response = self.client.get(url_for('admin.delete_link', link_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Link deleted.', data)
self.assertIn('405 Method Not Allowed', data)

response = self.client.post(url_for('admin.delete_link', link_id=1), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Link deleted.', data)

def test_manage_post_page(self):
response = self.client.get(url_for('admin.manage_post'))
data = response.get_data(as_text=True)
self.assertIn('Manage Posts', data)

def test_manage_comment_page(self):
response = self.client.get(url_for('admin.manage_comment'))
data = response.get_data(as_text=True)
self.assertIn('Manage Comments', data)

def test_manage_category_page(self):
response = self.client.get(url_for('admin.manage_category'))
data = response.get_data(as_text=True)
self.assertIn('Manage Categories', data)

def test_manage_link_page(self):
response = self.client.get(url_for('admin.manage_link'))
data = response.get_data(as_text=True)
self.assertIn('Manage Links', data)

def test_blog_setting(self):
response = self.client.post(url_for('admin.settings'), data=dict(
name='Grey Li',
blog_title='My Blog',
blog_sub_title='Just some raw ideas.',
bio='I am ...',
about='Example about page',
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Setting updated.', data)
self.assertIn('My Blog', data)

response = self.client.get(url_for('admin.settings'))
data = response.get_data(as_text=True)
self.assertIn('Grey Li', data)
self.assertIn('My Blog', data)

response = self.client.get(url_for('blog.about'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Example about page', data)
34 changes: 34 additions & 0 deletions tests/test_auth.py
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""
:author: Grey Li (李辉)
:url: http://greyli.com
:copyright: © 2018 Grey Li <withlihui@gmail.com>
:license: MIT, see LICENSE for more details.
"""
from flask import url_for

from tests.base import BaseTestCase


class AuthTestCase(BaseTestCase):

def test_login_user(self):
response = self.login()
data = response.get_data(as_text=True)
self.assertIn('Welcome back.', data)

def test_fail_login(self):
response = self.login(username='wrong-username', password='wrong-password')
data = response.get_data(as_text=True)
self.assertIn('Invalid username or password.', data)

def test_logout_user(self):
self.login()
response = self.logout()
data = response.get_data(as_text=True)
self.assertIn('Logout success.', data)

def test_login_protect(self):
response = self.client.get(url_for('admin.settings'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Please log in to access this page.', data)
25 changes: 25 additions & 0 deletions tests/test_basic.py
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
:author: Grey Li (李辉)
:url: http://greyli.com
:copyright: © 2018 Grey Li <withlihui@gmail.com>
:license: MIT, see LICENSE for more details.
"""
from flask import current_app

from tests.base import BaseTestCase


class BasicTestCase(BaseTestCase):

def test_app_exist(self):
self.assertFalse(current_app is None)

def test_app_is_testing(self):
self.assertTrue(current_app.config['TESTING'])

def test_404_error(self):
response = self.client.get('/foo')
data = response.get_data(as_text=True)
self.assertEqual(response.status_code, 404)
self.assertIn('404 Error', data)

0 comments on commit 3d6bbd9

Please sign in to comment.