8
8
from django .test import SimpleTestCase , TestCase , override_settings
9
9
from django .urls import reverse
10
10
from django .utils .safestring import SafeString
11
+ from django .utils .timezone import now
11
12
12
13
from members .models import MEMBERSHIP_LEVELS , PLATINUM_MEMBERSHIP , CorporateMember
13
14
@@ -143,6 +144,27 @@ def test_preview(self):
143
144
self .assertEqual (Release .objects .preview ().version , "1.9b2" )
144
145
145
146
147
+ class ReleaseTestCase (TestCase ):
148
+ def test_is_published (self ):
149
+ today = now ().date ()
150
+ future = today + datetime .timedelta (days = 1 )
151
+ past = today - datetime .timedelta (days = 1 )
152
+ cases = [
153
+ ({"date" : None , "is_active" : True }, False ),
154
+ ({"date" : None , "is_active" : False }, False ),
155
+ ({"date" : today , "is_active" : True }, True ),
156
+ ({"date" : today , "is_active" : False }, False ),
157
+ ({"date" : past , "is_active" : True }, True ),
158
+ ({"date" : past , "is_active" : False }, False ),
159
+ ({"date" : future , "is_active" : True }, False ),
160
+ ({"date" : future , "is_active" : False }, False ),
161
+ ]
162
+ for i , (params , expected ) in enumerate (cases ):
163
+ release = Release .objects .create (version = f"{ i } .0" , ** params )
164
+ with self .subTest (** params ):
165
+ self .assertIs (release .is_published , expected )
166
+
167
+
146
168
class ReleaseUploadToTestCase (SimpleTestCase ):
147
169
def test_upload_to_artifact (self ):
148
170
for version , filename , expected in [
@@ -201,15 +223,33 @@ def setUpClass(cls):
201
223
cls .form_class = admin .site .get_model_admin (Release ).get_form (request = None )
202
224
203
225
def test_non_published_releases_tarball_not_required (self ):
204
- form = self .form_class ({"version" : "1.0" , "date" : None })
205
- self .assertTrue (form .is_valid (), form .errors )
226
+ today = now ().date ()
227
+ future = today + datetime .timedelta (days = 1 )
228
+ past = today - datetime .timedelta (days = 1 )
229
+ cases = [
230
+ ({"date" : None , "is_active" : True }, False ),
231
+ ({"date" : None , "is_active" : False }, False ),
232
+ ({"date" : today , "is_active" : True }, True ),
233
+ ({"date" : today , "is_active" : False }, False ),
234
+ ({"date" : past , "is_active" : True }, True ),
235
+ ({"date" : past , "is_active" : False }, False ),
236
+ ({"date" : future , "is_active" : True }, False ),
237
+ ({"date" : future , "is_active" : False }, False ),
238
+ ]
239
+ for i , (params , tarball_required ) in enumerate (cases ):
240
+ form = self .form_class ({"version" : f"{ i } .0" , ** params })
241
+ with self .subTest (** params ):
242
+ self .assertIs (form .is_valid (), not tarball_required , form .errors )
206
243
207
- def test_published_releases_tarball_required (self ):
208
- form = self .form_class ({"version" : "1.0" , "date" : "2008-09-03" })
244
+ def test_published_release_tarball_required (self ):
245
+ form = self .form_class (
246
+ {"version" : "1.0" , "date" : "2008-09-03" , "is_active" : True }
247
+ )
248
+ self .assertFalse (form .is_valid ())
209
249
self .assertFormError (
210
250
form ,
211
251
"tarball" ,
212
- "This field is required when the release is active by having a date " ,
252
+ "This field is required when the release is active. " ,
213
253
)
214
254
215
255
def test_checksum_required_if_tarball_provided (self ):
@@ -220,7 +260,7 @@ def test_checksum_required_if_tarball_provided(self):
220
260
self .assertFormError (
221
261
form ,
222
262
"checksum" ,
223
- "This field is required when an artifact has been uploaded" ,
263
+ "This field is required when an artifact has been uploaded. " ,
224
264
)
225
265
226
266
def test_checksum_required_if_wheel_provided (self ):
@@ -231,7 +271,7 @@ def test_checksum_required_if_wheel_provided(self):
231
271
self .assertFormError (
232
272
form ,
233
273
"checksum" ,
234
- "This field is required when an artifact has been uploaded" ,
274
+ "This field is required when an artifact has been uploaded. " ,
235
275
)
236
276
237
277
def test_artifact_filename_validation_valid (self ):
@@ -340,6 +380,7 @@ class RedirectViewTestCase(TestCase):
340
380
def test_redirect (self ):
341
381
Release .objects .create (
342
382
version = "1.0" ,
383
+ is_active = True ,
343
384
tarball = "test.tar.gz" ,
344
385
wheel = "test.whl" ,
345
386
checksum = "test.checksum.txt" ,
@@ -355,13 +396,40 @@ def test_redirect(self):
355
396
self .assertRedirects (response , url , 301 , fetch_redirect_response = False )
356
397
357
398
def test_redirect_404 (self ):
358
- Release .objects .create (version = "1.0" )
399
+ Release .objects .create (version = "1.0" , is_active = True )
359
400
360
401
for kind in ["tarball" , "wheel" , "checksum" ]:
361
402
response = self .client .get (f"/download/1.0/{ kind } /" )
362
403
with self .subTest (kind = kind ):
363
404
self .assertEqual (response .status_code , 404 )
364
405
406
+ def test_redirect_is_not_published (self ):
407
+ today = now ().date ()
408
+ future = today + datetime .timedelta (days = 1 )
409
+ past = today - datetime .timedelta (days = 1 )
410
+ cases = [
411
+ ({"date" : None , "is_active" : True }, 404 ),
412
+ ({"date" : None , "is_active" : False }, 404 ),
413
+ ({"date" : today , "is_active" : True }, 301 ),
414
+ ({"date" : today , "is_active" : False }, 404 ),
415
+ ({"date" : past , "is_active" : True }, 301 ),
416
+ ({"date" : past , "is_active" : False }, 404 ),
417
+ ({"date" : future , "is_active" : True }, 404 ),
418
+ ({"date" : future , "is_active" : False }, 404 ),
419
+ ]
420
+ for i , (params , status_code ) in enumerate (cases ):
421
+ release = Release .objects .create (
422
+ version = f"{ i } .0" ,
423
+ tarball = "test.tar.gz" ,
424
+ wheel = "test.whl" ,
425
+ checksum = "test.checksum.txt" ,
426
+ ** params ,
427
+ )
428
+ for kind in ["tarball" , "wheel" , "checksum" ]:
429
+ response = self .client .get (f"/download/{ i } .0/{ kind } /" )
430
+ with self .subTest (kind = kind , ** params ):
431
+ self .assertEqual (response .status_code , status_code )
432
+
365
433
366
434
class CorporateMembersTestCase (TestCase ):
367
435
@classmethod
0 commit comments