-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_app.py
198 lines (119 loc) · 8.77 KB
/
test_app.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
198
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import setup_db, Restaurant, Reservations, MenuItems
class FsndCapstoneTestCase(unittest.TestCase):
def setUp(self):
"""Define test variables and initialize app."""
self.app = create_app()
self.client = self.app.test_client
# Test database name
self.database_name = "fsndcapstone_test"
self.database_path = "postgresql://postgres:viktor@localhost:5432/" + self.database_name
setup_db(self.app, self.database_path)
# binds the app to the current context
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
# create all tables
self.db.create_all()
# Test variables
# TOKENS
self.manager = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtwNE8walZuX2pjT0pJbTRJSFBlRyJ9.eyJpc3MiOiJodHRwczovL2ZzbmQtaGFwcHlob3VyLmF1dGgwLmNvbS8iLCJzdWIiOiJnb29nbGUtb2F1dGgyfDEwNzk0MjE1NzM5OTc0NTk0NTM1MiIsImF1ZCI6WyJmc25kY2Fwc3RvbmUiLCJodHRwczovL2ZzbmQtaGFwcHlob3VyLmF1dGgwLmNvbS91c2VyaW5mbyJdLCJpYXQiO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CBwcm9maWxlIGVtYWlsIiwicGVybWlzc2lvbnMiOlsiZ2V0OmxvZ2luX3Jlc3VsdHMiLCJnZXQ6bWVudV9pdGVtIiwiZ2V0OnJlc3RhdXJhbnQiLCJwb3N0OnJlc2VydmF0aW9uIiwicmVhZDp5b3Vyc2VsZiJdfQ.abCpTLTu0w3U6DeBCtlWOarT7FpY8caSKsp6kzuyGSf1QN8KoBkPrg8MDl5_UcWZ3hbqK7vvBZulEKggZmJ5nUlVLVu60-0s3oS3UrJD3z1uKDZm6ZAPEPOHJ7xS3-TQeHq_P844Lk2aW25LXQoSAJjSnX2wIcyqdl0s4gwRJGUJoHCUU0EvHO96GHKq32miy75PrNNTe-xp9Rq5iRcdrbPtQoveFdgSSU4MgoEKyO516KYmo3x_kZLxGEphsWpBM0y1TJGqQczCtYfJmPzaHyl0Ia_4rOq8RpWLRvE01uH7FqdCgUzffh-M7SoQy3CKpMrLuWGnxPqZnp9G2BirUA'
self.badtoken = 'badtoken'
# New Restaurant
self.new_restaurant = {
'name': 'Earls',
'address': '5555 Street rd'
}
#Patch Restaurant
self.change_restaurant_address = {
'address': '1111 Street rd'
}
# Post New Reservations
self.reservation_info = {
'time_of_res': "2020-08-19 18:30:00",
'num_of_people': 5,
'name_for_res': "Reservations Name"
}
#Add new restaurant if database is empty with good token and set current_rest_id
if Restaurant.query.filter(Restaurant.owner_id != 'badtoken').count() == 0:
res = self.client().post('/restaurants', headers={"Authorization": "Bearer {}".format(self.manager)}, json=self.new_restaurant)
self.current_rest_id = Restaurant.query.order_by(Restaurant.id.desc()).filter(Restaurant.owner_id != 'badtoken').first().id
# add new restaurant with bad token is it doesnt exist, and set bad_rest_id
if Restaurant.query.filter(Restaurant.owner_id == 'badtoken').count() == 0:
bad_token_rest = Restaurant(name="Bad name", address="bad address", owner_id="badtoken")
bad_token_rest.insert()
self.bad_rest_id = Restaurant.query.filter(Restaurant.owner_id == 'badtoken').first().id
def tearDown(self):
"""Executed after each test"""
pass
# TESTS START HERE
def test_homepage(self):
res = self.client().get('/')
self.assertEqual(res.status_code, 200)
def test_post_restaurant_with_valid_token(self):
res = self.client().post('/restaurants', headers={"Authorization": "Bearer {}".format(self.manager)}, json=self.new_restaurant)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
def test_401_post_restaurant_with_invalid_token(self):
res = self.client().post('/restaurants', headers={"Authorization": "Bearer {}".format(self.badtoken)}, json=self.new_restaurant)
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
self.assertEqual(data['success'], False)
def test_get_restaurants(self):
res = self.client().get('/restaurants')
data = json.loads(res.data)
restaurants = Restaurant.query.order_by(Restaurant.id).all()
output = [restaurant.format() for restaurant in restaurants]
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['restaurants'], output)
def test_get_restaurant_by_id(self):
res = self.client().get('/restaurants/' + str(self.current_rest_id))
data = json.loads(res.data)
restaurant = Restaurant.query.get(self.current_rest_id)
format_rest = restaurant.format()
self.assertEqual(res.status_code, 200)
self.assertEqual(data['restaurants'], format_rest)
def test_patch_restaurant_by_id(self):
res = self.client().patch('/restaurants/' + str(self.current_rest_id), headers={"Authorization": "Bearer {}".format(self.manager)}, json=self.change_restaurant_address)
data = json.loads(res.data)
restaurant = Restaurant.query.get(self.current_rest_id)
format_rest = restaurant.format()
self.assertEqual(res.status_code, 200)
self.assertEqual(data['updated_restaurant']['address'], self.change_restaurant_address['address'])
def test_patch_restaurant_that_is_not_owners(self):
res = self.client().patch('/restaurants/' + str(self.bad_rest_id), headers={"Authorization": "Bearer {}".format(self.manager)}, json=self.change_restaurant_address)
self.assertEqual(res.status_code, 401)
def test_patch_restaurant_that_does_not_exist(self):
res = self.client().patch('/restaurants/' + str(self.current_rest_id + 1000), headers={"Authorization": "Bearer {}".format(self.manager)}, json=self.change_restaurant_address)
self.assertEqual(res.status_code, 422)
def test_post_reservation_with_vaild_customer(self):
res = self.client().post('/restaurants/' + str(self.current_rest_id) + '/reservation', headers={"Authorization": "Bearer {}".format(self.customer)}, json=self.reservation_info)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
def test_post_reservation_with_invaild_customer(self):
res = self.client().post('/restaurants/' + str(self.current_rest_id) + '/reservation', headers={"Authorization": "Bearer {}".format(self.badtoken)}, json=self.reservation_info)
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
def test_post_reservation_with_non_existant_restaurant(self):
res = self.client().post('/restaurants/' + str(self.current_rest_id + 1000) + '/reservation', headers={"Authorization": "Bearer {}".format(self.customer)}, json=self.reservation_info)
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
def test_delete_restaurant_with_valid_owner(self):
res = self.client().delete('/restaurants/' + str(self.current_rest_id), headers={"Authorization": "Bearer {}".format(self.manager)})
deleted_rest = Restaurant.query.get(self.current_rest_id)
self.assertEqual(res.status_code, 200)
self.assertEqual(deleted_rest, None)
def test_delete_non_existant_restaurant(self):
res = self.client().delete('/restaurants/' + str((self.current_rest_id + 10000)), headers={"Authorization": "Bearer {}".format(self.manager)})
deleted_rest = Restaurant.query.get(self.current_rest_id + 10000)
self.assertEqual(res.status_code, 422)
self.assertEqual(deleted_rest, None)
if __name__ == "__main__":
unittest.main()