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 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
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 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 9ec6f9
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 73d120
    """ Form to configure the mail hook. """
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    field1 = wtforms.StringField("Title", [pagure.hooks.RequiredIf("active")])
Pierre-Yves Chibon 73d120
    field2 = wtforms.BooleanField("Title2", [wtforms.validators.Optional()])
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 73d120
                "Block Un-Signed commits",
Pierre-Yves Chibon 73d120
                "Block non fast-forward pushes",
Pierre-Yves Chibon 73d120
                "Fedmsg",
Pierre-Yves Chibon 73d120
                "IRC",
Pierre-Yves Chibon 73d120
                "Mail",
Pierre-Yves Chibon 73d120
                "Mirroring",
Pierre-Yves Chibon 73d120
                "Pagure",
Pierre-Yves Chibon 73d120
                "Pagure CI",
Pierre-Yves Chibon 73d120
                "Pagure requests",
Pierre-Yves Chibon 73d120
                "Pagure tickets",
Pierre-Yves Chibon 73d120
                "Prevent creating new branches by git push",
Pierre-Yves Chibon 73d120
                "Read the Doc",
Pierre-Yves Chibon 73d120
            ],
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 73d120
        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 73d120
        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 73d120
            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)
Pierre-Yves Chibon 73d120
            tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 73d120
            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 73d120
        output = self.app.get("/test/settings/Mail")
Pierre-Yves Chibon 3f60e3
        self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon 3f60e3
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            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>
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn('<label for="mail_to">Mail to</label>', output_text)
Pierre-Yves Chibon fe569e
Aurélien Bompard 626417
            csrf_token = output_text.split(
Pierre-Yves Chibon 73d120
                'name="csrf_token" type="hidden" value="'
Pierre-Yves Chibon 73d120
            )[1].split('">')[0]
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon fe569e
            data = {
Pierre-Yves Chibon 73d120
                "active": True,
Pierre-Yves Chibon 73d120
                "mail_to": "pingou@fp.org",
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon fe569e
            }
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Mail", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fe569e
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 77bdcd
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon fe569e
            self.assertIn(
Pierre-Yves Chibon 77bdcd
                '
'
Pierre-Yves Chibon 73d120
                "Project Settings\n",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("Hook Mail activated", output_text)
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon 73d120
            data = {"mail_to": "", "csrf_token": csrf_token}
Pierre-Yves Chibon fe569e
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Mail", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fe569e
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 77bdcd
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon fe569e
            self.assertIn(
Pierre-Yves Chibon 77bdcd
                '
'
Pierre-Yves Chibon 73d120
                "Project Settings\n",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("Hook Mail deactivated", output_text)
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 73d120
            self.assertEqual(str(err), 'no field named "active" in form')
Pierre-Yves Chibon 605777
Pierre-Yves Chibon 9ec6f9
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)