test_pagure_flask_ui_plugins_fedmsg.py 6.4 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 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
42
        pagure.ui.repo.SESSION = self.session
43
        pagure.ui.filters.SESSION = self.session
44 45 46 47 48 49 50 51 52 53

        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()

54
    def test_plugin_fedmsg(self):
55
        """ Test the fedmsg plugin on/off endpoint. """
56 57 58 59 60 61 62

        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)
63 64 65
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
66
            self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
67 68 69 70 71 72 73 74 75 76 77
            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)
78 79 80
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
81
            self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            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
97 98
            output = self.app.post(
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
99
            self.assertEqual(output.status_code, 200)
100
            self.assertIn(
101 102 103 104
                '<section class="settings">\n  <h3>Settings for test</h3>',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Fedmsg inactived',
105 106
                output.data)
            output = self.app.get('/test/settings/Fedmsg', data=data)
107 108 109
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
110
            self.assertIn('<h3>Fedmsg settings</h3>', output.data)
111 112 113
            self.assertIn(
                '<input id="active" name="active" type="checkbox" value="y">',
                output.data)
114 115 116 117 118 119 120 121 122 123

            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',
            }

124 125 126 127
            output = self.app.post(
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
128 129 130 131
                '<section class="settings">\n  <h3>Settings for test</h3>',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Fedmsg activated',
132 133 134
                output.data)
            output = self.app.get('/test/settings/Fedmsg', data=data)
            self.assertEqual(output.status_code, 200)
135 136 137
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
138
            self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
139 140 141 142 143 144 145 146 147
            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}
148 149 150 151
            output = self.app.post(
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertIn(
152 153 154 155
                '<section class="settings">\n  <h3>Settings for test</h3>',
                output.data)
            self.assertIn(
                '</button>\n                      Hook Fedmsg inactived',
156 157 158
                output.data)
            output = self.app.get('/test/settings/Fedmsg', data=data)
            self.assertEqual(output.status_code, 200)
159 160 161
            self.assertIn(
                '<div class="projectinfo m-t-1 m-b-1">\n'
                'test project #1        </div>', output.data)
162
            self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
163 164 165 166 167 168 169 170 171 172 173 174
            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)