Mercurial > p > roundup > code
comparison roundup/rest.py @ 8218:32aaf5dc562b
fix(REST): issue2551383; improve errors for bad json, fix PUT docs
While adding fuzz testing for email addresses via REST
/rest/data/user/1/address, I had an error when setting the address to
the same value it currently had. Traced this to a bug in
userauditor.py. Fixed the bug. Documented in upgrading.txt.
While trying to track down issue, I realized invalid json was being
accepted without error. So I fixed the code that parses the json and
have it return an error. Also modified some tests that broke (used
invalid json, or passed body (e.g. DELETE) but shouldn't have. Add
tests for bad json to verify new code.
Fixed test that wasn't initializing the body_file in each loop, so the
test wasn't actually supplying a body.
Also realised PUT documentation was not correct. Output format isn't
quite like GET.
Fuss tests for email address also added.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 17 Dec 2024 19:42:46 -0500 |
| parents | 14e92a595828 |
| children | 818751637b77 |
comparison
equal
deleted
inserted
replaced
| 8217:cd76d5d59c37 | 8218:32aaf5dc562b |
|---|---|
| 2727 strings. Note that json is UTF-8, so we convert any unicode to | 2727 strings. Note that json is UTF-8, so we convert any unicode to |
| 2728 string. | 2728 string. |
| 2729 | 2729 |
| 2730 ''' | 2730 ''' |
| 2731 def __init__(self, json_string): | 2731 def __init__(self, json_string): |
| 2732 ''' Parse the json string into an internal dict. ''' | 2732 '''Parse the json string into an internal dict. |
| 2733 | |
| 2734 Because spec for rest post once exactly (POE) shows | |
| 2735 posting empty content. An empty string results in an empty | |
| 2736 dict, not a json parse error. | |
| 2737 ''' | |
| 2733 def raise_error_on_constant(x): | 2738 def raise_error_on_constant(x): |
| 2734 raise ValueError("Unacceptable number: %s" % x) | 2739 raise ValueError("Unacceptable number: %s" % x) |
| 2740 if json_string == "": | |
| 2741 self.json_dict = {} | |
| 2742 self.value = None | |
| 2743 return | |
| 2744 | |
| 2735 try: | 2745 try: |
| 2736 self.json_dict = json.loads(json_string, | 2746 self.json_dict = json.loads(json_string, |
| 2737 parse_constant=raise_error_on_constant) | 2747 parse_constant=raise_error_on_constant) |
| 2738 self.value = [self.FsValue(index, self.json_dict[index]) | 2748 self.value = [self.FsValue(index, self.json_dict[index]) |
| 2739 for index in self.json_dict] | 2749 for index in self.json_dict] |
| 2740 except ValueError: | 2750 except (json.decoder.JSONDecodeError, ValueError) as e: |
| 2741 self.json_dict = {} | 2751 raise ValueError(e.args[0] + ". JSON is: " + json_string) |
| 2742 self.value = None | 2752 |
| 2743 | 2753 |
| 2744 class FsValue: | 2754 class FsValue: |
| 2745 '''Class that does nothing but response to a .value property ''' | 2755 '''Class that does nothing but response to a .value property ''' |
| 2746 def __init__(self, name, val): | 2756 def __init__(self, name, val): |
| 2747 self.name = u2s(name) | 2757 self.name = u2s(name) |
