-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
134 lines (111 loc) · 4.75 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
import unittest
from base64 import b64encode
from app import app
from models import Todo, User
from peewee import *
from playhouse.test_utils import test_database
# creating a disposable database
db = SqliteDatabase(':memory:')
db.connect()
db.create_tables([User, Todo], safe=True)
# provides basic authorization
headers = {
'Authorization': 'Basic ' + b64encode('user_one:password1'.
encode()).decode()
}
class InheritTestClient(unittest.TestCase):
"""Will be used as an inheritor for other classes"""
def setUp(self):
"""enables to play with database"""
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
self.app = app.test_client()
class HomepageView(InheritTestClient):
"""Tests the '/' view"""
def test_h1_appears(self):
"""tests that the homepage shows, with its <h1>"""
html = self.app.get('/')
self.assertIn('My TODOs!', html.get_data(as_text=True))
class UserModelTest(unittest.TestCase):
"""tests if can create a User instance, while blocking duplicates"""
def test_create_user_instance(self):
with test_database(db, (User,)):
User.create_user(
username='user_one',
email='user_one@gmail.com',
password='password1')
self.assertEqual(
User.select().get().username, 'user_one')
with self.assertRaises(Exception):
User.create_user(
username='user_one',
email='user_one@gmail.com',
password='password1')
class TodoModelTest(unittest.TestCase):
"""tests that we can create a Todo instance"""
def test_create_todo_instance(self):
with test_database(db, (Todo,)):
Todo.create(
name="todo1")
self.assertEqual(
Todo.select().get().name, 'todo1')
class ResourcesTodosTestWithoutUser(InheritTestClient):
"""tests that we can't create a Todo if not logged in"""
def test_todolist_cannot_receive_post_without_user(self):
with test_database(db, (Todo,)):
self.app.post('/api/v1/todos', data={'name': 'todo1'})
self.assertFalse(Todo.select().count())
class ResourcesTodosTestWithUser(InheritTestClient):
"""tests all of TodoList and Todo apis when logged in"""
def test_todolist_can_receive_post_with_user(self):
with test_database(db, (Todo, User)):
User.create_user(
username='user_one',
email='user_one@gmail.com',
password='password1')
# a todo is created through post on TodoList api
self.app.post(
'/api/v1/todos', headers=headers, data={'name': 'todo1'})
self.assertEqual(Todo.select().count(), 1)
self.assertEqual(
Todo.select().get().name, 'todo1')
# that todo is changed through 'PUT' on the Todo api
self.app.put(
'/api/v1/todos/1', headers=headers,
data={'name': 'todo1updated'})
self.assertEqual(
Todo.select().get().name, 'todo1updated')
# testing that the 'GET' works on TodoList api
html = self.app.get('/api/v1/todos')
self.assertIn('todo1updated', html.get_data(as_text=True))
# testing that the 'GET' works on the Todo api
html = self.app.get('/api/v1/todos/1')
self.assertIn('todo1updated', html.get_data(as_text=True))
# testing that the 'DELETE' works on the Todo api
self.app.delete(
'/api/v1/todos/1', headers=headers,
data={'name': 'todo1updated'})
self.assertFalse(Todo.select().count())
class ResourcesUsersTest(InheritTestClient):
# tests that we can create a user,
# but only if password verifcation matches
def test_user_create_fail(self):
"""verify_password doesn't match, should fail"""
with test_database(db, (User,)):
self.app.post('/api/v1/users', data={
'username': 'user_one',
'password': 'password1',
'verify_password': 'password2',
'email': 'user_one@gmail.com'})
self.assertFalse(User.select().count())
def test_user_create_success(self):
"""verify_password matches. Should succeed."""
with test_database(db, (User,)):
self.app.post('/api/v1/users', data={
'username': 'user_one',
'password': 'password1',
'verify_password': 'password1',
'email': 'user_one@gmail.com'})
self.assertTrue(User.select().count())
if __name__ == '__main__':
unittest.main()