Blame tests/test_pagure_flask_ui_plugins_mirror.py

Pierre-Yves Chibon 893d4f
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
"""
Pierre-Yves Chibon 893d4f
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
 Authors:
Pierre-Yves Chibon 893d4f
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
"""
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
import unittest
Pierre-Yves Chibon 893d4f
import sys
Pierre-Yves Chibon 893d4f
import os
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
from mock import patch
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon b7dbd6
import pagure.utils
Pierre-Yves Chibon 893d4f
import tests
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
class PagureFlaskPluginMirrortests(tests.Modeltests):
Pierre-Yves Chibon 893d4f
    """ Tests for mirror plugin of pagure """
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
    def setUp(self):
Pierre-Yves Chibon 893d4f
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 893d4f
        super(PagureFlaskPluginMirrortests, self).setUp()
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon b7dbd6
    def test_valid_ssh_url_pattern(self):
Pierre-Yves Chibon b7dbd6
        """ Check a number of valide ssh target that the pattern should let
Pierre-Yves Chibon b7dbd6
        through.
Pierre-Yves Chibon b7dbd6
        """
Pierre-Yves Chibon b7dbd6
        entries = [
Pierre-Yves Chibon 73d120
            "ssh://user@host.lcl:/path/to/repo.git",
Pierre-Yves Chibon 73d120
            "git@github.com:user/project.git",
Pierre-Yves Chibon 73d120
            "ssh://user@host.org/target",
Pierre-Yves Chibon 73d120
            "git+ssh://user@host.org/target",
Pierre-Yves Chibon 73d120
            "git+ssh://user@host.lcl:/path/to/repo.git",
Pierre-Yves Chibon b7dbd6
        ]
Pierre-Yves Chibon b7dbd6
        for el in entries:
Pierre-Yves Chibon b7dbd6
            print(el)
Pierre-Yves Chibon b7dbd6
            self.assertIsNotNone(pagure.utils.ssh_urlpattern.match(el))
Pierre-Yves Chibon b7dbd6
Pierre-Yves Chibon 893d4f
    def test_plugin_mirror_no_csrf(self):
Pierre-Yves Chibon 893d4f
        """ Test setting up the mirror plugin with no csrf. """
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 893d4f
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/settings/Mirroring")
Pierre-Yves Chibon 893d4f
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Mirroring - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            data = {}
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
            output = self.app.post("/test/settings/Mirroring", data=data)
Pierre-Yves Chibon 893d4f
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Mirroring - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
            self.assertFalse(
Pierre-Yves Chibon 73d120
                os.path.exists(
Pierre-Yves Chibon 73d120
                    os.path.join(
Pierre-Yves Chibon 73d120
                        self.path,
Pierre-Yves Chibon 73d120
                        "repos",
Pierre-Yves Chibon 73d120
                        "test.git",
Pierre-Yves Chibon 73d120
                        "hooks",
Pierre-Yves Chibon 73d120
                        "post-receive.mirror",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
    def test_plugin_mirror_no_data(self):
Pierre-Yves Chibon 893d4f
        """ Test the setting up the mirror plugin when there are no data
Pierre-Yves Chibon 893d4f
        provided in the request.
Pierre-Yves Chibon 893d4f
        """
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 893d4f
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 893d4f
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            # With the git repo
Pierre-Yves Chibon 893d4f
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Mirroring", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings - test - Pagure</title>", output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn(" Hook Mirroring deactivated", output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/settings/Mirroring", data=data)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Mirroring - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
            self.assertFalse(
Pierre-Yves Chibon 73d120
                os.path.exists(
Pierre-Yves Chibon 73d120
                    os.path.join(
Pierre-Yves Chibon 73d120
                        self.path,
Pierre-Yves Chibon 73d120
                        "repos",
Pierre-Yves Chibon 73d120
                        "test.git",
Pierre-Yves Chibon 73d120
                        "hooks",
Pierre-Yves Chibon 73d120
                        "post-receive.mirror",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
    def test_plugin_mirror_invalid_target(self):
Pierre-Yves Chibon 893d4f
        """ Test the setting up the mirror plugin when there are the target
Pierre-Yves Chibon 893d4f
        provided is invalid.
Pierre-Yves Chibon 893d4f
        """
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 893d4f
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 893d4f
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "active": True,
Pierre-Yves Chibon 73d120
                "target": "https://host.org/target",
Pierre-Yves Chibon 893d4f
            }
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            # With the git repo
Pierre-Yves Chibon 893d4f
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Mirroring", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Mirroring - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            if self.get_wtforms_version() >= (2, 2):
Pierre-Yves Chibon 893d4f
                self.assertIn(
Pierre-Yves Chibon 7a5ac6
                    '
\n '
Pierre-Yves Chibon 7a5ac6
                    '
Pierre-Yves Chibon 7a5ac6
                    'required type="text" value="https://host.org/target">\n'
Pierre-Yves Chibon 73d120
                    "    \n  "
Pierre-Yves Chibon 7a5ac6
                    '\n      
Invalid '
Pierre-Yves Chibon 73d120
                    "input.",
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 893d4f
            else:
Pierre-Yves Chibon 893d4f
                self.assertIn(
Ryan Lerch 014c1c
                    '
\n '
Ryan Lerch 014c1c
                    '
Ryan Lerch 014c1c
                    'type="text" value="https://host.org/target">\n    \n  '
Pierre-Yves Chibon 7a5ac6
                    '\n      
Invalid '
Pierre-Yves Chibon 73d120
                    "input.",
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/settings/Mirroring", data=data)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Mirroring - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
            self.assertFalse(
Pierre-Yves Chibon 73d120
                os.path.exists(
Pierre-Yves Chibon 73d120
                    os.path.join(
Pierre-Yves Chibon 73d120
                        self.path,
Pierre-Yves Chibon 73d120
                        "repos",
Pierre-Yves Chibon 73d120
                        "test.git",
Pierre-Yves Chibon 73d120
                        "hooks",
Pierre-Yves Chibon 73d120
                        "post-receive.mirror",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
    def test_setting_up_mirror(self):
Pierre-Yves Chibon 893d4f
        """ Test the setting up the mirror plugin.
Pierre-Yves Chibon 893d4f
        """
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 893d4f
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 893d4f
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "active": True,
Pierre-Yves Chibon 73d120
                "target": "ssh://user@host.org/target",
Pierre-Yves Chibon 893d4f
            }
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            # With the git repo
Pierre-Yves Chibon 893d4f
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Mirroring", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings - test - Pagure</title>", output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn(" Hook Mirroring activated", output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/settings/Mirroring", data=data)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Mirroring - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
            self.assertTrue(
Pierre-Yves Chibon 73d120
                os.path.exists(
Pierre-Yves Chibon 73d120
                    os.path.join(
Pierre-Yves Chibon 73d120
                        self.path, "repos", "test.git", "hooks", "post-receive"
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
    def test_plugin_mirror_deactivate(self):
Pierre-Yves Chibon 893d4f
        """ Test the deactivating the mirror plugin.
Pierre-Yves Chibon 893d4f
        """
Pierre-Yves Chibon 893d4f
        self.test_setting_up_mirror()
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 893d4f
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 893d4f
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            # De-Activate hook
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 893d4f
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Mirroring", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings - test - Pagure</title>", output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn(" Hook Mirroring deactivated", output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/settings/Mirroring", data=data)
Pierre-Yves Chibon 893d4f
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 893d4f
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Mirroring - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
            self.assertFalse(
Pierre-Yves Chibon 73d120
                os.path.exists(
Pierre-Yves Chibon 73d120
                    os.path.join(
Pierre-Yves Chibon 73d120
                        self.path,
Pierre-Yves Chibon 73d120
                        "repos",
Pierre-Yves Chibon 73d120
                        "test.git",
Pierre-Yves Chibon 73d120
                        "hooks",
Pierre-Yves Chibon 73d120
                        "post-receive.mirror",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 893d4f
    unittest.main(verbosity=2)