From 5f8b4ae895ecb228c5f5d615818ffe0a06a30473 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Tue, 4 Dec 2012 09:57:56 +0100 Subject: make media_manager a property of MediaEntry in mixin.py In all cases where get_media_manager(_media_type_as_string) was called in our code base we ultimately passed in a "MediaEntry().media_type" to get the matching MEDIA_MANAGER. It so makes sense to make this a function of the MediaEntry rather than a global function in mediagoblin.media_types and passing around media_entry.media_type as arguments all the time. It saves a few import statements and arguments. I also made it so the Media_manager property is cached for subsequent calls, although I am not too sure that this is needed (there are other cases for which this would make more sense) Also add a get_media_manager test to the media submission tests. It submits an image and checks that both media.media_type and media.media_manager return the right thing. Not sure if these tests could not be merged with an existing submission test, but it can't hurt to have things explicit. TODO: Right now we iterate through all existing media_managers to find the right one based on the string of its module name. This should be made a simple dict lookup to avoid all the extra work. Signed-off-by: Sebastian Spaeth --- mediagoblin/tests/test_submission.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index b7b0e574..b6fe0015 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -28,7 +28,7 @@ from mediagoblin.tests.tools import get_test_app, \ fixture_add_user from mediagoblin import mg_globals from mediagoblin.tools import template - +from mediagoblin.media_types.image import MEDIA_MANAGER as img_MEDIA_MANAGER def resource(filename): return resource_filename('mediagoblin.tests', 'test_submission/' + filename) @@ -197,6 +197,19 @@ class TestSubmission: assert 'Sorry, I don\'t support that file type :(' == \ str(form.file.errors[0]) + + def test_get_media_manager(self): + """Test if the get_media_manger function returns sensible things + """ + 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) + + def test_sniffing(self): ''' Test sniffing mechanism to assert that regular uploads work as intended -- cgit v1.2.3 From 52539acad2e945002b25d7e07991b634734eee30 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Wed, 5 Dec 2012 16:13:22 -0600 Subject: Add a UniqueConstraint add test in test_sql_migrations We should have this anyway, and Elrond needs it to help fix current broken migration thingies. --- mediagoblin/tests/test_sql_migrations.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index e3b55634..6383d096 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -322,6 +322,28 @@ def creature_power_hitpower_to_float(db_conn): creature_power.c.hitpower.alter(type=Float) +@RegisterMigration(8, FULL_MIGRATIONS) +def creature_power_name_creature_unique(db_conn): + """ + Add a unique constraint to name and creature on creature_power. + + We don't want multiple creature powers with the same name per creature! + """ + # Note: We don't actually check to see if this constraint is set + # up because at present there's no way to do so in sqlalchemy :\ + + metadata = MetaData(bind=db_conn.bind) + + creature_power = Table( + 'creature_power', metadata, + autoload=True, autoload_with=db_conn.bind) + + cons = changeset.constraint.UniqueConstraint( + 'name', 'creature', table=creature_power) + + cons.create() + + def _insert_migration1_objects(session): """ Test objects to insert for the first set of things @@ -660,7 +682,7 @@ def test_set1_to_set3(): u'__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) - assert migration_manager.latest_migration == 7 + assert migration_manager.latest_migration == 8 assert migration_manager.database_current_migration == 0 # Migrate @@ -679,14 +701,15 @@ def test_set1_to_set3(): + Running migration 5, "level_exit_index_from_and_to_level"... done. + Running migration 6, "creature_power_index_creature"... done. + Running migration 7, "creature_power_hitpower_to_float"... done. + + Running migration 8, "creature_power_name_creature_unique"... done. """ # Make sure version matches expected migration_manager = MigrationManager( u'__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) - assert migration_manager.latest_migration == 7 - assert migration_manager.database_current_migration == 7 + assert migration_manager.latest_migration == 8 + assert migration_manager.database_current_migration == 8 # Check all things in database match expected -- cgit v1.2.3 From d24a82970ea34d90b684e1adaf9ddebacf215b89 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Tue, 11 Dec 2012 15:38:28 +0100 Subject: Fix CSRF tests with webtest 1.4.0 CSRF tests apparently passed with earlier versions of webtest, but failed with the latest webtest (1.4.0) package. It choked on passing a "key=value; " cookie as it split at the semicolon and failed to find additional values or something like that. Removing the semicolon makes this test pass. Signed-off-by: Sebastian Spaeth --- mediagoblin/tests/test_csrf_middleware.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_csrf_middleware.py b/mediagoblin/tests/test_csrf_middleware.py index ad433fe8..d730909f 100644 --- a/mediagoblin/tests/test_csrf_middleware.py +++ b/mediagoblin/tests/test_csrf_middleware.py @@ -44,7 +44,7 @@ def test_csrf_token_must_match(test_app): # construct a request with a cookie, but no form token assert test_app.post('/auth/login/', - headers={'Cookie': str('%s=foo; ' % + headers={'Cookie': str('%s=foo' % mg_globals.app_config['csrf_cookie_name'])}, extra_environ={'gmg.verify_csrf': True}, expect_errors=True).status_int == 403 @@ -52,7 +52,7 @@ def test_csrf_token_must_match(test_app): # if both the cookie and form token are provided, they must match assert test_app.post('/auth/login/', {'csrf_token': 'blarf'}, - headers={'Cookie': str('%s=foo; ' % + headers={'Cookie': str('%s=foo' % mg_globals.app_config['csrf_cookie_name'])}, extra_environ={'gmg.verify_csrf': True}, expect_errors=True).\ @@ -60,7 +60,7 @@ def test_csrf_token_must_match(test_app): assert test_app.post('/auth/login/', {'csrf_token': 'foo'}, - headers={'Cookie': str('%s=foo; ' % + headers={'Cookie': str('%s=foo' % mg_globals.app_config['csrf_cookie_name'])}, extra_environ={'gmg.verify_csrf': True}).\ status_int == 200 -- cgit v1.2.3 From fc7b1b17eb430aee964c85f1ccf1776cb4be93c1 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Tue, 11 Dec 2012 16:55:11 +0100 Subject: Make sqlalchemy stop complaining about non-unicode input These tests output noisy sql complaints about receiving non-unicode for an unicode field. This was ... well ... because we were handing in non-unicode usernames and passwords. Prefixing usernames/passwords with u'' makes the testsuite less noisy and verbose. Signed-off-by: Sebastian Spaeth --- mediagoblin/tests/test_http_callback.py | 4 ++-- mediagoblin/tests/test_oauth.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_http_callback.py b/mediagoblin/tests/test_http_callback.py index d769af1e..8b0a03b9 100644 --- a/mediagoblin/tests/test_http_callback.py +++ b/mediagoblin/tests/test_http_callback.py @@ -30,8 +30,8 @@ class TestHTTPCallback(object): self.app = get_test_app() self.db = mg_globals.database - self.user_password = 'secret' - self.user = fixture_add_user('call_back', self.user_password) + self.user_password = u'secret' + self.user = fixture_add_user(u'call_back', self.user_password) self.login() diff --git a/mediagoblin/tests/test_oauth.py b/mediagoblin/tests/test_oauth.py index db4e226a..cedfc42a 100644 --- a/mediagoblin/tests/test_oauth.py +++ b/mediagoblin/tests/test_oauth.py @@ -34,8 +34,8 @@ class TestOAuth(object): self.pman = pluginapi.PluginManager() - self.user_password = '4cc355_70k3N' - self.user = fixture_add_user('joauth', self.user_password) + self.user_password = u'4cc355_70k3N' + self.user = fixture_add_user(u'joauth', self.user_password) self.login() -- cgit v1.2.3 From af6a43d140cd65989a6fe151f9df7bdafc622546 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Wed, 12 Dec 2012 12:29:22 +0100 Subject: More unicode fixes in the test suite Pass in unicode not (binary) strings where sqlite expects unicode values to prevent the test suite from (correctly) complaining about errors. I now pass the full suite without any complaints. Signed-off-by: Sebastian Spaeth --- mediagoblin/tests/test_oauth.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_oauth.py b/mediagoblin/tests/test_oauth.py index cedfc42a..a72f766e 100644 --- a/mediagoblin/tests/test_oauth.py +++ b/mediagoblin/tests/test_oauth.py @@ -59,13 +59,13 @@ class TestOAuth(object): def test_1_public_client_registration_without_redirect_uri(self): ''' Test 'public' OAuth client registration without any redirect uri ''' - response = self.register_client('OMGOMGOMG', 'public', + response = self.register_client(u'OMGOMGOMG', 'public', 'OMGOMG Apache License v2') ctx = self.get_context('oauth/client/register.html') client = self.db.OAuthClient.query.filter( - self.db.OAuthClient.name == 'OMGOMGOMG').first() + self.db.OAuthClient.name == u'OMGOMGOMG').first() assert response.status_int == 200 @@ -78,23 +78,23 @@ class TestOAuth(object): def test_2_successful_public_client_registration(self): ''' Successfully register a public client ''' self.login() - self.register_client('OMGOMG', 'public', 'OMG!', + self.register_client(u'OMGOMG', 'public', 'OMG!', 'http://foo.example') client = self.db.OAuthClient.query.filter( - self.db.OAuthClient.name == 'OMGOMG').first() + self.db.OAuthClient.name == u'OMGOMG').first() # Client should have been registered assert client def test_3_successful_confidential_client_reg(self): ''' Register a confidential OAuth client ''' - response = self.register_client('GMOGMO', 'confidential', 'NO GMO!') + response = self.register_client(u'GMOGMO', 'confidential', 'NO GMO!') assert response.status_int == 302 client = self.db.OAuthClient.query.filter( - self.db.OAuthClient.name == 'GMOGMO').first() + self.db.OAuthClient.name == u'GMOGMO').first() # Client should have been registered assert client @@ -103,6 +103,7 @@ class TestOAuth(object): def test_4_authorize_confidential_client(self): ''' Authorize a confidential client as a logged in user ''' + client = self.test_3_successful_confidential_client_reg() client_identifier = client.identifier -- cgit v1.2.3