-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_templating.py
More file actions
1766 lines (1456 loc) · 75.3 KB
/
test_templating.py
File metadata and controls
1766 lines (1456 loc) · 75.3 KB
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
import unittest
import time
from roundup.anypy.cgi_ import FieldStorage, MiniFieldStorage
from roundup.cgi.templating import *
from roundup.cgi.ZTUtils.Iterator import Iterator
from roundup.test import memorydb
from .test_actions import MockNull, true
from .html_norm import NormalizingHtmlParser
import pytest
from .pytest_patcher import mark_class
try:
from markdown2 import __version_info__ as md2__version_info__
except ImportError:
md2__version_info__ = (0,0,0)
if ReStructuredText:
skip_rst = lambda func, *args, **kwargs: func
else:
skip_rst = mark_class(pytest.mark.skip(
reason='ReStructuredText not available'))
import roundup.cgi.templating
if roundup.cgi.templating._import_mistune():
skip_mistune = lambda func, *args, **kwargs: func
else:
skip_mistune = mark_class(pytest.mark.skip(
reason='mistune not available'))
if roundup.cgi.templating._import_markdown2():
skip_markdown2 = lambda func, *args, **kwargs: func
else:
skip_markdown2 = mark_class(pytest.mark.skip(
reason='markdown2 not available'))
if roundup.cgi.templating._import_markdown():
skip_markdown = lambda func, *args, **kwargs: func
else:
skip_markdown = mark_class(pytest.mark.skip(
reason='markdown not available'))
from roundup.anypy.strings import u2s, s2u
from roundup.backends.sessions_common import SessionCommon
class MockConfig(dict):
def __getattr__(self, name):
try:
return self[name]
except KeyError as err:
raise AttributeError(err)
class MockDatabase(MockNull, SessionCommon):
def getclass(self, name):
# limit class names
if name not in [ 'issue', 'user', 'status' ]:
raise KeyError('There is no class called "%s"' % name)
# Class returned must have hasnode(id) method that returns true
# otherwise designators like 'issue1' can't be hyperlinked.
self.classes[name].hasnode = lambda id: True if int(id) < 10 else False
return self.classes[name]
# setup for csrf testing of otks database api
storage = {}
def set(self, key, **props):
MockDatabase.storage[key] = {}
if '__timestamp' not in props:
props['__timestamp'] = time.time() - 7*24*3600
MockDatabase.storage[key].update(props)
def get(self, key, field, default=None):
if key not in MockDatabase.storage:
return default
return MockDatabase.storage[key][field]
def getall(self, key):
if key not in MockDatabase.storage:
return default
return MockDatabase.storage[key]
def exists(self,key):
return key in MockDatabase.storage
def getOTKManager(self):
return MockDatabase()
def lifetime(self, seconds):
return time.time() - 7*24*3600 + seconds
class TemplatingTestCase(unittest.TestCase):
def setUp(self):
self.form = FieldStorage()
self.client = MockNull()
self.client.db = db = MockDatabase()
db.security.hasPermission = lambda *args, **kw: True
self.client.form = self.form
# add client props for testing anti_csrf_nonce
self.client.session_api = MockNull(_sid="1234567890")
self.client.db.getuid = lambda : 10
self.client.db.config = MockConfig (
{'WEB_CSRF_TOKEN_LIFETIME': 10,
'MARKDOWN_BREAK_ON_NEWLINE': False })
class HTMLDatabaseTestCase(TemplatingTestCase):
def test_HTMLDatabase___getitem__(self):
db = HTMLDatabase(self.client)
self.assertTrue(isinstance(db['issue'], HTMLClass))
# following assertions are invalid
# since roundup/cgi/templating.py r1.173.
# HTMLItem is function, not class,
# but HTMLUserClass and HTMLUser are passed on.
# these classes are no more. they have ceased to be.
#self.assertTrue(isinstance(db['user'], HTMLUserClass))
#self.assertTrue(isinstance(db['issue1'], HTMLItem))
#self.assertTrue(isinstance(db['user1'], HTMLUser))
def test_HTMLDatabase___getattr__(self):
db = HTMLDatabase(self.client)
self.assertTrue(isinstance(db.issue, HTMLClass))
# see comment in test_HTMLDatabase___getitem__
#self.assertTrue(isinstance(db.user, HTMLUserClass))
#self.assertTrue(isinstance(db.issue1, HTMLItem))
#self.assertTrue(isinstance(db.user1, HTMLUser))
def test_HTMLDatabase_classes(self):
db = HTMLDatabase(self.client)
db._db.classes = {'issue':MockNull(), 'user': MockNull()}
db.classes()
def test_HTMLDatabase_list(self):
# The list method used to produce a traceback when a None value
# for an order attribute of a class was encountered. This
# happens when the 'get' of the order attribute for a numeric
# id produced a None value. So we put '23' as a key into the
# list and set things up that a None value is returned on 'get'.
# This keeps db.issue static, otherwise it changes for each call
db = MockNull(issue = HTMLDatabase(self.client).issue)
db.issue._klass.list = lambda : ['23', 'a', 'b']
# Make db.getclass return something that has a sensible 'get' method
def get(x, y, allow_abort=True):
return None
mock = MockNull(get = get)
db.issue._db.getclass = lambda x : mock
l = db.issue.list()
class FunctionsTestCase(TemplatingTestCase):
def test_lookupIds(self):
db = HTMLDatabase(self.client)
def lookup(key):
if key == 'ok':
return '1'
if key == 'fail':
raise KeyError('fail')
if key == '@current_user':
raise KeyError('@current_user')
return key
db._db.classes = {'issue': MockNull(lookup=lookup)}
prop = MockNull(classname='issue')
self.assertEqual(lookupIds(db._db, prop, ['1','2']), ['1','2'])
self.assertEqual(lookupIds(db._db, prop, ['ok','2']), ['1','2'])
self.assertEqual(lookupIds(db._db, prop, ['ok', 'fail'], 1),
['1', 'fail'])
self.assertEqual(lookupIds(db._db, prop, ['ok', 'fail']), ['1'])
self.assertEqual(lookupIds(db._db, prop, ['ok', '@current_user']),
['1'])
def test_lookupKeys(self):
db = HTMLDatabase(self.client)
def get(entry, key, allow_abort=True):
return {'1': 'green', '2': 'eggs'}.get(entry, entry)
shrubbery = MockNull(get=get)
db._db.classes = {'shrubbery': shrubbery}
self.assertEqual(lookupKeys(shrubbery, 'spam', ['1','2']),
['green', 'eggs'])
self.assertEqual(lookupKeys(shrubbery, 'spam', ['ok','2']), ['ok',
'eggs'])
class HTMLClassTestCase(TemplatingTestCase) :
def test_link(self):
"""Make sure lookup of a Link property works even in the
presence of multiple values in the form."""
def lookup(key) :
self.assertEqual(key, key.strip())
return "Status%s"%key
self.form.list.append(MiniFieldStorage("issue@status", "1"))
self.form.list.append(MiniFieldStorage("issue@status", "2"))
status = hyperdb.Link("status")
self.client.db.classes = dict \
( issue = MockNull(getprops = lambda : dict(status = status))
, status = MockNull(get = lambda id, name : id, lookup = lookup)
)
self.client.form = self.form
cls = HTMLClass(self.client, "issue")
s = cls["status"]
self.assertEqual(s._value, '1')
def test_link_default(self):
"""Make sure default value for link is returned
if new item and no value in form."""
def lookup(key) :
self.assertEqual(key, key.strip())
return "Status%s"%key
status = hyperdb.Link("status")
# set default_value
status.__dict__['_Type__default_value'] = "4"
self.client.db.classes = dict \
( issue = MockNull(getprops = lambda : dict(status = status))
, status = MockNull(get = lambda id, name : id, lookup = lookup, get_default_value = lambda: 4)
)
self.client.form = self.form
cls = HTMLClass(self.client, "issue")
s = cls["status"]
self.assertEqual(s._value, '4')
def test_link_with_value_and_default(self):
"""Make sure default value is not used if there
is a value in the form."""
def lookup(key) :
self.assertEqual(key, key.strip())
return "Status%s"%key
self.form.list.append(MiniFieldStorage("issue@status", "2"))
self.form.list.append(MiniFieldStorage("issue@status", "1"))
status = hyperdb.Link("status")
# set default_value
status.__dict__['_Type__default_value'] = "4"
self.client.db.classes = dict \
( issue = MockNull(getprops = lambda : dict(status = status))
, status = MockNull(get = lambda id, name : id, lookup = lookup, get_default_value = lambda: 4)
)
self.client.form = self.form
cls = HTMLClass(self.client, "issue")
s = cls["status"]
self.assertEqual(s._value, '2')
def test_multilink(self):
"""`lookup` of an item will fail if leading or trailing whitespace
has not been stripped.
"""
def lookup(key) :
self.assertEqual(key, key.strip())
return "User%s"%key
self.form.list.append(MiniFieldStorage("nosy", "1, 2"))
nosy = hyperdb.Multilink("user")
self.client.db.classes = dict \
( issue = MockNull(getprops = lambda : dict(nosy = nosy))
, user = MockNull(get = lambda id, name : id, lookup = lookup)
)
cls = HTMLClass(self.client, "issue")
cls["nosy"]
def test_anti_csrf_nonce(self):
'''call the csrf creation function and do basic length test
Store the data in a mock db with the same api as the otk
db. Make sure nonce is 54 chars long. Lookup the nonce in
db and retrieve data. Verify that the nonce lifetime is
correct (within 1 second of 1 week - lifetime), the uid is
correct (1), the dummy sid is correct.
Consider three cases:
* create nonce via module function setting lifetime
* create nonce via TemplatingUtils method setting lifetime
* create nonce via module function with default lifetime
'''
# the value below is number of seconds in a week.
week_seconds = 604800
otks=self.client.db.getOTKManager()
for test in [ 'module', 'template', 'default_time' ]:
print("Testing:", test)
if test == 'module':
# test the module function
nonce1 = anti_csrf_nonce(self.client, lifetime=1)
# lifetime * 60 is the offset
greater_than = week_seconds - 1 * 60
elif test == 'template':
# call the function through the TemplatingUtils class
cls = TemplatingUtils(self.client)
nonce1 = cls.anti_csrf_nonce(lifetime=5)
greater_than = week_seconds - 5 * 60
elif test == 'default_time':
# use the module function but with no lifetime
nonce1 = anti_csrf_nonce(self.client)
# see above for web nonce lifetime.
greater_than = week_seconds - 10 * 60
self.assertEqual(len(nonce1), 54)
uid = otks.get(nonce1, 'uid', default=None)
sid = otks.get(nonce1, 'sid', default=None)
timestamp = otks.get(nonce1, '__timestamp', default=None)
self.assertEqual(uid, 10)
self.assertEqual(sid, self.client.session_api._sid)
now = time.time()
print("now, timestamp, greater, difference",
now, timestamp, greater_than, now - timestamp)
# lower bound of the difference is above. Upper bound
# of difference is run time between time.time() in
# the call to anti_csrf_nonce and the time.time() call
# that assigns ts above. I declare that difference
# to be less than 1 second for this to pass.
self.assertEqual(True,
greater_than <= now - timestamp < (greater_than + 1) )
def test_number__int__(self):
# test with number
p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
2345678.2345678)
self.assertEqual(p.__int__(), 2345678)
property = MockNull(get_default_value = lambda: None)
p = NumberHTMLProperty(self.client, 'testnum', '1', property,
'test', None)
with self.assertRaises(TypeError) as e:
p.__int__()
def test_number__float__(self):
# test with number
p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
2345678.2345678)
self.assertEqual(p.__float__(), 2345678.2345678)
property = MockNull(get_default_value = lambda: None)
p = NumberHTMLProperty(self.client, 'testnum', '1', property,
'test', None)
with self.assertRaises(TypeError) as e:
p.__float__()
def test_number_field(self):
import sys
_py3 = sys.version_info[0] > 2
# python2 truncates while python3 rounds. Sigh.
if _py3:
expected_val = 2345678.2345678
else:
expected_val = 2345678.23457
# test with number
p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
2345678.2345678)
self.client.db.config['WEB_USE_BROWSER_NUMBER_INPUT'] = False
self.assertEqual(p.field(),
('<input id="testnum1@test" name="testnum1@test" '
'size="30" type="text" value="%s">')%expected_val)
self.client.db.config['WEB_USE_BROWSER_NUMBER_INPUT'] = True
self.assertEqual(p.field(),
('<input id="testnum1@test" name="testnum1@test" '
'size="30" type="number" value="%s">')%expected_val)
self.assertEqual(p.field(size=10),
('<input id="testnum1@test" name="testnum1@test" '
'size="10" type="number" value="%s">')%expected_val)
self.assertEqual(p.field(size=10, dataprop="foo", dataprop2=5),
('<input dataprop="foo" dataprop2="5" '
'id="testnum1@test" name="testnum1@test" '
'size="10" type="number" '
'value="%s">'%expected_val))
self.assertEqual(p.field(size=10, klass="class1",
**{ "class": "class2 class3",
"data-prop": "foo",
"data-prop2": 5}),
('<input class="class2 class3" data-prop="foo" '
'data-prop2="5" id="testnum1@test" '
'klass="class1" '
'name="testnum1@test" size="10" type="number" '
'value="%s">')%expected_val)
# get plain representation if user can't edit
p.is_edit_ok = lambda: False
self.assertEqual(p.field(), p.plain())
# test with string which is wrong type
p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
"234567e.2345678")
self.assertEqual(p.field(),
('<input id="testnum1@test" name="testnum1@test" '
'size="30" type="number" value="234567e.2345678">'))
# test with None value, pretend property.__default_value = Null which
# is the default. It would be returned by get_default_value
# which I mock.
property = MockNull(get_default_value = lambda: None)
p = NumberHTMLProperty(self.client, 'testnum', '1', property,
'test', None)
self.assertEqual(p.field(),
('<input id="testnum1@test" name="testnum1@test" '
'size="30" type="number" value="">'))
def test_number_plain(self):
import sys
_py3 = sys.version_info[0] > 2
# python2 truncates while python3 rounds. Sigh.
if _py3:
expected_val = 2345678.2345678
else:
expected_val = 2345678.23457
p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
2345678.2345678)
self.assertEqual(p.plain(), "%s"%expected_val)
def test_number_pretty(self):
# test with number
p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
2345678.2345678)
self.assertEqual(p.pretty(), "2345678.235")
# test with string which is wrong type
p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
"2345678.2345678")
self.assertEqual(p.pretty(), "2345678.2345678")
# test with boolean
p = NumberHTMLProperty(self.client, 'testnum', '1', None, 'test',
True)
self.assertEqual(p.pretty(), "1.000")
# test with None value, pretend property.__default_value = Null which
# is the default. It would be returned by get_default_value
# which I mock.
property = MockNull(get_default_value = lambda: None)
p = NumberHTMLProperty(self.client, 'testnum', '1', property,
'test', None)
self.assertEqual(p.pretty(), '')
with self.assertRaises(ValueError) as e:
p.pretty('%0.3')
def test_string_url_quote(self):
''' test that urlquote quotes the string '''
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', 'test string< foo@bar')
self.assertEqual(p.url_quote(), 'test%20string%3C%20foo%40bar')
def test_string_email(self):
''' test that email obscures the email '''
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', 'rouilj@foo.example.com')
self.assertEqual(p.email(), 'rouilj at foo example ...')
def test_string_wrapped(self):
test_string = ('A long string that needs to be wrapped to'
' 80 characters and no more. Put in a link issue1.'
' Put in <html> to be escaped. Put in a'
' https://example.com/link as well. Let us see if'
' it will wrap properly.' )
test_result_wrap = {}
test_result_wrap[80] = ('A long string that needs to be wrapped to 80'
' characters and no more. Put in a\n'
'link <a href="issue1">issue1</a>. Put in'
' <html> to be escaped. Put in a <a'
' href="https://example.com/link"'
' rel="nofollow noopener">'
'https://example.com/link</a> as\n'
'well. Let us see if it will wrap properly.')
test_result_wrap[20] = (
'A long string that\n'
'needs to be wrapped\n'
'to 80 characters and\n'
'no more. Put in a\nlink <a href="issue1">issue1</a>. Put in\n'
'<html> to be\n'
'escaped. Put in a\n'
'<a href="https://example.com/link" rel="nofollow '
'noopener">https://example.com/link</a>\n'
'as well. Let us see\n'
'if it will wrap\n'
'properly.')
test_result_wrap[100] = (
'A long string that needs to be wrapped to 80 characters and no more. Put in a link <a href="issue1">issue1</a>. Put in\n'
'<html> to be escaped. Put in a <a href="https://example.com/link" rel="nofollow noopener">https://example.com/link</a> as well. Let us see if it will wrap\n'
'properly.')
p = StringHTMLProperty(self.client, 'test', '1', None, 'test',
test_string)
for i in [80, 20, 100]:
wrapped = p.wrapped(columns=i)
print(wrapped)
self.assertEqual(wrapped, test_result_wrap[i])
def test_string_plain_or_hyperlinked(self):
''' test that email obscures the email '''
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', 'A string <b> with rouilj@example.com embedded < html</b>')
self.assertEqual(p.plain(), 'A string <b> with rouilj@example.com embedded < html</b>')
self.assertEqual(p.plain(escape=1), 'A string <b> with rouilj@example.com embedded &lt; html</b>')
self.assertEqual(p.plain(hyperlink=1), 'A string <b> with <a href="mailto:rouilj@example.com">rouilj@example.com</a> embedded &lt; html</b>')
self.assertEqual(p.plain(escape=1, hyperlink=1), 'A string <b> with <a href="mailto:rouilj@example.com">rouilj@example.com</a> embedded &lt; html</b>')
self.assertEqual(p.hyperlinked(), 'A string <b> with <a href="mailto:rouilj@example.com">rouilj@example.com</a> embedded &lt; html</b>')
# check designators
for designator in [ "issue1", "issue 1" ]:
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', designator)
self.assertEqual(p.hyperlinked(),
'<a href="issue1">%s</a>'%designator)
# issue 100 > 10 which is a magic number for the mocked hasnode
# If id number is greater than 10 hasnode reports it does not have
# the node.
for designator in ['issue100', 'issue 100']:
p = StringHTMLProperty(self.client, 'test', '1', None, 'test',
designator)
self.assertEqual(p.hyperlinked(), designator)
# zoom class does not exist
for designator in ['zoom1', 'zoom100', 'zoom 1']:
p = StringHTMLProperty(self.client, 'test', '1', None, 'test',
designator)
self.assertEqual(p.hyperlinked(), designator)
@skip_rst
def test_string_rst(self):
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'A string with cmeerw@example.com *embedded* \u00df'))
# test case to make sure include directive is disabled
q = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'\n\n.. include:: XyZrMt.html\n\n<badtag>\n\n'))
q_result=u'''<div class="document">
<div class="system-message">
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils"><string></tt>, line 3)</p>
<p>"include" directive disabled.</p>
<pre class="literal-block">
.. include:: XyZrMt.html
</pre>
</div>
<p><badtag></p>
</div>
'''
# test case to make sure raw directive is disabled
r = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'\n\n.. raw:: html\n\n <badtag>\n\n'))
r_result='''<div class="document">
<div class="system-message">
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils"><string></tt>, line 3)</p>
<p>"raw" directive disabled.</p>
<pre class="literal-block">
.. raw:: html
<badtag>
</pre>
</div>
</div>
'''
# test case to make sure javascript and data url's aren't turned
# into links
s = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'<badtag>\njavascript:badcode data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=='))
s_result = '<div class="document">\n<p><badtag>\njavascript:badcode data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==</p>\n</div>\n'
# test url recognition
t = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'link is https://example.com/link for testing.'))
t_result = '<div class="document">\n<p>link is <a class="reference external" href="https://example.com/link">https://example.com/link</a> for testing.</p>\n</div>\n'
# test text that doesn't need to be processed
u = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'Just a plain old string here. Nothig to process.'))
u_result = '<div class="document">\n<p>Just a plain old string here. Nothig to process.</p>\n</div>\n'
self.assertEqual(p.rst(), u2s(u'<div class="document">\n<p>A string with <a class="reference external" href="mailto:cmeerw@example.com">cmeerw@example.com</a> <em>embedded</em> \u00df</p>\n</div>\n'))
self.assertEqual(q.rst(), u2s(q_result))
self.assertEqual(r.rst(), u2s(r_result))
self.assertEqual(s.rst(), u2s(s_result))
self.assertEqual(t.rst(), u2s(t_result))
self.assertEqual(u.rst(), u2s(u_result))
def test_string_field(self):
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', 'A string <b> with rouilj@example.com embedded < html</b>')
self.assertEqual(p.field(), '<input id="test1@test" name="test1@test" size="30" type="text" value="A string <b> with rouilj@example.com embedded &lt; html</b>">')
def test_string_multiline(self):
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', 'A string <b> with rouilj@example.com embedded < html</b>')
self.assertEqual(p.multiline(), '<textarea name="test1@test" id="test1@test" rows="5" cols="40">A string <b> with rouilj@example.com embedded &lt; html</b></textarea>')
self.assertEqual(p.multiline(rows=300, cols=100, **{'class':'css_class'}), '<textarea class="css_class" name="test1@test" id="test1@test" rows="300" cols="100">A string <b> with rouilj@example.com embedded &lt; html</b></textarea>')
def test_url_match(self):
'''Test the URL regular expression in StringHTMLProperty.
'''
def t(s, nothing=False, **groups):
m = StringHTMLProperty.hyper_re.search(s)
if nothing:
if m:
self.assertEqual(m, None, '%r matched (%r)'%(s, m.groupdict()))
return
else:
self.assertNotEqual(m, None, '%r did not match'%s)
d = m.groupdict()
for g in groups:
self.assertEqual(d[g], groups[g], '%s %r != %r in %r'%(g, d[g],
groups[g], s))
#t('123.321.123.321', 'url')
t('https://example.com/demo/issue8#24MRV9BZYx:V:1B~sssssssssssssss~4~4', url="https://example.com/demo/issue8#24MRV9BZYx:V:1B~sssssssssssssss~4~4")
t('http://localhost/', url='http://localhost/')
t('http://roundup.net/', url='http://roundup.net/')
t('http://richard@localhost/', url='http://richard@localhost/')
t('http://richard:sekrit@localhost/',
url='http://richard:sekrit@localhost/')
t('<HTTP://roundup.net/>', url='HTTP://roundup.net/')
t('www.a.ex', url='www.a.ex')
t('foo.a.ex', nothing=True)
t('StDevValidTimeSeries.GetObservation', nothing=True)
t('http://a.ex', url='http://a.ex')
t('http://a.ex/?foo&bar=baz\\.@!$%()qwerty',
url='http://a.ex/?foo&bar=baz\\.@!$%()qwerty')
t('www.foo.net', url='www.foo.net')
t('richard@com.example', email='richard@com.example')
t('r@a.com', email='r@a.com')
t('i1', **{'class':'i', 'id':'1'})
t('item123', **{'class':'item', 'id':'123'})
t('item 123', **{'class':'item', 'id':'123'})
t('www.user:pass@host.net', email='pass@host.net')
t('user:pass@www.host.net', url='user:pass@www.host.net')
t('123.35', nothing=True)
t('-.3535', nothing=True)
def test_url_replace(self):
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', '')
def t(s): return p.hyper_re.sub(p._hyper_repl, s)
ae = self.assertEqual
ae(t('issue5#msg10'), '<a href="issue5#msg10">issue5#msg10</a>')
ae(t('issue5'), '<a href="issue5">issue5</a>')
ae(t('issue2255'), 'issue2255')
ae(t('foo https://example.com/demo/issue8#24MRV9BZYx:V:1B~sssssssssssssss~4~4 bar'),
'foo <a href="https://example.com/demo/issue8#24MRV9BZYx:V:1B~sssssssssssssss~4~4" rel="nofollow noopener">'
'https://example.com/demo/issue8#24MRV9BZYx:V:1B~sssssssssssssss~4~4</a> bar')
ae(t('item123123123123'), 'item123123123123')
ae(t('http://roundup.net/'),
'<a href="http://roundup.net/" rel="nofollow noopener">http://roundup.net/</a>')
ae(t('<HTTP://roundup.net/>'),
'<<a href="HTTP://roundup.net/" rel="nofollow noopener">HTTP://roundup.net/</a>>')
ae(t('<http://roundup.net/>.'),
'<<a href="http://roundup.net/" rel="nofollow noopener">http://roundup.net/</a>>.')
ae(t('<www.roundup.net>'),
'<<a href="http://www.roundup.net" rel="nofollow noopener">www.roundup.net</a>>')
ae(t('(www.roundup.net)'),
'(<a href="http://www.roundup.net" rel="nofollow noopener">www.roundup.net</a>)')
ae(t('foo http://msdn.microsoft.com/en-us/library/ms741540(VS.85).aspx bar'),
'foo <a href="http://msdn.microsoft.com/en-us/library/ms741540(VS.85).aspx" rel="nofollow noopener">'
'http://msdn.microsoft.com/en-us/library/ms741540(VS.85).aspx</a> bar')
ae(t('(e.g. http://en.wikipedia.org/wiki/Python_(programming_language))'),
'(e.g. <a href="http://en.wikipedia.org/wiki/Python_(programming_language)" rel="nofollow noopener">'
'http://en.wikipedia.org/wiki/Python_(programming_language)</a>)')
ae(t('(e.g. http://en.wikipedia.org/wiki/Python_(programming_language)).'),
'(e.g. <a href="http://en.wikipedia.org/wiki/Python_(programming_language)" rel="nofollow noopener">'
'http://en.wikipedia.org/wiki/Python_(programming_language)</a>).')
ae(t('(e.g. http://en.wikipedia.org/wiki/Python_(programming_language))>.'),
'(e.g. <a href="http://en.wikipedia.org/wiki/Python_(programming_language)" rel="nofollow noopener">'
'http://en.wikipedia.org/wiki/Python_(programming_language)</a>)>.')
ae(t('(e.g. http://en.wikipedia.org/wiki/Python_(programming_language>)).'),
'(e.g. <a href="http://en.wikipedia.org/wiki/Python_(programming_language" rel="nofollow noopener">'
'http://en.wikipedia.org/wiki/Python_(programming_language</a>>)).')
for c in '.,;:!':
# trailing punctuation is not included
ae(t('http://roundup.net/%c ' % c),
'<a href="http://roundup.net/" rel="nofollow noopener">http://roundup.net/</a>%c ' % c)
# trailing punctuation is not included without trailing space
ae(t('http://roundup.net/%c' % c),
'<a href="http://roundup.net/" rel="nofollow noopener">http://roundup.net/</a>%c' % c)
# but it's included if it's part of the URL
ae(t('http://roundup.net/%c/' % c),
'<a href="http://roundup.net/%c/" rel="nofollow noopener">http://roundup.net/%c/</a>' % (c, c))
# including with a non / terminated path
ae(t('http://roundup.net/test%c ' % c),
'<a href="http://roundup.net/test" rel="nofollow noopener">http://roundup.net/test</a>%c ' % c)
# but it's included if it's part of the URL path
ae(t('http://roundup.net/%ctest' % c),
'<a href="http://roundup.net/%ctest" rel="nofollow noopener">http://roundup.net/%ctest</a>' % (c, c))
def test_input_html4(self):
# boolean attributes are just the attribute name
# indicate with attr=None or attr="attr"
# e.g. disabled
input=input_html4(required=None, size=30)
self.assertEqual(input, '<input required size="30" type="text">')
input=input_html4(required="required", size=30)
self.assertEqual(input, '<input required="required" size="30" type="text">')
attrs={"required": None, "class": "required", "size": 30}
input=input_html4(**attrs)
self.assertEqual(input, '<input class="required" required size="30" type="text">')
attrs={"disabled": "disabled", "class": "required", "size": 30}
input=input_html4(**attrs)
self.assertEqual(input, '<input class="required" disabled="disabled" size="30" type="text">')
class HTMLPropertyTestClass(unittest.TestCase):
def setUp(self):
self.form = FieldStorage()
self.client = MockNull()
self.client.db = db = memorydb.create('admin')
db.tx_Source = "web"
db.issue.addprop(tx_Source=hyperdb.String())
db.security.hasPermission = lambda *args, **kw: True
self.client.form = self.form
self.client._props = MockNull()
# add client props for testing anti_csrf_nonce
self.client.session_api = MockNull(_sid="1234567890")
self.client.db.getuid = lambda : 10
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
self._caplog = caplog
class BooleanHTMLPropertyTestCase(HTMLPropertyTestClass):
def setUp(self):
super(BooleanHTMLPropertyTestCase, self).setUp()
db = self.client.db
db.issue.addprop(boolvalt=hyperdb.Boolean())
db.issue.addprop(boolvalf=hyperdb.Boolean())
db.issue.addprop(boolvalunset=hyperdb.Boolean())
self.client.db.issue.create(title="title",
boolvalt = True,
boolvalf = False)
def tearDown(self):
self.client.db.close()
memorydb.db_nuke('')
testdata = [
("boolvalt", "Yes", True, False),
("boolvalf", "No", False, True),
("boolvalunset", "", False, True),
]
def test_BoolHTMLRadioButtons(self):
#propname = "boolvalt"
#plainval = "Yes"
self.maxDiff = None
for test_inputs in self.testdata:
params = {
"check1": 'checked="checked" ' if test_inputs[2] else "",
"check2": 'checked="checked" ' if test_inputs[3] else "",
"propname": test_inputs[0],
"plainval": test_inputs[1],
}
test_hyperdbBoolean = self.client.db.issue.getprops("1")[
params['propname']]
test_boolean = self.client.db.issue.get("1", params['propname'])
# client, classname, nodeid, prop, name, value,
# anonymous=0, offset=None
d = BooleanHTMLProperty(self.client, 'issue', '1',
test_hyperdbBoolean,
params['propname'], test_boolean)
self.assertIsInstance(d._value, (type(None), bool))
self.assertEqual(d.plain(), params['plainval'])
input_expected = (
'<input %(check1)sid="issue1@%(propname)s_yes" '
'name="issue1@%(propname)s" type="radio" value="yes">'
'<label class="rblabel" for="issue1@%(propname)s_yes">'
'Yes</label>'
'<input %(check2)sid="issue1@%(propname)s_no" name="issue1@%(propname)s" '
'type="radio" value="no"><label class="rblabel" '
'for="issue1@%(propname)s_no">No</label>') % params
self.assertEqual(d.field(), input_expected)
y_label = ( '<label class="rblabel" for="issue1@%(propname)s_yes">'
'True</label>') % params
n_label = ('<label class="rblabel" '
'for="issue1@%(propname)s_no">False</label>') % params
u_label = ('<label class="rblabel" '
'for="issue1@%(propname)s_unk">Ignore</label>') % params
input_expected = (
'<input %(check1)sid="issue1@%(propname)s_yes" '
'name="issue1@%(propname)s" type="radio" value="yes">'
+ y_label +
'<input %(check2)sid="issue1@%(propname)s_no" name="issue1@%(propname)s" '
'type="radio" value="no">' + n_label ) % params
self.assertEqual(d.field(y_label=y_label, n_label=n_label), input_expected)
input_expected = (
'<label class="rblabel" for="issue1@%(propname)s_yes">'
'Yes</label>'
'<input %(check1)sid="issue1@%(propname)s_yes" '
'name="issue1@%(propname)s" type="radio" value="yes">'
'<label class="rblabel" '
'for="issue1@%(propname)s_no">No</label>'
'<input %(check2)sid="issue1@%(propname)s_no" '
'name="issue1@%(propname)s" '
'type="radio" value="no">') % params
print(d.field(labelfirst=True))
self.assertEqual(d.field(labelfirst=True), input_expected)
input_expected = (
'<label class="rblabel" for="issue1@%(propname)s_unk">'
'Ignore</label>'
'<input id="issue1@%(propname)s_unk" '
'name="issue1@%(propname)s" type="radio" value="">'
'<input %(check1)sid="issue1@%(propname)s_yes" '
'name="issue1@%(propname)s" type="radio" value="yes">'
'<label class="rblabel" for="issue1@%(propname)s_yes">'
'Yes</label>'
'<input %(check2)sid="issue1@%(propname)s_no" name="issue1@%(propname)s" '
'type="radio" value="no"><label class="rblabel" '
'for="issue1@%(propname)s_no">No</label>') % params
self.assertEqual(d.field(u_label=u_label), input_expected)
# one test with the last d is enough.
# check permissions return
is_view_ok_orig = d.is_view_ok
is_edit_ok_orig = d.is_edit_ok
no_access = lambda : False
d.is_view_ok = no_access
self.assertEqual(d.plain(), "[hidden]")
d.is_edit_ok = no_access
self.assertEqual(d.field(), "[hidden]")
d.is_view_ok = is_view_ok_orig
self.assertEqual(d.field(), params['plainval'])
d.is_edit_ok = is_edit_ok_orig
class DateHTMLPropertyTestCase(HTMLPropertyTestClass):
def setUp(self):
super(DateHTMLPropertyTestCase, self).setUp()
db = self.client.db
db.issue.addprop(deadline=hyperdb.Date())
self.test_datestring = "2021-01-01.11:22:10"
self.client.db.issue.create(title="title",
deadline=date.Date(self.test_datestring))
self.client.db.getUserTimezone = lambda: "2"
def tearDown(self):
self.client.db.close()
memorydb.db_nuke('')
def exp_classhelp(self, cls='issue', prop='deadline', dlm='.'):
value = dlm.join (('2021-01-01', '11:22:10'))
return ('<a class="classhelp" data-calurl="%(cls)s?'
'@template=calendar&property=%(prop)s&'
'form=itemSynopsis&date=%(value)s" '
'data-height="200" data-width="300" href="javascript:help_window'
'(\'%(cls)s?@template=calendar&property=%(prop)s&'
'form=itemSynopsis&date=%(value)s\', 300, 200)">(cal)</a>'
) % {'cls': cls, 'prop': prop, 'value': value}
def test_DateHTMLWithDate(self):
"""Test methods when DateHTMLProperty._value is a hyperdb.Date()
"""
self.client.db.config['WEB_USE_BROWSER_DATE_INPUT'] = True
test_datestring = self.test_datestring
test_Date = self.client.db.issue.get("1", 'deadline')
test_hyperdbDate = self.client.db.issue.getprops("1")['deadline']
self.client.classname = "issue"
self.client.template = "item"
# client, classname, nodeid, prop, name, value,
# anonymous=0, offset=None
d = DateHTMLProperty(self.client, 'issue', '1', test_hyperdbDate,
'deadline', test_Date)
self.assertIsInstance(d._value, date.Date)
self.assertEqual(d.pretty(), " 1 January 2021")
self.assertEqual(d.pretty("%2d %B %Y"), "01 January 2021")
self.assertEqual(d.pretty(format="%Y-%m"), "2021-01")
self.assertEqual(d.plain(), "2021-01-01.13:22:10")
self.assertEqual(d.local("-4").plain(), "2021-01-01.07:22:10")
input_expected = """<input id="issue1@deadline" name="issue1@deadline" size="30" type="date" value="2021-01-01">"""
self.assertEqual(d.field(display_time=False), input_expected)
input_expected = '<input id="issue1@deadline" name="issue1@deadline" '\
'size="30" type="datetime-local" value="2021-01-01T13:22:10">'
self.assertEqual(d.field(), input_expected)
input_expected = '<input id="issue1@deadline" name="issue1@deadline" '\
'size="30" type="text" value="2021-01-01.13:22:10">'
field = d.field(format='%Y-%m-%d.%H:%M:%S', popcal=False)
self.assertEqual(field, input_expected)
# test with format
input_expected = '<input id="issue1@deadline" name="issue1@deadline" '\
'size="30" type="text" value="2021-01">' + self.exp_classhelp()
self.assertEqual(d.field(format="%Y-%m"), input_expected)
input_expected = '<input id="issue1@deadline" name="issue1@deadline" '\
'size="30" type="text" value="2021-01">'
input = d.field(format="%Y-%m", popcal=False)
self.assertEqual(input, input_expected)
def test_DateHTMLWithText(self):
"""Test methods when DateHTMLProperty._value is a string
rather than a hyperdb.Date()
"""
test_datestring = "2021-01-01 11:22:10"
test_date = hyperdb.Date("2")
self.form.list.append(MiniFieldStorage("test1@test", test_datestring))
self.client._props=test_date
self.client.db.config['WEB_USE_BROWSER_DATE_INPUT'] = False
self.client.db.classes = dict \
( test = MockNull(getprops = lambda : test_date)
)
self.client.classname = "test"
self.client.template = "item"
# client, classname, nodeid, prop, name, value,
# anonymous=0, offset=None
d = DateHTMLProperty(self.client, 'test', '1', self.client._props,
'test', '')
self.assertIs(type(d._value), str)
self.assertEqual(d.pretty(), "2021-01-01 11:22:10")
self.assertEqual(d.plain(), "2021-01-01 11:22:10")
input_expected = '<input id="test1@test" name="test1@test" size="30" '\
'type="text" value="2021-01-01 11:22:10">'
self.assertEqual(d.field(popcal=False), input_expected)
self.client.db.config['WEB_USE_BROWSER_DATE_INPUT'] = True
input_expected = '<input id="test1@test" name="test1@test" size="30" '\
'type="datetime-local" value="2021-01-01 11:22:10">'
self.assertEqual(d.field(), input_expected)
self.client.db.config['WEB_USE_BROWSER_DATE_INPUT'] = False
input_expected = '<input id="test1@test" name="test1@test" size="40" '\
'type="text" value="2021-01-01 11:22:10">'
self.assertEqual(d.field(size=40, popcal=False), input_expected)
input_expected = ('<input id="test1@test" name="test1@test" size="30" '
'type="text" value="2021-01-01 11:22:10">'
+ self.exp_classhelp(cls='test', prop='test', dlm=' '))
self.maxDiff=None
self.assertEqual(d.field(format="%Y-%m"), input_expected)
# format always uses type="text" even when date input is set
self.client.db.config['WEB_USE_BROWSER_DATE_INPUT'] = True
result = d.field(format="%Y-%m-%d", popcal=False)
input_expected = '<input id="test1@test" name="test1@test" size="30" '\
'type="text" value="2021-01-01 11:22:10">'
self.assertEqual(result, input_expected)
input_expected = ('<input id="test1@test" name="test1@test" size="30" '
'type="text" value="2021-01-01 11:22:10">'
+ self.exp_classhelp(cls='test', prop='test', dlm=' '))
self.assertEqual(d.field(format="%Y-%m"), input_expected)
result = d.field(format="%Y-%m-%dT%H:%M:%S", popcal=False)
input_expected = '<input id="test1@test" name="test1@test" size="30" '\
'type="text" value="2021-01-01 11:22:10">'
self.assertEqual(result, input_expected)
# common markdown test cases
class MarkdownTests:
def mangleMarkdown2(self, s):
''' markdown2's rel=nofollow support on 'a' tags isn't programmable.
So we are using it's builtin nofollow support. Mangle the string