-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_app.py
292 lines (243 loc) · 9.87 KB
/
test_app.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
Testing all aspects of the application
"""
from flask_mail import Message
# Database configuration is imported first prior to
# all other configurations
import os
os.environ['DATABASE_URL'] = 'sqlite://' # Use in-memory db, denoted by two forward slashes
# Email Support
os.environ['MAIL_SERVER'] = 'smtp.gmail.com'
os.environ['MAIL_USERNAME'] = '[email protected]'
os.environ['MAIL_PASSWORD'] = 'testparent'
os.environ['ADMINS'] = '[email protected]'
from app import app, db
from app.email import send_async_email, send_email
import unittest
from app.models import Parent, Student,Teacher, Admin, User, Email
from datetime import datetime, timedelta
from time import time
import jwt
from app import mail
from threading import Thread
class TestElearningApp(unittest.TestCase):
def setUp(self):
self.app = app
# Disable csrf protection
self.app.config['SECRET_KEY'] = 'testKEY'
self.app.config['WTF_CSRF_ENABLED'] = False
# Application context
self.appctx = self.app.app_context()
self.appctx.push()
db.create_all() # < --- create database during setup
self.add_parent_to_db() # < --- populate parent db
self.client = self.app.test_client() # < --- test client
def tearDown(self):
db.drop_all() # < --- discard database after each test
self.appctx.pop()
self.app = None
self.appctx = None
self.client = None
def test_elearning_app(self):
assert self.app is not None
assert app == self.app
def test_home_page_access(self):
''''''
response = self.client.get('/')
response_home = self.client.get('/home')
assert response.status_code == 200
assert response_home.status_code == 200
# =====================
# User
# =====================
def test_password_hashing(self):
user = User(username='testuser')
user.set_password('testuser2023')
assert user.check_password('muthoni') == False
assert user.check_password('testuser2023') == True
def test_avatar(self):
user = User(username='testuser', email='[email protected]')
assert user.avatar(36) == ('https://www.gravatar.com/avatar/'
'04678e8bacf37f21ebfbcdddefad9468'
'?d=identicon&s=36')
def test_reset_password_token(self):
"""Test generation of password reset token"""
user = User(username='testuser', email='[email protected]')
assert jwt.encode({'reset_password': user.email},'secret', algorithm='HS256') == \
('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXNldF9wYXNzd29yZCI6InRlc'
'3R1c2VyQGVtYWlsLmNvbSJ9.t__XarhVUwMSwekw_QsoipBREdvLjcl7kCwqwKlfnB8')
def test_verify_password_reset_token(self, expires_in=600):
"""Verify the token generated"""
user = User(username='testuser', email='[email protected]')
token = jwt.encode(
{'reset_password': user.email, 'exp': time() + expires_in},
'secret',
algorithm='HS256')
token = jwt.encode({'reset_password': '[email protected]'},'secret',algorithm='HS256')
email = jwt.decode(token, 'secret', algorithms=['HS256'])['reset_password']
assert email == '[email protected]'
# def sending_email_to_user(self, user):
# self.send_email(
# subject='Test Email',
# sender=app.config['MAIL_USERNAME'],
# recipients=[user.email],
# text_body='This is a test',
# html_body='<p>This is a test</p>')
# =====================
# End of user testing
# =====================
# =====================
# Parent
# =====================
def add_parent_to_db(self):
parent = Parent(
first_name='Test',
last_name='Parent',
username='testparent',
email='[email protected]',
phone_number='+254700111222',
current_residence='Roselyn, Nairobi'
)
parent.set_password('testparent2023')
db.session.add(parent)
db.session.commit()
def parent_login(self):
self.client.post('/login', data={
'username': 'testparent',
'password': 'testparent2023'
})
def test_parent_registration_form(self):
response = self.client.get('/register')
assert response.status_code == 200
html = response.get_data(as_text=True)
# All these fields must be included
assert 'name="first_name"' in html
assert 'name="last_name"' in html
assert 'name="username"' in html
assert 'name="email"' in html
assert 'name="password"' in html
assert 'name="confirm_password"' in html
assert 'name="phone_number"' in html
assert 'name="residence"' in html
assert 'name="register"' in html
def test_mismatched_passwords_during_parent_registration(self):
response = self.client.post('/register', data={
'first_name': 'test',
'last_name': 'parent',
'username': 'testparent',
'email': '[email protected]',
'password': 'testuser2023',
'confirm_password': 'testuser2023',
'phone_number': '+254700111222',
'residence': 'Roselyn, Nairobi'
})
assert response.status_code == 200
html = response.get_data(as_text=True)
assert 'Field must be equal to confirm_password' in html
def test_parent_registration(self):
response = self.client.post('/register/parent', data={
'first_name': 'test',
'last_name': 'parent',
'username': 'testparent',
'email': '[email protected]',
'password': 'testparent2023',
'confirm_password': 'testparent2023',
'phone_number': '+254700111222',
'current_residence': 'Nairobi',
}, follow_redirects=True)
assert response.status_code == 200
assert response.request.path == '/login' # <--- redirected to the login page
def test_parent_login(self):
response = self.client.post('/login', data={
'username': 'testparent',
'password': 'testparent2023'
}, follow_redirects=True)
assert response.status_code == 200
assert response.request.path == '/parent/profile'
html = response.get_data(as_text=True)
assert 'Hi, testparent!' in html
def test_parent_register_child(self):
self.parent_login()
response = self.client.post('/register/student', data={
'first_name': 'test',
'last_name': 'student',
'username': 'teststudent',
'email': '[email protected]',
'password': 'teststudent2023',
'confirm_password': 'teststudent2023',
'phone_number': '+254700111222',
'school': 'Roselyn Academy',
'age': 10,
'coding_experience': 'None',
'program': 'Introduction to Web Development',
'cohort': 1,
'program_schedule': 'Crash'
}, follow_redirects=True)
assert response.status_code == 200
html = response.get_data(as_text=True)
assert 'Student successfully registered' in html
assert response.request.path == '/parent/profile'
def test_parent_payment(self):
pass
def test_parent_deactivate_account(self):
parent = Parent(username='testparent', email='[email protected]')
response = self.client.get('/parent/deactivate-account', follow_redirects=True)
assert response.status_code == 200
assert response.request.path == '/parent/profile'
# self.sending_email_to_user(parent)
def test_parent_writing_email_to_support(self):
self.parent_login()
response = self.client.post('/contact-support', data={
'subject': 'Test Subject',
'body': 'Test email'
}, follow_redirects=True)
assert response.status_code == 200
html = response.get_data(as_text=True)
assert 'Email saved' in html
assert response.request.path == '/engagment-history'
response = self.client.get('/email/send', data={
'subject': 'I Need Help',
'body': 'I am new on this platform'
},follow_redirects=True)
assert response.status_code == 200
html = response.get_data(as_text=True)
assert 'I Need Help' in html
assert response.request.path == '/engagment-history'
def test_parent_delete_written_email(self):
self.parent_login()
# Add email to db
parent = Parent(username='testparent')
email = Email(subject='I Need Help', body='I am new on this platform', author=parent)
db.session.add(email)
db.session.commit()
# Deleting email from db
response = self.client.get('email/delete', follow_redirects=True)
email1 = Email.query.filter_by(subject='I Need Help').first_or_404()
db.session.delete(email1)
db.session.commit()
assert response.status_code == 200
assert email1 is None
assert response.request.path == '/engagment-history'
html = response.get_data(as_text=True)
assert 'Email deleted' in html
# =====================
# End of parent testing
# =====================
# =====================
# Student
# =====================
# =====================
# End of student testing
# =====================
# =====================
# Teacher
# =====================
# =====================
# End of teacher testing
# =====================
# =====================
# Admin
# =====================
# =====================
# End of admin testing
# =====================