Blame tests/test_pagure_flask_dump_load_ticket.py

Pierre-Yves Chibon c51c9e
# -*- coding: utf-8 -*-
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
"""
Pierre-Yves Chibon c51c9e
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
 Authors:
Pierre-Yves Chibon c51c9e
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
"""
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon c51c9e
import pkg_resources
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
import json
Pierre-Yves Chibon c51c9e
import unittest
Pierre-Yves Chibon c51c9e
import shutil
Pierre-Yves Chibon c51c9e
import sys
Pierre-Yves Chibon c51c9e
import tempfile
Pierre-Yves Chibon c51c9e
import os
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
import pygit2
Pierre-Yves Chibon c51c9e
from mock import patch
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon c51c9e
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
import pagure.lib
Pierre-Yves Chibon c51c9e
import tests
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
class PagureFlaskDumpLoadTicketTests(tests.Modeltests):
Pierre-Yves Chibon c51c9e
    """ Tests for flask application for dumping and re-loading the JSON of
Pierre-Yves Chibon c51c9e
    a ticket.
Pierre-Yves Chibon c51c9e
    """
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
    def setUp(self):
Pierre-Yves Chibon c51c9e
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon c51c9e
        super(PagureFlaskDumpLoadTicketTests, self).setUp()
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon c51c9e
        pagure.SESSION = self.session
Pierre-Yves Chibon c51c9e
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon c51c9e
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon c51c9e
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon c51c9e
        pagure.ui.fork.SESSION = self.session
Pierre-Yves Chibon c51c9e
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
Pierre-Yves Chibon c51c9e
        pagure.APP.config['FORK_FOLDER'] = os.path.join(tests.HERE, 'forks')
Pierre-Yves Chibon c51c9e
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon c51c9e
            tests.HERE, 'tickets')
Pierre-Yves Chibon c51c9e
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon c51c9e
            tests.HERE, 'docs')
Pierre-Yves Chibon c51c9e
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
Pierre-Yves Chibon c51c9e
            tests.HERE, 'requests')
Pierre-Yves Chibon c51c9e
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon c51c9e
    def test_dumping_ticket(self, send_email):
Pierre-Yves Chibon c51c9e
        """ Test dumping a ticket into a JSON blob. """
Pierre-Yves Chibon c51c9e
        send_email.return_value = True
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        tests.create_projects(self.session)
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        # Create repo
Pierre-Yves Chibon c51c9e
        self.gitrepo = os.path.join(tests.HERE, 'tickets', 'test.git')
Pierre-Yves Chibon c51c9e
        repopath = os.path.join(tests.HERE, 'tickets')
Pierre-Yves Chibon c51c9e
        os.makedirs(self.gitrepo)
Pierre-Yves Chibon c51c9e
        repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon c51c9e
        # Create an issue to play with
Pierre-Yves Chibon c51c9e
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            repo=repo,
Pierre-Yves Chibon c51c9e
            title='Test issue',
Pierre-Yves Chibon c51c9e
            content='We should work on this',
Pierre-Yves Chibon c51c9e
            user='pingou',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg.title, 'Test issue')
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        # Need another two issue to test the dependencie chain
Pierre-Yves Chibon c51c9e
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            repo=repo,
Pierre-Yves Chibon c51c9e
            title='Test issue #2',
Pierre-Yves Chibon c51c9e
            content='Another bug',
Pierre-Yves Chibon c51c9e
            user='pingou',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg.title, 'Test issue #2')
Pierre-Yves Chibon c51c9e
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            repo=repo,
Pierre-Yves Chibon c51c9e
            title='Test issue #3',
Pierre-Yves Chibon c51c9e
            content='That would be nice feature no?',
Pierre-Yves Chibon c51c9e
            user='foo',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg.title, 'Test issue #3')
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon c51c9e
        issue2 = pagure.lib.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon c51c9e
        issue3 = pagure.lib.search_issues(self.session, repo, issueid=3)
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        # Add a couple of comment on the ticket
Pierre-Yves Chibon c51c9e
        msg = pagure.lib.add_issue_comment(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            issue=issue,
Pierre-Yves Chibon c51c9e
            comment='Hey look a comment!',
Pierre-Yves Chibon c51c9e
            user='foo',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath,
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg, 'Comment added')
Pierre-Yves Chibon c51c9e
        msg = pagure.lib.add_issue_comment(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            issue=issue,
Pierre-Yves Chibon c51c9e
            comment='crazy right?',
Pierre-Yves Chibon c51c9e
            user='pingou',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath,
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg, 'Comment added')
Pierre-Yves Chibon c51c9e
        # Assign the ticket to someone
Pierre-Yves Chibon c51c9e
        msg = pagure.lib.add_issue_assignee(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            issue=issue,
Pierre-Yves Chibon c51c9e
            assignee='pingou',
Pierre-Yves Chibon c51c9e
            user='pingou',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath,
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg, 'Issue assigned')
Pierre-Yves Chibon c51c9e
        # Add a couple of tags on the ticket
Pierre-Yves Chibon 668fbd
        msg = pagure.lib.add_tag_obj(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon 668fbd
            obj=issue,
Pierre-Yves Chibon c51c9e
            tags=[' feature ', 'future '],
Pierre-Yves Chibon c51c9e
            user='pingou',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath,
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg, 'Tag added: feature, future')
Pierre-Yves Chibon c51c9e
        # Add dependencies
Pierre-Yves Chibon c51c9e
        msg = pagure.lib.add_issue_dependency(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            issue=issue,
Pierre-Yves Chibon c51c9e
            issue_blocked=issue2,
Pierre-Yves Chibon c51c9e
            user='pingou',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath,
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg, 'Dependency added')
Pierre-Yves Chibon c51c9e
        msg = pagure.lib.add_issue_dependency(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            issue=issue3,
Pierre-Yves Chibon c51c9e
            issue_blocked=issue,
Pierre-Yves Chibon c51c9e
            user='foo',
Pierre-Yves Chibon c51c9e
            ticketfolder=repopath,
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon c51c9e
        self.assertEqual(msg, 'Dependency added')
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        # Dump the JSON
Pierre-Yves Chibon c51c9e
        pagure.lib.git.update_git(issue, repo, repopath)
Pierre-Yves Chibon c51c9e
        repo = pygit2.Repository(self.gitrepo)
Pierre-Yves Chibon c51c9e
        cnt = len([commit
Pierre-Yves Chibon c51c9e
            for commit in repo.walk(
Pierre-Yves Chibon c51c9e
                repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL)])
Pierre-Yves Chibon c51c9e
        self.assertEqual(cnt, 10)
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        last_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon c51c9e
        patch = pagure.lib.git.commit_to_patch(repo, last_commit)
Pierre-Yves Chibon c51c9e
        for line in patch.split('\n'):
Pierre-Yves Chibon c51c9e
            if line.startswith('--- a/'):
Pierre-Yves Chibon c51c9e
                fileid = line.split('--- a/')[1]
Pierre-Yves Chibon c51c9e
                break
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        newpath = tempfile.mkdtemp(prefix='pagure-dump-load')
Pierre-Yves Chibon c51c9e
        clone_repo = pygit2.clone_repository(self.gitrepo, newpath)
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        self.assertEqual(len(os.listdir(newpath)), 4)
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon be1a1b
        ticket_json = os.path.join(tests.HERE, 'test_ticket.json')
Pierre-Yves Chibon be1a1b
        self.assertFalse(os.path.exists(ticket_json))
Pierre-Yves Chibon be1a1b
        shutil.copyfile(os.path.join(newpath, fileid), ticket_json)
Pierre-Yves Chibon be1a1b
        self.assertTrue(os.path.exists(ticket_json))
Pierre-Yves Chibon be1a1b
Pierre-Yves Chibon be1a1b
        shutil.rmtree(newpath)
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon e7d184
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon e7d184
    def test_reload_ticket(self, send_email):
Pierre-Yves Chibon e7d184
        """ Test reloading a ticket from a JSON blob. """
Pierre-Yves Chibon e7d184
        send_email.return_value = True
Pierre-Yves Chibon e7d184
        ticket_json = os.path.join(tests.HERE, 'test_ticket.json')
Pierre-Yves Chibon e7d184
        self.assertTrue(os.path.exists(ticket_json))
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon e7d184
        tests.create_projects(self.session)
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon e7d184
        jsondata = None
Pierre-Yves Chibon e7d184
        with open(ticket_json) as stream:
Pierre-Yves Chibon e7d184
            jsondata = json.load(stream)
Pierre-Yves Chibon e7d184
        self.assertNotEqual(jsondata, None)
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon e7d184
        pagure.lib.git.update_ticket_from_git(
Pierre-Yves Chibon e7d184
            self.session,
Pierre-Yves Chibon e7d184
            reponame='test',
Pierre-Yves Chibon e7d184
            username=None,
Pierre-Yves Chibon e7d184
            issue_uid='foobar',
Pierre-Yves Chibon e7d184
            json_data=jsondata,
Pierre-Yves Chibon e7d184
        )
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon e7d184
        # Post loading
Pierre-Yves Chibon e7d184
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon e7d184
        self.assertEqual(len(repo.issues), 1)
Pierre-Yves Chibon e7d184
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon e7d184
        # Check after re-loading
Pierre-Yves Chibon e7d184
        self.assertEqual(len(issue.comments), 2)
Pierre-Yves Chibon e7d184
        self.assertEqual(len(issue.tags), 2)
Pierre-Yves Chibon e7d184
        self.assertEqual(issue.tags_text, ['future', 'feature'])
Pierre-Yves Chibon e7d184
        self.assertEqual(issue.assignee.username, 'pingou')
Pierre-Yves Chibon e7d184
        self.assertEqual(issue.children, [])
Pierre-Yves Chibon e7d184
        self.assertEqual(issue.parents, [])
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon e7d184
        self.assertTrue(os.path.exists(ticket_json))
Pierre-Yves Chibon e7d184
        os.unlink(ticket_json)
Pierre-Yves Chibon e7d184
        self.assertFalse(os.path.exists(ticket_json))
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
if __name__ == '__main__':
Pierre-Yves Chibon c51c9e
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon c51c9e
        PagureFlaskDumpLoadTicketTests)
Pierre-Yves Chibon c51c9e
    unittest.TextTestRunner(verbosity=2).run(SUITE)