@@ -12,63 +12,73 @@ def test_get_userlist(self):
1212 """
1313 client = APIClient ()
1414 client .force_authenticate (user = self .superuser )
15- response = client .get ('/api/v2/users/' )
16-
17- model = {
18- 'count' : 4 ,
19- 'next' : None ,
20- 'previous' : None ,
21- }
22-
23- for key in model : # pylint: disable=consider-using-dict-items
24- with self .subTest (key = key ):
25- self .assertIn (key , response .data )
26- self .assertEqual (response .data [key ], model [key ])
2715
2816 results = [
29- {'id' : 101 ,
30- 'url' : 'http://testserver/api/v2/users/101' ,
31- 'username' : 'testUser' ,
32- 'student_id' : '12345X' ,
33- 'email' : 'test@aplus.com' ,
34- 'full_name' : 'Superb Student' ,
35- 'is_external' : False },
36- {'id' : 102 ,
37- 'url' : 'http://testserver/api/v2/users/102' ,
38- 'username' : 'grader' ,
39- 'student_id' : '67890Y' ,
40- 'email' : 'grader@aplus.com' ,
41- 'full_name' : 'Grumpy Grader' ,
42- 'is_external' : False },
43- {'id' : 103 ,
44- 'url' : 'http://testserver/api/v2/users/103' ,
45- 'username' : 'teacher' ,
46- 'student_id' : None ,
47- 'email' : 'teacher@aplus.com' ,
48- 'full_name' : 'Tedious Teacher' ,
49- 'is_external' : True },
50- {'id' : 104 ,
51- 'url' : 'http://testserver/api/v2/users/104' ,
52- 'username' : 'superuser' ,
53- 'student_id' : None ,
54- 'email' : 'superuser@aplus.com' ,
55- 'full_name' : 'Super User' ,
56- 'is_external' : True },
17+ {
18+ 'id' : 101 ,
19+ 'url' : 'http://testserver/api/v2/users/101' ,
20+ 'username' : 'testUser' ,
21+ 'student_id' : '12345X' ,
22+ 'email' : 'test@aplus.com' ,
23+ 'full_name' : 'Superb Student' ,
24+ 'is_external' : False
25+ },
26+ {
27+ 'id' : 102 ,
28+ 'url' : 'http://testserver/api/v2/users/102' ,
29+ 'username' : 'grader' ,
30+ 'student_id' : '67890Y' ,
31+ 'email' : 'grader@aplus.com' ,
32+ 'full_name' : 'Grumpy Grader' ,
33+ 'is_external' : False
34+ },
35+ {
36+ 'id' : 103 ,
37+ 'url' : 'http://testserver/api/v2/users/103' ,
38+ 'username' : 'teacher' ,
39+ 'student_id' : None ,
40+ 'email' : 'teacher@aplus.com' ,
41+ 'full_name' : 'Tedious Teacher' ,
42+ 'is_external' : True
43+ },
44+ {
45+ 'id' : 104 ,
46+ 'url' : 'http://testserver/api/v2/users/104' ,
47+ 'username' : 'superuser' ,
48+ 'student_id' : None ,
49+ 'email' : 'superuser@aplus.com' ,
50+ 'full_name' : 'Super User' ,
51+ 'is_external' : True
52+ },
5753 ]
5854
59- self .assertIn ('results' , response .data )
60- r = response .data ['results' ]
61- self .assertEqual (len (r ), len (results ), "Wrong number of results" )
62- for user in results :
55+ for i , email in enumerate ([result ['email' ] for result in results ]):
56+ response = client .get (f'/api/v2/users/?search={ email } ' )
57+
58+ model = {
59+ 'count' : 1 ,
60+ 'next' : None ,
61+ 'previous' : None ,
62+ }
63+
64+ for key in model : # pylint: disable=consider-using-dict-items
65+ with self .subTest (key = key ):
66+ self .assertIn (key , response .data )
67+ self .assertEqual (response .data [key ], model [key ])
68+
69+ self .assertIn ('results' , response .data )
70+ r = response .data ['results' ]
71+ self .assertEqual (len (r ), 1 , "Wrong number of results" )
72+ user = results [i ]
6373 with self .subTest (id = user ['id' ], user = user ):
6474 u = next ((u for u in r if u .get ('id' ) == user ['id' ]), None )
6575 self .assertNotEqual (u , None , "User with id %s not found from the result list" % (user ['id' ],))
6676 u = dict (u ) # convert OrderedDict to dict for more clear error messages
6777 self .assertDictEqual (u , user )
6878
69- # check that there is no extra fields
70- model ['results' ] = results
71- self .assertCountEqual (response .data , model )
79+ # check that there is no extra fields
80+ model ['results' ] = results
81+ self .assertCountEqual (response .data , model )
7282
7383 def test_get_userdetail (self ):
7484 client = APIClient ()
@@ -100,27 +110,11 @@ def check_response(url: str, expected_ids: Set[int]) -> None:
100110 ids = {u ['id' ] for u in response .data ['results' ]}
101111 self .assertSetEqual (ids , expected_ids )
102112
103- # Check that all individual field filters work (id, student_id, email)
104- # The API uses User.id values, not UserProfile.id, to identify users.
105- check_response ('/api/v2/users/?field=id&values=1,2,3,4' , set ())
106- check_response ('/api/v2/users/?field=id&values=101,102,103,104' , {101 , 102 , 103 , 104 })
107- check_response ('/api/v2/users/?field=student_id&values=12345X,67890Y' , {101 , 102 })
108- check_response ('/api/v2/users/?field=email&values=teacher@aplus.com,superuser@aplus.com' , {103 , 104 })
109-
110- # Check that partial matches are not returned
111- check_response ('/api/v2/users/?field=student_id&values=123,67890Y' , {102 })
113+ # Check that email field filter works
114+ check_response ('/api/v2/users/?search=teacher@aplus.com' , {103 })
112115
113116 # Check that unmatched values don't cause errors
114- check_response ('/api/v2/users/?field=id&values=1,101,999 ' , { 101 } )
117+ check_response ('/api/v2/users/?search=invalid@aplus.com ' , set () )
115118
116119 # Check that an empty or missing values list returns an empty result
117- check_response ('/api/v2/users/?field=email' , set ())
118-
119- # Check the 404 not found response when the given field does not exist.
120- # emails is the wrong name for the field, email is correct.
121- response = client .get ('/api/v2/users/?field=emails&values=teacher@aplus.com' )
122- self .assertEqual (response .status_code , 404 )
123-
124- # Invalid values: int expected for id, but str given.
125- response = client .get ('/api/v2/users/?field=id&values=hundred' )
126- self .assertEqual (response .status_code , 404 )
120+ check_response ('/api/v2/users/' , set ())
0 commit comments