|
1 | | -import unittest |
2 | 1 | from unittest import mock |
3 | 2 |
|
4 | 3 | import tableauserverclient.server.request_factory as factory |
5 | 4 | from tableauserverclient.helpers.strings import redact_xml |
6 | 5 | from tableauserverclient.filesys_helpers import to_filename, make_download_path |
7 | 6 |
|
8 | 7 |
|
9 | | -class BugFix257(unittest.TestCase): |
10 | | - def test_empty_request_works(self): |
11 | | - result = factory.EmptyRequest().empty_req() |
12 | | - self.assertEqual(b"<tsRequest />", result) |
13 | | - |
14 | | - |
15 | | -class FileSysHelpers(unittest.TestCase): |
16 | | - def test_to_filename(self): |
17 | | - invalid = [ |
18 | | - "23brhafbjrjhkbbea.txt", |
19 | | - "a_b_C.txt", |
20 | | - "windows space.txt", |
21 | | - "abc#def.txt", |
22 | | - "t@bL3A()", |
23 | | - ] |
24 | | - |
25 | | - valid = [ |
26 | | - "23brhafbjrjhkbbea.txt", |
27 | | - "a_b_C.txt", |
28 | | - "windows space.txt", |
29 | | - "abcdef.txt", |
30 | | - "tbL3A", |
31 | | - ] |
32 | | - |
33 | | - self.assertTrue(all([(to_filename(i) == v) for i, v in zip(invalid, valid)])) |
34 | | - |
35 | | - def test_make_download_path(self): |
36 | | - no_file_path = (None, "file.ext") |
37 | | - has_file_path_folder = ("/root/folder/", "file.ext") |
38 | | - has_file_path_file = ("outx", "file.ext") |
39 | | - |
40 | | - self.assertEqual("file.ext", make_download_path(*no_file_path)) |
41 | | - self.assertEqual("outx.ext", make_download_path(*has_file_path_file)) |
42 | | - |
43 | | - with mock.patch("os.path.isdir") as mocked_isdir: |
44 | | - mocked_isdir.return_value = True |
45 | | - self.assertEqual("/root/folder/file.ext", make_download_path(*has_file_path_folder)) |
46 | | - |
47 | | - |
48 | | -class LoggingTest(unittest.TestCase): |
49 | | - def test_redact_password_string(self): |
50 | | - redacted = redact_xml( |
51 | | - "<?xml version='1.0'?><workbook><password>this is password: my_super_secret_passphrase_which_nobody_should_ever_see password: value</password></workbook>" |
52 | | - ) |
53 | | - assert redacted.find("value") == -1 |
54 | | - assert redacted.find("secret") == -1 |
55 | | - assert redacted.find("ever_see") == -1 |
56 | | - assert redacted.find("my_super_secret_passphrase_which_nobody_should_ever_see") == -1 |
57 | | - |
58 | | - def test_redact_password_bytes(self): |
59 | | - redacted = redact_xml( |
60 | | - b"<?xml version='1.0'?><datasource><data-connection name='con-artist' password='value string with at least a password: valuesecret or two in it'/></datasource>" |
61 | | - ) |
62 | | - assert redacted.find(b"value") == -1 |
63 | | - assert redacted.find(b"secret") == -1 |
64 | | - |
65 | | - def test_redact_password_with_special_char(self): |
66 | | - redacted = redact_xml( |
67 | | - "<?xml version='1.0'?><content><safe_text value='this is a nondescript text line which is public' password='my_s per_secre>_passphrase_which_nobody_should_ever_see with password: value'> </safe_text></content>" |
68 | | - ) |
69 | | - assert redacted.find("my_s per_secre>_passphrase_which_nobody_should_ever_see with password: value") == -1 |
70 | | - |
71 | | - def test_redact_password_not_xml(self): |
72 | | - redacted = redact_xml( |
73 | | - "<content><safe_text value='this is a nondescript text line which is public' password='my_s per_secre>_passphrase_which_nobody_should_ever_see with password: value'> </safe_text></content>" |
74 | | - ) |
75 | | - assert redacted.find("my_s per_secre>_passphrase_which_nobody_should_ever_see") == -1 |
76 | | - |
77 | | - def test_redact_password_really_not_xml(self): |
78 | | - redacted = redact_xml( |
79 | | - "value='this is a nondescript text line which is public' password='my_s per_secre>_passphrase_which_nobody_should_ever_see with password: value and then a cookie " |
80 | | - ) |
81 | | - assert redacted.find("my_s per_secre>_passphrase_which_nobody_should_ever_see") == -1 |
82 | | - assert redacted.find("passphrase") == -1, redacted |
83 | | - assert redacted.find("cookie") == -1, redacted |
| 8 | +def test_empty_request_works(): |
| 9 | + result = factory.EmptyRequest().empty_req() |
| 10 | + assert b"<tsRequest />" == result |
| 11 | + |
| 12 | + |
| 13 | +def test_to_filename(): |
| 14 | + invalid = [ |
| 15 | + "23brhafbjrjhkbbea.txt", |
| 16 | + "a_b_C.txt", |
| 17 | + "windows space.txt", |
| 18 | + "abc#def.txt", |
| 19 | + "t@bL3A()", |
| 20 | + ] |
| 21 | + |
| 22 | + valid = [ |
| 23 | + "23brhafbjrjhkbbea.txt", |
| 24 | + "a_b_C.txt", |
| 25 | + "windows space.txt", |
| 26 | + "abcdef.txt", |
| 27 | + "tbL3A", |
| 28 | + ] |
| 29 | + |
| 30 | + assert all([(to_filename(i) == v) for i, v in zip(invalid, valid)]) |
| 31 | + |
| 32 | + |
| 33 | +def test_make_download_path(): |
| 34 | + no_file_path = (None, "file.ext") |
| 35 | + has_file_path_folder = ("/root/folder/", "file.ext") |
| 36 | + has_file_path_file = ("outx", "file.ext") |
| 37 | + |
| 38 | + assert "file.ext" == make_download_path(*no_file_path) |
| 39 | + assert "outx.ext" == make_download_path(*has_file_path_file) |
| 40 | + |
| 41 | + with mock.patch("os.path.isdir") as mocked_isdir: |
| 42 | + mocked_isdir.return_value = True |
| 43 | + assert "/root/folder/file.ext" == make_download_path(*has_file_path_folder) |
| 44 | + |
| 45 | + |
| 46 | +def test_redact_password_string(): |
| 47 | + redacted = redact_xml( |
| 48 | + "<?xml version='1.0'?><workbook><password>this is password: my_super_secret_passphrase_which_nobody_should_ever_see password: value</password></workbook>" |
| 49 | + ) |
| 50 | + assert redacted.find("value") == -1 |
| 51 | + assert redacted.find("secret") == -1 |
| 52 | + assert redacted.find("ever_see") == -1 |
| 53 | + assert redacted.find("my_super_secret_passphrase_which_nobody_should_ever_see") == -1 |
| 54 | + |
| 55 | + |
| 56 | +def test_redact_password_bytes(): |
| 57 | + redacted = redact_xml( |
| 58 | + b"<?xml version='1.0'?><datasource><data-connection name='con-artist' password='value string with at least a password: valuesecret or two in it'/></datasource>" |
| 59 | + ) |
| 60 | + assert redacted.find(b"value") == -1 |
| 61 | + assert redacted.find(b"secret") == -1 |
| 62 | + |
| 63 | + |
| 64 | +def test_redact_password_with_special_char(): |
| 65 | + redacted = redact_xml( |
| 66 | + "<?xml version='1.0'?><content><safe_text value='this is a nondescript text line which is public' password='my_s per_secre>_passphrase_which_nobody_should_ever_see with password: value'> </safe_text></content>" |
| 67 | + ) |
| 68 | + assert redacted.find("my_s per_secre>_passphrase_which_nobody_should_ever_see with password: value") == -1 |
| 69 | + |
| 70 | + |
| 71 | +def test_redact_password_not_xml(): |
| 72 | + redacted = redact_xml( |
| 73 | + "<content><safe_text value='this is a nondescript text line which is public' password='my_s per_secre>_passphrase_which_nobody_should_ever_see with password: value'> </safe_text></content>" |
| 74 | + ) |
| 75 | + assert redacted.find("my_s per_secre>_passphrase_which_nobody_should_ever_see") == -1 |
| 76 | + |
| 77 | + |
| 78 | +def test_redact_password_really_not_xml(): |
| 79 | + redacted = redact_xml( |
| 80 | + "value='this is a nondescript text line which is public' password='my_s per_secre>_passphrase_which_nobody_should_ever_see with password: value and then a cookie " |
| 81 | + ) |
| 82 | + assert redacted.find("my_s per_secre>_passphrase_which_nobody_should_ever_see") == -1 |
| 83 | + assert redacted.find("passphrase") == -1, redacted |
| 84 | + assert redacted.find("cookie") == -1, redacted |
0 commit comments