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 75b902
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 75b902
import pkg_resources
Pierre-Yves Chibon 75b902
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 75b902
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 75b902
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
import pagure
Pierre-Yves Chibon 75b902
import pagure.lib
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 75b902
    os.mkdir(os.path.join(repopath, 'templates'))
Pierre-Yves Chibon 75b902
    template = os.path.join(repopath, 'templates', 'RFE.md')
Pierre-Yves Chibon 75b902
    with open(template, 'w') as stream:
Pierre-Yves Chibon 75b902
        stream.write('RFE\n###\n\n* Idea description')
Pierre-Yves Chibon 75b902
    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 75b902
    author = pygit2.Signature(
Pierre-Yves Chibon 75b902
        'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 75b902
    committer = pygit2.Signature(
Pierre-Yves Chibon 75b902
        'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 75b902
    commit = clone_repo.create_commit(
Pierre-Yves Chibon 75b902
        'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 75b902
        author,
Pierre-Yves Chibon 75b902
        committer,
Pierre-Yves Chibon 75b902
        '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 75b902
        []
Pierre-Yves Chibon 75b902
    )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    # Create the 2018-bid.md template
Pierre-Yves Chibon 75b902
    template = os.path.join(repopath, 'templates', '2018-bid.md')
Pierre-Yves Chibon 75b902
    with open(template, 'w') as stream:
Pierre-Yves Chibon 75b902
        stream.write('Bid for 2018\n############\n\n* Location:')
Pierre-Yves Chibon 75b902
    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 75b902
    author = pygit2.Signature(
Pierre-Yves Chibon 75b902
        'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 75b902
    committer = pygit2.Signature(
Pierre-Yves Chibon 75b902
        'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 75b902
    clone_repo.create_commit(
Pierre-Yves Chibon 75b902
        'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 75b902
        author,
Pierre-Yves Chibon 75b902
        committer,
Pierre-Yves Chibon 75b902
        '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 75b902
        [commit.hex]
Pierre-Yves Chibon 75b902
    )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
class PagureFlaskIssuestests(tests.Modeltests):
Pierre-Yves Chibon 75b902
    """ Tests for flask issues controller of pagure """
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
Pierre-Yves Chibon 75b902
    @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 75b902
        super(PagureFlaskIssuestests, self).setUp()
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 75b902
        pagure.SESSION = self.session
Pierre-Yves Chibon 75b902
        pagure.ui.SESSION = self.session
Pierre-Yves Chibon 75b902
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon 75b902
        pagure.ui.issues.SESSION = self.session
Pierre-Yves Chibon 75b902
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon 75b902
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon 75b902
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 75b902
            self.path, 'tickets')
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        tests.create_projects(self.session)
Pierre-Yves Chibon 75b902
        tests.create_projects_git(
Pierre-Yves Chibon 75b902
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 75b902
        tests.create_projects_git(
Pierre-Yves Chibon 75b902
            os.path.join(self.path, 'tickets'))
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
        # Add a couple of templates to test2
Pierre-Yves Chibon 75b902
        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 75b902
            self.path, 'tickets', 'somenamespace', 'test3.git')
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 75b902
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 75b902
            output = self.app.get('/test/new_issue')
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 75b902
            self.assertIn(
Pierre-Yves Chibon 75b902
                '
\n New issue',
Pierre-Yves Chibon 75b902
                output.data)
Pierre-Yves Chibon 75b902
            self.assertNotIn(
Pierre-Yves Chibon 75b902
                '<label for="status">Type</label>',
Pierre-Yves Chibon 75b902
                output.data)
Pierre-Yves Chibon 75b902
            self.assertNotIn(
Pierre-Yves Chibon 75b902
                '<select class="form-control c-select" id="type" name="type">',</select>
Pierre-Yves Chibon 75b902
                output.data)
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 75b902
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 75b902
            output = self.app.get('/test2/new_issue')
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 75b902
            self.assertIn(
Pierre-Yves Chibon 75b902
                '
\n New issue',
Pierre-Yves Chibon 75b902
                output.data)
Pierre-Yves Chibon 75b902
            self.assertIn(
Pierre-Yves Chibon 75b902
                '<label for="status">Type</label>',
Pierre-Yves Chibon 75b902
                output.data)
Pierre-Yves Chibon 75b902
            self.assertIn(
Pierre-Yves Chibon 75b902
                '<select class="form-control c-select" id="type" name="type">',</select>
Pierre-Yves Chibon 75b902
                output.data)
Pierre-Yves Chibon 75b902
            self.assertIn(
Pierre-Yves Chibon 75b902
                '<option selected="" value="RFE">RFE</option>',
Pierre-Yves Chibon 75b902
                output.data)
Pierre-Yves Chibon 75b902
            self.assertIn(
Pierre-Yves Chibon 75b902
                '<option selected="" value="2018-bid">2018-bid</option>',
Pierre-Yves Chibon 75b902
                output.data)
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 75b902
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 75b902
            output = self.app.post('/pv/test/issue/template')
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 75b902
            data = json.loads(output.data)
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 75b902
                data,
Pierre-Yves Chibon 75b902
                {"code": "ERROR", "message": "Invalid input submitted"})
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 75b902
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 75b902
            data = {'csrf_token': csrf}
Pierre-Yves Chibon 75b902
            output = self.app.post('/pv/test/issue/template', data=data)
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 75b902
            data = json.loads(output.data)
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 75b902
                data,
Pierre-Yves Chibon 75b902
                {"code": "ERROR", "message": "No template provided"})
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
    def test_get_ticket_template_no_project(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 75b902
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 75b902
            data = {'csrf_token': csrf}
Pierre-Yves Chibon 75b902
            output = self.app.post(
Pierre-Yves Chibon 75b902
                '/pv/test/issue/template?template=RFE', 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 75b902
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 75b902
            data = {'csrf_token': csrf}
Pierre-Yves Chibon 75b902
            output = self.app.post(
Pierre-Yves Chibon 75b902
                '/pv/test/issue/template?template=RFE', data=data)
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 75b902
            data = json.loads(output.data)
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 75b902
                data,
Pierre-Yves Chibon 75b902
                {"code": "ERROR", "message": "No such template found"})
Pierre-Yves Chibon 75b902
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 75b902
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 75b902
            data = {'csrf_token': csrf}
Pierre-Yves Chibon 75b902
            output = self.app.post(
Pierre-Yves Chibon 75b902
                '/pv/test2/issue/template?template=RFE', data=data)
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 75b902
            data = json.loads(output.data)
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 75b902
                data,
Pierre-Yves Chibon 75b902
                {
Pierre-Yves Chibon 75b902
                    "code": "OK",
Pierre-Yves Chibon 75b902
                    "message": "RFE\n###\n\n* Idea description"
Pierre-Yves Chibon 75b902
                }
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 75b902
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 75b902
            csrf = self.get_csrf()
Pierre-Yves Chibon 75b902
            data = {'csrf_token': csrf}
Pierre-Yves Chibon 75b902
            output = self.app.post(
Pierre-Yves Chibon 75b902
                '/pv/somenamespace/test3/issue/template?template=RFE',
Pierre-Yves Chibon 75b902
                data=data)
Pierre-Yves Chibon 75b902
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 75b902
            data = json.loads(output.data)
Pierre-Yves Chibon 75b902
            self.assertEqual(
Pierre-Yves Chibon 75b902
                data,
Pierre-Yves Chibon 75b902
                {
Pierre-Yves Chibon 75b902
                    "code": "OK",
Pierre-Yves Chibon 75b902
                    "message": "RFE\n###\n\n* Idea description"
Pierre-Yves Chibon 75b902
                }
Pierre-Yves Chibon 75b902
            )
Pierre-Yves Chibon 75b902
Pierre-Yves Chibon 75b902
if __name__ == '__main__':
Pierre-Yves Chibon 75b902
    unittest.main(verbosity=2)