Skip to content

Commit d2ab647

Browse files
committed
fix "AttributeError: 'Request' object has no attribute 'user'"; add 4 test cases for apijson-head
1 parent f0c5026 commit d2ab647

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

tests/test.py

+52
Original file line numberDiff line numberDiff line change
@@ -813,3 +813,55 @@ def test_apijson_get():
813813
>>> print(d)
814814
{'code': 200, 'msg': 'success', 'moment': {'user_id': 2, 'date': '2018-11-01 00:00:00', 'content': 'test moment', 'picture_list': '[]', 'id': 1}, 'user': {'username': 'usera', 'email': 'usera@localhost', 'id': 2}}
815815
"""
816+
817+
def test_apijson_head():
818+
"""
819+
>>> application = make_simple_application(project_dir='.')
820+
>>> handler = application.handler()
821+
822+
>>> #apijson head
823+
>>> data ='''{
824+
... "moment": {
825+
... "user_id": 2
826+
... }
827+
... }'''
828+
>>> r = handler.post('/apijson/head', data=data, pre_call=pre_call_as("admin"), middlewares=[])
829+
>>> d = json_loads(r.data)
830+
>>> print(d)
831+
{'code': 200, 'msg': 'success', 'moment': {'code': 200, 'msg': 'success', 'count': 1}}
832+
833+
>>> #apijson head, with a nonexistant model
834+
>>> data ='''{
835+
... "nonexist": {
836+
... "user_id": 2
837+
... }
838+
... }'''
839+
>>> r = handler.post('/apijson/head', data=data, pre_call=pre_call_as("admin"), middlewares=[])
840+
>>> d = json_loads(r.data)
841+
>>> print(d)
842+
{'code': 400, 'msg': "model 'nonexist' not found"}
843+
844+
>>> #apijson head, without permission of HEAD
845+
>>> data ='''{
846+
... "privacy": {
847+
... "@role":"LOGIN",
848+
... "id": 1
849+
... }
850+
... }'''
851+
>>> r = handler.post('/apijson/head', data=data, pre_call=pre_call_as("admin"), middlewares=[])
852+
>>> d = json_loads(r.data)
853+
>>> print(d)
854+
{'code': 400, 'msg': "role 'LOGIN' not have permission HEAD for 'privacy'"}
855+
856+
>>> #apijson head, without user
857+
>>> data ='''{
858+
... "privacy": {
859+
... "@role":"ADMIN",
860+
... "id": 1
861+
... }
862+
... }'''
863+
>>> r = handler.post('/apijson/head', data=data, middlewares=[])
864+
>>> d = json_loads(r.data)
865+
>>> print(d)
866+
{'code': 400, 'msg': "no login user for role 'ADMIN'"}
867+
"""

uliweb_apijson/apijson/views.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,11 @@ def _head(self,key):
295295
else:
296296
params_role = "UNKNOWN"
297297
if params_role not in roles:
298-
return json({"code":400,"msg":"'%s' not accessible by role '%s'"%(model_name,params_role)})
298+
return json({"code":400,"msg":"role '%s' not have permission HEAD for '%s'"%(params_role,model_name)})
299299
if params_role == "UNKNOWN":
300300
permission_check_ok = True
301+
elif not hasattr(request,"user"):
302+
return json({"code":400,"msg":"no login user for role '%s'"%(params_role)})
301303
elif functions.has_role(request.user,params_role):
302304
permission_check_ok = True
303305
else:

0 commit comments

Comments
 (0)