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
farhaanbukhsh fbda94
import unittest
farhaanbukhsh fbda94
import sys
farhaanbukhsh fbda94
import os
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 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')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, 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'
Pierre-Yves Chibon e30e84
                'test project #1      ', output.data)
farhaanbukhsh fbda94
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_url">URL to the project on the CI '</label>
Clement Verna 5434db
                'service', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_job">Name of the job to trigger'</label>
Clement Verna 5434db
                '', output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_commit" type="checkbox" value="y">', output.data)
Clement Verna 926cdd
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_pr" type="checkbox" value="y">', 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'
Pierre-Yves Chibon e30e84
                'test project #1      ', output.data)
farhaanbukhsh fbda94
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_url">URL to the project on the CI '</label>
Clement Verna 5434db
                'service', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_job">Name of the job to trigger'</label>
Clement Verna 5434db
                '', output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_commit" type="checkbox" value="y">', output.data)
Clement Verna 926cdd
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_pr" type="checkbox" value="y">', output.data)
farhaanbukhsh fbda94
farhaanbukhsh fbda94
            # Activate hook
farhaanbukhsh fbda94
            data = {
Clement Verna 926cdd
                'active_commit': 'y',
Pierre-Yves Chibon 1e2f0d
                'ci_url': 'https://jenkins.fedoraproject.org',
Pierre-Yves Chibon 1e2f0d
                'ci_type': 'jenkins',
Clement Verna 5434db
                'ci_job': 'test/job'
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 e30e84
                'test project #1      ', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_url">URL to the project on the CI '</label>
Clement Verna 5434db
                'service', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_job">Name of the job to trigger'</label>
Clement Verna 5434db
                '', output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_commit" type="checkbox" value="y">', output.data)
Clement Verna 926cdd
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_pr" type="checkbox" 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'
Pierre-Yves Chibon e30e84
                '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'
Pierre-Yves Chibon e30e84
                'test project #1      ', output.data)
farhaanbukhsh fbda94
            self.assertTrue('

Pagure CI settings

' in output.data)
farhaanbukhsh fbda94
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_url">URL to the project on the CI '</label>
Clement Verna 5434db
                'service', output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_job">Name of the job to trigger'</label>
Clement Verna 5434db
                '', output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_commit" type="checkbox" value="y">', 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'
Pierre-Yves Chibon e30e84
                'test project #1      ', output.data)
farhaanbukhsh fbda94
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_url">URL to the project on the CI '</label>
Clement Verna 5434db
                'service', output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_job">Name of the job to trigger'</label>
Clement Verna 5434db
                '', output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_commit" type="checkbox" value="y">', output.data)
Pierre-Yves Chibon 1e2f0d
Pierre-Yves Chibon 1e2f0d
            # Missing the required ci_url
Clement Verna 926cdd
            data = {'csrf_token': csrf_token, 'active_commit': '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 e30e84
                '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(
Clement Verna 5434db
                '<input class="form-control" id="ci_url" name="ci_url" type="text" value="">'
Clement Verna 5434db
                '\nThis field is required.',
Clement Verna 5434db
                output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 5434db
                '<input class="form-control" id="ci_job" name="ci_job" type="text" value="">'
Pierre-Yves Chibon 1e2f0d
                '\nThis field is required.',
farhaanbukhsh fbda94
                output.data)
Pierre-Yves Chibon 1e2f0d
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 926cdd
                'name="active_commit" type="checkbox" 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 b130e5
        with tests.user_set(self.app.application, 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 e30e84
                'namespaced test project      ', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_url">URL to the project on the CI '</label>
Clement Verna 5434db
                'service', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_job">Name of the job to trigger'</label>
Clement Verna 5434db
                '', output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 926cdd
                '
Clement Verna 5434db
                'type="checkbox" value="y">', 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 = {
Clement Verna 926cdd
                'active_pr': 'y',
Pierre-Yves Chibon 7f3b76
                'ci_url': 'https://jenkins.fedoraproject.org',
Clement Verna 5434db
                'ci_job': 'test/job',
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 e30e84
                '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 e30e84
                'namespaced test project      ', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertTrue('

Pagure CI settings

' in output.data)
Pierre-Yves Chibon 7f3b76
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_url">URL to the project on the CI '</label>
Clement Verna 5434db
                'service', output.data)
Clement Verna 5434db
            self.assertIn(
Clement Verna 5434db
                '<label for="ci_job">Name of the job to trigger'</label>
Clement Verna 5434db
                '', output.data)
Pierre-Yves Chibon 7f3b76
            self.assertTrue(
Clement Verna 926cdd
                '
Clement Verna 5434db
                'type="checkbox" value="y">' 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)