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 893d4f
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 893d4f
    os.path.abspath(__file__)), '..'))
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 893d4f
        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 b7dbd6
            'ssh://user@host.lcl:/path/to/repo.git',
Pierre-Yves Chibon b7dbd6
            'git@github.com:user/project.git',
Pierre-Yves Chibon b7dbd6
            'ssh://user@host.org/target',
Pierre-Yves Chibon b7dbd6
            'git+ssh://user@host.org/target',
Pierre-Yves Chibon b7dbd6
            '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 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 893d4f
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 893d4f
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 893d4f
            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 893d4f
                '<title>Settings Mirroring - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 893d4f
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            data = {}
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            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 893d4f
                '<title>Settings Mirroring - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 893d4f
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            self.assertFalse(os.path.exists(os.path.join(
Pierre-Yves Chibon 893d4f
                self.path, 'repos', 'test.git', 'hooks',
Pierre-Yves Chibon 893d4f
                'post-receive.mirror')))
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 893d4f
        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 = {'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 893d4f
                '/test/settings/Mirroring', data=data, follow_redirects=True)
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 893d4f
                '<title>Settings - test - Pagure</title>', output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 893d4f
                ' Hook Mirroring deactivated',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            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 893d4f
                '<title>Settings Mirroring - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 893d4f
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            self.assertFalse(os.path.exists(os.path.join(
Pierre-Yves Chibon 893d4f
                self.path, 'repos', 'test.git', 'hooks',
Pierre-Yves Chibon 893d4f
                'post-receive.mirror')))
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 893d4f
        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 893d4f
                'csrf_token': csrf_token,
Pierre-Yves Chibon 893d4f
                'active': True,
Pierre-Yves Chibon 893d4f
                '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 893d4f
                '/test/settings/Mirroring', data=data, follow_redirects=True)
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 893d4f
                '<title>Settings Mirroring - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
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 7a5ac6
                    '    \n  '
Pierre-Yves Chibon 7a5ac6
                    '\n      
Invalid '
Pierre-Yves Chibon 7a5ac6
                    'input.', output_text)
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 7a5ac6
                    'input.', output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            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 893d4f
                '<title>Settings Mirroring - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 893d4f
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            self.assertFalse(os.path.exists(os.path.join(
Pierre-Yves Chibon 893d4f
                self.path, 'repos', 'test.git', 'hooks',
Pierre-Yves Chibon 893d4f
                'post-receive.mirror')))
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 893d4f
        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 893d4f
                'csrf_token': csrf_token,
Pierre-Yves Chibon 893d4f
                'active': True,
Pierre-Yves Chibon 893d4f
                '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 893d4f
                '/test/settings/Mirroring', data=data, follow_redirects=True)
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 893d4f
                '<title>Settings - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 893d4f
                ' Hook Mirroring activated',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            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 893d4f
                '<title>Settings Mirroring - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 893d4f
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            self.assertTrue(os.path.exists(os.path.join(
Pierre-Yves Chibon 893d4f
                self.path, 'repos', 'test.git', 'hooks',
Pierre-Yves Chibon 893d4f
                'post-receive')))
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 893d4f
        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 893d4f
            data = {'csrf_token': csrf_token}
Pierre-Yves Chibon 893d4f
            output = self.app.post(
Pierre-Yves Chibon 893d4f
                '/test/settings/Mirroring', data=data, follow_redirects=True)
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 893d4f
                '<title>Settings - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Pierre-Yves Chibon 893d4f
                ' Hook Mirroring deactivated',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            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 893d4f
                '<title>Settings Mirroring - test - Pagure</title>',
Pierre-Yves Chibon 893d4f
                output_text)
Pierre-Yves Chibon 893d4f
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 893d4f
                'type="checkbox" value="y">', output.get_data(as_text=True))
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
            self.assertFalse(os.path.exists(os.path.join(
Pierre-Yves Chibon 893d4f
                self.path, 'repos', 'test.git', 'hooks',
Pierre-Yves Chibon 893d4f
                'post-receive.mirror')))
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
Pierre-Yves Chibon 893d4f
if __name__ == '__main__':
Pierre-Yves Chibon 893d4f
    unittest.main(verbosity=2)