-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
173 lines (153 loc) · 6.23 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'''
These Test classes test the business logic of users and recipe
views and models.
'''
import unittest
import re
from flask_pymongo import PyMongo
import app as app_module
app = app_module.app
# Setting up test DB on Mongo and switching CSRF checks off
app.config["TESTING"] = True
app.config["WTF_CSRF_ENABLED"] = False
app.config['MONGO_URI'] = 'mongodb://localhost:27017/recipeGlutTesting'
mongo = PyMongo(app)
app_module.mongo = mongo
class AppTestCase(unittest.TestCase):
"""Clean the DB"""
def setUp(self):
self.client = app.test_client()
with app.app_context():
mongo.db.users.delete_many({})
mongo.db.recipes.delete_many({})
class AppTests(AppTestCase):
"""Test Home page loading"""
def test_index(self):
res = self.client.get('/')
data = res.data.decode('utf-8')
assert res.status == '200 OK'
assert 'A Glut of Recipes' in data
def test_recipes(self):
"""Test recipe list page loading"""
res = self.client.get('/recipes')
data = res.data.decode('utf-8')
assert res.status == '200 OK'
assert 'features' in data
def test_register_mismatch_passwords(self):
"""Check mismatched passwords on the registration form, expecting mismatch message"""
res = self.client.post('/register', data=dict(
username='fred',
password='joijqwdoijqwoid',
password2='qoijwdoiqwjdoiqwd',
email='[email protected]',
))
data = res.data.decode('utf-8')
assert 'Passwords must match' in data
def test_register_duplicate_username(self):
"""Check entering a username that is already used returns username is already taken message"""
res = self.client.post('/register', follow_redirects=True, data=dict(
username='fred',
password='asdfasdfasdf',
password2='asdfasdfasdf',
email='[email protected]',
))
data = res.data.decode('utf-8')
assert 'A Glut of Recipes' in data
res = self.client.post('/register', follow_redirects=True, data=dict(
username='fred',
password='asdfasdfasdf',
password2='asdfasdfasdf',
email='[email protected]',
))
data = res.data.decode('utf-8')
assert res.status == '200 OK'
assert 'that username is already taken' in data
def test_register_successful(self):
"""Check valid registration redirects to index page"""
res = self.client.post('/register', follow_redirects=True, data=dict(
username='freddie',
password='asdfasdfasdf',
password2='asdfasdfasdf',
email='[email protected]',
))
data = res.data.decode('utf-8')
assert res.status == '200 OK'
assert 'A Glut of Recipes' in data
class LoggedInTests(AppTestCase):
"""Separate class to clean DB with no cross referencing"""
def setUp(self):
"""
Clean the DB, add new user and recipe and check user and new recipe
shows on home after redirect
"""
super().setUp()
res = self.client.post('/register', follow_redirects=True, data=dict(
username='fred3',
password='asdfasdfasdf',
password2='asdfasdfasdf',
email='[email protected]',
))
res = self.client.post('/create_recipe', follow_redirects=True, data={
'title': 'Mac and cheese',
'short_description': 'Get this mac and cheese',
'ingredients': '8 spring onions',
'method': 'Put all the ingredients',
'tags': 'cheese, slow',
'image': 'some image link'
})
data = res.data.decode('utf-8')
assert 'fred3' in data
assert 'Mac and cheese'
def test_create_recipe(self):
"""Create recipe and check new recipe shows after redirect"""
res = self.client.post('/create_recipe', follow_redirects=True, data={
'title': 'Slow - cooker vegan bean chilli',
'short_description': 'Get this vegan',
'ingredients': '8 spring onions',
'method': 'Put all the ingredients',
'tags': 'vegan, slow',
'image': 'some image link'
})
data = res.data.decode('utf-8')
assert 'vegan' in data
def test_recipe_page(self):
"""Find Recipe and go to it's recipe page"""
res = self.client.get('/recipes')
# use regular expression to find Object id of recipe
ids = re.findall(r'href="/recipe/(\w+)"', res.data.decode("utf8"))
# check we have something
assert len(ids) > 0
# to go that recipe page using extracted id
res = self.client.get('/recipe/{}'.format(ids[0]))
data = res.data.decode('utf-8')
assert res.status == '200 OK'
assert 'Mac and cheese' in data
def test_edit_recipe(self):
"""Edit recipe and check redirect to home page"""
res = self.client.get('/recipes')
ids = re.findall(r'href="/recipe/(\w+)"', res.data.decode("utf8"))
assert len(ids) > 0
res = self.client.get('/edit_recipe/{}'.format(ids[0]))
data = res.data.decode('utf-8')
assert res.status == '200 OK'
assert 'Mac and cheese' in data
res = self.client.post('/edit_recipe/'.format(ids[0]), follow_redirects=True, data={
'title': 'Mac and cheese',
'short_description': 'Get this maccy and cheese',
'ingredients': '8 blocks of cheddar',
'method': 'Put all the ingredients',
'tags': 'cheese, slow',
'image': 'some image link'
})
assert res.status == '200 OK'
def test_delete_recipe(self):
"""Delete recipe and check recipe is not present after redirect"""
res = self.client.get('/recipes')
# use regular expression to find Object id of recipe
ids = re.findall(r'href="/recipe/(\w+)"', res.data.decode("utf-8"))
assert len(ids) > 0
# togo that delete recipe page using extracted id
res = self.client.post('/delete_recipe/{}'.format(ids[0]), follow_redirects=True)
data = res.data.decode('utf-8')
assert res.status == '200 OK'
assert 'Mac and cheese' not in data