-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
166 lines (113 loc) · 4.69 KB
/
tests.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
import unittest
from server import app
from model import db, User, GameMaster, Player, Game, GameInfo, init_app, connect_to_db, test_data
################################################################################
# class StorytimeTestsDatabase(unittest.TestCase):
# """Flask tests that use the database."""
# def setUp(self):
# """Setup before every test."""
# self.client = app.test_client()
# app.config['TESTING'] = True
# # Connect to test database
# connect_to_db(app, "postgresql:///testdb")
# # Create tables and add sample data
# db.create_all()
# test_data()
# def tearDown(self):
# """Close out testing environment."""
# db.session.close()
# db.drop_all()
# db.engine.dispose()
################################################################################
class Homepage(unittest.TestCase):
"""Tests for home page."""
def setUp(self):
self.client = app.test_client()
app.config['TESTING'] = True
# Connect to test database
connect_to_db(app, "postgresql:///testdb")
# Create tables and add sample data
db.create_all()
test_data()
def test_homepage(self):
result = self.client.get("/")
self.assertEqual(result.status_code, 200)
self.assertIn(b"Register", result.data)
def test_register(self):
result = self.client.get("/register")
self.assertIn(b"Registration Form", result.data)
def test_login(self):
result = self.client.get("/login")
self.assertIn(b"Welcome Back!", result.data)
self.assertNotIn(b"Register", result.data)
def tearDown(self):
"""Close out testing environment."""
db.session.close()
db.drop_all()
db.engine.dispose()
################################################################################
class Registration(unittest.TestCase):
"""Tests for registration."""
def setUp(self):
self.client = app.test_client()
app.config['TESTING'] = True
# Connect to test database
connect_to_db(app, "postgresql:///testdb")
# Create tables and add sample data
db.create_all()
test_data()
def test_registered(self):
result = self.client.post("/process-registration",
data={"user_fname": "Ele",
"user_lname": "Phant",
"username": "elephant",
"user_email": "elephant@gmail.com",
"user_password": "elephant",
"security": "pet",
"security_answer": "Azula"},
follow_redirects=True)
result = self.client.get("/main-page")
self.assertIn(b"Available Games", result.data)
self.assertIn(b"elephant", result.data)
def tearDown(self):
"""Close out testing environment."""
db.session.close()
db.drop_all()
db.engine.dispose()
################################################################################
class TestsLoggedIn(unittest.TestCase):
"""Tests on pages that require user log-in."""
def setUp(self):
"""Setup, including starting user session."""
self.client = app.test_client()
app.config['TESTING'] = True
# Connect to test database
connect_to_db(app, "postgresql:///testdb")
# Create tables and add sample data
db.create_all()
test_data()
with self.client as c:
with c.session_transaction() as session:
session["active_session"] = 1
def test_logged_in(self):
result = self.client.post("/login",
data={"email": "benneb",
"password": "6700869c8ff7480e34a70a708b028700dbaa3a033b5652b903afe89f49a31456"},
follow_redirects=True)
result = self.client.get("/main-page")
self.assertIn(b"Available Games", result.data)
self.assertIn(b"benneb", result.data)
def test_mainpage(self):
"""Tests for main page."""
result = self.client.get("/main-page")
self.assertIn(b"Games", result.data)
def test_gamepage(self):
result = self.client.get("/game-page?game_name=Cats%20Attack")
self.assertIn(b"Game Page", result.data)
def tearDown(self):
"""Close out testing environment."""
db.session.close()
db.drop_all()
db.engine.dispose()
if __name__ == "__main__":
unittest.main()