Blame tests/test_pagure_flask_ui_issue_pr_link.py

Pierre-Yves Chibon 11d019
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
"""
Pierre-Yves Chibon 11d019
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
 Authors:
Pierre-Yves Chibon 11d019
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
"""
Pierre-Yves Chibon 11d019
Aurélien Bompard 626417
from __future__ import unicode_literals
Aurélien Bompard 626417
Pierre-Yves Chibon 11d019
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 11d019
import pkg_resources
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
import datetime
Pierre-Yves Chibon 11d019
import json
Pierre-Yves Chibon 11d019
import unittest
Pierre-Yves Chibon 11d019
import re
Pierre-Yves Chibon 11d019
import shutil
Pierre-Yves Chibon 11d019
import sys
Pierre-Yves Chibon 11d019
import tempfile
Pierre-Yves Chibon 11d019
import time
Pierre-Yves Chibon 11d019
import os
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
import pygit2
Pierre-Yves Chibon 11d019
from mock import ANY, patch, MagicMock
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 11d019
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
import pagure
Pierre-Yves Chibon 11d019
import pagure.lib
Pierre-Yves Chibon 11d019
import tests
Pierre-Yves Chibon 11d019
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
class PagureFlaskPrIssueLinkTest(tests.Modeltests):
Pierre-Yves Chibon 11d019
    """ Tests pagure when linking PRs to tickets """
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
    maxDiff = None
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
    def setUp(self):
Pierre-Yves Chibon 11d019
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 11d019
        super(PagureFlaskPrIssueLinkTest, self).setUp()
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        tests.create_projects(self.session)
Pierre-Yves Chibon 11d019
        tests.create_projects(
Pierre-Yves Chibon 11d019
            self.session, is_fork=True, user_id=2, hook_token_suffix='bar')
Pierre-Yves Chibon 11d019
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 11d019
        tests.create_projects_git(os.path.join(
Pierre-Yves Chibon 11d019
            self.path, 'repos', 'forks', 'foo'), bare=True)
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Create issues to play with
Pierre-Yves Chibon 11d019
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 11d019
            session=self.session,
Pierre-Yves Chibon 11d019
            repo=repo,
Aurélien Bompard 626417
            title='tést íssüé',
Pierre-Yves Chibon 11d019
            content='We should work on this',
Pierre-Yves Chibon 11d019
            user='pingou',
Pierre-Yves Chibon 11d019
        )
Pierre-Yves Chibon 11d019
        self.session.commit()
Aurélien Bompard 626417
        self.assertEqual(msg.title, 'tést íssüé')
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 11d019
            session=self.session,
Pierre-Yves Chibon 11d019
            repo=repo,
Aurélien Bompard 626417
            title='tést íssüé #2',
Pierre-Yves Chibon 11d019
            content='We should still work on this',
Pierre-Yves Chibon 11d019
            user='foo',
Pierre-Yves Chibon 11d019
        )
Pierre-Yves Chibon 11d019
        self.session.commit()
Aurélien Bompard 626417
        self.assertEqual(msg.title, 'tést íssüé #2')
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Add a commit to the fork
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
Pierre-Yves Chibon 11d019
        repopath = os.path.join(newpath, 'test')
Pierre-Yves Chibon 11d019
        clone_repo = pygit2.clone_repository(os.path.join(
Pierre-Yves Chibon 11d019
            self.path, 'repos', 'forks', 'foo', 'test.git'), repopath)
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Create a file in that git repo
Pierre-Yves Chibon 11d019
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 11d019
            stream.write('foo\n bar')
Pierre-Yves Chibon 11d019
        clone_repo.index.add('sources')
Pierre-Yves Chibon 11d019
        clone_repo.index.write()
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        try:
Pierre-Yves Chibon 11d019
            com = repo.revparse_single('HEAD')
Pierre-Yves Chibon 11d019
            prev_commit = [com.oid.hex]
Pierre-Yves Chibon 11d019
        except:
Pierre-Yves Chibon 11d019
            prev_commit = []
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Commits the files added
Pierre-Yves Chibon 11d019
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 11d019
        author = pygit2.Signature(
Pierre-Yves Chibon 11d019
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 11d019
        committer = pygit2.Signature(
Pierre-Yves Chibon 11d019
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 11d019
        clone_repo.create_commit(
Pierre-Yves Chibon 11d019
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 11d019
            author,
Pierre-Yves Chibon 11d019
            committer,
Pierre-Yves Chibon 11d019
            'Add sources file for testing\n\n Relates to #2',
Pierre-Yves Chibon 11d019
            # binary string representing the tree object ID
Pierre-Yves Chibon 11d019
            tree,
Pierre-Yves Chibon 11d019
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 11d019
            prev_commit
Pierre-Yves Chibon 11d019
        )
Pierre-Yves Chibon 11d019
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 11d019
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 11d019
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Create the corresponding PR
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 11d019
        fork_repo = pagure.lib.get_authorized_project(
Pierre-Yves Chibon 11d019
            self.session, 'test', user='foo')
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        request = pagure.lib.new_pull_request(
Pierre-Yves Chibon 11d019
            self.session,
Pierre-Yves Chibon 11d019
            branch_from='master',
Pierre-Yves Chibon 11d019
            repo_to=repo,
Pierre-Yves Chibon 11d019
            branch_to='master',
Pierre-Yves Chibon 11d019
            title='test PR',
Pierre-Yves Chibon 11d019
            user='foo',
Pierre-Yves Chibon 11d019
            initial_comment=None,
Pierre-Yves Chibon 11d019
            repo_from=fork_repo,
Pierre-Yves Chibon 11d019
        )
Pierre-Yves Chibon 11d019
        self.session.commit()
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        pagure.lib.tasks.link_pr_to_ticket(request.uid)
Pierre-Yves Chibon 11d019
        self.assertEqual(request.id, 3)
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
    def test_ticket_no_link(self):
Pierre-Yves Chibon 11d019
        """ Test that no Related PR(s) block is showing in the issue page.
Pierre-Yves Chibon 11d019
        """
Pierre-Yves Chibon 11d019
        output = self.app.get('/test/issue/1')
Pierre-Yves Chibon 11d019
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 11d019
        self.assertNotIn(
Ryan Lerch e85d33
            'Related Pull Requests',
Aurélien Bompard 626417
            output.get_data(as_text=True))
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
    def test_ticket_link(self):
Pierre-Yves Chibon 11d019
        """ Test that no Related PR(s) block is showing in the issue page.
Pierre-Yves Chibon 11d019
        """
Pierre-Yves Chibon 11d019
        time.sleep(1)
Pierre-Yves Chibon 11d019
        output = self.app.get('/test/issue/2')
Pierre-Yves Chibon 11d019
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 11d019
        self.assertIn(
Ryan Lerch e85d33
            'Related Pull Requests',
Aurélien Bompard 626417
            output.get_data(as_text=True))
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
if __name__ == '__main__':
Pierre-Yves Chibon 11d019
    unittest.main(verbosity=2)