-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathtests.py
92 lines (69 loc) · 2.4 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
import unittest
from flask_testing import TestCase
from fbone import create_app
from fbone.user import User
from fbone.config import TestConfig
from fbone.extensions import db
class BaseTestCase(TestCase):
def create_app(self):
app = create_app(TestConfig)
return app
def init_data(self):
demo = User(
name=u'demo',
email=u'[email protected]',
password=u'123456')
db.session.add(demo)
db.session.commit()
def setUp(self):
"""Reset all tables before testing."""
db.create_all()
self.init_data()
def tearDown(self):
"""Clean db session and drop all tables."""
db.session.remove()
db.drop_all()
def login(self, username, password):
data = {
'login': username,
'password': password,
}
response = self.client.post('/login', data=data, follow_redirects=True)
assert "Logged in" in response.data
return response
def _logout(self):
response = self.client.get('/logout')
self.assertRedirects(response, location='/')
def _test_get_request(self, endpoint, template=None):
response = self.client.get(endpoint)
self.assert_200(response)
if template:
self.assertTemplateUsed(name=template)
return response
class TestFrontend(BaseTestCase):
def test_show(self):
self._test_get_request('/', 'index.html')
def test_signup(self):
self._test_get_request('/signup', 'frontend/signup.html')
data = {
'email': '[email protected]',
'password': '123456',
'name': 'new_user',
'agree': True,
}
response = self.client.post('/signup', data=data, follow_redirects=True)
assert "Signed up" in response.data
new_user = User.query.filter_by(email=data['email']).first()
assert new_user.name == "new_user"
def test_login(self):
self._test_get_request('/login', 'frontend/login.html')
response = self.client.post('/login', data={
'login': "[email protected]",
'password': "123456"}, follow_redirects=True)
assert "Logged in" in response.data
def test_logout(self):
self.login("[email protected]", "123456")
self._logout()
if __name__ == '__main__':
unittest.main()