88from django .conf import settings
99from django .urls import reverse
1010
11+ from patchwork .tests .api import utils
1112from patchwork .tests .utils import create_bundle
1213from patchwork .tests .utils import create_maintainer
1314from patchwork .tests .utils import create_project
@@ -53,19 +54,26 @@ def assertSerialized(self, bundle_obj, bundle_json):
5354 self .assertEqual (bundle_obj .project .id ,
5455 bundle_json ['project' ]['id' ])
5556
56- def test_list (self ):
57- """Validate we can list bundles ."""
57+ def test_list_empty (self ):
58+ """List bundles when none are present ."""
5859 resp = self .client .get (self .api_url ())
5960 self .assertEqual (status .HTTP_200_OK , resp .status_code )
6061 self .assertEqual (0 , len (resp .data ))
6162
63+ def _create_bundles (self ):
6264 user = create_user (username = 'myuser' )
6365 project = create_project (linkname = 'myproject' )
6466 bundle_public = create_bundle (public = True , owner = user ,
6567 project = project )
6668 bundle_private = create_bundle (public = False , owner = user ,
6769 project = project )
6870
71+ return user , project , bundle_public , bundle_private
72+
73+ def test_list_anonymous (self ):
74+ """List bundles as anonymous user."""
75+ user , project , bundle_public , _ = self ._create_bundles ()
76+
6977 # anonymous users
7078 # should only see the public bundle
7179 resp = self .client .get (self .api_url ())
@@ -74,6 +82,11 @@ def test_list(self):
7482 bundle_rsp = resp .data [0 ]
7583 self .assertSerialized (bundle_public , bundle_rsp )
7684
85+ @utils .store_samples ('bundle-list' )
86+ def test_list_authenticated (self ):
87+ """List bundles as an authenticated user."""
88+ user , project , bundle_public , bundle_private = self ._create_bundles ()
89+
7790 # authenticated user
7891 # should see the public and private bundle
7992 self .client .force_authenticate (user = user )
@@ -84,14 +97,24 @@ def test_list(self):
8497 resp .data , [bundle_public , bundle_private ]):
8598 self .assertSerialized (bundle_obj , bundle_rsp )
8699
100+ def test_list_filter_project (self ):
101+ """Filter bundles by project."""
102+ user , project , bundle_public , bundle_private = self ._create_bundles ()
103+
87104 # test filtering by project
105+ self .client .force_authenticate (user = user )
88106 resp = self .client .get (self .api_url (), {'project' : 'myproject' })
89107 self .assertEqual ([bundle_public .id , bundle_private .id ],
90108 [x ['id' ] for x in resp .data ])
91109 resp = self .client .get (self .api_url (), {'project' : 'invalidproject' })
92110 self .assertEqual (0 , len (resp .data ))
93111
112+ def test_list_filter_owner (self ):
113+ """Filter bundles by owner."""
114+ user , project , bundle_public , bundle_private = self ._create_bundles ()
115+
94116 # test filtering by owner, both ID and username
117+ self .client .force_authenticate (user = user )
95118 resp = self .client .get (self .api_url (), {'owner' : user .id })
96119 self .assertEqual ([bundle_public .id , bundle_private .id ],
97120 [x ['id' ] for x in resp .data ])
@@ -101,28 +124,63 @@ def test_list(self):
101124 resp = self .client .get (self .api_url (), {'owner' : 'otheruser' })
102125 self .assertEqual (0 , len (resp .data ))
103126
127+ @utils .store_samples ('bundle-list-1.0' )
104128 def test_list_version_1_0 (self ):
105- """Validate that newer fields are dropped for older API versions."""
106- create_bundle (public = True )
129+ """List bundles using API v1.0.
130+
131+ Validate that newer fields are dropped for older API versions.
132+ """
133+ user , _ , _ , _ = self ._create_bundles ()
107134
135+ self .client .force_authenticate (user = user )
108136 resp = self .client .get (self .api_url (version = '1.0' ))
109137 self .assertEqual (status .HTTP_200_OK , resp .status_code )
110- self .assertEqual (1 , len (resp .data ))
138+ self .assertEqual (2 , len (resp .data ))
111139 self .assertIn ('url' , resp .data [0 ])
112140 self .assertNotIn ('web_url' , resp .data [0 ])
113141
114- def test_detail (self ):
115- """Validate we can get a specific bundle."""
116- bundle = create_bundle (public = True )
142+ def test_detail_anonymous_public (self ):
143+ """Show public bundle as anonymous user.
144+
145+ Validate we can get a public bundle.
146+ """
147+ user , _ , bundle , _ = self ._create_bundles ()
117148
118149 resp = self .client .get (self .api_url (bundle .id ))
119150 self .assertEqual (status .HTTP_200_OK , resp .status_code )
120151 self .assertSerialized (bundle , resp .data )
121152
153+ @utils .store_samples ('bundle-detail-error-not-found' )
154+ def test_detail_anonymous_private (self ):
155+ """Show private bundle as anonymous user.
156+
157+ Validate we cannot get a private bundle if we're not the owner.
158+ """
159+ user , _ , _ , bundle = self ._create_bundles ()
160+
161+ resp = self .client .get (self .api_url (bundle .id ))
162+ self .assertEqual (status .HTTP_404_NOT_FOUND , resp .status_code )
163+
164+ @utils .store_samples ('bundle-detail' )
165+ def test_detail_authenticated (self ):
166+ """Show private bundle as authenticated user.
167+
168+ Validate we can get a private bundle if we're the owner.
169+ """
170+ user , _ , _ , bundle = self ._create_bundles ()
171+
172+ self .client .force_authenticate (user = user )
173+ resp = self .client .get (self .api_url (bundle .id ))
174+ self .assertEqual (status .HTTP_200_OK , resp .status_code )
175+ self .assertSerialized (bundle , resp .data )
176+
177+ @utils .store_samples ('bundle-detail-1.0' )
122178 def test_detail_version_1_0 (self ):
123- bundle = create_bundle (public = True )
179+ """Show bundle using API v1.0."""
180+ user , _ , bundle , _ = self ._create_bundles ()
124181
125182 resp = self .client .get (self .api_url (bundle .id , version = '1.0' ))
183+ self .assertEqual (status .HTTP_200_OK , resp .status_code )
126184 self .assertIn ('url' , resp .data )
127185 self .assertNotIn ('web_url' , resp .data )
128186
0 commit comments