Skip to content

Commit bb4eeb0

Browse files
committed
tests: add 15 tests for apijson_put
1 parent cbee292 commit bb4eeb0

File tree

4 files changed

+225
-3
lines changed

4 files changed

+225
-3
lines changed

Diff for: tests/demo/apps/apijson_demo/models.py

+4
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ class Comment(Model):
3131
class PublicNotice(Model):
3232
date = Field(datetime.datetime, auto_now_add=True)
3333
content = Field(TEXT)
34+
35+
class NoRequestTag(Model):
36+
user_id = Reference("user")
37+
content = Field(TEXT)

Diff for: tests/demo/apps/apijson_demo/settings.ini

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ privacy = 'apijson_demo.models.Privacy'
33
comment = 'apijson_demo.models.Comment'
44
moment = 'apijson_demo.models.Moment'
55
publicnotice = 'apijson_demo.models.PublicNotice'
6+
norequesttag = 'apijson_demo.models.NoRequestTag'
67

78
[APIJSON_MODELS]
89
user = {
@@ -42,7 +43,7 @@ publicnotice = {
4243
"GET" : { "roles" : ["OWNER","LOGIN","ADMIN","UNKNOWN"] },
4344
"HEAD" : { "roles" : ["OWNER","LOGIN","ADMIN","UNKNOWN"] },
4445
"POST" : { "roles" : ["OWNER","ADMIN"] },
45-
"PUT" : { "roles" : ["OWNER","ADMIN"] },
46+
"PUT" : { "roles" : ["OWNER","ADMIN","UNKNOWN"] },
4647
"DELETE" : { "roles" : ["OWNER","ADMIN"] },
4748
}
4849

@@ -68,5 +69,12 @@ comment = {
6869
"PUT" :{
6970
"ADD":{"@role": "OWNER"},
7071
"NECESSARY" : ["id","content"],
72+
"DISALLOW" : ["user_id","to_id"],
7173
},
7274
}
75+
76+
publicnotice = {
77+
"PUT" :{
78+
"NECESSARY" : ["id","content"],
79+
}
80+
}

Diff for: tests/test.py

+209
Original file line numberDiff line numberDiff line change
@@ -1024,4 +1024,213 @@ def test_apijson_put():
10241024
>>> d = json_loads(r.data)
10251025
>>> print(d)
10261026
{'code': 200, 'msg': 'success', 'moment': {'id': 1, 'code': 200, 'msg': 'success', 'count': 1}}
1027+
1028+
>>> #apijson put, model not exists
1029+
>>> data ='''{
1030+
... "nonexist": {
1031+
... "@role": "ADMIN",
1032+
... "id": 1,
1033+
... "content": "moment content after change 2"
1034+
... },
1035+
... "@tag": "nonexist"
1036+
... }'''
1037+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("admin"), middlewares=[])
1038+
>>> d = json_loads(r.data)
1039+
>>> print(d)
1040+
{'code': 400, 'msg': "model 'nonexist' not found"}
1041+
1042+
>>> #apijson put, with a model no tag
1043+
>>> data ='''{
1044+
... "norequesttag": {
1045+
... "user_id": 1,
1046+
... "content": "test"
1047+
... },
1048+
... "@tag": "norequesttag"
1049+
... }'''
1050+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1051+
>>> d = json_loads(r.data)
1052+
>>> print(d)
1053+
{'code': 400, 'msg': "tag 'norequesttag' not found"}
1054+
1055+
>>> #apijson put, test ADD in put
1056+
>>> data ='''{
1057+
... "moment": {
1058+
... "id": 2,
1059+
... "content": "moment content after change"
1060+
... },
1061+
... "@tag": "moment"
1062+
... }'''
1063+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1064+
>>> d = json_loads(r.data)
1065+
>>> print(d)
1066+
{'code': 400, 'msg': 'no permission'}
1067+
1068+
>>> #apijson put, without id
1069+
>>> data ='''{
1070+
... "moment": {
1071+
... "content": "moment content after change"
1072+
... },
1073+
... "@tag": "moment"
1074+
... }'''
1075+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1076+
>>> d = json_loads(r.data)
1077+
>>> print(d)
1078+
{'code': 400, 'msg': 'id param needed'}
1079+
1080+
>>> #apijson put, with id not integer
1081+
>>> data ='''{
1082+
... "moment": {
1083+
... "id": "abc",
1084+
... "content": "moment content after change"
1085+
... },
1086+
... "@tag": "moment"
1087+
... }'''
1088+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1089+
>>> d = json_loads(r.data)
1090+
>>> print(d)
1091+
{'code': 400, 'msg': "id 'abc' cannot convert to integer"}
1092+
1093+
>>> #apijson put, with a id cannot find record
1094+
>>> data ='''{
1095+
... "moment": {
1096+
... "id": 100,
1097+
... "content": "moment content after change"
1098+
... },
1099+
... "@tag": "moment"
1100+
... }'''
1101+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1102+
>>> d = json_loads(r.data)
1103+
>>> print(d)
1104+
{'code': 400, 'msg': "cannot find record which id = '100'"}
1105+
1106+
>>> #apijson put, with a role which have no permission
1107+
>>> data ='''{
1108+
... "moment": {
1109+
... "@role": "LOGIN",
1110+
... "id": 1,
1111+
... "content": "moment content after change"
1112+
... },
1113+
... "@tag": "moment"
1114+
... }'''
1115+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1116+
>>> d = json_loads(r.data)
1117+
>>> print(d)
1118+
{'code': 400, 'msg': "'moment' not accessible by role 'LOGIN'"}
1119+
1120+
>>> #apijson put, with UNKNOWN role
1121+
>>> data ='''{
1122+
... "publicnotice": {
1123+
... "@role": "UNKNOWN",
1124+
... "id": 1,
1125+
... "content": "content after change"
1126+
... },
1127+
... "@tag": "publicnotice"
1128+
... }'''
1129+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1130+
>>> d = json_loads(r.data)
1131+
>>> print(d)
1132+
{'code': 200, 'msg': 'success', 'publicnotice': {'id': 1, 'code': 200, 'msg': 'success', 'count': 1}}
1133+
1134+
>>> #apijson put, OWNER but not login
1135+
>>> data ='''{
1136+
... "moment": {
1137+
... "id": 1,
1138+
... "content": "moment content after change"
1139+
... },
1140+
... "@tag": "moment"
1141+
... }'''
1142+
>>> r = handler.post('/apijson/put', data=data, middlewares=[])
1143+
>>> d = json_loads(r.data)
1144+
>>> print(d)
1145+
{'code': 400, 'msg': "'OWNER' need login user"}
1146+
1147+
>>> #apijson put, with a role not permission
1148+
>>> data ='''{
1149+
... "moment": {
1150+
... "@role": "superuser",
1151+
... "id": 1,
1152+
... "content": "moment content after change"
1153+
... },
1154+
... "@tag": "moment"
1155+
... }'''
1156+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("admin"), middlewares=[])
1157+
>>> d = json_loads(r.data)
1158+
>>> print(d)
1159+
{'code': 400, 'msg': "'moment' not accessible by role 'superuser'"}
1160+
1161+
>>> #apijson put, with a field DISALLOWed
1162+
>>> data ='''{
1163+
... "comment": {
1164+
... "id": 2,
1165+
... "user_id": 2,
1166+
... "content": "content after change"
1167+
... },
1168+
... "@tag": "comment"
1169+
... }'''
1170+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1171+
>>> d = json_loads(r.data)
1172+
>>> print(d)
1173+
{'code': 400, 'msg': "request 'comment' disallow 'user_id'"}
1174+
1175+
1176+
>>> #apijson put, without a field in NECESSARY
1177+
>>> data ='''{
1178+
... "moment": {
1179+
... "id": 1
1180+
... },
1181+
... "@tag": "moment"
1182+
... }'''
1183+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1184+
>>> d = json_loads(r.data)
1185+
>>> print(d)
1186+
{'code': 400, 'msg': "request 'moment' have not necessary field 'content'"}
1187+
1188+
>>> #apijson put, with a non exist field
1189+
>>> data ='''{
1190+
... "moment": {
1191+
... "id": 1,
1192+
... "content": "moment content after change",
1193+
... "noexist": "test"
1194+
... },
1195+
... "@tag": "moment"
1196+
... }'''
1197+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1198+
>>> d = json_loads(r.data)
1199+
>>> print(d)
1200+
{'code': 400, 'msg': "'moment' don't have field 'noexist'"}
1201+
1202+
>>> #apijson put, and check get result
1203+
>>> data ='''{
1204+
... "moment": {
1205+
... "id": 1,
1206+
... "content": "check 789"
1207+
... },
1208+
... "@tag": "moment"
1209+
... }'''
1210+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1211+
>>> d = json_loads(r.data)
1212+
>>> print(d)
1213+
{'code': 200, 'msg': 'success', 'moment': {'id': 1, 'code': 200, 'msg': 'success', 'count': 1}}
1214+
>>> data ='''{
1215+
... "moment":{
1216+
... "id": %s
1217+
... }
1218+
... }'''%(d['moment']['id'])
1219+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1220+
>>> d = json_loads(r.data)
1221+
>>> print(d)
1222+
{'code': 200, 'msg': 'success', 'moment': {'user_id': 2, 'date': '2018-11-01 00:00:00', 'content': 'check 789', 'picture_list': '[]', 'id': 1}}
1223+
1224+
>>> #apijson put, and check get result
1225+
>>> data ='''{
1226+
... "moment": {
1227+
... "id": 1,
1228+
... "content": "check 789"
1229+
... },
1230+
... "@tag": "moment"
1231+
... }'''
1232+
>>> r = handler.post('/apijson/put', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1233+
>>> d = json_loads(r.data)
1234+
>>> print(d)
1235+
{'code': 400, 'msg': 'failed when updating, maybe no change', 'moment': {'id': 1, 'code': 400, 'msg': 'failed when updating, maybe no change', 'count': 0}}
10271236
"""

Diff for: uliweb_apijson/apijson/views.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def _put_one(self,key,tag):
494494
return json({"code":400,"msg":"id '%s' cannot convert to integer"%(params.get("id"))})
495495
obj = model.get(id_)
496496
if not obj:
497-
return json({"code":400,"msg":"cannot find record id '%s'"%(id_)})
497+
return json({"code":400,"msg":"cannot find record which id = '%s'"%(id_)})
498498

499499
permission_check_ok = False
500500
model_PUT = model_setting.get("PUT")
@@ -513,7 +513,7 @@ def _put_one(self,key,tag):
513513
permission_check_ok = True
514514
break
515515
else:
516-
return json({"code":400,"msg":"need login user"})
516+
return json({"code":400,"msg":"'OWNER' need login user"})
517517
elif role == "UNKNOWN":
518518
permission_check_ok = True
519519
break
@@ -522,6 +522,7 @@ def _put_one(self,key,tag):
522522
permission_check_ok = True
523523
break
524524

525+
#current implementation won't run here, but keep for safe
525526
if not permission_check_ok:
526527
return json({"code":400,"msg":"no permission"})
527528

0 commit comments

Comments
 (0)