Blame tests/test_pagure_flask_api_ui_private_repo.py

farhaanbukhsh 1df65f
# -*- coding: utf-8 -*-
farhaanbukhsh 1df65f
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
farhaanbukhsh 60d445
import datetime
farhaanbukhsh 1df65f
import unittest
farhaanbukhsh 1df65f
import shutil
farhaanbukhsh 1df65f
import sys
farhaanbukhsh c4f329
import tempfile
farhaanbukhsh 1df65f
import os
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
import json
farhaanbukhsh c4f329
import pygit2
Aurélien Bompard 626417
from mock import patch, MagicMock
farhaanbukhsh 1df65f
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
)
farhaanbukhsh 1df65f
Pierre-Yves Chibon 930073
import pagure.lib.query
farhaanbukhsh 1df65f
import tests
farhaanbukhsh c4f329
from pagure.lib.repo import PagureRepo
farhaanbukhsh 1df65f
Farhaan Bukhsh 8aa0f5
FULL_ISSUE_LIST = [
Farhaan Bukhsh 8aa0f5
    {
Farhaan Bukhsh 8aa0f5
        "assignee": None,
Farhaan Bukhsh 8aa0f5
        "blocks": [],
Farhaan Bukhsh 8aa0f5
        "close_status": None,
Farhaan Bukhsh 8aa0f5
        "closed_at": None,
Clement Verna 81c130
        "closed_by": None,
Farhaan Bukhsh 8aa0f5
        "comments": [],
Farhaan Bukhsh 8aa0f5
        "content": "We should work on this",
Farhaan Bukhsh 8aa0f5
        "custom_fields": [],
Farhaan Bukhsh 8aa0f5
        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
        "depends": [],
Farhaan Bukhsh 8aa0f5
        "id": 8,
Farhaan Bukhsh 8aa0f5
        "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
        "milestone": None,
Farhaan Bukhsh 8aa0f5
        "priority": None,
Farhaan Bukhsh 8aa0f5
        "private": True,
Farhaan Bukhsh 8aa0f5
        "status": "Open",
Farhaan Bukhsh 8aa0f5
        "tags": [],
Farhaan Bukhsh 8aa0f5
        "title": "Test issue",
Pierre-Yves Chibon 73d120
        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 8aa0f5
    },
Farhaan Bukhsh 8aa0f5
    {
Farhaan Bukhsh 8aa0f5
        "assignee": None,
Farhaan Bukhsh 8aa0f5
        "blocks": [],
Farhaan Bukhsh 8aa0f5
        "close_status": None,
Farhaan Bukhsh 8aa0f5
        "closed_at": None,
Clement Verna 81c130
        "closed_by": None,
Farhaan Bukhsh 8aa0f5
        "comments": [],
Farhaan Bukhsh 8aa0f5
        "content": "This issue needs attention",
Farhaan Bukhsh 8aa0f5
        "custom_fields": [],
Farhaan Bukhsh 8aa0f5
        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
        "depends": [],
Farhaan Bukhsh 8aa0f5
        "id": 7,
Farhaan Bukhsh 8aa0f5
        "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
        "milestone": None,
Farhaan Bukhsh 8aa0f5
        "priority": None,
Farhaan Bukhsh 8aa0f5
        "private": True,
Farhaan Bukhsh 8aa0f5
        "status": "Open",
Farhaan Bukhsh 8aa0f5
        "tags": [],
Farhaan Bukhsh 8aa0f5
        "title": "test issue",
Pierre-Yves Chibon 73d120
        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 8aa0f5
    },
Farhaan Bukhsh 8aa0f5
    {
Farhaan Bukhsh 8aa0f5
        "assignee": None,
Farhaan Bukhsh 8aa0f5
        "blocks": [],
Farhaan Bukhsh 8aa0f5
        "close_status": None,
Farhaan Bukhsh 8aa0f5
        "closed_at": None,
Clement Verna 81c130
        "closed_by": None,
Farhaan Bukhsh 8aa0f5
        "comments": [],
Farhaan Bukhsh 8aa0f5
        "content": "This issue needs attention",
Farhaan Bukhsh 8aa0f5
        "custom_fields": [],
Farhaan Bukhsh 8aa0f5
        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
        "depends": [],
Farhaan Bukhsh 8aa0f5
        "id": 6,
Farhaan Bukhsh 8aa0f5
        "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
        "milestone": None,
Farhaan Bukhsh 8aa0f5
        "priority": None,
Farhaan Bukhsh 8aa0f5
        "private": True,
Farhaan Bukhsh 8aa0f5
        "status": "Open",
Farhaan Bukhsh 8aa0f5
        "tags": [],
Farhaan Bukhsh 8aa0f5
        "title": "test issue",
Pierre-Yves Chibon 73d120
        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 8aa0f5
    },
Farhaan Bukhsh 8aa0f5
    {
Farhaan Bukhsh 8aa0f5
        "assignee": None,
Farhaan Bukhsh 8aa0f5
        "blocks": [],
Farhaan Bukhsh 8aa0f5
        "close_status": None,
Farhaan Bukhsh 8aa0f5
        "closed_at": None,
Clement Verna 81c130
        "closed_by": None,
Farhaan Bukhsh 8aa0f5
        "comments": [],
Farhaan Bukhsh 8aa0f5
        "content": "This issue needs attention",
Farhaan Bukhsh 8aa0f5
        "custom_fields": [],
Farhaan Bukhsh 8aa0f5
        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
        "depends": [],
Farhaan Bukhsh 8aa0f5
        "id": 5,
Farhaan Bukhsh 8aa0f5
        "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
        "milestone": None,
Farhaan Bukhsh 8aa0f5
        "priority": None,
Farhaan Bukhsh 8aa0f5
        "private": False,
Farhaan Bukhsh 8aa0f5
        "status": "Open",
Farhaan Bukhsh 8aa0f5
        "tags": [],
Farhaan Bukhsh 8aa0f5
        "title": "test issue",
Pierre-Yves Chibon 73d120
        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 8aa0f5
    },
Farhaan Bukhsh 8aa0f5
    {
Farhaan Bukhsh 8aa0f5
        "assignee": None,
Farhaan Bukhsh 8aa0f5
        "blocks": [],
Farhaan Bukhsh 8aa0f5
        "close_status": None,
Farhaan Bukhsh 8aa0f5
        "closed_at": None,
Clement Verna 81c130
        "closed_by": None,
Farhaan Bukhsh 8aa0f5
        "comments": [],
Farhaan Bukhsh 8aa0f5
        "content": "This issue needs attention",
Farhaan Bukhsh 8aa0f5
        "custom_fields": [],
Farhaan Bukhsh 8aa0f5
        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
        "depends": [],
Farhaan Bukhsh 8aa0f5
        "id": 4,
Farhaan Bukhsh 8aa0f5
        "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
        "milestone": None,
Farhaan Bukhsh 8aa0f5
        "priority": None,
Farhaan Bukhsh 8aa0f5
        "private": False,
Farhaan Bukhsh 8aa0f5
        "status": "Open",
Farhaan Bukhsh 8aa0f5
        "tags": [],
Farhaan Bukhsh 8aa0f5
        "title": "test issue",
Pierre-Yves Chibon 73d120
        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 8aa0f5
    },
Farhaan Bukhsh 8aa0f5
    {
Farhaan Bukhsh 8aa0f5
        "assignee": None,
Farhaan Bukhsh 8aa0f5
        "blocks": [],
Farhaan Bukhsh 8aa0f5
        "close_status": None,
Farhaan Bukhsh 8aa0f5
        "closed_at": None,
Clement Verna 81c130
        "closed_by": None,
Farhaan Bukhsh 8aa0f5
        "comments": [],
Farhaan Bukhsh 8aa0f5
        "content": "This issue needs attention",
Farhaan Bukhsh 8aa0f5
        "custom_fields": [],
Farhaan Bukhsh 8aa0f5
        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
        "depends": [],
Farhaan Bukhsh 8aa0f5
        "id": 3,
Farhaan Bukhsh 8aa0f5
        "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
        "milestone": None,
Farhaan Bukhsh 8aa0f5
        "priority": None,
Farhaan Bukhsh 8aa0f5
        "private": False,
Farhaan Bukhsh 8aa0f5
        "status": "Open",
Farhaan Bukhsh 8aa0f5
        "tags": [],
Farhaan Bukhsh 8aa0f5
        "title": "test issue",
Pierre-Yves Chibon 73d120
        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 8aa0f5
    },
Farhaan Bukhsh 8aa0f5
    {
Farhaan Bukhsh 8aa0f5
        "assignee": None,
Farhaan Bukhsh 8aa0f5
        "blocks": [],
Farhaan Bukhsh 8aa0f5
        "close_status": None,
Farhaan Bukhsh 8aa0f5
        "closed_at": None,
Clement Verna 81c130
        "closed_by": None,
Farhaan Bukhsh 8aa0f5
        "comments": [],
Farhaan Bukhsh 8aa0f5
        "content": "This issue needs attention",
Farhaan Bukhsh 8aa0f5
        "custom_fields": [],
Farhaan Bukhsh 8aa0f5
        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
        "depends": [],
Farhaan Bukhsh 8aa0f5
        "id": 2,
Farhaan Bukhsh 8aa0f5
        "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
        "milestone": None,
Farhaan Bukhsh 8aa0f5
        "priority": None,
Farhaan Bukhsh 8aa0f5
        "private": False,
Farhaan Bukhsh 8aa0f5
        "status": "Open",
Farhaan Bukhsh 8aa0f5
        "tags": [],
Farhaan Bukhsh 8aa0f5
        "title": "test issue",
Pierre-Yves Chibon 73d120
        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 8aa0f5
    },
Farhaan Bukhsh 8aa0f5
    {
Farhaan Bukhsh 8aa0f5
        "assignee": None,
Farhaan Bukhsh 8aa0f5
        "blocks": [],
Farhaan Bukhsh 8aa0f5
        "close_status": None,
Farhaan Bukhsh 8aa0f5
        "closed_at": None,
Clement Verna 81c130
        "closed_by": None,
Farhaan Bukhsh 8aa0f5
        "comments": [],
Farhaan Bukhsh 8aa0f5
        "content": "This issue needs attention",
Farhaan Bukhsh 8aa0f5
        "custom_fields": [],
Farhaan Bukhsh 8aa0f5
        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
        "depends": [],
Farhaan Bukhsh 8aa0f5
        "id": 1,
Farhaan Bukhsh 8aa0f5
        "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
        "milestone": None,
Farhaan Bukhsh 8aa0f5
        "priority": None,
Farhaan Bukhsh 8aa0f5
        "private": False,
Farhaan Bukhsh 8aa0f5
        "status": "Open",
Farhaan Bukhsh 8aa0f5
        "tags": [],
Farhaan Bukhsh 8aa0f5
        "title": "test issue",
Pierre-Yves Chibon 73d120
        "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
    },
Farhaan Bukhsh 8aa0f5
]
Farhaan Bukhsh 8aa0f5
farhaanbukhsh 753ee7
farhaanbukhsh 1df65f
class PagurePrivateRepotest(tests.Modeltests):
farhaanbukhsh 1df65f
    """ Tests for private repo in pagure """
farhaanbukhsh 1df65f
Pierre-Yves Chibon 2521db
    maxDiff = None
Pierre-Yves Chibon 2521db
farhaanbukhsh 1df65f
    def setUp(self):
farhaanbukhsh 1df65f
        """ Set up the environnment, ran before every tests. """
farhaanbukhsh 1df65f
        super(PagurePrivateRepotest, self).setUp()
farhaanbukhsh 1df65f
Pierre-Yves Chibon 73d120
        pagure.config.config["TESTING"] = True
Pierre-Yves Chibon 73d120
        pagure.config.config["DATAGREPPER_URL"] = None
Pierre-Yves Chibon 73d120
        pagure.config.config["PRIVATE_PROJECTS"] = True
Pierre-Yves Chibon 73d120
        pagure.config.config["VIRUS_SCAN_ATTACHMENTS"] = False
farhaanbukhsh 1df65f
farhaanbukhsh c4f329
    def set_up_git_repo(
Pierre-Yves Chibon 73d120
        self, new_project=None, branch_from="feature", mtype="FF"
Pierre-Yves Chibon 73d120
    ):
farhaanbukhsh c4f329
        """ Set up the git repo and create the corresponding PullRequest
farhaanbukhsh c4f329
        object.
farhaanbukhsh c4f329
        """
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "pmc.git")
farhaanbukhsh c4f329
        repo = pygit2.init_repository(gitrepo, bare=True)
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
        newpath = tempfile.mkdtemp(prefix="pagure-private-test")
Pierre-Yves Chibon 73d120
        repopath = os.path.join(newpath, "test")
farhaanbukhsh c4f329
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        clone_repo.index.add("sources")
farhaanbukhsh c4f329
        clone_repo.index.write()
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Commits the files added
farhaanbukhsh c4f329
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
farhaanbukhsh c4f329
        clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
farhaanbukhsh c4f329
            author,
farhaanbukhsh c4f329
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
farhaanbukhsh c4f329
            # binary string representing the tree object ID
farhaanbukhsh c4f329
            tree,
farhaanbukhsh c4f329
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
farhaanbukhsh c4f329
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
farhaanbukhsh c4f329
        ori_remote = clone_repo.remotes[0]
farhaanbukhsh c4f329
        PagureRepo.push(ori_remote, refname)
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
        if mtype == "merge":
Pierre-Yves Chibon 73d120
            with open(os.path.join(repopath, ".gitignore"), "w") as stream:
Pierre-Yves Chibon 73d120
                stream.write("*~")
Pierre-Yves Chibon 73d120
            clone_repo.index.add(".gitignore")
farhaanbukhsh c4f329
            clone_repo.index.write()
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            # Commits the files added
farhaanbukhsh c4f329
            tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
            author = pygit2.Signature("Alice Äuthòr", "alice@äuthòrs.tld")
farhaanbukhsh c4f329
            committer = pygit2.Signature(
Pierre-Yves Chibon 73d120
                "Cecil Cõmmîttër", "cecil@cõmmîttërs.tld"
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh c4f329
            clone_repo.create_commit(
Pierre-Yves Chibon 73d120
                "refs/heads/master",
farhaanbukhsh c4f329
                author,
farhaanbukhsh c4f329
                committer,
Pierre-Yves Chibon 73d120
                "Add .gitignore file for testing",
farhaanbukhsh c4f329
                # binary string representing the tree object ID
farhaanbukhsh c4f329
                tree,
farhaanbukhsh c4f329
                # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
                [first_commit.oid.hex],
farhaanbukhsh c4f329
            )
Pierre-Yves Chibon 73d120
            refname = "refs/heads/master:refs/heads/master"
farhaanbukhsh c4f329
            ori_remote = clone_repo.remotes[0]
farhaanbukhsh c4f329
            PagureRepo.push(ori_remote, refname)
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
        if mtype == "conflicts":
Pierre-Yves Chibon 73d120
            with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
                stream.write("foo\n bar\nbaz")
Pierre-Yves Chibon 73d120
            clone_repo.index.add("sources")
farhaanbukhsh c4f329
            clone_repo.index.write()
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            # Commits the files added
farhaanbukhsh c4f329
            tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
            author = pygit2.Signature("Alice Author", "alice@authors.tld")
farhaanbukhsh c4f329
            committer = pygit2.Signature(
Pierre-Yves Chibon 73d120
                "Cecil Committer", "cecil@committers.tld"
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh c4f329
            clone_repo.create_commit(
Pierre-Yves Chibon 73d120
                "refs/heads/master",
farhaanbukhsh c4f329
                author,
farhaanbukhsh c4f329
                committer,
Pierre-Yves Chibon 73d120
                "Add sources conflicting",
farhaanbukhsh c4f329
                # binary string representing the tree object ID
farhaanbukhsh c4f329
                tree,
farhaanbukhsh c4f329
                # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
                [first_commit.oid.hex],
farhaanbukhsh c4f329
            )
Pierre-Yves Chibon 73d120
            refname = "refs/heads/master:refs/heads/master"
farhaanbukhsh c4f329
            ori_remote = clone_repo.remotes[0]
farhaanbukhsh c4f329
            PagureRepo.push(ori_remote, refname)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Set the second repo
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        new_gitrepo = repopath
farhaanbukhsh c4f329
        if new_project:
farhaanbukhsh c4f329
            # Create a new git repo to play with
farhaanbukhsh c4f329
            new_gitrepo = os.path.join(newpath, new_project.fullname)
farhaanbukhsh c4f329
            if not os.path.exists(new_gitrepo):
farhaanbukhsh c4f329
                os.makedirs(new_gitrepo)
farhaanbukhsh c4f329
                new_repo = pygit2.clone_repository(gitrepo, new_gitrepo)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        repo = pygit2.Repository(new_gitrepo)
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
        if mtype != "nochanges":
farhaanbukhsh c4f329
            # Edit the sources file again
Pierre-Yves Chibon 73d120
            with open(os.path.join(new_gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
                stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
            repo.index.add("sources")
farhaanbukhsh c4f329
            repo.index.write()
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            # Commits the files added
farhaanbukhsh c4f329
            tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
            author = pygit2.Signature("Alice Author", "alice@authors.tld")
farhaanbukhsh c4f329
            committer = pygit2.Signature(
Pierre-Yves Chibon 73d120
                "Cecil Committer", "cecil@committers.tld"
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh c4f329
            repo.create_commit(
Pierre-Yves Chibon 73d120
                "refs/heads/%s" % branch_from,
farhaanbukhsh c4f329
                author,
farhaanbukhsh c4f329
                committer,
Pierre-Yves Chibon 73d120
                "A commit on branch %s" % branch_from,
farhaanbukhsh c4f329
                tree,
Pierre-Yves Chibon 73d120
                [first_commit.oid.hex],
farhaanbukhsh c4f329
            )
Pierre-Yves Chibon 73d120
            refname = "refs/heads/%s" % (branch_from)
farhaanbukhsh c4f329
            ori_remote = repo.remotes[0]
farhaanbukhsh c4f329
            PagureRepo.push(ori_remote, refname)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Create a PR for these changes
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "pmc")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
farhaanbukhsh c4f329
            session=self.session,
farhaanbukhsh c4f329
            repo_from=project,
farhaanbukhsh c4f329
            branch_from=branch_from,
farhaanbukhsh c4f329
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the %s branch" % branch_from,
Pierre-Yves Chibon 73d120
            user="pingou",
farhaanbukhsh c4f329
        )
farhaanbukhsh c4f329
        self.session.commit()
farhaanbukhsh c4f329
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "PR from the %s branch" % branch_from)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        shutil.rmtree(newpath)
farhaanbukhsh c4f329
farhaanbukhsh 1df65f
    def test_index(self):
farhaanbukhsh 1df65f
        """ Test the index endpoint. """
farhaanbukhsh 1df65f
Pierre-Yves Chibon 73d120
        output = self.app.get("/")
farhaanbukhsh 1df65f
        self.assertEqual(output.status_code, 200)
farhaanbukhsh 1df65f
        self.assertIn(
Pierre-Yves Chibon b1ec6e
            '

All Projects '

Pierre-Yves Chibon b1ec6e
            '0',
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        # Add a private project
farhaanbukhsh 1df65f
        item = pagure.lib.model.Project(
farhaanbukhsh 1df65f
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeee",
farhaanbukhsh 1df65f
            private=True,
farhaanbukhsh 1df65f
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.session.add(item)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        # Add a public project
farhaanbukhsh 1df65f
        item = pagure.lib.model.Project(
farhaanbukhsh 1df65f
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeccceee",
farhaanbukhsh 1df65f
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.session.add(item)
farhaanbukhsh 1df65f
        self.session.commit()
farhaanbukhsh 1df65f
Pierre-Yves Chibon 73d120
        output = self.app.get("/?page=abc")
farhaanbukhsh 1df65f
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        output_text = output.get_data(as_text=True)
farhaanbukhsh 1df65f
        self.assertIn(
Pierre-Yves Chibon b1ec6e
            '

All Projects '

Pierre-Yves Chibon b1ec6e
            '1',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 1df65f
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/", follow_redirects=True)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
farhaanbukhsh 1df65f
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("2 Projects", output_text)
Ryan Lerch 336cd6
            self.assertNotIn(
Pierre-Yves Chibon 73d120
                'Forks', output_text
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 1df65f
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output_text.count('Groups'), 0
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
    def test_view_user(self):
farhaanbukhsh 1df65f
        """ Test the view_user endpoint. """
farhaanbukhsh 1df65f
Pierre-Yves Chibon 73d120
        output = self.app.get("/user/foo?repopage=abc&forkpage=def")
farhaanbukhsh 1df65f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 75b0c3
        output_text = output.get_data(as_text=True)
farhaanbukhsh 1df65f
        self.assertIn(
Pierre-Yves Chibon 73d120
            """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Projects 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 1df65f
        self.assertIn(
Pierre-Yves Chibon 73d120
            """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Forks 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 75b0c3
        self.assertIn(
Pierre-Yves Chibon 73d120
            """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Groups 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        # Add a private project
farhaanbukhsh 1df65f
        item = pagure.lib.model.Project(
farhaanbukhsh 1df65f
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeee",
farhaanbukhsh 1df65f
            private=True,
farhaanbukhsh 1df65f
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.session.add(item)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        # Add a public project
farhaanbukhsh 1df65f
        item = pagure.lib.model.Project(
farhaanbukhsh 1df65f
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeccceee",
farhaanbukhsh 1df65f
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.session.add(item)
farhaanbukhsh 1df65f
        self.session.commit()
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.gitrepos = tests.create_projects_git(
Pierre-Yves Chibon 73d120
            pagure.config.config["GIT_FOLDER"]
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 1df65f
Pierre-Yves Chibon 73d120
        output = self.app.get("/user/foo")
farhaanbukhsh 1df65f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 75b0c3
        output_text = output.get_data(as_text=True)
farhaanbukhsh 1df65f
        self.assertIn(
Pierre-Yves Chibon 73d120
            """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Projects 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  1
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 1df65f
        self.assertIn(
Pierre-Yves Chibon 73d120
            """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Forks 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 75b0c3
        self.assertIn(
Pierre-Yves Chibon 73d120
            """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Groups 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 1df65f
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/user/foo")
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
farhaanbukhsh 1df65f
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Projects 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  1
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 1df65f
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Forks 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f0babf
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Groups 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 1df65f
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/user/foo")
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
farhaanbukhsh 1df65f
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Projects 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  1
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 1df65f
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Forks 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f0babf
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Groups 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 1df65f
farhaanbukhsh d59922
        # Check pingou has 0 projects
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/", follow_redirects=True)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
farhaanbukhsh d59922
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("0 Projects", output_text)
Ryan Lerch 336cd6
            self.assertNotIn(
Pierre-Yves Chibon 73d120
                'Forks', output_text
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh d59922
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output_text.count('Groups'), 0
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh d59922
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test3")
farhaanbukhsh b909de
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            session=self.session, project=repo, new_user="pingou", user="foo"
farhaanbukhsh b6711e
        )
Pierre-Yves Chibon b130e5
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
farhaanbukhsh b6711e
farhaanbukhsh b6711e
        # New user added to private projects
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/", follow_redirects=True)
Aurélien Bompard 626417
            output_text = output.get_data(as_text=True)
farhaanbukhsh b6711e
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn("1 Projects", output_text)
Ryan Lerch 336cd6
            self.assertNotIn(
Pierre-Yves Chibon 73d120
                'Forks', output_text
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh b6711e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output_text.count('Groups'), 0
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh b6711e
Pierre-Yves Chibon 73d120
    @patch("pagure.decorators.admin_session_timedout")
farhaanbukhsh c4f329
    def test_private_settings_ui(self, ast):
farhaanbukhsh c4f329
        """ Test UI for private repo"""
Pierre-Yves Chibon 52565d
        ast.return_value = False
farhaanbukhsh c4f329
farhaanbukhsh b909de
        # Add private repo
farhaanbukhsh 0163a1
        item = pagure.lib.model.Project(
farhaanbukhsh 0163a1
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh 0163a1
            private=True,
farhaanbukhsh 0163a1
        )
farhaanbukhsh 0163a1
        self.session.add(item)
farhaanbukhsh 0163a1
        self.session.commit()
farhaanbukhsh 0163a1
farhaanbukhsh 0163a1
        # Add a git repo
farhaanbukhsh b909de
        repo_path = os.path.join(
Pierre-Yves Chibon 73d120
            pagure.config.config.get("GIT_FOLDER"), "test4.git"
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 0163a1
        if not os.path.exists(repo_path):
farhaanbukhsh 0163a1
            os.makedirs(repo_path)
farhaanbukhsh 0163a1
        pygit2.init_repository(repo_path)
farhaanbukhsh 0163a1
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh c4f329
            tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
            tests.create_projects_git(pagure.config.config.get("GIT_FOLDER"))
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/settings")
farhaanbukhsh c4f329
farhaanbukhsh 0163a1
            # Check for a public repo
farhaanbukhsh c4f329
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 52565d
            self.assertNotIn(
Pierre-Yves Chibon 52565d
                '
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/settings")
farhaanbukhsh 0163a1
farhaanbukhsh 0163a1
            # Check for private repo
farhaanbukhsh 0163a1
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 0163a1
            self.assertIn(
Pierre-Yves Chibon bdcc9e
                '<input checked="" name="private" type="checkbox" value="private">',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 0163a1
farhaanbukhsh fc046c
            # Check the new project form has 'private' checkbox
Pierre-Yves Chibon 73d120
            output = self.app.get("/new")
farhaanbukhsh fc046c
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fc046c
            self.assertIn(
Pierre-Yves Chibon 52565d
                '<input id="private" name="private" type="checkbox" value="y">',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 73d120
    @patch("pagure.decorators.admin_session_timedout")
Pierre-Yves Chibon 52565d
    def test_private_settings_ui_update_privacy_false(self, ast):
Pierre-Yves Chibon 52565d
        """ Test UI for private repo"""
Pierre-Yves Chibon 52565d
        ast.return_value = False
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
        # Add private repo
Pierre-Yves Chibon 52565d
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 52565d
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
Pierre-Yves Chibon 52565d
            private=True,
Pierre-Yves Chibon 52565d
        )
Pierre-Yves Chibon 52565d
        self.session.add(item)
Pierre-Yves Chibon 52565d
        self.session.commit()
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
        # Add a git repo
Pierre-Yves Chibon 52565d
        repo_path = os.path.join(
Pierre-Yves Chibon 73d120
            pagure.config.config.get("GIT_FOLDER"), "test4.git"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 52565d
        pygit2.init_repository(repo_path)
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
            # Check for private repo
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/settings")
Pierre-Yves Chibon 52565d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 52565d
            self.assertIn(
Pierre-Yves Chibon bdcc9e
                '<input checked="" name="private" type="checkbox" value="private">',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 52565d
Aurélien Bompard 13bcde
            self.session.commit()
Pierre-Yves Chibon 73d120
            repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 52565d
            self.assertTrue(repo.private)
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
            # Make the project public
Pierre-Yves Chibon 52565d
            data = {
Pierre-Yves Chibon 73d120
                "description": "test project description",
Pierre-Yves Chibon 73d120
                "private": False,
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 52565d
            }
Pierre-Yves Chibon 52565d
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test4/update", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 52565d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
            self.assertIn("Project updated", output.get_data(as_text=True))
Pierre-Yves Chibon 52565d
            self.assertNotIn(
Pierre-Yves Chibon bdcc9e
                '<input checked="" name="private" type="checkbox" value="private">',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 52565d
Aurélien Bompard 13bcde
            self.session.commit()
Pierre-Yves Chibon 73d120
            repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 52565d
            self.assertFalse(repo.private)
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 73d120
    @patch("pagure.decorators.admin_session_timedout")
Pierre-Yves Chibon 52565d
    def test_private_settings_ui_update_privacy_true(self, ast):
Pierre-Yves Chibon 52565d
        """ Test UI for private repo"""
Pierre-Yves Chibon 52565d
        ast.return_value = False
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
        # Add private repo
Pierre-Yves Chibon 52565d
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 52565d
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
Pierre-Yves Chibon 52565d
            private=False,
Pierre-Yves Chibon 52565d
        )
Pierre-Yves Chibon 52565d
        self.session.add(item)
Pierre-Yves Chibon 52565d
        self.session.commit()
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
        # Add a git repo
Pierre-Yves Chibon 52565d
        repo_path = os.path.join(
Pierre-Yves Chibon 73d120
            pagure.config.config.get("GIT_FOLDER"), "test4.git"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 52565d
        pygit2.init_repository(repo_path)
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
            # Check for public repo
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/settings")
Pierre-Yves Chibon 52565d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 52565d
            self.assertNotIn(
Pierre-Yves Chibon 52565d
                '<input checked="" name="private" type="checkbox" value="private">',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 52565d
Aurélien Bompard 13bcde
            self.session.commit()
Pierre-Yves Chibon 73d120
            repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 52565d
            self.assertFalse(repo.private)
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
            # Make the project private
Pierre-Yves Chibon 52565d
            data = {
Pierre-Yves Chibon 73d120
                "description": "test project description",
Pierre-Yves Chibon 73d120
                "private": True,
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 52565d
            }
Pierre-Yves Chibon 52565d
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test4/update", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 52565d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
            self.assertIn("Project updated", output.get_data(as_text=True))
Pierre-Yves Chibon 52565d
            self.assertNotIn(
Pierre-Yves Chibon 52565d
                '<input checked="" name="private" type="checkbox" value="private">',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 52565d
Pierre-Yves Chibon 52565d
            # No change since we can't do public -> private
Aurélien Bompard 13bcde
            self.session.commit()
Pierre-Yves Chibon 73d120
            repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 52565d
            self.assertFalse(repo.private)
farhaanbukhsh 0163a1
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
farhaanbukhsh 4ea512
    def test_private_pr(self, send_email):
farhaanbukhsh c4f329
        """Test pull request made to the private repo"""
farhaanbukhsh c4f329
farhaanbukhsh 4ea512
        send_email.return_value = True
farhaanbukhsh c4f329
        # Add a private project
farhaanbukhsh c4f329
        item = pagure.lib.model.Project(
farhaanbukhsh c4f329
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="pmc",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh c4f329
            private=True,
farhaanbukhsh c4f329
        )
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        self.session.add(item)
farhaanbukhsh c4f329
        self.session.commit()
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "pmc")
farhaanbukhsh f889f7
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            session=self.session, project=repo, new_user="foo", user="pingou"
farhaanbukhsh f889f7
        )
farhaanbukhsh f889f7
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
farhaanbukhsh f889f7
farhaanbukhsh c4f329
        # Create all the git repos
farhaanbukhsh c4f329
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh c4f329
farhaanbukhsh 1432c1
        # Add a git repo
farhaanbukhsh b909de
        repo_path = os.path.join(
Pierre-Yves Chibon 73d120
            pagure.config.config.get("REQUESTS_FOLDER"), "pmc.git"
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 1432c1
        if not os.path.exists(repo_path):
farhaanbukhsh 1432c1
            os.makedirs(repo_path)
farhaanbukhsh 1432c1
        pygit2.init_repository(repo_path, bare=True)
Pierre-Yves Chibon f0babf
        # Check repo was created - Doesn't show on the public page
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/user/pingou/")
farhaanbukhsh c4f329
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c4f329
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Projects 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f0babf
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                Forks 
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 75b0c3
                  0
Pierre-Yves Chibon 75b0c3
                
Pierre-Yves Chibon 73d120
              """,
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f0babf
Pierre-Yves Chibon f0babf
            # Shows on the front page
Pierre-Yves Chibon 73d120
            output = self.app.get("/dashboard/projects")
Pierre-Yves Chibon f0babf
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c4f329
            self.assertIn(
Pierre-Yves Chibon f0babf
                """
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
              Projects 
Pierre-Yves Chibon 75b0c3
            
Pierre-Yves Chibon 75b0c3
            
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 75b0c3
                1
Pierre-Yves Chibon 75b0c3
              
Pierre-Yves Chibon 73d120
            """,
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
            self.set_up_git_repo(new_project=None, branch_from="feature")
Pierre-Yves Chibon 73d120
            project = pagure.lib.query._get_project(self.session, "pmc")
farhaanbukhsh c4f329
            self.assertEqual(len(project.requests), 1)
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
            output = self.app.get("/pmc/pull-request/1")
farhaanbukhsh c4f329
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Check repo was created
farhaanbukhsh c4f329
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/pmc/pull-requests")
farhaanbukhsh e9261e
            self.assertEqual(output.status_code, 404)
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/pmc/pull-requests")
farhaanbukhsh 1432c1
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c4f329
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/pmc/pull-requests")
farhaanbukhsh f889f7
            self.assertEqual(output.status_code, 200)
farhaanbukhsh f889f7
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
farhaanbukhsh 420a97
    def test_private_repo_issues_ui(self, p_send_email, p_ugt):
farhaanbukhsh 420a97
        """ Test issues made to private repo"""
farhaanbukhsh 420a97
        p_send_email.return_value = True
farhaanbukhsh 420a97
        p_ugt.return_value = True
farhaanbukhsh 420a97
farhaanbukhsh 420a97
        # Add private repo
farhaanbukhsh 420a97
        item = pagure.lib.model.Project(
farhaanbukhsh 420a97
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh 420a97
            private=True,
farhaanbukhsh 420a97
        )
farhaanbukhsh 420a97
        self.session.add(item)
farhaanbukhsh 420a97
        self.session.commit()
farhaanbukhsh 420a97
Pierre-Yves Chibon 73d120
        for repo in ["GIT_FOLDER", "TICKETS_FOLDER"]:
farhaanbukhsh d59922
            # Add a git repo
farhaanbukhsh d59922
            repo_path = os.path.join(
Pierre-Yves Chibon 73d120
                pagure.config.config.get(repo), "test4.git"
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh d59922
            if not os.path.exists(repo_path):
farhaanbukhsh d59922
                os.makedirs(repo_path)
farhaanbukhsh d59922
            pygit2.init_repository(repo_path)
farhaanbukhsh 420a97
farhaanbukhsh 44a97a
        # Check if the private repo issues are publicly not accesible
Pierre-Yves Chibon 73d120
        output = self.app.get("/test4/issues")
farhaanbukhsh e9261e
        self.assertEqual(output.status_code, 404)
farhaanbukhsh 420a97
farhaanbukhsh 420a97
        # Create issues to play with
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
farhaanbukhsh 420a97
            session=self.session,
farhaanbukhsh 420a97
            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",
farhaanbukhsh 420a97
        )
farhaanbukhsh 420a97
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue")
farhaanbukhsh 420a97
farhaanbukhsh 420a97
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh 420a97
farhaanbukhsh 420a97
            # Whole list
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/issues")
farhaanbukhsh e9261e
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 420a97
farhaanbukhsh 420a97
            # Check single issue
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/issue/1")
farhaanbukhsh e9261e
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 420a97
farhaanbukhsh 44a97a
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
            # Whole list
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/issues")
farhaanbukhsh 44a97a
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 44a97a
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh 420a97
farhaanbukhsh 420a97
            # Whole list
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/issues")
farhaanbukhsh 420a97
            self.assertEqual(output.status_code, 200)
farhaanbukhsh d59922
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Issues - test4 - Pagure</title>",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 420a97
            self.assertTrue(
Pierre-Yves Chibon 73d120
                ' 1 Open Issues\n'
Pierre-Yves Chibon 73d120
                in output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 420a97
farhaanbukhsh 420a97
            # Check single issue
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/issue/1")
farhaanbukhsh 420a97
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 420a97
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
farhaanbukhsh f889f7
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            session=self.session, project=repo, new_user="foo", user="pingou"
farhaanbukhsh f889f7
        )
farhaanbukhsh f889f7
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
farhaanbukhsh f889f7
Pierre-Yves Chibon 73d120
        user.username = "foo"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh f889f7
farhaanbukhsh f889f7
            # Whole list
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/issues")
farhaanbukhsh f889f7
            self.assertEqual(output.status_code, 200)
farhaanbukhsh d59922
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Issues - test4 - Pagure</title>",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh f889f7
            self.assertTrue(
Pierre-Yves Chibon 73d120
                ' 1 Open Issues\n'
Pierre-Yves Chibon 73d120
                in output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh f889f7
farhaanbukhsh f889f7
            # Check single issue
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4/issue/1")
farhaanbukhsh f889f7
            self.assertEqual(output.status_code, 200)
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 73d120
    @patch("pagure.decorators.admin_session_timedout")
Farhaan Bukhsh c03c25
    def test_private_repo_ui_for_different_repo_user(self, ast):
Farhaan Bukhsh c03c25
        """ Test the private repo for different ACLS"""
Farhaan Bukhsh c03c25
        ast.return_value = False
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        # Add private repo
Farhaan Bukhsh c03c25
        item = pagure.lib.model.Project(
Farhaan Bukhsh c03c25
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
Farhaan Bukhsh c03c25
            private=True,
Farhaan Bukhsh c03c25
        )
Farhaan Bukhsh c03c25
        self.session.add(item)
Farhaan Bukhsh c03c25
        self.session.commit()
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, "test4")
Farhaan Bukhsh c03c25
        # Add a git repo
Farhaan Bukhsh c03c25
        repo_path = os.path.join(
Pierre-Yves Chibon 73d120
            pagure.config.config.get("GIT_FOLDER"), "test4.git"
Pierre-Yves Chibon 73d120
        )
Farhaan Bukhsh c03c25
        pygit2.init_repository(repo_path)
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Farhaan Bukhsh c03c25
        with tests.user_set(self.app.application, user):
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
            # Check for private repo
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4")
Farhaan Bukhsh c03c25
            self.assertEqual(output.status_code, 200)
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        # Check if the user who doesn't have access to private repo can access it
Farhaan Bukhsh c03c25
        user = tests.FakeUser(username="foo")
Farhaan Bukhsh c03c25
        with tests.user_set(self.app.application, user):
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4")
Farhaan Bukhsh c03c25
            self.assertEqual(output.status_code, 404)
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        # Add commit access to a user
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_project(
Farhaan Bukhsh c03c25
            self.session,
Farhaan Bukhsh c03c25
            project=repo,
Farhaan Bukhsh c03c25
            new_user="foo",
Farhaan Bukhsh c03c25
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Farhaan Bukhsh c03c25
        )
Farhaan Bukhsh c03c25
        self.session.commit()
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, "test4")
Farhaan Bukhsh c03c25
        self.assertEqual(len(repo.users), 1)
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        # Check if the user can access private repo
Farhaan Bukhsh c03c25
        user = tests.FakeUser(username="foo")
Farhaan Bukhsh c03c25
        with tests.user_set(self.app.application, user):
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4")
Farhaan Bukhsh c03c25
            self.assertEqual(output.status_code, 200)
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        # Making a new user bar
Farhaan Bukhsh c03c25
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar baz",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Farhaan Bukhsh c03c25
        )
Farhaan Bukhsh c03c25
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Farhaan Bukhsh c03c25
        self.session.add(item)
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        self.session.commit()
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        # Check that bar shouldn't be able to access the project
Farhaan Bukhsh c03c25
        user = tests.FakeUser(username="bar")
Farhaan Bukhsh c03c25
        with tests.user_set(self.app.application, user):
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4")
Farhaan Bukhsh c03c25
            self.assertEqual(output.status_code, 404)
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        # Adding a ticket level access to bar
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_project(
Farhaan Bukhsh c03c25
            self.session,
Farhaan Bukhsh c03c25
            project=repo,
Farhaan Bukhsh c03c25
            new_user="bar",
Farhaan Bukhsh c03c25
            user="pingou",
Pierre-Yves Chibon 73d120
            access="ticket",
Farhaan Bukhsh c03c25
        )
Farhaan Bukhsh c03c25
        self.session.commit()
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, "test4")
Farhaan Bukhsh c03c25
        self.assertEqual(len(repo.users), 2)
Farhaan Bukhsh c03c25
Farhaan Bukhsh c03c25
        # Check if the ticket level access user can access the project
Farhaan Bukhsh c03c25
        user = tests.FakeUser(username="bar")
Farhaan Bukhsh c03c25
        with tests.user_set(self.app.application, user):
Farhaan Bukhsh c03c25
Pierre-Yves Chibon 73d120
            output = self.app.get("/test4")
Farhaan Bukhsh c03c25
            self.assertEqual(output.status_code, 200)
farhaanbukhsh f889f7
farhaanbukhsh 7ca1b1
    # API checks
farhaanbukhsh 7ca1b1
    def test_api_private_repo_projects(self):
farhaanbukhsh 7ca1b1
        """ Test api points for private repo for projects"""
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Add private repo
farhaanbukhsh 60d445
        item = pagure.lib.model.Project(
farhaanbukhsh 60d445
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh 60d445
            private=True,
farhaanbukhsh 60d445
        )
farhaanbukhsh 60d445
        self.session.add(item)
farhaanbukhsh 60d445
        self.session.commit()
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "test4.git")
farhaanbukhsh 60d445
        repo = pygit2.init_repository(gitrepo, bare=True)
farhaanbukhsh 60d445
Pierre-Yves Chibon 73d120
        newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
Pierre-Yves Chibon 73d120
        repopath = os.path.join(newpath, "repos", "test4")
farhaanbukhsh 60d445
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
farhaanbukhsh 60d445
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        clone_repo.index.add("sources")
farhaanbukhsh 60d445
        clone_repo.index.write()
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Commits the files added
farhaanbukhsh 60d445
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
farhaanbukhsh 60d445
        clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
farhaanbukhsh 60d445
            author,
farhaanbukhsh 60d445
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
farhaanbukhsh 60d445
            # binary string representing the tree object ID
farhaanbukhsh 60d445
            tree,
farhaanbukhsh 60d445
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
farhaanbukhsh 60d445
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
farhaanbukhsh 60d445
        ori_remote = clone_repo.remotes[0]
farhaanbukhsh 60d445
        PagureRepo.push(ori_remote, refname)
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Tag our first commit
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
farhaanbukhsh 60d445
        repo.create_tag(
Pierre-Yves Chibon 73d120
            "0.0.1",
Pierre-Yves Chibon 73d120
            first_commit.oid.hex,
Pierre-Yves Chibon 73d120
            pygit2.GIT_OBJ_COMMIT,
Pierre-Yves Chibon 73d120
            tagger,
Pierre-Yves Chibon 73d120
            "Release 0.0.1",
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Create a token for foo for this project
farhaanbukhsh 60d445
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="foobar_token",
farhaanbukhsh 60d445
            user_id=1,
farhaanbukhsh 60d445
            project_id=1,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
farhaanbukhsh 60d445
        )
farhaanbukhsh 60d445
        self.session.add(item)
farhaanbukhsh 60d445
        self.session.commit()
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.TokenAcl(token_id="foobar_token", acl_id=1)
farhaanbukhsh 60d445
        self.session.add(item)
farhaanbukhsh 60d445
        self.session.commit()
farhaanbukhsh 60d445
farhaanbukhsh 753ee7
        # Check if the admin requests
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh 753ee7
            # Check tags
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/git/tags")
farhaanbukhsh 753ee7
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertDictEqual(data, {"tags": ["0.0.1"], "total_tags": 1})
farhaanbukhsh 753ee7
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test4/git/tags")
farhaanbukhsh 753ee7
        self.assertEqual(output.status_code, 404)
farhaanbukhsh 60d445
farhaanbukhsh 753ee7
        # Chekc if user is not admin
farhaanbukhsh 753ee7
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/git/tags")
farhaanbukhsh 753ee7
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        shutil.rmtree(newpath)
Pierre-Yves Chibon 3604c1
farhaanbukhsh 7ca1b1
        # Check before adding
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
farhaanbukhsh 7ca1b1
        self.assertEqual(repo.tags, [])
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        # Adding a tag
Pierre-Yves Chibon 930073
        output = pagure.lib.query.update_tags(
Pierre-Yves Chibon 73d120
            self.session, repo, "infra", "pingou"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(output, ["Project tagged with: infra"])
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        # Check after adding
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
farhaanbukhsh 7ca1b1
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.tags_text, ["infra"])
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        # Check the API
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?tags=inf")
Pierre-Yves Chibon 842acb
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        del data["pagination"]
farhaanbukhsh 7ca1b1
        self.assertDictEqual(
farhaanbukhsh 7ca1b1
            data,
Pierre-Yves Chibon 842acb
            {
Pierre-Yves Chibon 73d120
                "args": {
Pierre-Yves Chibon 73d120
                    "fork": None,
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "owner": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "pattern": None,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "short": False,
Pierre-Yves Chibon 73d120
                    "tags": ["inf"],
Pierre-Yves Chibon 73d120
                    "username": None,
Pierre-Yves Chibon 842acb
                },
Pierre-Yves Chibon 73d120
                "projects": [],
Pierre-Yves Chibon 73d120
                "total_projects": 0,
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh 7ca1b1
        )
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        # Request by not a loggged in user
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?tags=infra")
Pierre-Yves Chibon 842acb
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 842acb
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        del data["pagination"]
Pierre-Yves Chibon 842acb
        self.assertDictEqual(
Pierre-Yves Chibon 842acb
            data,
Pierre-Yves Chibon 842acb
            {
Pierre-Yves Chibon 73d120
                "args": {
Pierre-Yves Chibon 73d120
                    "fork": None,
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "owner": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "pattern": None,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "short": False,
Pierre-Yves Chibon 73d120
                    "tags": ["infra"],
Pierre-Yves Chibon 73d120
                    "username": None,
Pierre-Yves Chibon 842acb
                },
Pierre-Yves Chibon 73d120
                "projects": [],
Pierre-Yves Chibon 73d120
                "total_projects": 0,
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 842acb
        )
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh 7ca1b1
            # Request by a non authorized user
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/projects?tags=infra")
Pierre-Yves Chibon 842acb
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 842acb
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            del data["pagination"]
Pierre-Yves Chibon 842acb
            self.assertDictEqual(
Pierre-Yves Chibon 842acb
                data,
Pierre-Yves Chibon 842acb
                {
Pierre-Yves Chibon 73d120
                    "args": {
Pierre-Yves Chibon 73d120
                        "fork": None,
Pierre-Yves Chibon 73d120
                        "namespace": None,
Pierre-Yves Chibon 73d120
                        "owner": None,
Pierre-Yves Chibon 73d120
                        "page": 1,
Pierre-Yves Chibon 73d120
                        "pattern": None,
Pierre-Yves Chibon 73d120
                        "per_page": 20,
Pierre-Yves Chibon 73d120
                        "short": False,
Pierre-Yves Chibon 73d120
                        "tags": ["infra"],
Pierre-Yves Chibon 73d120
                        "username": None,
Pierre-Yves Chibon 842acb
                    },
Pierre-Yves Chibon 73d120
                    "projects": [],
Pierre-Yves Chibon 73d120
                    "total_projects": 0,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 842acb
            )
farhaanbukhsh 7ca1b1
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh 7ca1b1
            # Private repo username is compulsion to pass
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/projects?tags=infra")
Pierre-Yves Chibon 842acb
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 842acb
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            del data["pagination"]
Pierre-Yves Chibon 842acb
            self.assertDictEqual(
Pierre-Yves Chibon 842acb
                data,
Pierre-Yves Chibon 842acb
                {
Pierre-Yves Chibon 73d120
                    "args": {
Pierre-Yves Chibon 73d120
                        "fork": None,
Pierre-Yves Chibon 73d120
                        "namespace": None,
Pierre-Yves Chibon 73d120
                        "owner": None,
Pierre-Yves Chibon 73d120
                        "page": 1,
Pierre-Yves Chibon 73d120
                        "pattern": None,
Pierre-Yves Chibon 73d120
                        "per_page": 20,
Pierre-Yves Chibon 73d120
                        "short": False,
Pierre-Yves Chibon 73d120
                        "tags": ["infra"],
Pierre-Yves Chibon 73d120
                        "username": None,
Pierre-Yves Chibon 842acb
                    },
Pierre-Yves Chibon 73d120
                    "projects": [],
Pierre-Yves Chibon 73d120
                    "total_projects": 0,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 842acb
            )
farhaanbukhsh 7ca1b1
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/projects?username=pingou")
farhaanbukhsh 7ca1b1
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["projects"][0]["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
            data["projects"][0]["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
            del data["pagination"]
farhaanbukhsh 7ca1b1
            self.assertDictEqual(
farhaanbukhsh 7ca1b1
                data,
farhaanbukhsh 7ca1b1
                {
Farhaan Bukhsh 8aa0f5
                    "args": {
Farhaan Bukhsh 8aa0f5
                        "fork": None,
Matt Prahl 091bab
                        "namespace": None,
Matt Prahl 7403a2
                        "owner": None,
Karsten Hopp a5d5f1
                        "page": 1,
Farhaan Bukhsh 8aa0f5
                        "pattern": None,
Karsten Hopp a5d5f1
                        "per_page": 20,
Pierre-Yves Chibon 9993ca
                        "short": False,
Farhaan Bukhsh 8aa0f5
                        "tags": [],
Pierre-Yves Chibon 73d120
                        "username": "pingou",
Farhaan Bukhsh 8aa0f5
                    },
farhaanbukhsh 7ca1b1
                    "total_projects": 1,
farhaanbukhsh 7ca1b1
                    "projects": [
farhaanbukhsh 7ca1b1
                        {
Farhaan Bukhsh 6ccd40
                            "access_groups": {
Farhaan Bukhsh 6ccd40
                                "admin": [],
Farhaan Bukhsh 6ccd40
                                "commit": [],
Pierre-Yves Chibon 73d120
                                "ticket": [],
Farhaan Bukhsh 6ccd40
                            },
Farhaan Bukhsh 6ccd40
                            "access_users": {
Farhaan Bukhsh 6ccd40
                                "admin": [],
Farhaan Bukhsh 6ccd40
                                "commit": [],
Pierre-Yves Chibon 73d120
                                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                                "ticket": [],
Farhaan Bukhsh 6ccd40
                            },
Farhaan Bukhsh 990ffe
                            "close_status": [],
Farhaan Bukhsh 990ffe
                            "custom_keys": [],
farhaanbukhsh 7ca1b1
                            "date_created": "1436527638",
Clement Verna cd6e30
                            "date_modified": "1436527638",
farhaanbukhsh 7ca1b1
                            "description": "test project description",
farhaanbukhsh 7ca1b1
                            "id": 1,
Farhaan Bukhsh 990ffe
                            "milestones": {},
farhaanbukhsh 7ca1b1
                            "name": "test4",
Farhaan Bukhsh 7c4229
                            "fullname": "test4",
Pierre-Yves Chibon 469e24
                            "url_path": "test4",
Farhaan Bukhsh 990ffe
                            "namespace": None,
farhaanbukhsh 7ca1b1
                            "parent": None,
farhaanbukhsh 7ca1b1
                            "priorities": {},
farhaanbukhsh 7ca1b1
                            "tags": ["infra"],
Pierre-Yves Chibon 73d120
                            "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                        }
Pierre-Yves Chibon 73d120
                    ],
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh 7ca1b1
            )
farhaanbukhsh 7ca1b1
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/projects?username=pingou&tags=infra")
farhaanbukhsh 7ca1b1
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["projects"][0]["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
            data["projects"][0]["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
            del data["pagination"]
farhaanbukhsh 7ca1b1
            self.assertDictEqual(
farhaanbukhsh 7ca1b1
                data,
farhaanbukhsh 7ca1b1
                {
Farhaan Bukhsh 8aa0f5
                    "args": {
Farhaan Bukhsh 8aa0f5
                        "fork": None,
Matt Prahl 091bab
                        "namespace": None,
Matt Prahl 7403a2
                        "owner": None,
Karsten Hopp a5d5f1
                        "page": 1,
Farhaan Bukhsh 8aa0f5
                        "pattern": None,
Karsten Hopp a5d5f1
                        "per_page": 20,
Pierre-Yves Chibon 9993ca
                        "short": False,
Farhaan Bukhsh 8aa0f5
                        "tags": ["infra"],
Pierre-Yves Chibon 73d120
                        "username": "pingou",
Farhaan Bukhsh 8aa0f5
                    },
farhaanbukhsh 7ca1b1
                    "total_projects": 1,
farhaanbukhsh 7ca1b1
                    "projects": [
farhaanbukhsh 7ca1b1
                        {
Farhaan Bukhsh 6ccd40
                            "access_groups": {
Farhaan Bukhsh 6ccd40
                                "admin": [],
Farhaan Bukhsh 6ccd40
                                "commit": [],
Pierre-Yves Chibon 73d120
                                "ticket": [],
Farhaan Bukhsh 6ccd40
                            },
Farhaan Bukhsh 6ccd40
                            "access_users": {
Farhaan Bukhsh 6ccd40
                                "admin": [],
Farhaan Bukhsh 6ccd40
                                "commit": [],
Pierre-Yves Chibon 73d120
                                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                                "ticket": [],
Farhaan Bukhsh 6ccd40
                            },
Farhaan Bukhsh 990ffe
                            "close_status": [],
Farhaan Bukhsh 990ffe
                            "custom_keys": [],
farhaanbukhsh 7ca1b1
                            "date_created": "1436527638",
Clement Verna cd6e30
                            "date_modified": "1436527638",
farhaanbukhsh 7ca1b1
                            "description": "test project description",
farhaanbukhsh 7ca1b1
                            "id": 1,
Farhaan Bukhsh 990ffe
                            "milestones": {},
farhaanbukhsh 7ca1b1
                            "name": "test4",
Farhaan Bukhsh 7c4229
                            "fullname": "test4",
Pierre-Yves Chibon 469e24
                            "url_path": "test4",
Farhaan Bukhsh 990ffe
                            "namespace": None,
farhaanbukhsh 7ca1b1
                            "parent": None,
farhaanbukhsh 7ca1b1
                            "priorities": {},
farhaanbukhsh 7ca1b1
                            "tags": ["infra"],
Pierre-Yves Chibon 73d120
                            "user": {"fullname": "PY C", "name": "pingou"},
farhaanbukhsh 7ca1b1
                        }
Pierre-Yves Chibon 73d120
                    ],
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh 7ca1b1
            )
farhaanbukhsh 7ca1b1
farhaanbukhsh 44a97a
    # Api pull-request views
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
farhaanbukhsh e3c47c
    def test_api_private_repo_fork(self, send_email):
farhaanbukhsh 44a97a
        """ Test api endpoints in api/fork"""
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
        send_email.return_value = True
farhaanbukhsh fd8d2a
farhaanbukhsh 44a97a
        # Add private repo
farhaanbukhsh 44a97a
        item = pagure.lib.model.Project(
farhaanbukhsh 44a97a
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh 44a97a
            private=True,
farhaanbukhsh 44a97a
        )
farhaanbukhsh 44a97a
        self.session.add(item)
farhaanbukhsh 44a97a
        self.session.commit()
farhaanbukhsh 44a97a
farhaanbukhsh fd8d2a
        tests.create_tokens(self.session)
farhaanbukhsh fd8d2a
        tests.create_tokens_acl(self.session)
farhaanbukhsh 44a97a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh 44a97a
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
farhaanbukhsh 44a97a
            session=self.session,
farhaanbukhsh 44a97a
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
farhaanbukhsh 44a97a
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
farhaanbukhsh 44a97a
        )
farhaanbukhsh 44a97a
        self.session.commit()
farhaanbukhsh 44a97a
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
farhaanbukhsh fd8d2a
farhaanbukhsh fd8d2a
        # Check list of PR
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test4/pull-requests")
farhaanbukhsh 44a97a
        self.assertEqual(output.status_code, 404)
farhaanbukhsh 44a97a
farhaanbukhsh fd8d2a
        # Check single PR
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
farhaanbukhsh fd8d2a
        self.assertEqual(output.status_code, 404)
farhaanbukhsh fd8d2a
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh 44a97a
            # List pull-requests
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/pull-requests")
farhaanbukhsh 44a97a
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["requests"][0]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["requests"][0]["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["requests"][0]["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["requests"][0]["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["requests"][0]["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["requests"][0]["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["requests"][0]["uid"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["requests"][0]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
            for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
                self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
                data["pagination"][k] = "http://localhost..."
farhaanbukhsh 44a97a
            self.assertDictEqual(
farhaanbukhsh 44a97a
                data,
farhaanbukhsh 44a97a
                {
Farhaan Bukhsh 02f421
                    "args": {
Farhaan Bukhsh 02f421
                        "assignee": None,
Farhaan Bukhsh 02f421
                        "author": None,
Karsten Hopp 566852
                        "page": 1,
Karsten Hopp 566852
                        "per_page": 20,
Pierre-Yves Chibon 73d120
                        "status": True,
Farhaan Bukhsh 02f421
                    },
Karsten Hopp 566852
                    "pagination": {
Pierre-Yves Chibon 73d120
                        "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                        "last": "http://localhost...",
Karsten Hopp 566852
                        "next": None,
Karsten Hopp 566852
                        "page": 1,
Karsten Hopp 566852
                        "pages": 1,
Karsten Hopp 566852
                        "per_page": 20,
Pierre-Yves Chibon 73d120
                        "prev": None,
Karsten Hopp 566852
                    },
Farhaan Bukhsh 02f421
                    "requests": [
Farhaan Bukhsh 02f421
                        {
Farhaan Bukhsh 02f421
                            "assignee": None,
Farhaan Bukhsh 02f421
                            "branch": "master",
Farhaan Bukhsh 02f421
                            "branch_from": "master",
Pierre-Yves Chibon f21734
                            "cached_merge_status": "unknown",
Farhaan Bukhsh 02f421
                            "closed_at": None,
Farhaan Bukhsh 02f421
                            "closed_by": None,
Farhaan Bukhsh 02f421
                            "comments": [],
Farhaan Bukhsh 02f421
                            "commit_start": None,
Farhaan Bukhsh 02f421
                            "commit_stop": None,
Farhaan Bukhsh 02f421
                            "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                            "last_updated": "1431414800",
Farhaan Bukhsh 02f421
                            "id": 1,
Farhaan Bukhsh 02f421
                            "initial_comment": None,
Farhaan Bukhsh 02f421
                            "project": {
Farhaan Bukhsh 6ccd40
                                "access_groups": {
Farhaan Bukhsh 6ccd40
                                    "admin": [],
Farhaan Bukhsh 6ccd40
                                    "commit": [],
Pierre-Yves Chibon 73d120
                                    "ticket": [],
Farhaan Bukhsh 6ccd40
                                },
Farhaan Bukhsh 6ccd40
                                "access_users": {
Farhaan Bukhsh 6ccd40
                                    "admin": [],
Farhaan Bukhsh 6ccd40
                                    "commit": [],
Pierre-Yves Chibon 73d120
                                    "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                                    "ticket": [],
Farhaan Bukhsh 6ccd40
                                },
Farhaan Bukhsh 02f421
                                "close_status": [],
Farhaan Bukhsh 02f421
                                "custom_keys": [],
Farhaan Bukhsh 02f421
                                "date_created": "1431414800",
Clement Verna cd6e30
                                "date_modified": "1431414800",
Farhaan Bukhsh 02f421
                                "description": "test project description",
Farhaan Bukhsh 02f421
                                "id": 1,
Farhaan Bukhsh 02f421
                                "milestones": {},
Farhaan Bukhsh 02f421
                                "name": "test4",
Farhaan Bukhsh 7c4229
                                "fullname": "test4",
Pierre-Yves Chibon 469e24
                                "url_path": "test4",
Farhaan Bukhsh 02f421
                                "namespace": None,
Farhaan Bukhsh 02f421
                                "parent": None,
Farhaan Bukhsh 02f421
                                "priorities": {},
Farhaan Bukhsh 02f421
                                "tags": [],
Pierre-Yves Chibon 73d120
                                "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 02f421
                            },
Farhaan Bukhsh 02f421
                            "remote_git": None,
Farhaan Bukhsh 02f421
                            "repo_from": {
Farhaan Bukhsh 6ccd40
                                "access_groups": {
Farhaan Bukhsh 6ccd40
                                    "admin": [],
Farhaan Bukhsh 6ccd40
                                    "commit": [],
Pierre-Yves Chibon 73d120
                                    "ticket": [],
Farhaan Bukhsh 6ccd40
                                },
Farhaan Bukhsh 6ccd40
                                "access_users": {
Farhaan Bukhsh 6ccd40
                                    "admin": [],
Farhaan Bukhsh 6ccd40
                                    "commit": [],
Pierre-Yves Chibon 73d120
                                    "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                                    "ticket": [],
Farhaan Bukhsh 6ccd40
                                },
Farhaan Bukhsh 02f421
                                "close_status": [],
Farhaan Bukhsh 02f421
                                "custom_keys": [],
Farhaan Bukhsh 02f421
                                "date_created": "1431414800",
Clement Verna cd6e30
                                "date_modified": "1431414800",
Farhaan Bukhsh 02f421
                                "description": "test project description",
Farhaan Bukhsh 02f421
                                "id": 1,
Farhaan Bukhsh 02f421
                                "milestones": {},
Farhaan Bukhsh 7c4229
                                "fullname": "test4",
Pierre-Yves Chibon 469e24
                                "url_path": "test4",
Farhaan Bukhsh 02f421
                                "name": "test4",
Farhaan Bukhsh 02f421
                                "namespace": None,
Farhaan Bukhsh 02f421
                                "parent": None,
Farhaan Bukhsh 02f421
                                "priorities": {},
Farhaan Bukhsh 02f421
                                "tags": [],
Pierre-Yves Chibon 73d120
                                "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 02f421
                            },
Farhaan Bukhsh 02f421
                            "status": "Open",
Pierre-Yves Chibon 31a638
                            "tags": [],
Pierre-Yves Chibon 2233c1
                            "threshold_reached": None,
Farhaan Bukhsh 02f421
                            "title": "test pull-request",
Farhaan Bukhsh 02f421
                            "uid": "1431414800",
Farhaan Bukhsh 02f421
                            "updated_on": "1431414800",
Pierre-Yves Chibon 73d120
                            "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 02f421
                        }
Farhaan Bukhsh 02f421
                    ],
Pierre-Yves Chibon 73d120
                    "total_requests": 1,
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh 44a97a
            )
Pierre-Yves Chibon 73d120
            headers = {"Authorization": "token foobar_token"}
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
            # Access Pull-Request authenticated
farhaanbukhsh e3c47c
            output = self.app.get(
Pierre-Yves Chibon 73d120
                "/api/0/test4/pull-requests", headers=headers
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 44a97a
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data2["requests"][0]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["requests"][0]["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["requests"][0]["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["requests"][0]["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["requests"][0]["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["requests"][0]["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["requests"][0]["uid"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["requests"][0]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
            for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
                self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
                data2["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon dd03b1
farhaanbukhsh 44a97a
            self.assertDictEqual(data, data2)
farhaanbukhsh 44a97a
farhaanbukhsh fd8d2a
            # For single PR
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/pull-request/1")
farhaanbukhsh f693ef
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["uid"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["last_updated"] = "1431414800"
farhaanbukhsh f693ef
            self.assertDictEqual(
farhaanbukhsh f693ef
                data,
Farhaan Bukhsh 02f421
                {
Farhaan Bukhsh 02f421
                    "assignee": None,
Farhaan Bukhsh 02f421
                    "branch": "master",
Farhaan Bukhsh 02f421
                    "branch_from": "master",
Pierre-Yves Chibon f21734
                    "cached_merge_status": "unknown",
Farhaan Bukhsh 02f421
                    "closed_at": None,
Farhaan Bukhsh 02f421
                    "closed_by": None,
Farhaan Bukhsh 02f421
                    "comments": [],
Farhaan Bukhsh 02f421
                    "commit_start": None,
Farhaan Bukhsh 02f421
                    "commit_stop": None,
Farhaan Bukhsh 02f421
                    "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                    "last_updated": "1431414800",
Farhaan Bukhsh 02f421
                    "id": 1,
Farhaan Bukhsh 02f421
                    "initial_comment": None,
Farhaan Bukhsh 02f421
                    "project": {
Farhaan Bukhsh 6ccd40
                        "access_groups": {
Farhaan Bukhsh 6ccd40
                            "admin": [],
Farhaan Bukhsh 6ccd40
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Farhaan Bukhsh 6ccd40
                        },
Farhaan Bukhsh 6ccd40
                        "access_users": {
Farhaan Bukhsh 6ccd40
                            "admin": [],
Farhaan Bukhsh 6ccd40
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Farhaan Bukhsh 6ccd40
                        },
Farhaan Bukhsh 02f421
                        "close_status": [],
Farhaan Bukhsh 02f421
                        "custom_keys": [],
Farhaan Bukhsh 02f421
                        "date_created": "1431414800",
Clement Verna cd6e30
                        "date_modified": "1431414800",
Farhaan Bukhsh 02f421
                        "description": "test project description",
Farhaan Bukhsh 02f421
                        "id": 1,
Farhaan Bukhsh 02f421
                        "milestones": {},
Farhaan Bukhsh 02f421
                        "name": "test4",
Farhaan Bukhsh 7c4229
                        "fullname": "test4",
Pierre-Yves Chibon 469e24
                        "url_path": "test4",
Farhaan Bukhsh 02f421
                        "namespace": None,
Farhaan Bukhsh 02f421
                        "parent": None,
Farhaan Bukhsh 02f421
                        "priorities": {},
Farhaan Bukhsh 02f421
                        "tags": [],
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 02f421
                    },
Farhaan Bukhsh 02f421
                    "remote_git": None,
Farhaan Bukhsh 02f421
                    "repo_from": {
Pierre-Yves Chibon 73d120
                        "access_groups": {
Farhaan Bukhsh 6ccd40
                            "admin": [],
Farhaan Bukhsh 6ccd40
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Farhaan Bukhsh 6ccd40
                        },
Farhaan Bukhsh 6ccd40
                        "access_users": {
Farhaan Bukhsh 6ccd40
                            "admin": [],
Farhaan Bukhsh 6ccd40
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Farhaan Bukhsh 6ccd40
                        },
Farhaan Bukhsh 02f421
                        "close_status": [],
Farhaan Bukhsh 02f421
                        "custom_keys": [],
Farhaan Bukhsh 02f421
                        "date_created": "1431414800",
Clement Verna cd6e30
                        "date_modified": "1431414800",
Farhaan Bukhsh 02f421
                        "description": "test project description",
Farhaan Bukhsh 02f421
                        "id": 1,
Farhaan Bukhsh 02f421
                        "milestones": {},
Farhaan Bukhsh 02f421
                        "name": "test4",
Farhaan Bukhsh 7c4229
                        "fullname": "test4",
Pierre-Yves Chibon 469e24
                        "url_path": "test4",
Farhaan Bukhsh 02f421
                        "namespace": None,
Farhaan Bukhsh 02f421
                        "parent": None,
Farhaan Bukhsh 02f421
                        "priorities": {},
Farhaan Bukhsh 02f421
                        "tags": [],
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 02f421
                    },
Farhaan Bukhsh 02f421
                    "status": "Open",
Pierre-Yves Chibon 31a638
                    "tags": [],
Pierre-Yves Chibon 2233c1
                    "threshold_reached": None,
Farhaan Bukhsh 02f421
                    "title": "test pull-request",
Farhaan Bukhsh 02f421
                    "uid": "1431414800",
Farhaan Bukhsh 02f421
                    "updated_on": "1431414800",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh f693ef
            )
farhaanbukhsh f693ef
farhaanbukhsh f693ef
            # Access Pull-Request authenticated
farhaanbukhsh e3c47c
            output = self.app.get(
Pierre-Yves Chibon 73d120
                "/api/0/test4/pull-request/1", headers=headers
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh f693ef
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data2["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["uid"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
            data2["last_updated"] = "1431414800"
farhaanbukhsh f693ef
            self.assertDictEqual(data, data2)
farhaanbukhsh f693ef
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
farhaanbukhsh fd8d2a
    def test_api_pr_private_repo_add_comment(self, mockemail):
farhaanbukhsh fd8d2a
        """ Test the api_pull_request_add_comment method of the flask api. """
farhaanbukhsh fd8d2a
        mockemail.return_value = True
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
farhaanbukhsh fd8d2a
farhaanbukhsh fd8d2a
        # Add private repo
farhaanbukhsh fd8d2a
        item = pagure.lib.model.Project(
farhaanbukhsh fd8d2a
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh fd8d2a
            private=True,
farhaanbukhsh fd8d2a
        )
farhaanbukhsh fd8d2a
        self.session.add(item)
farhaanbukhsh fd8d2a
        self.session.commit()
farhaanbukhsh fd8d2a
        tests.create_tokens(self.session)
farhaanbukhsh fd8d2a
        tests.create_tokens_acl(self.session)
farhaanbukhsh fd8d2a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh fd8d2a
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
farhaanbukhsh fd8d2a
            session=self.session,
farhaanbukhsh fd8d2a
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
farhaanbukhsh fd8d2a
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
farhaanbukhsh fd8d2a
        )
farhaanbukhsh fd8d2a
        self.session.commit()
farhaanbukhsh fd8d2a
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
farhaanbukhsh fd8d2a
farhaanbukhsh fd8d2a
        # Check comments before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh fd8d2a
        self.assertEqual(len(request.comments), 0)
farhaanbukhsh fd8d2a
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
farhaanbukhsh fd8d2a
farhaanbukhsh fd8d2a
        # Incomplete request
farhaanbukhsh fd8d2a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh fd8d2a
        self.assertEqual(output.status_code, 400)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh fd8d2a
        self.assertDictEqual(
farhaanbukhsh fd8d2a
            data,
farhaanbukhsh fd8d2a
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
farhaanbukhsh e3c47c
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh fd8d2a
        )
farhaanbukhsh fd8d2a
farhaanbukhsh fd8d2a
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh fd8d2a
        self.assertEqual(len(request.comments), 0)
farhaanbukhsh fd8d2a
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
farhaanbukhsh fd8d2a
farhaanbukhsh fd8d2a
        # Valid request
farhaanbukhsh fd8d2a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh fd8d2a
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
farhaanbukhsh fd8d2a
farhaanbukhsh fd8d2a
        # One comment added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh fd8d2a
        self.assertEqual(len(request.comments), 1)
farhaanbukhsh fd8d2a
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
farhaanbukhsh a0d3dc
    def test_api_private_repo_pr_add_flag(self, mockemail):
farhaanbukhsh a0d3dc
        """ Test the api_pull_request_add_flag method of the flask api. """
farhaanbukhsh a0d3dc
        mockemail.return_value = True
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Add private repo
farhaanbukhsh a0d3dc
        item = pagure.lib.model.Project(
farhaanbukhsh a0d3dc
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh a0d3dc
            private=True,
farhaanbukhsh a0d3dc
        )
farhaanbukhsh a0d3dc
        self.session.add(item)
farhaanbukhsh a0d3dc
        self.session.commit()
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Add private repo
farhaanbukhsh a0d3dc
        item = pagure.lib.model.Project(
farhaanbukhsh a0d3dc
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test2",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="foo_bar",
farhaanbukhsh a0d3dc
            private=True,
farhaanbukhsh a0d3dc
        )
farhaanbukhsh a0d3dc
        self.session.add(item)
farhaanbukhsh a0d3dc
        self.session.commit()
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        tests.create_tokens(self.session)
farhaanbukhsh a0d3dc
        tests.create_tokens_acl(self.session)
farhaanbukhsh a0d3dc
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Invalid project
farhaanbukhsh a0d3dc
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh a0d3dc
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh a0d3dc
        )
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Valid token, wrong project
farhaanbukhsh a0d3dc
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(output.status_code, 401)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["error", "error_code"])
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # No input
farhaanbukhsh a0d3dc
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh a0d3dc
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
farhaanbukhsh a0d3dc
        )
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
farhaanbukhsh a0d3dc
            session=self.session,
farhaanbukhsh a0d3dc
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
farhaanbukhsh a0d3dc
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
farhaanbukhsh a0d3dc
        )
farhaanbukhsh a0d3dc
        self.session.commit()
farhaanbukhsh a0d3dc
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Check comments before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(len(request.flags), 0)
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 100,
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
farhaanbukhsh a0d3dc
        }
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Incomplete request
farhaanbukhsh a0d3dc
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(output.status_code, 400)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh a0d3dc
        self.assertDictEqual(
farhaanbukhsh a0d3dc
            data,
farhaanbukhsh a0d3dc
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
farhaanbukhsh e3c47c
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh a0d3dc
        )
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(len(request.flags), 0)
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 0,
Pierre-Yves Chibon 73d120
            "comment": "Tests failed",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
farhaanbukhsh a0d3dc
        }
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Valid request
farhaanbukhsh a0d3dc
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
farhaanbukhsh a0d3dc
        self.assertDictEqual(
farhaanbukhsh a0d3dc
            data,
Pierre-Yves Chibon c27d61
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests failed",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": 0,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "failure",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag added",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh a0d3dc
        )
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # One flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests failed")
farhaanbukhsh a0d3dc
        self.assertEqual(request.flags[0].percent, 0)
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # Update flag
farhaanbukhsh a0d3dc
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 100,
Pierre-Yves Chibon 73d120
            "comment": "Tests passed",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
farhaanbukhsh a0d3dc
        }
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
farhaanbukhsh a0d3dc
        self.assertDictEqual(
farhaanbukhsh a0d3dc
            data,
Pierre-Yves Chibon c27d61
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests passed",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": 100,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "success",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag updated",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh a0d3dc
        )
farhaanbukhsh a0d3dc
farhaanbukhsh a0d3dc
        # One flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh a0d3dc
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests passed")
farhaanbukhsh a0d3dc
        self.assertEqual(request.flags[0].percent, 100)
farhaanbukhsh a0d3dc
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
farhaanbukhsh 6d10ff
    def test_api_private_repo_pr_close(self, send_email):
farhaanbukhsh 6d10ff
        """ Test the api_pull_request_close method of the flask api. """
farhaanbukhsh 6d10ff
        send_email.return_value = True
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Add private repo
farhaanbukhsh 6d10ff
        item = pagure.lib.model.Project(
farhaanbukhsh 6d10ff
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh 6d10ff
            private=True,
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.add(item)
farhaanbukhsh 6d10ff
        self.session.commit()
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        tests.create_tokens(self.session)
farhaanbukhsh 6d10ff
        tests.create_tokens_acl(self.session)
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Add private repo
farhaanbukhsh 6d10ff
        item = pagure.lib.model.Project(
farhaanbukhsh 6d10ff
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test2",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="foo_bar",
farhaanbukhsh 6d10ff
            private=True,
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.add(item)
farhaanbukhsh 6d10ff
        self.session.commit()
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
farhaanbukhsh 6d10ff
            session=self.session,
farhaanbukhsh 6d10ff
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
farhaanbukhsh 6d10ff
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.commit()
farhaanbukhsh 6d10ff
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Invalid project
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 6d10ff
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Valid token, wrong project
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 401)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["error", "error_code"])
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Invalid PR
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/2/close", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 6d10ff
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Create a token for foo for this project
farhaanbukhsh 6d10ff
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="foobar_token",
farhaanbukhsh 6d10ff
            user_id=2,
farhaanbukhsh 6d10ff
            project_id=1,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.add(item)
farhaanbukhsh 6d10ff
        self.session.commit()
Farhaan Bukhsh 5d8713
        # Allow the token to close PR
Pierre-Yves Chibon 930073
        acls = pagure.lib.query.get_acls(self.session)
Farhaan Bukhsh 5d8713
        acl = None
Farhaan Bukhsh 5d8713
        for acl in acls:
Pierre-Yves Chibon 73d120
            if acl.name == "pull_request_close":
Farhaan Bukhsh 5d8713
                break
farhaanbukhsh 6d10ff
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon 73d120
            token_id="foobar_token", acl_id=acl.id
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.add(item)
farhaanbukhsh 6d10ff
        self.session.commit()
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foobar_token"}
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # User not admin
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 6d10ff
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Close PR
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Pull-request closed!"})
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Patrick Uiterwijk 02a7cc
    def test_api_private_repo_pr_merge(self, send_email):
farhaanbukhsh 6d10ff
        """ Test the api_pull_request_merge method of the flask api. """
farhaanbukhsh 6d10ff
        send_email.return_value = True
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Add private repo
farhaanbukhsh 6d10ff
        item = pagure.lib.model.Project(
farhaanbukhsh 6d10ff
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh 6d10ff
            private=True,
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.add(item)
farhaanbukhsh 6d10ff
        self.session.commit()
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test4.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test4.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        tests.create_tokens(self.session)
farhaanbukhsh 6d10ff
        tests.create_tokens_acl(self.session)
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Add private repo
farhaanbukhsh 6d10ff
        item = pagure.lib.model.Project(
farhaanbukhsh 6d10ff
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test2",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="foo_bar",
farhaanbukhsh 6d10ff
            private=True,
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.add(item)
farhaanbukhsh 6d10ff
        self.session.commit()
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
farhaanbukhsh 6d10ff
            session=self.session,
farhaanbukhsh 6d10ff
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="test",
farhaanbukhsh 6d10ff
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.commit()
farhaanbukhsh 6d10ff
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Invalid project
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 6d10ff
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Valid token, wrong project
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 401)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["error", "error_code"])
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Invalid PR
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/2/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 6d10ff
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Create a token for foo for this project
farhaanbukhsh 6d10ff
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="foobar_token",
farhaanbukhsh 6d10ff
            user_id=2,
farhaanbukhsh 6d10ff
            project_id=1,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.add(item)
farhaanbukhsh 6d10ff
        self.session.commit()
Farhaan Bukhsh 5d8713
Farhaan Bukhsh 5d8713
        # Allow the token to merge PR
Pierre-Yves Chibon 930073
        acls = pagure.lib.query.get_acls(self.session)
Farhaan Bukhsh 5d8713
        acl = None
Farhaan Bukhsh 5d8713
        for acl in acls:
Pierre-Yves Chibon 73d120
            if acl.name == "pull_request_merge":
Farhaan Bukhsh 5d8713
                break
farhaanbukhsh 6d10ff
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon 73d120
            token_id="foobar_token", acl_id=acl.id
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
        self.session.add(item)
farhaanbukhsh 6d10ff
        self.session.commit()
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foobar_token"}
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # User not admin
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 6d10ff
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh 6d10ff
        )
farhaanbukhsh 6d10ff
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh 6d10ff
farhaanbukhsh 6d10ff
        # Merge PR
farhaanbukhsh 6d10ff
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 6d10ff
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Changes merged!"})
farhaanbukhsh fd8d2a
farhaanbukhsh 48869c
    def test_api_private_repo_new_issue(self):
farhaanbukhsh 48869c
        """ Test the api_new_issue method of the flask api. """
farhaanbukhsh 48869c
        # Add private repo
farhaanbukhsh 48869c
        item = pagure.lib.model.Project(
farhaanbukhsh 48869c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh 48869c
            private=True,
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
        self.session.add(item)
farhaanbukhsh 48869c
        self.session.commit()
farhaanbukhsh 48869c
Pierre-Yves Chibon 73d120
        for repo in ["GIT_FOLDER", "TICKETS_FOLDER"]:
farhaanbukhsh 48869c
            # Add a git repo
farhaanbukhsh 48869c
            repo_path = os.path.join(
Pierre-Yves Chibon 73d120
                pagure.config.config.get(repo), "test4.git"
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh 48869c
            if not os.path.exists(repo_path):
farhaanbukhsh 48869c
                os.makedirs(repo_path)
farhaanbukhsh 48869c
            pygit2.init_repository(repo_path, bare=True)
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        tests.create_tokens(self.session)
farhaanbukhsh 48869c
        tests.create_tokens_acl(self.session)
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Add private repo
farhaanbukhsh 48869c
        item = pagure.lib.model.Project(
farhaanbukhsh 48869c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test2",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="foo_bar",
farhaanbukhsh 48869c
            private=True,
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
        self.session.add(item)
farhaanbukhsh 48869c
        self.session.commit()
farhaanbukhsh 48869c
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Valid token, wrong project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test2/new_issue", headers=headers)
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 401)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["error", "error_code"])
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # No input
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test4/new_issue", headers=headers)
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 400)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 48869c
        self.assertDictEqual(
farhaanbukhsh 48869c
            data,
farhaanbukhsh 48869c
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
farhaanbukhsh e3c47c
                "error_code": "EINVALIDREQ",
Farhaan Bukhsh 8aa0f5
                "errors": {
Farhaan Bukhsh 8aa0f5
                    "issue_content": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "title": ["This field is required."],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 48869c
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Invalid repo
farhaanbukhsh 48869c
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/new_issue", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 48869c
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Incomplete request
farhaanbukhsh 48869c
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/new_issue", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 400)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 48869c
        self.assertDictEqual(
farhaanbukhsh 48869c
            data,
farhaanbukhsh 48869c
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
farhaanbukhsh e3c47c
                "error_code": "EINVALIDREQ",
Farhaan Bukhsh 8aa0f5
                "errors": {
Farhaan Bukhsh 8aa0f5
                    "issue_content": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "title": ["This field is required."],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        data = {
Pierre-Yves Chibon 73d120
            "title": "test issue",
Pierre-Yves Chibon 73d120
            "issue_content": "This issue needs attention",
farhaanbukhsh 48869c
        }
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Valid request
farhaanbukhsh 48869c
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/new_issue", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["issue"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issue"]["last_updated"] = "1431414800"
farhaanbukhsh 48869c
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"issue": FULL_ISSUE_LIST[7], "message": "Issue created"}
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
Matt Prahl 47b044
    def test_api_private_repo_view_issues(self):
farhaanbukhsh 48869c
        """ Test the api_view_issues method of the flask api. """
farhaanbukhsh 48869c
        self.test_api_private_repo_new_issue()
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Invalid repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/foo/issues")
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 48869c
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # List all opened issues
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/issues")
farhaanbukhsh 48869c
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["issues"][0]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["issues"][0]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
            for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
                self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
                data["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon 610db7
farhaanbukhsh 48869c
            self.assertDictEqual(
farhaanbukhsh 48869c
                data,
farhaanbukhsh 48869c
                {
farhaanbukhsh e3c47c
                    "args": {
farhaanbukhsh e3c47c
                        "assignee": None,
farhaanbukhsh e3c47c
                        "author": None,
Farhaan Bukhsh e498a1
                        "milestones": [],
Farhaan Bukhsh e498a1
                        "no_stones": None,
Matt Prahl 47b044
                        "order": None,
Farhaan Bukhsh e498a1
                        "priority": None,
Farhaan Bukhsh 8aa0f5
                        "since": None,
farhaanbukhsh e3c47c
                        "status": None,
Pierre-Yves Chibon 73d120
                        "tags": [],
farhaanbukhsh e3c47c
                    },
farhaanbukhsh e3c47c
                    "total_issues": 1,
farhaanbukhsh e3c47c
                    "issues": [
farhaanbukhsh e3c47c
                        {
farhaanbukhsh e3c47c
                            "assignee": None,
farhaanbukhsh e3c47c
                            "blocks": [],
Farhaan Bukhsh 8c7649
                            "close_status": None,
farhaanbukhsh 87bf09
                            "closed_at": None,
Clement Verna 81c130
                            "closed_by": None,
farhaanbukhsh e3c47c
                            "comments": [],
farhaanbukhsh e3c47c
                            "content": "This issue needs attention",
Farhaan Bukhsh 8c7649
                            "custom_fields": [],
farhaanbukhsh e3c47c
                            "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                            "last_updated": "1431414800",
farhaanbukhsh e3c47c
                            "depends": [],
farhaanbukhsh e3c47c
                            "id": 1,
Pierre-Yves Chibon 2521db
                            "milestone": None,
farhaanbukhsh e3c47c
                            "priority": None,
farhaanbukhsh e3c47c
                            "private": False,
farhaanbukhsh e3c47c
                            "status": "Open",
farhaanbukhsh e3c47c
                            "tags": [],
farhaanbukhsh e3c47c
                            "title": "test issue",
Pierre-Yves Chibon 73d120
                            "user": {"fullname": "PY C", "name": "pingou"},
farhaanbukhsh e3c47c
                        }
Pierre-Yves Chibon 6eccc3
                    ],
Pierre-Yves Chibon 73d120
                    "pagination": {
Pierre-Yves Chibon 73d120
                        "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                        "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                        "next": None,
Pierre-Yves Chibon 73d120
                        "page": 1,
Pierre-Yves Chibon 73d120
                        "pages": 1,
Pierre-Yves Chibon 73d120
                        "per_page": 20,
Pierre-Yves Chibon 73d120
                        "prev": None,
Pierre-Yves Chibon 6eccc3
                    },
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh 48869c
            )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Create private issue
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
farhaanbukhsh 48869c
            session=self.session,
farhaanbukhsh 48869c
            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",
farhaanbukhsh 48869c
            private=True,
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue")
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Private issues are retrieved
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/issues")
farhaanbukhsh 48869c
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["issues"][0]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["issues"][0]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["issues"][1]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["issues"][1]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
            for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
                self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
                data["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon 610db7
farhaanbukhsh 48869c
            self.assertDictEqual(
farhaanbukhsh 48869c
                data,
Farhaan Bukhsh 8c7649
                {
Farhaan Bukhsh 8c7649
                    "args": {
Farhaan Bukhsh 8c7649
                        "assignee": None,
Farhaan Bukhsh 8c7649
                        "author": None,
Farhaan Bukhsh e498a1
                        "milestones": [],
Farhaan Bukhsh e498a1
                        "no_stones": None,
Matt Prahl 47b044
                        "order": None,
Farhaan Bukhsh e498a1
                        "priority": None,
Farhaan Bukhsh 8c7649
                        "status": None,
Farhaan Bukhsh 8aa0f5
                        "since": None,
Pierre-Yves Chibon 73d120
                        "tags": [],
Farhaan Bukhsh 8c7649
                    },
Farhaan Bukhsh 8c7649
                    "issues": [
Farhaan Bukhsh 8c7649
                        {
Farhaan Bukhsh 8c7649
                            "assignee": None,
Farhaan Bukhsh 8c7649
                            "blocks": [],
Farhaan Bukhsh 8c7649
                            "close_status": None,
Farhaan Bukhsh 8c7649
                            "closed_at": None,
Clement Verna 81c130
                            "closed_by": None,
Farhaan Bukhsh 8c7649
                            "comments": [],
Farhaan Bukhsh 8c7649
                            "content": "We should work on this",
Farhaan Bukhsh 8c7649
                            "custom_fields": [],
Farhaan Bukhsh 8c7649
                            "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                            "last_updated": "1431414800",
Farhaan Bukhsh 8c7649
                            "depends": [],
Farhaan Bukhsh 8c7649
                            "id": 2,
Pierre-Yves Chibon 2521db
                            "milestone": None,
Farhaan Bukhsh 8c7649
                            "priority": None,
Farhaan Bukhsh 8c7649
                            "private": True,
Farhaan Bukhsh 8c7649
                            "status": "Open",
Farhaan Bukhsh 8c7649
                            "tags": [],
Farhaan Bukhsh 8c7649
                            "title": "Test issue",
Pierre-Yves Chibon 73d120
                            "user": {"fullname": "PY C", "name": "pingou"},
Farhaan Bukhsh 8c7649
                        },
Farhaan Bukhsh 8c7649
                        {
Farhaan Bukhsh 8c7649
                            "assignee": None,
Farhaan Bukhsh 8c7649
                            "blocks": [],
Farhaan Bukhsh 8c7649
                            "close_status": None,
Farhaan Bukhsh 8c7649
                            "closed_at": None,
Clement Verna 81c130
                            "closed_by": None,
Farhaan Bukhsh 8c7649
                            "comments": [],
Farhaan Bukhsh 8c7649
                            "content": "This issue needs attention",
Farhaan Bukhsh 8c7649
                            "custom_fields": [],
Farhaan Bukhsh 8c7649
                            "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                            "last_updated": "1431414800",
Farhaan Bukhsh 8c7649
                            "depends": [],
Farhaan Bukhsh 8c7649
                            "id": 1,
Pierre-Yves Chibon 2521db
                            "milestone": None,
Farhaan Bukhsh 8c7649
                            "priority": None,
Farhaan Bukhsh 8c7649
                            "private": False,
Farhaan Bukhsh 8c7649
                            "status": "Open",
Farhaan Bukhsh 8c7649
                            "tags": [],
Farhaan Bukhsh 8c7649
                            "title": "test issue",
Pierre-Yves Chibon 73d120
                            "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                        },
Farhaan Bukhsh 8c7649
                    ],
Pierre-Yves Chibon 73d120
                    "pagination": {
Pierre-Yves Chibon 73d120
                        "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                        "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                        "next": None,
Pierre-Yves Chibon 73d120
                        "page": 1,
Pierre-Yves Chibon 73d120
                        "pages": 1,
Pierre-Yves Chibon 73d120
                        "per_page": 20,
Pierre-Yves Chibon 73d120
                        "prev": None,
Pierre-Yves Chibon 6eccc3
                    },
Pierre-Yves Chibon 73d120
                    "total_issues": 2,
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh e3c47c
            )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Access issues authenticated but non-existing token
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbccc"}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test4/issues", headers=headers)
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 401)
farhaanbukhsh 48869c
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Access issues authenticated correctly
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test4/issues", headers=headers)
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["issues"][0]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issues"][0]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issues"][1]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issues"][1]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
        for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
            self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
            data["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon 610db7
farhaanbukhsh 48869c
        self.assertDictEqual(
farhaanbukhsh 48869c
            data,
farhaanbukhsh 48869c
            {
farhaanbukhsh e3c47c
                "args": {
farhaanbukhsh e3c47c
                    "assignee": None,
farhaanbukhsh e3c47c
                    "author": None,
Farhaan Bukhsh e498a1
                    "milestones": [],
Farhaan Bukhsh e498a1
                    "no_stones": None,
Matt Prahl 47b044
                    "order": None,
Farhaan Bukhsh e498a1
                    "priority": None,
farhaanbukhsh e3c47c
                    "status": None,
Farhaan Bukhsh 8aa0f5
                    "since": None,
Pierre-Yves Chibon 73d120
                    "tags": [],
farhaanbukhsh 48869c
                },
farhaanbukhsh e3c47c
                "issues": [
farhaanbukhsh e3c47c
                    {
farhaanbukhsh e3c47c
                        "assignee": None,
farhaanbukhsh e3c47c
                        "blocks": [],
Farhaan Bukhsh 8c7649
                        "close_status": None,
farhaanbukhsh 87bf09
                        "closed_at": None,
Clement Verna 81c130
                        "closed_by": None,
farhaanbukhsh e3c47c
                        "comments": [],
Farhaan Bukhsh 8c7649
                        "content": "We should work on this",
Farhaan Bukhsh 8c7649
                        "custom_fields": [],
farhaanbukhsh e3c47c
                        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                        "last_updated": "1431414800",
farhaanbukhsh e3c47c
                        "depends": [],
Farhaan Bukhsh 8c7649
                        "id": 2,
Pierre-Yves Chibon 2521db
                        "milestone": None,
farhaanbukhsh e3c47c
                        "priority": None,
Farhaan Bukhsh 8c7649
                        "private": True,
farhaanbukhsh e3c47c
                        "status": "Open",
farhaanbukhsh e3c47c
                        "tags": [],
Farhaan Bukhsh 8c7649
                        "title": "Test issue",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
farhaanbukhsh e3c47c
                    },
farhaanbukhsh e3c47c
                    {
farhaanbukhsh e3c47c
                        "assignee": None,
farhaanbukhsh e3c47c
                        "blocks": [],
Farhaan Bukhsh 8c7649
                        "close_status": None,
farhaanbukhsh 87bf09
                        "closed_at": None,
Clement Verna 81c130
                        "closed_by": None,
farhaanbukhsh e3c47c
                        "comments": [],
Farhaan Bukhsh 8c7649
                        "content": "This issue needs attention",
Farhaan Bukhsh 8c7649
                        "custom_fields": [],
farhaanbukhsh e3c47c
                        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                        "last_updated": "1431414800",
farhaanbukhsh e3c47c
                        "depends": [],
Farhaan Bukhsh 8c7649
                        "id": 1,
Pierre-Yves Chibon 2521db
                        "milestone": None,
farhaanbukhsh e3c47c
                        "priority": None,
Farhaan Bukhsh 8c7649
                        "private": False,
farhaanbukhsh e3c47c
                        "status": "Open",
farhaanbukhsh e3c47c
                        "tags": [],
Farhaan Bukhsh 8c7649
                        "title": "test issue",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                    },
Farhaan Bukhsh 8c7649
                ],
Pierre-Yves Chibon 73d120
                "pagination": {
Pierre-Yves Chibon 73d120
                    "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "next": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "pages": 1,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "prev": None,
Pierre-Yves Chibon 6eccc3
                },
Pierre-Yves Chibon 73d120
                "total_issues": 2,
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # List closed issue
farhaanbukhsh e3c47c
        output = self.app.get(
Pierre-Yves Chibon 73d120
            "/api/0/test4/issues?status=Closed", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
            self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
            data["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon 610db7
farhaanbukhsh 48869c
        self.assertDictEqual(
farhaanbukhsh 48869c
            data,
farhaanbukhsh 48869c
            {
farhaanbukhsh e3c47c
                "args": {
farhaanbukhsh e3c47c
                    "assignee": None,
farhaanbukhsh e3c47c
                    "author": None,
Farhaan Bukhsh e498a1
                    "milestones": [],
Farhaan Bukhsh e498a1
                    "no_stones": None,
Matt Prahl 47b044
                    "order": None,
Farhaan Bukhsh e498a1
                    "priority": None,
farhaanbukhsh e3c47c
                    "status": "Closed",
Farhaan Bukhsh 8aa0f5
                    "since": None,
Pierre-Yves Chibon 73d120
                    "tags": [],
farhaanbukhsh e3c47c
                },
Pierre-Yves Chibon 6eccc3
                "issues": [],
Pierre-Yves Chibon 73d120
                "pagination": {
Pierre-Yves Chibon 73d120
                    "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "next": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "pages": 0,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "prev": None,
Pierre-Yves Chibon 6eccc3
                },
farhaanbukhsh e3c47c
                "total_issues": 0,
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # List closed issue
farhaanbukhsh e3c47c
        output = self.app.get(
Pierre-Yves Chibon 73d120
            "/api/0/test4/issues?status=Invalid", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
            self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
            data["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon 610db7
farhaanbukhsh 48869c
        self.assertDictEqual(
farhaanbukhsh 48869c
            data,
farhaanbukhsh 48869c
            {
farhaanbukhsh e3c47c
                "args": {
farhaanbukhsh e3c47c
                    "assignee": None,
farhaanbukhsh e3c47c
                    "author": None,
Farhaan Bukhsh e498a1
                    "milestones": [],
Farhaan Bukhsh e498a1
                    "no_stones": None,
Matt Prahl 47b044
                    "order": None,
Farhaan Bukhsh e498a1
                    "priority": None,
farhaanbukhsh e3c47c
                    "status": "Invalid",
Farhaan Bukhsh 8aa0f5
                    "since": None,
Pierre-Yves Chibon 73d120
                    "tags": [],
farhaanbukhsh e3c47c
                },
Pierre-Yves Chibon 6eccc3
                "issues": [],
Pierre-Yves Chibon 73d120
                "pagination": {
Pierre-Yves Chibon 73d120
                    "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "next": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "pages": 0,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "prev": None,
Pierre-Yves Chibon 6eccc3
                },
farhaanbukhsh e3c47c
                "total_issues": 0,
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # List all issues
farhaanbukhsh e3c47c
        output = self.app.get(
Pierre-Yves Chibon 73d120
            "/api/0/test4/issues?status=All", headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["issues"][0]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issues"][0]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issues"][1]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issues"][1]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
        for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
            self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
            data["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon 610db7
farhaanbukhsh 48869c
        self.assertDictEqual(
farhaanbukhsh 48869c
            data,
farhaanbukhsh 48869c
            {
farhaanbukhsh 48869c
                "args": {
farhaanbukhsh 48869c
                    "assignee": None,
farhaanbukhsh 48869c
                    "author": None,
Farhaan Bukhsh e498a1
                    "milestones": [],
Farhaan Bukhsh e498a1
                    "no_stones": None,
Matt Prahl 47b044
                    "order": None,
Farhaan Bukhsh e498a1
                    "priority": None,
Farhaan Bukhsh 8aa0f5
                    "since": None,
farhaanbukhsh 48869c
                    "status": "All",
Pierre-Yves Chibon 73d120
                    "tags": [],
farhaanbukhsh 48869c
                },
farhaanbukhsh 48869c
                "issues": [
farhaanbukhsh 48869c
                    {
farhaanbukhsh 48869c
                        "assignee": None,
farhaanbukhsh 48869c
                        "blocks": [],
Farhaan Bukhsh 8c7649
                        "close_status": None,
farhaanbukhsh 87bf09
                        "closed_at": None,
Clement Verna 81c130
                        "closed_by": None,
farhaanbukhsh 48869c
                        "comments": [],
Farhaan Bukhsh 8c7649
                        "content": "We should work on this",
Farhaan Bukhsh 8c7649
                        "custom_fields": [],
farhaanbukhsh 48869c
                        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                        "last_updated": "1431414800",
farhaanbukhsh 48869c
                        "depends": [],
Farhaan Bukhsh 8c7649
                        "id": 2,
Pierre-Yves Chibon 2521db
                        "milestone": None,
farhaanbukhsh 48869c
                        "priority": None,
Farhaan Bukhsh 8c7649
                        "private": True,
farhaanbukhsh 48869c
                        "status": "Open",
farhaanbukhsh 48869c
                        "tags": [],
Farhaan Bukhsh 8c7649
                        "title": "Test issue",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
farhaanbukhsh 48869c
                    },
farhaanbukhsh 48869c
                    {
farhaanbukhsh 48869c
                        "assignee": None,
farhaanbukhsh 48869c
                        "blocks": [],
Farhaan Bukhsh 8c7649
                        "close_status": None,
farhaanbukhsh 87bf09
                        "closed_at": None,
Clement Verna 81c130
                        "closed_by": None,
farhaanbukhsh 48869c
                        "comments": [],
Farhaan Bukhsh 8c7649
                        "content": "This issue needs attention",
Farhaan Bukhsh 8c7649
                        "custom_fields": [],
farhaanbukhsh 48869c
                        "date_created": "1431414800",
Farhaan Bukhsh 8aa0f5
                        "last_updated": "1431414800",
farhaanbukhsh 48869c
                        "depends": [],
Farhaan Bukhsh 8c7649
                        "id": 1,
Pierre-Yves Chibon 2521db
                        "milestone": None,
farhaanbukhsh 48869c
                        "priority": None,
Farhaan Bukhsh 8c7649
                        "private": False,
farhaanbukhsh 48869c
                        "status": "Open",
farhaanbukhsh 48869c
                        "tags": [],
Farhaan Bukhsh 8c7649
                        "title": "test issue",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                    },
farhaanbukhsh 48869c
                ],
Pierre-Yves Chibon 73d120
                "pagination": {
Pierre-Yves Chibon 73d120
                    "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "next": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "pages": 1,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "prev": None,
Pierre-Yves Chibon 6eccc3
                },
Pierre-Yves Chibon 73d120
                "total_issues": 2,
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
    def test_api_pivate_repo_view_issue(self):
farhaanbukhsh 48869c
        """ Test the api_view_issue method of the flask api. """
farhaanbukhsh 48869c
        self.test_api_private_repo_new_issue()
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Invalid repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/foo/issue/1")
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 48869c
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Invalid issue for this repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test4/issue/1")
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 48869c
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh 48869c
        )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Un-authorized user
farhaanbukhsh 48869c
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/issue/1")
farhaanbukhsh 48869c
            self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
farhaanbukhsh 48869c
            self.assertDictEqual(
farhaanbukhsh 48869c
                data,
Pierre-Yves Chibon 73d120
                {"error": "Project not found", "error_code": "ENOPROJECT"},
farhaanbukhsh 48869c
            )
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Valid issue
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/issue/1")
farhaanbukhsh 48869c
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
            data["last_updated"] = "1431414800"
farhaanbukhsh 48869c
            self.assertDictEqual(
farhaanbukhsh 48869c
                data,
farhaanbukhsh 48869c
                {
farhaanbukhsh e3c47c
                    "assignee": None,
farhaanbukhsh e3c47c
                    "blocks": [],
Farhaan Bukhsh 8c7649
                    "close_status": None,
Farhaan Bukhsh 8c7649
                    "closed_at": None,
Clement Verna 81c130
                    "closed_by": None,
farhaanbukhsh e3c47c
                    "comments": [],
farhaanbukhsh e3c47c
                    "content": "This issue needs attention",
Farhaan Bukhsh 8c7649
                    "custom_fields": [],
farhaanbukhsh e3c47c
                    "date_created": "1431414800",
farhaanbukhsh e3c47c
                    "depends": [],
farhaanbukhsh e3c47c
                    "id": 1,
Farhaan Bukhsh 8aa0f5
                    "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
                    "milestone": None,
farhaanbukhsh e3c47c
                    "priority": None,
farhaanbukhsh e3c47c
                    "private": False,
farhaanbukhsh e3c47c
                    "status": "Open",
farhaanbukhsh e3c47c
                    "tags": [],
farhaanbukhsh e3c47c
                    "title": "test issue",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh 48869c
            )
farhaanbukhsh 48869c
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbccc"}
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Access issue authenticated but non-existing token
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test4/issue/1", headers=headers)
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 401)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(data.keys()), ["error", "error_code", "errors"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["errors"], "Invalid token")
farhaanbukhsh 48869c
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh 48869c
farhaanbukhsh 48869c
        # Access issue authenticated correctly
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test4/issue/1", headers=headers)
farhaanbukhsh 48869c
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1431414800"
farhaanbukhsh 48869c
        self.assertDictEqual(
farhaanbukhsh 48869c
            data,
farhaanbukhsh 48869c
            {
farhaanbukhsh e3c47c
                "assignee": None,
farhaanbukhsh e3c47c
                "blocks": [],
Farhaan Bukhsh 8c7649
                "close_status": None,
Farhaan Bukhsh 8c7649
                "closed_at": None,
Clement Verna 81c130
                "closed_by": None,
farhaanbukhsh e3c47c
                "comments": [],
farhaanbukhsh e3c47c
                "content": "This issue needs attention",
Farhaan Bukhsh 8c7649
                "custom_fields": [],
farhaanbukhsh e3c47c
                "date_created": "1431414800",
farhaanbukhsh e3c47c
                "depends": [],
farhaanbukhsh e3c47c
                "id": 1,
Farhaan Bukhsh 8aa0f5
                "last_updated": "1431414800",
Pierre-Yves Chibon 2521db
                "milestone": None,
farhaanbukhsh e3c47c
                "priority": None,
farhaanbukhsh e3c47c
                "private": False,
farhaanbukhsh e3c47c
                "status": "Open",
farhaanbukhsh e3c47c
                "tags": [],
farhaanbukhsh e3c47c
                "title": "test issue",
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh 48869c
        )
farhaanbukhsh fd8d2a
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
farhaanbukhsh f15bdf
    def test_api_private_repo_change_status_issue(self):
farhaanbukhsh bf482e
        """ Test the api_change_status_issue method of the flask api. """
farhaanbukhsh bf482e
        item = pagure.lib.model.Project(
farhaanbukhsh bf482e
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh bf482e
            private=True,
farhaanbukhsh bf482e
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
farhaanbukhsh bf482e
        self.session.add(item)
farhaanbukhsh bf482e
        self.session.commit()
farhaanbukhsh bf482e
Pierre-Yves Chibon 73d120
        for repo in ["GIT_FOLDER", "TICKETS_FOLDER"]:
farhaanbukhsh bf482e
            # Add a git repo
farhaanbukhsh bf482e
            repo_path = os.path.join(
Pierre-Yves Chibon 73d120
                pagure.config.config.get(repo), "test4.git"
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh bf482e
            if not os.path.exists(repo_path):
farhaanbukhsh bf482e
                os.makedirs(repo_path)
farhaanbukhsh bf482e
            pygit2.init_repository(repo_path, bare=True)
farhaanbukhsh bf482e
farhaanbukhsh bf482e
        tests.create_tokens(self.session)
farhaanbukhsh bf482e
        tests.create_tokens_acl(self.session)
farhaanbukhsh bf482e
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh bf482e
farhaanbukhsh bf482e
        # Invalid project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/foo/issue/1/status", headers=headers)
farhaanbukhsh bf482e
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh bf482e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh bf482e
        )
farhaanbukhsh bf482e
farhaanbukhsh bf482e
        # Valid token, wrong project
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh e3c47c
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/api/0/test2/issue/1/status", headers=headers
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh bf482e
            self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
farhaanbukhsh bf482e
            self.assertDictEqual(
farhaanbukhsh bf482e
                data,
Pierre-Yves Chibon 73d120
                {"error": "Project not found", "error_code": "ENOPROJECT"},
farhaanbukhsh bf482e
            )
farhaanbukhsh bf482e
farhaanbukhsh bf482e
        # No input
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test4/issue/1/status", headers=headers)
farhaanbukhsh bf482e
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh bf482e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Issue not found", "error_code": "ENOISSUE"}
farhaanbukhsh bf482e
        )
farhaanbukhsh bf482e
farhaanbukhsh bf482e
        # Create normal issue
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
farhaanbukhsh bf482e
            session=self.session,
farhaanbukhsh bf482e
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #1",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
farhaanbukhsh bf482e
            private=False,
farhaanbukhsh bf482e
        )
farhaanbukhsh bf482e
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #1")
farhaanbukhsh bf482e
farhaanbukhsh bf482e
        # Check status before
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 73d120
        self.assertEqual(issue.status, "Open")
farhaanbukhsh bf482e
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
farhaanbukhsh bf482e
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
farhaanbukhsh bf482e
            # Incomplete request
farhaanbukhsh bf482e
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/api/0/test4/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh bf482e
            self.assertEqual(output.status_code, 400)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
farhaanbukhsh bf482e
            self.assertDictEqual(
farhaanbukhsh bf482e
                data,
farhaanbukhsh bf482e
                {
René Genz dadd94
                    "error": "Invalid or incomplete input submitted",
farhaanbukhsh e3c47c
                    "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                    "errors": {"status": ["Not a valid choice"]},
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh bf482e
            )
farhaanbukhsh bf482e
farhaanbukhsh bf482e
            # No change
Pierre-Yves Chibon 73d120
            repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 73d120
            issue = pagure.lib.query.search_issues(
Pierre-Yves Chibon 73d120
                self.session, repo, issueid=1
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertEqual(issue.status, "Open")
farhaanbukhsh bf482e
Pierre-Yves Chibon 73d120
            data = {"status": "Open"}
farhaanbukhsh bf482e
farhaanbukhsh bf482e
            # Valid request but no change
farhaanbukhsh bf482e
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/api/0/test4/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh bf482e
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertDictEqual(data, {"message": "No changes"})
farhaanbukhsh bf482e
farhaanbukhsh bf482e
            # No change
Pierre-Yves Chibon 73d120
            repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 73d120
            issue = pagure.lib.query.search_issues(
Pierre-Yves Chibon 73d120
                self.session, repo, issueid=1
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertEqual(issue.status, "Open")
farhaanbukhsh bf482e
Pierre-Yves Chibon 73d120
            data = {"status": "Fixed"}
farhaanbukhsh bf482e
farhaanbukhsh bf482e
            # Valid request
farhaanbukhsh bf482e
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/api/0/test4/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
            )
farhaanbukhsh bf482e
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
farhaanbukhsh bf482e
            self.assertDictEqual(
farhaanbukhsh bf482e
                data,
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "message": [
Pierre-Yves Chibon 73d120
                        "Issue status updated to: Closed (was: Open)",
Pierre-Yves Chibon 73d120
                        "Issue close_status updated to: Fixed",
Pierre-Yves Chibon 73d120
                    ]
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh bf482e
            )
farhaanbukhsh bf482e
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
farhaanbukhsh f15bdf
    def test_api_private_repo_comment_issue(self, p_send_email, p_ugt):
farhaanbukhsh f15bdf
        """ Test the api_comment_issue method of the flask api. """
farhaanbukhsh f15bdf
        p_send_email.return_value = True
farhaanbukhsh f15bdf
        p_ugt.return_value = True
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        item = pagure.lib.model.Project(
farhaanbukhsh f15bdf
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test4",
Pierre-Yves Chibon 73d120
            description="test project description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeeceee",
farhaanbukhsh f15bdf
            private=True,
farhaanbukhsh f15bdf
        )
farhaanbukhsh f15bdf
        self.session.add(item)
farhaanbukhsh f15bdf
        self.session.commit()
farhaanbukhsh f15bdf
        tests.create_tokens(self.session)
farhaanbukhsh f15bdf
        tests.create_tokens_acl(self.session)
farhaanbukhsh f15bdf
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        # Invalid project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/foo/issue/1/comment", headers=headers)
farhaanbukhsh f15bdf
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh f15bdf
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
farhaanbukhsh f15bdf
        )
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        # Invalid token, right project
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbccc"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test4/issue/1/comment", headers=headers)
farhaanbukhsh f15bdf
        self.assertEqual(output.status_code, 401)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(data.keys()), ["error", "error_code", "errors"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["errors"], "Invalid token")
farhaanbukhsh f15bdf
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
farhaanbukhsh f15bdf
        # No input
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test4/issue/1/comment", headers=headers)
farhaanbukhsh f15bdf
        self.assertEqual(output.status_code, 404)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh f15bdf
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Issue not found", "error_code": "ENOISSUE"}
farhaanbukhsh f15bdf
        )
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        # Create normal issue
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
farhaanbukhsh f15bdf
            session=self.session,
farhaanbukhsh f15bdf
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #1",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
farhaanbukhsh f15bdf
            private=False,
Pierre-Yves Chibon 73d120
            issue_uid="aaabbbccc1",
farhaanbukhsh f15bdf
        )
farhaanbukhsh f15bdf
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #1")
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        # Check comments before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
farhaanbukhsh f15bdf
        self.assertEqual(len(issue.comments), 0)
farhaanbukhsh f15bdf
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        # Incomplete request
farhaanbukhsh f15bdf
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/issue/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh f15bdf
        self.assertEqual(output.status_code, 400)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
farhaanbukhsh f15bdf
        self.assertDictEqual(
farhaanbukhsh f15bdf
            data,
farhaanbukhsh f15bdf
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
farhaanbukhsh e3c47c
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh f15bdf
        )
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 73d120
        self.assertEqual(issue.status, "Open")
farhaanbukhsh f15bdf
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        # Valid request
farhaanbukhsh f15bdf
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test4/issue/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
farhaanbukhsh f15bdf
        self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
farhaanbukhsh f15bdf
        self.assertDictEqual(
farhaanbukhsh f15bdf
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "Comment added",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
farhaanbukhsh f15bdf
        )
farhaanbukhsh f15bdf
farhaanbukhsh f15bdf
        # One comment added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test4")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
farhaanbukhsh f15bdf
        self.assertEqual(len(issue.comments), 1)
farhaanbukhsh f15bdf
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
farhaanbukhsh d9fa21
    def test_api_view_issue_comment(self, p_send_email, p_ugt):
farhaanbukhsh d9fa21
        """ Test the api_view_issue_comment endpoint. """
farhaanbukhsh d9fa21
        p_send_email.return_value = True
farhaanbukhsh d9fa21
        p_ugt.return_value = True
farhaanbukhsh d9fa21
farhaanbukhsh d9fa21
        self.test_api_private_repo_comment_issue()
farhaanbukhsh d9fa21
farhaanbukhsh d9fa21
        # View a comment that does not exist
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/foo/issue/100/comment/2")
farhaanbukhsh d9fa21
        self.assertEqual(output.status_code, 404)
farhaanbukhsh d9fa21
farhaanbukhsh d9fa21
        # Issue exists but not the comment
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/issue/1/comment/2")
farhaanbukhsh d9fa21
        self.assertEqual(output.status_code, 404)
farhaanbukhsh d9fa21
farhaanbukhsh d9fa21
        # Issue and comment exists
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/issue/1/comment/1")
farhaanbukhsh d9fa21
        self.assertEqual(output.status_code, 404)
farhaanbukhsh d9fa21
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/issue/1/comment/1")
farhaanbukhsh d9fa21
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["date_created"] = "1435821770"
farhaanbukhsh d9fa21
            data["comment_date"] = "2015-07-02 09:22"
farhaanbukhsh d9fa21
            data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
farhaanbukhsh d9fa21
            self.assertDictEqual(
farhaanbukhsh d9fa21
                data,
farhaanbukhsh d9fa21
                {
farhaanbukhsh e3c47c
                    "avatar_url": "https://seccdn.libravatar.org/avatar/...",
farhaanbukhsh e3c47c
                    "comment": "This is a very interesting question",
farhaanbukhsh e3c47c
                    "comment_date": "2015-07-02 09:22",
Farhaan Bukhsh 8aa0f5
                    "notification": False,
farhaanbukhsh e3c47c
                    "date_created": "1435821770",
farhaanbukhsh e3c47c
                    "edited_on": None,
farhaanbukhsh e3c47c
                    "editor": None,
farhaanbukhsh e3c47c
                    "id": 1,
farhaanbukhsh e3c47c
                    "parent": None,
Lubomír Sedlář 59889e
                    "reactions": {},
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh d9fa21
            )
farhaanbukhsh d9fa21
farhaanbukhsh e3c47c
            # Issue and comment exists, using UID
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test4/issue/aaabbbccc1/comment/1")
farhaanbukhsh e3c47c
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c87985
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            data["date_created"] = "1435821770"
farhaanbukhsh e3c47c
            data["comment_date"] = "2015-07-02 09:22"
farhaanbukhsh e3c47c
            data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
farhaanbukhsh e3c47c
            self.assertDictEqual(
farhaanbukhsh e3c47c
                data,
farhaanbukhsh e3c47c
                {
farhaanbukhsh e3c47c
                    "avatar_url": "https://seccdn.libravatar.org/avatar/...",
farhaanbukhsh e3c47c
                    "comment": "This is a very interesting question",
farhaanbukhsh e3c47c
                    "comment_date": "2015-07-02 09:22",
Farhaan Bukhsh 8aa0f5
                    "notification": False,
farhaanbukhsh e3c47c
                    "date_created": "1435821770",
farhaanbukhsh e3c47c
                    "edited_on": None,
farhaanbukhsh e3c47c
                    "editor": None,
farhaanbukhsh e3c47c
                    "id": 1,
farhaanbukhsh e3c47c
                    "parent": None,
Lubomír Sedlář 59889e
                    "reactions": {},
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
farhaanbukhsh e3c47c
            )
farhaanbukhsh f15bdf
farhaanbukhsh bf482e
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)