Added tests for HTTP callbacks
authorJoar Wandborg <git@wandborg.com>
Wed, 26 Sep 2012 22:45:25 +0000 (00:45 +0200)
committerJoar Wandborg <git@wandborg.com>
Wed, 26 Sep 2012 22:45:25 +0000 (00:45 +0200)
mediagoblin/tests/test_http_callback.py [new file with mode: 0644]
mediagoblin/tests/test_mgoblin_app.ini
mediagoblin/tests/test_oauth.py
mediagoblin/tools/processing.py

diff --git a/mediagoblin/tests/test_http_callback.py b/mediagoblin/tests/test_http_callback.py
new file mode 100644 (file)
index 0000000..bce479c
--- /dev/null
@@ -0,0 +1,72 @@
+# 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/>.
+
+import json
+
+from urlparse import urlparse, parse_qs
+
+from mediagoblin import mg_globals
+from mediagoblin.tools import template, processing
+from mediagoblin.tests.tools import get_test_app, 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_test_app()
+        self.db = mg_globals.database
+
+        self.user_password = 'secret'
+        self.user = fixture_add_user('call_back', self.user_password)
+        self.oauth = oauth.TestOAuth()
+        self.oauth.setUp()
+
+        self.login()
+
+    def login(self):
+        self.app.post('/auth/login/', {
+            'username': self.user.username,
+            'password': self.user_password})
+
+    def get_access_token(self, client_id, code):
+        response = self.app.get('/oauth/access_token', {
+                'code': code,
+                'client_id': client_id})
+
+        response_data = json.loads(response.body)
+
+        return response_data['access_token']
+
+    def test_callback(self):
+        ''' Test processing HTTP callback '''
+        redirect, client_id = self.oauth.test_4_authorize_confidential_client()
+
+        code = parse_qs(urlparse(redirect.location).query)['code'][0]
+
+        access_token = self.get_access_token(client_id, code)
+
+        callback_url = 'https://foo.example?secrettestmediagoblinparam'
+
+        res = self.app.post('/api/submit?client_id={0}&access_token={1}'\
+                .format(
+                    client_id,
+                    access_token), {
+            'title': 'Test',
+            'callback_url': callback_url},
+            upload_files=[('file', GOOD_PNG)])
+
+        assert processing.TESTS_CALLBACKS[callback_url]['state'] == u'processed'
index b112d298e5bf42ad887e87bb74230ba5800d5465..cde61a70e1ac9bea4be878af7fb11ef37796292e 100644 (file)
@@ -33,4 +33,5 @@ CELERY_RESULT_DBURI = "sqlite:///%(here)s/test_user_dev/celery.db"
 BROKER_HOST = "sqlite:///%(here)s/test_user_dev/kombu.db"
 
 [plugins]
+[[mediagoblin.plugins.api]]
 [[mediagoblin.plugins.oauth]]
index ced2ef79f2215ff51a6db808ee8242aa123991f1..cb1be6c92b0c37e5a9a1b0431352d5b5c02a0802 100644 (file)
 import logging
 
 from mediagoblin import mg_globals
-from mediagoblin.init.plugins import setup_plugins
 from mediagoblin.tools import template, pluginapi
 from mediagoblin.tests.tools import get_test_app, fixture_add_user
-from mediagoblin.tests.test_pluginapi import with_cleanup, build_config
 
 
 _log = logging.getLogger(__name__)
@@ -104,6 +102,8 @@ class TestOAuth(object):
         ''' Authorize a confidential client as a logged in user '''
         client = self.test_3_successful_confidential_client_reg()
 
+        client_identifier = client.identifier
+
         redirect_uri = 'https://foo.example'
         response = self.app.get('/oauth/authorize', {
                 'client_id': client.identifier,
@@ -130,4 +130,4 @@ class TestOAuth(object):
 
         assert authorization_response.location.startswith(redirect_uri)
 
-        return authorization_response
+        return authorization_response, client_identifier
index 5840c7e0a537ad944fc05a3ef40c179f170d1213..cff4cb9d5d37e0c0fdf9d3a041dac3ef403c46d1 100644 (file)
@@ -21,8 +21,12 @@ import traceback
 from urllib2 import urlopen, Request, HTTPError
 from urllib import urlencode
 
+from mediagoblin.tools.common import TESTS_ENABLED
+
 _log = logging.getLogger(__name__)
 
+TESTS_CALLBACKS = {}
+
 
 def create_post_request(url, data, **kw):
     '''
@@ -62,6 +66,11 @@ def json_processing_callback(entry):
             'id': entry.id,
             'state': entry.state}
 
+    # Trigger testing mode, no callback will be sent
+    if url.endswith('secrettestmediagoblinparam'):
+        TESTS_CALLBACKS.update({url: data})
+        return True
+
     request = create_post_request(
             url,
             data,