8000 Fix for issue #50 Add regex validator to content_url (#64) · gmiretti/server-client-python@4f4112b · GitHub
[go: up one dir, main page]

Skip to content < 8000 /span>

Commit 4f4112b

Browse files
authored
Fix for issue tableau#50 Add regex validator to content_url (tableau#64)
Content_urls must contain only a subset of ascii characters (alphanumeric, -, _) and will fail on site creation otherwise. Server sends an error but it can be confusing to the user. Fix is to validate it in the library so we never send an invalid content_url * Added a property decorator that validates the content_url against a supplied regex * Updated tests to test new cases (with unicode!) and added a new test for 'valid' cases to exercise regex
1 parent 03d26cd commit 4f4112b

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

tableauserverclient/models/property_decorators.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from functools import wraps
23

34

@@ -84,3 +85,17 @@ def wrapper(self, value):
8485
return wrapper
8586

8687
return property_type_decorator
88+
89+
90+
def property_matches(regex_to_match, error):
91+
92+
compiled_re = re.compile(regex_to_match)
93+
94+
def wrapper(func):
95+
@wraps(func)
96+
def validate_regex_decorator(self, value):
97+
if not compiled_re.match(value):
98+
raise ValueError(error)
99+
return func(self, value)
100+
return validate_regex_decorator
101+
return wrapper

tableauserverclient/models/site_item.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import xml.etree.ElementTree as ET
2-
from .property_decorators import property_is_enum, property_is_boolean, property_not_empty, property_not_nullable
2+
from .property_decorators import (property_is_enum, property_is_boolean, property_matches,
3+
property_not_empty, property_not_nullable)
34
from .. import NAMESPACE
45

56

7+
VALID_CONTENT_URL_RE = r"^[a-zA-Z0-9_\-]*$"
8+
9+
610
class SiteItem(object):
711
class AdminMode:
812
ContentAndUsers = 'ContentAndUsers'
@@ -45,6 +49,7 @@ def content_url(self):
4549

4650
@content_url.setter
4751
@property_not_nullable
52+
@property_matches(VALID_CONTENT_URL_RE, "content_url can contain only letters, numbers, dashes, and underscores")
4853
def content_url(self, value):
4954
self._content_url = value
5055

test/test_site_model.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# coding=utf-8
2+
13
import unittest
24
import tableauserverclient as TSC
35

@@ -19,10 +21,21 @@ def test_invalid_admin_mode(self):
1921
site.admin_mode = "Hello"
2022

2123
def test_invalid_content_url(self):
22-
self.assertRaises(ValueError, TSC.SiteItem, "site", None)
23-
site = TSC.SiteItem("site", "url")
24+
2425
with self.assertRaises(ValueError):
25-
site.content_url = None
26+
site = TSC.SiteItem(name="蚵仔煎", content_url="蚵仔煎")
27+
28+
with self.assertRaises(ValueError):
29+
site = TSC.SiteItem(name="蚵仔煎", content_url=None)
30+
31+
def test_set_valid_content_url(self):
32+
# Default Site
33+
site = TSC.SiteItem(name="Default", content_url="")
34+
self.assertEqual(site.content_url, "")
35+
36+
# Unicode Name and ascii content_url
37+
site = TSC.SiteItem(name="蚵仔煎", content_url="omlette")
38+
self.assertEqual(site.content_url, "omlette")
2639

2740
def test_invalid_disable_subscriptions(self):
2841
site = TSC.SiteItem("site", "url")

0 commit comments

Comments
 (0)
0