added tests
authorRodney Ewing <ewing.rj@gmail.com>
Wed, 12 Jun 2013 20:56:43 +0000 (13:56 -0700)
committerRodney Ewing <ewing.rj@gmail.com>
Mon, 26 Aug 2013 13:33:28 +0000 (06:33 -0700)
mediagoblin/submit/views.py
mediagoblin/tests/test_submission.py

index d5f05cb7fc1674a265d0e75eb82d868bca10f95a..2b76abf7d58db845956c56debf650202dd46686e 100644 (file)
@@ -44,7 +44,7 @@ def submit_start(request):
     First view for submitting a file.
     """
     user = request.user
-    if user.upload_limit:
+    if user.upload_limit >= 0:
         upload_limit = user.upload_limit
     else:
         upload_limit = mg_globals.app_config['upload_limit']
@@ -54,8 +54,8 @@ def submit_start(request):
             request,
             messages.WARNING,
             _('Sorry, you have reached your upload limit.'))
-        return redirect(
-            request, '/u/{0}'.format(user.username))
+        return redirect(request, "mediagoblin.user_pages.user_home",
+                        user=request.user.username)
 
     submit_form = submit_forms.SubmitStartForm(request.form,
         license=request.user.license_preference)
@@ -105,15 +105,15 @@ def submit_start(request):
                     entry.queued_media_file) / (1024.0 * 1024)
                 file_size = float('{0:.2f}'.format(file_size))
 
-                # Check if over upload limit
+                # Check if user is over upload limit
                 if (user.uploaded + file_size) >= upload_limit:
                     messages.add_message(
                         request,
                         messages.WARNING,
                         _('Sorry, uploading this file will put you over your'
                           ' upload limit.'))
-                    return redirect(
-                        request, '/u/{0}'.format(user.username))
+                    return redirect(request, "mediagoblin.user_pages.user_home",
+                        user=user.username)
 
                 user.uploaded = user.uploaded + file_size
                 user.save()
@@ -137,7 +137,7 @@ def submit_start(request):
                 add_comment_subscription(request.user, entry)
 
                 return redirect(request, "mediagoblin.user_pages.user_home",
-                                user=request.user.username)
+                                user=user.username)
             except Exception as e:
                 '''
                 This section is intended to catch exceptions raised in
index ac9410637bab31a8ceaa6a3b752fcd15763eb57b..fdc322dc03ac4b1ce64b4f7218c2ddec84f5f2cc 100644 (file)
@@ -24,7 +24,8 @@ import pytest
 
 from mediagoblin.tests.tools import fixture_add_user
 from mediagoblin import mg_globals
-from mediagoblin.db.models import MediaEntry
+from mediagoblin.db.models import MediaEntry, User
+from mediagoblin.db.base import Session
 from mediagoblin.tools import template
 from mediagoblin.media_types.image import ImageMediaManager
 from mediagoblin.media_types.pdf.processing import check_prerequisites as pdf_check_prerequisites
@@ -107,9 +108,38 @@ class TestSubmission:
         self.logout()
         self.test_app.get(url)
 
+    def user_upload_limits(self, uploaded=None, upload_limit=None):
+        if uploaded:
+            self.test_user.uploaded = uploaded
+        if upload_limit:
+            self.test_user.upload_limit = upload_limit
+
+        self.test_user.save()
+
+        # Reload
+        self.test_user = User.query.filter_by(
+            username=self.test_user.username
+        ).first()
+
+        # ... and detach from session:
+        Session.expunge(self.test_user)
+
     def test_normal_jpg(self):
+        # User uploaded should be 0
+        assert self.test_user.uploaded == 0
+
         self.check_normal_upload(u'Normal upload 1', GOOD_JPG)
 
+        # User uploaded should be the same as GOOD_JPG size in Mb
+        file_size = os.stat(GOOD_JPG).st_size / (1024.0 * 1024)
+        file_size = float('{0:.2f}'.format(file_size))
+
+        # Reload user
+        self.test_user = User.query.filter_by(
+            username=self.test_user.username
+        ).first()
+        assert self.test_user.uploaded == file_size
+
     def test_normal_png(self):
         self.check_normal_upload(u'Normal upload 2', GOOD_PNG)
 
@@ -121,6 +151,33 @@ class TestSubmission:
         self.check_url(response, '/u/{0}/'.format(self.test_user.username))
         assert 'mediagoblin/user_pages/user.html' in context
 
+    def test_default_upload_limits(self):
+        self.user_upload_limits(uploaded=500)
+
+        response, context = self.do_post({'title': u'Normal upload 4'},
+                                         do_follow=True,
+                                         **self.upload_data(GOOD_JPG))
+        self.check_url(response, '/u/{0}/'.format(self.test_user.username))
+        assert 'mediagoblin/user_pages/user.html' in context
+
+    def test_user_upload_limit(self):
+        self.user_upload_limits(uploaded=25, upload_limit=25)
+
+        response, context = self.do_post({'title': u'Normal upload 4'},
+                                         do_follow=True,
+                                         **self.upload_data(GOOD_JPG))
+        self.check_url(response, '/u/{0}/'.format(self.test_user.username))
+        assert 'mediagoblin/user_pages/user.html' in context
+
+    def test_user_under_limit(self):
+        self.user_upload_limits(uploaded=499)
+
+        response, context = self.do_post({'title': u'Normal upload 4'},
+                                         do_follow=True,
+                                         **self.upload_data(GOOD_JPG))
+        self.check_url(response, '/u/{0}/'.format(self.test_user.username))
+        assert 'mediagoblin/user_pages/user.html' in context
+
     def check_media(self, request, find_data, count=None):
         media = MediaEntry.query.filter_by(**find_data)
         if count is not None:
@@ -155,6 +212,7 @@ class TestSubmission:
                     'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
 
     def test_delete(self):
+        self.user_upload_limits(uploaded=50)
         response, request = self.do_post({'title': u'Balanced Goblin'},
                                          *REQUEST_CONTEXT, do_follow=True,
                                          **self.upload_data(GOOD_JPG))
@@ -199,6 +257,14 @@ class TestSubmission:
         self.check_media(request, {'id': media_id}, 0)
         self.check_comments(request, media_id, 0)
 
+        # Reload user
+        self.test_user = User.query.filter_by(
+            username = self.test_user.username
+        ).first()
+
+        # Check that user.uploaded is the same as before the upload
+        assert self.test_user.uploaded == 50
+
     def test_evil_file(self):
         # Test non-suppoerted file with non-supported extension
         # -----------------------------------------------------