forked from Khan/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets_test.py
executable file
·1588 lines (1299 loc) · 67.3 KB
/
snippets_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
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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""Tests for the snippets server.
This tests the functionality found at weekly-snippets.appspot.com.
c.f. http://code.google.com/appengine/docs/python/tools/localunittesting.html
c.f. http://webtest.pythonpaste.org/en/latest/index.html
"""
__author__ = 'Craig Silverstein <csilvers@khanacademy.org>'
import datetime
import os
import re
import sys
import time
try: # Work under either python2.5 or python2.7
import unittest2 as unittest
except ImportError:
import unittest
# Update sys.path so it can find these. We just need to add
# 'google_appengine', but we add all of $PATH to be easy. This
# assumes the google_appengine directory is on the path.
sys.path.extend(os.environ['PATH'].split(':'))
import dev_appserver
dev_appserver.fix_sys_path()
from google.appengine.datastore import datastore_stub_util
from google.appengine.ext import db
from google.appengine.ext import testbed
import webtest # may need to do 'pip install webtest'
import hipchatlib
import models
import slacklib
import snippets
_TEST_TODAY = datetime.datetime(2012, 2, 23)
class SnippetsTestBase(unittest.TestCase):
def setUp(self):
# We're not interested in testing consistency stuff in these tests.
self.testbed = testbed.Testbed()
self.testbed.activate()
policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
probability=1)
self.testbed.init_datastore_v3_stub(consistency_policy=policy)
self.testbed.init_user_stub()
self.request_fetcher = webtest.TestApp(snippets.application)
snippets._TODAY_FN = lambda: _TEST_TODAY
# Make sure we never accidentally send messages to chat.
self.old_send_to_hipchat_room = hipchatlib.send_to_hipchat_room
self.hipchat_sends = []
hipchatlib.send_to_hipchat_room = (
lambda *args: self.hipchat_sends.append(args))
self.old_send_to_slack_channel = slacklib.send_to_slack_channel
self.slack_sends = []
slacklib.send_to_slack_channel = (
lambda *args: self.slack_sends.append(args))
def tearDown(self):
self.testbed.deactivate()
hipchatlib.send_to_hipchat_room = self.old_send_to_hipchat_room
slacklib.send_to_slack_channel = self.old_send_to_slack_channel
def login(self, email):
self.testbed.setup_env(user_email=email, overwrite=True)
self.testbed.setup_env(user_id=email, overwrite=True)
self.testbed.setup_env(user_is_admin='0', overwrite=True)
# Now make sure there are global settings.
settings = models.AppSettings.get(
create_if_missing=True,
domains=['example.com', 'some_other_domain.com'],
)
settings.hostname = 'https://example.com'
settings.put()
def set_is_admin(self):
self.testbed.setup_env(user_is_admin='1', overwrite=True)
def assertNumSnippets(self, body, expected_count):
"""Assert the page 'body' has exactly expected_count snippets in it."""
# We annotate the div at the beginning of each snippet with
# class="<etc> unique-snippet".
self.assertEqual(expected_count, body.count('unique-snippet'),
body)
def _ith_snippet(self, body, snippet_number):
"""For user- and weekly-pages, return the i-th snippet, 0-indexed."""
# The +1 is because the 0-th element is stuff before the 1st snippet.
# If we get an IndexError, it means there aren't that many snippets.
try:
return body.split('unique-snippet',
snippet_number + 2)[snippet_number + 1]
except IndexError:
raise IndexError('Has fewer than %d snippets:\n%s'
% ((snippet_number + 1), body))
def assertInSnippet(self, text, body, snippet_number):
"""For snippet-page 'body', assert 'text' is in the i-th snippet.
This works for both user-pages and weekly-pages -- we figure out
the boundaries of the snippets, and check whether the text is
in the given snippet.
If text is a list or tuple, we check each item of text in turn.
Arguments:
text: the text to find in the snippet. If a list or tuple,
test each item in the list in turn.
body: the full html page
snippet_number: which snippet on the page to examine.
Index starts at 0.
"""
self.assertIn(text, self._ith_snippet(body, snippet_number))
def assertNotInSnippet(self, text, body, snippet_number):
"""For snippet-page 'body', assert 'text' is not in the ith snippet."""
self.assertNotIn(text, self._ith_snippet(body, snippet_number))
class UserTestBase(SnippetsTestBase):
"""The most common base: someone who is logged in as user@example.com."""
def setUp(self):
super(UserTestBase, self).setUp()
self.login('user@example.com')
class PostTestCase(SnippetsTestBase):
"""test the correct output from the server when POSTing"""
def testPostSnippet(self):
self.login('user@example.com')
url = '/update_snippet'
params = {
'week': '02-20-2012',
'snippet': 'my inspired snippet'
}
response = self.request_fetcher.post(url, params, status=200)
self.assertIn('{"status": 200, "message": "ok"}', response)
def testPostSnippetAsOtherPerson(self):
self.login('user@example.com')
url = '/update_snippet'
params = {
'week': '02-20-2012',
'snippet': 'my fallacious snippet',
'u': 'joeuser@example.com'
}
response = self.request_fetcher.post(url, params, status=403)
self.assertIn('"status": 403', response)
self.assertIn('joeuser@example.com', response)
def testPostSnippetNotLoggedIn(self):
url = '/update_snippet'
params = {
'week': '02-20-2012',
'snippet': 'my fallacious snippet',
'u': 'user@example.com'
}
response = self.request_fetcher.post(url, params, status=403)
self.assertIn('"status": 403', response)
self.assertIn('"message": "not logged in"', response)
def testPostSnippetIsolation(self):
# updating a single snippet via POST should not affect other snippets
self.login('user@example.com')
# create three snippets
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
url = '/update_snippet?week=02-27-2012&snippet=my+second+snippet'
self.request_fetcher.get(url)
url = '/update_snippet?week=03-05-2012&snippet=my+third+snippet'
self.request_fetcher.get(url)
# update only middle snippet
url = '/update_snippet'
params = {
'week': '02-27-2012',
'snippet': 'updated second snippet'
}
self.request_fetcher.post(url, params, status=200)
# make sure only the second snippet changed
response = self.request_fetcher.get('/')
self.assertInSnippet('>my third snippet<', response.body, 0)
self.assertInSnippet('>updated second snippet<', response.body, 1)
self.assertNotInSnippet('>my second snippet<', response.body, 1)
self.assertInSnippet('>my snippet<', response.body, 2)
class LoginRequiredTestCase(SnippetsTestBase):
def assert_requires_login(self, response):
"""Assert that a response causes us to redirect to the login page."""
self.assertIn('login', response.headers.get('Location', '').lower())
def assert_does_not_require_login(self, response):
"""Assert that a response is not a redirect to the login page."""
self.assertNotIn('login', response.headers.get('Location', '').lower())
def testLoginRequiredForUserView(self):
url = '/'
response = self.request_fetcher.get(url)
self.assert_requires_login(response)
self.login('user@example.com')
response = self.request_fetcher.get(url)
self.assert_does_not_require_login(response)
def testLoginRequiredForWeeklyView(self):
url = '/weekly'
response = self.request_fetcher.get(url)
self.assert_requires_login(response)
self.login('user@example.com')
response = self.request_fetcher.get(url)
self.assert_does_not_require_login(response)
def testLoginRequiredForSettingsView(self):
url = '/settings'
response = self.request_fetcher.get(url)
self.assert_requires_login(response)
self.login('user@example.com')
response = self.request_fetcher.get(url)
self.assert_does_not_require_login(response)
def testLoginRequiredToUpdateSnippet(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
response = self.request_fetcher.get(url)
self.assert_requires_login(response)
self.login('user@example.com')
response = self.request_fetcher.get(url)
self.assert_does_not_require_login(response)
def testLoginRequiredToUpdateSettings(self):
url = '/update_settings'
response = self.request_fetcher.get(url)
self.assert_requires_login(response)
self.login('user@example.com')
response = self.request_fetcher.get(url)
self.assert_does_not_require_login(response)
class AccessTestCase(UserTestBase):
"""Tests that you can't modify someone who is not yourself."""
def testCanViewOtherSnippets(self):
url = '/?u=notuser@example.com'
# Raises an error if we don't get a 200 response.
self.request_fetcher.get(url)
def testCanViewWeeklyPage(self):
"""u is ignored for /weekly, but included for completeness."""
url = '/weekly?u=notuser@example.com'
self.request_fetcher.get(url)
def testCannotEditOtherSnippets(self):
url = ('/update_snippet?week=02-20-2012&snippet=my+snippet'
'&u=notuser@example.com')
# Raises an error if we don't get a 500 response (meaning no perm).
self.request_fetcher.get(url, status=500)
def testCannotViewOtherSettings(self):
url = '/settings?u=notuser@example.com'
self.request_fetcher.get(url, status=500)
def testCannotEditOtherSettings(self):
url = '/update_settings?u=notuser@example.com'
self.request_fetcher.get(url, status=500)
def testCanEditOwnSnippets(self):
url = ('/update_snippet?week=02-20-2012&snippet=my+snippet'
'&u=user@example.com')
self.request_fetcher.get(url)
def testCanViewOwnSettings(self):
url = '/settings?u=user@example.com'
self.request_fetcher.get(url)
def testCanEditOwnSettings(self):
url = '/update_settings?u=user@example.com'
self.request_fetcher.get(url)
def testCanEditOtherSnippetsAsAdmin(self):
self.set_is_admin()
url = ('/update_snippet?week=02-20-2012&snippet=my+snippet'
'&u=notuser@example.com')
self.request_fetcher.get(url)
def testCanViewOtherSettingsAsAdmin(self):
self.set_is_admin()
url = '/settings?u=notuser@example.com'
self.request_fetcher.get(url)
def testCanEditOtherSettingsAsAdmin(self):
self.set_is_admin()
url = '/update_settings?u=notuser@example.com'
self.request_fetcher.get(url)
class NewUserTestCase(UserTestBase):
"""Test the workflow for registering as a new user."""
def testNewUserLogin(self):
response = self.request_fetcher.get('/')
self.assertIn('<title>New user</title>', response.body)
def testNewUserContinueUrl(self):
"""After verifying settings, we should go back to snippet-entry."""
response = self.request_fetcher.get('/')
m = re.search(r'<a href="(/settings[^"]*)">', response.body)
continue_url = m.group(1)
settings_response = self.request_fetcher.get(continue_url)
self.assertIn('name="redirect_to" value="snippet_entry"',
settings_response.body)
# Now kinda-simulate clicking on the submit button.
done_response = self.request_fetcher.get(
'/update_settings?u=user@example.com&redirect_to=snippet_entry')
if done_response.status_int in (301, 302, 303, 304):
done_response = done_response.follow()
self.assertIn('Snippets for user@example.com', done_response.body)
def testNewAdminWithNoAppSettings(self):
"""The first time someone logs in, we should go to app settings."""
self.set_is_admin()
app_settings = models.AppSettings.get()
app_settings.delete()
response = self.request_fetcher.get('/')
if response.status_int in (301, 302, 303, 304):
response = response.follow()
self.assertIn('<title>Application settings</title>', response.body)
def testNewAdminContinueUrls(self):
"""We should go from app settings to user settings to snippet."""
self.set_is_admin()
app_settings = models.AppSettings.get()
app_settings.delete()
response = self.request_fetcher.get('/')
if response.status_int in (301, 302, 303, 304):
response = response.follow()
m = re.search(r'name="redirect_to" value="([^"]*)"', response.body)
continue_url = m.group(1)
# Now kinda-simulate clicking on the submit button.
done_response = self.request_fetcher.get(
'/admin/update_settings?domains=example.com&redirect_to=%s'
% continue_url)
if done_response.status_int in (301, 302, 303, 304):
done_response = done_response.follow()
self.assertIn('<title>User settings', done_response.body)
self.assertIn('name="redirect_to" value="snippet_entry"',
done_response.body)
def testNewUserWithNoAppSettings(self):
"""For non-admins, we should not offer the app-settings page."""
app_settings = models.AppSettings.get()
app_settings.delete()
response = self.request_fetcher.get('/')
self.assertIn('<title>New user</title>', response.body)
def testNewUserInheritsAppDefaults(self):
app_settings = models.AppSettings.get()
app_settings.default_markdown = True
app_settings.default_private = True
app_settings.put()
response = self.request_fetcher.get('/')
m = re.search(r'<a href="(/settings[^"]*)">', response.body)
continue_url = m.group(1)
settings_response = self.request_fetcher.get(continue_url)
self.assertRegexpMatches(settings_response.body,
r'name="markdown"\s+value="yes"\s+checked')
self.assertRegexpMatches(settings_response.body,
r'name="private"\s+value="yes"\s+checked')
# Now change the app-defaults and make sure this is reflected in
# the new-user setup page.
app_settings.default_markdown = False
app_settings.default_private = False
app_settings.put()
response = self.request_fetcher.get('/')
m = re.search(r'<a href="(/settings[^"]*)">', response.body)
continue_url = m.group(1)
settings_response = self.request_fetcher.get(continue_url)
self.assertRegexpMatches(settings_response.body,
r'name="markdown"\s+value="no"\s+checked')
self.assertRegexpMatches(settings_response.body,
r'name="private"\s+value="no"\s+checked')
def testNewUserInValidDomain(self):
self.login('newuser@example.com')
response = self.request_fetcher.get('/settings')
self.assertIn('<title>User settings', response.body)
def testNewUserInInvalidDomain(self):
"""Test you can not register as a new user from a random domain."""
self.login('newuser@notallowed.com')
# TODO(csilvers): give a nice error page instead of a 500.
self.request_fetcher.get('/settings', status=500)
def testSettingsPageDoesNotCreateANewUser(self):
"""Only *saving* the settings should create a new user."""
response = self.request_fetcher.get('/')
self.assertIn('<title>New user</title>', response.body)
m = re.search(r'<a href="(/settings[^"]*)">', response.body)
continue_url = m.group(1)
self.request_fetcher.get(continue_url) # visit /settings
# Now if we go to / again, we should get the new-user page again
# because the settings were never saved.
response = self.request_fetcher.get('/')
self.assertIn('<title>New user</title>', response.body)
class AppSettingsTestCase(UserTestBase):
"""Test the app-settings page."""
def setUp(self):
super(AppSettingsTestCase, self).setUp()
self.set_is_admin()
def testDomainsParsing(self):
self.request_fetcher.get(
'/admin/update_settings?domains=a.com,b.com++c.com%0Bd.com,%0B')
app_settings = models.AppSettings.get()
self.assertEqual(['a.com', 'b.com', 'c.com', 'd.com'],
app_settings.domains)
class UserSettingsTestCase(UserTestBase):
"""Test setting and using user settings."""
def assertInputIsChecked(self, name, body, snippet_number):
self.assertRegexpMatches(body, r'name="%s" value="True"\s+checked\s*>'
% name, snippet_number)
def assertInputIsNotChecked(self, name, body, snippet_number):
self.assertRegexpMatches(body, r'name="%s" value="True"\s*>' % name,
snippet_number)
def testDefaultUserSettings(self):
# Make sure the rest of the tests are actually testing
# non-default behavior.
self.request_fetcher.get('/update_settings?u=user@example.com')
response = self.request_fetcher.get('/')
# Neither private nor 'is-markdown' are checked by default.
self.assertInputIsNotChecked('private', response.body, 0)
self.assertInputIsNotChecked('is_markdown', response.body, 0)
def testPrivateUser(self):
self.request_fetcher.get(
'/update_settings?u=user@example.com&private=yes')
response = self.request_fetcher.get('/')
self.assertInputIsChecked('private', response.body, 0)
self.assertInputIsNotChecked('is_markdown', response.body, 0)
def testMarkdownUser(self):
self.request_fetcher.get(
'/update_settings?u=user@example.com&markdown=yes')
response = self.request_fetcher.get('/')
self.assertInputIsNotChecked('private', response.body, 0)
self.assertInputIsChecked('is_markdown', response.body, 0)
def testSettingsForFilledInSnippets(self):
url = '/update_snippet?week=02-21-2011&snippet=old+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 53)
self.assertInputIsNotChecked('private', response.body, 9)
self.assertInputIsNotChecked('is_markdown', response.body, 9)
self.assertInSnippet('old snippet', response.body, 52)
self.assertInputIsNotChecked('private', response.body, 52)
self.assertInputIsNotChecked('is_markdown', response.body, 52)
self.request_fetcher.get(
'/update_settings?u=user@example.com&markdown=yes')
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 53)
self.assertInputIsNotChecked('private', response.body, 9)
self.assertInputIsChecked('is_markdown', response.body, 9)
# But the existing snippet is unaffected.
self.assertInSnippet('old snippet', response.body, 52)
self.assertInputIsNotChecked('private', response.body, 52)
self.assertInputIsNotChecked('is_markdown', response.body, 52)
self.request_fetcher.get(
'/update_settings?u=user@example.com&private=yes')
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 53)
self.assertInputIsChecked('private', response.body, 9)
self.assertInputIsNotChecked('is_markdown', response.body, 9)
self.assertInSnippet('old snippet', response.body, 52)
self.assertInputIsNotChecked('private', response.body, 52)
self.assertInputIsNotChecked('is_markdown', response.body, 52)
def testCategoryUnset(self):
self.request_fetcher.get('/update_settings?u=user@example.com')
response = self.request_fetcher.get('/')
self.assertInSnippet(
'<strong>WARNING:</strong> Snippet will go in the "(unknown)"',
response.body, 0
)
def testCategorySet(self):
self.request_fetcher.get('/update_settings?u=user@example.com')
self.request_fetcher.get(
'/update_settings?u=user@example.com&category=Dev')
response = self.request_fetcher.get('/')
self.assertNotInSnippet(
'<strong>WARNING:</strong> Snippet will go in the "(unknown)"',
response.body, 0
)
def testCategoryUnsetButSnippetHasContent(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertInSnippet(
'<strong>WARNING:</strong> Snippet will go in the "(unknown)"',
response.body, 0
)
def testCategoryCheckForFilledInSnippets(self):
url = '/update_snippet?week=02-21-2011&snippet=old+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 53)
self.assertInSnippet(
'<strong>WARNING:</strong> Snippet will go in the "(unknown)"',
response.body, 9
)
self.assertInSnippet('old snippet', response.body, 52)
self.assertInSnippet(
'<strong>WARNING:</strong> Snippet will go in the "(unknown)"',
response.body, 52
)
def testHiddenUser(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 2)
response = self.request_fetcher.get('/weekly?week=02-27-2012')
self.assertNumSnippets(response.body, 1) # "no snippet this week"
url = '/update_settings?u=user@example.com&is_hidden=yes'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
# Hiding doesn't affect the user-snippets page, just the weekly one.
self.assertNumSnippets(response.body, 2)
response = self.request_fetcher.get('/weekly?week=02-27-2012')
self.assertNumSnippets(response.body, 0)
# And it doesn't affect existing snippets, just empty ones.
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 1)
def testNewSnippetUnhides(self):
url = '/update_snippet?week=02-13-2012&snippet=my+snippet'
self.request_fetcher.get(url)
url = '/update_settings?u=user@example.com&is_hidden=yes'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 0)
url = '/update_snippet?week=02-27-2012&snippet=new+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 1)
response = self.request_fetcher.get('/weekly?week=02-27-2012')
self.assertNumSnippets(response.body, 1)
def testChangingSettingsUnhides(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
url = '/update_settings?u=user@example.com&is_hidden=yes'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-27-2012')
self.assertNumSnippets(response.body, 0)
url = '/update_settings?u=user@example.com'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-27-2012')
self.assertNumSnippets(response.body, 1)
def testHideButton(self):
# First, register the user.
url = '/update_settings?u=user@example.com'
self.request_fetcher.get(url)
url = '/update_snippet?week=02-13-2012&snippet=my+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 1)
# Now hide using the hide button.
url = '/update_settings?u=user@example.com&hide=Hide'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 0)
def testDeleteButton(self):
# First, register the user.
url = '/update_settings?category=dummy'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNotIn('<title>New user</title>', response.body)
url = '/update_settings?u=user@example.com&delete=Delete'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertIn('<title>New user</title>', response.body)
class SetAndViewSnippetsTestCase(UserTestBase):
"""Set some snippets, then make sure they're viewable."""
def testSetAndViewInUserMode(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 2)
self.assertInSnippet('>my snippet<', response.body, 0)
def testSetAndViewInWeeklyMode(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 1)
self.assertInSnippet('>my snippet<', response.body, 0)
def testCannotSeeInOtherWeek(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-13-2012')
self.assertNumSnippets(response.body, 1)
self.assertNotIn('>my snippet<', response.body)
def testViewSnippetsForTwoUsers(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
self.login('other@example.com')
url = '/update_snippet?week=02-20-2012&snippet=other+snippet'
self.request_fetcher.get(url)
# This is done as other
response = self.request_fetcher.get('/')
self.assertIn('other@example.com', response.body)
self.assertNumSnippets(response.body, 2)
self.assertInSnippet('>other snippet<', response.body, 0)
self.assertNotIn('user@example.com', response.body)
self.assertNotIn('>my snippet<', response.body)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 2)
self.assertInSnippet('other@example.com', response.body, 0)
self.assertInSnippet('>other snippet<', response.body, 0)
self.assertInSnippet('user@example.com', response.body, 1)
self.assertInSnippet('>my snippet<', response.body, 1)
# This is done as user
self.login('user@example.com')
response = self.request_fetcher.get('/')
self.assertIn('user@example.com', response.body)
self.assertNumSnippets(response.body, 2)
self.assertInSnippet('>my snippet<', response.body, 0)
self.assertNotIn('other@example.com', response.body)
self.assertNotIn('>other snippet<', response.body)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 2)
self.assertInSnippet('other@example.com', response.body, 0)
self.assertInSnippet('>other snippet<', response.body, 0)
self.assertInSnippet('user@example.com', response.body, 1)
self.assertInSnippet('>my snippet<', response.body, 1)
def testViewSnippetsForTwoWeeks(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
url = '/update_snippet?week=02-27-2012&snippet=my+second+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 3)
# Snippets go in reverse chronological order (i.e. newest first)
self.assertInSnippet('>my second snippet<', response.body, 0)
self.assertInSnippet('>my snippet<', response.body, 1)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 1)
self.assertInSnippet('user@example.com', response.body, 0)
self.assertInSnippet('>my snippet<', response.body, 0)
self.assertNotIn('>my second snippet<', response.body)
response = self.request_fetcher.get('/weekly?week=02-27-2012')
self.assertNumSnippets(response.body, 1)
self.assertInSnippet('user@example.com', response.body, 0)
self.assertInSnippet('>my second snippet<', response.body, 0)
self.assertNotIn('>my snippet<', response.body)
response = self.request_fetcher.get('/weekly?week=02-13-2012')
self.assertNumSnippets(response.body, 1)
self.assertNotIn('>my snippet<', response.body)
self.assertNotIn('>my second snippet<', response.body)
def testViewEmptySnippetsInWeekMode(self):
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
self.login('other@example.com')
url = '/update_snippet?week=02-27-2012&snippet=other+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 2)
# Other-user comes first alphabetically.
self.assertInSnippet('other@example.com', response.body, 0)
self.assertInSnippet('user@example.com', response.body, 1)
self.assertInSnippet('>my snippet<', response.body, 1)
response = self.request_fetcher.get('/weekly?week=02-27-2012')
self.assertNumSnippets(response.body, 2)
self.assertInSnippet('other@example.com', response.body, 0)
self.assertInSnippet('>other snippet<', response.body, 0)
self.assertInSnippet('user@example.com', response.body, 1)
def testViewEmptySnippetsInUserMode(self):
"""Occurs when there's a gap between two snippets."""
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
url = '/update_snippet?week=02-06-2012&snippet=my+old+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 3)
self.assertInSnippet('>my snippet<', response.body, 0)
self.assertInSnippet('>my old snippet<', response.body, 2)
def testCategorizeSnippets(self):
"""Weekly view should sort based on user categories."""
# I give the users numeric names to make it easy to see sorting.
self.login('2@example.com')
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
url = '/update_settings?category=a+1st'
self.request_fetcher.get(url)
self.login('3@example.com')
url = '/update_snippet?week=02-20-2012&snippet=also+snippet'
self.request_fetcher.get(url)
url = '/update_settings?category=a+1st'
self.request_fetcher.get(url)
self.login('1@example.com')
url = '/update_snippet?week=02-20-2012&snippet=late+snippet'
self.request_fetcher.get(url)
url = '/update_settings?category=b+2nd'
self.request_fetcher.get(url)
self.login('4@example.com')
url = '/update_snippet?week=02-20-2012&snippet=late+snippet'
self.request_fetcher.get(url)
# Order should be 4 ((unknown) category), 2 and 3 (a 1st) and
# then 1 (b 2nd).
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 4)
self.assertInSnippet('4@example.com', response.body, 0)
self.assertInSnippet('2@example.com', response.body, 1)
self.assertInSnippet('3@example.com', response.body, 2)
self.assertInSnippet('1@example.com', response.body, 3)
def testViewSnippetAfterAUserIsDeleted(self):
"""When a user is deleted, their snippet should still show up."""
self.login('2@example.com')
url = '/update_snippet?week=02-20-2012&snippet=my+snippet'
self.request_fetcher.get(url)
url = '/update_settings?category=a+1st'
self.request_fetcher.get(url)
# Now delete user 2
u = models.User.all().filter('email =', '2@example.com').get()
u.delete()
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 1)
self.assertInSnippet('2@example.com', response.body, 0)
self.assertTrue('(unknown)' in response.body)
def testWarningsWhenDue(self):
url = '/update_snippet?week=02-06-2012&snippet=old+snippet'
self.request_fetcher.get(url)
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 19)
response = self.request_fetcher.get('/')
self.assertNotIn('Due today', response.body)
self.assertNotIn('OVERDUE', response.body)
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 20)
response = self.request_fetcher.get('/')
self.assertIn('Due today', response.body)
self.assertNotIn('OVERDUE', response.body)
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 21)
response = self.request_fetcher.get('/')
self.assertNotIn('Due today', response.body)
self.assertIn('OVERDUE', response.body)
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 22)
response = self.request_fetcher.get('/')
self.assertNotIn('Due today', response.body)
self.assertIn('OVERDUE', response.body)
def testWarningsWhenNotDue(self):
url = '/update_snippet?week=02-13-2012&snippet=my+snippet'
self.request_fetcher.get(url)
for date in (19, 20, 21, 22):
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, date)
response = self.request_fetcher.get('/')
self.assertNotIn('Due today', response.body)
self.assertNotIn('OVERDUE', response.body)
def testPrettyDateFormatting(self):
# Just so we're not a new user.
url = '/update_snippet?week=02-06-2012&snippet=my+snippet'
self.request_fetcher.get(url)
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 6)
response = self.request_fetcher.get('/')
self.assertIn('February 6, 2012', response.body)
def testUrlize(self):
url = '/update_snippet?week=02-20-2012&snippet=visit+http://foo.com'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/weekly?week=02-20-2012')
self.assertNumSnippets(response.body, 1)
self.assertInSnippet(
'>visit <a href="http://foo.com">http://foo.com</a><',
response.body, 0)
# Also make sure we urlize on the user page.
self.login('2@example.com')
response = self.request_fetcher.get('/?u=user@example.com')
self.assertNumSnippets(response.body, 2)
self.assertInSnippet(
'>visit <a href="http://foo.com">http://foo.com</a><',
response.body, 0)
def testEditMode(self):
url = '/update_snippet?week=02-20-2012&snippet=hello'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/?u=user@example.com')
self.assertIn('Make snippet private', response.body)
response = self.request_fetcher.get('/?u=user@example.com&edit=1')
self.assertIn('Make snippet private', response.body)
response = self.request_fetcher.get('/?u=user@example.com&edit=0')
self.assertNotIn('Make snippet private', response.body)
class ShowCorrectWeekTestCase(UserTestBase):
"""Test we show the right snippets for edit/view based on day of week."""
def setUp(self):
super(ShowCorrectWeekTestCase, self).setUp()
# Register the user so snippet-fetching works.
url = '/update_settings?category=dummy'
self.request_fetcher.get(url)
def testMonday(self):
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 20)
response = self.request_fetcher.get('/')
self.assertInSnippet('February 20, 2012', response.body, 0)
# For *viewing*'s snippets, we always show last week's snippets.
response = self.request_fetcher.get('/weekly')
self.assertIn('February 13, 2012', response.body)
def testTuesday(self):
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 21)
response = self.request_fetcher.get('/')
self.assertInSnippet('February 20, 2012', response.body, 0)
response = self.request_fetcher.get('/weekly')
self.assertIn('February 13, 2012', response.body)
def testWednesday(self):
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 22)
response = self.request_fetcher.get('/')
self.assertInSnippet('February 20, 2012', response.body, 0)
response = self.request_fetcher.get('/weekly')
self.assertIn('February 13, 2012', response.body)
def testThursday(self):
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 23)
response = self.request_fetcher.get('/')
self.assertInSnippet('February 20, 2012', response.body, 0)
response = self.request_fetcher.get('/weekly')
self.assertIn('February 13, 2012', response.body)
def testFriday(self):
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 24)
response = self.request_fetcher.get('/')
self.assertInSnippet('February 20, 2012', response.body, 0)
response = self.request_fetcher.get('/weekly')
self.assertIn('February 13, 2012', response.body)
def testSaturday(self):
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 25)
response = self.request_fetcher.get('/')
self.assertInSnippet('February 20, 2012', response.body, 0)
response = self.request_fetcher.get('/weekly')
self.assertIn('February 13, 2012', response.body)
def testSunday(self):
snippets._TODAY_FN = lambda: datetime.datetime(2012, 2, 26)
response = self.request_fetcher.get('/')
self.assertInSnippet('February 20, 2012', response.body, 0)
response = self.request_fetcher.get('/weekly')
self.assertIn('February 13, 2012', response.body)
class NosnippetGapFillingTestCase(UserTestBase):
"""Test we show correct text when folks miss a week for snippets."""
def testNoSnippets(self):
# If nobody is registered, the user-db will be empty.
response = self.request_fetcher.get('/weekly')
self.assertNumSnippets(response.body, 0)
url = '/update_settings?category=dummy' # register the user
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 2)
def testOneSnippetInDistantPast(self):
url = '/update_snippet?week=02-21-2011&snippet=old+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 53)
self.assertInSnippet('old snippet', response.body, 52)
response = self.request_fetcher.get('/weekly')
self.assertNumSnippets(response.body, 1)
def testTwoSnippetsInDistantPast(self):
url = '/update_snippet?week=08-22-2011&snippet=oldish+snippet'
self.request_fetcher.get(url)
url = '/update_snippet?week=02-21-2011&snippet=old+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 53)
self.assertInSnippet('oldish snippet', response.body, 26)
self.assertInSnippet('old snippet', response.body, 52)
response = self.request_fetcher.get('/weekly')
self.assertNumSnippets(response.body, 1)
def testSnippetInTheFuture(self):
url = '/update_snippet?week=02-18-2013&snippet=future+snippet'
self.request_fetcher.get(url)
response = self.request_fetcher.get('/')
self.assertNumSnippets(response.body, 54)
self.assertInSnippet('future snippet', response.body, 0)
response = self.request_fetcher.get('/weekly')
self.assertNumSnippets(response.body, 1)
def testSnippetInThePastAndFuture(self):
url = '/update_snippet?week=02-21-2011&snippet=old+snippet'
self.request_fetcher.get(url)
url = '/update_snippet?week=02-18-2013&snippet=future+snippet'
self.request_fetcher.get(url)