-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
260 lines (212 loc) · 7.12 KB
/
Copy pathtest_app.py
File metadata and controls
260 lines (212 loc) · 7.12 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import pytest
import app as main
from database import db
from models import User
@pytest.fixture
def app():
app = main.create_app()
app.debug = True
# disabling CSRF protection for testing purposes
app.config['WTF_CSRF_ENABLED'] = False
client = app.test_client()
ctx = app.app_context()
ctx.push()
yield client # magic happens here
ctx.pop()
@pytest.fixture
def init_database():
# create db
db.create_all()
# let's make two users
user1 = User(username='test1_user', phone='8675309')
user2 = User(username='test2_user', phone='1111111')
# let's make an admin
admin = User(username='admin', phone='12345678901')
# set passwords
user1.set_password('8675309', 'wouldnt-you-like-to-know')
user2.set_password('1111111', 'deep-state-secrets')
admin.set_password('12345678901', 'Administrator@1')
# add to db
db.session.add(user1)
db.session.add(user2)
db.session.add(admin)
# Commit the changes for the users
db.session.commit()
yield db # magic happens here
db.drop_all()
def test_login_page(app):
res = app.get("/login")
assert res.status_code == 200
def test_registration_page(app):
res = app.get("/register")
assert res.status_code == 200
def test_valid_registration(app, init_database):
username = "new_user"
phone = "2222222"
password = "super-complex-password"
res = app.post("/register", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
def test_invalid_registration(app, init_database):
# this test fails because of a duplicate user
username = "test1_user"
phone = "8675309"
password = "wouldnt-you-like-to-know"
res = app.post("/register", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'failure' in res.data
assert res.status_code == 200
def test_valid_login(app, init_database):
# authenticate a registered user successfully
username = "test1_user"
phone = "8675309"
password = "wouldnt-you-like-to-know"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
def test_invalid_2fa_login(app, init_database):
# correct username and password, but wrong 2fa device
username = "test2_user"
phone = "3333333"
password = "deep-state-secrets"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'Incorrect' in res.data
assert res.status_code == 200
def test_spellcheck_functionality(app, init_database):
# authenticate a registered user successfully
username = "test1_user"
phone = "8675309"
password = "wouldnt-you-like-to-know"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
print(res)
content = "Take a sad sogn and make it better. Remember to let her under your skyn, then you begin to make it betta."
res = app.post("/spell_check", data=dict(
content=content
), follow_redirects=False)
assert b'sogn, skyn, betta' in res.data
assert res.status_code == 200
def test_cors_headers(app):
res = app.options("/login")
print(res)
def test_admin_login(app, init_database):
username = "admin"
phone = "12345678901"
password = "Administrator@1"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
return
def test_user_history_query(app, init_database):
username = "test1_user"
phone = "8675309"
password = "wouldnt-you-like-to-know"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
print(res)
content = "Take a sad sogn and make it better. Remember to let her under your skyn, then you begin to make it betta."
res = app.post("/spell_check", data=dict(
content=content
), follow_redirects=False)
assert b'sogn, skyn, betta' in res.data
assert res.status_code == 200
res = app.get("/history")
assert b'Take a sad sogn and make it better. Remember to let her under your skyn, then you begin to make it betta.' in res.data
return
def test_admin_history_query(app, init_database):
# create content
username = "test1_user"
phone = "8675309"
password = "wouldnt-you-like-to-know"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
print(res)
content = "Take a sad sogn and make it better. Remember to let her under your skyn, then you begin to make it betta."
res = app.post("/spell_check", data=dict(
content=content
), follow_redirects=False)
assert b'sogn, skyn, betta' in res.data
assert res.status_code == 200
# log in admin
username = "admin"
phone = "12345678901"
password = "Administrator@1"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
# query user
res = app.get("/history/query1")
assert b'Take a sad sogn and make it better. Remember to let her under your skyn, then you begin to make it betta.' in res.data
return
def test_unauthorized_view(app, init_database):
# create content
username = "test1_user"
phone = "8675309"
password = "wouldnt-you-like-to-know"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
print(res)
content = "Take a sad sogn and make it better. Remember to let her under your skyn, then you begin to make it betta."
res = app.post("/spell_check", data=dict(
content=content
), follow_redirects=False)
assert b'sogn, skyn, betta' in res.data
assert res.status_code == 200
# log in second user
username = "test2_user"
phone = "1111111"
password = "deep-state-secrets"
res = app.post("/login", data=dict(
username=username,
phone=phone,
password=password
), follow_redirects=False)
assert b'success' in res.data
assert res.status_code == 200
# query user
res = app.get("/history/query1")
assert b'Take a sad sogn and make it better. Remember to let her under your skyn, then you begin to make it betta.' not in res.data
return