aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/tests')
-rw-r--r--mediagoblin/tests/auth_configs/persona_appconfig.ini42
-rw-r--r--mediagoblin/tests/test_http_callback.py4
-rw-r--r--mediagoblin/tests/test_oauth1.py166
-rw-r--r--mediagoblin/tests/test_oauth2.py (renamed from mediagoblin/tests/test_oauth.py)12
-rw-r--r--mediagoblin/tests/test_persona.py210
5 files changed, 426 insertions, 8 deletions
diff --git a/mediagoblin/tests/auth_configs/persona_appconfig.ini b/mediagoblin/tests/auth_configs/persona_appconfig.ini
new file mode 100644
index 00000000..0bd5d634
--- /dev/null
+++ b/mediagoblin/tests/auth_configs/persona_appconfig.ini
@@ -0,0 +1,42 @@
+# 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/>.
+[mediagoblin]
+direct_remote_path = /test_static/
+email_sender_address = "notice@mediagoblin.example.org"
+email_debug_mode = true
+
+# TODO: Switch to using an in-memory database
+sql_engine = "sqlite:///%(here)s/user_dev/mediagoblin.db"
+
+# Celery shouldn't be set up by the application as it's setup via
+# mediagoblin.init.celery.from_celery
+celery_setup_elsewhere = true
+
+[storage:publicstore]
+base_dir = %(here)s/user_dev/media/public
+base_url = /mgoblin_media/
+
+[storage:queuestore]
+base_dir = %(here)s/user_dev/media/queue
+
+[celery]
+CELERY_ALWAYS_EAGER = true
+CELERY_RESULT_DBURI = "sqlite:///%(here)s/user_dev/celery.db"
+BROKER_HOST = "sqlite:///%(here)s/user_dev/kombu.db"
+
+[plugins]
+[[mediagoblin.plugins.persona]]
+
diff --git a/mediagoblin/tests/test_http_callback.py b/mediagoblin/tests/test_http_callback.py
index a0511af7..64b7ee8f 100644
--- a/mediagoblin/tests/test_http_callback.py
+++ b/mediagoblin/tests/test_http_callback.py
@@ -23,7 +23,7 @@ from mediagoblin import mg_globals
from mediagoblin.tools import processing
from mediagoblin.tests.tools import fixture_add_user
from mediagoblin.tests.test_submission import GOOD_PNG
-from mediagoblin.tests import test_oauth as oauth
+from mediagoblin.tests import test_oauth2 as oauth
class TestHTTPCallback(object):
@@ -44,7 +44,7 @@ class TestHTTPCallback(object):
'password': self.user_password})
def get_access_token(self, client_id, client_secret, code):
- response = self.test_app.get('/oauth/access_token', {
+ response = self.test_app.get('/oauth-2/access_token', {
'code': code,
'client_id': client_id,
'client_secret': client_secret})
diff --git a/mediagoblin/tests/test_oauth1.py b/mediagoblin/tests/test_oauth1.py
new file mode 100644
index 00000000..073c2884
--- /dev/null
+++ b/mediagoblin/tests/test_oauth1.py
@@ -0,0 +1,166 @@
+# 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 cgi
+
+import pytest
+from urlparse import parse_qs, urlparse
+
+from oauthlib.oauth1 import Client
+
+from mediagoblin import mg_globals
+from mediagoblin.tools import template, pluginapi
+from mediagoblin.tests.tools import fixture_add_user
+
+
+class TestOAuth(object):
+
+ MIME_FORM = "application/x-www-form-urlencoded"
+ MIME_JSON = "application/json"
+
+ @pytest.fixture(autouse=True)
+ def setup(self, test_app):
+ self.test_app = test_app
+
+ self.db = mg_globals.database
+
+ self.pman = pluginapi.PluginManager()
+
+ self.user_password = "AUserPassword123"
+ self.user = fixture_add_user("OAuthy", self.user_password)
+
+ self.login()
+
+ def login(self):
+ self.test_app.post(
+ "/auth/login/", {
+ "username": self.user.username,
+ "password": self.user_password})
+
+ def register_client(self, **kwargs):
+ """ Regiters a client with the API """
+
+ kwargs["type"] = "client_associate"
+ kwargs["application_type"] = kwargs.get("application_type", "native")
+ return self.test_app.post("/api/client/register", kwargs)
+
+ def test_client_client_register_limited_info(self):
+ """ Tests that a client can be registered with limited information """
+ response = self.register_client()
+ client_info = response.json
+
+ client = self.db.Client.query.filter_by(id=client_info["client_id"]).first()
+
+ assert response.status_int == 200
+ assert client is not None
+
+ def test_client_register_full_info(self):
+ """ Provides every piece of information possible to register client """
+ query = {
+ "application_name": "Testificate MD",
+ "application_type": "web",
+ "contacts": "someone@someplace.com tuteo@tsengeo.lu",
+ "logo_url": "http://ayrel.com/utral.png",
+ "redirect_uris": "http://navi-kosman.lu http://gmg-yawne-oeru.lu",
+ }
+
+ response = self.register_client(**query)
+ client_info = response.json
+
+ client = self.db.Client.query.filter_by(id=client_info["client_id"]).first()
+
+ assert client is not None
+ assert client.secret == client_info["client_secret"]
+ assert client.application_type == query["application_type"]
+ assert client.redirect_uri == query["redirect_uris"].split()
+ assert client.logo_url == query["logo_url"]
+ assert client.contacts == query["contacts"].split()
+
+
+ def test_client_update(self):
+ """ Tests that you can update a client """
+ # first we need to register a client
+ response = self.register_client()
+
+ client_info = response.json
+ client = self.db.Client.query.filter_by(id=client_info["client_id"]).first()
+
+ # Now update
+ update_query = {
+ "type": "client_update",
+ "application_name": "neytiri",
+ "contacts": "someone@someplace.com abc@cba.com",
+ "logo_url": "http://place.com/picture.png",
+ "application_type": "web",
+ "redirect_uris": "http://blah.gmg/whatever https://inboxen.org/",
+ }
+
+ update_response = self.register_client(**update_query)
+
+ assert update_response.status_int == 200
+ client_info = update_response.json
+ client = self.db.Client.query.filter_by(id=client_info["client_id"]).first()
+
+ assert client.secret == client_info["client_secret"]
+ assert client.application_type == update_query["application_type"]
+ assert client.application_name == update_query["application_name"]
+ assert client.contacts == update_query["contacts"].split()
+ assert client.logo_url == update_query["logo_url"]
+ assert client.redirect_uri == update_query["redirect_uris"].split()
+
+ def to_authorize_headers(self, data):
+ headers = ""
+ for key, value in data.items():
+ headers += '{0}="{1}",'.format(key, value)
+ return {"Authorization": "OAuth " + headers[:-1]}
+
+ def test_request_token(self):
+ """ Test a request for a request token """
+ response = self.register_client()
+
+ client_id = response.json["client_id"]
+
+ endpoint = "/oauth/request_token"
+ request_query = {
+ "oauth_consumer_key": client_id,
+ "oauth_nonce": "abcdefghij",
+ "oauth_timestamp": 123456789.0,
+ "oauth_callback": "https://some.url/callback",
+ }
+
+ headers = self.to_authorize_headers(request_query)
+
+ headers["Content-Type"] = self.MIME_FORM
+
+ response = self.test_app.post(endpoint, headers=headers)
+ response = cgi.parse_qs(response.body)
+
+ # each element is a list, reduce it to a string
+ for key, value in response.items():
+ response[key] = value[0]
+
+ request_token = self.db.RequestToken.query.filter_by(
+ token=response["oauth_token"]
+ ).first()
+
+ client = self.db.Client.query.filter_by(id=client_id).first()
+
+ assert request_token is not None
+ assert request_token.secret == response["oauth_token_secret"]
+ assert request_token.client == client.id
+ assert request_token.used == False
+ assert request_token.callback == request_query["oauth_callback"]
+
diff --git a/mediagoblin/tests/test_oauth.py b/mediagoblin/tests/test_oauth2.py
index ea3bd798..86f9e8cc 100644
--- a/mediagoblin/tests/test_oauth.py
+++ b/mediagoblin/tests/test_oauth2.py
@@ -51,7 +51,7 @@ class TestOAuth(object):
def register_client(self, name, client_type, description=None,
redirect_uri=''):
return self.test_app.post(
- '/oauth/client/register', {
+ '/oauth-2/client/register', {
'name': name,
'description': description,
'type': client_type,
@@ -115,7 +115,7 @@ class TestOAuth(object):
client_identifier = client.identifier
redirect_uri = 'https://foo.example'
- response = self.test_app.get('/oauth/authorize', {
+ response = self.test_app.get('/oauth-2/authorize', {
'client_id': client.identifier,
'scope': 'all',
'redirect_uri': redirect_uri})
@@ -129,7 +129,7 @@ class TestOAuth(object):
# Short for client authorization post reponse
capr = self.test_app.post(
- '/oauth/client/authorize', {
+ '/oauth-2/client/authorize', {
'client_id': form.client_id.data,
'allow': 'Allow',
'next': form.next.data})
@@ -155,7 +155,7 @@ class TestOAuth(object):
client = self.db.OAuthClient.query.filter(
self.db.OAuthClient.identifier == unicode(client_id)).first()
- token_res = self.test_app.get('/oauth/access_token?client_id={0}&\
+ token_res = self.test_app.get('/oauth-2/access_token?client_id={0}&\
code={1}&client_secret={2}'.format(client_id, code, client.secret))
assert token_res.status_int == 200
@@ -183,7 +183,7 @@ code={1}&client_secret={2}'.format(client_id, code, client.secret))
client = self.db.OAuthClient.query.filter(
self.db.OAuthClient.identifier == unicode(client_id)).first()
- token_res = self.test_app.get('/oauth/access_token?\
+ token_res = self.test_app.get('/oauth-2/access_token?\
code={0}&client_secret={1}'.format(code, client.secret))
assert token_res.status_int == 200
@@ -204,7 +204,7 @@ code={0}&client_secret={1}'.format(code, client.secret))
client = self.db.OAuthClient.query.filter(
self.db.OAuthClient.identifier == client_id).first()
- token_res = self.test_app.get('/oauth/access_token',
+ token_res = self.test_app.get('/oauth-2/access_token',
{'refresh_token': token_data['refresh_token'],
'client_id': client_id,
'client_secret': client.secret
diff --git a/mediagoblin/tests/test_persona.py b/mediagoblin/tests/test_persona.py
new file mode 100644
index 00000000..ce795258
--- /dev/null
+++ b/mediagoblin/tests/test_persona.py
@@ -0,0 +1,210 @@
+# 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 urlparse
+import pkg_resources
+import pytest
+import mock
+
+from mediagoblin import mg_globals
+from mediagoblin.db.base import Session
+from mediagoblin.tests.tools import get_app
+from mediagoblin.tools import template
+
+
+# App with plugin enabled
+@pytest.fixture()
+def persona_plugin_app(request):
+ return get_app(
+ request,
+ mgoblin_config=pkg_resources.resource_filename(
+ 'mediagoblin.tests.auth_configs',
+ 'persona_appconfig.ini'))
+
+
+class TestPersonaPlugin(object):
+ def test_authentication_views(self, persona_plugin_app):
+ res = persona_plugin_app.get('/auth/login/')
+
+ assert urlparse.urlsplit(res.location)[2] == '/'
+
+ res = persona_plugin_app.get('/auth/register/')
+
+ assert urlparse.urlsplit(res.location)[2] == '/'
+
+ res = persona_plugin_app.get('/auth/persona/login/')
+
+ assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
+
+ res = persona_plugin_app.get('/auth/persona/register/')
+
+ assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
+
+ @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test@example.com'))
+ def _test_registration():
+ # No register users
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/auth/persona/login/', {})
+
+ assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
+ context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
+ register_form = context['register_form']
+
+ assert register_form.email.data == u'test@example.com'
+ assert register_form.persona_email.data == u'test@example.com'
+
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/auth/persona/register/', {})
+
+ assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
+ context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
+ register_form = context['register_form']
+
+ assert register_form.username.errors == [u'This field is required.']
+ assert register_form.email.errors == [u'This field is required.']
+ assert register_form.persona_email.errors == [u'This field is required.']
+
+ # Successful register
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/auth/persona/register/',
+ {'username': 'chris',
+ 'email': 'chris@example.com',
+ 'persona_email': 'test@example.com'})
+ res.follow()
+
+ assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
+ assert 'mediagoblin/user_pages/user.html' in template.TEMPLATE_TEST_CONTEXT
+
+ # Try to register same Persona email address
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/auth/persona/register/',
+ {'username': 'chris1',
+ 'email': 'chris1@example.com',
+ 'persona_email': 'test@example.com'})
+
+ assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
+ context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
+ register_form = context['register_form']
+
+ assert register_form.persona_email.errors == [u'Sorry, an account is already registered to that Persona email.']
+
+ # Logout
+ persona_plugin_app.get('/auth/logout/')
+
+ # Get user and detach from session
+ test_user = mg_globals.database.User.query.filter_by(
+ username=u'chris').first()
+ test_user.email_verified = True
+ test_user.status = u'active'
+ test_user.save()
+ test_user = mg_globals.database.User.query.filter_by(
+ username=u'chris').first()
+ Session.expunge(test_user)
+
+ # Add another user for _test_edit_persona
+ persona_plugin_app.post(
+ '/auth/persona/register/',
+ {'username': 'chris1',
+ 'email': 'chris1@example.com',
+ 'persona_email': 'test1@example.com'})
+
+ # Log back in
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/auth/persona/login/')
+ res.follow()
+
+ assert urlparse.urlsplit(res.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']
+ session = context['request'].session
+ assert session['user_id'] == unicode(test_user.id)
+
+ _test_registration()
+
+ @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'new@example.com'))
+ def _test_edit_persona():
+ # Try and delete only Persona email address
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/edit/persona/',
+ {'email': 'test@example.com'})
+
+ assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
+ context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
+ form = context['form']
+
+ assert form.email.errors == [u"You can't delete your only Persona email address unless you have a password set."]
+
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/edit/persona/', {})
+
+ assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
+ context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
+ form = context['form']
+
+ assert form.email.errors == [u'This field is required.']
+
+ # Try and delete Persona not owned by the user
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/edit/persona/',
+ {'email': 'test1@example.com'})
+
+ assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
+ context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
+ form = context['form']
+
+ assert form.email.errors == [u'That Persona email address is not registered to this account.']
+
+ res = persona_plugin_app.get('/edit/persona/add/')
+
+ assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
+
+ # Add Persona email address
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/edit/persona/add/')
+ res.follow()
+
+ assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
+
+ # Delete a Persona
+ res = persona_plugin_app.post(
+ '/edit/persona/',
+ {'email': 'test@example.com'})
+ res.follow()
+
+ assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
+
+ _test_edit_persona()
+
+ @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test1@example.com'))
+ def _test_add_existing():
+ template.clear_test_template_context()
+ res = persona_plugin_app.post(
+ '/edit/persona/add/')
+ res.follow()
+
+ assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
+
+ _test_add_existing()