moving forgot_password views back to gmg/auth and cleanup
[mediagoblin.git] / mediagoblin / tests / test_pluginapi.py
index d40a5081729c3231e50c586f46b40dfe23864669..eae0ce15199ac6e97742b71abc91e11b7afc70a1 100644 (file)
 # 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 os
+import json
 import sys
 
 from configobj import ConfigObj
 import pytest
+import pkg_resources
+from validate import VdtTypeError
 
 from mediagoblin import mg_globals
 from mediagoblin.init.plugins import setup_plugins
+from mediagoblin.init.config import read_mediagoblin_config
+from mediagoblin.gmg_commands.assetlink import link_plugin_assets
 from mediagoblin.tools import pluginapi
+from mediagoblin.tests.tools import get_app
+from mediagoblin.tools.common import CollectingPrinter
 
 
 def with_cleanup(*modules_to_delete):
@@ -177,19 +185,22 @@ def test_disabled_plugin():
     assert len(pman.plugins) == 0
 
 
+CONFIG_ALL_CALLABLES = [
+        ('mediagoblin', {}, []),
+        ('plugins', {}, [
+                ('mediagoblin.tests.testplugins.callables1', {}, []),
+                ('mediagoblin.tests.testplugins.callables2', {}, []),
+                ('mediagoblin.tests.testplugins.callables3', {}, []),
+            ])
+    ]
+
+
 @with_cleanup()
-def test_callable_runone():
+def test_hook_handle():
     """
-    Test the callable_runone method
+    Test the hook_handle method
     """
-    cfg = build_config([
-            ('mediagoblin', {}, []),
-            ('plugins', {}, [
-                    ('mediagoblin.tests.testplugins.callables1', {}, []),
-                    ('mediagoblin.tests.testplugins.callables2', {}, []),
-                    ('mediagoblin.tests.testplugins.callables3', {}, []),
-                    ])
-            ])
+    cfg = build_config(CONFIG_ALL_CALLABLES)
 
     mg_globals.app_config = cfg['mediagoblin']
     mg_globals.global_config = cfg
@@ -198,50 +209,42 @@ def test_callable_runone():
 
     # Just one hook provided
     call_log = []
-    assert pluginapi.callable_runone(
+    assert pluginapi.hook_handle(
         "just_one", call_log) == "Called just once"
     assert call_log == ["expect this one call"]
 
     # Nothing provided and unhandled not okay
     call_log = []
-    with pytest.raises(pluginapi.UnhandledCallable):
-        pluginapi.callable_runone(
-            "nothing_handling", call_log)
+    pluginapi.hook_handle(
+        "nothing_handling", call_log) == None
     assert call_log == []
 
     # Nothing provided and unhandled okay
     call_log = []
-    assert pluginapi.callable_runone(
+    assert pluginapi.hook_handle(
         "nothing_handling", call_log, unhandled_okay=True) is None
     assert call_log == []
     
     # Multiple provided, go with the first!
     call_log = []
-    assert pluginapi.callable_runone(
+    assert pluginapi.hook_handle(
         "multi_handle", call_log) == "the first returns"
     assert call_log == ["Hi, I'm the first"]
 
     # Multiple provided, one has CantHandleIt
     call_log = []
-    assert pluginapi.callable_runone(
+    assert pluginapi.hook_handle(
         "multi_handle_with_canthandle",
         call_log) == "the second returns"
     assert call_log == ["Hi, I'm the second"]
 
 
 @with_cleanup()
-def test_callable_runall():
+def test_hook_runall():
     """
-    Test the callable_runall method
+    Test the hook_runall method
     """
-    cfg = build_config([
-            ('mediagoblin', {}, []),
-            ('plugins', {}, [
-                    ('mediagoblin.tests.testplugins.callables1', {}, []),
-                    ('mediagoblin.tests.testplugins.callables2', {}, []),
-                    ('mediagoblin.tests.testplugins.callables3', {}, []),
-                    ])
-            ])
+    cfg = build_config(CONFIG_ALL_CALLABLES)
 
     mg_globals.app_config = cfg['mediagoblin']
     mg_globals.global_config = cfg
@@ -250,19 +253,19 @@ def test_callable_runall():
 
     # Just one hook, check results
     call_log = []
-    assert pluginapi.callable_runall(
-        "just_one", call_log) == ["Called just once", None, None]
+    assert pluginapi.hook_runall(
+        "just_one", call_log) == ["Called just once"]
     assert call_log == ["expect this one call"]
 
     # None provided, check results
     call_log = []
-    assert pluginapi.callable_runall(
+    assert pluginapi.hook_runall(
         "nothing_handling", call_log) == []
     assert call_log == []
 
     # Multiple provided, check results
     call_log = []
-    assert pluginapi.callable_runall(
+    assert pluginapi.hook_runall(
         "multi_handle", call_log) == [
             "the first returns",
             "the second returns",
@@ -275,7 +278,7 @@ def test_callable_runall():
 
     # Multiple provided, one has CantHandleIt, check results
     call_log = []
-    assert pluginapi.callable_runall(
+    assert pluginapi.hook_runall(
         "multi_handle_with_canthandle", call_log) == [
             "the second returns",
             "the third returns",
@@ -283,3 +286,181 @@ def test_callable_runall():
     assert call_log == [
         "Hi, I'm the second",
         "Hi, I'm the third"]
+
+
+@with_cleanup()
+def test_hook_transform():
+    """
+    Test the hook_transform method
+    """
+    cfg = build_config(CONFIG_ALL_CALLABLES)
+
+    mg_globals.app_config = cfg['mediagoblin']
+    mg_globals.global_config = cfg
+
+    setup_plugins()
+
+    assert pluginapi.hook_transform(
+        "expand_tuple", (-1, 0)) == (-1, 0, 1, 2, 3)
+
+
+def test_plugin_config():
+    """
+    Make sure plugins can set up their own config
+    """
+    config, validation_result = read_mediagoblin_config(
+        pkg_resources.resource_filename(
+            'mediagoblin.tests', 'appconfig_plugin_specs.ini'))
+
+    pluginspec_section = config['plugins'][
+        'mediagoblin.tests.testplugins.pluginspec']
+    assert pluginspec_section['some_string'] == 'not blork'
+    assert pluginspec_section['dont_change_me'] == 'still the default'
+
+    # Make sure validation works... this should be an error
+    assert isinstance(
+        validation_result[
+            'plugins'][
+                'mediagoblin.tests.testplugins.pluginspec'][
+                    'some_int'],
+        VdtTypeError)
+
+    # the callables thing shouldn't really have anything though.
+    assert len(config['plugins'][
+        'mediagoblin.tests.testplugins.callables1']) == 0
+
+
+@pytest.fixture()
+def context_modified_app(request):
+    """
+    Get a MediaGoblin app fixture using appconfig_context_modified.ini
+    """
+    return get_app(
+        request,
+        mgoblin_config=pkg_resources.resource_filename(
+            'mediagoblin.tests', 'appconfig_context_modified.ini'))
+
+
+def test_modify_context(context_modified_app):
+    """
+    Test that we can modify both the view/template specific and
+    global contexts for templates.
+    """
+    # Specific thing passed into a page
+    result = context_modified_app.get("/modify_context/specific/")
+    assert result.body.strip() == """Specific page!
+
+specific thing: in yer specificpage
+global thing: globally appended!
+something: orother
+doubleme: happyhappy"""
+
+    # General test, should have global context variable only
+    result = context_modified_app.get("/modify_context/")
+    assert result.body.strip() == """General page!
+
+global thing: globally appended!
+lol: cats
+doubleme: joyjoy"""
+
+
+@pytest.fixture()
+def static_plugin_app(request):
+    """
+    Get a MediaGoblin app fixture using appconfig_static_plugin.ini
+    """
+    return get_app(
+        request,
+        mgoblin_config=pkg_resources.resource_filename(
+            'mediagoblin.tests', 'appconfig_static_plugin.ini'))
+
+
+def test_plugin_assetlink(static_plugin_app):
+    """
+    Test that the assetlink command works correctly
+    """
+    linked_assets_dir = mg_globals.app_config['plugin_linked_assets_dir']
+    plugin_link_dir = os.path.join(
+        linked_assets_dir.rstrip(os.path.sep),
+        'staticstuff')
+
+    plugin_statics = pluginapi.hook_runall("static_setup")
+    assert len(plugin_statics) == 1
+    plugin_static = plugin_statics[0]
+
+    def run_assetlink():
+        printer = CollectingPrinter()
+
+        link_plugin_assets(
+            plugin_static, linked_assets_dir, printer)
+
+        return printer
+
+    # it shouldn't exist yet
+    assert not os.path.lexists(plugin_link_dir)
+
+    # link dir doesn't exist, link it
+    result = run_assetlink().collection[0]
+    assert result == \
+        'Linked asset directory for plugin "staticstuff":\n  %s\nto:\n  %s\n' % (
+            plugin_static.file_path.rstrip(os.path.sep),
+            plugin_link_dir)
+    assert os.path.lexists(plugin_link_dir)
+    assert os.path.islink(plugin_link_dir)
+    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path
+
+    # link dir exists, leave it alone
+    # (and it should exist still since we just ran it..)
+    result = run_assetlink().collection[0]
+    assert result == 'Skipping "staticstuff"; already set up.\n'
+    assert os.path.lexists(plugin_link_dir)
+    assert os.path.islink(plugin_link_dir)
+    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path
+
+    # link dir exists, is a symlink to somewhere else (re-link)
+    junk_file_path = os.path.join(
+        linked_assets_dir.rstrip(os.path.sep),
+        'junk.txt')
+    with file(junk_file_path, 'w') as junk_file:
+        junk_file.write('barf')
+
+    os.unlink(plugin_link_dir)
+    os.symlink(junk_file_path, plugin_link_dir)
+
+    result = run_assetlink().combined_string
+    assert result == """Old link found for "staticstuff"; removing.
+Linked asset directory for plugin "staticstuff":
+  %s
+to:
+  %s
+""" % (plugin_static.file_path.rstrip(os.path.sep), plugin_link_dir)
+    assert os.path.lexists(plugin_link_dir)
+    assert os.path.islink(plugin_link_dir)
+    assert os.path.realpath(plugin_link_dir) == plugin_static.file_path
+
+    # link dir exists, but is a non-symlink
+    os.unlink(plugin_link_dir)
+    with file(plugin_link_dir, 'w') as clobber_file:
+        clobber_file.write('clobbered!')
+
+    result = run_assetlink().collection[0]
+    assert result == 'Could not link "staticstuff": %s exists and is not a symlink\n' % (
+        plugin_link_dir)
+
+    with file(plugin_link_dir, 'r') as clobber_file:
+        assert clobber_file.read() == 'clobbered!'
+
+
+def test_plugin_staticdirect(static_plugin_app):
+    """
+    Test that the staticdirect utilities pull up the right things
+    """
+    result = json.loads(
+        static_plugin_app.get('/staticstuff/').body)
+
+    assert len(result) == 2
+
+    assert result['mgoblin_bunny_pic'] == '/test_static/images/bunny_pic.png'
+    assert result['plugin_bunny_css'] == \
+        '/plugin_static/staticstuff/css/bunnify.css'
+