1111from django .urls import reverse
1212
1313from patchwork .models import Patch
14+ from patchwork .tests .api import utils
1415from patchwork .tests .utils import create_maintainer
1516from patchwork .tests .utils import create_patch
1617from patchwork .tests .utils import create_person
@@ -58,73 +59,98 @@ def assertSerialized(self, patch_obj, patch_json):
5859 self .assertEqual (patch_obj .project .id ,
5960 patch_json ['project' ]['id' ])
6061
61- def test_list (self ):
62- """Validate we can list a patch ."""
62+ def test_list_empty (self ):
63+ """List patches when none are present ."""
6364 resp = self .client .get (self .api_url ())
6465 self .assertEqual (status .HTTP_200_OK , resp .status_code )
6566 self .assertEqual (0 , len (resp .data ))
6667
68+ def _create_patch (self ):
6769 person_obj = create_person (email = 'test@example.com' )
6870 project_obj = create_project (linkname = 'myproject' )
6971 state_obj = create_state (name = 'Under Review' )
7072 patch_obj = create_patch (state = state_obj , project = project_obj ,
7173 submitter = person_obj )
7274
73- # anonymous user
75+ return patch_obj
76+
77+ def test_list_anonymous (self ):
78+ """List patches as anonymous user."""
79+ patch = self ._create_patch ()
80+
7481 resp = self .client .get (self .api_url ())
7582 self .assertEqual (status .HTTP_200_OK , resp .status_code )
7683 self .assertEqual (1 , len (resp .data ))
7784 patch_rsp = resp .data [0 ]
78- self .assertSerialized (patch_obj , patch_rsp )
85+ self .assertSerialized (patch , patch_rsp )
7986 self .assertNotIn ('headers' , patch_rsp )
8087 self .assertNotIn ('content' , patch_rsp )
8188 self .assertNotIn ('diff' , patch_rsp )
8289
83- # authenticated user
90+ @utils .store_samples ('patch-list' )
91+ def test_list_authenticated (self ):
92+ """List patches as an authenticated user."""
93+ patch = self ._create_patch ()
8494 user = create_user ()
95+
8596 self .client .force_authenticate (user = user )
8697 resp = self .client .get (self .api_url ())
8798 self .assertEqual (status .HTTP_200_OK , resp .status_code )
8899 self .assertEqual (1 , len (resp .data ))
89100 patch_rsp = resp .data [0 ]
90- self .assertSerialized (patch_obj , patch_rsp )
101+ self .assertSerialized (patch , patch_rsp )
91102
92- # test filtering by state
93- resp = self .client .get (self .api_url (), {'state' : 'under-review' })
94- self .assertEqual ([patch_obj .id ], [x ['id' ] for x in resp .data ])
95- resp = self .client .get (self .api_url (), {'state' : 'missing-state' })
96- self .assertEqual (0 , len (resp .data ))
103+ def test_list_filter_state (self ):
104+ """Filter patches by state."""
105+ self ._create_patch ()
106+ user = create_user ()
107+
108+ state_obj_b = create_state (name = 'New' )
109+ create_patch (state = state_obj_b )
110+ state_obj_c = create_state (name = 'RFC' )
111+ create_patch (state = state_obj_c )
112+
113+ self .client .force_authenticate (user = user )
114+ resp = self .client .get (self .api_url (), [('state' , 'under-review' ),
115+ ('state' , 'new' )])
116+ self .assertEqual (2 , len (resp .data ))
117+
118+ def test_list_filter_project (self ):
119+ """Filter patches by project."""
120+ patch = self ._create_patch ()
121+ user = create_user ()
122+
123+ self .client .force_authenticate (user = user )
97124
98- # test filtering by project
99125 resp = self .client .get (self .api_url (), {'project' : 'myproject' })
100- self .assertEqual ([patch_obj .id ], [x ['id' ] for x in resp .data ])
126+ self .assertEqual ([patch .id ], [x ['id' ] for x in resp .data ])
127+
101128 resp = self .client .get (self .api_url (), {'project' : 'invalidproject' })
102129 self .assertEqual (0 , len (resp .data ))
103130
131+ def test_list_filter_submitter (self ):
132+ """Filter patches by submitter."""
133+ patch = self ._create_patch ()
134+ submitter = patch .submitter
135+ user = create_user ()
136+
137+ self .client .force_authenticate (user = user )
138+
104139 # test filtering by submitter, both ID and email
105- resp = self .client .get (self .api_url (), {'submitter' : person_obj .id })
106- self .assertEqual ([patch_obj .id ], [x ['id' ] for x in resp .data ])
140+ resp = self .client .get (self .api_url (), {'submitter' : submitter .id })
141+ self .assertEqual ([patch .id ], [x ['id' ] for x in resp .data ])
142+
107143 resp = self .client .get (self .api_url (), {
108144 'submitter' : 'test@example.com' })
109- self .assertEqual ([patch_obj .id ], [x ['id' ] for x in resp .data ])
145+ self .assertEqual ([patch .id ], [x ['id' ] for x in resp .data ])
146+
110147 resp = self .client .get (self .api_url (), {
111148 'submitter' : 'test@example.org' })
112149 self .assertEqual (0 , len (resp .data ))
113150
114- state_obj_b = create_state (name = 'New' )
115- create_patch (state = state_obj_b )
116- state_obj_c = create_state (name = 'RFC' )
117- create_patch (state = state_obj_c )
118-
119- resp = self .client .get (self .api_url ())
120- self .assertEqual (3 , len (resp .data ))
121- resp = self .client .get (self .api_url (), [('state' , 'under-review' )])
122- self .assertEqual (1 , len (resp .data ))
123- resp = self .client .get (self .api_url (), [('state' , 'under-review' ),
124- ('state' , 'new' )])
125- self .assertEqual (2 , len (resp .data ))
126-
151+ @utils .store_samples ('patch-list-1-0' )
127152 def test_list_version_1_0 (self ):
153+ """List patches using API v1.0."""
128154 create_patch ()
129155
130156 resp = self .client .get (self .api_url (version = '1.0' ))
@@ -133,8 +159,9 @@ def test_list_version_1_0(self):
133159 self .assertIn ('url' , resp .data [0 ])
134160 self .assertNotIn ('web_url' , resp .data [0 ])
135161
162+ @utils .store_samples ('patch-detail' )
136163 def test_detail (self ):
137- """Validate we can get a specific patch."""
164+ """Show a specific patch."""
138165 patch = create_patch (
139166 content = 'Reviewed-by: Test User <test@example.com>\n ' ,
140167 headers = 'Received: from somewhere\n Received: from another place'
@@ -154,6 +181,7 @@ def test_detail(self):
154181 self .assertEqual (patch .diff , resp .data ['diff' ])
155182 self .assertEqual (0 , len (resp .data ['tags' ]))
156183
184+ @utils .store_samples ('patch-detail-1-0' )
157185 def test_detail_version_1_0 (self ):
158186 patch = create_patch ()
159187
@@ -185,27 +213,45 @@ def test_create(self):
185213 resp = self .client .post (self .api_url (), patch )
186214 self .assertEqual (status .HTTP_405_METHOD_NOT_ALLOWED , resp .status_code )
187215
188- def test_update (self ):
189- """Ensure updates can be performed by maintainers."""
190- project = create_project ()
191- patch = create_patch (project = project )
216+ @utils .store_samples ('patch-update-error-forbidden' )
217+ def test_update_anonymous (self ):
218+ """Update patch as anonymous user.
219+
220+ Ensure updates can be performed by maintainers.
221+ """
222+ patch = create_patch ()
192223 state = create_state ()
193224
194- # anonymous user
195225 resp = self .client .patch (self .api_url (patch .id ), {'state' : state .name })
196226 self .assertEqual (status .HTTP_403_FORBIDDEN , resp .status_code )
197227
198- # authenticated user
228+ def test_update_non_maintainer (self ):
229+ """Update patch as non-maintainer.
230+
231+ Ensure updates can be performed by maintainers.
232+ """
233+ patch = create_patch ()
234+ state = create_state ()
199235 user = create_user ()
236+
200237 self .client .force_authenticate (user = user )
201238 resp = self .client .patch (self .api_url (patch .id ), {'state' : state .name })
202239 self .assertEqual (status .HTTP_403_FORBIDDEN , resp .status_code )
203240
204- # maintainer
241+ @utils .store_samples ('patch-update' )
242+ def test_update_maintainer (self ):
243+ """Update patch as maintainer.
244+
245+ Ensure updates can be performed by maintainers.
246+ """
247+ project = create_project ()
248+ patch = create_patch (project = project )
249+ state = create_state ()
205250 user = create_maintainer (project )
251+
206252 self .client .force_authenticate (user = user )
207- resp = self .client .patch (self .api_url (patch .id ), {
208- 'state' : state .name , 'delegate' : user .id })
253+ resp = self .client .patch (self .api_url (patch .id ),
254+ { 'state' : state .name , 'delegate' : user .id })
209255 self .assertEqual (status .HTTP_200_OK , resp .status_code , resp )
210256 self .assertEqual (Patch .objects .get (id = patch .id ).state , state )
211257 self .assertEqual (Patch .objects .get (id = patch .id ).delegate , user )
@@ -217,22 +263,35 @@ def test_update(self):
217263 self .assertEqual (status .HTTP_200_OK , resp .status_code , resp )
218264 self .assertIsNone (Patch .objects .get (id = patch .id ).delegate )
219265
220- def test_update_invalid (self ):
221- """Ensure we handle invalid Patch updates."""
266+ @utils .store_samples ('patch-update-error-bad-request' )
267+ def test_update_invalid_state (self ):
268+ """Update patch with invalid fields.
269+
270+ Ensure we handle invalid Patch updates.
271+ """
222272 project = create_project ()
223273 state = create_state ()
224274 patch = create_patch (project = project , state = state )
225275 user = create_maintainer (project )
226276
227- # invalid state
228277 self .client .force_authenticate (user = user )
229278 resp = self .client .patch (self .api_url (patch .id ), {'state' : 'foobar' })
230279 self .assertEqual (status .HTTP_400_BAD_REQUEST , resp .status_code )
231280 self .assertContains (resp , 'Expected one of: %s.' % state .name ,
232281 status_code = status .HTTP_400_BAD_REQUEST )
233282
234- # invalid delegate
283+ def test_update_invalid_delegate (self ):
284+ """Update patch with invalid fields.
285+
286+ Ensure we handle invalid Patch updates.
287+ """
288+ project = create_project ()
289+ state = create_state ()
290+ patch = create_patch (project = project , state = state )
291+ user_a = create_maintainer (project )
235292 user_b = create_user ()
293+
294+ self .client .force_authenticate (user = user_a )
236295 resp = self .client .patch (self .api_url (patch .id ),
237296 {'delegate' : user_b .id })
238297 self .assertEqual (status .HTTP_400_BAD_REQUEST , resp .status_code )
0 commit comments