-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest.py
142 lines (113 loc) · 4.93 KB
/
test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
from application import app
from server import user_datastore
from database import db
import unittest
import tempfile
import re
import json
from flask_security.utils import encrypt_password
from flask_security import current_user
from flask_security.utils import login_user
from models import User, Role, SomeStuff
class FlaskTestCase(unittest.TestCase):
def setUp(self):
app.config.from_object('config.TestingConfig')
self.client = app.test_client()
db.init_app(app)
with app.app_context():
db.create_all()
user_datastore.create_user(email='test', password=encrypt_password('test'))
db.session.commit()
def tearDown(self):
with app.app_context():
db.session.remove()
db.drop_all()
def _post(self, route, data=None, content_type=None, follow_redirects=True, headers=None):
content_type = content_type or 'application/x-www-form-urlencoded'
return self.client.post(route, data=data, follow_redirects=follow_redirects, content_type=content_type,
headers=headers)
def _login(self, email=None, password=None):
# Get CSRF token from login form
csrf_token = ''
rv = self.client.get('/login')
matches = re.findall('name="csrf_token".*?value="(.*?)"', rv.data.decode())
if matches:
csrf_token = matches[0]
# POST login form
email = email or 'test'
password = password or 'test'
data = {
'email': email,
'password': password,
'remember': 'y',
'csrf_token': csrf_token
}
return self._post('/login', data=data, follow_redirects=False)
class ModelsTest(FlaskTestCase):
def test_protectedstuff(self):
with app.app_context():
instance = SomeStuff(data1=1337, data2='Test')
db.session.add(instance)
db.session.commit()
self.assertTrue(hasattr(instance, 'id'))
class ViewsTest(FlaskTestCase):
def test_page(self):
rv = self.client.get('/')
self.assertEqual(200, rv.status_code)
def test_protected_page(self):
rv = self.client.get('/mypage')
self.assertIn('Redirecting...', rv.data.decode())
self._login()
rv = self.client.get('/mypage')
self.assertIn('It works', rv.data.decode())
rv = self.client.get('/logout')
self.assertEqual(302, rv.status_code)
class APITest(FlaskTestCase):
def _auth(self, username=None, password=None):
username = username or 'test'
password = password or 'test'
rv = self._post('/api/v1/auth',
data=json.dumps({'username': username, 'password': password})
)
return json.loads(rv.data.decode())
def _get(self, route, data=None, content_type=None, follow_redirects=True, headers=None):
content_type = content_type or 'application/json'
if hasattr(self, 'token'):
headers = headers or {'Authorization': 'JWT ' + self.token}
return self.client.get(route, data=data, follow_redirects=follow_redirects, content_type=content_type,
headers=headers)
def _post(self, route, data=None, content_type=None, follow_redirects=True, headers=None):
content_type = content_type or 'application/json'
if hasattr(self, 'token'):
headers = headers or {'Authorization': 'Bearer ' + self.token}
return self.client.post(route, data=data, follow_redirects=follow_redirects, content_type=content_type,
headers=headers)
def test_auth(self):
# Get auth token with invalid credentials
auth_resp = self._auth('not', 'existing')
self.assertEqual(401, auth_resp['status_code'])
# Get auth token with valid credentials
auth_resp = self._auth('test', 'test')
self.assertIn(u'access_token', auth_resp)
self.token = auth_resp['access_token']
# Get empty collection
rv = self._get('/api/v1/protected_stuff')
self.assertEqual(200, rv.status_code)
data = json.loads(rv.data.decode())
self.assertEqual(data['num_results'], 0)
# Post object to collection
rv = self._post('/api/v1/protected_stuff', data=json.dumps({'data1': 1337, 'data2': 'Test'}))
self.assertEqual(201, rv.status_code)
# Get collection if new object
rv = self._get('/api/v1/protected_stuff')
data = json.loads(rv.data.decode())
self.assertEqual(data['num_results'], 1)
# Post another object and get it back
rv = self._post('/api/v1/protected_stuff', data=json.dumps({'data1': 2, 'data2': ''}))
self.assertEqual(201, rv.status_code)
rv = self._get('/api/v1/protected_stuff/2')
data = json.loads(rv.data.decode())
self.assertEqual(data['data1'], 2)
if __name__ == '__main__':
unittest.main()