Blame tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py

Pierre-Yves Chibon e8d17b
# -*- coding: utf-8 -*-
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
"""
Pierre-Yves Chibon 9fae46
 (c) 2015-2016 - Copyright Red Hat Inc
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
 Authors:
Pierre-Yves Chibon e8d17b
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
"""
Pierre-Yves Chibon e8d17b
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Pierre-Yves Chibon e8d17b
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
import unittest
Pierre-Yves Chibon e8d17b
import shutil
Pierre-Yves Chibon e8d17b
import sys
Pierre-Yves Chibon e8d17b
import os
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon e8d17b
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon fe5017
import pagure.lib
Pierre-Yves Chibon e8d17b
import tests
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
Clement Verna 109c4b
class PagureFlaskPluginPagureTicketHooktests(tests.SimplePagureTest):
Pierre-Yves Chibon fe5017
    """ Tests for pagure_hook plugin of pagure """
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon fe5017
    def test_plugin_pagure_ticket(self):
Pierre-Yves Chibon fe5017
        """ Test the pagure_ticket plugin on/off endpoint. """
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
        tests.create_projects(self.session)
Patrick Uiterwijk 170974
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Lubomír Sedlář 4443c5
            output = self.app.get('/test/settings/Pagure tickets')
Pierre-Yves Chibon e8d17b
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Aurélien Bompard 626417
                'test project #1      ', output_text)
Aurélien Bompard 626417
            self.assertIn('

Pagure tickets settings

', output_text)
Aurélien Bompard 626417
            self.assertIn(
Clement Verna 2c8224
                '
Aurélien Bompard 626417
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon e8d17b
Aurélien Bompard 626417
            csrf_token = output_text.split(
Pierre-Yves Chibon e8d17b
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            data = {}
Pierre-Yves Chibon e8d17b
Lubomír Sedlář 4443c5
            output = self.app.post('/test/settings/Pagure tickets', data=data)
Pierre-Yves Chibon e8d17b
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Aurélien Bompard 626417
                'test project #1      ', output_text)
Aurélien Bompard 626417
            self.assertIn('

Pagure tickets settings

', output_text)
Aurélien Bompard 626417
            self.assertIn(
Clement Verna 2c8224
                '
Aurélien Bompard 626417
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            data['csrf_token'] = csrf_token
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 3f60e3
            # Create the tickets repo
Slavek Kabrda adea20
            tests.create_projects_git(os.path.join(self.path, 'repos', 'tickets'))
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Lubomír Sedlář 4443c5
                '/test/settings/Pagure tickets', data=data,
Pierre-Yves Chibon 1e4803
                follow_redirects=True)
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Aurélien Bompard 626417
                output_text)
Aurélien Bompard 626417
            self.assertIn(
Aurélien Bompard 626417
                '\n                      Hook Pagure tickets deactivated',
Aurélien Bompard 626417
                output_text)
Pierre-Yves Chibon 1e4803
Lubomír Sedlář 4443c5
            output = self.app.get('/test/settings/Pagure tickets')
Pierre-Yves Chibon e8d17b
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Aurélien Bompard 626417
                'test project #1      ', output_text)
Aurélien Bompard 626417
            self.assertIn('

Pagure tickets settings

', output_text)
Aurélien Bompard 626417
            self.assertIn(
Clement Verna 2c8224
                '
Aurélien Bompard 626417
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            self.assertFalse(os.path.exists(os.path.join(
Jeremy Cline 20109f
                self.path, 'tickets', 'test.git', 'hooks',
Pierre-Yves Chibon fe5017
                'post-receive.pagure')))
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            # Activate hook
Pierre-Yves Chibon e8d17b
            data = {
Pierre-Yves Chibon e8d17b
                'csrf_token': csrf_token,
Pierre-Yves Chibon e8d17b
                'active': 'y',
Pierre-Yves Chibon e8d17b
            }
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Lubomír Sedlář 4443c5
                '/test/settings/Pagure tickets', data=data,
Pierre-Yves Chibon 1e4803
                follow_redirects=True)
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Aurélien Bompard 626417
                output_text)
Aurélien Bompard 626417
            self.assertIn(
Aurélien Bompard 626417
                '\n                      Hook Pagure tickets activated',
Aurélien Bompard 626417
                output_text)
Pierre-Yves Chibon 1e4803
Lubomír Sedlář 4443c5
            output = self.app.get('/test/settings/Pagure tickets')
Pierre-Yves Chibon e8d17b
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Aurélien Bompard 626417
                'test project #1      ', output_text)
Aurélien Bompard 626417
            self.assertIn('

Pagure tickets settings

', output_text)
Aurélien Bompard 626417
            self.assertIn(
Clement Verna 2c8224
                '
Aurélien Bompard 626417
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            self.assertTrue(os.path.exists(os.path.join(
Slavek Kabrda adea20
                self.path, 'repos', 'tickets', 'test.git', 'hooks',
Pierre-Yves Chibon 02a5c3
                'post-receive.pagure-ticket')))
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            # De-Activate hook
Pierre-Yves Chibon e8d17b
            data = {'csrf_token': csrf_token}
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Lubomír Sedlář 4443c5
                '/test/settings/Pagure tickets', data=data,
Pierre-Yves Chibon 1e4803
                follow_redirects=True)
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 4beb7c
            self.assertIn(
Pierre-Yves Chibon 4beb7c
                '<section class="settings">\n  

Settings for test

',</section>
Aurélien Bompard 626417
                output_text)
Aurélien Bompard 626417
            self.assertIn(
Aurélien Bompard 626417
                '\n                      Hook Pagure tickets deactivated',
Aurélien Bompard 626417
                output_text)
Pierre-Yves Chibon 1e4803
Lubomír Sedlář 4443c5
            output = self.app.get('/test/settings/Pagure tickets')
Pierre-Yves Chibon 1e4803
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 98f2af
            self.assertIn(
Pierre-Yves Chibon 98f2af
                '
\n'
Aurélien Bompard 626417
                'test project #1      ', output_text)
Aurélien Bompard 626417
            self.assertIn('

Pagure tickets settings

', output_text)
Aurélien Bompard 626417
            self.assertIn(
Clement Verna 2c8224
                '
Aurélien Bompard 626417
                'type="checkbox" value="y">', output_text)
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            self.assertFalse(os.path.exists(os.path.join(
Jeremy Cline 20109f
                self.path, 'tickets', 'test.git', 'hooks',
Pierre-Yves Chibon 02a5c3
                'post-receive.pagure-ticket')))
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 548704
            # Try re-activate hook w/o the git repo
Pierre-Yves Chibon 548704
            data = {
Pierre-Yves Chibon 548704
                'csrf_token': csrf_token,
Pierre-Yves Chibon 548704
                'active': 'y',
Pierre-Yves Chibon 548704
            }
Slavek Kabrda adea20
            shutil.rmtree(os.path.join(self.path, 'repos', 'tickets', 'test.git'))
Pierre-Yves Chibon 548704
Lubomír Sedlář 4443c5
            output = self.app.post('/test/settings/Pagure tickets', data=data)
Pierre-Yves Chibon 548704
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 548704
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)