aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/tests')
-rw-r--r--mediagoblin/tests/test_auth.py31
-rw-r--r--mediagoblin/tests/test_config.py2
-rw-r--r--mediagoblin/tests/test_misc.py73
-rw-r--r--mediagoblin/tests/test_modelmethods.py147
-rw-r--r--mediagoblin/tests/test_submission.py9
-rw-r--r--mediagoblin/tests/tools.py18
6 files changed, 255 insertions, 25 deletions
diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py
index f4409121..a59a319c 100644
--- a/mediagoblin/tests/test_auth.py
+++ b/mediagoblin/tests/test_auth.py
@@ -40,7 +40,6 @@ def test_bcrypt_check_password():
'notthepassword',
'$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO')
-
# Same thing, but with extra fake salt.
assert not auth_lib.bcrypt_check_password(
'notthepassword',
@@ -58,7 +57,6 @@ def test_bcrypt_gen_password_hash():
assert not auth_lib.bcrypt_check_password(
'notthepassword', hashed_pw)
-
# Same thing, extra salt.
hashed_pw = auth_lib.bcrypt_gen_password_hash(pw, '3><7R45417')
assert auth_lib.bcrypt_check_password(
@@ -77,8 +75,7 @@ def test_register_views(test_app):
test_app.get('/auth/register/')
# Make sure it rendered with the appropriate template
- assert template.TEMPLATE_TEST_CONTEXT.has_key(
- 'mediagoblin/auth/register.html')
+ assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
# Try to register without providing anything, should error
# --------------------------------------------------------
@@ -137,8 +134,7 @@ def test_register_views(test_app):
assert_equal(
urlparse.urlsplit(response.location)[2],
'/u/happygirl/')
- assert template.TEMPLATE_TEST_CONTEXT.has_key(
- 'mediagoblin/user_pages/user.html')
+ assert 'mediagoblin/user_pages/user.html' in template.TEMPLATE_TEST_CONTEXT
## Make sure user is in place
new_user = mg_globals.database.User.find_one(
@@ -231,8 +227,7 @@ def test_register_views(test_app):
assert_equal(
urlparse.urlsplit(response.location)[2],
'/auth/login/')
- assert template.TEMPLATE_TEST_CONTEXT.has_key(
- 'mediagoblin/auth/login.html')
+ assert 'mediagoblin/auth/login.html' in template.TEMPLATE_TEST_CONTEXT
## Make sure link to change password is sent by email
assert len(mail.EMAIL_TEST_INBOX) == 1
@@ -278,7 +273,7 @@ def test_register_views(test_app):
## Verify step 1 of password-change works -- can see form to change password
template.clear_test_template_context()
response = test_app.get("%s?%s" % (path, get_params))
- assert template.TEMPLATE_TEST_CONTEXT.has_key('mediagoblin/auth/change_fp.html')
+ assert 'mediagoblin/auth/change_fp.html' in template.TEMPLATE_TEST_CONTEXT
## Verify step 2.1 of password-change works -- report success to user
template.clear_test_template_context()
@@ -288,8 +283,7 @@ def test_register_views(test_app):
'password': 'iamveryveryhappy',
'token': parsed_get_params['token']})
response.follow()
- assert template.TEMPLATE_TEST_CONTEXT.has_key(
- 'mediagoblin/auth/login.html')
+ assert 'mediagoblin/auth/login.html' in template.TEMPLATE_TEST_CONTEXT
## Verify step 2.2 of password-change works -- login w/ new password success
template.clear_test_template_context()
@@ -303,8 +297,7 @@ def test_register_views(test_app):
assert_equal(
urlparse.urlsplit(response.location)[2],
'/')
- assert template.TEMPLATE_TEST_CONTEXT.has_key(
- 'mediagoblin/root.html')
+ assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
def test_authentication_views():
@@ -318,8 +311,7 @@ def test_authentication_views():
# Get login
# ---------
test_app.get('/auth/login/')
- assert template.TEMPLATE_TEST_CONTEXT.has_key(
- 'mediagoblin/auth/login.html')
+ assert 'mediagoblin/auth/login.html' in template.TEMPLATE_TEST_CONTEXT
# Failed login - blank form
# -------------------------
@@ -383,8 +375,7 @@ def test_authentication_views():
assert_equal(
urlparse.urlsplit(response.location)[2],
'/')
- assert template.TEMPLATE_TEST_CONTEXT.has_key(
- 'mediagoblin/root.html')
+ assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
# Make sure user is in the session
context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
@@ -401,13 +392,12 @@ def test_authentication_views():
assert_equal(
urlparse.urlsplit(response.location)[2],
'/')
- assert template.TEMPLATE_TEST_CONTEXT.has_key(
- 'mediagoblin/root.html')
+ assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
# Make sure the user is not in the session
context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
session = context['request'].session
- assert session.has_key('user_id') == False
+ assert 'user_id' not in session
# User is redirected to custom URL if POST['next'] is set
# -------------------------------------------------------
@@ -420,4 +410,3 @@ def test_authentication_views():
assert_equal(
urlparse.urlsplit(response.location)[2],
'/u/chris/')
-
diff --git a/mediagoblin/tests/test_config.py b/mediagoblin/tests/test_config.py
index 7d8c65c1..b13adae6 100644
--- a/mediagoblin/tests/test_config.py
+++ b/mediagoblin/tests/test_config.py
@@ -36,7 +36,7 @@ def test_read_mediagoblin_config():
assert this_conf['carrotapp']['carrotcake'] == False
assert this_conf['carrotapp']['num_carrots'] == 1
- assert not this_conf['carrotapp'].has_key('encouragement_phrase')
+ assert 'encouragement_phrase' not in this_conf['carrotapp']
assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == True
# A good file
diff --git a/mediagoblin/tests/test_misc.py b/mediagoblin/tests/test_misc.py
index ae5d7e50..776affc6 100644
--- a/mediagoblin/tests/test_misc.py
+++ b/mediagoblin/tests/test_misc.py
@@ -16,9 +16,80 @@
from nose.tools import assert_equal
-from mediagoblin.tests.tools import get_app
+from mediagoblin.db.base import Session
+from mediagoblin.db.models import User, MediaEntry, MediaComment
+from mediagoblin.tests.tools import get_app, \
+ fixture_add_user, fixture_media_entry
+
def test_404_for_non_existent():
test_app = get_app(dump_old_app=False)
res = test_app.get('/does-not-exist/', expect_errors=True)
assert_equal(res.status_int, 404)
+
+
+def test_user_deletes_other_comments():
+ user_a = fixture_add_user(u"chris_a")
+ user_b = fixture_add_user(u"chris_b")
+
+ media_a = fixture_media_entry(uploader=user_a.id, save=False)
+ media_b = fixture_media_entry(uploader=user_b.id, save=False)
+ Session.add(media_a)
+ Session.add(media_b)
+ Session.flush()
+
+ # Create all 4 possible comments:
+ for u_id in (user_a.id, user_b.id):
+ for m_id in (media_a.id, media_b.id):
+ cmt = MediaComment()
+ cmt.media_entry = m_id
+ cmt.author = u_id
+ cmt.content = u"Some Comment"
+ Session.add(cmt)
+
+ Session.flush()
+
+ usr_cnt1 = User.query.count()
+ med_cnt1 = MediaEntry.query.count()
+ cmt_cnt1 = MediaComment.query.count()
+
+ User.query.get(user_a.id).delete(commit=False)
+
+ usr_cnt2 = User.query.count()
+ med_cnt2 = MediaEntry.query.count()
+ cmt_cnt2 = MediaComment.query.count()
+
+ # One user deleted
+ assert_equal(usr_cnt2, usr_cnt1 - 1)
+ # One media gone
+ assert_equal(med_cnt2, med_cnt1 - 1)
+ # Three of four comments gone.
+ assert_equal(cmt_cnt2, cmt_cnt1 - 3)
+
+ User.query.get(user_b.id).delete()
+
+ usr_cnt2 = User.query.count()
+ med_cnt2 = MediaEntry.query.count()
+ cmt_cnt2 = MediaComment.query.count()
+
+ # All users gone
+ assert_equal(usr_cnt2, usr_cnt1 - 2)
+ # All media gone
+ assert_equal(med_cnt2, med_cnt1 - 2)
+ # All comments gone
+ assert_equal(cmt_cnt2, cmt_cnt1 - 4)
+
+
+def test_media_deletes_broken_attachment():
+ user_a = fixture_add_user(u"chris_a")
+
+ media = fixture_media_entry(uploader=user_a.id, save=False)
+ media.attachment_files.append(dict(
+ name=u"some name",
+ filepath=[u"does", u"not", u"exist"],
+ ))
+ Session.add(media)
+ Session.flush()
+
+ MediaEntry.query.get(media.id).delete()
+ User.query.get(user_a.id).delete()
diff --git a/mediagoblin/tests/test_modelmethods.py b/mediagoblin/tests/test_modelmethods.py
new file mode 100644
index 00000000..7719bd97
--- /dev/null
+++ b/mediagoblin/tests/test_modelmethods.py
@@ -0,0 +1,147 @@
+# GNU MediaGoblin -- federated, autonomous media hosting
+# 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Maybe not every model needs a test, but some models have special
+# methods, and so it makes sense to test them here.
+
+from nose.tools import assert_equal
+
+from mediagoblin.db.base import Session
+from mediagoblin.db.models import MediaEntry
+
+from mediagoblin.tests.tools import get_app, \
+ fixture_add_user
+
+import mock
+
+
+class FakeUUID(object):
+ hex = 'testtest-test-test-test-testtesttest'
+
+UUID_MOCK = mock.Mock(return_value=FakeUUID())
+
+
+class TestMediaEntrySlugs(object):
+ def setUp(self):
+ self.test_app = get_app(dump_old_app=True)
+ self.chris_user = fixture_add_user(u'chris')
+ self.emily_user = fixture_add_user(u'emily')
+ self.existing_entry = self._insert_media_entry_fixture(
+ title=u"Beware, I exist!",
+ slug=u"beware-i-exist")
+
+ def _insert_media_entry_fixture(self, title=None, slug=None, this_id=None,
+ uploader=None, save=True):
+ entry = MediaEntry()
+ entry.title = title or u"Some title"
+ entry.slug = slug
+ entry.id = this_id
+ entry.uploader = uploader or self.chris_user.id
+ entry.media_type = u'image'
+
+ if save:
+ entry.save()
+
+ return entry
+
+ def test_unique_slug_from_title(self):
+ entry = self._insert_media_entry_fixture(u"Totally unique slug!", save=False)
+ entry.generate_slug()
+ assert entry.slug == u'totally-unique-slug'
+
+ def test_old_good_unique_slug(self):
+ entry = self._insert_media_entry_fixture(
+ u"A title here", u"a-different-slug-there", save=False)
+ entry.generate_slug()
+ assert entry.slug == u"a-different-slug-there"
+
+ def test_old_weird_slug(self):
+ entry = self._insert_media_entry_fixture(
+ slug=u"wowee!!!!!", save=False)
+ entry.generate_slug()
+ assert entry.slug == u"wowee"
+
+ def test_existing_slug_use_id(self):
+ entry = self._insert_media_entry_fixture(
+ u"Beware, I exist!!", this_id=9000, save=False)
+ entry.generate_slug()
+ assert entry.slug == u"beware-i-exist-9000"
+
+ @mock.patch('uuid.uuid4', UUID_MOCK)
+ def test_existing_slug_cant_use_id(self):
+ # This one grabs the nine thousand slug
+ self._insert_media_entry_fixture(
+ slug=u"beware-i-exist-9000")
+
+ entry = self._insert_media_entry_fixture(
+ u"Beware, I exist!!", this_id=9000, save=False)
+ entry.generate_slug()
+ assert entry.slug == u"beware-i-exist-test"
+
+ @mock.patch('uuid.uuid4', UUID_MOCK)
+ def test_existing_slug_cant_use_id_extra_junk(self):
+ # This one grabs the nine thousand slug
+ self._insert_media_entry_fixture(
+ slug=u"beware-i-exist-9000")
+
+ # This one grabs makes sure the annoyance doesn't stop
+ self._insert_media_entry_fixture(
+ slug=u"beware-i-exist-test")
+
+ entry = self._insert_media_entry_fixture(
+ u"Beware, I exist!!", this_id=9000, save=False)
+ entry.generate_slug()
+ assert entry.slug == u"beware-i-exist-testtest"
+
+ def test_garbage_slug(self):
+ """
+ Titles that sound totally like Q*Bert shouldn't have slugs at
+ all. We'll just reference them by id.
+
+ ,
+ / \ (@!#?@!)
+ |\,/| ,-, /
+ | |#| ( ")~
+ / \|/ \ L L
+ |\,/|\,/|
+ | |#, |#|
+ / \|/ \|/ \
+ |\,/|\,/|\,/|
+ | |#| |#| |#|
+ / \|/ \|/ \|/ \
+ |\,/|\,/|\,/|\,/|
+ | |#| |#| |#| |#|
+ \|/ \|/ \|/ \|/
+ """
+ qbert_entry = self._insert_media_entry_fixture(
+ u"@!#?@!", save=False)
+ qbert_entry.generate_slug()
+ assert qbert_entry.slug is None
+
+
+def test_media_data_init():
+ Session.rollback()
+ Session.remove()
+ media = MediaEntry()
+ media.media_type = u"mediagoblin.media_types.image"
+ assert media.media_data is None
+ media.media_data_init()
+ assert media.media_data is not None
+ obj_in_session = 0
+ for obj in Session():
+ obj_in_session += 1
+ print repr(obj)
+ assert_equal(obj_in_session, 0)
diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py
index 00f1ed3d..fc3d8c83 100644
--- a/mediagoblin/tests/test_submission.py
+++ b/mediagoblin/tests/test_submission.py
@@ -27,6 +27,7 @@ from pkg_resources import resource_filename
from mediagoblin.tests.tools import get_app, \
fixture_add_user
from mediagoblin import mg_globals
+from mediagoblin.db.models import MediaEntry
from mediagoblin.tools import template
from mediagoblin.media_types.image import MEDIA_MANAGER as img_MEDIA_MANAGER
@@ -40,6 +41,7 @@ EVIL_FILE = resource('evil')
EVIL_JPG = resource('evil.jpg')
EVIL_PNG = resource('evil.png')
BIG_BLUE = resource('bigblue.png')
+from .test_exif import GPS_JPG
GOOD_TAG_STRING = u'yin,yang'
BAD_TAG_STRING = unicode('rage,' + 'f' * 26 + 'u' * 26)
@@ -122,7 +124,7 @@ class TestSubmission:
self.check_normal_upload(u'Normal upload 2', GOOD_PNG)
def check_media(self, request, find_data, count=None):
- media = request.db.MediaEntry.find(find_data)
+ media = MediaEntry.find(find_data)
if count is not None:
assert_equal(media.count(), count)
if count == 0:
@@ -265,6 +267,11 @@ class TestSubmission:
# -------------------------------------------
self.check_false_image(u'Malicious Upload 3', EVIL_PNG)
+ def test_media_data(self):
+ self.check_normal_upload(u"With GPS data", GPS_JPG)
+ media = self.check_media(None, {"title": u"With GPS data"}, 1)
+ assert_equal(media.media_data.gps_latitude, 59.336666666666666)
+
def test_processing(self):
data = {'title': u'Big Blue'}
response, request = self.do_post(data, *REQUEST_CONTEXT, do_follow=True,
diff --git a/mediagoblin/tests/tools.py b/mediagoblin/tests/tools.py
index 18d4ec0c..cc4a7add 100644
--- a/mediagoblin/tests/tools.py
+++ b/mediagoblin/tests/tools.py
@@ -25,7 +25,7 @@ from paste.deploy import loadapp
from webtest import TestApp
from mediagoblin import mg_globals
-from mediagoblin.db.models import User, Collection
+from mediagoblin.db.models import User, MediaEntry, Collection
from mediagoblin.tools import testing
from mediagoblin.init.config import read_mediagoblin_config
from mediagoblin.db.open import setup_connection_and_db_from_config
@@ -228,6 +228,22 @@ def fixture_add_user(username=u'chris', password=u'toast',
return test_user
+def fixture_media_entry(title=u"Some title", slug=None,
+ uploader=None, save=True, gen_slug=True):
+ entry = MediaEntry()
+ entry.title = title
+ entry.slug = slug
+ entry.uploader = uploader or fixture_add_user().id
+ entry.media_type = u'image'
+
+ if gen_slug:
+ entry.generate_slug()
+ if save:
+ entry.save()
+
+ return entry
+
+
def fixture_add_collection(name=u"My first Collection", user=None):
if user is None:
user = fixture_add_user()