-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
95 lines (86 loc) · 3.55 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
from project import app, db, bcrypt
from project.models import User
from flask_testing import TestCase
from flask import json
import unittest
import jwt
def authenticate(username, password):
user = User.query.filter(User.username == username).first()
if bcrypt.check_password_hash(user.password, password):
token = jwt.encode({'id': user.id}, 'secret', algorithm='HS256').decode('utf-8')
return token
class BaseTestCase(TestCase):
def create_app(self):
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///testing.db'
return app
def setUp(self):
db.create_all()
user1 = User(email='[email protected]', username='jrob', image_url='#', password='pass1')
user2 = User(email='[email protected]', username='mundefined', image_url='http://static.djbooth.net/pics-features/jaden-smith.jpg', password='willow')
user3 = User(email='[email protected]', username='aa-ron', image_url='https://pbs.twimg.com/profile_images/805534047002705925/TDWxrUN8_400x400.jpg', password='iheartsleep')
db.session.add_all([user1, user2, user3])
db.session.commit()
def tearDown(self):
db.drop_all()
def _login_user(self):
return self.client.post('/api/users/auth', content_type='application/json',
data=json.dumps({
'username': 'jrob',
'password': 'pass1'
})).json
def test_index(self):
user_auth = self._login_user()
response = self.client.get('/api/users',
headers={
'Authorization':'bearer ' + user_auth['token'],
'Content-Type':'application/json'
})
expect_json = [{
'id': 1,
'username': 'jrob',
'image_url': '#',
'email': '[email protected]',
'messages': {'id': 0, 'message': None, 'created': None}
}, {
'id': 2,
'username': 'mundefined',
'image_url': 'http://static.djbooth.net/pics-features/jaden-smith.jpg',
'email': '[email protected]',
'messages': {'id': 0, 'message': None, 'created': None}
}, {
'id': 3,
'username': 'aa-ron',
'image_url': 'https://pbs.twimg.com/profile_images/805534047002705925/TDWxrUN8_400x400.jpg',
'email': '[email protected]',
'messages': {'id': 0, 'message': None, 'created': None}
}]
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, expect_json)
def test_signup(self):
response = self.client.post('/api/users', content_type='application/json',
data=json.dumps({
'username': 'michelle',
'password': 'branch',
'email': '[email protected]',
'image_url': None
}))
expect_json = {
'id': 4,
'username': 'michelle',
'email': '[email protected]',
'image_url': None,
'messages': {'id': 0, 'message': None, 'created': None}
}
self.assertEqual(response.status_code, 200)
self.assertEqual(str(response.json), str(expect_json))
# def test_auth(self):
# response = self.client.post('api/users/auth', content_type='application/json',
# data=json.dumps({
# 'username': 'jrob',
# 'password': 'pass1'
# }))
# expect_json = {authenticate('jrob', 'pass1')}
# self.assertEqual(response.status_code, 200)
# self.assertEqual(response.json, {'token'})
if __name__ == '__main__':
unittest.main()