-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_tests.py
907 lines (774 loc) · 28.9 KB
/
unit_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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
import unittest
from flask import g
import db
from app import app
import sys
import re
#from io import BytesIO
validation_value = b'theme-color'
def getCSRF(resp):
lines = resp.get_data(as_text=True).split("\n")
csrf = ""
for line in lines:
match = re.match(r'.*id="csrf_token".*value="(.*)".*', line)
if match is not None:
csrf = match.group(1)
break
return csrf
class FlaskTestCase(unittest.TestCase):
# This is a helper class that sets up the proper Flask execution context
# so that the test cases that inherit it will work properly.
def setUp(self):
# Allow exceptions (if any) to propagate to the test client.
app.testing = True
# Create a test client.
self.client = app.test_client(use_cookies=True)
# Create an application context for testing.
self.app_context = app.test_request_context()
self.app_context.push()
def tearDown(self):
# Clean up the application context.
self.app_context.pop()
class DatabaseTestCaseDay0(FlaskTestCase):
"""Setup Cursor for Testing"""
@staticmethod
def execute_sql(resource_name):
with app.open_resource(resource_name, mode='r') as f:
g.cursor.execute(f.read())
g.connection.commit()
def setUp(self):
super(DatabaseTestCaseDay0, self).setUp()
db.open_db()
self.execute_sql('db\create_tables.sql')
def tearDown(self):
db.close_db()
super(DatabaseTestCaseDay0, self).tearDown()
"""------------------------"""
def test_all_listings(self):
listings = db.all_listings()
self.assertEqual(
len(listings), 0,
"All Listings Day 0 - Unexpected extra listings.")
def test_all_users(self):
users = db.all_users()
self.assertEqual(
len(users), 0, "All Users Day 0 - Unexpected extra users.")
def test_title_like_listings(self):
listings = db.title_like_listings("addtest")
self.assertEqual(
len(listings), 0,
"Title Like Listings Day 0 - Unexpected match for listing.")
def test_search_like_category(self):
listings = db.search_like_category("veg")
self.assertEqual(
len(listings), 0,
"Search Like Category Day 0 - Unexpected match for category.")
def test_search_like_users(self):
users = db.search_like_users("ha")
self.assertEqual(
len(users), 0,
"Search Like Users Day 0 - Unexpected match for user.")
def test_add_listing(self):
try:
rowcount = db.add_listing({
'seller_id': 0,
'title': "addtest",
'photo': "",
'description': "This is a test.",
'original_quantity': 10,
'available_quantity': 10,
'unit_type': "each",
'price_per_unit': 1.1,
'total_price': 11.0,
'category_id': 1,
'date_harvested': "2018-04-19",
'is_tradeable': True
})
except:
self.assertTrue(
True,
"Add Listing Day 0 - This should pass. Listing should not be added without category."
)
return
self.assertTrue(
False,
"Add Listing Day 0 - This should not be reached. Listing should not be added without category."
)
def test_get_one_listing(self):
listing = db.get_one_listing(1)
self.assertTrue(
listing is None,
"Get One Listing Day 0 - Non-existent listing should not have returned."
)
def test_get_one_user(self):
user = db.get_one_user(1)
self.assertTrue(
user is None,
"Get One User Day 0 - Non-existent listing should not have returned."
)
def test_find_user(self):
user = db.find_user("[email protected]")
self.assertTrue(
user is None,
"Find User Day 0 - Non-existent user should not have returned.")
def test_get_one_login(self):
user = db.get_one_login("[email protected]")
self.assertTrue(
user is None,
"Get One Login Day 0 - Non-existent user should not have returned."
)
def test_get_user_listings(self):
listings = db.get_user_listings(4)
self.assertEqual(
len(listings), 0,
"Get User Listings Day 0 - Returned listings for non-existent user."
)
def test_update_available_quantity(self):
rowcount = db.update_available_quantity(4, 1)
self.assertEqual(
rowcount, 0,
"Update Available Quantity Day 0 - Update quantity affected unexpected row."
)
def test_get_user_address(self):
address = db.get_user_address(2)
self.assertTrue(
address is None,
"Get User Address Day 0 - Address returned for non-existent user.")
def test_get_user_address_via_listing(self):
address = db.get_user_address_via_listing(4)
self.assertTrue(
address is None,
"Get User Address Via Listing Day 0 - Address returned for non-existent listing."
)
def test_get_listing_details_for_confirmation_page(self):
details = db.get_listing_details_for_confirmation_page(4)
self.assertTrue(
details is None,
"Get Listing Details For Confirmation Page Day 0 - Details returned for non-existent listing."
)
def test_add_new_order(self):
try:
rowcount = db.add_new_order(4, 7, 7.00, 2)
except:
self.assertTrue(
True,
"Add New Order Day 0 - This should pass. Listing does not exist."
)
return
self.assertTrue(
False,
"Add New Order Day 0 - This should not be reached. Order added for non-existent listing."
)
def test_create_new_address(self):
try:
rowcount = db.create_new_address({
"street": "Somewhere",
"city": "in",
"state": 15,
"zipcode": "99999"
})
except:
self.assertTrue(
True,
"Create New Address Day 0 - This should pass. States do not exist."
)
return
self.assertTrue(
False,
"Create New Address Day 0 - This should not be reached. States do not exist."
)
def test_get_all_states(self):
states = db.get_all_states()
self.assertEqual(
len(states), 0,
"Get All States Day 0 - Unexpected states in empty database.")
def test_create_user(self):
rowcount = db.create_user({
"address_id": 1,
"email": "[email protected]",
"first": "brandon",
"last": "george",
"photo": 'images/uploaded-images/Corn.jpg',
"pass": "somanly",
"bio": "Oh man!"
})
self.assertEqual(rowcount, 1,
"Create User Day 0 - Failed to create new user.")
def test_get_latest_user_id(self):
user_id = db.get_latest_user_id()
self.assertEqual(user_id, 1,
"Get Latest User ID Day 0 - Unexpected latest user.")
class DatabaseTestCaseDay1(FlaskTestCase):
"""Setup Cursor for Testing"""
@staticmethod
def execute_sql(resource_name):
with app.open_resource(resource_name, mode='r') as f:
g.cursor.execute(f.read())
g.connection.commit()
def setUp(self):
super(DatabaseTestCaseDay1, self).setUp()
db.open_db()
self.execute_sql('db\create_tables.sql')
self.execute_sql('db\seed_tables.sql')
def tearDown(self):
db.close_db()
super(DatabaseTestCaseDay1, self).tearDown()
"""------------------------"""
def test_all_listings(self):
listings = db.all_listings()
self.assertEqual(
len(listings), 5,
"All Listings Day 1 - Unexpected number of listings in day 1.")
def test_all_users(self):
users = db.all_users()
self.assertEqual(
len(users), 4,
"All Users Day 1 - Unexpected number of users in day 1.")
def test_title_like_listings(self):
db.add_listing({
'seller_id': 0,
'title': "addtest1",
'photo': "",
'description': "This is a test.",
'original_quantity': 10,
'available_quantity': 10,
'unit_type': "each",
'price_per_unit': 1.1,
'total_price': 11.0,
'category_id': 1,
'date_harvested': "2018-04-19",
'is_tradeable': True
})
listings = db.title_like_listings("addtest")
self.assertEqual(
len(listings), 1,
"Title Like Listings Day 1 - Unexpected number of listings like \"addtest\" in day 1."
)
db.add_listing({
'seller_id': 0,
'title': "addtest2",
'photo': "",
'description': "This is a test.",
'original_quantity': 10,
'available_quantity': 10,
'unit_type': "each",
'price_per_unit': 1.1,
'total_price': 11.0,
'category_id': 1,
'date_harvested': "2018-04-19",
'is_tradeable': True
})
listings = db.title_like_listings("addtest")
self.assertEqual(
len(listings), 2,
"Title Like Listings Day 1 - Unexpected number of listings like \"addtest\" in day 1."
)
def test_search_like_category(self):
listings = db.search_like_category("veg")
self.assertEqual(
len(listings), 5,
"Search Like Category Day 1 - Unexpected number of listings in vegetable category."
)
listings = db.search_like_category("fru")
self.assertEqual(
len(listings), 0,
"Search Like Category Day 1 - Unexpected number of listings in fruit category."
)
def test_search_like_users(self):
users = db.search_like_users("ha")
self.assertEqual(
len(users), 2,
"Search Like Users Day 1 - Unexpected number of users with \"ha\"."
)
users = db.search_like_users("jon")
self.assertEqual(
len(users), 1,
"Search Like Users Day 1 - Unexpected number of users with \"jon\"."
)
users = db.search_like_users("john")
self.assertEqual(
len(users), 0,
"Search Like Users Day 1 - Unexpected number of users with \"john\"."
)
def test_add_listing(self):
rowcount = db.add_listing({
'seller_id': 0,
'title': "addtest",
'photo': "",
'description': "This is a test.",
'original_quantity': 10,
'available_quantity': 10,
'unit_type': "each",
'price_per_unit': 1.1,
'total_price': 11.0,
'category_id': 1,
'date_harvested': "2018-04-19",
'is_tradeable': True
})
self.assertEqual(rowcount, 1,
"Add Listing Day 1 - Adding listing failed.")
def test_get_one_listing(self):
listing = db.get_one_listing(1)
self.assertEqual(listing['title'], "Peppers")
def test_get_one_user(self):
user = db.get_one_user(10)
self.assertTrue(
user is None,
"Get One User Day 1 - Non-existent user should not have returned.")
user = db.get_one_user(2)
self.assertEqual(
user["first_name"], "amish",
"Get One User Day 1 - Unable to get user with id of 2.")
def test_find_user(self):
user = db.find_user("[email protected]")
self.assertEqual(
user["first_name"], "jon",
"Find User Day 0 - Non-existent user should not have returned.")
def test_get_one_login(self):
user = db.get_one_login("[email protected]")
self.assertEqual(
user["first_name"], "jon",
"Get One Login Day 1 - Non-existent user should not have returned."
)
def test_get_user_listings(self):
listings = db.get_user_listings(1)
self.assertEqual(
len(listings), 1,
"Get User Listings Day 1 - Unexpected number of listings returned for user with id 1."
)
listings = db.get_user_listings(4)
self.assertEqual(
len(listings), 2,
"Get User Listings Day 1 - Unexpected number of listings returned for user with id 4."
)
def test_update_available_quantity(self):
rowcount = db.update_available_quantity(4, 10)
self.assertEqual(
rowcount, 0,
"Update Available Quantity Day 1 - Updated non-existent listing available_quantity."
)
db.update_available_quantity(7, 1)
listing = db.get_one_listing(1)
self.assertEqual(
listing["available_quantity"], 43,
"Update Available Quantity Day 1 - Unexpected available quantity after update."
)
def test_get_user_address(self):
address = db.get_user_address(2)
self.assertEqual(
address["street"], '236 West Reade Ave',
"Get User Address Day 1 - Unexpected address returned for user with id 2."
)
address = db.get_user_address(10)
self.assertTrue(
address is None,
"Get User Address Day 1 - Address returned for non-existent user.")
def test_get_user_address_via_listing(self):
address = db.get_user_address_via_listing(4)
self.assertEqual(
address["street"], "236 West Reade Ave",
"Get User Address Via Listing Day 1 - Unexpected address returned for listing with id 4."
)
address = db.get_user_address_via_listing(10)
self.assertTrue(
address is None,
"Get User Address Via Listing Day 1 - Address returned for non-existent listing."
)
def test_get_listing_details_for_confirmation_page(self):
details = db.get_listing_details_for_confirmation_page(4)
self.assertEqual(
details["first_name"], "tim",
"Get Listing Details For Confirmation Page Day 1 - Unexpected first name returned for listing details with id 4."
)
details = db.get_listing_details_for_confirmation_page(10)
self.assertTrue(
details is None,
"Get Listing Details For Confirmation Page Day 1 - Details returned for non-existent listing."
)
def test_create_new_address(self):
rowcount = db.create_new_address({
"street": "Somewhere",
"city": "in",
"state": 15,
"zipcode": "99999"
})
self.assertEqual(
rowcount, (1, [2]),
"Create New Address Day 1 - Failed to create new address.")
def test_add_new_order(self):
rowcount = db.add_new_order(4, 7, 7.00, 2)
self.assertEqual(
rowcount, 1,
"Add New Order Day 1 - Failed to add order on listing with id 4.")
try:
rowcount = db.add_new_order(10, 7, 7.00, 2)
except:
self.assertTrue(
True,
"Add New Order Day 1 - This should pass. Listing does not exist."
)
return
self.assertTrue(
False,
"Add New Order Day 1 - This should not be reached. Order added for non-existent listing."
)
def test_get_all_states(self):
states = db.get_all_states()
self.assertEqual(
len(states), 52,
"Get All States Day 1 - Unexpected number of states.")
def test_get_latest_user_id(self):
user_id = db.get_latest_user_id()
self.assertEqual(user_id, 5,
"Get Latest User ID Day 1 - Unexpected latest user.")
class ApplicationTestCaseDay0(FlaskTestCase):
"""Setup Cursor for Testing"""
@staticmethod
def execute_sql(resource_name):
with app.open_resource(resource_name, mode='r') as f:
g.cursor.execute(f.read())
g.connection.commit()
def setUp(self):
super(ApplicationTestCaseDay0, self).setUp()
db.open_db()
self.execute_sql('db\create_tables.sql')
def tearDown(self):
db.close_db()
super(ApplicationTestCaseDay0, self).tearDown()
"""------------------------"""
def test_index(self):
pass
# resp = self.client.get('/')
# self.assertTrue(validation_value in resp.data,
# "Index Day 0 - Missing site title.")
# self.assertTrue(b'Product Feed' in resp.data,
# "Index Day 0 - Missing page title.")
# self.assertTrue(b'No search results found' in resp.data,
# "Index Day 0 - Missing default response.")
def test_listing_detail(self):
try:
resp = self.client.get('/listing/1')
except:
self.assertTrue(
True,
"Listing Detail Day 0 - This should pass. Listing does not exist."
)
return
self.assertTrue(
False,
"Listing Detail Day 0 - This should not be reached. Listing does not exist."
)
def test_search(self):
resp = self.client.get('/search')
self.assertTrue(validation_value in resp.data,
"Search Day 0 - Missing site title for no search.")
self.assertTrue(b'Search for amazing products nearby!' in resp.data,
"Search Day 0 - Found matches for no search.")
resp = self.client.get('/search?search=thing')
self.assertTrue(validation_value in resp.data,
"Search Day 0 - Missing site title on bad search.")
self.assertTrue(
b'We could not find any matching products' in resp.data,
"Search Day 0 - Found matches for bad search.")
def test_listing_purchase(self):
resp = self.client.get('/listing/buy/1', follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Listing Purchase Day 0 - Missing site title.")
self.assertTrue(b'Login' in resp.data,
"Listing Purchase Day 0 - Missing login page title.")
def test_new_listing(self):
resp = self.client.get('/listing/add', follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Listing Purchase Day 0 - Missing site title.")
self.assertTrue(b'Login' in resp.data,
"Listing Purchase Day 0 - Missing login page title.")
def test_user_profile(self):
try:
resp = self.client.get('/user/1')
except:
self.assertTrue(
True,
"User Profile Day 0 - This should pass. User does not exist.")
return
self.assertTrue(
False,
"User Profile Day 0 - This should not be reached. User does not exist."
)
# def test_account(self):
# resp = self.client.get('/account', follow_redirects=True)
# self.assertTrue(validation_value in resp.data,
# "Account Day 0 - Missing site title.")
# self.assertTrue(b'Your Account' in resp.data,
# "Account Day 0 - Missing page title.")
# def test_settings(self):
# resp = self.client.get('/settings', follow_redirects=True)
# self.assertTrue(validation_value in resp.data,
# "Settings Day 0 - Missing site title.")
# self.assertTrue(b'Settings' in resp.data,
# "Settings Day 0 - Missing page title.")
def test_login(self):
resp = self.client.get('/login', follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Login Day 0 - Missing site title.")
self.assertTrue(b'Password' in resp.data,
"Login Day 0 - Missing title for password field.")
class ApplicationTestCaseDay1(FlaskTestCase):
"""Setup Cursor for Testing"""
@staticmethod
def execute_sql(resource_name):
with app.open_resource(resource_name, mode='r') as f:
g.cursor.execute(f.read())
g.connection.commit()
def setUp(self):
super(ApplicationTestCaseDay1, self).setUp()
db.open_db()
self.execute_sql('db\create_tables.sql')
self.execute_sql('db\seed_tables.sql')
def tearDown(self):
db.close_db()
super(ApplicationTestCaseDay1, self).tearDown()
"""------------------------"""
def test_index(self):
resp = self.client.get('/')
self.assertTrue(validation_value in resp.data,
"Index Day 1 - Missing site title.")
self.assertTrue(b'Product Feed' in resp.data,
"Index Day 1 - Missing page title.")
self.assertTrue(b'Corn' in resp.data,
"Index Day 1 - Missing tile for corn listing.")
def test_listing_detail(self):
resp = self.client.get('/listing/1')
self.assertTrue(validation_value in resp.data,
"Listing Detail Day 1 - Missing site title.")
self.assertTrue(b'Peppers' in resp.data,
"Listing Detail Day 1 - Missing listing title.")
self.assertTrue(b'1.00' in resp.data,
"Listing Detail Day 1 - Missing price per unit.")
def test_search(self):
resp = self.client.get('/search')
self.assertTrue(validation_value in resp.data,
"Search Day 1 - Missing site title for no search.")
self.assertTrue(b'Search for amazing products nearby!' in resp.data,
"Search Day 1 - Found matches for no search.")
resp = self.client.get('/search?search=thing')
self.assertTrue(validation_value in resp.data,
"Search Day 1 - Missing site title on bad search.")
self.assertTrue(
b'We could not find any matching products' in resp.data,
"Search Day 1 - Found matches for bad search.")
resp = self.client.get('/search?search=i')
self.assertTrue(validation_value in resp.data,
"Search Day 1 - Missing site title on broad search.")
self.assertTrue(
b'Zucchini' in resp.data,
"Search Day 1 - Missing listing match for broad search.")
self.assertTrue(b'Amish' in resp.data,
"Search Day 1 - Missing user match for broad search.")
def test_listing_purchase(self):
resp = self.client.get('/listing/buy/1', follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Listing Purchase Day 1 - Missing site title.")
self.assertTrue(b'Login' in resp.data,
"Listing Purchase Day 1 - Missing login page title.")
resp = self.client.get('/login')
csrf = getCSRF(resp)
resp = self.client.post(
"/login",
data=dict(csrf_token=csrf, email="[email protected]", password="tim"),
follow_redirects=True)
resp = self.client.get('/listing/buy/1')
self.assertTrue(validation_value in resp.data,
"Listing Purchase Day 1 1 - Missing site title.")
self.assertTrue(
b'Quantity Available' in resp.data,
"Listing Purchase Day 1 1 - Missing available quantity.")
self.assertTrue(
b'50' in resp.data,
"Listing Purchase Day 1 1 - Unexpected available quantity before update."
)
csrf = getCSRF(resp)
resp = self.client.post(
"/listing/buy/1",
data=dict(csrf_token=csrf, quantity=7, submit="Pay"),
follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Listing Purchase Day 1 2 - Missing site title.")
self.assertTrue(b'Total' in resp.data,
"Listing Purchase Day 1 2 - Missing title for total.")
self.assertTrue(b'7.00' in resp.data,
"Listing Purchase Day 1 2 - Unexpected total.")
resp = self.client.get('/listing/buy/1')
csrf = getCSRF(resp)
resp = self.client.post(
"/listing/buy/1",
data=dict(csrf_token=csrf, quantity=60, submit="Make+Purchase"),
follow_redirects=True)
self.assertTrue(
b'Pay' in resp.data,
"Listing Purchase Day 1 2 - Should still be on buy listing page.")
self.assertTrue(
b'Please select no more than the quantity that is available.' in
resp.data,
"Listing Purchase Day 1 2 - Missing flash message when purchase too large."
)
self.assertTrue(
b'Quantity Available' in resp.data,
"Listing Purchase Day 1 2 - Did not find quantity available on buy listing page."
)
self.assertTrue(
b'43' in resp.data,
"Listing Purchase Day 1 2 - Did not find expected available quantity on buy listing page."
)
try:
resp = self.client.get('/listing/buy/10')
except:
self.assertTrue(
True,
"Buy Listing Day 1 - This should pass. Listing does not exist."
)
return
self.assertTrue(
False,
"Buy Listing Day 1 - This should not be reached. Listing does not exist."
)
def test_new_listing(self):
resp = self.client.get('/listing/add', follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Listing Purchase Day 1 - Missing site title.")
self.assertTrue(b'Login' in resp.data,
"Listing Purchase Day 1 - Missing login page title.")
resp = self.client.get('/login')
csrf = getCSRF(resp)
resp = self.client.post(
"/login",
data=dict(csrf_token=csrf, email="[email protected]", password="tim"),
follow_redirects=True)
resp = self.client.get('/listing/add')
# csrf = getCSRF(resp)
self.assertTrue(validation_value in resp.data,
"New Listing Day 1 - Missing site title.")
self.assertTrue(b'Title' in resp.data,
"New Listing Day 1 - Missing title field.")
# f = open("./static/images/uploaded-images/Corn.jpg", "rb")
# read_data = f.read()
#
# resp = self.client.post("/listing/add", data=dict(csrf_token=csrf, title="test", photo=(bytes(read_data), "image.jpg"),
# description="This is a test.", original_quantity=10,
# unit_type="each", price_per_unit=0.97,
# category_id=1, is_tradeable="y",
# date_harvested="2018-05-02", submit="Add"),
# follow_redirects=True)
#
# f.close()
#
# text = resp.get_data(as_text=True).split("\n")
# for i in text:
# print(i, file=sys.stderr)
# self.assertTrue(validation_value in resp.data, "New Listing Day 1 - Missing site title.")
# self.assertTrue(b'Product Feed' in resp.data, "New Listing Day 1 - Missing page title.")
# self.assertTrue(b'vegetable' in resp.data, "Did not find expected category on add listing page.")
def test_user_profile(self):
resp = self.client.get('/user/1')
self.assertTrue(validation_value in resp.data,
"User Profile Day 1 - Missing site title.")
self.assertTrue(b'Hannes' in resp.data,
"User Profile Day 1 - Missing first name.")
self.assertTrue(b'Peppers' in resp.data,
"User Profile Day 1 - Missing listing title.")
try:
resp = self.client.get("/user/10")
except:
self.assertTrue(
True,
"User Profile Day 1 - This should pass. User does not exist.")
return
self.assertTrue(
False,
"User Profile Day 1 - This should not be reached. User does not exist."
)
def test_account(self):
# resp = self.client.get('/account')
# self.assertTrue(validation_value in resp.data,
# "Account Day 1 - Missing site title.")
# self.assertTrue(b'Your Account' in resp.data,
# "Account Day 1 - Missing page title.")
resp = self.client.get('/login')
csrf = getCSRF(resp)
resp = self.client.post(
"/login",
data=dict(csrf_token=csrf, email="tim@ours", password="tim"),
follow_redirects=True)
# resp = self.client.get('/account')
# self.assertTrue(validation_value in resp.data,
# "Account Day 1 - Missing site title.")
# self.assertTrue(b'Your Account' in resp.data,
# "Account Day 1 - Missing page title.")
# self.assertTrue(b'admin' not in resp.data,
# "Account Day 1 - Viewing non-admin as admin.")
# resp = self.client.get('/logout', follow_redirects=True)
# self.assertTrue(validation_value in resp.data, "Here 1 - Missing site title.")
# self.assertTrue(b'logged out' in resp.data, "Here 1 - Missing logged out flash.")
#
# resp = self.client.get('/login')
# csrf = getCSRF(resp)
# resp = self.client.post('/login', data=dict(csrf_token=csrf, email="[email protected]", password="hannes"), follow_redirects=True)
# text = resp.get_data(as_text=True).split("\n")
# for i in text:
# print(i, file=sys.stderr)
# self.assertTrue(validation_value in resp.data, "Here 2 - Missing site title.")
# self.assertTrue(b'logged in' in resp.data, "Here 2 - Missing logged in flash.")
#
# resp = self.client.get('/account')
# self.assertTrue(validation_value in resp.data, "Account Day 1 - Missing site title.")
# self.assertTrue(b'Your Account' in resp.data, "Account Day 1 - Missing page title.")
# self.assertTrue(b'admin' in resp.data, "Account Day 1 - Viewing admin as non-admin.")
def test_login(self):
resp = self.client.get('/login')
self.assertTrue(validation_value in resp.data,
"Login Day 1 - Missing site title.")
self.assertTrue(b'Password' in resp.data,
"Login Day 1 - Missing title for password field.")
csrf = getCSRF(resp)
resp = self.client.post(
"/login",
data=dict(csrf_token=csrf, email="tim@ours", password="tim"),
follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Login Day 1 - Missing site title.")
self.assertTrue(b'Invalid email' in resp.data,
"Login Day 1 - Missing invalid email in flash.")
resp = self.client.get('/login')
csrf = getCSRF(resp)
resp = self.client.post(
"/login",
data=dict(
csrf_token=csrf, email="[email protected]", password="nottim"),
follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Login Day 1 - Missing site title.")
self.assertTrue(b'Invalid password' in resp.data,
"Login Day 1 - Missing invalid password in flash.")
resp = self.client.get('/login')
csrf = getCSRF(resp)
resp = self.client.post(
"/login",
data=dict(csrf_token=csrf, email="[email protected]", password="tim"),
follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Login Day 1 - Missing site title.")
self.assertTrue(b'logged in' in resp.data,
"Login Day 1 - Missing logged in flash.")
def test_logout(self):
resp = self.client.get('/login')
csrf = getCSRF(resp)
self.client.post(
'/login',
data=dict(csrf_token=csrf, email="[email protected]", password="tim"),
follow_redirects=True)
resp = self.client.get('/logout', follow_redirects=True)
self.assertTrue(validation_value in resp.data,
"Logout Day 1 - Missing site title.")
self.assertTrue(b'logged out' in resp.data,
"Logout Day 1 - Missing logged out flash.")
if __name__ == '__main__':
unittest.main()