forked from LinvernSS/afs-labs-student
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeaturetests.py
119 lines (87 loc) · 3.96 KB
/
featuretests.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
import unittest
import os
import sys
from server import app
from model import connect_to_db, db, example_data, Product
current = os.getcwd()
current = current.split("/")
current = "/".join(current[:-1])
sys.path.append(current)
class FlaskTests(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
app.config['TESTING'] = True
# Connect to test database
connect_to_db(app)
# Create tables and add sample data
db.create_all()
example_data()
self.cart = Product.query.filter(Product.product_id.in_([1])).all()
def tearDown(self):
db.session.close()
db.drop_all()
def test_register(self):
result = self.client.post("/register",
data={"first_name": "Jane",
"last_name": "Doe",
"email": "[email protected]",
"password": "123"},
follow_redirects=True)
self.assertIn(b"Registration successful!", result.data)
result = self.client.get('/register')
self.assertIn(b'register_img', result.data)
def test_login(self):
login = self.client.post("/login",
data={"email": "[email protected]", "password": "password"},
follow_redirects=True)
self.assertIn(b"Success", login.data)
logged_in = self.client.get("/products")
self.assertIn(b"Account", logged_in.data)
self.assertIn(b"Log Out", logged_in.data)
result = self.client.get('/account')
self.assertIn(b'Jane Doe', result.data)
logout = self.client.get("/logout", follow_redirects=True)
self.assertNotIn(b"Account", logout.data)
self.assertIn(b"Login", logout.data)
def test_frontpage(self):
result = self.client.get("/")
self.assertIn(b"Farm", result.data)
def test_all_products(self):
result = self.client.get("/products")
self.assertIn(b"Add to Cart</button>", result.data)
def test_product_page(self):
result = self.client.get("/products/1")
self.assertIn(b"Blackberries", result.data)
result = self.client.post('/products/1', data={'productId': 1}, follow_redirects=True)
self.assertIn(b'Blackberries', result.data)
def test_locations(self):
result = self.client.get("/locations")
self.assertIn(b'<div id="map">', result.data)
def test_search(self):
result = self.client.get("/search?terms=black")
self.assertIn(b"Organic Blackberries", result.data)
def test_cart_and_checkout(self):
result = self.client.post('/add-item', json={'product_id': 1}, follow_redirects=True)
self.assertIn(b'Success!', result.data)
result = self.client.post('/update-cart', json={'product_id': 1, 'qty': 3}, follow_redirects=True)
self.assertIn(b'Success', result.data)
result = self.client.get("/cart")
self.assertIn(b"Shopping Cart", result.data)
result = self.client.post('/cart',
json={'delivery': 'delivery',
'address': {'street': '1234 Main Street',
'zipcode': '31626'}},
follow_redirects=True)
self.assertIn(b'Success', result.data)
result = self.client.get('/checkout')
self.assertIn(b'Thank you', result.data)
self.client.post("/login",
data={"email": "[email protected]", "password": "password"},
follow_redirects=True)
result = self.client.post('/checkout')
self.assertIn(b'Your order has been placed!', result.data)
if __name__ == "__main__":
log_file = 'log_file.txt'
with open(log_file, "w") as f:
runner = unittest.TextTestRunner(f)
unittest.main(testRunner=runner)