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
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 9ec6f9
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 9ec6f9
import unittest
Pierre-Yves Chibon 9ec6f9
import sys
Pierre-Yves Chibon 9ec6f9
import os
Pierre-Yves Chibon 605777
import wtforms
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
Clement Verna 109c4b
class PagureFlaskPluginstests(tests.SimplePagureTest):
Pierre-Yves Chibon fe5017
    """ Tests for flask plugins controller of pagure """
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 c918cd
                'Block Un-Signed commits',
Pierre-Yves Chibon c918cd
                'Block non fast-forward pushes',
Pierre-Yves Chibon c918cd
                'Fedmsg',
Pierre-Yves Chibon c918cd
                'IRC',
Pierre-Yves Chibon c918cd
                'Mail',
Pierre-Yves Chibon c918cd
                'Pagure',
Pierre-Yves Chibon c918cd
                'Pagure CI',
Pierre-Yves Chibon c918cd
                'Pagure requests',
Pierre-Yves Chibon c918cd
                'Pagure tickets',
Pierre-Yves Chibon c918cd
                'Prevent creating new branches by git push',
Pierre-Yves Chibon c918cd
                '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 b130e5
        with tests.user_set(self.app.application, 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)
Patrick Uiterwijk 170974
            tests.create_projects_git(os.path.join(self.path, 'repos'))
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 b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fe569e
            output = self.app.get('/test/settings/Mail')
Pierre-Yves Chibon fe569e
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon fe569e
            self.assertIn(
Pierre-Yves Chibon e954b5
                '<form action="/test/settings/Mail" method="post">',</form>
Aurélien Bompard 626417
                output_text)
Pierre-Yves Chibon e954b5
            self.assertIn(
Clement Verna 2c8224
                '<label for="mail_to">Mail to</label>',
Aurélien Bompard 626417
                output_text)
Pierre-Yves Chibon fe569e
Aurélien Bompard 626417
            csrf_token = output_text.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>
Aurélien Bompard 626417
                output.get_data(as_text=True))
Pierre-Yves Chibon fe569e
            self.assertIn(
Aurélien Bompard 626417
                '\n                      Hook Mail activated', output.get_data(as_text=True))
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>
Aurélien Bompard 626417
                output.get_data(as_text=True))
Pierre-Yves Chibon fe569e
            self.assertIn(
Aurélien Bompard 626417
                '\n                      Hook Mail deactivated', output.get_data(as_text=True))
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 393f31
    unittest.main(verbosity=2)