Blame tests/test_pagure_flask_ui_plugins_pagure_ci.py

farhaanbukhsh fbda94
# -*- coding: utf-8 -*-
farhaanbukhsh fbda94
farhaanbukhsh fbda94
__requires__ = ['SQLAlchemy >= 0.8']
farhaanbukhsh fbda94
import pkg_resources
farhaanbukhsh fbda94
farhaanbukhsh fbda94
import json
farhaanbukhsh fbda94
import unittest
farhaanbukhsh fbda94
import shutil
farhaanbukhsh fbda94
import sys
farhaanbukhsh fbda94
import os
farhaanbukhsh fbda94
farhaanbukhsh fbda94
import pygit2
farhaanbukhsh fbda94
from mock import patch
farhaanbukhsh fbda94
Pierre-Yves Chibon 7f3b76
# Insert the PAGURE_CONFIG env variable before we do the imports
Pierre-Yves Chibon 7f3b76
HERE = os.path.join(os.path.dirname(os.path.abspath(__file__)))
Pierre-Yves Chibon 7f3b76
CONFIG = os.path.join(HERE, 'test_config')
Pierre-Yves Chibon 7f3b76
os.environ['PAGURE_CONFIG'] = CONFIG
Pierre-Yves Chibon 7f3b76
farhaanbukhsh fbda94
sys.path.insert(0, os.path.join(os.path.dirname(
farhaanbukhsh fbda94
    os.path.abspath(__file__)), '..'))
farhaanbukhsh fbda94
farhaanbukhsh fbda94
import pagure.lib
farhaanbukhsh fbda94
import tests
farhaanbukhsh fbda94
farhaanbukhsh fbda94
Clement Verna 109c4b
class PagureFlaskPluginPagureCItests(tests.SimplePagureTest):
farhaanbukhsh fbda94
    """ Tests for flask plugins controller of pagure """
farhaanbukhsh fbda94
farhaanbukhsh fbda94
    def setUp(self):
farhaanbukhsh fbda94
        """ Set up the environnment, ran before every tests. """
farhaanbukhsh fbda94
        super(PagureFlaskPluginPagureCItests, self).setUp()
farhaanbukhsh fbda94
farhaanbukhsh fbda94
        pagure.APP.config['TESTING'] = True
farhaanbukhsh fbda94
        pagure.SESSION = self.session
farhaanbukhsh fbda94
        pagure.ui.SESSION = self.session
farhaanbukhsh fbda94
        pagure.ui.app.SESSION = self.session
farhaanbukhsh fbda94
        pagure.ui.plugins.SESSION = self.session
farhaanbukhsh fbda94
        pagure.ui.repo.SESSION = self.session
farhaanbukhsh fbda94
        pagure.ui.filters.SESSION = self.session
farhaanbukhsh fbda94
farhaanbukhsh fbda94
farhaanbukhsh fbda94
    def test_plugin_pagure_ci(self):
farhaanbukhsh fbda94
        """ Test the pagure ci plugin on/off endpoint. """
farhaanbukhsh fbda94
farhaanbukhsh fbda94
        tests.create_projects(self.session)
Patrick Uiterwijk 170974
        tests.create_projects_git(os.path.join(self.path, 'repos'))
farhaanbukhsh fbda94
farhaanbukhsh fbda94
        user = tests.FakeUser(username='pingou')
farhaanbukhsh fbda94
        with tests.user_set(pagure.APP, user):
farhaanbukhsh fbda94
            output = self.app.get('/test/settings/Pagure CI')
farhaanbukhsh fbda94
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fbda94
            self.assertIn(
farhaanbukhsh fbda94
                '
\n'
farhaanbukhsh fbda94
                'test project #1        ', output.data)
farhaanbukhsh fbda94
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<label for="ci_url">URL to the project on the CI '</label>
Pierre-Yves Chibon 1e2f0d
                'service' , output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<input id="active" name="active" type="checkbox" value="y">',
Pierre-Yves Chibon 1e2f0d
                output.data)
farhaanbukhsh fbda94
farhaanbukhsh fbda94
            csrf_token = output.data.split(
farhaanbukhsh fbda94
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
farhaanbukhsh fbda94
farhaanbukhsh fbda94
            data = {}
farhaanbukhsh fbda94
farhaanbukhsh fbda94
            output = self.app.post('/test/settings/Pagure CI', data=data)
farhaanbukhsh fbda94
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fbda94
            self.assertIn(
farhaanbukhsh fbda94
                '
\n'
farhaanbukhsh fbda94
                'test project #1        ', output.data)
farhaanbukhsh fbda94
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<label for="ci_url">URL to the project on the CI '</label>
Pierre-Yves Chibon 1e2f0d
                'service' , output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<input id="active" name="active" type="checkbox" value="y">',
Pierre-Yves Chibon 1e2f0d
                output.data)
farhaanbukhsh fbda94
farhaanbukhsh fbda94
            # Activate hook
farhaanbukhsh fbda94
            data = {
farhaanbukhsh fbda94
                'active': 'y',
Pierre-Yves Chibon 1e2f0d
                'ci_url': 'https://jenkins.fedoraproject.org',
Pierre-Yves Chibon 1e2f0d
                'ci_type': 'jenkins',
farhaanbukhsh fbda94
            }
Pierre-Yves Chibon 1e2f0d
            # CSRF Token missing
farhaanbukhsh fbda94
            output = self.app.post(
farhaanbukhsh fbda94
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
Pierre-Yves Chibon 1e2f0d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '
\n'
Pierre-Yves Chibon 1e2f0d
                'test project #1        ', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<label for="ci_url">URL to the project on the CI '</label>
Pierre-Yves Chibon 1e2f0d
                'service' , output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '
Pierre-Yves Chibon 1e2f0d
                'value="y">', output.data)
Pierre-Yves Chibon 1e2f0d
Pierre-Yves Chibon 1e2f0d
            data['csrf_token'] = csrf_token
Pierre-Yves Chibon 1e2f0d
Pierre-Yves Chibon 1e2f0d
            # Activate hook
farhaanbukhsh fbda94
            output = self.app.post(
farhaanbukhsh fbda94
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
farhaanbukhsh fbda94
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fbda94
            self.assertIn(
farhaanbukhsh fbda94
                '
\n'
farhaanbukhsh fbda94
                'test project #1        ', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<title>Settings - test - Pagure</title>', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn('

Settings for test

', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '\n                      Hook Pagure CI activated',
Pierre-Yves Chibon 1e2f0d
                output.data)
farhaanbukhsh fbda94
farhaanbukhsh fbda94
            output = self.app.get('/test/settings/Pagure CI')
farhaanbukhsh fbda94
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fbda94
            self.assertIn(
farhaanbukhsh fbda94
                '
\n'
farhaanbukhsh fbda94
                'test project #1        ', output.data)
farhaanbukhsh fbda94
            self.assertTrue('

Pagure CI settings

' in output.data)
farhaanbukhsh fbda94
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<label for="ci_url">URL to the project on the CI '</label>
Pierre-Yves Chibon 1e2f0d
                'service' , output.data)
farhaanbukhsh fbda94
            self.assertTrue(
Pierre-Yves Chibon 1e2f0d
                '<input checked="" id="active" name="active" type="checkbox" value="y">'
farhaanbukhsh fbda94
                in output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '
\nhttps://pagure.org/api/0/ci/jenkins/test/',
Pierre-Yves Chibon 7f3b76
                output.data)
farhaanbukhsh fbda94
Pierre-Yves Chibon 1e2f0d
            # De-activate the hook
farhaanbukhsh fbda94
            data = {
farhaanbukhsh fbda94
                'csrf_token': csrf_token,
farhaanbukhsh fbda94
            }
farhaanbukhsh fbda94
            output = self.app.post(
farhaanbukhsh fbda94
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
farhaanbukhsh fbda94
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fbda94
            self.assertIn(
Michael Watters f855b8
                '\n                      Hook Pagure CI deactivated',
Pierre-Yves Chibon 1e2f0d
                output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
farhaanbukhsh fbda94
                '<section class="settings">\n  

Settings for test

',</section>
farhaanbukhsh fbda94
                output.data)
farhaanbukhsh fbda94
farhaanbukhsh fbda94
            output = self.app.get('/test/settings/Pagure CI')
Pierre-Yves Chibon 1e2f0d
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fbda94
            self.assertIn(
farhaanbukhsh fbda94
                '
\n'
farhaanbukhsh fbda94
                'test project #1        ', output.data)
farhaanbukhsh fbda94
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<label for="ci_url">URL to the project on the CI '</label>
Pierre-Yves Chibon 1e2f0d
                'service' , output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '
Pierre-Yves Chibon 1e2f0d
                'value="y">', output.data)
Pierre-Yves Chibon 1e2f0d
Pierre-Yves Chibon 1e2f0d
            # Missing the required ci_url
Pierre-Yves Chibon 1e2f0d
            data = {'csrf_token': csrf_token, 'active': 'y'}
farhaanbukhsh fbda94
farhaanbukhsh fbda94
            output = self.app.post(
farhaanbukhsh fbda94
                '/test/settings/Pagure CI', data=data, follow_redirects=True)
farhaanbukhsh fbda94
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fbda94
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '
\n'
Pierre-Yves Chibon 1e2f0d
                'test project #1        ', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn('

Pagure CI settings

', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertFalse(
Pierre-Yves Chibon 1e2f0d
                '\n                      Hook activated' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '<input id="ci_url" name="ci_url" type="text" value="">'
Pierre-Yves Chibon 1e2f0d
                '\nThis field is required.',
farhaanbukhsh fbda94
                output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Pierre-Yves Chibon 1e2f0d
                '
Pierre-Yves Chibon 1e2f0d
                'value="y">', output.data)
farhaanbukhsh fbda94
Pierre-Yves Chibon 7f3b76
    def test_plugin_pagure_ci_namespaced(self):
Pierre-Yves Chibon 7f3b76
        """ Test the pagure ci plugin on/off endpoint. """
Pierre-Yves Chibon 7f3b76
Pierre-Yves Chibon 7f3b76
        tests.create_projects(self.session)
Patrick Uiterwijk 170974
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Pierre-Yves Chibon 7f3b76
Pierre-Yves Chibon 7f3b76
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 7f3b76
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 7f3b76
            output = self.app.get('/somenamespace/test3/settings/Pagure CI')
Pierre-Yves Chibon 7f3b76
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '
\n'
Pierre-Yves Chibon 7f3b76
                'namespaced test project        ', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '<label for="ci_url">URL to the project on the CI '</label>
Pierre-Yves Chibon 7f3b76
                'service' , output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '<input id="active" name="active" type="checkbox" value="y">',
Pierre-Yves Chibon 7f3b76
                output.data)
Pierre-Yves Chibon 7f3b76
Pierre-Yves Chibon 7f3b76
            csrf_token = output.data.split(
Pierre-Yves Chibon 7f3b76
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 7f3b76
Pierre-Yves Chibon 7f3b76
            # Activate hook
Pierre-Yves Chibon 7f3b76
            data = {
Pierre-Yves Chibon 7f3b76
                'active': 'y',
Pierre-Yves Chibon 7f3b76
                'ci_url': 'https://jenkins.fedoraproject.org',
Pierre-Yves Chibon 7f3b76
                'ci_type': 'jenkins',
Pierre-Yves Chibon 7f3b76
                'csrf_token': csrf_token,
Pierre-Yves Chibon 7f3b76
            }
Pierre-Yves Chibon 7f3b76
Pierre-Yves Chibon 7f3b76
            # Activate hook
Pierre-Yves Chibon 7f3b76
            output = self.app.post(
Pierre-Yves Chibon 7f3b76
                '/somenamespace/test3/settings/Pagure CI', data=data, follow_redirects=True)
Pierre-Yves Chibon 7f3b76
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '
\n'
Pierre-Yves Chibon 7f3b76
                'namespaced test project        ', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '<title>Settings - somenamespace/test3 - Pagure</title>', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn('

Settings for somenamespace/test3

', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '\n                      Hook Pagure CI activated',
Pierre-Yves Chibon 7f3b76
                output.data)
Pierre-Yves Chibon 7f3b76
Pierre-Yves Chibon 7f3b76
            output = self.app.get('/somenamespace/test3/settings/Pagure CI')
Pierre-Yves Chibon 7f3b76
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '
\n'
Pierre-Yves Chibon 7f3b76
                'namespaced test project        ', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '<label for="ci_url">URL to the project on the CI '</label>
Pierre-Yves Chibon 7f3b76
                'service' , output.data)
Pierre-Yves Chibon 7f3b76
            self.assertTrue(
Pierre-Yves Chibon 7f3b76
                '<input checked="" id="active" name="active" type="checkbox" value="y">'
Pierre-Yves Chibon 7f3b76
                in output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Pierre-Yves Chibon 7f3b76
                '
\nhttps://pagure.org/api/0/ci/jenkins/somenamespace/test3/',
Pierre-Yves Chibon 7f3b76
                output.data)
Pierre-Yves Chibon 7f3b76
farhaanbukhsh fbda94
farhaanbukhsh fbda94
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)