diff options
Diffstat (limited to 'mediagoblin/tests')
26 files changed, 534 insertions, 299 deletions
diff --git a/mediagoblin/tests/conftest.py b/mediagoblin/tests/conftest.py new file mode 100644 index 00000000..25f495d6 --- /dev/null +++ b/mediagoblin/tests/conftest.py @@ -0,0 +1,15 @@ +from mediagoblin.tests import tools + +import pytest + +@pytest.fixture() +def test_app(request): + """ + py.test fixture to pass sandboxed mediagoblin applications into tests that + want them. + + You could make a local version of this method for your own tests + to override the paste and config files being used by passing them + in differently to get_app. + """ + return tools.get_app(request) diff --git a/mediagoblin/tests/pytest.ini b/mediagoblin/tests/pytest.ini new file mode 100644 index 00000000..d4aa2d69 --- /dev/null +++ b/mediagoblin/tests/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +usefixtures = tmpdir
\ No newline at end of file diff --git a/mediagoblin/tests/test_api.py b/mediagoblin/tests/test_api.py index 82b1c1b4..cff25776 100644 --- a/mediagoblin/tests/test_api.py +++ b/mediagoblin/tests/test_api.py @@ -20,9 +20,11 @@ import base64 from pkg_resources import resource_filename +import pytest + from mediagoblin import mg_globals from mediagoblin.tools import template, pluginapi -from mediagoblin.tests.tools import get_app, fixture_add_user +from mediagoblin.tests.tools import fixture_add_user _log = logging.getLogger(__name__) @@ -43,15 +45,14 @@ BIG_BLUE = resource('bigblue.png') class TestAPI(object): - def setUp(self): - self.app = get_app(dump_old_app=False) + def setup(self): self.db = mg_globals.database self.user_password = u'4cc355_70k3N' self.user = fixture_add_user(u'joapi', self.user_password) - def login(self): - self.app.post( + def login(self, test_app): + test_app.post( '/auth/login/', { 'username': self.user.username, 'password': self.user_password}) @@ -65,14 +66,14 @@ class TestAPI(object): self.user.username, self.user_password])))} - def do_post(self, data, **kwargs): + def do_post(self, data, test_app, **kwargs): url = kwargs.pop('url', '/api/submit') do_follow = kwargs.pop('do_follow', False) if not 'headers' in kwargs.keys(): kwargs['headers'] = self.http_auth_headers() - response = self.app.post(url, data, **kwargs) + response = test_app.post(url, data, **kwargs) if do_follow: response.follow() @@ -82,21 +83,22 @@ class TestAPI(object): def upload_data(self, filename): return {'upload_files': [('file', filename)]} - def test_1_test_test_view(self): - self.login() + def test_1_test_test_view(self, test_app): + self.login(test_app) - response = self.app.get( + response = test_app.get( '/api/test', headers=self.http_auth_headers()) assert response.body == \ '{"username": "joapi", "email": "joapi@example.com"}' - def test_2_test_submission(self): - self.login() + def test_2_test_submission(self, test_app): + self.login(test_app) response = self.do_post( {'title': 'Great JPG!'}, + test_app, **self.upload_data(GOOD_JPG)) assert response.status_int == 200 diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index f4409121..755727f9 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -17,12 +17,10 @@ import urlparse import datetime -from nose.tools import assert_equal - from mediagoblin import mg_globals from mediagoblin.auth import lib as auth_lib from mediagoblin.db.models import User -from mediagoblin.tests.tools import setup_fresh_app, get_app, fixture_add_user +from mediagoblin.tests.tools import fixture_add_user from mediagoblin.tools import template, mail @@ -40,7 +38,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 +55,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( @@ -67,7 +63,6 @@ def test_bcrypt_gen_password_hash(): 'notthepassword', hashed_pw, '3><7R45417') -@setup_fresh_app def test_register_views(test_app): """ Massive test function that all our registration-related views all work. @@ -77,8 +72,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 # -------------------------------------------------------- @@ -105,8 +99,8 @@ def test_register_views(test_app): context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] form = context['register_form'] - assert_equal (form.username.errors, [u'Field must be between 3 and 30 characters long.']) - assert_equal (form.password.errors, [u'Field must be between 5 and 1024 characters long.']) + assert form.username.errors == [u'Field must be between 3 and 30 characters long.'] + assert form.password.errors == [u'Field must be between 5 and 1024 characters long.'] ## bad form template.clear_test_template_context() @@ -117,11 +111,11 @@ def test_register_views(test_app): context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] form = context['register_form'] - assert_equal (form.username.errors, [u'This field does not take email addresses.']) - assert_equal (form.email.errors, [u'This field requires an email address.']) + assert form.username.errors == [u'This field does not take email addresses.'] + assert form.email.errors == [u'This field requires an email address.'] ## At this point there should be no users in the database ;) - assert_equal(User.query.count(), 0) + assert User.query.count() == 0 # Successful register # ------------------- @@ -134,11 +128,8 @@ def test_register_views(test_app): response.follow() ## Did we redirect to the proper page? Use the right template? - assert_equal( - urlparse.urlsplit(response.location)[2], - '/u/happygirl/') - assert template.TEMPLATE_TEST_CONTEXT.has_key( - 'mediagoblin/user_pages/user.html') + assert urlparse.urlsplit(response.location)[2] == '/u/happygirl/' + 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( @@ -228,11 +219,8 @@ def test_register_views(test_app): response.follow() ## Did we redirect to the proper page? Use the right template? - assert_equal( - urlparse.urlsplit(response.location)[2], - '/auth/login/') - assert template.TEMPLATE_TEST_CONTEXT.has_key( - 'mediagoblin/auth/login.html') + assert urlparse.urlsplit(response.location)[2] == '/auth/login/' + 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 @@ -262,7 +250,7 @@ def test_register_views(test_app): response = test_app.get( "/auth/forgot_password/verify/?userid=%s&token=total_bs" % unicode( new_user.id), status=404) - assert_equal(response.status.split()[0], u'404') # status="404 NOT FOUND" + assert response.status.split()[0] == u'404' # status="404 NOT FOUND" ## Try using an expired token to change password, shouldn't work template.clear_test_template_context() @@ -271,14 +259,14 @@ def test_register_views(test_app): new_user.fp_token_expire = datetime.datetime.now() new_user.save() response = test_app.get("%s?%s" % (path, get_params), status=404) - assert_equal(response.status.split()[0], u'404') # status="404 NOT FOUND" + assert response.status.split()[0] == u'404' # status="404 NOT FOUND" new_user.fp_token_expire = real_token_expiration new_user.save() ## 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 +276,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() @@ -300,26 +287,21 @@ def test_register_views(test_app): # User should be redirected response.follow() - assert_equal( - urlparse.urlsplit(response.location)[2], - '/') - assert template.TEMPLATE_TEST_CONTEXT.has_key( - 'mediagoblin/root.html') + assert urlparse.urlsplit(response.location)[2] == '/' + assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT -def test_authentication_views(): +def test_authentication_views(test_app): """ Test logging in and logging out """ - test_app = get_app(dump_old_app=False) # Make a new user test_user = fixture_add_user(active_user=False) # 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 # ------------------------- @@ -380,11 +362,8 @@ def test_authentication_views(): # User should be redirected response.follow() - assert_equal( - urlparse.urlsplit(response.location)[2], - '/') - assert template.TEMPLATE_TEST_CONTEXT.has_key( - 'mediagoblin/root.html') + assert urlparse.urlsplit(response.location)[2] == '/' + assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT # Make sure user is in the session context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] @@ -398,16 +377,13 @@ def test_authentication_views(): # Should be redirected to index page response.follow() - assert_equal( - urlparse.urlsplit(response.location)[2], - '/') - assert template.TEMPLATE_TEST_CONTEXT.has_key( - 'mediagoblin/root.html') + assert urlparse.urlsplit(response.location)[2] == '/' + 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 # ------------------------------------------------------- @@ -417,7 +393,4 @@ def test_authentication_views(): 'username': u'chris', 'password': 'toast', 'next' : '/u/chris/'}) - assert_equal( - urlparse.urlsplit(response.location)[2], - '/u/chris/') - + assert urlparse.urlsplit(response.location)[2] == '/u/chris/' diff --git a/mediagoblin/tests/test_cache.py b/mediagoblin/tests/test_cache.py index 48fa1386..403173cd 100644 --- a/mediagoblin/tests/test_cache.py +++ b/mediagoblin/tests/test_cache.py @@ -15,7 +15,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. -from mediagoblin.tests.tools import setup_fresh_app from mediagoblin import mg_globals @@ -37,7 +36,6 @@ def _get_some_data(key): return value -@setup_fresh_app def test_cache_working(test_app): some_data_cache = mg_globals.cache.get_cache('sum_data') assert not some_data_cache.has_key('herp') diff --git a/mediagoblin/tests/test_collections.py b/mediagoblin/tests/test_collections.py index b19f6362..87782f30 100644 --- a/mediagoblin/tests/test_collections.py +++ b/mediagoblin/tests/test_collections.py @@ -14,17 +14,12 @@ # 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/>. -from mediagoblin.tests.tools import fixture_add_collection, fixture_add_user, \ - get_app +from mediagoblin.tests.tools import fixture_add_collection, fixture_add_user from mediagoblin.db.models import Collection, User -from mediagoblin.db.base import Session -from nose.tools import assert_equal -def test_user_deletes_collection(): +def test_user_deletes_collection(test_app): # Setup db. - get_app(dump_old_app=False) - user = fixture_add_user() coll = fixture_add_collection(user=user) # Reload into session: @@ -34,4 +29,4 @@ def test_user_deletes_collection(): user.delete() cnt2 = Collection.query.count() - assert_equal(cnt1, cnt2 + 1) + assert cnt1 == cnt2 + 1 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_csrf_middleware.py b/mediagoblin/tests/test_csrf_middleware.py index e720264c..a272caf6 100644 --- a/mediagoblin/tests/test_csrf_middleware.py +++ b/mediagoblin/tests/test_csrf_middleware.py @@ -14,12 +14,10 @@ # 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/>. -from mediagoblin.tests.tools import get_app from mediagoblin import mg_globals -def test_csrf_cookie_set(): - test_app = get_app(dump_old_app=False) +def test_csrf_cookie_set(test_app): cookie_name = mg_globals.app_config['csrf_cookie_name'] # get login page @@ -33,11 +31,14 @@ def test_csrf_cookie_set(): assert response.headers.get('Vary', False) == 'Cookie' -def test_csrf_token_must_match(): - # We need a fresh app for this test on webtest < 1.3.6. - # We do not understand why, but it fixes the tests. - # If we require webtest >= 1.3.6, we can switch to a non fresh app here. - test_app = get_app(dump_old_app=True) +# We need a fresh app for this test on webtest < 1.3.6. +# We do not understand why, but it fixes the tests. +# If we require webtest >= 1.3.6, we can switch to a non fresh app here. +# +# ... this comment might be irrelevant post-pytest-fixtures, but I'm not +# removing it yet in case we move to module-level tests :) +# -- cwebber +def test_csrf_token_must_match(test_app): # construct a request with no cookie or form token assert test_app.post('/auth/login/', @@ -67,8 +68,7 @@ def test_csrf_token_must_match(): extra_environ={'gmg.verify_csrf': True}).\ status_int == 200 -def test_csrf_exempt(): - test_app = get_app(dump_old_app=False) +def test_csrf_exempt(test_app): # monkey with the views to decorate a known endpoint import mediagoblin.auth.views from mediagoblin.meddleware.csrf import csrf_exempt diff --git a/mediagoblin/tests/test_edit.py b/mediagoblin/tests/test_edit.py index 7db6eaea..cda2607f 100644 --- a/mediagoblin/tests/test_edit.py +++ b/mediagoblin/tests/test_edit.py @@ -14,35 +14,35 @@ # 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/>. -from nose.tools import assert_equal +import pytest from mediagoblin import mg_globals from mediagoblin.db.models import User -from mediagoblin.tests.tools import get_app, fixture_add_user +from mediagoblin.tests.tools import fixture_add_user from mediagoblin.tools import template from mediagoblin.auth.lib import bcrypt_check_password class TestUserEdit(object): - def setUp(self): - self.app = get_app(dump_old_app=False) + def setup(self): # set up new user self.user_password = u'toast' self.user = fixture_add_user(password = self.user_password) - self.login() - def login(self): - self.app.post( + def login(self, test_app): + test_app.post( '/auth/login/', { 'username': self.user.username, 'password': self.user_password}) - def test_user_deletion(self): + def test_user_deletion(self, test_app): """Delete user via web interface""" + self.login(test_app) + # Make sure user exists assert User.query.filter_by(username=u'chris').first() - res = self.app.post('/edit/account/delete/', {'confirmed': 'y'}) + res = test_app.post('/edit/account/delete/', {'confirmed': 'y'}) # Make sure user has been deleted assert User.query.filter_by(username=u'chris').first() == None @@ -52,14 +52,16 @@ class TestUserEdit(object): #Restore user at end of test self.user = fixture_add_user(password = self.user_password) - self.login() + self.login(test_app) - def test_change_password(self): + def test_change_password(self, test_app): """Test changing password correctly and incorrectly""" + self.login(test_app) + # test that the password can be changed # template.clear_test_template_context() - res = self.app.post( + res = test_app.post( '/edit/account/', { 'old_password': 'toast', 'new_password': '123456', @@ -67,7 +69,7 @@ class TestUserEdit(object): }) # Check for redirect on success - assert_equal(res.status_int, 302) + assert res.status_int == 302 # test_user has to be fetched again in order to have the current values test_user = User.query.filter_by(username=u'chris').first() assert bcrypt_check_password('123456', test_user.pw_hash) @@ -76,7 +78,7 @@ class TestUserEdit(object): # test that the password cannot be changed if the given # old_password is wrong template.clear_test_template_context() - self.app.post( + test_app.post( '/edit/account/', { 'old_password': 'toast', 'new_password': '098765', @@ -86,50 +88,54 @@ class TestUserEdit(object): assert not bcrypt_check_password('098765', test_user.pw_hash) - - def test_change_bio_url(self): + def test_change_bio_url(self, test_app): """Test changing bio and URL""" + self.login(test_app) + # Test if legacy profile editing URL redirects correctly - res = self.app.post( + res = test_app.post( '/edit/profile/', { 'bio': u'I love toast!', 'url': u'http://dustycloud.org/'}, expect_errors=True) # Should redirect to /u/chris/edit/ - assert_equal (res.status_int, 302) + assert res.status_int == 302 assert res.headers['Location'].endswith("/u/chris/edit/") - res = self.app.post( + res = test_app.post( '/u/chris/edit/', { 'bio': u'I love toast!', 'url': u'http://dustycloud.org/'}) test_user = User.query.filter_by(username=u'chris').first() - assert_equal(test_user.bio, u'I love toast!') - assert_equal(test_user.url, u'http://dustycloud.org/') + assert test_user.bio == u'I love toast!' + assert test_user.url == u'http://dustycloud.org/' # change a different user than the logged in (should fail with 403) fixture_add_user(username=u"foo") - res = self.app.post( + res = test_app.post( '/u/foo/edit/', { 'bio': u'I love toast!', 'url': u'http://dustycloud.org/'}, expect_errors=True) - assert_equal(res.status_int, 403) + assert res.status_int == 403 # test changing the bio and the URL inproperly too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't' - self.app.post( + test_app.post( '/u/chris/edit/', { # more than 500 characters 'bio': too_long_bio, 'url': 'this-is-no-url'}) # Check form errors - context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/edit/edit_profile.html'] + context = template.TEMPLATE_TEST_CONTEXT[ + 'mediagoblin/edit/edit_profile.html'] form = context['form'] - assert_equal(form.bio.errors, [u'Field must be between 0 and 500 characters long.']) - assert_equal(form.url.errors, [u'This address contains errors']) + assert form.bio.errors == [ + u'Field must be between 0 and 500 characters long.'] + assert form.url.errors == [ + u'This address contains errors'] # test changing the url inproperly diff --git a/mediagoblin/tests/test_globals.py b/mediagoblin/tests/test_globals.py index 303f89e2..fe3088f8 100644 --- a/mediagoblin/tests/test_globals.py +++ b/mediagoblin/tests/test_globals.py @@ -14,16 +14,16 @@ # 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/>. -from nose.tools import assert_raises +import pytest from mediagoblin import mg_globals class TestGlobals(object): - def setUp(self): + def setup(self): self.old_database = mg_globals.database - def tearDown(self): + def teardown(self): mg_globals.database = self.old_database def test_setup_globals(self): @@ -36,7 +36,7 @@ class TestGlobals(object): assert mg_globals.public_store == 'my favorite public_store!' assert mg_globals.queue_store == 'my favorite queue_store!' - assert_raises( + pytest.raises( AssertionError, mg_globals.setup_globals, - no_such_global_foo = "Dummy") + no_such_global_foo="Dummy") diff --git a/mediagoblin/tests/test_http_callback.py b/mediagoblin/tests/test_http_callback.py index 8bee7045..e2c85d0d 100644 --- a/mediagoblin/tests/test_http_callback.py +++ b/mediagoblin/tests/test_http_callback.py @@ -20,28 +20,27 @@ from urlparse import urlparse, parse_qs from mediagoblin import mg_globals from mediagoblin.tools import processing -from mediagoblin.tests.tools import get_app, fixture_add_user +from mediagoblin.tests.tools import fixture_add_user from mediagoblin.tests.test_submission import GOOD_PNG from mediagoblin.tests import test_oauth as oauth class TestHTTPCallback(object): - def setUp(self): - self.app = get_app(dump_old_app=False) + def _setup(self, test_app): self.db = mg_globals.database self.user_password = u'secret' self.user = fixture_add_user(u'call_back', self.user_password) - self.login() + self.login(test_app) - def login(self): - self.app.post('/auth/login/', { + def login(self, testapp): + testapp.post('/auth/login/', { 'username': self.user.username, 'password': self.user_password}) - def get_access_token(self, client_id, client_secret, code): - response = self.app.get('/oauth/access_token', { + def get_access_token(self, testapp, client_id, client_secret, code): + response = testapp.get('/oauth/access_token', { 'code': code, 'client_id': client_id, 'client_secret': client_secret}) @@ -50,13 +49,15 @@ class TestHTTPCallback(object): return response_data['access_token'] - def test_callback(self): + def test_callback(self, test_app): ''' Test processing HTTP callback ''' + self._setup(test_app) self.oauth = oauth.TestOAuth() - self.oauth.setUp() + self.oauth._setup(test_app) - redirect, client_id = self.oauth.test_4_authorize_confidential_client() + redirect, client_id = self.oauth.test_4_authorize_confidential_client( + test_app) code = parse_qs(urlparse(redirect.location).query)['code'][0] @@ -65,11 +66,11 @@ class TestHTTPCallback(object): client_secret = client.secret - access_token = self.get_access_token(client_id, client_secret, code) + access_token = self.get_access_token(test_app, client_id, client_secret, code) callback_url = 'https://foo.example?secrettestmediagoblinparam' - res = self.app.post('/api/submit?client_id={0}&access_token={1}\ + res = test_app.post('/api/submit?client_id={0}&access_token={1}\ &client_secret={2}'.format( client_id, access_token, diff --git a/mediagoblin/tests/test_messages.py b/mediagoblin/tests/test_messages.py index 4c0f3e2e..3ac917b0 100644 --- a/mediagoblin/tests/test_messages.py +++ b/mediagoblin/tests/test_messages.py @@ -15,18 +15,15 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from mediagoblin.messages import fetch_messages, add_message -from mediagoblin.tests.tools import get_app from mediagoblin.tools import template - -def test_messages(): +def test_messages(test_app): """ Added messages should show up in the request.session, fetched messages should be the same as the added ones, and fetching should clear the message list. """ - test_app = get_app(dump_old_app=False) # Aquire a request object test_app.get('/') context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] diff --git a/mediagoblin/tests/test_misc.py b/mediagoblin/tests/test_misc.py index ae5d7e50..755d863f 100644 --- a/mediagoblin/tests/test_misc.py +++ b/mediagoblin/tests/test_misc.py @@ -14,11 +14,78 @@ # 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/>. -from nose.tools import assert_equal +from mediagoblin.db.base import Session +from mediagoblin.db.models import User, MediaEntry, MediaComment +from mediagoblin.tests.tools import fixture_add_user, fixture_media_entry -from mediagoblin.tests.tools import get_app -def test_404_for_non_existent(): - test_app = get_app(dump_old_app=False) +def test_404_for_non_existent(test_app): res = test_app.get('/does-not-exist/', expect_errors=True) - assert_equal(res.status_int, 404) + assert res.status_int == 404 + + +def test_user_deletes_other_comments(test_app): + 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 usr_cnt2 == usr_cnt1 - 1 + # One media gone + assert med_cnt2 == med_cnt1 - 1 + # Three of four comments gone. + assert 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 usr_cnt2 == usr_cnt1 - 2 + # All media gone + assert med_cnt2 == med_cnt1 - 2 + # All comments gone + assert cmt_cnt2 == cmt_cnt1 - 4 + + +def test_media_deletes_broken_attachment(test_app): + 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..427aa47c --- /dev/null +++ b/mediagoblin/tests/test_modelmethods.py @@ -0,0 +1,167 @@ +# 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 mediagoblin.db.base import Session +from mediagoblin.db.models import MediaEntry + +from mediagoblin.tests.tools import 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.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, test_app): + self._setup() + + 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, test_app): + self._setup() + + 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, test_app): + self._setup() + + 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, test_app): + self._setup() + + 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" + + def test_existing_slug_cant_use_id(self, test_app): + self._setup() + + # Getting tired of dealing with test_app and this mock.patch + # thing conflicting, getting lazy. + @mock.patch('uuid.uuid4', UUID_MOCK) + def _real_test(): + # 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" + + _real_test() + + def test_existing_slug_cant_use_id_extra_junk(self, test_app): + self._setup() + + # Getting tired of dealing with test_app and this mock.patch + # thing conflicting, getting lazy. + @mock.patch('uuid.uuid4', UUID_MOCK) + def _real_test(): + # 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" + + _real_test() + + def test_garbage_slug(self, test_app): + """ + Titles that sound totally like Q*Bert shouldn't have slugs at + all. We'll just reference them by id. + + , + / \ (@!#?@!) + |\,/| ,-, / + | |#| ( ")~ + / \|/ \ L L + |\,/|\,/| + | |#, |#| + / \|/ \|/ \ + |\,/|\,/|\,/| + | |#| |#| |#| + / \|/ \|/ \|/ \ + |\,/|\,/|\,/|\,/| + | |#| |#| |#| |#| + \|/ \|/ \|/ \|/ + """ + self._setup() + + 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(test_app): + 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 obj_in_session == 0 diff --git a/mediagoblin/tests/test_oauth.py b/mediagoblin/tests/test_oauth.py index 94ba5dab..901556fe 100644 --- a/mediagoblin/tests/test_oauth.py +++ b/mediagoblin/tests/test_oauth.py @@ -21,15 +21,14 @@ from urlparse import parse_qs, urlparse from mediagoblin import mg_globals from mediagoblin.tools import template, pluginapi -from mediagoblin.tests.tools import get_app, fixture_add_user +from mediagoblin.tests.tools import fixture_add_user _log = logging.getLogger(__name__) class TestOAuth(object): - def setUp(self): - self.app = get_app() + def _setup(self, test_app): self.db = mg_globals.database self.pman = pluginapi.PluginManager() @@ -37,17 +36,17 @@ class TestOAuth(object): self.user_password = u'4cc355_70k3N' self.user = fixture_add_user(u'joauth', self.user_password) - self.login() + self.login(test_app) - def login(self): - self.app.post( + def login(self, test_app): + test_app.post( '/auth/login/', { 'username': self.user.username, 'password': self.user_password}) - def register_client(self, name, client_type, description=None, + def register_client(self, test_app, name, client_type, description=None, redirect_uri=''): - return self.app.post( + return test_app.post( '/oauth/client/register', { 'name': name, 'description': description, @@ -57,9 +56,11 @@ class TestOAuth(object): def get_context(self, template_name): return template.TEMPLATE_TEST_CONTEXT[template_name] - def test_1_public_client_registration_without_redirect_uri(self): + def test_1_public_client_registration_without_redirect_uri(self, test_app): ''' Test 'public' OAuth client registration without any redirect uri ''' - response = self.register_client(u'OMGOMGOMG', 'public', + self._setup(test_app) + + response = self.register_client(test_app, u'OMGOMGOMG', 'public', 'OMGOMG Apache License v2') ctx = self.get_context('oauth/client/register.html') @@ -75,10 +76,10 @@ class TestOAuth(object): # Should not pass through assert not client - def test_2_successful_public_client_registration(self): + def test_2_successful_public_client_registration(self, test_app): ''' Successfully register a public client ''' - self.login() - self.register_client(u'OMGOMG', 'public', 'OMG!', + self._setup(test_app) + self.register_client(test_app, u'OMGOMG', 'public', 'OMG!', 'http://foo.example') client = self.db.OAuthClient.query.filter( @@ -87,9 +88,12 @@ class TestOAuth(object): # Client should have been registered assert client - def test_3_successful_confidential_client_reg(self): + def test_3_successful_confidential_client_reg(self, test_app): ''' Register a confidential OAuth client ''' - response = self.register_client(u'GMOGMO', 'confidential', 'NO GMO!') + self._setup(test_app) + + response = self.register_client( + test_app, u'GMOGMO', 'confidential', 'NO GMO!') assert response.status_int == 302 @@ -101,15 +105,16 @@ class TestOAuth(object): return client - def test_4_authorize_confidential_client(self): + def test_4_authorize_confidential_client(self, test_app): ''' Authorize a confidential client as a logged in user ''' + self._setup(test_app) - client = self.test_3_successful_confidential_client_reg() + client = self.test_3_successful_confidential_client_reg(test_app) client_identifier = client.identifier redirect_uri = 'https://foo.example' - response = self.app.get('/oauth/authorize', { + response = test_app.get('/oauth/authorize', { 'client_id': client.identifier, 'scope': 'admin', 'redirect_uri': redirect_uri}) @@ -122,7 +127,7 @@ class TestOAuth(object): form = ctx['form'] # Short for client authorization post reponse - capr = self.app.post( + capr = test_app.post( '/oauth/client/authorize', { 'client_id': form.client_id.data, 'allow': 'Allow', @@ -139,16 +144,19 @@ class TestOAuth(object): def get_code_from_redirect_uri(self, uri): return parse_qs(urlparse(uri).query)['code'][0] - def test_token_endpoint_successful_confidential_request(self): + def test_token_endpoint_successful_confidential_request(self, test_app): ''' Successful request against token endpoint ''' - code_redirect, client_id = self.test_4_authorize_confidential_client() + self._setup(test_app) + + code_redirect, client_id = self.test_4_authorize_confidential_client( + test_app) code = self.get_code_from_redirect_uri(code_redirect.location) client = self.db.OAuthClient.query.filter( self.db.OAuthClient.identifier == unicode(client_id)).first() - token_res = self.app.get('/oauth/access_token?client_id={0}&\ + token_res = test_app.get('/oauth/access_token?client_id={0}&\ code={1}&client_secret={2}'.format(client_id, code, client.secret)) assert token_res.status_int == 200 @@ -162,16 +170,19 @@ code={1}&client_secret={2}'.format(client_id, code, client.secret)) assert type(token_data['expires_in']) == int assert token_data['expires_in'] > 0 - def test_token_endpont_missing_id_confidential_request(self): + def test_token_endpont_missing_id_confidential_request(self, test_app): ''' Unsuccessful request against token endpoint, missing client_id ''' - code_redirect, client_id = self.test_4_authorize_confidential_client() + self._setup(test_app) + + code_redirect, client_id = self.test_4_authorize_confidential_client( + test_app) code = self.get_code_from_redirect_uri(code_redirect.location) client = self.db.OAuthClient.query.filter( self.db.OAuthClient.identifier == unicode(client_id)).first() - token_res = self.app.get('/oauth/access_token?\ + token_res = test_app.get('/oauth/access_token?\ code={0}&client_secret={1}'.format(code, client.secret)) assert token_res.status_int == 200 diff --git a/mediagoblin/tests/test_paste.ini b/mediagoblin/tests/test_paste.ini index d7c18642..875b4f65 100644 --- a/mediagoblin/tests/test_paste.ini +++ b/mediagoblin/tests/test_paste.ini @@ -10,7 +10,7 @@ use = egg:Paste#urlmap [app:mediagoblin] use = egg:mediagoblin#app filter-with = beaker -config = %(here)s/test_mgoblin_app.ini +config = %(here)s/mediagoblin.ini [app:publicstore_serve] use = egg:Paste#static diff --git a/mediagoblin/tests/test_pluginapi.py b/mediagoblin/tests/test_pluginapi.py index 315a95da..245c396d 100644 --- a/mediagoblin/tests/test_pluginapi.py +++ b/mediagoblin/tests/test_pluginapi.py @@ -19,7 +19,6 @@ from configobj import ConfigObj from mediagoblin import mg_globals from mediagoblin.init.plugins import setup_plugins from mediagoblin.tools import pluginapi -from nose.tools import eq_ def with_cleanup(*modules_to_delete): @@ -97,7 +96,7 @@ def test_no_plugins(): setup_plugins() # Make sure we didn't load anything. - eq_(len(pman.plugins), 0) + assert len(pman.plugins) == 0 @with_cleanup('mediagoblin.plugins.sampleplugin') @@ -117,14 +116,14 @@ def test_one_plugin(): setup_plugins() # Make sure we only found one plugin - eq_(len(pman.plugins), 1) + assert len(pman.plugins) == 1 # Make sure the plugin is the one we think it is. - eq_(pman.plugins[0], 'mediagoblin.plugins.sampleplugin') + assert pman.plugins[0] == 'mediagoblin.plugins.sampleplugin' # Make sure there was one hook registered - eq_(len(pman.hooks), 1) + assert len(pman.hooks) == 1 # Make sure _setup_plugin_called was called once import mediagoblin.plugins.sampleplugin - eq_(mediagoblin.plugins.sampleplugin._setup_plugin_called, 1) + assert mediagoblin.plugins.sampleplugin._setup_plugin_called == 1 @with_cleanup('mediagoblin.plugins.sampleplugin') @@ -145,14 +144,14 @@ def test_same_plugin_twice(): setup_plugins() # Make sure we only found one plugin - eq_(len(pman.plugins), 1) + assert len(pman.plugins) == 1 # Make sure the plugin is the one we think it is. - eq_(pman.plugins[0], 'mediagoblin.plugins.sampleplugin') + assert pman.plugins[0] == 'mediagoblin.plugins.sampleplugin' # Make sure there was one hook registered - eq_(len(pman.hooks), 1) + assert len(pman.hooks) == 1 # Make sure _setup_plugin_called was called once import mediagoblin.plugins.sampleplugin - eq_(mediagoblin.plugins.sampleplugin._setup_plugin_called, 1) + assert mediagoblin.plugins.sampleplugin._setup_plugin_called == 1 @with_cleanup() @@ -172,4 +171,4 @@ def test_disabled_plugin(): setup_plugins() # Make sure we didn't load the plugin - eq_(len(pman.plugins), 0) + assert len(pman.plugins) == 0 diff --git a/mediagoblin/tests/test_storage.py b/mediagoblin/tests/test_storage.py index 61326ae9..294fec7d 100644 --- a/mediagoblin/tests/test_storage.py +++ b/mediagoblin/tests/test_storage.py @@ -18,7 +18,7 @@ import os import tempfile -from nose.tools import assert_raises, assert_equal, assert_true +import pytest from werkzeug.utils import secure_filename from mediagoblin import storage @@ -41,10 +41,8 @@ def test_clean_listy_filepath(): assert storage.clean_listy_filepath( ['../../../etc/', 'passwd']) == expected - assert_raises( - storage.InvalidFilepath, - storage.clean_listy_filepath, - ['../../', 'linooks.jpg']) + with pytest.raises(storage.InvalidFilepath): + storage.clean_listy_filepath(['../../', 'linooks.jpg']) class FakeStorageSystem(): @@ -78,10 +76,10 @@ def test_storage_system_from_config(): 'garbage_arg': 'garbage_arg', 'storage_class': 'mediagoblin.tests.test_storage:FakeStorageSystem'}) - assert_equal(this_storage.foobie, 'eiboof') - assert_equal(this_storage.blech, 'hcelb') - assert_equal(unicode(this_storage.__class__), - u'mediagoblin.tests.test_storage.FakeStorageSystem') + assert this_storage.foobie == 'eiboof' + assert this_storage.blech == 'hcelb' + assert unicode(this_storage.__class__) == \ + u'mediagoblin.tests.test_storage.FakeStorageSystem' ########################## @@ -108,7 +106,7 @@ def test_basic_storage__resolve_filepath(): assert result == os.path.join( tmpdir, 'etc/passwd') - assert_raises( + pytest.raises( storage.InvalidFilepath, this_storage._resolve_filepath, ['../../', 'etc', 'passwd']) @@ -205,7 +203,7 @@ def test_basic_storage_delete_file(): def test_basic_storage_url_for_file(): # Not supplying a base_url should actually just bork. tmpdir, this_storage = get_tmp_filestorage() - assert_raises( + pytest.raises( storage.NoWebServing, this_storage.file_url, ['dir1', 'dir2', 'filename.txt']) diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index 00f1ed3d..ac714252 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -21,12 +21,11 @@ sys.setdefaultencoding('utf-8') import urlparse import os -from nose.tools import assert_equal, assert_true from pkg_resources import resource_filename -from mediagoblin.tests.tools import get_app, \ - fixture_add_user +from mediagoblin.tests.tools import 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 +39,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) @@ -49,8 +49,8 @@ REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request'] class TestSubmission: - def setUp(self): - self.test_app = get_app(dump_old_app=False) + def _setup(self, test_app): + self.test_app = test_app # TODO: Possibly abstract into a decorator like: # @as_authenticated_user('chris') @@ -86,27 +86,29 @@ class TestSubmission: def check_comments(self, request, media_id, count): comments = request.db.MediaComment.find({'media_entry': media_id}) - assert_equal(count, len(list(comments))) + assert count == len(list(comments)) + + def test_missing_fields(self, test_app): + self._setup(test_app) - def test_missing_fields(self): # Test blank form # --------------- response, form = self.do_post({}, *FORM_CONTEXT) - assert_equal(form.file.errors, [u'You must provide a file.']) + assert form.file.errors == [u'You must provide a file.'] # Test blank file # --------------- response, form = self.do_post({'title': u'test title'}, *FORM_CONTEXT) - assert_equal(form.file.errors, [u'You must provide a file.']) + assert form.file.errors == [u'You must provide a file.'] def check_url(self, response, path): - assert_equal(urlparse.urlsplit(response.location)[2], path) + assert urlparse.urlsplit(response.location)[2] == path def check_normal_upload(self, title, filename): response, context = self.do_post({'title': title}, do_follow=True, **self.upload_data(filename)) self.check_url(response, '/u/{0}/'.format(self.test_user.username)) - assert_true('mediagoblin/user_pages/user.html' in context) + assert 'mediagoblin/user_pages/user.html' in context # Make sure the media view is at least reachable, logged in... url = '/u/{0}/m/{1}/'.format(self.test_user.username, title.lower().replace(' ', '-')) @@ -115,21 +117,25 @@ class TestSubmission: self.logout() self.test_app.get(url) - def test_normal_jpg(self): + def test_normal_jpg(self, test_app): + self._setup(test_app) self.check_normal_upload(u'Normal upload 1', GOOD_JPG) - def test_normal_png(self): + def test_normal_png(self, test_app): + self._setup(test_app) 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) + assert media.count() == count if count == 0: return return media[0] - def test_tags(self): + def test_tags(self, test_app): + self._setup(test_app) + # Good tag string # -------- response, request = self.do_post({'title': u'Balanced Goblin 2', @@ -149,12 +155,14 @@ class TestSubmission: 'tags': BAD_TAG_STRING}, *FORM_CONTEXT, **self.upload_data(GOOD_JPG)) - assert_equal(form.tags.errors, [ + assert form.tags.errors == [ u'Tags must be shorter than 50 characters. ' \ 'Tags that are too long: ' \ - 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']) + 'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu'] + + def test_delete(self, test_app): + self._setup(test_app) - def test_delete(self): response, request = self.do_post({'title': u'Balanced Goblin'}, *REQUEST_CONTEXT, do_follow=True, **self.upload_data(GOOD_JPG)) @@ -171,7 +179,7 @@ class TestSubmission: 'slug': u"Balanced=Goblin", 'tags': u''}) media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) - assert_equal(media.slug, u"balanced-goblin") + assert media.slug == u"balanced-goblin" # Add a comment, so we can test for its deletion later. self.check_comments(request, media_id, 0) @@ -199,33 +207,39 @@ class TestSubmission: self.check_media(request, {'id': media_id}, 0) self.check_comments(request, media_id, 0) - def test_evil_file(self): + def test_evil_file(self, test_app): + self._setup(test_app) + # Test non-suppoerted file with non-supported extension # ----------------------------------------------------- response, form = self.do_post({'title': u'Malicious Upload 1'}, *FORM_CONTEXT, **self.upload_data(EVIL_FILE)) - assert_equal(len(form.file.errors), 1) + assert len(form.file.errors) == 1 assert 'Sorry, I don\'t support that file type :(' == \ str(form.file.errors[0]) - def test_get_media_manager(self): + def test_get_media_manager(self, test_app): """Test if the get_media_manger function returns sensible things """ + self._setup(test_app) + response, request = self.do_post({'title': u'Balanced Goblin'}, *REQUEST_CONTEXT, do_follow=True, **self.upload_data(GOOD_JPG)) media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) - assert_equal(media.media_type, u'mediagoblin.media_types.image') - assert_equal(media.media_manager, img_MEDIA_MANAGER) + assert media.media_type == u'mediagoblin.media_types.image' + assert media.media_manager == img_MEDIA_MANAGER - def test_sniffing(self): + def test_sniffing(self, test_app): ''' Test sniffing mechanism to assert that regular uploads work as intended ''' + self._setup(test_app) + template.clear_test_template_context() response = self.test_app.post( '/submit/', { @@ -252,20 +266,36 @@ class TestSubmission: **self.upload_data(filename)) self.check_url(response, '/u/{0}/'.format(self.test_user.username)) entry = mg_globals.database.MediaEntry.find_one({'title': title}) - assert_equal(entry.state, 'failed') - assert_equal(entry.fail_error, u'mediagoblin.processing:BadMediaFail') + assert entry.state == 'failed' + assert entry.fail_error == u'mediagoblin.processing:BadMediaFail' + + def test_evil_jpg(self, test_app): + self._setup(test_app) - def test_evil_jpg(self): # Test non-supported file with .jpg extension # ------------------------------------------- self.check_false_image(u'Malicious Upload 2', EVIL_JPG) - def test_evil_png(self): + def test_evil_png(self, test_app): + self._setup(test_app) + # Test non-supported file with .png extension # ------------------------------------------- self.check_false_image(u'Malicious Upload 3', EVIL_PNG) - def test_processing(self): + def test_media_data(self, test_app): + self._setup(test_app) + + self.check_normal_upload(u"With GPS data", GPS_JPG) + media = self.check_media(None, {"title": u"With GPS data"}, 1) + assert media.media_data.gps_latitude == 59.336666666666666 + + def test_processing(self, test_app): + self._setup(test_app) + + public_store_dir = mg_globals.global_config[ + 'storage:publicstore']['base_dir'] + data = {'title': u'Big Blue'} response, request = self.do_post(data, *REQUEST_CONTEXT, do_follow=True, **self.upload_data(BIG_BLUE)) @@ -275,12 +305,11 @@ class TestSubmission: ('medium', 'bigblue.medium.png'), ('thumb', 'bigblue.thumbnail.png')): # Does the processed image have a good filename? - filename = resource_filename( - 'mediagoblin.tests', - os.path.join('test_user_dev/media/public', - *media.media_files.get(key, []))) - assert_true(filename.endswith('_' + basename)) + filename = os.path.join( + public_store_dir, + *media.media_files.get(key, [])) + assert filename.endswith('_' + basename) # Is it smaller than the last processed image we looked at? size = os.stat(filename).st_size - assert_true(last_size > size) + assert last_size > size last_size = size diff --git a/mediagoblin/tests/test_submission/evil b/mediagoblin/tests/test_submission/evil Binary files differindex 775da664..2c850e29 100755 --- a/mediagoblin/tests/test_submission/evil +++ b/mediagoblin/tests/test_submission/evil diff --git a/mediagoblin/tests/test_submission/evil.jpg b/mediagoblin/tests/test_submission/evil.jpg Binary files differindex 775da664..2c850e29 100755 --- a/mediagoblin/tests/test_submission/evil.jpg +++ b/mediagoblin/tests/test_submission/evil.jpg diff --git a/mediagoblin/tests/test_submission/evil.png b/mediagoblin/tests/test_submission/evil.png Binary files differindex 775da664..2c850e29 100755 --- a/mediagoblin/tests/test_submission/evil.png +++ b/mediagoblin/tests/test_submission/evil.png diff --git a/mediagoblin/tests/test_tags.py b/mediagoblin/tests/test_tags.py index ccb93085..e25cc283 100644 --- a/mediagoblin/tests/test_tags.py +++ b/mediagoblin/tests/test_tags.py @@ -14,17 +14,15 @@ # 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/>. -from mediagoblin.tests.tools import get_app from mediagoblin.tools import text -def test_list_of_dicts_conversion(): +def test_list_of_dicts_conversion(test_app): """ When the user adds tags to a media entry, the string from the form is converted into a list of tags, where each tag is stored in the database as a dict. Each tag dict should contain the tag's name and slug. Another function performs the reverse operation when populating a form to edit tags. """ - test_app = get_app(dump_old_app=False) # Leading, trailing, and internal whitespace should be removed and slugified assert text.convert_to_tag_list_of_dicts('sleep , 6 AM, chainsaw! ') == [ {'name': u'sleep', 'slug': u'sleep'}, diff --git a/mediagoblin/tests/test_tests.py b/mediagoblin/tests/test_tests.py deleted file mode 100644 index d539f1e0..00000000 --- a/mediagoblin/tests/test_tests.py +++ /dev/null @@ -1,36 +0,0 @@ -# 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/>. - -from mediagoblin import mg_globals -from mediagoblin.tests.tools import get_app, fixture_add_user -from mediagoblin.db.models import User - - -def test_get_app_wipes_db(): - """ - Make sure we get a fresh database on every wipe :) - """ - get_app(dump_old_app=True) - assert User.query.count() == 0 - - fixture_add_user() - assert User.query.count() == 1 - - get_app(dump_old_app=False) - assert User.query.count() == 1 - - get_app(dump_old_app=True) - assert User.query.count() == 0 diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py index 636c8689..3b2fc2c6 100644 --- a/mediagoblin/tests/test_workbench.py +++ b/mediagoblin/tests/test_workbench.py @@ -25,7 +25,7 @@ from mediagoblin.tests.test_storage import get_tmp_filestorage class TestWorkbench(object): - def setUp(self): + def setup(self): self.workbench_manager = workbench.WorkbenchManager( os.path.join(tempfile.gettempdir(), u'mgoblin_workbench_testing')) diff --git a/mediagoblin/tests/tools.py b/mediagoblin/tests/tools.py index 18d4ec0c..f7025715 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 @@ -43,16 +43,16 @@ TEST_APP_CONFIG = pkg_resources.resource_filename( 'mediagoblin.tests', 'test_mgoblin_app.ini') TEST_USER_DEV = pkg_resources.resource_filename( 'mediagoblin.tests', 'test_user_dev') -MGOBLIN_APP = None + USER_DEV_DIRECTORIES_TO_SETUP = [ 'media/public', 'media/queue', 'beaker/sessions/data', 'beaker/sessions/lock'] BAD_CELERY_MESSAGE = """\ -Sorry, you *absolutely* must run nosetests with the +Sorry, you *absolutely* must run tests with the mediagoblin.init.celery.from_tests module. Like so: -$ CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_tests ./bin/nosetests""" +$ CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_tests ./bin/py.test""" class BadCeleryEnviron(Exception): pass @@ -103,7 +103,30 @@ def suicide_if_bad_celery_environ(): raise BadCeleryEnviron(BAD_CELERY_MESSAGE) -def get_app(dump_old_app=True): +def get_app(request, paste_config=None, mgoblin_config=None): + """Create a MediaGoblin app for testing. + + Args: + - request: Not an http request, but a pytest fixture request. We + use this to make temporary directories that pytest + automatically cleans up as needed. + - paste_config: particular paste config used by this application. + - mgoblin_config: particular mediagoblin config used by this + application. + """ + paste_config = paste_config or TEST_SERVER_CONFIG + mgoblin_config = mgoblin_config or TEST_APP_CONFIG + + # This is the directory we're copying the paste/mgoblin config stuff into + run_dir = request.config._tmpdirhandler.mktemp( + 'mgoblin_app', numbered=True) + user_dev_dir = run_dir.mkdir('test_user_dev').strpath + + new_paste_config = run_dir.join('paste.ini').strpath + new_mgoblin_config = run_dir.join('mediagoblin.ini').strpath + shutil.copyfile(paste_config, new_paste_config) + shutil.copyfile(mgoblin_config, new_mgoblin_config) + suicide_if_bad_celery_environ() # Make sure we've turned on testing @@ -112,26 +135,16 @@ def get_app(dump_old_app=True): # Leave this imported as it sets up celery. from mediagoblin.init.celery import from_tests - global MGOBLIN_APP - - # Just return the old app if that exists and it's okay to set up - # and return - if MGOBLIN_APP and not dump_old_app: - return MGOBLIN_APP - Session.rollback() Session.remove() - # Remove and reinstall user_dev directories - if os.path.exists(TEST_USER_DEV): - shutil.rmtree(TEST_USER_DEV) - + # install user_dev directories for directory in USER_DEV_DIRECTORIES_TO_SETUP: - full_dir = os.path.join(TEST_USER_DEV, directory) + full_dir = os.path.join(user_dev_dir, directory) os.makedirs(full_dir) # Get app config - global_config, validation_result = read_mediagoblin_config(TEST_APP_CONFIG) + global_config, validation_result = read_mediagoblin_config(new_mgoblin_config) app_config = global_config['mediagoblin'] # Run database setup/migrations @@ -139,7 +152,7 @@ def get_app(dump_old_app=True): # setup app and return test_app = loadapp( - 'config:' + TEST_SERVER_CONFIG) + 'config:' + new_paste_config) # Re-setup celery setup_celery_app(app_config, global_config) @@ -151,26 +164,10 @@ def get_app(dump_old_app=True): mg_globals.app.meddleware.insert(0, TestingMeddleware(mg_globals.app)) app = TestApp(test_app) - MGOBLIN_APP = app return app -def setup_fresh_app(func): - """ - Decorator to setup a fresh test application for this function. - - Cleans out test buckets and passes in a new, fresh test_app. - """ - @wraps(func) - def wrapper(*args, **kwargs): - test_app = get_app() - testing.clear_test_buckets() - return func(test_app, *args, **kwargs) - - return wrapper - - def install_fixtures_simple(db, fixtures): """ Very simply install fixtures in the database @@ -228,6 +225,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() |