Skip to content

Commit e6f5c82

Browse files
committed
fix "'Request' object has no attribute 'user'"; add 7 test cases
1 parent ff13580 commit e6f5c82

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

Diff for: tests/test.py

+90-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ def test_apijson_get():
6666
>>> print(d)
6767
{'code': 200, 'msg': 'success', 'user': {'username': 'admin', 'nickname': 'Administrator', 'email': 'admin@localhost', 'is_superuser': True, 'id': 1}}
6868
69+
>>> #query with @column which have a non existing column name
70+
>>> data ='''{
71+
... "user":{
72+
... "@role":"OWNER",
73+
... "@column": "id,username,email,nickname,is_superuser,nonexisting"
74+
... }
75+
... }'''
76+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
77+
>>> d = json_loads(r.data)
78+
>>> print(d)
79+
{'code': 200, 'msg': 'success', 'user': {'username': 'admin', 'nickname': 'Administrator', 'email': 'admin@localhost', 'is_superuser': True, 'id': 1}}
80+
81+
>>> #query with a non existing column property
82+
>>> data ='''{
83+
... "user":{
84+
... "nonexisting": 1
85+
... }
86+
... }'''
87+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
88+
>>> d = json_loads(r.data)
89+
>>> print(d)
90+
{'code': 400, 'msg': "'user' have no attribute 'nonexisting'"}
91+
6992
>>> #query one with a non existing model
7093
>>> data ='''{
7194
... "nonexist":{
@@ -120,7 +143,7 @@ def test_apijson_get():
120143
>>> r = handler.post('/apijson/get', data=data, middlewares=[])
121144
>>> d = json_loads(r.data)
122145
>>> print(d)
123-
{'code': 400, 'msg': "no user for role 'OWNER'"}
146+
{'code': 400, 'msg': "no login user for role 'OWNER'"}
124147
125148
>>> #query one with OWNER but cannot filter with OWNER
126149
>>> data ='''{
@@ -200,6 +223,72 @@ def test_apijson_get():
200223
>>> print(d)
201224
{'code': 400, 'msg': "'user' not accessible by role 'superuser'"}
202225
226+
>>> #query array
227+
>>> data ='''{
228+
... "[]":{
229+
... "user": {"@role":"ADMIN"}
230+
... }
231+
... }'''
232+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
233+
>>> d = json_loads(r.data)
234+
>>> print(d)
235+
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'admin', 'nickname': 'Administrator', 'email': 'admin@localhost', 'is_superuser': True, 'last_login': None, 'date_join': '2018-11-01 00:00:00', 'image': '', 'active': False, 'locked': False, 'deleted': False, 'auth_type': 'default', 'timezone': '', 'id': 1}}, {'user': {'username': 'usera', 'nickname': 'User A', 'email': 'usera@localhost', 'is_superuser': False, 'last_login': None, 'date_join': '2018-02-02 00:00:00', 'image': '', 'active': False, 'locked': False, 'deleted': False, 'auth_type': 'default', 'timezone': '', 'id': 2}}, {'user': {'username': 'userb', 'nickname': 'User B', 'email': 'userb@localhost', 'is_superuser': False, 'last_login': None, 'date_join': '2018-03-03 00:00:00', 'image': '', 'active': False, 'locked': False, 'deleted': False, 'auth_type': 'default', 'timezone': '', 'id': 3}}, {'user': {'username': 'userc', 'nickname': 'User C', 'email': 'userc@localhost', 'is_superuser': False, 'last_login': None, 'date_join': '2018-04-04 00:00:00', 'image': '', 'active': False, 'locked': False, 'deleted': False, 'auth_type': 'default', 'timezone': '', 'id': 4}}]}
236+
237+
>>> #query array
238+
>>> data ='''{
239+
... "[]":{
240+
... "user": {
241+
... "@role":"ADMIN",
242+
... "@column":"id,username,nickname,email"
243+
... }
244+
... }
245+
... }'''
246+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
247+
>>> d = json_loads(r.data)
248+
>>> print(d)
249+
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'admin', 'nickname': 'Administrator', 'email': 'admin@localhost', 'id': 1}}, {'user': {'username': 'usera', 'nickname': 'User A', 'email': 'usera@localhost', 'id': 2}}, {'user': {'username': 'userb', 'nickname': 'User B', 'email': 'userb@localhost', 'id': 3}}, {'user': {'username': 'userc', 'nickname': 'User C', 'email': 'userc@localhost', 'id': 4}}]}
250+
251+
>>> #query array with non existing role
252+
>>> data ='''{
253+
... "[]":{
254+
... "user": {
255+
... "@role":"NONEXISTING",
256+
... "@column":"id,username,nickname,email"
257+
... }
258+
... }
259+
... }'''
260+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
261+
>>> d = json_loads(r.data)
262+
>>> print(d)
263+
{'code': 400, 'msg': "'user' not accessible by role 'NONEXISTING'"}
264+
265+
>>> #query array with UNKNOWN
266+
>>> data ='''{
267+
... "[]":{
268+
... "user": {
269+
... "@column":"id,username,nickname,email"
270+
... }
271+
... }
272+
... }'''
273+
>>> r = handler.post('/apijson/get', data=data, middlewares=[])
274+
>>> d = json_loads(r.data)
275+
>>> print(d)
276+
{'code': 400, 'msg': "'user' not accessible by role 'UNKNOWN'"}
277+
278+
>>> #query array without login user
279+
>>> data ='''{
280+
... "[]":{
281+
... "user": {
282+
... "@role":"ADMIN",
283+
... "@column":"id,username,nickname,email"
284+
... }
285+
... }
286+
... }'''
287+
>>> r = handler.post('/apijson/get', data=data, middlewares=[])
288+
>>> d = json_loads(r.data)
289+
>>> print(d)
290+
{'code': 400, 'msg': "no login user for role 'ADMIN'"}
291+
203292
>>> #Association query: Two tables, one to one,ref path is absolute path
204293
>>> data ='''{
205294
... "moment":{},

Diff for: uliweb_apijson/apijson/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ def _check_GET_permission(self):
9090
params_role = self.params.get("@role")
9191

9292
if not params_role:
93-
if request.user:
93+
if hasattr(request,"user"):
9494
params_role = "LOGIN"
9595
else:
9696
params_role = "UNKNOWN"
97+
elif params_role != "UNKNOWN":
98+
if not hasattr(request,"user"):
99+
raise UliwebError("no login user for role '%s'"%(params_role))
97100
if params_role not in roles:
98101
raise UliwebError("'%s' not accessible by role '%s'"%(self.name,params_role))
99102
if params_role == "UNKNOWN":

Diff for: uliweb_apijson/apijson/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _get_one(self,key):
118118
params_role = "UNKNOWN"
119119
elif params_role != "UNKNOWN":
120120
if not hasattr(request,"user"):
121-
return json({"code":400,"msg":"no user for role '%s'"%(params_role)})
121+
return json({"code":400,"msg":"no login user for role '%s'"%(params_role)})
122122
if params_role not in roles:
123123
return json({"code":400,"msg":"'%s' not accessible by role '%s'"%(model_name,params_role)})
124124
if params_role == "UNKNOWN":

0 commit comments

Comments
 (0)