Skip to content

Commit ef3cd06

Browse files
committed
Added a few comments and a test that fails with the pre-patched code
and uses POST.
1 parent 1372db1 commit ef3cd06

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

roundup/rest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,9 @@ class SimulateFieldStorageFromJson():
16091609
16101610
references used when accessing a FieldStorage structure.
16111611
1612-
That's what this class does.
1612+
That's what this class does with all names and values as native
1613+
strings. Note that json is UTF-8, so we convert any unicode to
1614+
string.
16131615
16141616
'''
16151617
def __init__(self, json_string):
@@ -1625,10 +1627,13 @@ class FsValue:
16251627
def __init__(self, name, val):
16261628
self.name=u2s(name)
16271629
if is_us(val):
1630+
# handle most common type first
16281631
self.value=u2s(val)
16291632
elif type(val) == type([]):
1633+
# then lists of strings
16301634
self.value = [ u2s(v) for v in val ]
16311635
else:
1636+
# then stringify anything else (int, float)
16321637
self.value = str(val)
16331638

16341639
def __getitem__(self, index):

test/rest_common.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def setUp(self):
5353
self.db.tx_Source = 'web'
5454

5555
self.db.issue.addprop(tx_Source=hyperdb.String())
56+
self.db.issue.addprop(anint=hyperdb.Integer())
57+
self.db.issue.addprop(afloat=hyperdb.Number())
58+
self.db.issue.addprop(abool=hyperdb.Boolean())
5659
self.db.msg.addprop(tx_Source=hyperdb.String())
5760

5861
self.db.post_init()
@@ -697,14 +700,74 @@ def testEtagProcessing(self):
697700
else:
698701
self.assertEqual(self.dummy_client.response_code, 412)
699702

703+
def testDispatchPost(self):
704+
"""
705+
run POST through rest dispatch(). This also tests
706+
sending json payload through code as dispatch is the
707+
code that changes json payload into something we can
708+
process.
709+
"""
710+
711+
# TEST #0
712+
# POST: issue make joe assignee and admin and demo as
713+
# nosy
714+
# simulate: /rest/data/issue
715+
body=b'{ "title": "Joe Doe has problems", \
716+
"nosy": [ "1", "3" ], \
717+
"assignedto": "2", \
718+
"abool": true, \
719+
"afloat": 2.3, \
720+
"anint": 567890 \
721+
}'
722+
env = { "CONTENT_TYPE": "application/json",
723+
"CONTENT_LENGTH": len(body),
724+
"REQUEST_METHOD": "POST"
725+
}
726+
headers={"accept": "application/json; version=1",
727+
"content-type": env['CONTENT_TYPE'],
728+
"content-length": env['CONTENT_LENGTH'],
729+
}
730+
self.headers=headers
731+
# we need to generate a FieldStorage the looks like
732+
# FieldStorage(None, None, 'string') rather than
733+
# FieldStorage(None, None, [])
734+
body_file=BytesIO(body) # FieldStorage needs a file
735+
form = client.BinaryFieldStorage(body_file,
736+
headers=headers,
737+
environ=env)
738+
self.server.client.request.headers.get=self.get_header
739+
results = self.server.dispatch(env["REQUEST_METHOD"],
740+
"/rest/data/issue",
741+
form)
742+
743+
print(results)
744+
self.assertEqual(self.server.client.response_code, 201)
745+
json_dict = json.loads(b2s(results))
746+
self.assertEqual(json_dict['data']['link'],
747+
"http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1")
748+
self.assertEqual(json_dict['data']['id'], "1")
749+
results = self.server.dispatch('GET',
750+
"/rest/data/issue/1", self.empty_form)
751+
print(results)
752+
json_dict = json.loads(b2s(results))
753+
self.assertEqual(json_dict['data']['link'],
754+
"http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1")
755+
self.assertEqual(json_dict['data']['attributes']['abool'], True)
756+
self.assertEqual(json_dict['data']['attributes']['afloat'], 2.3)
757+
self.assertEqual(json_dict['data']['attributes']['anint'], 567890)
758+
self.assertEqual(len(json_dict['data']['attributes']['nosy']), 3)
759+
self.assertEqual(json_dict['data']['attributes']\
760+
['assignedto']['link'],
761+
"http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2")
762+
763+
700764
def testDispatch(self):
701765
"""
702766
run changes through rest dispatch(). This also tests
703767
sending json payload through code as dispatch is the
704768
code that changes json payload into something we can
705769
process.
706770
"""
707-
708771
# TEST #1
709772
# PUT: joe's 'realname' using json data.
710773
# simulate: /rest/data/user/<id>/realname

0 commit comments

Comments
 (0)