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
Farhaan Bukhsh 72e9db
import pagure
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
Pierre-Yves Chibon c51c9e
    @patch('pagure.lib.notify.send_email')
Patrick Uiterwijk b71da2
    @patch('pagure.lib.git._maybe_wait')
Patrick Uiterwijk b71da2
    def test_dumping_reloading_ticket(self, mw, send_email):
Pierre-Yves Chibon c51c9e
        """ Test dumping a ticket into a JSON blob. """
Patrick Uiterwijk b71da2
        mw.side_effect = lambda result: result.get()
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
Jeremy Cline 20109f
        self.gitrepo = os.path.join(self.path, 'tickets', 'test.git')
Jeremy Cline 20109f
        repopath = os.path.join(self.path, '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
Farhaan Bukhsh 72e9db
        repo = pagure.get_authorized_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
Mark Reynolds eabdc8
        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 a18547
        self.assertEqual(msg, 'Issue assigned to pingou')
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 a18547
        self.assertEqual(msg, 'Issue tagged with: 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 a18547
        self.assertEqual(msg, 'Issue marked as depending on: #2')
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 a18547
        self.assertEqual(msg, 'Issue marked as depending on: #1')
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        # Dump the JSON
Patrick Uiterwijk 114ca2
        pagure.lib.git.update_git(issue, repo, repopath).wait()
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 a8a5f4
        self.assertEqual(cnt, 9)
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
Jeremy Cline 20109f
        ticket_json = os.path.join(self.path, '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 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
Jeremy Cline 20109f
        shutil.rmtree(newpath)
Jeremy Cline 20109f
Jeremy Cline 20109f
        # Test reloading the JSON
Jeremy Cline 20109f
        self.tearDown()
Jeremy Cline 20109f
        self.setUp()
Jeremy Cline 20109f
        tests.create_projects(self.session)
Jeremy Cline 20109f
Patrick Uiterwijk b71da2
        # Create repo
Patrick Uiterwijk b71da2
        self.gitrepo = os.path.join(self.path, 'tickets', 'test.git')
Patrick Uiterwijk b71da2
        repopath = os.path.join(self.path, 'tickets')
Patrick Uiterwijk b71da2
        os.makedirs(self.gitrepo)
Patrick Uiterwijk b71da2
        pygit2.init_repository(self.gitrepo, bare=True)
Patrick Uiterwijk b71da2
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 5abfd2
            namespace=None,
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
Farhaan Bukhsh 72e9db
        repo = pagure.get_authorized_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
Mark Reynolds eabdc8
        self.assertEqual(len(issue.comments), 3)
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 c51c9e
Pierre-Yves Chibon c51c9e
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)