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 9071b6
 (c) 2015-2018 - 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
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
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 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 e8d17b
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)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            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 73d120
                "<title>Settings Pagure tickets - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Aurélien Bompard 626417
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon e8d17b
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 e8d17b
Pierre-Yves Chibon e8d17b
            data = {}
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 73d120
            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 73d120
                "<title>Settings Pagure tickets - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Aurélien Bompard 626417
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 73d120
            data["csrf_token"] = csrf_token
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 3f60e3
            # Create the tickets repo
Pierre-Yves Chibon 73d120
            tests.create_projects_git(
Pierre-Yves Chibon 73d120
                os.path.join(self.path, "repos", "tickets")
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Pagure tickets",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
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 9071b6
                '
'
Pierre-Yves Chibon 73d120
                "Project Settings\n",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("Hook Pagure tickets deactivated", output_text)
Pierre-Yves Chibon 1e4803
Pierre-Yves Chibon 73d120
            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 73d120
                "<title>Settings Pagure tickets - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Aurélien Bompard 626417
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
            self.assertFalse(
Pierre-Yves Chibon 73d120
                os.path.exists(
Pierre-Yves Chibon 73d120
                    os.path.join(
Pierre-Yves Chibon 73d120
                        self.path,
Pierre-Yves Chibon 73d120
                        "tickets",
Pierre-Yves Chibon 73d120
                        "test.git",
Pierre-Yves Chibon 73d120
                        "hooks",
Pierre-Yves Chibon 73d120
                        "post-receive.pagure",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            # Activate hook
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token, "active": "y"}
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 73d120
            print("    ==== ACTIVATE ")
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Pagure tickets",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
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 9071b6
                '
'
Pierre-Yves Chibon 73d120
                "Project Settings\n",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("Hook Pagure tickets activated", output_text)
Pierre-Yves Chibon 1e4803
Pierre-Yves Chibon 73d120
            print("    ==== CHECK ")
Pierre-Yves Chibon 73d120
            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 73d120
                "<title>Settings Pagure tickets - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Aurélien Bompard 626417
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon e8d17b
            # De-Activate hook
Pierre-Yves Chibon 73d120
            print("    ==== DEACTIVATE ")
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 1e4803
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Pagure tickets",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
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 9071b6
                '
'
Pierre-Yves Chibon 73d120
                "Project Settings\n",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("Hook Pagure tickets deactivated", output_text)
Pierre-Yves Chibon 1e4803
Pierre-Yves Chibon 73d120
            print("    ==== CHECK ")
Pierre-Yves Chibon 73d120
            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 73d120
                "<title>Settings Pagure tickets - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Aurélien Bompard 626417
            self.assertIn(
Ryan Lerch 014c1c
                '
Pierre-Yves Chibon 73d120
                'type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
            self.assertFalse(
Pierre-Yves Chibon 73d120
                os.path.exists(
Pierre-Yves Chibon 73d120
                    os.path.join(
Pierre-Yves Chibon 73d120
                        self.path,
Pierre-Yves Chibon 73d120
                        "tickets",
Pierre-Yves Chibon 73d120
                        "test.git",
Pierre-Yves Chibon 73d120
                        "hooks",
Pierre-Yves Chibon 73d120
                        "post-receive.pagure-ticket",
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 548704
            # Try re-activate hook w/o the git repo
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token, "active": "y"}
Pierre-Yves Chibon 73d120
            shutil.rmtree(
Pierre-Yves Chibon 73d120
                os.path.join(self.path, "repos", "tickets", "test.git")
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
            print("    ==== NO GIT REPO ")
Pierre-Yves Chibon 73d120
            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 73d120
        print("*** DONE ***")
Pierre-Yves Chibon 930073
Pierre-Yves Chibon e8d17b
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)