Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 99812cf

Browse files
fix @ROLE support in apijson-put; add 7 test cases
1 parent b27ad9b commit 99812cf

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

‎tests/demo/apps/apijson_demo/settings.ini‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ user = {
1010
"secret_fields" : ["password"],
1111
"GET" : { "roles" : ["LOGIN","ADMIN","OWNER"] },
1212
"HEAD" : { "roles" : ["LOGIN","ADMIN","OWNER"] },
13-
"POST" : { "roles" : ["ADMIN"] },
13+
#"POST" : { "roles" : ["ADMIN"] }, #remove for test case
1414
"PUT" : { "roles" : ["ADMIN","OWNER"] },
1515
"DELETE" : { "roles" : ["ADMIN"] },
1616
}
@@ -63,7 +63,7 @@ comment = {
6363
"POST" :{
6464
"ADD" :{"@role": "OWNER"},
6565
"DISALLOW" : ["id"],
66-
"NECESSARY" : ["content"]
66+
"NECESSARY" : ["moment_id","content"]
6767
},
6868
"PUT" :{
6969
"ADD":{"@role": "OWNER"},

‎tests/test.py‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,3 +922,106 @@ def test_apijson_head():
922922
>>> print(d)
923923
{'code': 400, 'msg': "'moment' don't have field 'nonexist'"}
924924
"""
925+
926+
def test_apijson_post():
927+
"""
928+
>>> application = make_simple_application(project_dir='.')
929+
>>> handler = application.handler()
930+
931+
>>> #apijson post, without @tag
932+
>>> data ='''{
933+
... "moment": {
934+
... "content": "new moment for test",
935+
... "picture_list": ["http://static.oschina.net/uploads/user/48/96331_50.jpg"]
936+
... }
937+
... }'''
938+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("admin"), middlewares=[])
939+
>>> d = json_loads(r.data)
940+
>>> print(d)
941+
{'code': 400, 'msg': "'tag' parameter is needed"}
942+
943+
>>> #apijson post
944+
>>> data ='''{
945+
... "moment": {
946+
... "content": "new moment for test",
947+
... "picture_list": ["http://static.oschina.net/uploads/user/48/96331_50.jpg"]
948+
... },
949+
... "@tag": "moment"
950+
... }'''
951+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("admin"), middlewares=[])
952+
>>> d = json_loads(r.data)
953+
>>> del d['moment']['date']
954+
>>> print(d)
955+
{'code': 200, 'msg': 'success', 'moment': {'user_id': 1, 'content': 'new moment for test', 'picture_list': ['http://static.oschina.net/uploads/user/48/96331_50.jpg'], 'id': 4, 'code': 200, 'message': 'success'}}
956+
957+
>>> #apijson post to a non exist model
958+
>>> data ='''{
959+
... "nonexist": {
960+
... "content": "new moment for test",
961+
... "picture_list": ["http://static.oschina.net/uploads/user/48/96331_50.jpg"]
962+
... },
963+
... "@tag": "nonexist"
964+
... }'''
965+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("admin"), middlewares=[])
966+
>>> d = json_loads(r.data)
967+
>>> print(d)
968+
{'code': 400, 'msg': "model 'nonexist' not found"}
969+
970+
>>> #apijson post, tag is model which not define in APIJSON_REQUESTS
971+
>>> data ='''{
972+
... "moment": {
973+
... "content": "new moment for test",
974+
... "picture_list": ["http://static.oschina.net/uploads/user/48/96331_50.jpg"]
975+
... },
976+
... "@tag": "role"
977+
... }'''
978+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("admin"), middlewares=[])
979+
>>> d = json_loads(r.data)
980+
>>> print(d)
981+
{'code': 400, 'msg': "tag 'role' not found"}
982+
983+
>>> #apijson post, tag is model which not define in APIJSON_REQUESTS
984+
>>> data ='''{
985+
... "user": {
986+
... "username": "test"
987+
... },
988+
... "@tag": "user"
989+
... }'''
990+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("admin"), middlewares=[])
991+
>>> d = json_loads(r.data)
992+
>>> print(d)
993+
{'code': 400, 'msg': "tag 'user' not found"}
994+
"""
995+
996+
def test_apijson_put():
997+
"""
998+
>>> application = make_simple_application(project_dir='.')
999+
>>> handler = application.handler()
1000+
1001+
>>> #apijson put
1002+
>>> data ='''{
1003+
... "moment": {
1004+
... "id": 1,
1005+
... "content": "moment content after change"
1006+
... },
1007+
... "@tag": "moment"
1008+
... }'''
1009+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1010+
>>> d = json_loads(r.data)
1011+
>>> print(d)
1012+
{'code': 200, 'msg': 'success', 'moment': {'id': 1, 'code': 200, 'msg': 'success', 'count': 1}}
1013+
1014+
>>> #apijson put, with @role
1015+
>>> data ='''{
1016+
... "moment": {
1017+
... "@role": "ADMIN",
1018+
... "id": 1,
1019+
... "content": "moment content after change 2"
1020+
... },
1021+
... "@tag": "moment"
1022+
... }'''
1023+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("admin"), middlewares=[])
1024+
>>> d = json_loads(r.data)
1025+
>>> print(d)
1026+
{'code': 200, 'msg': 'success', 'moment': {'id': 1, 'code': 200, 'msg': 'success', 'count': 1}}
1027+
"""

‎uliweb_apijson/apijson/views.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ def _put_one(self,key,tag):
542542
for k in params:
543543
if k=="id":
544544
continue
545+
elif k[0]=="@":
546+
continue
545547
elif hasattr(obj,k):
546548
kwargs[k] = params[k]
547549
else:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /