When media is deleted, delete associated comments too.
[mediagoblin.git] / mediagoblin / tests / test_submission.py
index 7c3727451abfb7358fbf7c61b017d99a04730915..1f56779e4cae0c202d24a65756691d0202a9ab54 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
@@ -16,6 +16,7 @@
 
 import urlparse
 import pkg_resources
+import re
 
 from nose.tools import assert_equal, assert_true, assert_false
 
@@ -49,11 +50,17 @@ class TestSubmission:
 
         self.test_user = test_user
 
+        self.login()
+
+    def login(self):
         self.test_app.post(
             '/auth/login/', {
                 'username': u'chris',
                 'password': 'toast'})
 
+    def logout(self):
+        self.test_app.get('/auth/logout/')
+
     def test_missing_fields(self):
         # Test blank form
         # ---------------
@@ -93,6 +100,14 @@ class TestSubmission:
         assert template.TEMPLATE_TEST_CONTEXT.has_key(
             'mediagoblin/user_pages/user.html')
 
+        # Make sure the media view is at least reachable, logged in...
+        self.test_app.get('/u/chris/m/normal-upload-1/')
+        # ... and logged out too.
+        self.logout()
+        self.test_app.get('/u/chris/m/normal-upload-1/')
+        # Log back in for the remaining tests.
+        self.login()
+
         # Test PNG
         # --------
         template.clear_test_template_context()
@@ -125,7 +140,7 @@ class TestSubmission:
         context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/user_pages/user.html']
         request = context['request']
         media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
-        assert_equal(media['tags'],
+        assert_equal(media.tags,
                      [{'name': u'yin', 'slug': u'yin'},
                                             {'name': u'yang', 'slug': u'yang'}])
 
@@ -165,6 +180,18 @@ class TestSubmission:
         # Does media entry exist?
         assert_true(media)
 
+        # Add a comment, so we can test for its deletion later.
+        get_comments = lambda: list(
+            request.db.MediaComment.find({'media_entry': media._id}))
+        assert_false(get_comments())
+        response = self.test_app.post(
+            request.urlgen('mediagoblin.user_pages.media_post_comment',
+                           user=self.test_user.username,
+                           media=media._id),
+            {'comment_content': 'i love this test'})
+        response.follow()
+        assert_true(get_comments())
+
         # Do not confirm deletion
         # ---------------------------------------------------
         response = self.test_app.post(
@@ -204,6 +231,9 @@ class TestSubmission:
             request.db.MediaEntry.find(
                 {'_id': media._id}).count())
 
+        # How about the comment?
+        assert_false(get_comments())
+
     def test_malicious_uploads(self):
         # Test non-suppoerted file with non-supported extension
         # -----------------------------------------------------
@@ -216,7 +246,8 @@ class TestSubmission:
 
         context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
         form = context['submit_form']
-        assert form.file.errors == [u'Invalid file type.']
+        assert re.match(r'^Could not extract any file extension from ".*?"$', str(form.file.errors[0]))
+        assert len(form.file.errors) == 1
 
         # NOTE: The following 2 tests will ultimately fail, but they
         #   *will* pass the initial form submission step.  Instead,
@@ -237,9 +268,9 @@ class TestSubmission:
 
         entry = mg_globals.database.MediaEntry.find_one(
             {'title': 'Malicious Upload 2'})
-        assert_equal(entry['state'], 'failed')
+        assert_equal(entry.state, 'failed')
         assert_equal(
-            entry['fail_error'],
+            entry.fail_error,
             u'mediagoblin.processing:BadMediaFail')
 
         # Test non-supported file with .png extension
@@ -257,7 +288,7 @@ class TestSubmission:
 
         entry = mg_globals.database.MediaEntry.find_one(
             {'title': 'Malicious Upload 3'})
-        assert_equal(entry['state'], 'failed')
+        assert_equal(entry.state, 'failed')
         assert_equal(
-            entry['fail_error'],
+            entry.fail_error,
             u'mediagoblin.processing:BadMediaFail')