Skip to content

Commit 8a266cf

Browse files
committed
tests: add 11 tests for apijson_delete
1 parent 6f8cade commit 8a266cf

File tree

3 files changed

+192
-6
lines changed

3 files changed

+192
-6
lines changed

tests/demo/apps/apijson_demo/settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ publicnotice = {
4444
"HEAD" : { "roles" : ["OWNER","LOGIN","ADMIN","UNKNOWN"] },
4545
"POST" : { "roles" : ["OWNER","ADMIN"] },
4646
"PUT" : { "roles" : ["OWNER","ADMIN","UNKNOWN"] },
47-
"DELETE" : { "roles" : ["OWNER","ADMIN"] },
47+
"DELETE" : { "roles" : ["OWNER","ADMIN","UNKNOWN"] },
4848
}
4949

5050
[APIJSON_REQUESTS]

tests/test.py

Lines changed: 190 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
from uliweb import manage
33
from uliweb.manage import make_simple_application
44
from json import loads as json_loads
5+
from nose import with_setup
56

6-
os.chdir('demo')
7+
def setup():
8+
os.chdir('demo')
79

8-
manage.call('uliweb syncdb -v')
9-
manage.call('uliweb reset -v -y')
10-
manage.call('uliweb dbinit -v')
10+
manage.call('uliweb syncdb -v')
11+
manage.call('uliweb reset -v -y')
12+
manage.call('uliweb dbinit -v')
13+
14+
def teardown():
15+
pass
1116

1217
def pre_call_as(username):
1318
from uliweb import models
@@ -17,6 +22,7 @@ def pre_call(request):
1722
request.user = user
1823
return pre_call
1924

25+
@with_setup(setup,teardown)
2026
def test_apijson_get():
2127
"""
2228
>>> application = make_simple_application(project_dir='.')
@@ -1233,3 +1239,183 @@ def test_apijson_put():
12331239
>>> print(d)
12341240
{'code': 400, 'msg': 'failed when updating, maybe no change', 'moment': {'id': 1, 'code': 400, 'msg': 'failed when updating, maybe no change', 'count': 0}}
12351241
"""
1242+
1243+
def test_apijson_delete():
1244+
"""
1245+
>>> application = make_simple_application(project_dir='.')
1246+
>>> handler = application.handler()
1247+
1248+
>>> #apijson delete
1249+
>>> data ='''{
1250+
... "moment": {
1251+
... "id": 1
1252+
... },
1253+
... "@tag": "moment"
1254+
... }'''
1255+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1256+
>>> d = json_loads(r.data)
1257+
>>> print(d)
1258+
{'code': 200, 'msg': 'success', 'moment': {'id': 1, 'code': 200, 'message': 'success', 'count': 1}}
1259+
>>> data ='''{
1260+
... "moment": {
1261+
... "id": 1
1262+
... }
1263+
... }'''
1264+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1265+
>>> d = json_loads(r.data)
1266+
>>> print(d)
1267+
{'code': 200, 'msg': 'success', 'moment': None}
1268+
1269+
>>> #apijson delete, without @tag
1270+
>>> data ='''{
1271+
... "moment": {
1272+
... "content": "new moment for test"
1273+
... },
1274+
... "@tag": "moment"
1275+
... }'''
1276+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1277+
>>> d = json_loads(r.data)
1278+
>>> data ='''{
1279+
... "moment": {
1280+
... "id": %s
1281+
... }
1282+
... }'''%(d["moment"]["id"])
1283+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1284+
>>> d = json_loads(r.data)
1285+
>>> print(d)
1286+
{'code': 400, 'msg': "'tag' parameter is needed"}
1287+
1288+
>>> #apijson delete, with non exist model
1289+
>>> data ='''{
1290+
... "nonexist": {
1291+
... "id": 1
1292+
... },
1293+
... "@tag": "nonexist"
1294+
... }'''
1295+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1296+
>>> d = json_loads(r.data)
1297+
>>> print(d)
1298+
{'code': 400, 'msg': "model 'nonexist' not found"}
1299+
1300+
>>> #apijson delete, default to OWNER and delete other's record
1301+
>>> data ='''{
1302+
... "moment": {
1303+
... "id": 2
1304+
... },
1305+
... "@tag": "moment"
1306+
... }'''
1307+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1308+
>>> d = json_loads(r.data)
1309+
>>> print(d)
1310+
{'code': 400, 'msg': 'no permission'}
1311+
1312+
>>> #apijson delete, without id
1313+
>>> data ='''{
1314+
... "moment": {
1315+
... },
1316+
... "@tag": "moment"
1317+
... }'''
1318+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1319+
>>> d = json_loads(r.data)
1320+
>>> print(d)
1321+
{'code': 400, 'msg': 'id param needed'}
1322+
1323+
>>> #apijson delete, id not int
1324+
>>> data ='''{
1325+
... "moment": {
1326+
... "id": "abc"
1327+
... },
1328+
... "@tag": "moment"
1329+
... }'''
1330+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1331+
>>> d = json_loads(r.data)
1332+
>>> print(d)
1333+
{'code': 400, 'msg': "id 'abc' cannot convert to integer"}
1334+
1335+
>>> #apijson delete
1336+
>>> data ='''{
1337+
... "moment": {
1338+
... "id": 100
1339+
... },
1340+
... "@tag": "moment"
1341+
... }'''
1342+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1343+
>>> d = json_loads(r.data)
1344+
>>> print(d)
1345+
{'code': 400, 'msg': "cannot find record id = '100'"}
1346+
1347+
>>> #apijson delete, with a role having no permission
1348+
>>> data ='''{
1349+
... "moment": {
1350+
... "content": "new moment for test"
1351+
... },
1352+
... "@tag": "moment"
1353+
... }'''
1354+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1355+
>>> d = json_loads(r.data)
1356+
>>> data ='''{
1357+
... "moment": {
1358+
... "@role": "UNKNOWN",
1359+
... "id": %s
1360+
... },
1361+
... "@tag": "moment"
1362+
... }'''%(d["moment"]["id"])
1363+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1364+
>>> d = json_loads(r.data)
1365+
>>> print(d)
1366+
{'code': 400, 'msg': "'moment' not accessible by role 'UNKNOWN'"}
1367+
1368+
>>> #apijson delete, with OWNER but not login
1369+
>>> data ='''{
1370+
... "moment": {
1371+
... "content": "new moment for test"
1372+
... },
1373+
... "@tag": "moment"
1374+
... }'''
1375+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1376+
>>> d = json_loads(r.data)
1377+
>>> data ='''{
1378+
... "moment": {
1379+
... "id": %s
1380+
... },
1381+
... "@tag": "moment"
1382+
... }'''%(d["moment"]["id"])
1383+
>>> r = handler.post('/apijson/delete', data=data, middlewares=[])
1384+
>>> d = json_loads(r.data)
1385+
>>> print(d)
1386+
{'code': 400, 'msg': 'need login user'}
1387+
1388+
>>> #apijson delete, with UNKNOWN role
1389+
>>> data ='''{
1390+
... "publicnotice": {
1391+
... "@role": "UNKNOWN",
1392+
... "id": 1
1393+
... },
1394+
... "@tag": "publicnotice"
1395+
... }'''
1396+
>>> r = handler.post('/apijson/delete', data=data, middlewares=[])
1397+
>>> d = json_loads(r.data)
1398+
>>> print(d)
1399+
{'code': 200, 'msg': 'success', 'publicnotice': {'id': 1, 'code': 200, 'message': 'success', 'count': 1}}
1400+
1401+
>>> #apijson delete, with a role which have no permission
1402+
>>> data ='''{
1403+
... "moment": {
1404+
... "content": "new moment for test"
1405+
... },
1406+
... "@tag": "moment"
1407+
... }'''
1408+
>>> r = handler.post('/apijson/post', data=data, pre_call=pre_call_as("usera"), middlewares=[])
1409+
>>> d = json_loads(r.data)
1410+
>>> data ='''{
1411+
... "moment": {
1412+
... "@role": "superuser",
1413+
... "id": %s
1414+
... },
1415+
... "@tag": "moment"
1416+
... }'''%(d["moment"]["id"])
1417+
>>> r = handler.post('/apijson/delete', data=data, pre_call=pre_call_as("admin"), middlewares=[])
1418+
>>> d = json_loads(r.data)
1419+
>>> print(d)
1420+
{'code': 400, 'msg': "'moment' not accessible by role 'superuser'"}
1421+
"""

uliweb_apijson/apijson/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ def _delete_one(self,key,tag):
617617
return json({"code":400,"msg":"id '%s' cannot convert to integer"%(params.get("id"))})
618618
obj = model.get(id_)
619619
if not obj:
620-
return json({"code":400,"msg":"cannot find record id '%s'"%(id_)})
620+
return json({"code":400,"msg":"cannot find record id = '%s'"%(id_)})
621621

622622
permission_check_ok = False
623623
DELETE = model_setting.get("DELETE")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy