@@ -72,73 +72,96 @@ def assertSerialized(self, patch_obj, patch_json):
7272 self .assertEqual (patch_obj .project .id ,
7373 patch_json ['project' ]['id' ])
7474
75- def test_list (self ):
76- """Validate we can list a patch ."""
75+ def test_list_empty (self ):
76+ """List patches when none are present ."""
7777 resp = self .client .get (self .api_url ())
7878 self .assertEqual (status .HTTP_200_OK , resp .status_code )
7979 self .assertEqual (0 , len (resp .data ))
8080
81+ def _create_patch (self ):
8182 person_obj = create_person (email = 'test@example.com' )
8283 project_obj = create_project (linkname = 'myproject' )
8384 state_obj = create_state (name = 'Under Review' )
8485 patch_obj = create_patch (state = state_obj , project = project_obj ,
8586 submitter = person_obj )
8687
87- # anonymous user
88+ return patch_obj
89+
90+ def test_list_anonymous (self ):
91+ """List patches as anonymous user."""
92+ patch = self ._create_patch ()
93+
8894 resp = self .client .get (self .api_url ())
8995 self .assertEqual (status .HTTP_200_OK , resp .status_code )
9096 self .assertEqual (1 , len (resp .data ))
9197 patch_rsp = resp .data [0 ]
92- self .assertSerialized (patch_obj , patch_rsp )
98+ self .assertSerialized (patch , patch_rsp )
9399 self .assertNotIn ('headers' , patch_rsp )
94100 self .assertNotIn ('content' , patch_rsp )
95101 self .assertNotIn ('diff' , patch_rsp )
96102
97- # authenticated user
103+ def test_list_authenticated (self ):
104+ """List patches as an authenticated user."""
105+ patch = self ._create_patch ()
98106 user = create_user ()
107+
99108 self .client .force_authenticate (user = user )
100109 resp = self .client .get (self .api_url ())
101110 self .assertEqual (status .HTTP_200_OK , resp .status_code )
102111 self .assertEqual (1 , len (resp .data ))
103112 patch_rsp = resp .data [0 ]
104- self .assertSerialized (patch_obj , patch_rsp )
113+ self .assertSerialized (patch , patch_rsp )
105114
106- # test filtering by state
107- resp = self .client .get (self .api_url (), {'state' : 'under-review' })
108- self .assertEqual ([patch_obj .id ], [x ['id' ] for x in resp .data ])
109- resp = self .client .get (self .api_url (), {'state' : 'missing-state' })
110- self .assertEqual (0 , len (resp .data ))
115+ def test_list_filter_state (self ):
116+ """Filter patches by state."""
117+ self ._create_patch ()
118+ user = create_user ()
119+
120+ state_obj_b = create_state (name = 'New' )
121+ create_patch (state = state_obj_b )
122+ state_obj_c = create_state (name = 'RFC' )
123+ create_patch (state = state_obj_c )
124+
125+ self .client .force_authenticate (user = user )
126+ resp = self .client .get (self .api_url (), [('state' , 'under-review' ),
127+ ('state' , 'new' )])
128+ self .assertEqual (2 , len (resp .data ))
129+
130+ def test_list_filter_project (self ):
131+ """Filter patches by project."""
132+ patch = self ._create_patch ()
133+ user = create_user ()
134+
135+ self .client .force_authenticate (user = user )
111136
112- # test filtering by project
113137 resp = self .client .get (self .api_url (), {'project' : 'myproject' })
114- self .assertEqual ([patch_obj .id ], [x ['id' ] for x in resp .data ])
138+ self .assertEqual ([patch .id ], [x ['id' ] for x in resp .data ])
139+
115140 resp = self .client .get (self .api_url (), {'project' : 'invalidproject' })
116141 self .assertEqual (0 , len (resp .data ))
117142
143+ def test_list_filter_submitter (self ):
144+ """Filter patches by submitter."""
145+ patch = self ._create_patch ()
146+ submitter = patch .submitter
147+ user = create_user ()
148+
149+ self .client .force_authenticate (user = user )
150+
118151 # test filtering by submitter, both ID and email
119- resp = self .client .get (self .api_url (), {'submitter' : person_obj .id })
120- self .assertEqual ([patch_obj .id ], [x ['id' ] for x in resp .data ])
152+ resp = self .client .get (self .api_url (), {'submitter' : submitter .id })
153+ self .assertEqual ([patch .id ], [x ['id' ] for x in resp .data ])
154+
121155 resp = self .client .get (self .api_url (), {
122156 'submitter' : 'test@example.com' })
123- self .assertEqual ([patch_obj .id ], [x ['id' ] for x in resp .data ])
157+ self .assertEqual ([patch .id ], [x ['id' ] for x in resp .data ])
158+
124159 resp = self .client .get (self .api_url (), {
125160 'submitter' : 'test@example.org' })
126161 self .assertEqual (0 , len (resp .data ))
127162
128- state_obj_b = create_state (name = 'New' )
129- create_patch (state = state_obj_b )
130- state_obj_c = create_state (name = 'RFC' )
131- create_patch (state = state_obj_c )
132-
133- resp = self .client .get (self .api_url ())
134- self .assertEqual (3 , len (resp .data ))
135- resp = self .client .get (self .api_url (), [('state' , 'under-review' )])
136- self .assertEqual (1 , len (resp .data ))
137- resp = self .client .get (self .api_url (), [('state' , 'under-review' ),
138- ('state' , 'new' )])
139- self .assertEqual (2 , len (resp .data ))
140-
141163 def test_list_version_1_0 (self ):
164+ """List patches using API v1.0."""
142165 create_patch ()
143166
144167 resp = self .client .get (self .api_url (version = '1.0' ))
@@ -148,7 +171,7 @@ def test_list_version_1_0(self):
148171 self .assertNotIn ('web_url' , resp .data [0 ])
149172
150173 def test_detail (self ):
151- """Validate we can get a specific patch."""
174+ """Show a specific patch."""
152175 patch = create_patch (
153176 content = 'Reviewed-by: Test User <test@example.com>\n ' ,
154177 headers = 'Received: from somewhere\n Received: from another place'
@@ -199,27 +222,43 @@ def test_create(self):
199222 resp = self .client .post (self .api_url (), patch )
200223 self .assertEqual (status .HTTP_405_METHOD_NOT_ALLOWED , resp .status_code )
201224
202- def test_update (self ):
203- """Ensure updates can be performed by maintainers."""
204- project = create_project ()
205- patch = create_patch (project = project )
225+ def test_update_anonymous (self ):
226+ """Update patch as anonymous user.
227+
228+ Ensure updates can be performed by maintainers.
229+ """
230+ patch = create_patch ()
206231 state = create_state ()
207232
208- # anonymous user
209233 resp = self .client .patch (self .api_url (patch .id ), {'state' : state .name })
210234 self .assertEqual (status .HTTP_403_FORBIDDEN , resp .status_code )
211235
212- # authenticated user
236+ def test_update_non_maintainer (self ):
237+ """Update patch as non-maintainer.
238+
239+ Ensure updates can be performed by maintainers.
240+ """
241+ patch = create_patch ()
242+ state = create_state ()
213243 user = create_user ()
244+
214245 self .client .force_authenticate (user = user )
215246 resp = self .client .patch (self .api_url (patch .id ), {'state' : state .name })
216247 self .assertEqual (status .HTTP_403_FORBIDDEN , resp .status_code )
217248
218- # maintainer
249+ def test_update_maintainer (self ):
250+ """Update patch as maintainer.
251+
252+ Ensure updates can be performed by maintainers.
253+ """
254+ project = create_project ()
255+ patch = create_patch (project = project )
256+ state = create_state ()
219257 user = create_maintainer (project )
258+
220259 self .client .force_authenticate (user = user )
221- resp = self .client .patch (self .api_url (patch .id ), {
222- 'state' : state .name , 'delegate' : user .id })
260+ resp = self .client .patch (self .api_url (patch .id ),
261+ { 'state' : state .name , 'delegate' : user .id })
223262 self .assertEqual (status .HTTP_200_OK , resp .status_code , resp )
224263 self .assertEqual (Patch .objects .get (id = patch .id ).state , state )
225264 self .assertEqual (Patch .objects .get (id = patch .id ).delegate , user )
@@ -231,22 +270,34 @@ def test_update(self):
231270 self .assertEqual (status .HTTP_200_OK , resp .status_code , resp )
232271 self .assertIsNone (Patch .objects .get (id = patch .id ).delegate )
233272
234- def test_update_invalid (self ):
235- """Ensure we handle invalid Patch updates."""
273+ def test_update_invalid_state (self ):
274+ """Update patch with invalid fields.
275+
276+ Ensure we handle invalid Patch updates.
277+ """
236278 project = create_project ()
237279 state = create_state ()
238280 patch = create_patch (project = project , state = state )
239281 user = create_maintainer (project )
240282
241- # invalid state
242283 self .client .force_authenticate (user = user )
243284 resp = self .client .patch (self .api_url (patch .id ), {'state' : 'foobar' })
244285 self .assertEqual (status .HTTP_400_BAD_REQUEST , resp .status_code )
245286 self .assertContains (resp , 'Expected one of: %s.' % state .name ,
246287 status_code = status .HTTP_400_BAD_REQUEST )
247288
248- # invalid delegate
289+ def test_update_invalid_delegate (self ):
290+ """Update patch with invalid fields.
291+
292+ Ensure we handle invalid Patch updates.
293+ """
294+ project = create_project ()
295+ state = create_state ()
296+ patch = create_patch (project = project , state = state )
297+ user_a = create_maintainer (project )
249298 user_b = create_user ()
299+
300+ self .client .force_authenticate (user = user_a )
250301 resp = self .client .patch (self .api_url (patch .id ),
251302 {'delegate' : user_b .id })
252303 self .assertEqual (status .HTTP_400_BAD_REQUEST , resp .status_code )
0 commit comments