test_pagure_flask_ui_plugins_pagure_request_hook.py 7.14 KB
Newer Older
1 2 3
# -*- coding: utf-8 -*-

"""
4
 (c) 2015-2016 - Copyright Red Hat Inc
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

 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
42
        pagure.ui.repo.SESSION = self.session
43
        pagure.ui.filters.SESSION = self.session
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

        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):
Lubomír Sedlář's avatar
Lubomír Sedlář committed
61
            output = self.app.get('/test/settings/Pagure requests')
62
            self.assertEqual(output.status_code, 200)
63 64 65
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
Lubomír Sedlář's avatar
Lubomír Sedlář committed
66
            self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
67 68 69 70 71 72 73 74 75
            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 = {}

Lubomír Sedlář's avatar
Lubomír Sedlář committed
76
            output = self.app.post('/test/settings/Pagure requests', data=data)
77
            self.assertEqual(output.status_code, 200)
78 79 80
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
Lubomír Sedlář's avatar
Lubomír Sedlář committed
81
            self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
82 83 84 85 86 87
            self.assertTrue(
                '<input id="active" name="active" type="checkbox" value="y">'
                in output.data)

            data['csrf_token'] = csrf_token
            # No git found
88 89
            output = self.app.post('/test/settings/Pagure requests', data=data)
            self.assertEqual(output.status_code, 404)
90 91 92 93 94

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

            # With the git repo
95
            tests.create_projects_git(tests.HERE)
96
            output = self.app.post(
Lubomír Sedlář's avatar
Lubomír Sedlář committed
97
                '/test/settings/Pagure requests', data=data,
98 99
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
100 101 102
            self.assertIn(
                '<section class="settings">\n  <h3>Settings for test</h3>',
                output.data)
103
            self.assertTrue(
Lubomír Sedlář's avatar
Lubomír Sedlář committed
104
                '</button>\n                      Hook Pagure requests inactived'
105 106
                in output.data)

Lubomír Sedlář's avatar
Lubomír Sedlář committed
107
            output = self.app.get('/test/settings/Pagure requests')
108
            self.assertEqual(output.status_code, 200)
109 110 111
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
Lubomír Sedlář's avatar
Lubomír Sedlář committed
112
            self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
113 114 115 116 117 118 119 120 121 122 123 124 125 126
            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',
            }

127
            output = self.app.post(
Lubomír Sedlář's avatar
Lubomír Sedlář committed
128
                '/test/settings/Pagure requests', data=data,
129 130
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
131 132 133
            self.assertIn(
                '<section class="settings">\n  <h3>Settings for test</h3>',
                output.data)
134
            self.assertTrue(
Lubomír Sedlář's avatar
Lubomír Sedlář committed
135
                '</button>\n                      Hook Pagure requests activated'
136 137
                in output.data)

Lubomír Sedlář's avatar
Lubomír Sedlář committed
138
            output = self.app.get('/test/settings/Pagure requests')
139
            self.assertEqual(output.status_code, 200)
140 141 142
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
Lubomír Sedlář's avatar
Lubomír Sedlář committed
143
            self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
144 145 146 147 148 149 150 151 152 153
            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}
154
            output = self.app.post(
Lubomír Sedlář's avatar
Lubomír Sedlář committed
155
                '/test/settings/Pagure requests', data=data,
156 157
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
158 159 160
            self.assertIn(
                '<section class="settings">\n  <h3>Settings for test</h3>',
                output.data)
161
            self.assertTrue(
Lubomír Sedlář's avatar
Lubomír Sedlář committed
162
                '</button>\n                      Hook Pagure requests inactived'
163 164
                in output.data)

Lubomír Sedlář's avatar
Lubomír Sedlář committed
165
            output = self.app.get('/test/settings/Pagure requests')
166
            self.assertEqual(output.status_code, 200)
167 168 169
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
Lubomír Sedlář's avatar
Lubomír Sedlář committed
170
            self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
            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'))

Lubomír Sedlář's avatar
Lubomír Sedlář committed
186
            output = self.app.post('/test/settings/Pagure requests', data=data)
187 188 189 190 191 192 193
            self.assertEqual(output.status_code, 404)


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