|
1 | 1 | import logging |
2 | | -import unittest |
3 | 2 | from unittest.mock import * |
4 | 3 | import io |
5 | 4 |
|
|
8 | 7 | import tableauserverclient as TSC |
9 | 8 |
|
10 | 9 |
|
11 | | -class UserModelTests(unittest.TestCase): |
12 | | - def test_invalid_auth_setting(self): |
13 | | - user = TSC.UserItem("me", TSC.UserItem.Roles.Publisher) |
14 | | - with self.assertRaises(ValueError): |
15 | | - user.auth_setting = "Hello" |
16 | | - |
17 | | - def test_invalid_site_role(self): |
18 | | - user = TSC.UserItem("me", TSC.UserItem.Roles.Publisher) |
19 | | - with self.assertRaises(ValueError): |
20 | | - user.site_role = "Hello" |
21 | | - |
22 | | - |
23 | | -class UserDataTest(unittest.TestCase): |
24 | | - logger = logging.getLogger("UserDataTest") |
25 | | - |
26 | | - role_inputs = [ |
27 | | - ["creator", "system", "yes", "SiteAdministrator"], |
28 | | - ["None", "system", "no", "SiteAdministrator"], |
29 | | - ["explorer", "SysTEm", "no", "SiteAdministrator"], |
30 | | - ["creator", "site", "yes", "SiteAdministratorCreator"], |
31 | | - ["explorer", "site", "yes", "SiteAdministratorExplorer"], |
32 | | - ["creator", "SITE", "no", "SiteAdministratorCreator"], |
33 | | - ["creator", "none", "yes", "Creator"], |
34 | | - ["explorer", "none", "yes", "ExplorerCanPublish"], |
35 | | - ["viewer", "None", "no", "Viewer"], |
36 | | - ["explorer", "no", "yes", "ExplorerCanPublish"], |
37 | | - ["EXPLORER", "noNO", "yes", "ExplorerCanPublish"], |
38 | | - ["explorer", "no", "no", "Explorer"], |
39 | | - ["unlicensed", "none", "no", "Unlicensed"], |
40 | | - ["Chef", "none", "yes", "Unlicensed"], |
41 | | - ["yes", "yes", "yes", "Unlicensed"], |
42 | | - ] |
43 | | - |
44 | | - valid_import_content = [ |
45 | | - "username, pword, fname, creator, site, yes, email", |
46 | | - "username, pword, fname, explorer, none, no, email", |
47 | | - "", |
48 | | - "u", |
49 | | - "p", |
50 | | - ] |
51 | | - |
52 | | - valid_username_content = ["jfitzgerald@tableau.com"] |
53 | | - |
54 | | - usernames = [ |
55 | | - "valid", |
56 | | - "valid@email.com", |
57 | | - "domain/valid", |
58 | | - "domain/valid@tmail.com", |
59 | | - "va!@#$%^&*()lid", |
60 | | - "in@v@lid", |
61 | | - "in valid", |
62 | | - "", |
63 | | - ] |
64 | | - |
65 | | - def test_validate_usernames(self): |
66 | | - TSC.UserItem.validate_username_or_throw(UserDataTest.usernames[0]) |
67 | | - TSC.UserItem.validate_username_or_throw(UserDataTest.usernames[1]) |
68 | | - TSC.UserItem.validate_username_or_throw(UserDataTest.usernames[2]) |
69 | | - TSC.UserItem.validate_username_or_throw(UserDataTest.usernames[3]) |
70 | | - TSC.UserItem.validate_username_or_throw(UserDataTest.usernames[4]) |
71 | | - with self.assertRaises(AttributeError): |
72 | | - TSC.UserItem.validate_username_or_throw(UserDataTest.usernames[5]) |
73 | | - with self.assertRaises(AttributeError): |
74 | | - TSC.UserItem.validate_username_or_throw(UserDataTest.usernames[6]) |
75 | | - |
76 | | - def test_evaluate_role(self): |
77 | | - for line in UserDataTest.role_inputs: |
78 | | - actual = TSC.UserItem.CSVImport._evaluate_site_role(line[0], line[1], line[2]) |
79 | | - assert actual == line[3], line + [actual] |
80 | | - |
81 | | - def test_get_user_detail_empty_line(self): |
82 | | - test_line = "" |
83 | | - test_user = TSC.UserItem.CSVImport.create_user_from_line(test_line) |
84 | | - assert test_user is None |
85 | | - |
86 | | - def test_get_user_detail_standard(self): |
87 | | - test_line = "username, pword, fname, license, admin, pub, email" |
88 | | - test_user: TSC.UserItem = TSC.UserItem.CSVImport.create_user_from_line(test_line) |
89 | | - assert test_user.name == "username", test_user.name |
90 | | - assert test_user.fullname == "fname", test_user.fullname |
91 | | - assert test_user.site_role == "Unlicensed", test_user.site_role |
92 | | - assert test_user.email == "email", test_user.email |
93 | | - |
94 | | - def test_get_user_details_only_username(self): |
95 | | - test_line = "username" |
96 | | - test_user: TSC.UserItem = TSC.UserItem.CSVImport.create_user_from_line(test_line) |
97 | | - |
98 | | - def test_populate_user_details_only_some(self): |
99 | | - values = "username, , , creator, admin" |
100 | | - user = TSC.UserItem.CSVImport.create_user_from_line(values) |
101 | | - assert user.name == "username" |
102 | | - |
103 | | - def test_validate_user_detail_standard(self): |
104 | | - test_line = "username, pword, fname, creator, site, 1, email" |
105 | | - TSC.UserItem.CSVImport._validate_import_line_or_throw(test_line, UserDataTest.logger) |
106 | | - TSC.UserItem.CSVImport.create_user_from_line(test_line) |
107 | | - |
108 | | - # for file handling |
109 | | - def _mock_file_content(self, content: list[str]) -> io.TextIOWrapper: |
110 | | - # the empty string represents EOF |
111 | | - # the tests run through the file twice, first to validate then to fetch |
112 | | - mock = MagicMock(io.TextIOWrapper) |
113 | | - content.append("") # EOF |
114 | | - mock.readline.side_effect = content |
115 | | - mock.name = "file-mock" |
116 | | - return mock |
117 | | - |
118 | | - def test_validate_import_file(self): |
119 | | - test_data = self._mock_file_content(UserDataTest.valid_import_content) |
120 | | - valid, invalid = TSC.UserItem.CSVImport.validate_file_for_import(test_data, UserDataTest.logger) |
121 | | - assert valid == 2, f"Expected two lines to be parsed, got {valid}" |
122 | | - assert invalid == [], f"Expected no failures, got {invalid}" |
123 | | - |
124 | | - def test_validate_usernames_file(self): |
125 | | - test_data = self._mock_file_content(UserDataTest.usernames) |
126 | | - valid, invalid = TSC.UserItem.CSVImport.validate_file_for_import(test_data, UserDataTest.logger) |
127 | | - assert valid == 5, f"Exactly 5 of the lines were valid, counted {valid + invalid}" |
| 10 | +def test_invalid_auth_setting(): |
| 11 | + user = TSC.UserItem("me", TSC.UserItem.Roles.Publisher) |
| 12 | + with pytest.raises(ValueError): |
| 13 | + user.auth_setting = "Hello" |
| 14 | + |
| 15 | + |
| 16 | +def test_invalid_site_role(): |
| 17 | + user = TSC.UserItem("me", TSC.UserItem.Roles.Publisher) |
| 18 | + with pytest.raises(ValueError): |
| 19 | + user.site_role = "Hello" |
| 20 | + |
| 21 | + |
| 22 | +logger = logging.getLogger("UserModelTest") |
| 23 | + |
| 24 | + |
| 25 | +role_inputs = [ |
| 26 | + ["creator", "system", "yes", "SiteAdministrator"], |
| 27 | + ["None", "system", "no", "SiteAdministrator"], |
| 28 | + ["explorer", "SysTEm", "no", "SiteAdministrator"], |
| 29 | + ["creator", "site", "yes", "SiteAdministratorCreator"], |
| 30 | + ["explorer", "site", "yes", "SiteAdministratorExplorer"], |
| 31 | + ["creator", "SITE", "no", "SiteAdministratorCreator"], |
| 32 | + ["creator", "none", "yes", "Creator"], |
| 33 | + ["explorer", "none", "yes", "ExplorerCanPublish"], |
| 34 | + ["viewer", "None", "no", "Viewer"], |
| 35 | + ["explorer", "no", "yes", "ExplorerCanPublish"], |
| 36 | + ["EXPLORER", "noNO", "yes", "ExplorerCanPublish"], |
| 37 | + ["explorer", "no", "no", "Explorer"], |
| 38 | + ["unlicensed", "none", "no", "Unlicensed"], |
| 39 | + ["Chef", "none", "yes", "Unlicensed"], |
| 40 | + ["yes", "yes", "yes", "Unlicensed"], |
| 41 | +] |
| 42 | + |
| 43 | +valid_import_content = [ |
| 44 | + "username, pword, fname, creator, site, yes, email", |
| 45 | + "username, pword, fname, explorer, none, no, email", |
| 46 | + "", |
| 47 | + "u", |
| 48 | + "p", |
| 49 | +] |
| 50 | + |
| 51 | +valid_username_content = ["jfitzgerald@tableau.com"] |
| 52 | + |
| 53 | +usernames = [ |
| 54 | + "valid", |
| 55 | + "valid@email.com", |
| 56 | + "domain/valid", |
| 57 | + "domain/valid@tmail.com", |
| 58 | + "va!@#$%^&*()lid", |
| 59 | + "in@v@lid", |
| 60 | + "in valid", |
| 61 | + "", |
| 62 | +] |
| 63 | + |
| 64 | + |
| 65 | +def test_validate_usernames() -> None: |
| 66 | + TSC.UserItem.validate_username_or_throw(usernames[0]) |
| 67 | + TSC.UserItem.validate_username_or_throw(usernames[1]) |
| 68 | + TSC.UserItem.validate_username_or_throw(usernames[2]) |
| 69 | + TSC.UserItem.validate_username_or_throw(usernames[3]) |
| 70 | + TSC.UserItem.validate_username_or_throw(usernames[4]) |
| 71 | + with pytest.raises(AttributeError): |
| 72 | + TSC.UserItem.validate_username_or_throw(usernames[5]) |
| 73 | + with pytest.raises(AttributeError): |
| 74 | + TSC.UserItem.validate_username_or_throw(usernames[6]) |
| 75 | + |
| 76 | + |
| 77 | +def test_evaluate_role() -> None: |
| 78 | + for line in role_inputs: |
| 79 | + actual = TSC.UserItem.CSVImport._evaluate_site_role(line[0], line[1], line[2]) |
| 80 | + assert actual == line[3], line + [actual] |
| 81 | + |
| 82 | + |
| 83 | +def test_get_user_detail_empty_line() -> None: |
| 84 | + test_line = "" |
| 85 | + test_user = TSC.UserItem.CSVImport.create_user_from_line(test_line) |
| 86 | + assert test_user is None |
| 87 | + |
| 88 | + |
| 89 | +def test_get_user_detail_standard() -> None: |
| 90 | + test_line = "username, pword, fname, license, admin, pub, email" |
| 91 | + test_user = TSC.UserItem.CSVImport.create_user_from_line(test_line) |
| 92 | + assert test_user is not None |
| 93 | + assert test_user.name == "username", test_user.name |
| 94 | + assert test_user.fullname == "fname", test_user.fullname |
| 95 | + assert test_user.site_role == "Unlicensed", test_user.site_role |
| 96 | + assert test_user.email == "email", test_user.email |
| 97 | + |
| 98 | + |
| 99 | +def test_get_user_details_only_username() -> None: |
| 100 | + test_line = "username" |
| 101 | + test_user = TSC.UserItem.CSVImport.create_user_from_line(test_line) |
| 102 | + |
| 103 | + |
| 104 | +def test_populate_user_details_only_some() -> None: |
| 105 | + values = "username, , , creator, admin" |
| 106 | + user = TSC.UserItem.CSVImport.create_user_from_line(values) |
| 107 | + assert user is not None |
| 108 | + assert user.name == "username" |
| 109 | + |
| 110 | + |
| 111 | +def test_validate_user_detail_standard() -> None: |
| 112 | + test_line = "username, pword, fname, creator, site, 1, email" |
| 113 | + TSC.UserItem.CSVImport._validate_import_line_or_throw(test_line, logger) |
| 114 | + TSC.UserItem.CSVImport.create_user_from_line(test_line) |
| 115 | + |
| 116 | + |
| 117 | +# for file handling |
| 118 | +def _mock_file_content(content: list[str]) -> io.TextIOWrapper: |
| 119 | + # the empty string represents EOF |
| 120 | + # the tests run through the file twice, first to validate then to fetch |
| 121 | + mock = MagicMock(io.TextIOWrapper) |
| 122 | + content.append("") # EOF |
| 123 | + mock.readline.side_effect = content |
| 124 | + mock.name = "file-mock" |
| 125 | + return mock |
| 126 | + |
| 127 | + |
| 128 | +def test_validate_import_file() -> None: |
| 129 | + test_data = _mock_file_content(valid_import_content) |
| 130 | + valid, invalid = TSC.UserItem.CSVImport.validate_file_for_import(test_data, logger) |
| 131 | + assert valid == 2, f"Expected two lines to be parsed, got {valid}" |
| 132 | + assert invalid == [], f"Expected no failures, got {invalid}" |
| 133 | + |
| 134 | + |
| 135 | +def test_validate_usernames_file() -> None: |
| 136 | + test_data = _mock_file_content(usernames) |
| 137 | + valid, invalid = TSC.UserItem.CSVImport.validate_file_for_import(test_data, logger) |
| 138 | + assert valid == 5, f"Exactly 5 of the lines were valid, counted {valid + len(invalid)}" |
0 commit comments