code:
# -*- coding: utf-8 -*-

"""
 (c) 2015-2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
import wtforms
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import pagure.hooks
import pagure.ui.plugins
import tests

class FakeForm(wtforms.Form):
    ''' Form to configure the mail hook. '''
    field1 = wtforms.TextField(
        'Title', [pagure.hooks.RequiredIf('active')]
    )
    field2 = wtforms.BooleanField(
        'Title2', [wtforms.validators.Optional()]
    )

class PagureFlaskPluginstests(tests.Modeltests):
    """ Tests for flask plugins controller of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginstests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_get_plugin_names(self):
        """ Test the get_plugin_names function. """
        names = pagure.ui.plugins.get_plugin_names()
        self.assertEqual(
            sorted(names),
            [
                'Block Un-Signed commits',  'Block non fast-forward pushes',
                'Fedmsg', 'IRC', 'Mail', 'Pagure', 'Pagure CI',
                'Pagure requests', 'Pagure tickets', 'Read the Doc',
            ]
        )

    def test_get_plugin(self):
        """ Test the get_plugin function. """
        name = pagure.ui.plugins.get_plugin('Mail')
        self.assertEqual(str(name), "<class 'pagure.hooks.mail.Mail'>")

    def test_view_plugin_page(self):
        """ Test the view_plugin_page endpoint. """

        output = self.app.get('/foo/settings/Mail')
        self.assertEqual(output.status_code, 302)

        user = tests.FakeUser()
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/foo/settings/Mail')
            self.assertEqual(output.status_code, 404)

            tests.create_projects(self.session)
            tests.create_projects_git(tests.HERE)

            output = self.app.get('/test/settings/Mail')
            self.assertEqual(output.status_code, 403)

        user.username = 'pingou'
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/Mail')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '<form action="/test/settings/Mail" method="post">',
                output.data)
            self.assertIn(
                'Mail to	',
                output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {
                'active': True,
                'mail_to': 'pingou@fp.org',
                'csrf_token': csrf_token,
            }

            output = self.app.post(
                '/test/settings/Mail', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Mail activated', output.data)

            data = {
                'mail_to': '',
                'csrf_token': csrf_token,
            }

            output = self.app.post(
                '/test/settings/Mail', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Mail inactived', output.data)

    def test_RequiredIf(self):
        """ Test the behavior of the RequiredIf validator. """
        form = FakeForm()

        try:
            form.validate()
        except Exception as err:
            self.assertEqual(
                str(err), 'no field named "active" in form')

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskPluginstests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2015-2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginFedmsgtests(tests.Modeltests):
    """ Tests for fedmsg plugin of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginFedmsgtests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_fedmsg(self):
        """ Test the fedmsg plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/Fedmsg')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Fedmsg settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post('/test/settings/Fedmsg', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Fedmsg settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.fedmsg')))

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post('/test/settings/Fedmsg', data=data)
            self.assertEqual(output.status_code, 404)

            tests.create_projects_git(tests.HERE)

            # With the git repo
            output = self.app.post(
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Fedmsg inactived',
                output.data)
            output = self.app.get('/test/settings/Fedmsg', data=data)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn('
Fedmsg settings
', output.data)
            self.assertIn(
                '<input id="active" name="active" type="checkbox" value="y">',
                output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.fedmsg')))

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
            }

            output = self.app.post(
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Fedmsg activated',
                output.data)
            output = self.app.get('/test/settings/Fedmsg', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Fedmsg settings
' in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertTrue(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.fedmsg')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post(
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Fedmsg inactived',
                output.data)
            output = self.app.get('/test/settings/Fedmsg', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Fedmsg settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.fedmsg')))

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginFedmsgtests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)

repo_name: bill/auger

license: mit


code:
# -*- coding: utf-8 -*-

"""
 (c) 2015-2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginIRCtests(tests.Modeltests):
    """ Tests for pagure_hook plugin of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginIRCtests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_mail(self):
        """ Test the irc plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/IRC')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
IRC settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post('/test/settings/IRC', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
IRC settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.irc')))

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post('/test/settings/IRC', data=data)
            self.assertEqual(output.status_code, 404)

            tests.create_projects_git(tests.HERE)

            # With the git repo
            output = self.app.post(
                '/test/settings/IRC', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook IRC inactived' in output.data)

            output = self.app.get('/test/settings/IRC')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
IRC settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.irc')))

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
                'server': 'irc.freenode.net',
                'port': 6667,
                'room': '#fedora-apps',
            }

            output = self.app.post(
                '/test/settings/IRC', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook IRC activated' in output.data)

            output = self.app.get('/test/settings/IRC')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
IRC settings
' in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            # TODO: Fix this
            #self.assertTrue(os.path.exists(os.path.join(
                #tests.HERE, 'test.git', 'hooks', 'post-receive.irc')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post('/test/settings/IRC', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook IRC inactived' in output.data)

            output = self.app.get('/test/settings/IRC')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
IRC settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.irc')))

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginIRCtests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2015-2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginMailtests(tests.Modeltests):
    """ Tests for flask plugins controller of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginMailtests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_mail(self):
        """ Test the mail plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/Mail')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Mail settings
' in output.data)
            self.assertTrue(
                'Mail to	'
                in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post('/test/settings/Mail', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Mail settings
' in output.data)
            self.assertTrue(
                'Mail to	'
                in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post('/test/settings/Mail', data=data)
            self.assertEqual(output.status_code, 404)

            tests.create_projects_git(tests.HERE)

            # With the git repo
            output = self.app.post(
                '/test/settings/Mail', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Mail inactived' in output.data)

            output = self.app.get('/test/settings/Mail')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Mail settings
' in output.data)
            self.assertTrue(
                'Mail to	'
                in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))

            # Missing the required mail_to
            data = {'csrf_token': csrf_token, 'active': 'y'}

            output = self.app.post(
                '/test/settings/Mail', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Mail settings
' in output.data)
            self.assertFalse(
                '</button>\n                      Hook activated' in output.data)
            self.assertTrue(
                '<input id="mail_to" name="mail_to" type="text" value="">	'
                '\nThis field is required.	'
                in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
                'mail_to': 'foo@bar'
            }

            output = self.app.post(
                '/test/settings/Mail', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Mail activated' in output.data)

            output = self.app.get('/test/settings/Mail')
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Mail settings
' in output.data)
            self.assertTrue(
                'Mail to	'
                in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertTrue(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post(
                '/test/settings/Mail', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Mail inactived' in output.data)

            output = self.app.get('/test/settings/Mail')
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Mail settings
' in output.data)
            self.assertTrue(
                'Mail to	'
                in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginMailtests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginNoFFtests(tests.Modeltests):
    """ Tests for Block non fast-forward pushes plugin of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginNoFFtests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_noff(self):
        """ Test the noff plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get(
                '/test/settings/Block non fast-forward pushes')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block non fast-forward pushes settings
',
                output.data)
            self.assertIn(
                '<input id="branches" name="branches" type="text" '
                'value="">	', output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post(
                '/test/settings/Block non fast-forward pushes', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block non fast-forward pushes settings
',
                output.data)
            self.assertIn(
                '<input id="branches" name="branches" type="text" '
                'value="">	', output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post(
                '/test/settings/Block non fast-forward pushes', data=data)
            self.assertEqual(output.status_code, 404)

            tests.create_projects_git(tests.HERE)

            # With the git repo
            output = self.app.post(
                '/test/settings/Block non fast-forward pushes',
                data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Block non '
                'fast-forward pushes inactived' in output.data)

            output = self.app.get(
                '/test/settings/Block non fast-forward pushes')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block non fast-forward pushes settings
',
                output.data)
            self.assertIn(
                '<input id="branches" name="branches" type="text" '
                'value="">	', output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))

            # Missing the required mail_to
            data = {'csrf_token': csrf_token, 'active': 'y'}

            output = self.app.post(
                '/test/settings/Block non fast-forward pushes',
                data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block non fast-forward pushes settings
',
                output.data)
            self.assertNotIn(
                '</button>\n                      Hook activated',
                output.data)
            self.assertIn(
                '<input id="branches" name="branches" type="text" '
                'value="">	', output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks',
                'pre-receive.pagureforcecommit')))

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
                'branches': 'master',
            }

            output = self.app.post(
                '/test/settings/Block non fast-forward pushes',
                data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Block non '
                'fast-forward pushes activated', output.data)

            output = self.app.get(
                '/test/settings/Block non fast-forward pushes')
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block non fast-forward pushes settings
',
                output.data)
            self.assertIn(
                '<input id="branches" name="branches" type="text" '
                'value="master">	', output.data)
            self.assertIn(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">', output.data)

            self.assertTrue(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks',
                'pre-receive.pagureforcecommit')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post(
                '/test/settings/Block non fast-forward pushes',
                data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Block non '
                'fast-forward pushes inactived', output.data)

            output = self.app.get(
                '/test/settings/Block non fast-forward pushes')
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block non fast-forward pushes settings
',
                output.data)
            self.assertIn(
                '<input id="branches" name="branches" type="text" '
                'value="">	', output.data)
            self.assertIn(
                '<input id="active" name="active" type="checkbox" '
                'value="y">', output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks',
                'pre-receive.pagureforcecommit')))

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginNoFFtests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginPagureCItests(tests.Modeltests):
    """ Tests for flask plugins controller of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginPagureCItests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_pagure_ci(self):
        """ Test the pagure ci plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/Pagure CI')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure CI settings
' in output.data)
            self.assertTrue(
                'Name of project in Pagure	'
                in output.data)
            self.assertTrue(
                'Name of project in Jenkins	'
                in output.data)
            self.assertTrue(
                'Jenkins URL	'
                in output.data)
            self.assertTrue(
                'Jenkins token	'
                in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post('/test/settings/Pagure CI', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure CI settings
' in output.data)
            self.assertTrue(
                'Name of project in Pagure	'
                in output.data)
            self.assertTrue(
                'Name of project in Jenkins	'
                in output.data)
            self.assertTrue(
                'Jenkins URL	'
                in output.data)
            self.assertTrue(
                'Jenkins token	'
                in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
                'pagure_name': 'test',
                'jenkins_name': 'jenkins_test',
                'jenkins_url': 'https://jenkins.fedoraproject.org',
                'jenkins_token': 'BEEFCAFE'
            }
            # No git found
            output = self.app.post(
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 404)

            tests.create_projects_git(tests.HERE)

            data = {'csrf_token': csrf_token}
            # With the git repo
            output = self.app.post(
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)

            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure CI settings
' in output.data)
            self.assertFalse(
                '</button>\n                      Hook activated' in output.data)
            self.assertTrue(
                '<input id="pagure_name" name="pagure_name" type="text" value="">	'
                '\nThis field is required.	'
                in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">' in output.data)

            output = self.app.get('/test/settings/Pagure CI')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure CI settings
' in output.data)
            self.assertTrue(
                'Name of project in Pagure	'
                in output.data)
            self.assertTrue(
                'Name of project in Jenkins	'
                in output.data)
            self.assertTrue(
                'Jenkins URL	'
                in output.data)
            self.assertTrue(
                'Jenkins token	'
                in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" value="y">'
                in output.data)

            # Missing the required
            data = {'csrf_token': csrf_token, 'active': 'y'}

            output = self.app.post(
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure CI settings
' in output.data)
            self.assertFalse(
                '</button>\n                      Hook activated' in output.data)
            self.assertTrue(
                '<input id="pagure_name" name="pagure_name" type="text" value="">	'
                '\nThis field is required.	'
                in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
                'pagure_name': 'test',
                'jenkins_name': 'jenkins_test',
                'jenkins_url': 'https://jenkins.fedoraproject.org',
                'jenkins_token': 'BEEFCAFE'
            }

            output = self.app.post(
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure CI activated' in output.data)

            output = self.app.get('/test/settings/Pagure CI')
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure CI settings
' in output.data)
            self.assertTrue(
                'Name of project in Pagure	'
                in output.data)
            self.assertTrue(
                '<input id="pagure_name" name="pagure_name" type="text" value="test">	'
                in output.data)
            self.assertTrue(
                'Name of project in Jenkins	'
                in output.data)
            self.assertTrue(
                '<input id="jenkins_name" name="jenkins_name" type="text" value="jenkins_test">	'
                in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" value="y">'
                in output.data)

            # De-Activate hook
            data = {
                'csrf_token': csrf_token,
                'pagure_name': 'test',
                'jenkins_name': 'jenkins_test',
                'jenkins_url': 'https://jenkins.fedoraproject.org',
                'jenkins_token': 'BEEFCAFE'
            }
            output = self.app.post(
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                '</button>\n                      Hook Pagure CI inactived' in output.data)

            self.assertIn(
                '
\n
Settings for test
',
                output.data)

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginPagureCItests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2015-2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginPagureHooktests(tests.Modeltests):
    """ Tests for pagure_hook plugin of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginPagureHooktests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
            tests.HERE, 'requests')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_mail(self):
        """ Test the pagure hook plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/Pagure')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post('/test/settings/Pagure', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post('/test/settings/Pagure', data=data)
            self.assertEqual(output.status_code, 404)

            tests.create_projects_git(tests.HERE)
            tests.create_projects_git(os.path.join(tests.HERE, 'docs'))
            tests.create_projects_git(os.path.join(tests.HERE, 'requests'))

            # With the git repo
            output = self.app.post(
                '/test/settings/Pagure', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure inactived'
                in output.data)

            output = self.app.get('/test/settings/Pagure')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.pagure')))

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
            }

            output = self.app.post(
                '/test/settings/Pagure', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure activated'
                in output.data)

            output = self.app.get('/test/settings/Pagure')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure settings
' in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertTrue(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.pagure')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post(
                '/test/settings/Pagure', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure inactived'
                in output.data)

            output = self.app.get('/test/settings/Pagure')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks', 'post-receive.pagure')))

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginPagureHooktests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2015-2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginPagureRequestHooktests(tests.Modeltests):
    """ Tests for pagure_hook plugin of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginPagureRequestHooktests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
            tests.HERE, 'requests')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_pagure_request(self):
        """ Test the pagure_request plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/Pagure requests')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure requests settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post('/test/settings/Pagure requests', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure requests settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post('/test/settings/Pagure requests', data=data)
            self.assertEqual(output.status_code, 404)

            # Create both the requests repo
            tests.create_projects_git(os.path.join(tests.HERE, 'requests'))

            # With the git repo
            tests.create_projects_git(tests.HERE)
            output = self.app.post(
                '/test/settings/Pagure requests', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure requests inactived'
                in output.data)

            output = self.app.get('/test/settings/Pagure requests')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure requests settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'requests', 'test.git', 'hooks',
                'post-receive.pagure')))

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
            }

            output = self.app.post(
                '/test/settings/Pagure requests', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure requests activated'
                in output.data)

            output = self.app.get('/test/settings/Pagure requests')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure requests settings
' in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertTrue(os.path.exists(os.path.join(
                tests.HERE, 'requests', 'test.git', 'hooks',
                'post-receive.pagure-requests')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post(
                '/test/settings/Pagure requests', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure requests inactived'
                in output.data)

            output = self.app.get('/test/settings/Pagure requests')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure requests settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'requests', 'test.git', 'hooks',
                'post-receive.pagure-requests')))

            # Try re-activate hook w/o the git repo
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
            }
            shutil.rmtree(os.path.join(tests.HERE, 'requests', 'test.git'))

            output = self.app.post('/test/settings/Pagure requests', data=data)
            self.assertEqual(output.status_code, 404)

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginPagureRequestHooktests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2015-2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginPagureTicketHooktests(tests.Modeltests):
    """ Tests for pagure_hook plugin of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginPagureTicketHooktests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_pagure_ticket(self):
        """ Test the pagure_ticket plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/Pagure tickets')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure tickets settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post('/test/settings/Pagure tickets', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure tickets settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post('/test/settings/Pagure tickets', data=data)
            self.assertEqual(output.status_code, 404)

            # Create both the tickets repo
            tests.create_projects_git(os.path.join(tests.HERE, 'tickets'))

            # With the git repo
            tests.create_projects_git(tests.HERE)
            output = self.app.post(
                '/test/settings/Pagure tickets', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure tickets inactived'
                in output.data)

            output = self.app.get('/test/settings/Pagure tickets')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure tickets settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'tickets', 'test.git', 'hooks',
                'post-receive.pagure')))

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
            }

            output = self.app.post(
                '/test/settings/Pagure tickets', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure tickets activated'
                in output.data)

            output = self.app.get('/test/settings/Pagure tickets')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure tickets settings
' in output.data)
            self.assertTrue(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertTrue(os.path.exists(os.path.join(
                tests.HERE, 'tickets', 'test.git', 'hooks',
                'post-receive.pagure-ticket')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post(
                '/test/settings/Pagure tickets', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Pagure tickets inactived'
                in output.data)

            output = self.app.get('/test/settings/Pagure tickets')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertTrue('
Pagure tickets settings
' in output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" '
                'value="y">' in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'tickets', 'test.git', 'hooks',
                'post-receive.pagure-ticket')))

            # Try re-activate hook w/o the git repo
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
            }
            shutil.rmtree(os.path.join(tests.HERE, 'tickets', 'test.git'))

            output = self.app.post('/test/settings/Pagure tickets', data=data)
            self.assertEqual(output.status_code, 404)

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginPagureTicketHooktests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginRtdHooktests(tests.Modeltests):
    """ Tests for rtd_hook plugin of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginRtdHooktests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
            tests.HERE, 'requests')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_pagure_request(self):
        """ Test the pagure_request plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get('/test/settings/Read the Doc')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn('
Read the Doc settings
', output.data)
            self.assertIn(
                '<input id="active" name="active" type="checkbox" value="y">',
                output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post('/test/settings/Read the Doc', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn('
Read the Doc settings
', output.data)
            self.assertIn(
                '<input id="active" name="active" type="checkbox" value="y">',
                output.data)

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post('/test/settings/Read the Doc', data=data)
            self.assertEqual(output.status_code, 404)

            # Create both the requests repo
            tests.create_projects_git(os.path.join(tests.HERE, 'requests'))

            # With the git repo
            tests.create_projects_git(tests.HERE)
            output = self.app.post(
                '/test/settings/Read the Doc', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Read the Doc inactived',
                output.data)

            output = self.app.get('/test/settings/Read the Doc')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn('
Read the Doc settings
', output.data)
            self.assertIn(
                '<input id="active" name="active" type="checkbox" value="y">',
                output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'requests', 'test.git', 'hooks',
                'post-receive.pagure')))

            # Activate hook
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
                'project_name': 'foo',
            }

            output = self.app.post(
                '/test/settings/Read the Doc', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Read the Doc activated',
                output.data)

            output = self.app.get('/test/settings/Read the Doc')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn('
Read the Doc settings
', output.data)
            self.assertIn(
                '<input checked id="active" name="active" type="checkbox" '
                'value="y">', output.data)

            self.assertTrue(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks',
                'post-receive.rtd')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post(
                '/test/settings/Read the Doc', data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Read the Doc inactived',
                output.data)

            output = self.app.get('/test/settings/Read the Doc')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn('
Read the Doc settings
', output.data)
            self.assertIn(
                '<input id="active" name="active" type="checkbox" '
                'value="y">', output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks',
                'post-receive.rtd')))

            # Try re-activate hook w/o the git repo
            data = {
                'csrf_token': csrf_token,
                'active': 'y',
                'project_name': 'foo',
            }
            shutil.rmtree(os.path.join(tests.HERE, 'test.git'))

            output = self.app.post('/test/settings/Read the Doc', data=data)
            self.assertEqual(output.status_code, 404)

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginRtdHooktests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests

class PagureFlaskPluginUnsignedtests(tests.Modeltests):
    """ Tests for Block pushes with unsigned commit plugin of pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPluginUnsignedtests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.plugins.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.filters.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
            tests.HERE, 'forks')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    def test_plugin_unsigned(self):
        """ Test the noff plugin on/off endpoint. """

        tests.create_projects(self.session)

        user = tests.FakeUser(username='pingou')
        with tests.user_set(pagure.APP, user):
            output = self.app.get(
                '/test/settings/Block Un-Signed commits')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block Un-Signed commits settings
',
                output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {}

            output = self.app.post(
                '/test/settings/Block Un-Signed commits', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block Un-Signed commits settings
',
                output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            data['csrf_token'] = csrf_token
            # No git found
            output = self.app.post(
                '/test/settings/Block Un-Signed commits', data=data)
            self.assertEqual(output.status_code, 404)

            tests.create_projects_git(tests.HERE)

            # With the git repo
            output = self.app.post(
                '/test/settings/Block Un-Signed commits',
                data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Block Un-Signed '
                'commits inactived' in output.data)

            output = self.app.get(
                '/test/settings/Block Un-Signed commits')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block Un-Signed commits settings
',
                output.data)
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks',
                'pre-receive.pagureunsignedcommit')))

            # Activate the hook
            data = {'csrf_token': csrf_token, 'active': 'y'}

            output = self.app.post(
                '/test/settings/Block Un-Signed commits',
                data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertNotIn(
                '</button>\n                      Hook activated',
                output.data)

            self.assertTrue(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks',
                'pre-receive.pagureunsignedcommit')))

            # De-Activate hook
            data = {'csrf_token': csrf_token}
            output = self.app.post(
                '/test/settings/Block Un-Signed commits',
                data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '
\n
Settings for test
',
                output.data)
            self.assertTrue(
                '</button>\n                      Hook Block Un-Signed '
                'commits inactived' in output.data)

            output = self.app.get(
                '/test/settings/Block Un-Signed commits')
            self.assertIn(
                '
\n'
                'test project #1
', output.data)
            self.assertIn(
                '
Block Un-Signed commits settings
',
                output.data)
            self.assertIn(
                '<input id="active" name="active" type="checkbox" '
                'value="y">', output.data)

            self.assertFalse(os.path.exists(os.path.join(
                tests.HERE, 'test.git', 'hooks',
                'pre-receive.pagureunsignedcommit')))

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPluginUnsignedtests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)


code:
# -*- coding: utf-8 -*-

"""
 (c) 2016 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import datetime
import json
import unittest
import shutil
import sys
import tempfile
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

import pagure.lib
import tests
from pagure.lib.repo import PagureRepo

class PagureFlaskPrioritiestests(tests.Modeltests):
    """ Tests for the behavior of priorities in pagure """

    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(PagureFlaskPrioritiestests, self).setUp()

        pagure.APP.config['TESTING'] = True
        pagure.SESSION = self.session
        pagure.ui.SESSION = self.session
        pagure.ui.app.SESSION = self.session
        pagure.ui.filters.SESSION = self.session
        pagure.ui.repo.SESSION = self.session
        pagure.ui.issues.SESSION = self.session

        pagure.APP.config['GIT_FOLDER'] = tests.HERE
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
            tests.HERE, 'requests')
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
            tests.HERE, 'tickets')
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
            tests.HERE, 'docs')
        self.app = pagure.APP.test_client()

    @patch('pagure.lib.git.update_git')
    @patch('pagure.lib.notify.send_email')
    def test_ticket_with_no_priority(self, p_send_email, p_ugt):
        """ Test creating a ticket without priority. """
        p_send_email.return_value = True
        p_ugt.return_value = True

        tests.create_projects(self.session)
        tests.create_projects_git(os.path.join(tests.HERE), bare=True)

        user = tests.FakeUser()
        user.username = 'pingou'
        with tests.user_set(pagure.APP, user):

            # Get the CSRF token
            output = self.app.get('/test/new_issue')
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                '
\n        New issue'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {
                'title': 'Test issue',
                'issue_content': 'We really should improve on this issue',
                'status': 'Open',
                'csrf_token': csrf_token,
            }

            # Create the issue
            output = self.app.post(
                '/test/new_issue', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '<title>Issue #1: Test issue - test - Pagure</title>',
                output.data)
            self.assertIn(
                '',
                output.data)
            self.assertNotIn('
', output.data)
            self.assertNotIn('<option value="1">High</option>', output.data)

    @patch('pagure.lib.git.update_git')
    @patch('pagure.lib.notify.send_email')
    def test_ticket_with_priorities(self, p_send_email, p_ugt):
        """ Test creating a ticket with priorities. """
        p_send_email.return_value = True
        p_ugt.return_value = True

        tests.create_projects(self.session)
        tests.create_projects_git(os.path.join(tests.HERE), bare=True)

        # Set some priorities
        repo = pagure.lib.get_project(self.session, 'test')
        repo.priorities = {'1': 'High', '2': 'Normal'}
        self.session.add(repo)
        self.session.commit()

        user = tests.FakeUser()
        user.username = 'pingou'
        with tests.user_set(pagure.APP, user):

            # Get the CSRF token
            output = self.app.get('/test/new_issue')
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                '
\n        New issue'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {
                'title': 'Test issue',
                'issue_content': 'We really should improve on this issue',
                'status': 'Open',
                'csrf_token': csrf_token,
            }

            # Create the issue
            output = self.app.post(
                '/test/new_issue', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '<title>Issue #1: Test issue - test - Pagure</title>',
                output.data)
            self.assertIn(
                '',
                output.data)
            self.assertIn('
', output.data)
            self.assertIn('<option value="1">High</option>', output.data)

    def test_update_priorities(self):
        """ Test updating priorities of a repo. """
        tests.create_projects(self.session)
        tests.create_projects_git(os.path.join(tests.HERE), bare=True)

        # Set some priorities
        repo = pagure.lib.get_project(self.session, 'test')
        self.assertEqual(repo.priorities, {})

        user = tests.FakeUser()
        user.username = 'pingou'
        with tests.user_set(pagure.APP, user):

            # Get the CSRF token
            output = self.app.get('/test/settings')
            self.assertEqual(output.status_code, 200)
            self.assertIn(
                '<title>Settings - test - Pagure</title>', output.data)
            self.assertIn('
Settings for test
', output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {
                'priority_weigth': 1,
                'priority_title': 'High',
            }
            output = self.app.post(
                '/test/update/priorities', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            # Check the redirect
            self.assertIn(
                '<title>Settings - test - Pagure</title>', output.data)
            self.assertIn('
Settings for test
', output.data)
            # Check the result of the action -- None, no CSRF
            repo = pagure.lib.get_project(self.session, 'test')
            self.assertEqual(repo.priorities, {})

            data = {
                'priority_weigth': 1,
                'priority_title': 'High',
                'csrf_token': csrf_token,
            }
            output = self.app.post(
                '/test/update/priorities', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            # Check the redirect
            self.assertIn(
                '<title>Settings - test - Pagure</title>', output.data)
            self.assertIn('
Settings for test
', output.data)
            # Check the result of the action -- Priority recorded
            repo = pagure.lib.get_project(self.session, 'test')
            self.assertEqual(repo.priorities, {u'': u'', u'1': u'High'})

            data = {
                'priority_weigth': [1, 2, 3],
                'priority_title': ['High', 'Normal', 'Low'],
                'csrf_token': csrf_token,
            }
            output = self.app.post(
                '/test/update/priorities', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            # Check the redirect
            self.assertIn(
                '<title>Settings - test - Pagure</title>', output.data)
            self.assertIn('
Settings for test
', output.data)
            # Check the result of the action -- Priority recorded
            repo = pagure.lib.get_project(self.session, 'test')
            self.assertEqual(
                repo.priorities,
                {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
            )

            # Check error - less weigths than titles
            data = {
                'priority_weigth': [1, 2],
                'priority_title': ['High', 'Normal', 'Low'],
                'csrf_token': csrf_token,
            }
            output = self.app.post(
                '/test/update/priorities', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            # Check the redirect
            self.assertIn(
                '<title>Settings - test - Pagure</title>', output.data)
            self.assertIn('
Settings for test
', output.data)
            self.assertIn(
                '</button>\n'
                '                      Priorities weights and titles are '
                'not of the same length', output.data)            # Check the result of the action -- Priorities un-changed
            repo = pagure.lib.get_project(self.session, 'test')
            self.assertEqual(
                repo.priorities,
                {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
            )

            # Check error - weigths must be integer
            data = {
                'priority_weigth': [1, 2, 'c'],
                'priority_title': ['High', 'Normal', 'Low'],
                'csrf_token': csrf_token,
            }
            output = self.app.post(
                '/test/update/priorities', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            # Check the redirect
            self.assertIn(
                '<title>Settings - test - Pagure</title>', output.data)
            self.assertIn('
Settings for test
', output.data)
            self.assertIn(
                '</button>\n'
                '                      Priorities weights must be numbers',
                output.data)
            # Check the result of the action -- Priorities un-changed
            repo = pagure.lib.get_project(self.session, 'test')
            self.assertEqual(
                repo.priorities,
                {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
            )

            # Check error - Twice the same priority weigth
            data = {
                'priority_weigth': [1, 2, 2],
                'priority_title': ['High', 'Normal', 'Low'],
                'csrf_token': csrf_token,
            }
            output = self.app.post(
                '/test/update/priorities', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            # Check the redirect
            self.assertIn(
                '<title>Settings - test - Pagure</title>', output.data)
            self.assertIn('
Settings for test
', output.data)
            self.assertIn(
                '</button>\n'
                '                      Priority weight 2 is present 2 times',
                output.data)
            # Check the result of the action -- Priorities un-changed
            repo = pagure.lib.get_project(self.session, 'test')
            self.assertEqual(
                repo.priorities,
                {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
            )

            # Check error - Twice the same priority title
            data = {
                'priority_weigth': [1, 2, 3],
                'priority_title': ['High', 'Normal', 'Normal'],
                'csrf_token': csrf_token,
            }
            output = self.app.post(
                '/test/update/priorities', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            # Check the redirect
            self.assertIn(
                '<title>Settings - test - Pagure</title>', output.data)
            self.assertIn('
Settings for test
', output.data)
            self.assertIn(
                '</button>\n'
                '                      Priority Normal is present 2 times',
                output.data)
            # Check the result of the action -- Priorities un-changed
            repo = pagure.lib.get_project(self.session, 'test')
            self.assertEqual(
                repo.priorities,
                {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
            )

            # Check the behavior if the project disabled the issue tracker
            settings = repo.settings
            settings['issue_tracker'] = False
            repo.settings = settings
            self.session.add(repo)
            self.session.commit()

            output = self.app.post(
                '/test/update/priorities', data=data)
            self.assertEqual(output.status_code, 404)

            # Check for an invalid project
            output = self.app.post(
                '/foo/update/priorities', data=data)
            self.assertEqual(output.status_code, 404)

        # Check for a non-admin user
        settings = repo.settings
        settings['issue_tracker'] = True
        repo.settings = settings
        self.session.add(repo)
        self.session.commit()

        user.username = 'ralph'
        with tests.user_set(pagure.APP, user):
            output = self.app.post(
                '/test/update/priorities', data=data)
            self.assertEqual(output.status_code, 403)

if __name__ == '__main__':
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
        PagureFlaskPrioritiestests)
    unittest.TextTestRunner(verbosity=2).run(SUITE)