Blame tests/test_pagure_flask_ui_plugins.py

Pierre-Yves Chibon 9ec6f9
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
"""
Pierre-Yves Chibon 9fae46
 (c) 2015-2016 - Copyright Red Hat Inc
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
 Authors:
Pierre-Yves Chibon 9ec6f9
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
"""
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 9ec6f9
import pkg_resources
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
import json
Pierre-Yves Chibon 9ec6f9
import unittest
Pierre-Yves Chibon 9ec6f9
import shutil
Pierre-Yves Chibon 9ec6f9
import sys
Pierre-Yves Chibon 9ec6f9
import os
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
import pygit2
Pierre-Yves Chibon 605777
import wtforms
Pierre-Yves Chibon 9ec6f9
from mock import patch
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 9ec6f9
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon fe5017
import pagure.lib
Pierre-Yves Chibon 295cfb
import pagure.lib.plugins
Pierre-Yves Chibon fe5017
import pagure.hooks
Pierre-Yves Chibon 9ec6f9
import tests
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 605777
class FakeForm(wtforms.Form):
Pierre-Yves Chibon 605777
    ''' Form to configure the mail hook. '''
Pierre-Yves Chibon 605777
    field1 = wtforms.TextField(
Pierre-Yves Chibon fe5017
        'Title', [pagure.hooks.RequiredIf('active')]
Pierre-Yves Chibon 605777
    )
Pierre-Yves Chibon 605777
    field2 = wtforms.BooleanField(
Pierre-Yves Chibon 605777
        'Title2', [wtforms.validators.Optional()]
Pierre-Yves Chibon 605777
    )
Pierre-Yves Chibon 605777
Pierre-Yves Chibon 605777
Pierre-Yves Chibon fe5017
class PagureFlaskPluginstests(tests.Modeltests):
Pierre-Yves Chibon fe5017
    """ Tests for flask plugins controller of pagure """
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
    def setUp(self):
Pierre-Yves Chibon 9ec6f9
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon fe5017
        super(PagureFlaskPluginstests, self).setUp()
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon fe5017
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon fe5017
        pagure.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.plugins.SESSION = self.session
Pierre-Yves Chibon 59394d
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon 9fae46
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon 9ec6f9
Jeremy Cline 20109f
        pagure.APP.config['GIT_FOLDER'] = self.path
Pierre-Yves Chibon fe5017
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'tickets')
Pierre-Yves Chibon fe5017
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'docs')
Pierre-Yves Chibon fe5017
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon fe569e
    def test_get_plugin_names(self):
Pierre-Yves Chibon fe569e
        """ Test the get_plugin_names function. """
Pierre-Yves Chibon 295cfb
        names = pagure.lib.plugins.get_plugin_names()
Pierre-Yves Chibon fe569e
        self.assertEqual(
Pierre-Yves Chibon fe569e
            sorted(names),
Pierre-Yves Chibon 46e3b7
            [
Pierre-Yves Chibon 0ce05f
                'Block Un-Signed commits',  'Block non fast-forward pushes',
Farhaan Bukhsh 03a70d
                'Fedmsg', 'IRC', 'Mail', 'Pagure', 'Pagure CI',
Farhaan Bukhsh 03a70d
                'Pagure requests', 'Pagure tickets', 'Read the Doc',
Pierre-Yves Chibon 46e3b7
            ]
Pierre-Yves Chibon 46e3b7
        )
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon fe569e
    def test_get_plugin(self):
Pierre-Yves Chibon fe569e
        """ Test the get_plugin function. """
Pierre-Yves Chibon 295cfb
        name = pagure.lib.plugins.get_plugin('Mail')
Pierre-Yves Chibon fe569e
        self.assertEqual(str(name), "<class 'pagure.hooks.mail.mail'="">")</class>
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon fe569e
    def test_view_plugin_page(self):
Pierre-Yves Chibon fe569e
        """ Test the view_plugin_page endpoint. """
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 3f60e3
        # No Git repo
Pierre-Yves Chibon 9ec6f9
        output = self.app.get('/foo/settings/Mail')
Pierre-Yves Chibon 3f60e3
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
        user = tests.FakeUser()
Pierre-Yves Chibon fe5017
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 9ec6f9
            output = self.app.get('/foo/settings/Mail')
Pierre-Yves Chibon 9ec6f9
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
            tests.create_projects(self.session)
Jeremy Cline 20109f
            tests.create_projects_git(self.path)
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
            output = self.app.get('/test/settings/Mail')
Pierre-Yves Chibon 9ec6f9
            self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 3f60e3
        # User not logged in
Pierre-Yves Chibon 3f60e3
        output = self.app.get('/test/settings/Mail')
Pierre-Yves Chibon 3f60e3
        self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon 3f60e3
Pierre-Yves Chibon fe569e
        user.username = 'pingou'
Pierre-Yves Chibon fe569e
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon fe569e
            output = self.app.get('/test/settings/Mail')
Pierre-Yves Chibon fe569e
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe569e
            self.assertIn(
Pierre-Yves Chibon e954b5
                '<form action="/test/settings/Mail" method="post">',</form>
Pierre-Yves Chibon e954b5
                output.data)
Pierre-Yves Chibon e954b5
            self.assertIn(
Pierre-Yves Chibon e954b5
                '<label for="mail_to">Mail to</label>',
Pierre-Yves Chibon e954b5
                output.data)
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon fe569e
            csrf_token = output.data.split(
Pierre-Yves Chibon fe569e
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon fe569e
            data = {
Pierre-Yves Chibon fe569e
                'active': True,
Pierre-Yves Chibon fe569e
                'mail_to': 'pingou@fp.org',
Pierre-Yves Chibon fe569e
                'csrf_token': csrf_token,
Pierre-Yves Chibon fe569e
            }
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 1e4803
                '/test/settings/Mail', data=data, follow_redirects=True)
Pierre-Yves Chibon fe569e
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe569e
            self.assertIn(
Pierre-Yves Chibon e954b5
                '<section class="settings">\n  

Settings for test

',</section>
Pierre-Yves Chibon 1e4803
                output.data)
Pierre-Yves Chibon fe569e
            self.assertIn(
Pierre-Yves Chibon 03a66a
                '\n                      Hook Mail activated', output.data)
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon fe569e
            data = {
Pierre-Yves Chibon fe569e
                'mail_to': '',
Pierre-Yves Chibon fe569e
                'csrf_token': csrf_token,
Pierre-Yves Chibon fe569e
            }
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 1e4803
                '/test/settings/Mail', data=data, follow_redirects=True)
Pierre-Yves Chibon fe569e
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe569e
            self.assertIn(
Pierre-Yves Chibon e954b5
                '<section class="settings">\n  

Settings for test

',</section>
Pierre-Yves Chibon 1e4803
                output.data)
Pierre-Yves Chibon fe569e
            self.assertIn(
Michael Watters f855b8
                '\n                      Hook Mail deactivated', output.data)
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon 605777
    def test_RequiredIf(self):
Pierre-Yves Chibon 605777
        """ Test the behavior of the RequiredIf validator. """
Pierre-Yves Chibon 605777
        form = FakeForm()
Pierre-Yves Chibon 605777
Pierre-Yves Chibon 605777
        try:
Pierre-Yves Chibon 605777
            form.validate()
Pierre-Yves Chibon fa97f7
        except Exception as err:
Pierre-Yves Chibon 605777
            self.assertEqual(
Pierre-Yves Chibon 605777
                str(err), 'no field named "active" in form')
Pierre-Yves Chibon 605777
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
if __name__ == '__main__':
Pierre-Yves Chibon fe5017
    SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskPluginstests)
Pierre-Yves Chibon 9ec6f9
    unittest.TextTestRunner(verbosity=2).run(SUITE)