This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
forked from ArvisP/Rubix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·603 lines (522 loc) · 22.4 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
from app import app, db
from flask import request
from werkzeug.security import check_password_hash
import os.path as op
import unittest
import datetime
import os
from flask_testing import TestCase
from flask_login import current_user
from app.models import User, Competition, Announcement, Event, EventUserLink
from bs4 import BeautifulSoup
class BaseTestCase(TestCase):
'''
Initializes the app
'''
def create_app(self):
app.config.from_object('config.TestConfig')
return app
'''
Creates the database and adds data to the database
'''
def setUp(self):
self.dir = os.path.dirname(
os.path.abspath(__file__))
#self.app = rubix.app.test_client()
self.response = self.client.get("/")
self.soup = BeautifulSoup(self.response.data,
'html.parser')
db.create_all()
db.session.add(User("Firstname", "Lastname", "test@test.com", "test123"))
db.session.add(User("Mock", "User", "mock@user.com", "mock123"))
db.session.add(User("new", "User", "new@user.com", "new123"))
db.session.add(User("Competitor", "Jones", "comp@jones.com", "comp123"))
db.session.add(User("delegate", "DeeDee", "delegatetest@test.com", "1234567890"))
user = User.query.filter_by(email="test@test.com").first()
user1 = User.query.filter_by(email="new@user.com").first()
user2 = User.query.filter_by(email="comp@jones.com").first()
delegateuser = User.query.filter_by(email='delegatetest@test.com',credentials=2).first()
db.session.add(Competition(user.id, "Test name", "Test location", "Test City", "Test State", "10031", datetime.date(2017, 12, 31)))
db.session.add(Competition(20, "Cant view", "Cant view", "New York", "NY", "10031", datetime.date(2017, 12, 31)))
comp = Competition.query.filter_by(comp_id=1).first()
comp.approved = True
comp20 = Competition.query.filter_by(organizer_id=20).first()
announce1 = Announcement(comp.comp_id, user.id, "Test announcement", "Test body")
announce2 = Announcement(comp20.comp_id, 2, "Can i see this?", "maybe")
db.session.add(announce1)
db.session.add(announce2)
event1 = Event('4x4x4 Cube', 'Round 1', datetime.time(11, 0, 0), datetime.time(12, 0, 0))
db.session.add(event1)
comp.comp_events.append(event1)
comp.competitors.append(user)
comp.competitors.append(user1)
comp.competitors.append(user2)
register_user1 = EventUserLink(user=user1, event=event1)
register_user2 = EventUserLink(user=user2, event=event1)
db.session.add(register_user1)
db.session.add(register_user2)
register_user2.volunteer = True
register_user2.volunteer_role = 'Judge'
register_user1.staff = True
register_user1.staff_role = 'Scrambler'
db.session.commit()
'''
Drops all tables from the database
'''
def tearDown(self):
db.session.remove()
db.drop_all()
def login(self, email, password):
return self.client.post('/login', data=dict(
email=email,
password=password
), follow_redirects=True)
def logout(self):
return self.app.get('/logout', follow_redirects=True)
class TestBasic(BaseTestCase):
'''Ensure that flask was set up correctly'''
def test_index(self):
response = self.client.get('/login', content_type='html/text')
self.assertEqual(response.status_code, 200)
class TestUnitRubix(BaseTestCase):
def test_check_rubix_config(self):
file_exists = op.exists(op.join(self.dir,'config.py'))
self.assertTrue(file_exists)
def test_check_rubix_createdata(self):
file_exists = op.exists(op.join(self.dir,'db_create_data.py'))
self.assertTrue(file_exists)
def test_check_rubix_createdb(self):
file_exists = op.exists(op.join(self.dir,'db_create.py'))
self.assertTrue(file_exists)
def test_check_rubix_run(self):
file_exists = op.exists(op.join(self.dir,'run.py'))
self.assertTrue(file_exists)
class TestUser(BaseTestCase):
def test_user_signup(self):
with self.client:
response = self.client.post(
'/signup',
data=dict(
first_name="New", last_name="User",
email="new@user.com", password="newuser"
),
follow_redirects=True
)
user = User.query.filter_by(email="new@user.com").first()
self.assertTrue(user)
def test_incorrect_user_signup(self):
with self.client:
response = self.client.post(
'/signup',
data=dict(
first_name="New", last_name="User",
email="newuser", password="newuser"
),
follow_redirects=True
)
self.assertIn(b'Please enter a valid email.', response.data)
self.assertIn('/signup', request.url)
def test_login_page_loads(self):
response = self.client.get('/login', content_type='html/text')
self.assertIn(b'Log in', response.data)
def test_login(self):
with self.client:
response = self.login('test@test.com', 'test123')
# self.assertIn(b'Logged in', response.data)
self.assertTrue(current_user.email == "test@test.com")
def test_incorrect_login(self):
with self.client:
response = self.client.post(
'/login',
data=dict(email="test", password="test123"),
follow_redirects=True
)
self.assertIn(b'Please enter your email address.', response.data)
self.assertIn('/login', request.url)
def test_logout(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/logout', follow_redirects=True)
self.assertIn(b'You have been logged out!', response.data)
self.assertFalse(current_user.is_active)
# Ensure that logout page requires user login
def test_logout_route_requires_login(self):
response = self.client.get('/logout', follow_redirects=True)
self.assertIn(b'You need to be logged in to access this page!', response.data)
def test_get_by_id(self):
with self.client:
self.login('test@test.com', 'test123')
self.assertTrue(current_user.id == 1)
self.assertFalse(current_user.id == 20)
def test_check_password(self):
user = User.query.filter_by(email='test@test.com').first()
self.assertTrue(check_password_hash(user.password_hash, 'test123'))
self.assertFalse(check_password_hash(user.password_hash, 'foobar'))
class TestHost(BaseTestCase):
def test_host_requires_login(self):
response = self.client.get('/host', follow_redirects=True)
self.assertIn(b'You need to be logged in to access this page!', response.data)
def test_host_submission(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.post(
'/host',
data=dict(
name="Cube Day",
location="Cubicle",
date=datetime.date(2018, 5, 13),
events="rubikscube"
),
follow_redirects=True
)
comp = Competition.query.filter_by(title="Cube Day").first()
self.assertTrue(comp)
def test_submission_on_manage(self):
with self.client:
self.login('test@test.com', 'test123')
self.client.post(
'/host',
data=dict(
name="Cube Day",
location="Cubicle",
date=datetime.date(2018, 5, 13),
),
follow_redirects=True
)
response = self.client.get('/manage', content_type='html/text')
self.assertIn(b'Cube Day', response.data)
self.assertIn(b'2018-05-13', response.data)
self.assertIn(b'Manage', response.data)
def test_submission_matches_user(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.post(
'/host',
data=dict(
name="Cube Day",
location="Cubicle",
date=datetime.date(2018, 5, 13),
events="rubikscube"
),
follow_redirects=True
)
comp = Competition.query.filter_by(title="Cube Day").first()
self.assertTrue(comp.organizer_id == current_user.id)
class TestManage(BaseTestCase):
def test_manage_requires_login(self):
response = self.client.get('/manage', follow_redirects=True)
self.assertIn(b'You need to be logged in to access this page!', response.data)
def test_manage_displays_competition(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/manage', content_type='html/text')
self.assertIn(b'Test name', response.data)
self.assertIn(b'Test location', response.data)
self.assertIn(b'2017-12-31', response.data)
self.assertIn(b'Manage', response.data)
def test_manage_only_displays_currentuser_comps(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/manage', content_type='html/text')
self.assertIn(b'Test name', response.data)
self.assertFalse(b'Cant view' in response.data)
def test_manage_competitors_and_schedule(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/manage/1', content_type='html/text')
self.assertIn(b'Firstname', response.data)
self.assertIn(b'Competitor', response.data)
self.assertIn(b'4x4x4 Cube', response.data)
def test_manage_shows_organizer_edit_buttons(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/manage/1', content_type='html/text')
self.assertIn(b'New Event', response.data)
self.assertIn(b'Edit', response.data)
self.assertIn(b'Delete', response.data)
def test_manage_event_shows_info(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/manage/1/schedule/1', content_type='html/text')
self.assertIn(b'11:00 AM - 12:00 PM', response.data)
self.assertIn(b'Volunteers:', response.data)
self.assertIn(b'Staff:', response.data)
def test_manage_accept_competition(self):
with self.client:
self.client
def test_manage_edit_event_displays_correctly(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/manage/1/schedule/1/edit', content_type='html/text')
self.assertIn(b'Change Role', response.data)
class TestAnnouncement(BaseTestCase):
def test_announcements_exist(self):
with self.client:
self.client.post(
'/login',
data=dict(email="mock@user.com", password="mock123"),
follow_redirects=True
)
response = self.client.get('/manage/1/announcements', content_type='html/text')
self.assertIn(b'Test announcement', response.data)
self.assertIn(b'Test body', response.data)
def test_comp_owner_can_post_announcement(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/manage/1/announcements', content_type='html/text')
self.assertIn(b'Title', response.data)
self.assertIn(b'Body', response.data)
def test_announcement_gets_posted(self):
with self.client:
self.login('test@test.com', 'test123')
self.client.post(
'/manage/1/announcements',
data=dict(title="new post", body="new body"),
follow_redirects=True
)
response = self.client.get('/manage/1/announcements', content_type='html/text')
self.assertIn(b'new post', response.data)
self.assertIn(b'new body', response.data)
def test_incorrect_announcement_gets_posted(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.post(
'/manage/1/announcements',
data=dict(title="", body=""),
follow_redirects=True
)
self.assertIn(b'Please enter a title', response.data)
self.assertIn(b'Please enter a body', response.data)
class TestSchedule(BaseTestCase):
def test_event_exist(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.get('/manage/1', content_type='html/text')
self.assertIn(b'4x4x4 Cube', response.data)
def test_event_is_created(self):
with self.client:
self.login('test@test.com', 'test123')
response = self.client.post(
'/manage/1/newevent',
data=dict(
event="5x5x5 Cube",
event_round="Round 1",
start_time="11:00 AM",
end_time="12:00 PM"
),
follow_redirects=True
)
self.assertIn(b'5x5x5 Cube', response.data)
self.assertIn(b'Round 1', response.data)
# class TestUserProfile(BaseTestCase):
# # this tests to check that the name in the tab is the same as the one we said so here, which it is....but not working
# # error: AttributeError: 'Flask' object has no attribute 'get'
# def test_user_profile_tab_name(self):
# with self.client:
# self.client.post(
# '/login',
# data=dict(email="delegatetest@test.com", password="1234567890"),
# follow_redirects=True
# )
# response = self.client.get('/profile', content_type='html/text')
# self.client.assertEqual("current_user.first_name current_user.last_name",self.soup.title.text)
# def test_user_profile_header(self):
# with self.client:
# self.client.post(
# '/login',
# data=dict(email="delegatetest@test.com", password="1234567890"),
# follow_redirects=True
# )
# response = self.client.get('/profile', content_type='html/text')
# self.client.assertEqual("current_user.first_name current_user.last_name",self.soup.h1.text.strip())
class TestUnitApp(BaseTestCase):
def test_check_routes_file(self):
file_exits = op.exists(op.join(self.dir,
'app',
'__init__.py'))
self.assertTrue(file_exits)
def test_check_routes(self):
file_exits = op.exists(op.join(self.dir,
'app',
'routes.py'))
self.assertTrue(file_exits)
def test_check_models(self):
file_exits = op.exists(op.join(self.dir,
'app',
'models.py'))
self.assertTrue(file_exits)
def test_check_forms(self):
file_exits = op.exists(op.join(self.dir,
'app',
'forms.py'))
self.assertTrue(file_exits)
# this is the checks for the profile layout, this layout is used by both regular users and delegates
def test_check_profile_layout(self):
file_exits = op.exists(op.join(self.dir,
'app',
'templates',
'profile-layout.html'))
self.assertTrue(file_exits)
def test_check_profile_page(self):
file_exits = op.exists(op.join(self.dir,
'app',
'templates',
'user_profile.html'))
self.assertTrue(file_exits)
def test_check_delegate_page(self):
file_exits = op.exists(op.join(self.dir,
'app',
'templates',
'delegate_profile.html'))
self.assertTrue(file_exits)
def test_check_landing_page(self):
file_exits = op.exists(op.join(self.dir,
'app',
'templates',
'landing_page.html'))
self.assertTrue(file_exits)
def test_check_layout(self):
file_exits = op.exists(op.join(self.dir,
'app',
'templates',
'layout.html'))
self.assertTrue(file_exits)
def test_check_404(self):
file_exits = op.exists(op.join(self.dir,
'app',
'templates',
'404.html'))
self.assertTrue(file_exits)
class TestUnitProject(BaseTestCase):
# this __init__.py file is for the entire projects folder
def test_check_init(self):
file_exits = op.exists(op.join(self.dir,
'project',
'__init__.py'
))
self.assertTrue(file_exits)
# HOST directory
def test_check_host_init(self):
file_exits = op.exists(op.join(self.dir,
'project',
'host',
'__init__.py'))
self.assertTrue(file_exits)
def test_check_host_view(self):
file_exits = op.exists(op.join(self.dir,
'project',
'host',
'views.py'))
self.assertTrue(file_exits)
def test_check_host_file(self):
file_exits = op.exists(op.join(self.dir,
'project',
'host',
'templates',
'host.html'))
self.assertTrue(file_exits)
#manage directory
def test_check_manage_init(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'__init__.py'))
self.assertTrue(file_exists)
def test_check_manage_view(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'views.py'))
self.assertTrue(file_exists)
def test_check_manage_announcements(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'announcements.html'))
self.assertTrue(file_exists)
def test_check_manage_competition_layout(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'competition_layout.html'))
self.assertTrue(file_exists)
def test_check_manage_compnavbar(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'competition_navbar.html'))
self.assertTrue(file_exists)
def test_check_manage_details(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'details.html'))
self.assertTrue(file_exists)
def test_check_manage_edit(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'edit.html'))
self.assertTrue(file_exists)
def test_check_manage_event(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'event.html'))
self.assertTrue(file_exists)
def test_check_manage_file(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'manage.html'))
self.assertTrue(file_exists)
def test_check_manage_newevent(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'newevent.html'))
self.assertTrue(file_exists)
def test_check_manage_schedule(self):
file_exists = op.exists(op.join(self.dir,
'project',
'manage',
'templates',
'schedule.html'))
self.assertTrue(file_exists)
# User directory
def test_check_manage_init(self):
file_exists = op.exists(op.join(self.dir,
'project',
'users',
'__init__.py'))
self.assertTrue(file_exists)
def test_check_manage_views(self):
file_exists = op.exists(op.join(self.dir,
'project',
'users',
'views.py'))
self.assertTrue(file_exists)
def test_check_user_login(self):
file_exists = op.exists(op.join(self.dir,
'project',
'users',
'templates',
'login.html'))
self.assertTrue(file_exists)
def test_check_user_signup(self):
file_exists = op.exists(op.join(self.dir,
'project',
'users',
'templates',
'signup.html'))
self.assertTrue(file_exists)
if __name__ == '__main__':
unittest.main()