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 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
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
Patrick Uiterwijk 657738
import time
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 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 c51c9e
Pierre-Yves Chibon 930073
import pagure.lib.query
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 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    @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
Pierre-Yves Chibon 73d120
        self.gitrepo = os.path.join(self.path, "repos", "tickets", "test.git")
Pierre-Yves Chibon 73d120
        repopath = os.path.join(self.path, "repos", "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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon c51c9e
        # Create an issue to play with
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon 73d120
        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 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #2",
Pierre-Yves Chibon 73d120
            content="Another bug",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #2")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #3",
Pierre-Yves Chibon 73d120
            content="That would be nice feature no?",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #3")
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 930073
        issue2 = pagure.lib.query.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 930073
        issue3 = pagure.lib.query.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 930073
        msg = pagure.lib.query.add_issue_comment(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            issue=issue,
Pierre-Yves Chibon 73d120
            comment="Hey look a comment!",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Comment added")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_issue_comment(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon c51c9e
            issue=issue,
Pierre-Yves Chibon 73d120
            comment="crazy right?",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Comment added")
Pierre-Yves Chibon c51c9e
        # Assign the ticket to someone
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_issue_assignee(
Pierre-Yves Chibon 73d120
            session=self.session, issue=issue, assignee="pingou", user="pingou"
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Issue assigned to pingou")
Pierre-Yves Chibon c51c9e
        # Add a couple of tags on the ticket
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_tag_obj(
Pierre-Yves Chibon c51c9e
            session=self.session,
Pierre-Yves Chibon 668fbd
            obj=issue,
Pierre-Yves Chibon 73d120
            tags=[" feature ", "future "],
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Issue tagged with: feature, future")
Pierre-Yves Chibon c51c9e
        # Add dependencies
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.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 73d120
            user="pingou",
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Issue marked as depending on: #2")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_issue_dependency(
Pierre-Yves Chibon 73d120
            session=self.session, issue=issue3, issue_blocked=issue, user="foo"
Pierre-Yves Chibon c51c9e
        )
Pierre-Yves Chibon c51c9e
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Issue marked as depending on: #1")
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon c51c9e
        # Dump the JSON
Patrick Uiterwijk 3f97f6
        pagure.lib.git.update_git(issue, repo).wait()
Pierre-Yves Chibon c51c9e
        repo = pygit2.Repository(self.gitrepo)
Pierre-Yves Chibon 73d120
        cnt = len(
Pierre-Yves Chibon 73d120
            [
Pierre-Yves Chibon 73d120
                commit
Pierre-Yves Chibon 73d120
                for commit in repo.walk(
Pierre-Yves Chibon 73d120
                    repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
            ]
Pierre-Yves Chibon 73d120
        )
Patrick Uiterwijk 015941
        self.assertIn(cnt, (9, 10))
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon 73d120
        last_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon c51c9e
        patch = pagure.lib.git.commit_to_patch(repo, last_commit)
Pierre-Yves Chibon 73d120
        for line in patch.split("\n"):
Pierre-Yves Chibon 73d120
            if line.startswith("--- a/"):
Pierre-Yves Chibon 73d120
                fileid = line.split("--- a/")[1]
Pierre-Yves Chibon c51c9e
                break
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon 73d120
        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 73d120
        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
Pierre-Yves Chibon 73d120
        self.gitrepo = os.path.join(self.path, "tickets", "test.git")
Pierre-Yves Chibon 73d120
        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 73d120
            reponame="test",
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon e7d184
            username=None,
Pierre-Yves Chibon 73d120
            issue_uid="foobar",
Pierre-Yves Chibon e7d184
            json_data=jsondata,
Pierre-Yves Chibon 73d120
            agent="pingou",
Pierre-Yves Chibon e7d184
        )
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon e7d184
        # Post loading
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon e7d184
        self.assertEqual(len(repo.issues), 1)
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.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 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(issue.tags_text), sorted(["future", "feature"])
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        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 73d120
        self.assertEqual(issue.status, "Open")
Pierre-Yves Chibon e7d184
Pierre-Yves Chibon c51c9e
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)