Blame tests/test_pagure_flask_ui_plugins_pagure_request_hook.py

Pierre-Yves Chibon 74ef37
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
"""
Pierre-Yves Chibon 9fae46
 (c) 2015-2016 - Copyright Red Hat Inc
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
 Authors:
Pierre-Yves Chibon 74ef37
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
"""
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 74ef37
import pkg_resources
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
import json
Pierre-Yves Chibon 74ef37
import unittest
Pierre-Yves Chibon 74ef37
import shutil
Pierre-Yves Chibon 74ef37
import sys
Pierre-Yves Chibon 74ef37
import os
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
import pygit2
Pierre-Yves Chibon 74ef37
from mock import patch
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 74ef37
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
import pagure.lib
Pierre-Yves Chibon 74ef37
import tests
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
class PagureFlaskPluginPagureRequestHooktests(tests.Modeltests):
Pierre-Yves Chibon 74ef37
    """ Tests for pagure_hook plugin of pagure """
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
    def setUp(self):
Pierre-Yves Chibon 74ef37
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 74ef37
        super(PagureFlaskPluginPagureRequestHooktests, self).setUp()
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 74ef37
        pagure.SESSION = self.session
Pierre-Yves Chibon 74ef37
        pagure.ui.SESSION = self.session
Pierre-Yves Chibon 74ef37
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon 74ef37
        pagure.ui.plugins.SESSION = self.session
Pierre-Yves Chibon 59394d
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon 9fae46
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
        pagure.APP.config['GIT_FOLDER'] = tests.HERE
Pierre-Yves Chibon 74ef37
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
Pierre-Yves Chibon 74ef37
            tests.HERE, 'forks')
Pierre-Yves Chibon 74ef37
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 74ef37
            tests.HERE, 'requests')
Pierre-Yves Chibon 74ef37
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 74ef37
            tests.HERE, 'docs')
Pierre-Yves Chibon 74ef37
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
    def test_plugin_pagure_request(self):
Pierre-Yves Chibon 74ef37
        """ Test the pagure_request plugin on/off endpoint. """
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
        tests.create_projects(self.session)
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 74ef37
        with tests.user_set(pagure.APP, user):
Lubomír Sedlář 4443c5
            output = self.app.get('/test/settings/Pagure requests')
Pierre-Yves Chibon 74ef37
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Pierre-Yves Chibon 98f2af
                'test project #1        ', output.data)
Lubomír Sedlář 4443c5
            self.assertTrue('

Pagure requests settings

' in output.data)
Pierre-Yves Chibon 74ef37
            self.assertTrue(
Pierre-Yves Chibon 74ef37
                '<input id="active" name="active" type="checkbox" value="y">'
Pierre-Yves Chibon 74ef37
                in output.data)
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            csrf_token = output.data.split(
Pierre-Yves Chibon 74ef37
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            data = {}
Pierre-Yves Chibon 74ef37
Lubomír Sedlář 4443c5
            output = self.app.post('/test/settings/Pagure requests', data=data)
Pierre-Yves Chibon 74ef37
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Pierre-Yves Chibon 98f2af
                'test project #1        ', output.data)
Lubomír Sedlář 4443c5
            self.assertTrue('

Pagure requests settings

' in output.data)
Pierre-Yves Chibon 74ef37
            self.assertTrue(
Pierre-Yves Chibon 74ef37
                '<input id="active" name="active" type="checkbox" value="y">'
Pierre-Yves Chibon 74ef37
                in output.data)
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            data['csrf_token'] = csrf_token
Pierre-Yves Chibon 74ef37
            # No git found
Clement Verna 380ece
            output = self.app.post('/test/settings/Pagure requests', data=data)
Clement Verna 380ece
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            # Create both the requests repo
Pierre-Yves Chibon 74ef37
            tests.create_projects_git(os.path.join(tests.HERE, 'requests'))
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            # With the git repo
Ghost-script ac44e3
            tests.create_projects_git(tests.HERE)
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Lubomír Sedlář 4443c5
                '/test/settings/Pagure requests', data=data,
Pierre-Yves Chibon 1e4803
                follow_redirects=True)
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Pierre-Yves Chibon 4beb7c
                output.data)
Pierre-Yves Chibon 1e4803
            self.assertTrue(
Lubomír Sedlář 4443c5
                '\n                      Hook Pagure requests inactived'
Pierre-Yves Chibon 1e4803
                in output.data)
Pierre-Yves Chibon 1e4803
Lubomír Sedlář 4443c5
            output = self.app.get('/test/settings/Pagure requests')
Pierre-Yves Chibon 74ef37
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Pierre-Yves Chibon 98f2af
                'test project #1        ', output.data)
Lubomír Sedlář 4443c5
            self.assertTrue('

Pagure requests settings

' in output.data)
Pierre-Yves Chibon 74ef37
            self.assertTrue(
Pierre-Yves Chibon 74ef37
                '<input id="active" name="active" type="checkbox" value="y">'
Pierre-Yves Chibon 74ef37
                in output.data)
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            self.assertFalse(os.path.exists(os.path.join(
Pierre-Yves Chibon 74ef37
                tests.HERE, 'requests', 'test.git', 'hooks',
Pierre-Yves Chibon 74ef37
                'post-receive.pagure')))
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            # Activate hook
Pierre-Yves Chibon 74ef37
            data = {
Pierre-Yves Chibon 74ef37
                'csrf_token': csrf_token,
Pierre-Yves Chibon 74ef37
                'active': 'y',
Pierre-Yves Chibon 74ef37
            }
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Lubomír Sedlář 4443c5
                '/test/settings/Pagure requests', data=data,
Pierre-Yves Chibon 1e4803
                follow_redirects=True)
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Pierre-Yves Chibon 4beb7c
                output.data)
Pierre-Yves Chibon 1e4803
            self.assertTrue(
Lubomír Sedlář 4443c5
                '\n                      Hook Pagure requests activated'
Pierre-Yves Chibon 1e4803
                in output.data)
Pierre-Yves Chibon 1e4803
Lubomír Sedlář 4443c5
            output = self.app.get('/test/settings/Pagure requests')
Pierre-Yves Chibon 74ef37
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Pierre-Yves Chibon 98f2af
                'test project #1        ', output.data)
Lubomír Sedlář 4443c5
            self.assertTrue('

Pagure requests settings

' in output.data)
Pierre-Yves Chibon 74ef37
            self.assertTrue(
Pierre-Yves Chibon 74ef37
                '
Pierre-Yves Chibon 74ef37
                'value="y">' in output.data)
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            self.assertTrue(os.path.exists(os.path.join(
Pierre-Yves Chibon 74ef37
                tests.HERE, 'requests', 'test.git', 'hooks',
Pierre-Yves Chibon 74ef37
                'post-receive.pagure-requests')))
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            # De-Activate hook
Pierre-Yves Chibon 74ef37
            data = {'csrf_token': csrf_token}
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Lubomír Sedlář 4443c5
                '/test/settings/Pagure requests', data=data,
Pierre-Yves Chibon 1e4803
                follow_redirects=True)
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Pierre-Yves Chibon 4beb7c
                output.data)
Pierre-Yves Chibon 1e4803
            self.assertTrue(
Lubomír Sedlář 4443c5
                '\n                      Hook Pagure requests inactived'
Pierre-Yves Chibon 1e4803
                in output.data)
Pierre-Yves Chibon 1e4803
Lubomír Sedlář 4443c5
            output = self.app.get('/test/settings/Pagure requests')
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Pierre-Yves Chibon 98f2af
                'test project #1        ', output.data)
Lubomír Sedlář 4443c5
            self.assertTrue('

Pagure requests settings

' in output.data)
Pierre-Yves Chibon 74ef37
            self.assertTrue(
Pierre-Yves Chibon 74ef37
                '
Pierre-Yves Chibon 74ef37
                'value="y">' in output.data)
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            self.assertFalse(os.path.exists(os.path.join(
Pierre-Yves Chibon 74ef37
                tests.HERE, 'requests', 'test.git', 'hooks',
Pierre-Yves Chibon 74ef37
                'post-receive.pagure-requests')))
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
            # Try re-activate hook w/o the git repo
Pierre-Yves Chibon 74ef37
            data = {
Pierre-Yves Chibon 74ef37
                'csrf_token': csrf_token,
Pierre-Yves Chibon 74ef37
                'active': 'y',
Pierre-Yves Chibon 74ef37
            }
Pierre-Yves Chibon 74ef37
            shutil.rmtree(os.path.join(tests.HERE, 'requests', 'test.git'))
Pierre-Yves Chibon 74ef37
Lubomír Sedlář 4443c5
            output = self.app.post('/test/settings/Pagure requests', data=data)
Pierre-Yves Chibon 74ef37
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
Pierre-Yves Chibon 74ef37
if __name__ == '__main__':
Pierre-Yves Chibon 74ef37
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon 74ef37
        PagureFlaskPluginPagureRequestHooktests)
Pierre-Yves Chibon 74ef37
    unittest.TextTestRunner(verbosity=2).run(SUITE)