Blame tests/test_pagure_flask_ui_plugins_noff.py

Pierre-Yves Chibon b6ab03
# -*- coding: utf-8 -*-
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
"""
Pierre-Yves Chibon 9071b6
 (c) 2016-2018 - Copyright Red Hat Inc
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
 Authors:
Pierre-Yves Chibon b6ab03
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
"""
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon b6ab03
import unittest
Pierre-Yves Chibon b6ab03
import sys
Pierre-Yves Chibon b6ab03
import os
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
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 b6ab03
Pierre-Yves Chibon b6ab03
import tests
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
Clement Verna 109c4b
class PagureFlaskPluginNoFFtests(tests.SimplePagureTest):
Pierre-Yves Chibon b6ab03
    """ Tests for Block non fast-forward pushes plugin of pagure """
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
    def test_plugin_noff(self):
Pierre-Yves Chibon b6ab03
        """ Test the noff plugin on/off endpoint. """
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon b6ab03
            output = self.app.get(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Block non fast-forward pushes - test - "</title>
Pierre-Yves Chibon 73d120
                "Pagure",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon c3d71e
            if self.get_wtforms_version() >= (2, 2):
Pierre-Yves Chibon c3d71e
                self.assertIn(
Pierre-Yves Chibon 7a5ac6
                    '
Pierre-Yves Chibon 73d120
                    'required type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon c3d71e
            else:
Pierre-Yves Chibon c3d71e
                self.assertIn(
Ryan Lerch 014c1c
                    '
Pierre-Yves Chibon 73d120
                    'type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon b6ab03
            self.assertTrue(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">' in output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
Aurélien Bompard 626417
            csrf_token = output_text.split(
Pierre-Yves Chibon 73d120
                'name="csrf_token" type="hidden" value="'
Pierre-Yves Chibon 73d120
            )[1].split('">')[0]
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            data = {}
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes", data=data
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Block non fast-forward pushes - test - "</title>
Pierre-Yves Chibon 73d120
                "Pagure",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon c3d71e
            if self.get_wtforms_version() >= (2, 2):
Pierre-Yves Chibon c3d71e
                self.assertIn(
Pierre-Yves Chibon 7a5ac6
                    '
Pierre-Yves Chibon 73d120
                    'required type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon c3d71e
            else:
Pierre-Yves Chibon c3d71e
                self.assertIn(
Ryan Lerch 014c1c
                    '
Pierre-Yves Chibon 73d120
                    'type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon b6ab03
            self.assertTrue(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">' in output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon 73d120
            data["csrf_token"] = csrf_token
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            # With the git repo
Pierre-Yves Chibon b6ab03
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 9071b6
                '
'
Pierre-Yves Chibon 73d120
                "Project Settings\n",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertTrue(
Pierre-Yves Chibon 73d120
                "Hook Block non "
Pierre-Yves Chibon 73d120
                "fast-forward pushes deactivated" in output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            output = self.app.get(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Block non fast-forward pushes - test - "</title>
Pierre-Yves Chibon 73d120
                "Pagure",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon c3d71e
            if self.get_wtforms_version() >= (2, 2):
Pierre-Yves Chibon c3d71e
                self.assertIn(
Pierre-Yves Chibon 7a5ac6
                    '
Pierre-Yves Chibon 73d120
                    'required type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon c3d71e
            else:
Pierre-Yves Chibon c3d71e
                self.assertIn(
Ryan Lerch 014c1c
                    '
Pierre-Yves Chibon 73d120
                    'type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon b6ab03
            self.assertTrue(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">' in output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
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.mail",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            # Missing the required mail_to
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token, "active": "y"}
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Block non fast-forward pushes - test - "</title>
Pierre-Yves Chibon 73d120
                "Pagure",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertNotIn("Hook activated", output_text)
Pierre-Yves Chibon 3e9b1d
            if self.get_wtforms_version() >= (2, 2):
Pierre-Yves Chibon 3e9b1d
                self.assertIn(
Pierre-Yves Chibon 7a5ac6
                    '
Pierre-Yves Chibon 73d120
                    'required type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 3e9b1d
            else:
Pierre-Yves Chibon 3e9b1d
                self.assertIn(
Ryan Lerch 014c1c
                    '
Pierre-Yves Chibon 73d120
                    'type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon b6ab03
            self.assertTrue(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">' in output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
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
                        "pre-receive.pagureforcecommit",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            # Activate hook
Pierre-Yves Chibon b6ab03
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "active": "y",
Pierre-Yves Chibon 73d120
                "branches": "master",
Pierre-Yves Chibon b6ab03
            }
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 9071b6
                '
'
Pierre-Yves Chibon 73d120
                "Project Settings\n",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 73d120
                "Hook Block non " "fast-forward pushes activated", output_text
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            output = self.app.get(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes"
Pierre-Yves Chibon 73d120
            )
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Block non fast-forward pushes - test - "</title>
Pierre-Yves Chibon 73d120
                "Pagure",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon c3d71e
            if self.get_wtforms_version() >= (2, 2):
Pierre-Yves Chibon c3d71e
                self.assertIn(
Pierre-Yves Chibon 7a5ac6
                    '
Pierre-Yves Chibon 73d120
                    'required type="text" value="master">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon c3d71e
            else:
Pierre-Yves Chibon c3d71e
                self.assertIn(
Ryan Lerch 014c1c
                    '
Pierre-Yves Chibon 73d120
                    'type="text" value="master">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon b6ab03
            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 b6ab03
Pierre-Yves Chibon b6ab03
            # De-Activate hook
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon b6ab03
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 9071b6
                '
'
Pierre-Yves Chibon 73d120
                "Project Settings\n",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 73d120
                "Hook Block non " "fast-forward pushes deactivated",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
            output = self.app.get(
Pierre-Yves Chibon 73d120
                "/test/settings/Block non fast-forward pushes"
Pierre-Yves Chibon 73d120
            )
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon b6ab03
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Settings Block non fast-forward pushes - test - "</title>
Pierre-Yves Chibon 73d120
                "Pagure",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon c3d71e
            if self.get_wtforms_version() >= (2, 2):
Pierre-Yves Chibon c3d71e
                self.assertIn(
Pierre-Yves Chibon 7a5ac6
                    '
Pierre-Yves Chibon 73d120
                    'required type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon c3d71e
            else:
Pierre-Yves Chibon c3d71e
                self.assertIn(
Ryan Lerch 014c1c
                    '
Pierre-Yves Chibon 73d120
                    'type="text" value="">',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon b6ab03
            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 b6ab03
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
                        "pre-receive.pagureforcecommit",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon b6ab03
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)