-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_routes.py
115 lines (78 loc) · 3.34 KB
/
test_routes.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
from unittest import TestCase
from server import app
from model import connect_to_db
class FlaskTests(TestCase):
def setUp(self):
"""Stuff to do before every test."""
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'mypaleta'
self.client = app.test_client()
with self.client as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
# Connect only to the demo db
connect_to_db(app, 'postgresql:///paleta_test')
def test_homepage_route(self):
""" Test homepage route rendering """
result = self.client.get('/')
self.assertEqual(result.status_code, 200)
self.assertIn('<h1>Welcome to Paleta', result.data)
def test_gallery_route(self):
""" Test gallery route rendering """
result = self.client.get('/gallery')
self.assertEqual(result.status_code, 200)
self.assertIn('<h2>Paleta Gallery</h2>', result.data)
def test_filter_route(self):
""" Test profile route rendering """
result = self.client.get('/image_filter')
self.assertEqual(result.status_code, 200)
self.assertIn('<p>Enter a hex color:', result.data)
def test_login(self):
result = self.client.post("/login",
data={"email": "[email protected]", "password": "jahlela"},
follow_redirects=True)
self.assertIn("Successfully logged in!", result.data)
def test_logout(self):
result = self.client.get("/logout", follow_redirects=True)
self.assertIn("Successfully logged out!", result.data)
class FlaskTestsLoggedOut(TestCase):
"""Flask tests with user logged in to session."""
def setUp(self):
"""Stuff to do before every test."""
app.config['TESTING'] = True
self.client = app.test_client()
def test_navbar_logged_in(self):
""" Test that user can't see logout when logged out """
result = self.client.get("/", follow_redirects=True)
self.assertNotIn("Logout</a>", result.data)
self.assertIn("Login</a>", result.data)
class FlaskTestsLoggedIn(TestCase):
"""Flask tests with user logged in to session."""
def setUp(self):
"""Stuff to do before every test."""
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'mypaleta'
self.client = app.test_client()
with self.client as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
sess['logged_in'] = True
def test_navbar_logged_in(self):
""" Test that user can't see login when logged in """
result = self.client.get("/", follow_redirects=True)
self.assertNotIn("Login</a>", result.data)
self.assertIn("Logout</a>", result.data)
def test_profile_route(self):
""" Test profile route rendering """
result = self.client.get('/users/1')
self.assertEqual(result.status_code, 200)
self.assertIn('Profile</h2>', result.data)
def tearDown(self):
""" Do after each tests """
with self.client as c:
with c.session_transaction() as sess:
sess['user_id'] = None
sess['logged_in'] = False
if __name__ == "__main__":
import unittest
unittest.main()