-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
220 lines (155 loc) · 7.14 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import server
import unittest
from server import *
from helper_functions import *
from model import *
import os
from datetime import datetime
from pytz import timezone
import pytz
################################################################################
class ServerTestsWithSession(unittest.TestCase):
"""Tests for the cat texts site that require session"""
def setUp(self):
"""Code to run before every test."""
self.client = server.app.test_client()
server.app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'key'
connect_to_db(app, "postgresql:///testdb")
db.create_all()
example_data()
with self.client as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_update(self):
"""Can we reach the update cat info page?"""
result = self.client.get("/update", follow_redirects=True)
self.assertIn("Update Cat Info!", result.data)
def test_do_update(self):
"""Can we update some cat info?"""
result = self.client.post("/update", data={"cat-name": "Leoboi"},
follow_redirects=True)
self.assertIn("Successfully updated Leoboi's info!", result.data)
def test_logout(self):
"""Can we log out a user that's been logged in?"""
result = self.client.get("/logout", follow_redirects=True)
self.assertIn("Goodbye!", result.data)
def test_main_page(self):
"""Can we log in and reach the main page?"""
result = self.client.get("/main")
self.assertIn("Your cat's current info:", result.data)
self.assertIn('Hellboy', result.data)
self.assertIn('tuna', result.data)
self.assertIn('meowing', result.data)
def test_login_fail_pw(self):
"""Can we reject a user with the wrong password?"""
result = self.client.post("/login", data={"email": "[email protected]",
"password": "jfsghjkdfhgh"},
follow_redirects=True)
self.assertIn("Incorrect password.", result.data)
def test_login_fail_email(self):
"""Can we reject a user with the wrong email?"""
result = self.client.post("/login", data={"email": "[email protected]",
"password": "jfsghjkdfhgh"},
follow_redirects=True)
self.assertIn("Incorrect email.", result.data)
class ServerTestsWithoutSession(unittest.TestCase):
"""Tests for the cat texts site"""
def setUp(self):
"""Code to run before every test."""
connect_to_db(app, "postgresql:///testdb")
self.client = server.app.test_client()
server.app.config['TESTING'] = True
db.create_all()
example_data()
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_welcome_page(self):
"""Can we reach the welcome page?"""
result = self.client.get("/")
self.assertIn('Login or <a href="/register">Register</a>', result.data)
def test_registration_page(self):
"""Can we reach the registration page?"""
result = self.client.get("/register")
self.assertIn('Register for Cat Texts!', result.data)
def test_registration(self):
"""Can we register a new user and reach the thanks page?"""
import bcrypt
password='imcute'
password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
result = self.client.post("/register", data={"phone": "413-329-3187",
"email": "[email protected]",
"password": password,
"user_id": "1",
"cat-name": "Hellboy",
"dinner-time": "7:00",
"timezone": "US/Pacific",
"ampm": "pm",
"cat-snack": "tuna",
"cat-activity": "meowing",
"cat-activity2": "sleeping",
"cat-toy": "space ball",
"cat-toy2": "catnip carrot"
}, follow_redirects=True)
self.assertIn("/main", result.data)
self.assertIn("You're now signed up for Cat Texts!", result.data)
def test_login_success(self):
"""Can we log in a registered user?"""
result = self.client.post("/login", data={"email": "[email protected]",
"password": "hellboy",
}, follow_redirects=True)
self.assertIn("/", result.data)
self.assertIn("Successfully logged in!", result.data)
class HelperFunctionTexts(unittest.TestCase):
"""Tests all helper functions"""
def test_parse_time(self):
"""Can we parse time correctly?"""
time = "7:00"
assert parse_time(time) == [7, 0]
def test_make_hour(self):
"""Can we format hours properly for display?"""
hour = 5
assert make_hour(hour) == "05"
def test_make_minutes(self):
"""Can we format minutes properly for display?"""
minutes = "4"
assert make_minutes(minutes) == "04"
def test_make_24_hour_time(self):
"""Can we format the time correctly?"""
ampm = "pm"
hour = 7
assert make_24_hour_time(ampm, hour) == 19
def test_am_or_pm(self):
"""Is the time am or pm?"""
hour = 17
assert am_or_pm(hour) == "pm"
hour = 7
assert am_or_pm(hour) == "am"
def test_make_12_hour_time(self):
"""Can we convert 24 hour time to 12 hour time?"""
hour = 23
assert make_12_hour_time(hour) == 11
def test_convert_to_utc(self):
"""Can we convert user's local time to a datetime object in UTC?"""
hour = 3
minutes = 4
user_timezone = "US/Pacific"
# this is kind of a dubious test as I copied much of the code from the
# function itself. However, it needs to be a datetime object to verify
# that it's working, so I'm not sure if there is a better way to get
# around this
date = datetime.now()
date = date.replace(tzinfo=pytz.utc)
this_timezone = timezone(user_timezone)
date = date.astimezone(this_timezone)
date = date.replace(hour=hour, minute=minutes, second=0, microsecond=0)
date = date.astimezone(pytz.utc)
assert convert_to_utc(hour, minutes, user_timezone) == date
if __name__ == "__main__":
unittest.main()