-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_views.py
197 lines (134 loc) · 6.62 KB
/
test_user_views.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""User View tests"""
# FLASK_ENV=production python -m unittest test_user_views.py
import os
from unittest import TestCase
from models import db, connect_db, User, Groceries, Favorites
# from bs4 import BeautifulSoup
os.environ['DATABASE_URL'] = "postgresql:///mygrub_test"
# import app
from app import app, CURR_USER_KEY
db.create_all()
# so forms ignore CSRF
app.config['WTF_CSRF_ENABLED'] = False
class MessageViewTestCase(TestCase):
"""TEst views for messages"""
def setUp(self):
"""Create test client, and sample data"""
db.drop_all()
db.create_all()
self.client = app.test_client()
self.testuser = User.signup(username="testuser",
email="[email protected]",
password="testuser")
self.testuser_id = 8989
self.testuser.id = self.testuser_id
db.session.commit()
def tearDown(self):
resp = super().tearDown()
db.session.rollback()
return resp
def test_add_groceries(self):
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser_id
resp = c.post("/groceries/add/1001", follow_redirects=True)
self.assertEqual(resp.status_code, 200)
groc = Groceries.query.filter(Groceries.user_id == self.testuser_id).all()
self.assertEqual(len(groc), 1)
self.assertEqual(groc[0].user_id, self.testuser_id)
self.assertEqual(groc[0].name, 'butter')
def test_add_favorites(self):
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser_id
resp = c.post("/recipes/654812/favorite", follow_redirects=True)
self.assertEqual(resp.status_code, 200)
favs = Favorites.query.filter(Favorites.user_id == self.testuser_id).all()
self.assertEqual(len(favs), 1)
self.assertEqual(favs[0].user_id, self.testuser_id)
self.assertEqual(favs[0].recipe_id, 654812)
self.assertEqual(favs[0].name, 'Pasta and Seafood')
self.assertIsNotNone(favs[0].img_url)
def test_unauthorized_user_page_access(self):
# self.setup_followers()
with self.client as c:
resp = c.get(f"/user/{self.testuser_id}", follow_redirects=True)
self.assertEqual(resp.status_code, 200)
self.assertNotIn("testuser", str(resp.data))
self.assertIn("Access unauthorized", str(resp.data))
########################
# Setup for future test#
########################
def setup_groceries(self):
g1 = Groceries(user_id = self.testuser_id, ingredient_id = 5062, name = 'chicken breasts')
g2 = Groceries(user_id = self.testuser_id, ingredient_id = 2009, name = 'chilli powder')
g3 = Groceries(user_id = self.testuser_id, ingredient_id = 2047, name = 'table salt')
db.session.add_all([g1, g2, g3])
db.session.commit()
def setup_favorites(self):
f1 = Favorites(user_id = self.testuser_id, recipe_id = 654812, name = 'Pasta and Seafood', img_url = 'https://spoonacular.com/recipeImages/654812-556x370.jpg')
f2 = Favorites(user_id = self.testuser_id, recipe_id = 662151, name = 'Sugar Cookies', img_url = 'https://spoonacular.com/recipeImages/662151-556x370.jpg')
f3 = Favorites(user_id = self.testuser_id, recipe_id = 637876, name = 'Chicken 65', img_url = 'https://spoonacular.com/recipeImages/637876-556x370.jpg')
f4 = Favorites(user_id = self.testuser_id, recipe_id = 555555, name = 'Tester', img_url = 'https://spoonacular.com/recipeImages/637876-556x370.jpg')
db.session.add_all([f1, f2, f3])
db.session.commit()
#########################
def test_users_show(self):
self.setup_groceries()
self.setup_favorites()
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser_id
resp = c.get(f"/user/{self.testuser_id}")
self.assertEqual(resp.status_code, 200)
self.assertIn("testuser", str(resp.data))
# for amount of favorites
self.assertIn("4", str(resp.data))
# for amount of groceries
self.assertIn("3", str(resp.data))
def test_show_groceries(self):
self.setup_groceries()
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser_id
resp = c.get('/user/groceries')
self.assertEqual(resp.status_code, 200)
self.assertIn('testuser', str(resp.data))
self.assertIn('chicken breasts', str(resp.data))
self.assertIn('table salt', str(resp.data))
self.assertNotIn('pepper', str(resp.data))
def test_remove_grocery(self):
self.setup_groceries()
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser_id
resp1 = c.post('/groceries/remove/5062')
self.assertEqual(resp1.status_code, 302)
resp2 = c.get('/user/groceries')
self.assertEqual(resp2.status_code, 200)
self.assertNotIn('chicken breasts', str(resp2.data))
self.assertIn('table salt', str(resp2.data))
self.assertIn('chilli powder', str(resp2.data))
def test_remove_all_groceries(self):
self.setup_groceries()
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser_id
resp1 = c.post('/groceries/remove/all')
self.assertEqual(resp1.status_code, 200)
resp2 = c.get('/user/groceries')
self.assertEqual(resp2.status_code, 200)
self.assertNotIn('chicken breasts', str(resp2.data))
self.assertNotIn('table salt', str(resp2.data))
self.assertNotIn('chilli powder', str(resp2.data))
def test_show_favorites(self):
self.setup_favorites()
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser_id
resp = c.get('/user/favorites')
self.assertEqual(resp.status_code, 200)
self.assertIn('favorites', str(resp.data))
self.assertIn('Pasta and Seafood', str(resp.data))
self.assertIn('Chicken 65', str(resp.data))
self.assertIn('https://spoonacular.com/recipeImages/662151-556x370.jpg', str(resp.data))