-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
368 lines (262 loc) · 12 KB
/
test.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from flask_testing import TestCase
from website import db
from website.auth import auth
from website.views import views
from website.models import User, Music
import unittest
from flask import Flask
from flask_login import LoginManager, current_user
from werkzeug.security import check_password_hash
class Test(TestCase):
SQLALCHEMY_DATABASE_URI = 'sqlite://test.db'
TESTING = True
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs'
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///test.db'
db.init_app(app)
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
return app
def setUp(self):
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
# Class for testing the User table in the application's database
class Test_User(Test):
# NOT A TEST. Function to create a user
def create_test_user(self):
user = User(email='[email protected]', first_name='Test', password='password')
db.session.add(user)
db.session.commit()
return user
# Test that user is correctly added to database. Attributes not passed as
# parameters should be defaults.
def test_add_user(self):
user = self.create_test_user()
assert user in db.session
assert user.email == '[email protected]'
assert user.first_name == 'Test'
assert user.password == 'password'
assert not user.is_admin
assert user.background_color == '#000000'
assert user.drum_color == '#e4a33a'
# Test function to change user's email
def test_set_email(self):
user = self.create_test_user()
user.set_email('[email protected]')
assert user.email == '[email protected]'
# Test function to change user's first name
def test_set_first_name(self):
user = self.create_test_user()
user.set_first_name('Emily')
assert user.first_name == 'Emily'
# Test function to change user's password
def test_set_password(self):
user = self.create_test_user()
user.set_password('steelDores')
assert user.password == 'steelDores'
# Test function to change user's admin status
def test_set_admin(self):
user = self.create_test_user()
user.set_is_admin(True)
assert user.is_admin
# Test function to change user's desired background color
def test_set_background(self):
user = self.create_test_user()
user.set_background('#ffffff')
assert user.background_color == '#ffffff'
# Test function to change user's desired drum color
def test_set_drum_color(self):
user = self.create_test_user()
user.set_drum_color('#47ffff')
assert user.drum_color == '#47ffff'
# Class for testing the Music table in the application's database
class Test_Music(Test):
# NOT A TEST. Function to create a music sample.
def create_test_music(self):
music = Music(title='Song Title')
db.session.add(music)
db.session.commit()
return music
# Test that music is correctly added to database. Attributes not passed as
# parameters should be defaults.
def test_add_music(self):
music = self.create_test_music()
assert music in db.session
assert music.title == 'Song Title'
assert music.composer == ''
assert music.genre == ''
assert music.description == ''
assert music.pdf_link == ''
assert music.audio_link == ''
assert music.user_id == None
# Test function to change music sample's title
def test_set_title(self):
music = self.create_test_music()
music.set_title('New Title')
assert music.title == 'New Title'
# Test function to change music sample's composer
def test_set_composer(self):
music = self.create_test_music()
music.set_composer('Mozart')
assert music.composer == 'Mozart'
# Test function to change music sample's genre
def test_set_genre(self):
music = self.create_test_music()
music.set_genre('Classical')
assert music.genre == 'Classical'
# Test function to change music sample's description
def test_set_description(self):
music = self.create_test_music()
music.set_description('A very nice tune.')
assert music.description == 'A very nice tune.'
# Test function to change music sample's PDF link
def test_set_pdf(self):
music = self.create_test_music()
music.set_pdf('https://musescore.com/classicman/fur-elise')
assert music.pdf_link == 'https://musescore.com/classicman/fur-elise'
# Test function to change music sample's PDF link
def test_set_audio(self):
music = self.create_test_music()
music.set_audio('https://www.youtube.com')
assert music.audio_link == 'https://www.youtube.com'
# Class for testing the web application's forms
class Test_Forms(Test):
def test_sign_up(self):
form = {'email':'[email protected]', 'firstName': 'Bill',
'password1': 'password', 'password2': 'password'}
with self.client:
response = self.client.post('/sign-up', data=form)
#assert response.request.path == '/menu'
# Check that a user has been created
assert db.session.query(User).count() == 1
# Check that added user has given attributes
user = User.query.filter_by(email=form['email']).first()
assert user.email == form['email']
assert user.first_name == form['firstName']
assert check_password_hash(user.password, form['password1'])
# Check that user is an admin
assert user.is_admin
# Check that user is logged in after signing up
assert current_user.is_authenticated
# Test that a new user cannot be created with an email that already exists
with self.assertRaises(Exception) as context:
self.client.post('/sign-up', data=form)
# Check that no new users have been created
assert db.session.query(User).count() == 1
# Test that email must be greater than 3 characters
form['email'] = 'no'
with self.assertRaises(Exception) as context:
self.client.post('/sign-up', data=form)
assert db.session.query(User).count() == 1
# Test that first name must be greater than 1 character
form['email'] = '[email protected]'
form['firstName'] = 'B'
with self.assertRaises(Exception) as context:
self.client.post('/sign-up', data=form)
assert db.session.query(User).count() == 1
# Test that passwords must match
form['firstName'] = 'Bill'
form['password2'] = 'steeldrum'
with self.assertRaises(Exception) as context:
self.client.post('/sign-up', data=form)
assert db.session.query(User).count() == 1
# Test that password must be at least 7 characters
form['password1'] = 'tester'
form['password2'] = 'tester'
with self.assertRaises(Exception) as context:
self.client.post('/sign-up', data=form)
assert db.session.query(User).count() == 1
def test_login(self):
# Create a user in database
form_user = {'email':'[email protected]', 'firstName': 'Bill',
'password1': 'password', 'password2': 'password'}
self.client.post('/sign-up', data=form_user)
self.client.get('/logout')
with self.client:
# Test that user is not logged in when password is not valid
form = {'email': '[email protected]', 'password': 'incorrect'}
with self.assertRaises(Exception) as context:
self.client.post('/sign-up', data=form)
assert not current_user.is_authenticated
# Test that user is not logged in if email does not exist
form['email'] = '[email protected]'
with self.assertRaises(Exception) as context:
self.client.post('/sign-up', data=form)
assert not current_user.is_authenticated
# Test that user is logged in when entering valid info
form['email'] = '[email protected]'
form['password'] = 'password'
self.client.post('/login', data=form)
assert current_user.is_authenticated
assert current_user.email == form['email']
assert check_password_hash(current_user.password, form['password'])
def test_logout(self):
with self.client:
form_user = {'email':'[email protected]', 'firstName': 'Bill',
'password1': 'password', 'password2': 'password'}
self.client.post('/sign-up', data=form_user)
assert current_user.is_authenticated
# Test that user is logged out correctly
self.client.get('/logout')
assert not current_user.is_authenticated
def test_settings(self):
# Create a user in database
form_user = {'email':'[email protected]', 'firstName': 'Bill',
'password1': 'password', 'password2': 'password'}
self.client.post('/sign-up', data=form_user)
user = User.query.one()
form = {'background':'#ffffff', 'drum-color': '#000000',
'new-password': 'new password'}
self.client.post('/settings', data=form)
# Check that user settings were changed
assert user.background_color == '#ffffff'
assert user.drum_color == '#000000'
assert check_password_hash(user.password, form['new-password'])
# Check that password is not changed if nothing is written in field
form['new-password'] = None
self.client.post('/settings', data=form)
assert check_password_hash(user.password, 'new password')
# Check that password is not changed if new password is less than 7 characters
form['new-password'] = 'tester'
with self.assertRaises(Exception) as context:
self.client.post('/settings', data=form)
assert check_password_hash(user.password, 'new password')
def test_add_music_form(self):
# Create a user in database
form_user = {'email':'[email protected]', 'firstName': 'Bill',
'password1': 'password', 'password2': 'password'}
self.client.post('/sign-up', data=form_user)
# Info for new music sample
form = {'title':'Fur Elise', 'composer':'Beethoven', 'genre':'Classical',
'description':'A nice tune.',
'pdf_link': 'https://musescore.com/classicman/fur-elise',
'audio_link': 'https://www.youtube.com'}
self.client.post('/add-music', data=form)
# Check that a row has been added to the Music table
assert db.session.query(Music).count() == 1
sample = Music.query.one()
# Check that added sample has given attributes
assert sample.title == form['title']
assert sample.composer == form['composer']
assert sample.genre == form['genre']
assert sample.description == form['description']
assert sample.pdf_link == form['pdf_link']
assert sample.audio_link == form['audio_link']
# Check that title must be entered
form['title'] = ''
with self.assertRaises(Exception) as context:
self.client.post('/add-music', data=form)
# Check that no new samples have been added
assert db.session.query(User).count() == 1
if __name__ == '__main__':
unittest.main()