Blame tests/test_pagure_flask_ui_issues_templates.py

Pierre-Yves Chibon 75b902
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
"""
Pierre-Yves Chibon 75b902
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
 Authors:
Pierre-Yves Chibon 75b902
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
"""
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 75b902
import json
Pierre-Yves Chibon 75b902
import unittest
Pierre-Yves Chibon 75b902
import sys
Pierre-Yves Chibon 75b902
import os
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
import pygit2
Pierre-Yves Chibon 75b902
from mock import patch, MagicMock
Pierre-Yves Chibon 75b902
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 75b902
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 75b902
import tests
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
def create_templates(repopath):
Pierre-Yves Chibon 75b902
    """ Create a couple of templates at the specified repo.
Pierre-Yves Chibon 75b902
    """
Pierre-Yves Chibon 75b902
    clone_repo = pygit2.Repository(repopath)
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    # Create the RFE template
Pierre-Yves Chibon 73d120
    os.mkdir(os.path.join(repopath, "templates"))
Pierre-Yves Chibon 73d120
    template = os.path.join(repopath, "templates", "RFE.md")
Pierre-Yves Chibon 73d120
    with open(template, "w") as stream:
Pierre-Yves Chibon 73d120
        stream.write("RFE\n###\n\n* Idea description")
Pierre-Yves Chibon 73d120
    clone_repo.index.add(os.path.join("templates", "RFE.md"))
Pierre-Yves Chibon 75b902
    clone_repo.index.write()
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    # Commit
Pierre-Yves Chibon 75b902
    tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
    author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
    committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 75b902
    commit = clone_repo.create_commit(
Pierre-Yves Chibon 73d120
        "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 75b902
        author,
Pierre-Yves Chibon 75b902
        committer,
Pierre-Yves Chibon 73d120
        "Add a RFE template",
Pierre-Yves Chibon 75b902
        # binary string representing the tree object ID
Pierre-Yves Chibon 75b902
        tree,
Pierre-Yves Chibon 75b902
        # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
        [],
Pierre-Yves Chibon 75b902
    )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    # Create the 2018-bid.md template
Pierre-Yves Chibon 73d120
    template = os.path.join(repopath, "templates", "2018-bid.md")
Pierre-Yves Chibon 73d120
    with open(template, "w") as stream:
Pierre-Yves Chibon 73d120
        stream.write("Bid for 2018\n############\n\n* Location:")
Pierre-Yves Chibon 73d120
    clone_repo.index.add(os.path.join("templates", "2018-bid.md"))
Pierre-Yves Chibon 75b902
    clone_repo.index.write()
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    # Commit
Pierre-Yves Chibon 75b902
    tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
    author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
    committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 164e67
    commit = clone_repo.create_commit(
Pierre-Yves Chibon 73d120
        "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 75b902
        author,
Pierre-Yves Chibon 75b902
        committer,
Pierre-Yves Chibon 73d120
        "Add a RFE template",
Pierre-Yves Chibon 75b902
        # binary string representing the tree object ID
Pierre-Yves Chibon 75b902
        tree,
Pierre-Yves Chibon 75b902
        # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
        [commit.hex],
Pierre-Yves Chibon 75b902
    )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 164e67
    # Create the default.md template
Pierre-Yves Chibon 73d120
    template = os.path.join(repopath, "templates", "default.md")
Pierre-Yves Chibon 73d120
    with open(template, "w") as stream:
Pierre-Yves Chibon 73d120
        stream.write("Report your issue")
Pierre-Yves Chibon 73d120
    clone_repo.index.add(os.path.join("templates", "default.md"))
Pierre-Yves Chibon 164e67
    clone_repo.index.write()
Pierre-Yves Chibon 164e67
Pierre-Yves Chibon 164e67
    # Commit
Pierre-Yves Chibon 164e67
    tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
    author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
    committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 164e67
    clone_repo.create_commit(
Pierre-Yves Chibon 73d120
        "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 164e67
        author,
Pierre-Yves Chibon 164e67
        committer,
Pierre-Yves Chibon 73d120
        "Add a default template",
Pierre-Yves Chibon 164e67
        # binary string representing the tree object ID
Pierre-Yves Chibon 164e67
        tree,
Pierre-Yves Chibon 164e67
        # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
        [commit.hex],
Pierre-Yves Chibon 164e67
    )
Pierre-Yves Chibon 164e67
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 689da1
class PagureFlaskIssuesTemplatetests(tests.Modeltests):
Pierre-Yves Chibon 75b902
    """ Tests for flask issues controller of pagure """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 75b902
    def setUp(self):
Pierre-Yves Chibon 75b902
        """ Set up the environnment, run before every tests. """
Pierre-Yves Chibon 689da1
        super(PagureFlaskIssuesTemplatetests, self).setUp()
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 73d120
        pagure.config.config["TICKETS_FOLDER"] = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "tickets"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "tickets"))
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        # Add a couple of templates to test2
Pierre-Yves Chibon 73d120
        repopath = os.path.join(self.path, "tickets", "test2.git")
Pierre-Yves Chibon 75b902
        create_templates(repopath)
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        # Add a couple of templates to somenamespace/test3
Pierre-Yves Chibon 75b902
        repopath = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "tickets", "somenamespace", "test3.git"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 75b902
        create_templates(repopath)
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    def test_new_issue_no_template(self):
Pierre-Yves Chibon 75b902
        """ Test the new_issue endpoint when the project has no templates.
Pierre-Yves Chibon 75b902
        """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/new_issue")
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 75b902
            self.assertIn(
Ryan Lerch 7230c1
                '

New Issue

\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertNotIn("Issue Templates", output_text)
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    def test_new_issue_w_template(self):
Pierre-Yves Chibon 75b902
        """ Test the new_issue endpoint when the project has templates. """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test2/new_issue")
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 75b902
            self.assertIn(
Ryan Lerch 7230c1
                '

New Issue

\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("Issue Templates", output_text)
Pierre-Yves Chibon 75b902
            self.assertIn(
Pierre-Yves Chibon aede19
                'RFE',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 164e67
            self.assertIn(
Pierre-Yves Chibon aede19
                '2018-bid',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 75b902
            self.assertIn(
Pierre-Yves Chibon aede19
                'default',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 322961
            self.assertIn(
Lenka Segura 06fb20
                'placeholder="Enter your comment here" tabindex=2 required>'
Pierre-Yves Chibon 73d120
                "Report your issue",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 322961
Pierre-Yves Chibon 322961
    def test_new_issue_w_specific_template(self):
Pierre-Yves Chibon 322961
        """ Test the new_issue endpoint when the project has templates. """
Pierre-Yves Chibon 322961
Pierre-Yves Chibon 322961
        user = tests.FakeUser()
Pierre-Yves Chibon 322961
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test2/new_issue?template=2018-bid")
Pierre-Yves Chibon 322961
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 322961
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 322961
            self.assertIn(
Pierre-Yves Chibon 322961
                '

New Issue

\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("Issue Templates", output_text)
Pierre-Yves Chibon 322961
            self.assertIn(
Pierre-Yves Chibon aede19
                'RFE',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 322961
            self.assertIn(
Pierre-Yves Chibon aede19
                '2018-bid',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 322961
            self.assertIn(
Pierre-Yves Chibon aede19
                'default',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 322961
            self.assertIn(
Lenka Segura 06fb20
                'placeholder="Enter your comment here" tabindex=2 required>'
Pierre-Yves Chibon 73d120
                "Bid for 2018\n############",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    def test_get_ticket_template_no_csrf(self):
Pierre-Yves Chibon 75b902
        """ Test the get_ticket_template endpoint when the project has no
Pierre-Yves Chibon 75b902
        templates.
Pierre-Yves Chibon 75b902
        """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/test/issue/template")
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 73d120
                data, {"code": "ERROR", "message": "Invalid input submitted"}
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    def test_get_ticket_template_no_template_specified(self):
Pierre-Yves Chibon 75b902
        """ Test the get_ticket_template endpoint when not specifying which
Pierre-Yves Chibon 75b902
        template to get.
Pierre-Yves Chibon 75b902
        """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf}
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/test/issue/template", data=data)
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 73d120
                data, {"code": "ERROR", "message": "No template provided"}
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    def test_get_ticket_template_no_project(self):
Pierre-Yves Chibon 689da1
        """ Test the get_ticket_template endpoint when the project does not
Pierre-Yves Chibon 689da1
        exist.
Pierre-Yves Chibon 75b902
        """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf}
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/foobar/issue/template", data=data)
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    def test_get_ticket_template_no_template(self):
Pierre-Yves Chibon 75b902
        """ Test the get_ticket_template endpoint when the project has no
Pierre-Yves Chibon 75b902
        templates.
Pierre-Yves Chibon 75b902
        """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf}
Pierre-Yves Chibon 75b902
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/pv/test/issue/template?template=RFE", data=data
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 73d120
                data, {"code": "ERROR", "message": "No such template found"}
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 689da1
    def test_get_ticket_template_issue_tracker_disabled(self):
Pierre-Yves Chibon 689da1
        """ Test the get_ticket_template endpoint when the project has
Pierre-Yves Chibon 689da1
        disabled its issue tracker.
Pierre-Yves Chibon 689da1
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 689da1
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["issue_tracker"] = False
Pierre-Yves Chibon 689da1
        repo.settings = settings
Pierre-Yves Chibon 689da1
        self.session.add(repo)
Pierre-Yves Chibon 689da1
        self.session.commit()
Pierre-Yves Chibon 689da1
Pierre-Yves Chibon 689da1
        user = tests.FakeUser()
Pierre-Yves Chibon 689da1
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 689da1
            csrf = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf}
Pierre-Yves Chibon 689da1
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/pv/test/issue/template?template=RFE", data=data
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 689da1
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 689da1
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 689da1
            self.assertEqual(
Pierre-Yves Chibon 689da1
                data,
Pierre-Yves Chibon 689da1
                {
Pierre-Yves Chibon 73d120
                    "code": "ERROR",
Pierre-Yves Chibon 73d120
                    "message": "No issue tracker found for this project",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 689da1
            )
Pierre-Yves Chibon 689da1
Pierre-Yves Chibon 75b902
    def test_get_ticket_template_w_template(self):
Pierre-Yves Chibon 75b902
        """ Test the get_ticket_template endpoint when the project has
Pierre-Yves Chibon 75b902
        templates.
Pierre-Yves Chibon 75b902
        """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf}
Pierre-Yves Chibon 75b902
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/pv/test2/issue/template?template=RFE", data=data
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 75b902
                data,
Pierre-Yves Chibon 73d120
                {"code": "OK", "message": "RFE\n###\n\n* Idea description"},
Pierre-Yves Chibon 75b902
            )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    def test_get_ticket_template_w_template_namespace(self):
Pierre-Yves Chibon 75b902
        """ Test the get_ticket_template endpoint when the project has
Pierre-Yves Chibon 75b902
        templates and a namespace.
Pierre-Yves Chibon 75b902
        """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf}
Pierre-Yves Chibon 75b902
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/pv/somenamespace/test3/issue/template?template=RFE",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 75b902
                data,
Pierre-Yves Chibon 73d120
                {"code": "OK", "message": "RFE\n###\n\n* Idea description"},
Pierre-Yves Chibon 75b902
            )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 75b902
    unittest.main(verbosity=2)