Blame tests/test_pagure_flask_ui_plugins_fedmsg.py

Pierre-Yves Chibon 1348de
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
"""
Pierre-Yves Chibon 9fae46
 (c) 2015-2016 - Copyright Red Hat Inc
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
 Authors:
Pierre-Yves Chibon 1348de
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
"""
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 1348de
import pkg_resources
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
import json
Pierre-Yves Chibon 1348de
import unittest
Pierre-Yves Chibon 1348de
import shutil
Pierre-Yves Chibon 1348de
import sys
Pierre-Yves Chibon 1348de
import os
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
import pygit2
Pierre-Yves Chibon 1348de
from mock import patch
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 1348de
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
import pagure.lib
Pierre-Yves Chibon 1348de
import tests
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
class PagureFlaskPluginFedmsgtests(tests.Modeltests):
Pierre-Yves Chibon 1348de
    """ Tests for fedmsg plugin of pagure """
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
    def setUp(self):
Pierre-Yves Chibon 1348de
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 1348de
        super(PagureFlaskPluginFedmsgtests, self).setUp()
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 1348de
        pagure.SESSION = self.session
Pierre-Yves Chibon 1348de
        pagure.ui.SESSION = self.session
Pierre-Yves Chibon 1348de
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon 1348de
        pagure.ui.plugins.SESSION = self.session
Pierre-Yves Chibon f834f2
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon 9fae46
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon 1348de
Jeremy Cline 20109f
        pagure.APP.config['GIT_FOLDER'] = self.path
Pierre-Yves Chibon 1348de
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'forks')
Pierre-Yves Chibon 1348de
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'tickets')
Pierre-Yves Chibon 1348de
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'docs')
Pierre-Yves Chibon 1348de
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 7f1275
    def test_plugin_fedmsg(self):
Pierre-Yves Chibon f834f2
        """ Test the fedmsg plugin on/off endpoint. """
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
        tests.create_projects(self.session)
Jeremy Cline 20109f
        tests.create_projects_git(self.path)
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 1348de
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 1348de
            output = self.app.get('/test/settings/Fedmsg')
Pierre-Yves Chibon 1348de
            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)
Pierre-Yves Chibon 4beb7c
            self.assertTrue('

Fedmsg settings

' in output.data)
Pierre-Yves Chibon 1348de
            self.assertTrue(
Pierre-Yves Chibon 1348de
                '<input id="active" name="active" type="checkbox" value="y">'
Pierre-Yves Chibon 1348de
                in output.data)
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            csrf_token = output.data.split(
Pierre-Yves Chibon 1348de
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            data = {}
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            output = self.app.post('/test/settings/Fedmsg', data=data)
Pierre-Yves Chibon 1348de
            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)
Pierre-Yves Chibon 4beb7c
            self.assertTrue('

Fedmsg settings

' in output.data)
Pierre-Yves Chibon 1348de
            self.assertTrue(
Pierre-Yves Chibon 1348de
                '<input id="active" name="active" type="checkbox" value="y">'
Pierre-Yves Chibon 1348de
                in output.data)
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            self.assertFalse(os.path.exists(os.path.join(
Jeremy Cline 20109f
                self.path, 'test.git', 'hooks', 'post-receive.fedmsg')))
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            data['csrf_token'] = csrf_token
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            # With the git repo
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 1e4803
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
Pierre-Yves Chibon 1348de
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1e4803
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Pierre-Yves Chibon 4beb7c
                output.data)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '\n                      Hook Fedmsg inactived',
Pierre-Yves Chibon 1e4803
                output.data)
Pierre-Yves Chibon 1e4803
            output = self.app.get('/test/settings/Fedmsg', data=data)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Pierre-Yves Chibon 98f2af
                'test project #1        ', output.data)
Pierre-Yves Chibon 4beb7c
            self.assertIn('

Fedmsg settings

', output.data)
Pierre-Yves Chibon 1e4803
            self.assertIn(
Pierre-Yves Chibon 1e4803
                '<input id="active" name="active" type="checkbox" value="y">',
Pierre-Yves Chibon 1e4803
                output.data)
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            self.assertFalse(os.path.exists(os.path.join(
Jeremy Cline 20109f
                self.path, 'test.git', 'hooks', 'post-receive.fedmsg')))
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            # Activate hook
Pierre-Yves Chibon 1348de
            data = {
Pierre-Yves Chibon 1348de
                'csrf_token': csrf_token,
Pierre-Yves Chibon 1348de
                'active': 'y',
Pierre-Yves Chibon 1348de
            }
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 1e4803
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1e4803
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Pierre-Yves Chibon 4beb7c
                output.data)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '\n                      Hook Fedmsg activated',
Pierre-Yves Chibon 1e4803
                output.data)
Pierre-Yves Chibon 1e4803
            output = self.app.get('/test/settings/Fedmsg', data=data)
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)
Pierre-Yves Chibon 4beb7c
            self.assertTrue('

Fedmsg settings

' in output.data)
Pierre-Yves Chibon 1348de
            self.assertTrue(
Pierre-Yves Chibon 1348de
                '
Pierre-Yves Chibon 1348de
                'value="y">' in output.data)
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            self.assertTrue(os.path.exists(os.path.join(
Jeremy Cline 20109f
                self.path, 'test.git', 'hooks', 'post-receive.fedmsg')))
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            # De-Activate hook
Pierre-Yves Chibon 1348de
            data = {'csrf_token': csrf_token}
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 1e4803
                '/test/settings/Fedmsg', data=data, follow_redirects=True)
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1e4803
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Pierre-Yves Chibon 4beb7c
                output.data)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '\n                      Hook Fedmsg inactived',
Pierre-Yves Chibon 1e4803
                output.data)
Pierre-Yves Chibon 1e4803
            output = self.app.get('/test/settings/Fedmsg', data=data)
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)
Pierre-Yves Chibon 4beb7c
            self.assertTrue('

Fedmsg settings

' in output.data)
Pierre-Yves Chibon 1348de
            self.assertTrue(
Pierre-Yves Chibon 1348de
                '
Pierre-Yves Chibon 1348de
                'value="y">' in output.data)
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
            self.assertFalse(os.path.exists(os.path.join(
Jeremy Cline 20109f
                self.path, 'test.git', 'hooks', 'post-receive.fedmsg')))
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
Pierre-Yves Chibon 1348de
if __name__ == '__main__':
Pierre-Yves Chibon 1348de
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon 1348de
        PagureFlaskPluginFedmsgtests)
Pierre-Yves Chibon 1348de
    unittest.TextTestRunner(verbosity=2).run(SUITE)