-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.py
69 lines (48 loc) · 2.05 KB
/
test_server.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
"""Test suite for mental health exercises app."""
import server
import unittest
from flask import session
from model import connect_to_db
class HallOfMirrorsIntegrationTestCase(unittest.TestCase):
"""Integration tests: testing Flask server."""
def setUp(self):
"""To do before every test."""
print("(setUp ran)")
self.client = server.app.test_client()
# The `test_client` method technically comes from Werkzeug, a library
# that Flask uses
server.app.config['TESTING'] = True
def tearDown(self):
"""To do after every test."""
print("(tearDown ran)")
return
def test_homepage(self):
"""Test for expected HTML in homepage."""
result = self.client.get('/')
self.assertIn(b'<h2 id="signin">Log In</h2>', result.data)
# `result.data` is the response string (html), returned in
# bytestring format (`b'`)
class FlaskTestsLoggedIn(unittest.TestCase):
"""Flask tests with user logged in to session."""
def setUp(self):
"""To do before every test."""
server.app.config['TESTING'] = True
server.app.config['SECRET_KEY'] = 'key'
self.client = server.app.test_client()
# Start each test with a user ID stored in the session.
with self.client as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
# Is 'user_id' the only key in session? Yes
connect_to_db(server.app)
def test_important_page(self):
"""Test important page."""
result = self.client.get("/users/my_exercises", follow_redirects=True)
self.assertIn(b"Here are all of the exercises you have completed and your past responses:", result.data)
def test_create_exercise_page(self):
"""Test page for logged in user to author an exercise."""
result = self.client.get("/create")
self.assertIn(b"I want my exercise to have more prompts!", result.data)
if __name__ == '__main__':
# If called like a script, run tests
unittest.main()