Blame tests/test_pagure_flask_api_project.py

Pierre-Yves Chibon f15f18
# -*- coding: utf-8 -*-
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
"""
Pierre-Yves Chibon 6882b8
 (c) 2015-2018 - Copyright Red Hat Inc
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
 Authors:
Pierre-Yves Chibon f15f18
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Karsten Hopp be01e8
   Karsten Hopp <karsten@redhat.com></karsten@redhat.com>
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
"""
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon 6ffde7
import datetime
Pierre-Yves Chibon f15f18
import json
Pierre-Yves Chibon f15f18
import unittest
Pierre-Yves Chibon f15f18
import shutil
Pierre-Yves Chibon f15f18
import sys
Pierre-Yves Chibon f15f18
import tempfile
Pierre-Yves Chibon f15f18
import os
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
import pygit2
Aurélien Bompard 00115d
from celery.result import EagerResult
Matt Prahl 489ad1
from mock import patch, Mock
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon b130e5
import pagure.flask_app
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon f15f18
import tests
Pierre-Yves Chibon 27a73d
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
class PagureFlaskApiProjecttests(tests.Modeltests):
Pierre-Yves Chibon f15f18
    """ Tests for the flask API of pagure for issue """
Pierre-Yves Chibon f15f18
Aurélien Bompard 24a837
    def setUp(self):
Aurélien Bompard 24a837
        super(PagureFlaskApiProjecttests, self).setUp()
Aurélien Bompard 24a837
        self.gga_patcher = patch(
Pierre-Yves Chibon 73d120
            "pagure.lib.tasks.generate_gitolite_acls.delay"
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 24a837
        self.mock_gen_acls = self.gga_patcher.start()
Pierre-Yves Chibon 73d120
        task_result = EagerResult("abc-1234", True, "SUCCESS")
Aurélien Bompard 24a837
        self.mock_gen_acls.return_value = task_result
Aurélien Bompard 24a837
Aurélien Bompard 24a837
    def tearDown(self):
Aurélien Bompard 24a837
        self.gga_patcher.stop()
Aurélien Bompard 24a837
        super(PagureFlaskApiProjecttests, self).tearDown()
Aurélien Bompard 24a837
Pierre-Yves Chibon f15f18
    def test_api_git_tags(self):
Pierre-Yves Chibon f15f18
        """ Test the api_git_tags method of the flask api. """
Pierre-Yves Chibon f15f18
        tests.create_projects(self.session)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon f15f18
        repo = pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 73d120
        newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
Pierre-Yves Chibon 73d120
        repopath = os.path.join(newpath, "test")
Pierre-Yves Chibon f15f18
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        # 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")
Pierre-Yves Chibon f15f18
        clone_repo.index.write()
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        # Commits the files added
Pierre-Yves Chibon f15f18
        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")
Pierre-Yves Chibon f15f18
        clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon f15f18
            author,
Pierre-Yves Chibon f15f18
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Pierre-Yves Chibon f15f18
            # binary string representing the tree object ID
Pierre-Yves Chibon f15f18
            tree,
Pierre-Yves Chibon f15f18
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
Pierre-Yves Chibon f15f18
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon f15f18
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 27a73d
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        # 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)
Pierre-Yves Chibon f15f18
        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
        )
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 0197ec
        # Check tags
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/git/tags")
Pierre-Yves Chibon f15f18
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"tags": ["0.0.1"], "total_tags": 1})
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 06f5ad
        # Check tags with commits
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/git/tags?with_commits=True")
Pierre-Yves Chibon 06f5ad
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["tags"]["0.0.1"] = "bb8fa2aa199da08d6085e1c9badc3d83d188d38c"
Pierre-Yves Chibon 06f5ad
        self.assertDictEqual(
Pierre-Yves Chibon 06f5ad
            data,
Pierre-Yves Chibon 06f5ad
            {
Pierre-Yves Chibon 73d120
                "tags": {"0.0.1": "bb8fa2aa199da08d6085e1c9badc3d83d188d38c"},
Pierre-Yves Chibon 73d120
                "total_tags": 1,
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 06f5ad
        )
Pierre-Yves Chibon 06f5ad
Pierre-Yves Chibon 62ffe0
        shutil.rmtree(newpath)
Pierre-Yves Chibon 62ffe0
Matt Prahl cd940d
    def test_api_git_branches(self):
Matt Prahl cd940d
        """ Test the api_git_branches method of the flask api. """
Matt Prahl cd940d
        # Create a git repo to add branches to
Matt Prahl cd940d
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos", "test.git")
Matt Prahl cd940d
        tests.add_content_git_repo(repo_path)
Pierre-Yves Chibon 73d120
        new_repo_path = tempfile.mkdtemp(prefix="pagure-api-git-branches-test")
Matt Prahl cd940d
        clone_repo = pygit2.clone_repository(repo_path, new_repo_path)
Matt Prahl cd940d
Matt Prahl cd940d
        # Create two other branches based on master
Pierre-Yves Chibon 73d120
        for branch in ["pats-win-49", "pats-win-51"]:
Pierre-Yves Chibon 29ff0a
            clone_repo.create_branch(branch, clone_repo.head.peel())
Pierre-Yves Chibon 73d120
            refname = "refs/heads/{0}:refs/heads/{0}".format(branch)
Matt Prahl cd940d
            PagureRepo.push(clone_repo.remotes[0], refname)
Matt Prahl cd940d
Matt Prahl cd940d
        # Check that the branches show up on the API
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/git/branches")
Matt Prahl cd940d
        # Delete the cloned git repo after the API call
Matt Prahl cd940d
        shutil.rmtree(new_repo_path)
Matt Prahl cd940d
Matt Prahl cd940d
        # Verify the API data
Matt Prahl cd940d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Matt Prahl cd940d
        self.assertDictEqual(
Matt Prahl cd940d
            data,
Matt Prahl cd940d
            {
Pierre-Yves Chibon 73d120
                "branches": ["master", "pats-win-49", "pats-win-51"],
Pierre-Yves Chibon 73d120
                "total_branches": 3,
Pierre-Yves Chibon 73d120
            },
Matt Prahl cd940d
        )
Matt Prahl cd940d
Matt Prahl cd940d
    def test_api_git_branches_empty_repo(self):
Matt Prahl cd940d
        """ Test the api_git_branches method of the flask api when the repo is
Matt Prahl cd940d
        empty.
Matt Prahl cd940d
        """
Matt Prahl cd940d
        # Create a git repo without any branches
Matt Prahl cd940d
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo_base_path = os.path.join(self.path, "repos")
Matt Prahl cd940d
        tests.create_projects_git(repo_base_path)
Matt Prahl cd940d
Matt Prahl cd940d
        # Check that no branches show up on the API
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/git/branches")
Matt Prahl cd940d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"branches": [], "total_branches": 0})
Matt Prahl cd940d
Matt Prahl cd940d
    def test_api_git_branches_no_repo(self):
Matt Prahl cd940d
        """ Test the api_git_branches method of the flask api when there is no
Matt Prahl cd940d
        repo on a project.
Matt Prahl cd940d
        """
Matt Prahl cd940d
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/git/branches")
Matt Prahl cd940d
        self.assertEqual(output.status_code, 404)
Matt Prahl cd940d
Matt Prahl ad5147
    def test_api_git_urls(self):
Matt Prahl ad5147
        """ Test the api_project_git_urls method of the flask api.
Matt Prahl ad5147
        """
Matt Prahl ad5147
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/git/urls")
Matt Prahl ad5147
        self.assertEqual(output.status_code, 200)
Matt Prahl ad5147
        expected_rv = {
Pierre-Yves Chibon 73d120
            "urls": {
Pierre-Yves Chibon 73d120
                "git": "git://localhost.localdomain/test.git",
Pierre-Yves Chibon 73d120
                "ssh": "ssh://git@localhost.localdomain/test.git",
Matt Prahl ad5147
            },
Pierre-Yves Chibon 73d120
            "total_urls": 2,
Matt Prahl ad5147
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Matt Prahl ad5147
        self.assertDictEqual(data, expected_rv)
Matt Prahl ad5147
Matt Prahl ad5147
    def test_api_git_urls_no_project(self):
Matt Prahl ad5147
        """ Test the api_project_git_urls method of the flask api when there is
Matt Prahl ad5147
        no project.
Matt Prahl ad5147
        """
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test1234/git/urls")
Matt Prahl ad5147
        self.assertEqual(output.status_code, 404)
Matt Prahl ad5147
        expected_rv = {
Pierre-Yves Chibon 73d120
            "error": "Project not found",
Pierre-Yves Chibon 73d120
            "error_code": "ENOPROJECT",
Matt Prahl ad5147
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Matt Prahl ad5147
        self.assertDictEqual(data, expected_rv)
Matt Prahl ad5147
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"PRIVATE_PROJECTS": True})
Matt Prahl ad5147
    def test_api_git_urls_private_project(self):
Matt Prahl ad5147
        """ Test the api_project_git_urls method of the flask api when the
Matt Prahl ad5147
        project is private.
Matt Prahl ad5147
        """
Matt Prahl ad5147
        tests.create_projects(self.session)
Matt Prahl ad5147
        tests.create_tokens(self.session)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Matt Prahl ad5147
Pierre-Yves Chibon 73d120
        test_project = pagure.lib.query._get_project(self.session, "test")
Matt Prahl ad5147
        test_project.private = True
Matt Prahl ad5147
        self.session.add(test_project)
Matt Prahl ad5147
        self.session.commit()
Matt Prahl ad5147
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/git/urls", headers=headers)
Matt Prahl ad5147
        self.assertEqual(output.status_code, 200)
Matt Prahl ad5147
        expected_rv = {
Pierre-Yves Chibon 73d120
            "urls": {
Pierre-Yves Chibon 73d120
                "git": "git://localhost.localdomain/test.git",
Pierre-Yves Chibon 73d120
                "ssh": "ssh://git@localhost.localdomain/test.git",
Matt Prahl ad5147
            },
Pierre-Yves Chibon 73d120
            "total_urls": 2,
Matt Prahl ad5147
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Matt Prahl ad5147
        self.assertDictEqual(data, expected_rv)
Matt Prahl ad5147
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"PRIVATE_PROJECTS": True})
Matt Prahl ad5147
    def test_api_git_urls_private_project_no_login(self):
Matt Prahl ad5147
        """ Test the api_project_git_urls method of the flask api when the
Matt Prahl ad5147
        project is private and the user is not logged in.
Matt Prahl ad5147
        """
Matt Prahl ad5147
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        test_project = pagure.lib.query._get_project(self.session, "test")
Matt Prahl ad5147
        test_project.private = True
Matt Prahl ad5147
        self.session.add(test_project)
Matt Prahl ad5147
        self.session.commit()
Matt Prahl ad5147
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/git/urls")
Matt Prahl ad5147
        self.assertEqual(output.status_code, 404)
Matt Prahl ad5147
        expected_rv = {
Pierre-Yves Chibon 73d120
            "error": "Project not found",
Pierre-Yves Chibon 73d120
            "error_code": "ENOPROJECT",
Matt Prahl ad5147
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Matt Prahl ad5147
        self.assertDictEqual(data, expected_rv)
Matt Prahl ad5147
Pierre-Yves Chibon 1ca90b
    def test_api_projects_pattern(self):
Pierre-Yves Chibon 1ca90b
        """ Test the api_projects method of the flask api. """
Pierre-Yves Chibon 1ca90b
        tests.create_projects(self.session)
Pierre-Yves Chibon 1ca90b
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?pattern=test")
Pierre-Yves Chibon 1ca90b
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        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"]
Pierre-Yves Chibon 1ca90b
        expected_data = {
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": "test",
Pierre-Yves Chibon 73d120
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "short": False,
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "username": None,
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "projects": [
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "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
                    ],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1436527638",
Pierre-Yves Chibon 73d120
                    "date_modified": "1436527638",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                }
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 1,
Pierre-Yves Chibon 1ca90b
        }
Pierre-Yves Chibon 1ca90b
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon 1ca90b
Pierre-Yves Chibon 1ca90b
    def test_api_projects_pattern_short(self):
Pierre-Yves Chibon 1ca90b
        """ Test the api_projects method of the flask api. """
Pierre-Yves Chibon 1ca90b
        tests.create_projects(self.session)
Pierre-Yves Chibon 1ca90b
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?pattern=te*&short=1")
Pierre-Yves Chibon 1ca90b
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        del data["pagination"]
Pierre-Yves Chibon 1ca90b
        expected_data = {
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": "te*",
Pierre-Yves Chibon 73d120
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "short": True,
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "username": None,
Pierre-Yves Chibon fd7136
            },
Pierre-Yves Chibon 73d120
            "projects": [
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "description": "test project #2",
Pierre-Yves Chibon 73d120
                    "fullname": "test2",
Pierre-Yves Chibon 73d120
                    "name": "test2",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "description": "namespaced test project",
Pierre-Yves Chibon 73d120
                    "fullname": "somenamespace/test3",
Pierre-Yves Chibon 73d120
                    "name": "test3",
Pierre-Yves Chibon 73d120
                    "namespace": "somenamespace",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 3,
Pierre-Yves Chibon fd7136
        }
Pierre-Yves Chibon fd7136
        self.maxDiff = None
Pierre-Yves Chibon fd7136
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon fd7136
Pierre-Yves Chibon fd7136
    def test_api_projects_owner(self):
Pierre-Yves Chibon fd7136
        """ Test the api_projects method of the flask api. """
Pierre-Yves Chibon fd7136
        tests.create_projects(self.session)
Pierre-Yves Chibon fd7136
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?owner=foo")
Pierre-Yves Chibon fd7136
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fd7136
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        del data["pagination"]
Pierre-Yves Chibon fd7136
        expected_data = {
Pierre-Yves Chibon 73d120
            "args": {
Pierre-Yves Chibon 73d120
                "fork": None,
Pierre-Yves Chibon 73d120
                "namespace": None,
Pierre-Yves Chibon 73d120
                "owner": "foo",
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": [],
Pierre-Yves Chibon 73d120
                "username": None,
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "projects": [],
Pierre-Yves Chibon 73d120
            "total_projects": 0,
Pierre-Yves Chibon fd7136
        }
Pierre-Yves Chibon fd7136
        self.maxDiff = None
Pierre-Yves Chibon fd7136
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon fd7136
Pierre-Yves Chibon fd7136
    def test_api_projects_not_owner(self):
Pierre-Yves Chibon fd7136
        """ Test the api_projects method of the flask api. """
Pierre-Yves Chibon fd7136
        tests.create_projects(self.session)
Pierre-Yves Chibon fd7136
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?owner=!foo&short=1")
Pierre-Yves Chibon fd7136
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fd7136
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        del data["pagination"]
Pierre-Yves Chibon fd7136
        expected_data = {
Pierre-Yves Chibon 73d120
            "args": {
Pierre-Yves Chibon 73d120
                "fork": None,
Pierre-Yves Chibon 73d120
                "namespace": None,
Pierre-Yves Chibon 73d120
                "owner": "!foo",
Pierre-Yves Chibon 73d120
                "page": 1,
Pierre-Yves Chibon 73d120
                "pattern": None,
Pierre-Yves Chibon 73d120
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "short": True,
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "username": None,
Pierre-Yves Chibon 1ca90b
            },
Pierre-Yves Chibon 73d120
            "projects": [
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "description": "test project #2",
Pierre-Yves Chibon 73d120
                    "fullname": "test2",
Pierre-Yves Chibon 73d120
                    "name": "test2",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "description": "namespaced test project",
Pierre-Yves Chibon 73d120
                    "fullname": "somenamespace/test3",
Pierre-Yves Chibon 73d120
                    "name": "test3",
Pierre-Yves Chibon 73d120
                    "namespace": "somenamespace",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 3,
Pierre-Yves Chibon 1ca90b
        }
Karsten Hopp a5d5f1
        self.maxDiff = None
Pierre-Yves Chibon 1ca90b
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon 1ca90b
Pierre-Yves Chibon ceb262
    def test_api_projects(self):
Pierre-Yves Chibon ceb262
        """ Test the api_projects method of the flask api. """
Pierre-Yves Chibon ceb262
        tests.create_projects(self.session)
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Check before adding
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon ceb262
        self.assertEqual(repo.tags, [])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # 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"])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Check after adding
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon ceb262
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.tags_text, ["infra"])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Check the API
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?tags=inf")
Vivek Anand dfe82f
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Karsten Hopp a5d5f1
        null = None
Pierre-Yves Chibon 73d120
        del data["pagination"]
Pierre-Yves Chibon ceb262
        self.assertDictEqual(
Pierre-Yves Chibon ceb262
            data,
Vivek Anand dfe82f
            {
Vivek Anand dfe82f
                "total_projects": 0,
Vivek Anand dfe82f
                "projects": [],
Vivek Anand dfe82f
                "args": {
Vivek Anand dfe82f
                    "fork": None,
Vivek Anand dfe82f
                    "namespace": None,
Vivek Anand dfe82f
                    "owner": None,
Karsten Hopp a5d5f1
                    "page": 1,
Vivek Anand dfe82f
                    "pattern": None,
Karsten Hopp a5d5f1
                    "per_page": 20,
Vivek Anand dfe82f
                    "short": False,
Vivek Anand dfe82f
                    "tags": ["inf"],
Pierre-Yves Chibon 73d120
                    "username": None,
Karsten Hopp a5d5f1
                },
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon ceb262
        )
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?tags=infra")
Pierre-Yves Chibon ceb262
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        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"]
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl ada420
            "args": {
Matt Prahl ada420
                "fork": None,
Matt Prahl ada420
                "namespace": None,
Matt Prahl 7403a2
                "owner": None,
Karsten Hopp a5d5f1
                "page": 1,
Matt Prahl ada420
                "pattern": None,
Karsten Hopp a5d5f1
                "per_page": 20,
Pierre-Yves Chibon 1ca90b
                "short": False,
Matt Prahl ada420
                "tags": ["infra"],
Pierre-Yves Chibon 73d120
                "username": None,
Matt Prahl 6bf79a
            },
Pierre-Yves Chibon 73d120
            "projects": [
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "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
                    ],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1436527638",
Pierre-Yves Chibon 73d120
                    "date_modified": "1436527638",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": ["infra"],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 07a462
                }
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 1,
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Matt Prahl 6bf79a
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?owner=pingou")
Matt Prahl 7403a2
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        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
        data["projects"][1]["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["projects"][1]["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["projects"][2]["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["projects"][2]["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
        del data["pagination"]
Matt Prahl 7403a2
        expected_data = {
Matt Prahl 7403a2
            "args": {
Matt Prahl 7403a2
                "fork": None,
Matt Prahl 7403a2
                "namespace": None,
Matt Prahl 7403a2
                "owner": "pingou",
Karsten Hopp a5d5f1
                "page": 1,
Matt Prahl 7403a2
                "pattern": None,
Karsten Hopp a5d5f1
                "per_page": 20,
Pierre-Yves Chibon 1ca90b
                "short": False,
Matt Prahl 7403a2
                "tags": [],
Pierre-Yves Chibon 73d120
                "username": None,
Matt Prahl 7403a2
            },
Matt Prahl 7403a2
            "projects": [
Matt Prahl 7403a2
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 7403a2
                    "access_users": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "close_status": [
Matt Prahl 7403a2
                        "Invalid",
Matt Prahl 7403a2
                        "Insufficient data",
Matt Prahl 7403a2
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Matt Prahl 7403a2
                    ],
Matt Prahl 7403a2
                    "custom_keys": [],
Matt Prahl 7403a2
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl 7403a2
                    "description": "test project #1",
Matt Prahl 7403a2
                    "fullname": "test",
Pierre-Yves Chibon 469e24
                    "url_path": "test",
Matt Prahl 7403a2
                    "id": 1,
Matt Prahl 7403a2
                    "milestones": {},
Matt Prahl 7403a2
                    "name": "test",
Matt Prahl 7403a2
                    "namespace": None,
Matt Prahl 7403a2
                    "parent": None,
Matt Prahl 7403a2
                    "priorities": {},
Matt Prahl 7403a2
                    "tags": ["infra"],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 7403a2
                },
Matt Prahl 7403a2
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 7403a2
                    "access_users": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "close_status": [
Matt Prahl 7403a2
                        "Invalid",
Matt Prahl 7403a2
                        "Insufficient data",
Matt Prahl 7403a2
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Matt Prahl 7403a2
                    ],
Matt Prahl 7403a2
                    "custom_keys": [],
Matt Prahl 7403a2
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl 7403a2
                    "description": "test project #2",
Matt Prahl 7403a2
                    "fullname": "test2",
Pierre-Yves Chibon 469e24
                    "url_path": "test2",
Matt Prahl 7403a2
                    "id": 2,
Matt Prahl 7403a2
                    "milestones": {},
Matt Prahl 7403a2
                    "name": "test2",
Matt Prahl 7403a2
                    "namespace": None,
Matt Prahl 7403a2
                    "parent": None,
Matt Prahl 7403a2
                    "priorities": {},
Matt Prahl 7403a2
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 7403a2
                },
Matt Prahl 7403a2
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 7403a2
                    "access_users": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "close_status": [
Matt Prahl 7403a2
                        "Invalid",
Matt Prahl 7403a2
                        "Insufficient data",
Matt Prahl 7403a2
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Matt Prahl 7403a2
                    ],
Matt Prahl 7403a2
                    "custom_keys": [],
Matt Prahl 7403a2
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl 7403a2
                    "description": "namespaced test project",
Matt Prahl 7403a2
                    "fullname": "somenamespace/test3",
Pierre-Yves Chibon 469e24
                    "url_path": "somenamespace/test3",
Matt Prahl 7403a2
                    "id": 3,
Matt Prahl 7403a2
                    "milestones": {},
Matt Prahl 7403a2
                    "name": "test3",
Matt Prahl 7403a2
                    "namespace": "somenamespace",
Matt Prahl 7403a2
                    "parent": None,
Matt Prahl 7403a2
                    "priorities": {},
Matt Prahl 7403a2
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Matt Prahl 7403a2
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 3,
Matt Prahl 7403a2
        }
Matt Prahl 7403a2
        self.assertDictEqual(data, expected_data)
Matt Prahl 7403a2
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?username=pingou")
Pierre-Yves Chibon 7485e2
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        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
        data["projects"][1]["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["projects"][1]["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["projects"][2]["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["projects"][2]["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
        del data["pagination"]
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl 6bf79a
            "args": {
Pierre-Yves Chibon e33592
                "fork": None,
Matt Prahl ada420
                "namespace": None,
Matt Prahl 7403a2
                "owner": None,
Karsten Hopp a5d5f1
                "page": 1,
Pierre-Yves Chibon e33592
                "pattern": None,
Karsten Hopp a5d5f1
                "per_page": 20,
Pierre-Yves Chibon 1ca90b
                "short": False,
Pierre-Yves Chibon e33592
                "tags": [],
Pierre-Yves Chibon 73d120
                "username": "pingou",
Matt Prahl 6bf79a
            },
Matt Prahl 6bf79a
            "projects": [
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Pierre-Yves Chibon f254bf
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl 6bf79a
                    "description": "test project #1",
Matt Prahl 6bf79a
                    "fullname": "test",
Pierre-Yves Chibon 469e24
                    "url_path": "test",
Matt Prahl 6bf79a
                    "id": 1,
Matt Prahl 6bf79a
                    "milestones": {},
Matt Prahl 6bf79a
                    "name": "test",
Matt Prahl 6bf79a
                    "namespace": None,
Matt Prahl 6bf79a
                    "parent": None,
Matt Prahl 6bf79a
                    "priorities": {},
Matt Prahl 6bf79a
                    "tags": ["infra"],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 07a462
                },
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Pierre-Yves Chibon f254bf
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl 6bf79a
                    "description": "test project #2",
Matt Prahl 6bf79a
                    "fullname": "test2",
Pierre-Yves Chibon 469e24
                    "url_path": "test2",
Matt Prahl 6bf79a
                    "id": 2,
Matt Prahl 6bf79a
                    "milestones": {},
Matt Prahl 6bf79a
                    "name": "test2",
Matt Prahl 6bf79a
                    "namespace": None,
Matt Prahl 6bf79a
                    "parent": None,
Matt Prahl 6bf79a
                    "priorities": {},
Matt Prahl 6bf79a
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
clime afed57
                },
clime afed57
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
clime afed57
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl 6bf79a
                    "description": "namespaced test project",
Matt Prahl 6bf79a
                    "fullname": "somenamespace/test3",
Pierre-Yves Chibon 469e24
                    "url_path": "somenamespace/test3",
Matt Prahl 6bf79a
                    "id": 3,
Matt Prahl 6bf79a
                    "milestones": {},
Matt Prahl 6bf79a
                    "name": "test3",
Matt Prahl 6bf79a
                    "namespace": "somenamespace",
Matt Prahl 6bf79a
                    "parent": None,
Matt Prahl 6bf79a
                    "priorities": {},
Matt Prahl 6bf79a
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Matt Prahl 6bf79a
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 3,
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Matt Prahl 6bf79a
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?username=pingou&tags=infra")
Pierre-Yves Chibon 7485e2
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        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"]
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl 6bf79a
            "args": {
Pierre-Yves Chibon e33592
                "fork": None,
Matt Prahl ada420
                "namespace": None,
Matt Prahl 7403a2
                "owner": None,
Karsten Hopp a5d5f1
                "page": 1,
Pierre-Yves Chibon e33592
                "pattern": None,
Karsten Hopp a5d5f1
                "per_page": 20,
Pierre-Yves Chibon 1ca90b
                "short": False,
Matt Prahl 6bf79a
                "tags": ["infra"],
Matt Prahl 6c3d54
                "username": "pingou",
Matt Prahl 6bf79a
            },
Pierre-Yves Chibon 73d120
            "projects": [
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "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
                    ],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1436527638",
Pierre-Yves Chibon 73d120
                    "date_modified": "1436527638",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": ["infra"],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 07a462
                }
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 1,
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?namespace=somenamespace")
Matt Prahl 6c3d54
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        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"]
Matt Prahl 6c3d54
        expected_data = {
Matt Prahl 6c3d54
            "args": {
Matt Prahl 6c3d54
                "fork": None,
Matt Prahl 7403a2
                "owner": None,
Karsten Hopp a5d5f1
                "page": 1,
Matt Prahl ada420
                "namespace": "somenamespace",
Karsten Hopp a5d5f1
                "per_page": 20,
Matt Prahl 6c3d54
                "pattern": None,
Pierre-Yves Chibon 1ca90b
                "short": False,
Matt Prahl 6c3d54
                "tags": [],
Pierre-Yves Chibon 73d120
                "username": None,
Matt Prahl 6c3d54
            },
Matt Prahl 6c3d54
            "projects": [
Matt Prahl 6c3d54
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 6c3d54
                    "access_users": {
Matt Prahl 6c3d54
                        "admin": [],
Matt Prahl 6c3d54
                        "commit": [],
Matt Prahl 6c3d54
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Matt Prahl 6c3d54
                    "close_status": [
Matt Prahl 6c3d54
                        "Invalid",
Matt Prahl 6c3d54
                        "Insufficient data",
Matt Prahl 6c3d54
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Matt Prahl 6c3d54
                    ],
Matt Prahl 6c3d54
                    "custom_keys": [],
Matt Prahl 6c3d54
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl 6c3d54
                    "description": "namespaced test project",
Matt Prahl 6c3d54
                    "fullname": "somenamespace/test3",
Pierre-Yves Chibon 469e24
                    "url_path": "somenamespace/test3",
Matt Prahl 6c3d54
                    "id": 3,
Matt Prahl 6c3d54
                    "milestones": {},
Matt Prahl 6c3d54
                    "name": "test3",
Matt Prahl 6c3d54
                    "namespace": "somenamespace",
Matt Prahl 6c3d54
                    "parent": None,
Matt Prahl 6c3d54
                    "priorities": {},
Matt Prahl 6c3d54
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 6c3d54
                }
Matt Prahl 6c3d54
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 1,
Matt Prahl 6c3d54
        }
Matt Prahl 6c3d54
        self.assertDictEqual(data, expected_data)
Matt Prahl 6c3d54
Pierre-Yves Chibon b42ba1
    def test_api_project(self):
Pierre-Yves Chibon b42ba1
        """ Test the api_project method of the flask api. """
Pierre-Yves Chibon b42ba1
        tests.create_projects(self.session)
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Check before adding
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon b42ba1
        self.assertEqual(repo.tags, [])
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # 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"])
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Check after adding
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon b42ba1
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.tags_text, ["infra"])
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Check the API
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Non-existing project
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/random")
Pierre-Yves Chibon b42ba1
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b42ba1
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error_code": "ENOPROJECT", "error": "Project not found"}
Pierre-Yves Chibon b42ba1
        )
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Existing project
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test")
Pierre-Yves Chibon b42ba1
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
        expected_data = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 6bf79a
            "access_users": {
Matt Prahl 6bf79a
                "admin": [],
Matt Prahl 6bf79a
                "commit": [],
Matt Prahl 6bf79a
                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon fd3701
            "close_status": [
Pierre-Yves Chibon fd3701
                "Invalid",
Pierre-Yves Chibon fd3701
                "Insufficient data",
Pierre-Yves Chibon fd3701
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Pierre-Yves Chibon fd3701
            ],
Pierre-Yves Chibon fd3701
            "custom_keys": [],
Pierre-Yves Chibon fd3701
            "date_created": "1436527638",
Pierre-Yves Chibon fd3701
            "date_modified": "1436527638",
Pierre-Yves Chibon fd3701
            "description": "test project #1",
Pierre-Yves Chibon fd3701
            "fullname": "test",
Pierre-Yves Chibon 469e24
            "url_path": "test",
Pierre-Yves Chibon fd3701
            "id": 1,
Pierre-Yves Chibon fd3701
            "milestones": {},
Pierre-Yves Chibon fd3701
            "name": "test",
Pierre-Yves Chibon fd3701
            "namespace": None,
Pierre-Yves Chibon fd3701
            "parent": None,
Pierre-Yves Chibon fd3701
            "priorities": {},
Pierre-Yves Chibon fd3701
            "tags": ["infra"],
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon fd3701
        }
Pierre-Yves Chibon fd3701
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
    def test_api_project_group(self):
Pierre-Yves Chibon fd3701
        """ Test the api_project method of the flask api. """
Pierre-Yves Chibon fd3701
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # 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"])
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Check after adding
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon fd3701
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.tags_text, ["infra"])
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Add a group to the project
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon fd3701
            self.session,
Pierre-Yves Chibon 73d120
            group_name="some_group",
Pierre-Yves Chibon 73d120
            display_name="Some Group",
Pierre-Yves Chibon fd3701
            description=None,
Pierre-Yves Chibon 73d120
            group_type="bar",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon fd3701
            is_admin=False,
Pierre-Yves Chibon fd3701
            blacklist=[],
Pierre-Yves Chibon fd3701
        )
Pierre-Yves Chibon fd3701
        self.session.commit()
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
            self.session, group_name="some_group"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon 930073
        pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon fd3701
            self.session,
Pierre-Yves Chibon fd3701
            project,
Pierre-Yves Chibon 73d120
            new_group="some_group",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Pierre-Yves Chibon fd3701
            create=False,
Pierre-Yves Chibon 73d120
            is_admin=True,
Pierre-Yves Chibon fd3701
        )
Pierre-Yves Chibon fd3701
        self.session.commit()
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Check the API
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Existing project
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test?expand_group=1")
Pierre-Yves Chibon fd3701
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
        expected_data = {
Pierre-Yves Chibon fd3701
            "access_groups": {
Pierre-Yves Chibon fd3701
                "admin": [],
Pierre-Yves Chibon fd3701
                "commit": ["some_group"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon fd3701
            },
Pierre-Yves Chibon fd3701
            "access_users": {
Pierre-Yves Chibon fd3701
                "admin": [],
Pierre-Yves Chibon fd3701
                "commit": [],
Pierre-Yves Chibon fd3701
                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon fd3701
            "close_status": [
Pierre-Yves Chibon fd3701
                "Invalid",
Pierre-Yves Chibon fd3701
                "Insufficient data",
Pierre-Yves Chibon fd3701
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Pierre-Yves Chibon fd3701
            ],
Pierre-Yves Chibon fd3701
            "custom_keys": [],
Pierre-Yves Chibon fd3701
            "date_created": "1436527638",
Pierre-Yves Chibon fd3701
            "date_modified": "1436527638",
Pierre-Yves Chibon fd3701
            "description": "test project #1",
Pierre-Yves Chibon fd3701
            "fullname": "test",
Pierre-Yves Chibon 469e24
            "url_path": "test",
Pierre-Yves Chibon 73d120
            "group_details": {"some_group": ["foo"]},
Pierre-Yves Chibon fd3701
            "id": 1,
Pierre-Yves Chibon fd3701
            "milestones": {},
Pierre-Yves Chibon fd3701
            "name": "test",
Pierre-Yves Chibon fd3701
            "namespace": None,
Pierre-Yves Chibon fd3701
            "parent": None,
Pierre-Yves Chibon fd3701
            "priorities": {},
Pierre-Yves Chibon fd3701
            "tags": ["infra"],
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon fd3701
        }
Pierre-Yves Chibon fd3701
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
    def test_api_project_group_but_no_group(self):
Pierre-Yves Chibon fd3701
        """ Test the api_project method of the flask api when asking for
Pierre-Yves Chibon fd3701
        group details while there are none associated.
Pierre-Yves Chibon fd3701
        """
Pierre-Yves Chibon fd3701
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # 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"])
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Check after adding
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon fd3701
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.tags_text, ["infra"])
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Check the API
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Existing project
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test?expand_group=0")
Pierre-Yves Chibon fd3701
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1436527638"
Pierre-Yves Chibon 73d120
        expected_data = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon fd3701
            "access_users": {
Pierre-Yves Chibon fd3701
                "admin": [],
Pierre-Yves Chibon fd3701
                "commit": [],
Pierre-Yves Chibon fd3701
                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon 73d120
            },
Matt Prahl 6bf79a
            "close_status": [
Pierre-Yves Chibon b42ba1
                "Invalid",
Pierre-Yves Chibon b42ba1
                "Insufficient data",
Pierre-Yves Chibon b42ba1
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Matt Prahl 6bf79a
            ],
Matt Prahl 6bf79a
            "custom_keys": [],
Matt Prahl 6bf79a
            "date_created": "1436527638",
Clement Verna cd6e30
            "date_modified": "1436527638",
Matt Prahl 6bf79a
            "description": "test project #1",
Matt Prahl 6bf79a
            "fullname": "test",
Pierre-Yves Chibon 469e24
            "url_path": "test",
Matt Prahl 6bf79a
            "id": 1,
Matt Prahl 6bf79a
            "milestones": {},
Matt Prahl 6bf79a
            "name": "test",
Matt Prahl 6bf79a
            "namespace": None,
Matt Prahl 6bf79a
            "parent": None,
Matt Prahl 6bf79a
            "priorities": {},
Matt Prahl 6bf79a
            "tags": ["infra"],
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon b42ba1
Matt Prahl a01c25
    def test_api_projects_pagination(self):
Matt Prahl a01c25
        """ Test the api_projects method of the flask api with pagination. """
Matt Prahl a01c25
        tests.create_projects(self.session)
Matt Prahl a01c25
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?page=1")
Matt Prahl a01c25
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Clement Verna cd6e30
        for i in range(3):
Pierre-Yves Chibon 73d120
            data["projects"][i]["date_created"] = "1436527638"
Pierre-Yves Chibon 73d120
            data["projects"][i]["date_modified"] = "1436527638"
Matt Prahl a01c25
        expected_data = {
Matt Prahl a01c25
            "args": {
Matt Prahl a01c25
                "fork": None,
Matt Prahl a01c25
                "namespace": None,
Matt Prahl a01c25
                "owner": None,
Matt Prahl a01c25
                "page": 1,
Matt Prahl a01c25
                "per_page": 20,
Matt Prahl a01c25
                "pattern": None,
Matt Prahl a01c25
                "short": False,
Matt Prahl a01c25
                "tags": [],
Pierre-Yves Chibon 73d120
                "username": None,
Matt Prahl a01c25
            },
Matt Prahl a01c25
            "pagination": {
Matt Prahl a01c25
                "next": None,
Matt Prahl a01c25
                "page": 1,
Matt Prahl a01c25
                "pages": 1,
Matt Prahl a01c25
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "prev": None,
Matt Prahl a01c25
            },
Matt Prahl a01c25
            "projects": [
Matt Prahl a01c25
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl a01c25
                    "access_users": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Matt Prahl a01c25
                    },
Matt Prahl a01c25
                    "close_status": [
Matt Prahl a01c25
                        "Invalid",
Matt Prahl a01c25
                        "Insufficient data",
Matt Prahl a01c25
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Matt Prahl a01c25
                    ],
Matt Prahl a01c25
                    "custom_keys": [],
Matt Prahl a01c25
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl a01c25
                    "description": "test project #1",
Matt Prahl a01c25
                    "fullname": "test",
Pierre-Yves Chibon 469e24
                    "url_path": "test",
Matt Prahl a01c25
                    "id": 1,
Matt Prahl a01c25
                    "milestones": {},
Matt Prahl a01c25
                    "name": "test",
Matt Prahl a01c25
                    "namespace": None,
Matt Prahl a01c25
                    "parent": None,
Matt Prahl a01c25
                    "priorities": {},
Matt Prahl a01c25
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl a01c25
                },
Matt Prahl a01c25
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl a01c25
                    "access_users": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Matt Prahl a01c25
                    },
Matt Prahl a01c25
                    "close_status": [
Matt Prahl a01c25
                        "Invalid",
Matt Prahl a01c25
                        "Insufficient data",
Matt Prahl a01c25
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Matt Prahl a01c25
                    ],
Matt Prahl a01c25
                    "custom_keys": [],
Matt Prahl a01c25
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl a01c25
                    "description": "test project #2",
Matt Prahl a01c25
                    "fullname": "test2",
Pierre-Yves Chibon 469e24
                    "url_path": "test2",
Matt Prahl a01c25
                    "id": 2,
Matt Prahl a01c25
                    "milestones": {},
Matt Prahl a01c25
                    "name": "test2",
Matt Prahl a01c25
                    "namespace": None,
Matt Prahl a01c25
                    "parent": None,
Matt Prahl a01c25
                    "priorities": {},
Matt Prahl a01c25
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl a01c25
                },
Matt Prahl a01c25
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl a01c25
                    "access_users": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Matt Prahl a01c25
                    "close_status": [
Matt Prahl a01c25
                        "Invalid",
Matt Prahl a01c25
                        "Insufficient data",
Matt Prahl a01c25
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Matt Prahl a01c25
                    ],
Matt Prahl a01c25
                    "custom_keys": [],
Matt Prahl a01c25
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl a01c25
                    "description": "namespaced test project",
Matt Prahl a01c25
                    "fullname": "somenamespace/test3",
Pierre-Yves Chibon 469e24
                    "url_path": "somenamespace/test3",
Matt Prahl a01c25
                    "id": 3,
Matt Prahl a01c25
                    "milestones": {},
Matt Prahl a01c25
                    "name": "test3",
Matt Prahl a01c25
                    "namespace": "somenamespace",
Matt Prahl a01c25
                    "parent": None,
Matt Prahl a01c25
                    "priorities": {},
Matt Prahl a01c25
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Matt Prahl a01c25
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 3,
Matt Prahl a01c25
        }
Aurélien Bompard f61bb3
        # Test URLs
Aurélien Bompard f61bb3
        self.assertURLEqual(
Aurélien Bompard f61bb3
            data["pagination"].pop("first"),
Aurélien Bompard f61bb3
            "http://localhost/api/0/projects?per_page=20&page=1",
Aurélien Bompard f61bb3
        )
Aurélien Bompard f61bb3
        self.assertURLEqual(
Aurélien Bompard f61bb3
            data["pagination"].pop("last"),
Aurélien Bompard f61bb3
            "http://localhost/api/0/projects?per_page=20&page=1",
Aurélien Bompard f61bb3
        )
Matt Prahl a01c25
        self.assertDictEqual(data, expected_data)
Matt Prahl a01c25
Matt Prahl a01c25
    def test_api_projects_pagination_per_page(self):
Matt Prahl a01c25
        """ Test the api_projects method of the flask api with pagination and
Matt Prahl a01c25
        the `per_page` argument set. """
Matt Prahl a01c25
        tests.create_projects(self.session)
Matt Prahl a01c25
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?page=2&per_page=2")
Matt Prahl a01c25
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        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"
Matt Prahl a01c25
        expected_data = {
Matt Prahl a01c25
            "args": {
Matt Prahl a01c25
                "fork": None,
Matt Prahl a01c25
                "namespace": None,
Matt Prahl a01c25
                "owner": None,
Matt Prahl a01c25
                "page": 2,
Matt Prahl a01c25
                "per_page": 2,
Matt Prahl a01c25
                "pattern": None,
Matt Prahl a01c25
                "short": False,
Matt Prahl a01c25
                "tags": [],
Pierre-Yves Chibon 73d120
                "username": None,
Matt Prahl a01c25
            },
Pierre-Yves Chibon 73d120
            "pagination": {"next": None, "page": 2, "pages": 2, "per_page": 2},
Matt Prahl a01c25
            "projects": [
Matt Prahl a01c25
                {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl a01c25
                    "access_users": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Matt Prahl a01c25
                    },
Matt Prahl a01c25
                    "close_status": [
Matt Prahl a01c25
                        "Invalid",
Matt Prahl a01c25
                        "Insufficient data",
Matt Prahl a01c25
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Matt Prahl a01c25
                    ],
Matt Prahl a01c25
                    "custom_keys": [],
Matt Prahl a01c25
                    "date_created": "1436527638",
Clement Verna cd6e30
                    "date_modified": "1436527638",
Matt Prahl a01c25
                    "description": "namespaced test project",
Matt Prahl a01c25
                    "fullname": "somenamespace/test3",
Pierre-Yves Chibon 469e24
                    "url_path": "somenamespace/test3",
Matt Prahl a01c25
                    "id": 3,
Matt Prahl a01c25
                    "milestones": {},
Matt Prahl a01c25
                    "name": "test3",
Matt Prahl a01c25
                    "namespace": "somenamespace",
Matt Prahl a01c25
                    "parent": None,
Matt Prahl a01c25
                    "priorities": {},
Matt Prahl a01c25
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl a01c25
                }
Matt Prahl a01c25
            ],
Pierre-Yves Chibon 73d120
            "total_projects": 3,
Matt Prahl a01c25
        }
Aurélien Bompard f61bb3
        self.assertURLEqual(
Aurélien Bompard f61bb3
            data["pagination"].pop("first"),
Aurélien Bompard f61bb3
            "http://localhost/api/0/projects?per_page=2&page=1",
Aurélien Bompard f61bb3
        )
Aurélien Bompard f61bb3
        self.assertURLEqual(
Aurélien Bompard f61bb3
            data["pagination"].pop("prev"),
Aurélien Bompard f61bb3
            "http://localhost/api/0/projects?per_page=2&page=1",
Aurélien Bompard f61bb3
        )
Aurélien Bompard f61bb3
        self.assertURLEqual(
Aurélien Bompard f61bb3
            data["pagination"].pop("last"),
Aurélien Bompard f61bb3
            "http://localhost/api/0/projects?per_page=2&page=2",
Aurélien Bompard f61bb3
        )
Matt Prahl a01c25
        self.assertDictEqual(data, expected_data)
Matt Prahl a01c25
Matt Prahl a01c25
    def test_api_projects_pagination_invalid_page(self):
Matt Prahl a01c25
        """ Test the api_projects method of the flask api when an invalid page
Matt Prahl a01c25
        value is entered. """
Matt Prahl a01c25
        tests.create_projects(self.session)
Matt Prahl a01c25
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?page=-3")
Matt Prahl a01c25
        self.assertEqual(output.status_code, 400)
Matt Prahl a01c25
Matt Prahl a01c25
    def test_api_projects_pagination_invalid_page_str(self):
Matt Prahl a01c25
        """ Test the api_projects method of the flask api when an invalid type
Matt Prahl a01c25
        for the page value is entered. """
Matt Prahl a01c25
        tests.create_projects(self.session)
Matt Prahl a01c25
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?page=abcd")
Matt Prahl a01c25
        self.assertEqual(output.status_code, 400)
Matt Prahl a01c25
Matt Prahl a01c25
    def test_api_projects_pagination_invalid_per_page_too_low(self):
Matt Prahl a01c25
        """ Test the api_projects method of the flask api when a per_page
Matt Prahl a01c25
        value is below 1. """
Matt Prahl a01c25
        tests.create_projects(self.session)
Matt Prahl a01c25
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?page=1&per_page=0")
Matt Prahl a01c25
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        error = json.loads(output.get_data(as_text=True))
Matt Prahl a01c25
        self.assertEqual(
Pierre-Yves Chibon 73d120
            error["error"], "The per_page value must be between 1 and 100"
Pierre-Yves Chibon 73d120
        )
Matt Prahl a01c25
Matt Prahl a01c25
    def test_api_projects_pagination_invalid_per_page_too_high(self):
Matt Prahl a01c25
        """ Test the api_projects method of the flask api when a per_page
Matt Prahl a01c25
        value is above 100. """
Matt Prahl a01c25
        tests.create_projects(self.session)
Matt Prahl a01c25
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?page=1&per_page=101")
Matt Prahl a01c25
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        error = json.loads(output.get_data(as_text=True))
Matt Prahl a01c25
        self.assertEqual(
Pierre-Yves Chibon 73d120
            error["error"], "The per_page value must be between 1 and 100"
Pierre-Yves Chibon 73d120
        )
Matt Prahl a01c25
Matt Prahl a01c25
    def test_api_projects_pagination_invalid_per_page_str(self):
Matt Prahl a01c25
        """ Test the api_projects method of the flask api when an invalid type
Matt Prahl a01c25
        for the per_page value is entered. """
Matt Prahl a01c25
        tests.create_projects(self.session)
Matt Prahl a01c25
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?page=1&per_page=abcd")
Matt Prahl a01c25
        self.assertEqual(output.status_code, 400)
Matt Prahl a01c25
Matt Prahl a01c25
    def test_api_projects_pagination_beyond_last_page(self):
Matt Prahl a01c25
        """ Test the api_projects method of the flask api when a page value
Matt Prahl a01c25
        that is larger than the last page is entered. """
Matt Prahl a01c25
        tests.create_projects(self.session)
Matt Prahl a01c25
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/projects?page=99999")
Vivek Anand dfe82f
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard f61bb3
        self.assertURLEqual(
Aurélien Bompard f61bb3
            data["pagination"].pop("first"),
Aurélien Bompard f61bb3
            "http://localhost/api/0/projects?per_page=20&page=1",
Aurélien Bompard f61bb3
        )
Aurélien Bompard f61bb3
        self.assertURLEqual(
Aurélien Bompard f61bb3
            data["pagination"].pop("last"),
Aurélien Bompard f61bb3
            "http://localhost/api/0/projects?per_page=20&page=1",
Aurélien Bompard f61bb3
        )
Aurélien Bompard f61bb3
        self.assertURLEqual(
Aurélien Bompard f61bb3
            data["pagination"].pop("prev"),
Aurélien Bompard f61bb3
            "http://localhost/api/0/projects?per_page=20&page=99998",
Aurélien Bompard f61bb3
        )
Pierre-Yves Chibon b13c25
        self.assertEqual(
Pierre-Yves Chibon b13c25
            data,
Pierre-Yves Chibon b13c25
            {
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": 99999,
Pierre-Yves Chibon 73d120
                    "pattern": None,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "short": False,
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "username": None,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "pagination": {
Pierre-Yves Chibon 73d120
                    "next": None,
Pierre-Yves Chibon 73d120
                    "page": 99999,
Pierre-Yves Chibon 73d120
                    "pages": 1,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "projects": [],
Pierre-Yves Chibon 73d120
                "total_projects": 3,
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon b13c25
        )
Matt Prahl a01c25
Matt Prahl 30f8b1
    def test_api_modify_project_main_admin(self):
Matt Prahl ab0f46
        """ Test the api_modify_project method of the flask api when the
Matt Prahl ab0f46
        request is to change the main_admin of the project. """
Matt Prahl 30f8b1
        tests.create_projects(self.session)
Matt Prahl 30f8b1
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test", headers=headers, data={"main_admin": "foo"}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1496338274"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1496338274"
Pierre-Yves Chibon ae22c6
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon ae22c6
            "access_users": {
Pierre-Yves Chibon ae22c6
                "admin": [],
Pierre-Yves Chibon ae22c6
                "commit": [],
Pierre-Yves Chibon 73d120
                "owner": ["foo"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon ae22c6
            },
Pierre-Yves Chibon ae22c6
            "close_status": [
Pierre-Yves Chibon ae22c6
                "Invalid",
Pierre-Yves Chibon ae22c6
                "Insufficient data",
Pierre-Yves Chibon ae22c6
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Pierre-Yves Chibon ae22c6
            ],
Pierre-Yves Chibon ae22c6
            "custom_keys": [],
Pierre-Yves Chibon ae22c6
            "date_created": "1496338274",
Pierre-Yves Chibon ae22c6
            "date_modified": "1496338274",
Pierre-Yves Chibon ae22c6
            "description": "test project #1",
Pierre-Yves Chibon ae22c6
            "fullname": "test",
Pierre-Yves Chibon ae22c6
            "url_path": "test",
Pierre-Yves Chibon ae22c6
            "id": 1,
Pierre-Yves Chibon ae22c6
            "milestones": {},
Pierre-Yves Chibon ae22c6
            "name": "test",
Pierre-Yves Chibon ae22c6
            "namespace": None,
Pierre-Yves Chibon ae22c6
            "parent": None,
Pierre-Yves Chibon ae22c6
            "priorities": {},
Pierre-Yves Chibon ae22c6
            "tags": [],
Pierre-Yves Chibon ae22c6
            "user": {
Pierre-Yves Chibon ae22c6
                "default_email": "foo@bar.com",
Pierre-Yves Chibon 73d120
                "emails": ["foo@bar.com"],
Pierre-Yves Chibon ae22c6
                "fullname": "foo bar",
Pierre-Yves Chibon 73d120
                "name": "foo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
mprahl 8f40ee
mprahl 8f40ee
    def test_api_modify_project_main_admin_retain_access(self):
mprahl 8f40ee
        """ Test the api_modify_project method of the flask api when the
mprahl 8f40ee
        request is to change the main_admin of the project and retain_access
mprahl 8f40ee
        is true. """
mprahl 8f40ee
        tests.create_projects(self.session)
mprahl 8f40ee
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
mprahl 8f40ee
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test",
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data={"main_admin": "foo", "retain_access": True},
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1496338274"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1496338274"
Pierre-Yves Chibon ae22c6
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon ae22c6
            "access_users": {
Pierre-Yves Chibon 73d120
                "admin": ["pingou"],
Pierre-Yves Chibon ae22c6
                "commit": [],
Pierre-Yves Chibon 73d120
                "owner": ["foo"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon ae22c6
            },
Pierre-Yves Chibon ae22c6
            "close_status": [
Pierre-Yves Chibon ae22c6
                "Invalid",
Pierre-Yves Chibon ae22c6
                "Insufficient data",
Pierre-Yves Chibon ae22c6
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Pierre-Yves Chibon ae22c6
            ],
Pierre-Yves Chibon ae22c6
            "custom_keys": [],
Pierre-Yves Chibon ae22c6
            "date_created": "1496338274",
Pierre-Yves Chibon ae22c6
            "date_modified": "1496338274",
Pierre-Yves Chibon ae22c6
            "description": "test project #1",
Pierre-Yves Chibon ae22c6
            "fullname": "test",
Pierre-Yves Chibon ae22c6
            "url_path": "test",
Pierre-Yves Chibon ae22c6
            "id": 1,
Pierre-Yves Chibon ae22c6
            "milestones": {},
Pierre-Yves Chibon ae22c6
            "name": "test",
Pierre-Yves Chibon ae22c6
            "namespace": None,
Pierre-Yves Chibon ae22c6
            "parent": None,
Pierre-Yves Chibon ae22c6
            "priorities": {},
Pierre-Yves Chibon ae22c6
            "tags": [],
Pierre-Yves Chibon ae22c6
            "user": {
Pierre-Yves Chibon ae22c6
                "default_email": "foo@bar.com",
Pierre-Yves Chibon 73d120
                "emails": ["foo@bar.com"],
Pierre-Yves Chibon ae22c6
                "fullname": "foo bar",
Pierre-Yves Chibon 73d120
                "name": "foo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
mprahl 10dcea
mprahl 10dcea
    def test_api_modify_project_main_admin_retain_access_already_user(self):
mprahl 10dcea
        """ Test the api_modify_project method of the flask api when the
mprahl 10dcea
        request is to change the main_admin of the project and retain_access
mprahl 10dcea
        is true and the user becoming the main_admin already has access. """
mprahl 10dcea
        tests.create_projects(self.session)
mprahl 10dcea
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
mprahl 10dcea
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            project,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
mprahl 10dcea
        )
mprahl 10dcea
        self.session.commit()
mprahl 10dcea
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test",
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data={"main_admin": "foo", "retain_access": True},
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1496338274"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1496338274"
Pierre-Yves Chibon ae22c6
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon ae22c6
            "access_users": {
Pierre-Yves Chibon 73d120
                "admin": ["pingou"],
Pierre-Yves Chibon ae22c6
                "commit": [],
Pierre-Yves Chibon 73d120
                "owner": ["foo"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon ae22c6
            },
Pierre-Yves Chibon ae22c6
            "close_status": [
Pierre-Yves Chibon ae22c6
                "Invalid",
Pierre-Yves Chibon ae22c6
                "Insufficient data",
Pierre-Yves Chibon ae22c6
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Pierre-Yves Chibon ae22c6
            ],
Pierre-Yves Chibon ae22c6
            "custom_keys": [],
Pierre-Yves Chibon ae22c6
            "date_created": "1496338274",
Pierre-Yves Chibon ae22c6
            "date_modified": "1496338274",
Pierre-Yves Chibon ae22c6
            "description": "test project #1",
Pierre-Yves Chibon ae22c6
            "fullname": "test",
Pierre-Yves Chibon ae22c6
            "url_path": "test",
Pierre-Yves Chibon ae22c6
            "id": 1,
Pierre-Yves Chibon ae22c6
            "milestones": {},
Pierre-Yves Chibon ae22c6
            "name": "test",
Pierre-Yves Chibon ae22c6
            "namespace": None,
Pierre-Yves Chibon ae22c6
            "parent": None,
Pierre-Yves Chibon ae22c6
            "priorities": {},
Pierre-Yves Chibon ae22c6
            "tags": [],
Pierre-Yves Chibon ae22c6
            "user": {
Pierre-Yves Chibon ae22c6
                "default_email": "foo@bar.com",
Pierre-Yves Chibon 73d120
                "emails": ["foo@bar.com"],
Pierre-Yves Chibon ae22c6
                "fullname": "foo bar",
Pierre-Yves Chibon 73d120
                "name": "foo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
mprahl 8f40ee
mprahl 0f9ac5
    def test_api_modify_project_main_admin_json(self):
mprahl 0f9ac5
        """ Test the api_modify_project method of the flask api when the
mprahl 0f9ac5
        request is to change the main_admin of the project using JSON. """
mprahl 0f9ac5
        tests.create_projects(self.session)
mprahl 0f9ac5
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {
Pierre-Yves Chibon 73d120
            "Authorization": "token aaabbbcccddd",
Pierre-Yves Chibon 73d120
            "Content-Type": "application/json",
Pierre-Yves Chibon 73d120
        }
mprahl 0f9ac5
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test",
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=json.dumps({"main_admin": "foo"}),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1496338274"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1496338274"
Pierre-Yves Chibon ae22c6
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon ae22c6
            "access_users": {
Pierre-Yves Chibon ae22c6
                "admin": [],
Pierre-Yves Chibon ae22c6
                "commit": [],
Pierre-Yves Chibon 73d120
                "owner": ["foo"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon ae22c6
            },
Pierre-Yves Chibon ae22c6
            "close_status": [
Pierre-Yves Chibon ae22c6
                "Invalid",
Pierre-Yves Chibon ae22c6
                "Insufficient data",
Pierre-Yves Chibon ae22c6
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Pierre-Yves Chibon ae22c6
            ],
Pierre-Yves Chibon ae22c6
            "custom_keys": [],
Pierre-Yves Chibon ae22c6
            "date_created": "1496338274",
Pierre-Yves Chibon ae22c6
            "date_modified": "1496338274",
Pierre-Yves Chibon ae22c6
            "description": "test project #1",
Pierre-Yves Chibon ae22c6
            "fullname": "test",
Pierre-Yves Chibon ae22c6
            "url_path": "test",
Pierre-Yves Chibon ae22c6
            "id": 1,
Pierre-Yves Chibon ae22c6
            "milestones": {},
Pierre-Yves Chibon ae22c6
            "name": "test",
Pierre-Yves Chibon ae22c6
            "namespace": None,
Pierre-Yves Chibon ae22c6
            "parent": None,
Pierre-Yves Chibon ae22c6
            "priorities": {},
Pierre-Yves Chibon ae22c6
            "tags": [],
Pierre-Yves Chibon ae22c6
            "user": {
Pierre-Yves Chibon ae22c6
                "default_email": "foo@bar.com",
Pierre-Yves Chibon 73d120
                "emails": ["foo@bar.com"],
Pierre-Yves Chibon ae22c6
                "fullname": "foo bar",
Pierre-Yves Chibon 73d120
                "name": "foo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
Matt Prahl ab0f46
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"PAGURE_ADMIN_USERS": "foo"})
Matt Prahl ab0f46
    def test_api_modify_project_main_admin_as_site_admin(self):
Matt Prahl ab0f46
        """ Test the api_modify_project method of the flask api when the
Matt Prahl ab0f46
        request is to change the main_admin of the project and the user is a
Matt Prahl ab0f46
        Pagure site admin. """
Matt Prahl ab0f46
        tests.create_projects(self.session)
Matt Prahl ab0f46
        tests.create_tokens(self.session, user_id=2, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Matt Prahl ab0f46
Pierre-Yves Chibon 8b7097
        # date before:
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 8b7097
        date_before = project.date_modified
Pierre-Yves Chibon 8b7097
        self.assertIsNotNone(date_before)
Pierre-Yves Chibon 8b7097
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test", headers=headers, data={"main_admin": "foo"}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1496338274"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1496338274"
Pierre-Yves Chibon ae22c6
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon ae22c6
            "access_users": {
Pierre-Yves Chibon ae22c6
                "admin": [],
Pierre-Yves Chibon ae22c6
                "commit": [],
Pierre-Yves Chibon 73d120
                "owner": ["foo"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon ae22c6
            },
Pierre-Yves Chibon ae22c6
            "close_status": [
Pierre-Yves Chibon ae22c6
                "Invalid",
Pierre-Yves Chibon ae22c6
                "Insufficient data",
Pierre-Yves Chibon ae22c6
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Pierre-Yves Chibon ae22c6
            ],
Pierre-Yves Chibon ae22c6
            "custom_keys": [],
Pierre-Yves Chibon ae22c6
            "date_created": "1496338274",
Pierre-Yves Chibon ae22c6
            "date_modified": "1496338274",
Pierre-Yves Chibon ae22c6
            "description": "test project #1",
Pierre-Yves Chibon ae22c6
            "fullname": "test",
Pierre-Yves Chibon ae22c6
            "url_path": "test",
Pierre-Yves Chibon ae22c6
            "id": 1,
Pierre-Yves Chibon ae22c6
            "milestones": {},
Pierre-Yves Chibon ae22c6
            "name": "test",
Pierre-Yves Chibon ae22c6
            "namespace": None,
Pierre-Yves Chibon ae22c6
            "parent": None,
Pierre-Yves Chibon ae22c6
            "priorities": {},
Pierre-Yves Chibon ae22c6
            "tags": [],
Pierre-Yves Chibon ae22c6
            "user": {
Pierre-Yves Chibon ae22c6
                "default_email": "foo@bar.com",
Pierre-Yves Chibon 73d120
                "emails": ["foo@bar.com"],
Pierre-Yves Chibon ae22c6
                "fullname": "foo bar",
Pierre-Yves Chibon 73d120
                "name": "foo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
Matt Prahl 30f8b1
Pierre-Yves Chibon 8b7097
        # date after:
Pierre-Yves Chibon 8b7097
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 8b7097
        self.assertNotEqual(date_before, project.date_modified)
Pierre-Yves Chibon 8b7097
Matt Prahl 30f8b1
    def test_api_modify_project_main_admin_not_main_admin(self):
Matt Prahl 30f8b1
        """ Test the api_modify_project method of the flask api when the
Matt Prahl 30f8b1
        requester is not the main_admin of the project and requests to change
Matt Prahl 30f8b1
        the main_admin.
Matt Prahl 30f8b1
        """
Matt Prahl 30f8b1
        tests.create_projects(self.session)
Pierre-Yves Chibon 930073
        project_user = pagure.lib.query.model.ProjectUser(
Pierre-Yves Chibon 73d120
            project_id=1, user_id=2, access="admin"
Matt Prahl 30f8b1
        )
Matt Prahl 30f8b1
        self.session.add(project_user)
Matt Prahl 30f8b1
        self.session.commit()
Matt Prahl 30f8b1
        tests.create_tokens(self.session, project_id=None, user_id=2)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test", headers=headers, data={"main_admin": "foo"}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon ae22c6
        expected_error = {
Pierre-Yves Chibon 73d120
            "error": (
Pierre-Yves Chibon 73d120
                "Only the main admin can set the main admin of a " "project"
Pierre-Yves Chibon 73d120
            ),
Pierre-Yves Chibon 73d120
            "error_code": "ENOTMAINADMIN",
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(
Pierre-Yves Chibon 73d120
            json.loads(output.get_data(as_text=True)), expected_error
Pierre-Yves Chibon 73d120
        )
Matt Prahl 30f8b1
Matt Prahl 30f8b1
    def test_api_modify_project_not_admin(self):
Matt Prahl 30f8b1
        """ Test the api_modify_project method of the flask api when the
Matt Prahl 30f8b1
        requester is not an admin of the project.
Matt Prahl 30f8b1
        """
Matt Prahl 30f8b1
        tests.create_projects(self.session)
Matt Prahl 30f8b1
        tests.create_tokens(self.session, project_id=None, user_id=2)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test", headers=headers, data={"main_admin": "foo"}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon ae22c6
        expected_error = {
Pierre-Yves Chibon 73d120
            "error": "You are not allowed to modify this project",
Pierre-Yves Chibon 73d120
            "error_code": "EMODIFYPROJECTNOTALLOWED",
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(
Pierre-Yves Chibon 73d120
            json.loads(output.get_data(as_text=True)), expected_error
Pierre-Yves Chibon 73d120
        )
Matt Prahl 30f8b1
Matt Prahl 30f8b1
    def test_api_modify_project_invalid_request(self):
Matt Prahl 30f8b1
        """ Test the api_modify_project method of the flask api when the
Matt Prahl 30f8b1
        request data is invalid.
Matt Prahl 30f8b1
        """
Matt Prahl 30f8b1
        tests.create_projects(self.session)
Matt Prahl 30f8b1
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 73d120
        output = self.app.patch("/api/0/test", headers=headers, data="invalid")
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon ae22c6
        expected_error = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(
Pierre-Yves Chibon 73d120
            json.loads(output.get_data(as_text=True)), expected_error
Pierre-Yves Chibon 73d120
        )
Matt Prahl 30f8b1
Matt Prahl 30f8b1
    def test_api_modify_project_invalid_keys(self):
Matt Prahl 30f8b1
        """ Test the api_modify_project method of the flask api when the
Matt Prahl 30f8b1
        request data contains an invalid key.
Matt Prahl 30f8b1
        """
Matt Prahl 30f8b1
        tests.create_projects(self.session)
Matt Prahl 30f8b1
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test", headers=headers, data={"invalid": "invalid"}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon ae22c6
        expected_error = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(
Pierre-Yves Chibon 73d120
            json.loads(output.get_data(as_text=True)), expected_error
Pierre-Yves Chibon 73d120
        )
Matt Prahl 30f8b1
Matt Prahl 30f8b1
    def test_api_modify_project_invalid_new_main_admin(self):
Matt Prahl 30f8b1
        """ Test the api_modify_project method of the flask api when the
Matt Prahl 30f8b1
        request is to change the main_admin of the project to a main_admin
Matt Prahl 30f8b1
        that doesn't exist.
Matt Prahl 30f8b1
        """
Matt Prahl 30f8b1
        tests.create_projects(self.session)
Matt Prahl 30f8b1
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon ae22c6
        output = self.app.patch(
Pierre-Yves Chibon 73d120
            "/api/0/test", headers=headers, data={"main_admin": "tbrady"}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon ae22c6
        expected_error = {
Pierre-Yves Chibon 73d120
            "error": "No such user found",
Pierre-Yves Chibon 73d120
            "error_code": "ENOUSER",
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(
Pierre-Yves Chibon 73d120
            json.loads(output.get_data(as_text=True)), expected_error
Pierre-Yves Chibon 73d120
        )
Matt Prahl 30f8b1
Matt Prahl 733eaa
    def test_api_project_watchers(self):
Matt Prahl 733eaa
        """ Test the api_project_watchers method of the flask api. """
Matt Prahl 733eaa
        tests.create_projects(self.session)
Matt Prahl 733eaa
        # The user is not logged in and the owner is watching issues implicitly
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
        self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
        expected_data = {
Matt Prahl 733eaa
            "total_watchers": 1,
Pierre-Yves Chibon 73d120
            "watchers": {"pingou": ["issues"]},
Matt Prahl 733eaa
        }
Pierre-Yves Chibon 73d120
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
        )
Matt Prahl 733eaa
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Matt Prahl 733eaa
            # Non-existing project
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/random/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
            data = json.loads(output.get_data(as_text=True))
Matt Prahl 733eaa
            self.assertDictEqual(
Matt Prahl 733eaa
                data,
Pierre-Yves Chibon 73d120
                {"error_code": "ENOPROJECT", "error": "Project not found"},
Matt Prahl 733eaa
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching issues implicitly
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
            expected_data = {
Matt Prahl 733eaa
                "total_watchers": 1,
Pierre-Yves Chibon 73d120
                "watchers": {"pingou": ["issues"]},
Matt Prahl 733eaa
            }
Pierre-Yves Chibon 73d120
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Pierre-Yves Chibon 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching issues and commits explicitly
Pierre-Yves Chibon 930073
            pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
                self.session, project, "pingou", "3"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b130e5
            self.session.commit()
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
            expected_data = {
Matt Prahl 733eaa
                "total_watchers": 1,
Pierre-Yves Chibon 73d120
                "watchers": {"pingou": ["issues", "commits"]},
Matt Prahl 733eaa
            }
Pierre-Yves Chibon 73d120
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching issues explicitly
Pierre-Yves Chibon 930073
            pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
                self.session, project, "pingou", "1"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b130e5
            self.session.commit()
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
            expected_data = {
Matt Prahl 733eaa
                "total_watchers": 1,
Pierre-Yves Chibon 73d120
                "watchers": {"pingou": ["issues"]},
Matt Prahl 733eaa
            }
Pierre-Yves Chibon 73d120
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching commits explicitly
Pierre-Yves Chibon 930073
            pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
                self.session, project, "pingou", "2"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b130e5
            self.session.commit()
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
            expected_data = {
Matt Prahl 733eaa
                "total_watchers": 1,
Pierre-Yves Chibon 73d120
                "watchers": {"pingou": ["commits"]},
Matt Prahl 733eaa
            }
Pierre-Yves Chibon 73d120
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching commits explicitly and foo is watching
Matt Prahl 733eaa
            # issues implicitly
Matt Prahl 733eaa
            project_user = pagure.lib.model.ProjectUser(
Pierre-Yves Chibon 73d120
                project_id=project.id, user_id=2, access="commit"
Matt Prahl 733eaa
            )
Pierre-Yves Chibon 930073
            pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
                self.session, project, "pingou", "2"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 594b28
            self.session.add(project_user)
Pierre-Yves Chibon 594b28
            self.session.commit()
Matt Prahl 733eaa
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
            expected_data = {
Matt Prahl 733eaa
                "total_watchers": 2,
Pierre-Yves Chibon 73d120
                "watchers": {"foo": ["issues"], "pingou": ["commits"]},
Matt Prahl 733eaa
            }
Pierre-Yves Chibon 73d120
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner and foo are watching issues implicitly
Pierre-Yves Chibon 930073
            pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
                self.session, project, "pingou", "-1"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b130e5
            self.session.commit()
Matt Prahl 733eaa
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
            expected_data = {
Matt Prahl 733eaa
                "total_watchers": 2,
Pierre-Yves Chibon 73d120
                "watchers": {"foo": ["issues"], "pingou": ["issues"]},
Matt Prahl 733eaa
            }
Pierre-Yves Chibon 73d120
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner and foo through group membership are watching issues
Matt Prahl 733eaa
            # implicitly
Pierre-Yves Chibon 930073
            pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
                self.session, project, "pingou", "-1"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            project_membership = (
Pierre-Yves Chibon 73d120
                self.session.query(pagure.lib.model.ProjectUser)
Pierre-Yves Chibon 73d120
                .filter_by(user_id=2, project_id=project.id)
Pierre-Yves Chibon 73d120
                .one()
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 594b28
            self.session.delete(project_membership)
Pierre-Yves Chibon 594b28
            self.session.commit()
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 930073
            msg = pagure.lib.query.add_group(
Matt Prahl 733eaa
                self.session,
Pierre-Yves Chibon 73d120
                group_name="some_group",
Pierre-Yves Chibon 73d120
                display_name="Some Group",
Matt Prahl 733eaa
                description=None,
Pierre-Yves Chibon 73d120
                group_type="bar",
Pierre-Yves Chibon 73d120
                user="pingou",
Matt Prahl 733eaa
                is_admin=False,
Matt Prahl 733eaa
                blacklist=[],
Matt Prahl 733eaa
            )
Pierre-Yves Chibon 594b28
            self.session.commit()
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 930073
            group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
                self.session, group_name="some_group"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 930073
            pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon 73d120
                self.session, "foo", group, "pingou", False
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 930073
            pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 594b28
                self.session,
Pierre-Yves Chibon 594b28
                project,
Pierre-Yves Chibon 73d120
                new_group="some_group",
Pierre-Yves Chibon 73d120
                user="pingou",
Pierre-Yves Chibon 73d120
                access="commit",
Pierre-Yves Chibon 594b28
                create=False,
Pierre-Yves Chibon 73d120
                is_admin=True,
Matt Prahl 733eaa
            )
Pierre-Yves Chibon 594b28
            self.session.commit()
Matt Prahl 733eaa
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
            expected_data = {
Matt Prahl 733eaa
                "total_watchers": 2,
Pierre-Yves Chibon 73d120
                "watchers": {"@some_group": ["issues"], "pingou": ["issues"]},
Matt Prahl 733eaa
            }
Pierre-Yves Chibon 73d120
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching issues implicitly and foo will be watching
Matt Prahl 733eaa
            # commits explicitly but is in a group with commit access
Pierre-Yves Chibon 930073
            pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
                self.session, project, "pingou", "-1"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 930073
            pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
                self.session, project, "foo", "2"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fd2f36
            self.session.commit()
Matt Prahl 733eaa
Pierre-Yves Chibon 73d120
            output = self.app.get("/api/0/test/watchers")
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 200)
Matt Prahl 733eaa
            expected_data = {
Ralph Bean e69883
                "total_watchers": 3,
Matt Prahl 733eaa
                "watchers": {
Ralph Bean e69883
                    "@some_group": ["issues"],
Matt Prahl 733eaa
                    "foo": ["commits"],
Pierre-Yves Chibon 73d120
                    "pingou": ["issues"],
Pierre-Yves Chibon 73d120
                },
Matt Prahl 733eaa
            }
Pierre-Yves Chibon 73d120
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                json.loads(output.get_data(as_text=True)), expected_data
Pierre-Yves Chibon 73d120
            )
Matt Prahl 733eaa
Aurélien Bompard 24a837
    def test_api_new_project(self):
Pierre-Yves Chibon 1c1b5d
        """ Test the api_new_project method of the flask api. """
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon b63f28
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "tickets"))
Pierre-Yves Chibon 1c1b5d
        tests.create_tokens(self.session)
Pierre-Yves Chibon 1c1b5d
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foo_token"}
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Invalid token
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new", headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        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 0a54a2
        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"], "Missing ACLs: create_project")
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # No input
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new", headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {
Pierre-Yves Chibon 73d120
                    "name": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "description": ["This field is required."],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 73d120
        data = {"name": "test"}
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Incomplete request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new", data=data, headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"description": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 73d120
        data = {"name": "test", "description": "Just a small test project"}
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Valid request but repo already exists
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 73d120
                "error": 'It is not possible to create the repo "test"',
Pierre-Yves Chibon 73d120
                "error_code": "ENOCODE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        data = {
Pierre-Yves Chibon 73d120
            "name": "api1",
Pierre-Yves Chibon 73d120
            "description": "Mighty mighty description",
Pierre-Yves Chibon 73d120
            "avatar_email": 123,
Lenka Segura 64cf5d
        }
Lenka Segura 64cf5d
Lenka Segura 64cf5d
        # invalid avatar_email - number
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Lenka Segura 64cf5d
        self.assertEqual(output.status_code, 400)
Lenka Segura 64cf5d
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"avatar_email": ["avatar_email must be an email"]},
Pierre-Yves Chibon 73d120
            },
Lenka Segura 64cf5d
        )
Lenka Segura 64cf5d
Lenka Segura 64cf5d
        data = {
Pierre-Yves Chibon 73d120
            "name": "api1",
Pierre-Yves Chibon 73d120
            "description": "Mighty mighty description",
Pierre-Yves Chibon 73d120
            "avatar_email": [1, 2, 3],
Lenka Segura 64cf5d
        }
Lenka Segura 64cf5d
Lenka Segura 64cf5d
        # invalid avatar_email - list
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Lenka Segura 64cf5d
        self.assertEqual(output.status_code, 400)
Lenka Segura 64cf5d
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"avatar_email": ["avatar_email must be an email"]},
Pierre-Yves Chibon 73d120
            },
Lenka Segura 64cf5d
        )
Lenka Segura 64cf5d
Lenka Segura 64cf5d
        data = {
Pierre-Yves Chibon 73d120
            "name": "api1",
Pierre-Yves Chibon 73d120
            "description": "Mighty mighty description",
Pierre-Yves Chibon 73d120
            "avatar_email": True,
Lenka Segura 64cf5d
        }
Lenka Segura 64cf5d
Lenka Segura 64cf5d
        # invalid avatar_email - boolean
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Lenka Segura 64cf5d
        self.assertEqual(output.status_code, 400)
Lenka Segura 64cf5d
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"avatar_email": ["avatar_email must be an email"]},
Pierre-Yves Chibon 73d120
            },
Lenka Segura 64cf5d
        )
Lenka Segura 64cf5d
Lenka Segura 64cf5d
        data = {
Pierre-Yves Chibon 73d120
            "name": "api1",
Pierre-Yves Chibon 73d120
            "description": "Mighty mighty description",
Pierre-Yves Chibon 73d120
            "avatar_email": "mighty@email.com",
Lenka Segura 64cf5d
        }
Lenka Segura 64cf5d
Lenka Segura 64cf5d
        # valid avatar_email
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Lenka Segura 64cf5d
        self.assertEqual(output.status_code, 200)
Lenka Segura 64cf5d
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": 'Project "api1" created'})
Lenka Segura 64cf5d
Lenka Segura 64cf5d
        data = {
Pierre-Yves Chibon 73d120
            "name": "test_42",
Pierre-Yves Chibon 73d120
            "description": "Just another small test project",
Pierre-Yves Chibon 1c1b5d
        }
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Valid request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": 'Project "test_42" created'})
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    @patch.dict(
Pierre-Yves Chibon 73d120
        "pagure.config.config",
Pierre-Yves Chibon 73d120
        {
Pierre-Yves Chibon 73d120
            "PAGURE_ADMIN_USERS": ["pingou"],
Pierre-Yves Chibon 73d120
            "ALLOW_ADMIN_IGNORE_EXISTING_REPOS": True,
Pierre-Yves Chibon 73d120
        },
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon bdc553
    def test_adopt_repos(self):
Pierre-Yves Chibon bdc553
        """ Test the new_project endpoint with existing git repo. """
Pierre-Yves Chibon bdc553
        # Before
Pierre-Yves Chibon 930073
        projects = pagure.lib.query.search_projects(self.session)
Pierre-Yves Chibon bdc553
        self.assertEqual(len(projects), 0)
Pierre-Yves Chibon bdc553
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon bdc553
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="aaabbbcccddd",
Pierre-Yves Chibon bdc553
            user_id=1,
Pierre-Yves Chibon bdc553
            project_id=None,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=10),
Pierre-Yves Chibon bdc553
        )
Pierre-Yves Chibon bdc553
        self.session.add(item)
Pierre-Yves Chibon bdc553
        self.session.commit()
Pierre-Yves Chibon bdc553
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon bdc553
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon bdc553
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon bdc553
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            input_data = {"name": "test", "description": "Project #1"}
Pierre-Yves Chibon bdc553
Pierre-Yves Chibon bdc553
            # Valid request
Pierre-Yves Chibon bdc553
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/api/0/new/", data=input_data, headers=headers
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon bdc553
            self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon bdc553
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bdc553
            self.assertDictEqual(
Pierre-Yves Chibon bdc553
                data,
Pierre-Yves Chibon bdc553
                {
Pierre-Yves Chibon 73d120
                    "error": "The main repo test.git already exists",
Pierre-Yves Chibon 73d120
                    "error_code": "ENOCODE",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon bdc553
            )
Pierre-Yves Chibon bdc553
Pierre-Yves Chibon 73d120
            input_data["ignore_existing_repos"] = "y"
Pierre-Yves Chibon bdc553
            # Valid request
Pierre-Yves Chibon bdc553
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/api/0/new/", data=input_data, headers=headers
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon bdc553
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bdc553
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertDictEqual(data, {"message": 'Project "test" created'})
Pierre-Yves Chibon bdc553
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"PRIVATE_PROJECTS": True})
Aurélien Bompard 24a837
    def test_api_new_project_private(self):
Pierre-Yves Chibon 3a7944
        """ Test the api_new_project method of the flask api to create
Pierre-Yves Chibon 3a7944
        a private project. """
Pierre-Yves Chibon 3a7944
Pierre-Yves Chibon 3a7944
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "tickets"))
Pierre-Yves Chibon 3a7944
        tests.create_tokens(self.session)
Pierre-Yves Chibon 3a7944
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 3a7944
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3a7944
Pierre-Yves Chibon 3a7944
        data = {
Pierre-Yves Chibon 73d120
            "name": "test",
Pierre-Yves Chibon 73d120
            "description": "Just a small test project",
Pierre-Yves Chibon 73d120
            "private": True,
Pierre-Yves Chibon 3a7944
        }
Pierre-Yves Chibon 3a7944
Pierre-Yves Chibon 3a7944
        # Valid request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon 3a7944
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3a7944
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"message": 'Project "pingou/test" created'}
Pierre-Yves Chibon 3a7944
        )
Pierre-Yves Chibon 3a7944
Aurélien Bompard 24a837
    def test_api_new_project_user_token(self):
Pierre-Yves Chibon 1dfd94
        """ Test the api_new_project method of the flask api. """
Pierre-Yves Chibon 1dfd94
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "tickets"))
Pierre-Yves Chibon 1dfd94
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 1dfd94
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foo_token"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Invalid token
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new", headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        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 0a54a2
        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"], "Missing ACLs: create_project")
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # No input
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new", headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {
Pierre-Yves Chibon 73d120
                    "name": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "description": ["This field is required."],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"name": "test"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Incomplete request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new", data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"description": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"name": "test", "description": "Just a small test project"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid request but repo already exists
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 73d120
                "error": 'It is not possible to create the repo "test"',
Pierre-Yves Chibon 73d120
                "error_code": "ENOCODE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 73d120
            "name": "test_42",
Pierre-Yves Chibon 73d120
            "description": "Just another small test project",
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": 'Project "test_42" created'})
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 2aa56f
        # Project with a namespace
Pierre-Yves Chibon 73d120
        pagure.config.config["ALLOWED_PREFIX"] = ["rpms"]
Pierre-Yves Chibon 2aa56f
        data = {
Pierre-Yves Chibon 73d120
            "name": "test_42",
Pierre-Yves Chibon 73d120
            "namespace": "pingou",
Pierre-Yves Chibon 73d120
            "description": "Just another small test project",
Pierre-Yves Chibon 2aa56f
        }
Pierre-Yves Chibon 2aa56f
Pierre-Yves Chibon 2aa56f
        # Invalid namespace
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon 2aa56f
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2aa56f
        self.assertDictEqual(
Pierre-Yves Chibon 2aa56f
            data,
Pierre-Yves Chibon 2aa56f
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 2aa56f
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"namespace": ["Not a valid choice"]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 2aa56f
        )
Pierre-Yves Chibon 2aa56f
Pierre-Yves Chibon 2aa56f
        data = {
Pierre-Yves Chibon 73d120
            "name": "test_42",
Pierre-Yves Chibon 73d120
            "namespace": "rpms",
Pierre-Yves Chibon 73d120
            "description": "Just another small test project",
Pierre-Yves Chibon 2aa56f
        }
Pierre-Yves Chibon 2aa56f
Pierre-Yves Chibon 2aa56f
        # All good
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon 2aa56f
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2aa56f
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"message": 'Project "rpms/test_42" created'}
Pierre-Yves Chibon 2aa56f
        )
Pierre-Yves Chibon 2aa56f
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"USER_NAMESPACE": True})
Aurélien Bompard 24a837
    def test_api_new_project_user_ns(self):
Pierre-Yves Chibon baec09
        """ Test the api_new_project method of the flask api. """
Pierre-Yves Chibon baec09
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "tickets"))
Pierre-Yves Chibon baec09
        tests.create_tokens(self.session)
Pierre-Yves Chibon baec09
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon baec09
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Create a project with the user namespace feature on
Pierre-Yves Chibon baec09
        data = {
Pierre-Yves Chibon 73d120
            "name": "testproject",
Pierre-Yves Chibon 73d120
            "description": "Just another small test project",
Pierre-Yves Chibon baec09
        }
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Valid request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon baec09
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon baec09
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"message": 'Project "pingou/testproject" created'}
Pierre-Yves Chibon baec09
        )
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Create a project with a namespace and the user namespace feature on
Pierre-Yves Chibon baec09
        data = {
Pierre-Yves Chibon 73d120
            "name": "testproject2",
Pierre-Yves Chibon 73d120
            "namespace": "testns",
Pierre-Yves Chibon 73d120
            "description": "Just another small test project",
Pierre-Yves Chibon baec09
        }
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Valid request
Pierre-Yves Chibon 73d120
        with patch.dict(
Pierre-Yves Chibon 73d120
            "pagure.config.config", {"ALLOWED_PREFIX": ["testns"]}
Pierre-Yves Chibon 73d120
        ):
Pierre-Yves Chibon 73d120
            output = self.app.post("/api/0/new/", data=data, headers=headers)
Pierre-Yves Chibon baec09
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon baec09
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"message": 'Project "testns/testproject2" created'}
Pierre-Yves Chibon baec09
        )
Pierre-Yves Chibon baec09
Aurélien Bompard 24a837
    def test_api_fork_project(self):
Pierre-Yves Chibon f0e08e
        """ Test the api_fork_project method of the flask api. """
Pierre-Yves Chibon f0e08e
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        for folder in ["docs", "tickets", "requests", "repos"]:
Pierre-Yves Chibon f0e08e
            tests.create_projects_git(
Pierre-Yves Chibon 73d120
                os.path.join(self.path, folder), bare=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f0e08e
        tests.create_tokens(self.session)
Pierre-Yves Chibon f0e08e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foo_token"}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Invalid token
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork", headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        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 0a54a2
        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"], "Missing ACLs: fork_project")
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # No input
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork", headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"repo": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"name": "test"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Incomplete request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork", data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"repo": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"repo": "test"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork/", data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"message": 'Repo "test" cloned to "pingou/test"'}
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"repo": "test"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # project already forked
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork/", data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 73d120
                "error": 'Repo "forks/pingou/test" already exists',
Pierre-Yves Chibon 73d120
                "error_code": "ENOCODE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"repo": "test", "username": "pingou"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Fork already exists
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork/", data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 73d120
                "error": 'Repo "forks/pingou/test" already exists',
Pierre-Yves Chibon 73d120
                "error_code": "ENOCODE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"repo": "test", "namespace": "pingou"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Repo does not exists
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork/", data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Aurélien Bompard 24a837
    def test_api_fork_project_user_token(self):
Pierre-Yves Chibon 1dfd94
        """ Test the api_fork_project method of the flask api. """
Pierre-Yves Chibon 1dfd94
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        for folder in ["docs", "tickets", "requests", "repos"]:
Pierre-Yves Chibon 1dfd94
            tests.create_projects_git(
Pierre-Yves Chibon 73d120
                os.path.join(self.path, folder), bare=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 1dfd94
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 1dfd94
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foo_token"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Invalid token
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork", headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        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 0a54a2
        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"], "Missing ACLs: fork_project")
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # No input
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork", headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"repo": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon 73d120
        data = {"name": "test"}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Incomplete request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork", data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"repo": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon 73d120
        data = {"repo": "test"}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Valid request
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork/", data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"message": 'Repo "test" cloned to "pingou/test"'}
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon 73d120
        data = {"repo": "test"}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # project already forked
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork/", data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon 73d120
                "error": 'Repo "forks/pingou/test" already exists',
Pierre-Yves Chibon 73d120
                "error_code": "ENOCODE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon 73d120
        data = {"repo": "test", "username": "pingou"}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Fork already exists
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork/", data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon 73d120
                "error": 'Repo "forks/pingou/test" already exists',
Pierre-Yves Chibon 73d120
                "error_code": "ENOCODE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 73d120
        data = {"repo": "test", "namespace": "pingou"}
Pierre-Yves Chibon 2c43bb
Pierre-Yves Chibon 2c43bb
        # Repo does not exists
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/fork/", data=data, headers=headers)
Pierre-Yves Chibon 2c43bb
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2c43bb
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 2c43bb
        )
Pierre-Yves Chibon 2c43bb
Aurélien Bompard 24a837
    def test_api_generate_acls(self):
Matt Prahl 489ad1
        """ Test the api_generate_acls method of the flask api """
Matt Prahl 489ad1
        tests.create_projects(self.session)
Matt Prahl 489ad1
        tests.create_tokens(self.session, project_id=None)
Matt Prahl 489ad1
        tests.create_tokens_acl(
Pierre-Yves Chibon 73d120
            self.session, "aaabbbcccddd", "generate_acls_project"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Matt Prahl 489ad1
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon ae22c6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/generateacls",
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data={"wait": False},
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ae22c6
        expected_output = {
Pierre-Yves Chibon 73d120
            "message": "Project ACL generation queued",
Pierre-Yves Chibon 73d120
            "taskid": "abc-1234",
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon ae22c6
        self.mock_gen_acls.assert_called_once_with(
Pierre-Yves Chibon 73d120
            name="test", namespace=None, user=None, group=None
Pierre-Yves Chibon 73d120
        )
mprahl 0f9ac5
Aurélien Bompard 24a837
    def test_api_generate_acls_json(self):
mprahl 0f9ac5
        """ Test the api_generate_acls method of the flask api using JSON """
mprahl 0f9ac5
        tests.create_projects(self.session)
mprahl 0f9ac5
        tests.create_tokens(self.session, project_id=None)
mprahl 0f9ac5
        tests.create_tokens_acl(
Pierre-Yves Chibon 73d120
            self.session, "aaabbbcccddd", "generate_acls_project"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        headers = {
Pierre-Yves Chibon 73d120
            "Authorization": "token aaabbbcccddd",
Pierre-Yves Chibon 73d120
            "Content-Type": "application/json",
Pierre-Yves Chibon 73d120
        }
mprahl 0f9ac5
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon ae22c6
Pierre-Yves Chibon ae22c6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/generateacls",
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=json.dumps({"wait": False}),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ae22c6
        expected_output = {
Pierre-Yves Chibon 73d120
            "message": "Project ACL generation queued",
Pierre-Yves Chibon 73d120
            "taskid": "abc-1234",
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon ae22c6
        self.mock_gen_acls.assert_called_once_with(
Pierre-Yves Chibon 73d120
            name="test", namespace=None, user=None, group=None
Pierre-Yves Chibon 73d120
        )
Matt Prahl 489ad1
Aurélien Bompard 24a837
    def test_api_generate_acls_wait_true(self):
Matt Prahl 489ad1
        """ Test the api_generate_acls method of the flask api when wait is
Matt Prahl 489ad1
        set to True """
Matt Prahl 489ad1
        tests.create_projects(self.session)
Matt Prahl 489ad1
        tests.create_tokens(self.session, project_id=None)
Matt Prahl 489ad1
        tests.create_tokens_acl(
Pierre-Yves Chibon 73d120
            self.session, "aaabbbcccddd", "generate_acls_project"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Matt Prahl 489ad1
Aurélien Bompard 24a837
        task_result = Mock()
Pierre-Yves Chibon 73d120
        task_result.id = "abc-1234"
Aurélien Bompard 24a837
        self.mock_gen_acls.return_value = task_result
Matt Prahl 489ad1
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon ae22c6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/generateacls",
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data={"wait": True},
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        expected_output = {"message": "Project ACLs generated"}
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon ae22c6
        self.mock_gen_acls.assert_called_once_with(
Pierre-Yves Chibon 73d120
            name="test", namespace=None, user=None, group=None
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertTrue(task_result.get.called)
Matt Prahl 489ad1
Matt Prahl 489ad1
    def test_api_generate_acls_no_project(self):
Matt Prahl 489ad1
        """ Test the api_generate_acls method of the flask api when the project
Matt Prahl 489ad1
        doesn't exist """
Matt Prahl 489ad1
        tests.create_projects(self.session)
Matt Prahl 489ad1
        tests.create_tokens(self.session, project_id=None)
Matt Prahl 489ad1
        tests.create_tokens_acl(
Pierre-Yves Chibon 73d120
            self.session, "aaabbbcccddd", "generate_acls_project"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Matt Prahl 489ad1
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon ae22c6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test12345123/git/generateacls",
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data={"wait": False},
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ae22c6
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ae22c6
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ae22c6
        expected_output = {
Pierre-Yves Chibon 73d120
            "error_code": "ENOPROJECT",
Pierre-Yves Chibon 73d120
            "error": "Project not found",
Pierre-Yves Chibon ae22c6
        }
Pierre-Yves Chibon ae22c6
        self.assertEqual(data, expected_output)
Matt Prahl 489ad1
Pierre-Yves Chibon 6ffde7
    def test_api_new_git_branch(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_new_branch method of the flask api """
Karsten Hopp be01e8
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos")
Pierre-Yves Chibon 6ffde7
        tests.create_projects_git(repo_path, bare=True)
Pierre-Yves Chibon 73d120
        tests.add_content_git_repo(os.path.join(repo_path, "test.git"))
Karsten Hopp be01e8
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "create_branch")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        args = {"branch": "test123"}
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/branch", headers=headers, data=args
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        expected_output = {"message": "Project branch was created"}
Pierre-Yves Chibon 6ffde7
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 73d120
        git_path = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(git_path)
Pierre-Yves Chibon 73d120
        self.assertIn("test123", repo_obj.listall_branches())
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_api_new_git_branch_json(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_new_branch method of the flask api """
Pierre-Yves Chibon 6ffde7
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos")
Pierre-Yves Chibon 6ffde7
        tests.create_projects_git(repo_path, bare=True)
Pierre-Yves Chibon 73d120
        tests.add_content_git_repo(os.path.join(repo_path, "test.git"))
Pierre-Yves Chibon 6ffde7
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "create_branch")
Pierre-Yves Chibon 73d120
        headers = {
Pierre-Yves Chibon 73d120
            "Authorization": "token aaabbbcccddd",
Pierre-Yves Chibon 73d120
            "Content-Type": "application/json",
Pierre-Yves Chibon 73d120
        }
Pierre-Yves Chibon 73d120
        args = {"branch": "test123"}
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/branch", headers=headers, data=json.dumps(args)
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Karsten Hopp be01e8
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        expected_output = {"message": "Project branch was created"}
Karsten Hopp be01e8
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 73d120
        git_path = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(git_path)
Pierre-Yves Chibon 73d120
        self.assertIn("test123", repo_obj.listall_branches())
Karsten Hopp be01e8
Pierre-Yves Chibon 6ffde7
    def test_api_new_git_branch_from_branch(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_new_branch method of the flask api """
Karsten Hopp be01e8
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos")
Pierre-Yves Chibon 6ffde7
        tests.create_projects_git(repo_path, bare=True)
Pierre-Yves Chibon 73d120
        tests.add_content_git_repo(os.path.join(repo_path, "test.git"))
Karsten Hopp be01e8
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "create_branch")
Pierre-Yves Chibon 73d120
        git_path = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(git_path)
Pierre-Yves Chibon 73d120
        parent = pagure.lib.git.get_branch_ref(repo_obj, "master").peel()
Pierre-Yves Chibon 73d120
        repo_obj.create_branch("dev123", parent)
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        args = {"branch": "test123", "from_branch": "dev123"}
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/branch", headers=headers, data=args
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Karsten Hopp be01e8
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        expected_output = {"message": "Project branch was created"}
Karsten Hopp be01e8
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 73d120
        self.assertIn("test123", repo_obj.listall_branches())
Karsten Hopp be01e8
Pierre-Yves Chibon 6ffde7
    def test_api_new_git_branch_already_exists(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_new_branch method of the flask api when branch already
Pierre-Yves Chibon 6ffde7
        exists """
Karsten Hopp be01e8
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos")
Pierre-Yves Chibon 6ffde7
        tests.create_projects_git(repo_path, bare=True)
Pierre-Yves Chibon 73d120
        tests.add_content_git_repo(os.path.join(repo_path, "test.git"))
Karsten Hopp be01e8
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "create_branch")
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        args = {"branch": "master"}
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/branch", headers=headers, data=args
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 400)
Karsten Hopp be01e8
        data = json.loads(output.get_data(as_text=True))
Karsten Hopp be01e8
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": 'The branch "master" already exists',
Pierre-Yves Chibon 73d120
            "error_code": "ENOCODE",
Karsten Hopp be01e8
        }
Karsten Hopp be01e8
        self.assertEqual(data, expected_output)
Karsten Hopp be01e8
Pierre-Yves Chibon 6ffde7
    def test_api_new_git_branch_from_commit(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_new_branch method of the flask api """
Karsten Hopp be01e8
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repos_path = os.path.join(self.path, "repos")
Pierre-Yves Chibon 6ffde7
        tests.create_projects_git(repos_path, bare=True)
Pierre-Yves Chibon 73d120
        git_path = os.path.join(repos_path, "test.git")
Pierre-Yves Chibon 6ffde7
        tests.add_content_git_repo(git_path)
Pierre-Yves Chibon 6ffde7
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "create_branch")
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(git_path)
Pierre-Yves Chibon 73d120
        from_commit = repo_obj.revparse_single("HEAD").oid.hex
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        args = {"branch": "test123", "from_commit": from_commit}
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/branch", headers=headers, data=args
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Karsten Hopp be01e8
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        expected_output = {"message": "Project branch was created"}
Karsten Hopp be01e8
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 73d120
        self.assertIn("test123", repo_obj.listall_branches())
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
class PagureFlaskApiProjectFlagtests(tests.Modeltests):
Pierre-Yves Chibon 6ffde7
    """ Tests for the flask API of pagure for flagging commit in project
Pierre-Yves Chibon 6ffde7
    """
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def setUp(self):
Pierre-Yves Chibon 6ffde7
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 6ffde7
        super(PagureFlaskApiProjectFlagtests, self).setUp()
Karsten Hopp be01e8
Karsten Hopp be01e8
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo_path = os.path.join(self.path, "repos")
Pierre-Yves Chibon 73d120
        self.git_path = os.path.join(repo_path, "test.git")
Pierre-Yves Chibon 6ffde7
        tests.create_projects_git(repo_path, bare=True)
Pierre-Yves Chibon 6ffde7
        tests.add_content_git_repo(self.git_path)
Karsten Hopp be01e8
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "commit_flag")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_missing_status(self):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with missing precentage. """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Karsten Hopp be01e8
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Karsten Hopp be01e8
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
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",
Karsten Hopp be01e8
        }
Karsten Hopp be01e8
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Karsten Hopp be01e8
        self.assertEqual(output.status_code, 400)
Karsten Hopp be01e8
        data = json.loads(output.get_data(as_text=True))
Karsten Hopp be01e8
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            "errors": {"status": ["Not a valid choice"]},
Karsten Hopp be01e8
        }
Karsten Hopp be01e8
        self.assertEqual(data, expected_output)
Karsten Hopp be01e8
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_missing_username(self):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with missing username. """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Karsten Hopp be01e8
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Karsten Hopp be01e8
        data = {
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",
Pierre-Yves Chibon 73d120
            "status": "success",
Karsten Hopp be01e8
        }
Karsten Hopp be01e8
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Karsten Hopp be01e8
        self.assertEqual(output.status_code, 400)
Karsten Hopp be01e8
        data = json.loads(output.get_data(as_text=True))
Karsten Hopp be01e8
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            "errors": {"username": ["This field is required."]},
Karsten Hopp be01e8
        }
Karsten Hopp be01e8
        self.assertEqual(data, expected_output)
Karsten Hopp be01e8
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_missing_comment(self):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with missing comment. """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Karsten Hopp be01e8
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Karsten Hopp be01e8
        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",
Pierre-Yves Chibon 73d120
            "status": "success",
Karsten Hopp be01e8
        }
Karsten Hopp be01e8
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 400)
Karsten Hopp be01e8
        data = json.loads(output.get_data(as_text=True))
Karsten Hopp be01e8
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_missing_url(self):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with missing url. """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 100,
Pierre-Yves Chibon 73d120
            "comment": "Tests passed",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
            "status": "success",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6ffde7
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            "errors": {"url": ["This field is required."]},
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_invalid_token(self):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with missing info. """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token 123"}
Pierre-Yves Chibon 6ffde7
        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",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6ffde7
        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 0a54a2
        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")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_invalid_status(self):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with an invalid status. """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
        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
            "status": "foobar",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6ffde7
        self.assertEqual(
Pierre-Yves Chibon 6ffde7
            data,
Pierre-Yves Chibon 6ffde7
            {
Pierre-Yves Chibon 73d120
                "errors": {"status": ["Not a valid choice"]},
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_with_uid(self):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with provided uid. """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
        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",
Pierre-Yves Chibon 73d120
            "status": "success",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6ffde7
        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"]["commit_hash"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon 6ffde7
        expected_output = {
Pierre-Yves Chibon 73d120
            "flag": {
Pierre-Yves Chibon 73d120
                "comment": "Tests passed",
Pierre-Yves Chibon 73d120
                "commit_hash": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                "percent": 100,
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 6ffde7
            },
Pierre-Yves Chibon 73d120
            "message": "Flag added",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_without_uid(self, mock_email):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with missing info.
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        Also ensure notifications aren't sent when they are not asked for.
Pierre-Yves Chibon 6ffde7
        """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
        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
            "status": "success",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertNotEqual(data["uid"], "jenkins_build_pagure_100+seed")
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["uid"] = "b1de8f80defd4a81afe2e09f39678087"
Pierre-Yves Chibon 6ffde7
        expected_output = {
Pierre-Yves Chibon 73d120
            "flag": {
Pierre-Yves Chibon 73d120
                "comment": "Tests passed",
Pierre-Yves Chibon 73d120
                "commit_hash": commit.oid.hex,
Pierre-Yves Chibon 73d120
                "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                "percent": 100,
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 6ffde7
            },
Pierre-Yves Chibon 73d120
            "message": "Flag added",
Pierre-Yves Chibon 73d120
            "uid": "b1de8f80defd4a81afe2e09f39678087",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        mock_email.assert_not_called()
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_with_notification(self, mock_email):
Pierre-Yves Chibon 6ffde7
        """ Test flagging a commit with notification enabled. """
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        # Enable commit notifications
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 6ffde7
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["notify_on_commit_flag"] = True
Pierre-Yves Chibon 6ffde7
        repo.settings = settings
Pierre-Yves Chibon 6ffde7
        self.session.add(repo)
Pierre-Yves Chibon 6ffde7
        self.session.commit()
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
        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
            "status": "success",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertNotEqual(data["uid"], "jenkins_build_pagure_100+seed")
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["uid"] = "b1de8f80defd4a81afe2e09f39678087"
Pierre-Yves Chibon 6ffde7
        expected_output = {
Pierre-Yves Chibon 73d120
            "flag": {
Pierre-Yves Chibon 73d120
                "comment": "Tests passed",
Pierre-Yves Chibon 73d120
                "commit_hash": commit.oid.hex,
Pierre-Yves Chibon 73d120
                "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                "percent": 100,
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 6ffde7
            },
Pierre-Yves Chibon 73d120
            "message": "Flag added",
Pierre-Yves Chibon 73d120
            "uid": "b1de8f80defd4a81afe2e09f39678087",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        mock_email.assert_called_once_with(
Pierre-Yves Chibon 73d120
            "\nJenkins flagged the commit "
Pierre-Yves Chibon 73d120
            "`" + commit.oid.hex + "` as success: "
Pierre-Yves Chibon 73d120
            "Tests passed\n\n"
Pierre-Yves Chibon 73d120
            "http://localhost.localdomain/test/c/" + commit.oid.hex + "\n",
Pierre-Yves Chibon 73d120
            "Commit #" + commit.oid.hex + " - Jenkins: success",
Pierre-Yves Chibon 73d120
            "bar@pingou.com",
Pierre-Yves Chibon 73d120
            in_reply_to="test-project-1",
Pierre-Yves Chibon 73d120
            mail_id="test-commit-1-1",
Pierre-Yves Chibon 73d120
            project_name="test",
Pierre-Yves Chibon 73d120
            user_from="Jenkins",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    @patch.dict(
Pierre-Yves Chibon 73d120
        "pagure.config.config",
Pierre-Yves Chibon 73d120
        {
Pierre-Yves Chibon 73d120
            "FLAG_STATUSES_LABELS": {
Pierre-Yves Chibon 73d120
                "pend!": "label-info",
Pierre-Yves Chibon 73d120
                "succeed!": "label-success",
Pierre-Yves Chibon 73d120
                "fail!": "label-danger",
Pierre-Yves Chibon 73d120
                "what?": "label-warning",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "FLAG_PENDING": "pend!",
Pierre-Yves Chibon 73d120
            "FLAG_SUCCESS": "succeed!",
Pierre-Yves Chibon 73d120
            "FLAG_FAILURE": "fail!",
Pierre-Yves Chibon 73d120
        },
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 6ffde7
    def test_flag_commit_with_custom_flags(self):
Pierre-Yves Chibon 6ffde7
        """ Test flagging when custom flags are set up
Pierre-Yves Chibon 6ffde7
        """
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
        send_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
            "status": "succeed!",
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=send_data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["flag"]["status"], "succeed!")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        # Try invalid flag status
Pierre-Yves Chibon 73d120
        send_data["status"] = "nooooo...."
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/c/%s/flag" % commit.oid.hex,
Pierre-Yves Chibon 73d120
            headers=headers,
Pierre-Yves Chibon 73d120
            data=send_data,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6ffde7
        self.assertEqual(
Pierre-Yves Chibon 6ffde7
            data,
Pierre-Yves Chibon 6ffde7
            {
Pierre-Yves Chibon 73d120
                "errors": {"status": ["Not a valid choice"]},
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_commit_flags(self):
Pierre-Yves Chibon 6ffde7
        """ Test retrieving commit flags. """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 6ffde7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 73d120
        commit = repo_obj.revparse_single("HEAD")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        # test with no flags
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/c/%s/flag" % commit.oid.hex)
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            json.loads(output.get_data(as_text=True)),
Pierre-Yves Chibon 73d120
            {"total_flags": 0, "flags": []},
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        # add some flags and retrieve them
Pierre-Yves Chibon 930073
        pagure.lib.query.add_commit_flag(
Pierre-Yves Chibon 6ffde7
            session=self.session,
Pierre-Yves Chibon 6ffde7
            repo=repo,
Pierre-Yves Chibon 6ffde7
            commit_hash=commit.oid.hex,
Pierre-Yves Chibon 73d120
            username="simple-koji-ci",
Pierre-Yves Chibon 73d120
            status="pending",
Pierre-Yves Chibon 6ffde7
            percent=None,
Pierre-Yves Chibon 73d120
            comment="Build is running",
Pierre-Yves Chibon 73d120
            url="https://koji.fp.o/koji...",
Pierre-Yves Chibon 73d120
            uid="uid",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            token="aaabbbcccddd",
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 930073
        pagure.lib.query.add_commit_flag(
Pierre-Yves Chibon 6ffde7
            session=self.session,
Pierre-Yves Chibon 6ffde7
            repo=repo,
Pierre-Yves Chibon 6ffde7
            commit_hash=commit.oid.hex,
Pierre-Yves Chibon 73d120
            username="complex-koji-ci",
Pierre-Yves Chibon 73d120
            status="success",
Pierre-Yves Chibon 6ffde7
            percent=None,
Pierre-Yves Chibon 73d120
            comment="Build succeeded",
Pierre-Yves Chibon 73d120
            url="https://koji.fp.o/koji...",
Pierre-Yves Chibon 73d120
            uid="uid2",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            token="aaabbbcccddd",
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
        self.session.commit()
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/c/%s/flag" % commit.oid.hex)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        for f in data["flags"]:
Pierre-Yves Chibon 73d120
            f["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
            f["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
            f["commit_hash"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon 6ffde7
        expected_output = {
Pierre-Yves Chibon 6ffde7
            "flags": [
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "comment": "Build is running",
Pierre-Yves Chibon 73d120
                    "commit_hash": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": None,
Pierre-Yves Chibon 73d120
                    "status": "pending",
Pierre-Yves Chibon 73d120
                    "url": "https://koji.fp.o/koji...",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "foo bar", "name": "foo"},
Pierre-Yves Chibon 73d120
                    "username": "simple-koji-ci",
Pierre-Yves Chibon 6ffde7
                },
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "comment": "Build succeeded",
Pierre-Yves Chibon 73d120
                    "commit_hash": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": None,
Pierre-Yves Chibon 73d120
                    "status": "success",
Pierre-Yves Chibon 73d120
                    "url": "https://koji.fp.o/koji...",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "foo bar", "name": "foo"},
Pierre-Yves Chibon 73d120
                    "username": "complex-koji-ci",
Pierre-Yves Chibon 6ffde7
                },
Pierre-Yves Chibon 6ffde7
            ],
Pierre-Yves Chibon 73d120
            "total_flags": 2,
Karsten Hopp be01e8
        }
Pierre-Yves Chibon 6ffde7
Karsten Hopp be01e8
        self.assertEqual(data, expected_output)
Karsten Hopp be01e8
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
class PagureFlaskApiProjectModifyAclTests(tests.Modeltests):
Pierre-Yves Chibon 6ffde7
    """ Tests for the flask API of pagure for modifying ACLs in a project
Pierre-Yves Chibon 6ffde7
    """
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    maxDiff = None
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def setUp(self):
Pierre-Yves Chibon 6ffde7
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 6ffde7
        super(PagureFlaskApiProjectModifyAclTests, self).setUp()
Karsten Hopp be01e8
        tests.create_projects(self.session)
Karsten Hopp be01e8
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Karsten Hopp be01e8
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 6ffde7
        self.assertEquals(
Pierre-Yves Chibon 73d120
            project.access_users, {"admin": [], "commit": [], "ticket": []}
Karsten Hopp be01e8
        )
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_no_project(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when the project
Pierre-Yves Chibon 6ffde7
        doesn't exist """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Karsten Hopp be01e8
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "bar", "acl": "commit"}
Karsten Hopp be01e8
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test12345123/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 404)
Karsten Hopp be01e8
        data = json.loads(output.get_data(as_text=True))
Karsten Hopp be01e8
        expected_output = {
Pierre-Yves Chibon 73d120
            "error_code": "ENOPROJECT",
Pierre-Yves Chibon 73d120
            "error": "Project not found",
Karsten Hopp be01e8
        }
Karsten Hopp be01e8
        self.assertEqual(data, expected_output)
Karsten Hopp be01e8
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_no_user(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when the user
Pierre-Yves Chibon 6ffde7
        doesn't exist """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
mprahl 7d811e
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "nosuchuser", "acl": "commit"}
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
mprahl 7d811e
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "No such user found",
Pierre-Yves Chibon 73d120
            "error_code": "ENOUSER",
mprahl 7d811e
        }
mprahl 7d811e
        self.assertEqual(data, expected_output)
mprahl 7d811e
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_no_group(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when the group
Pierre-Yves Chibon 6ffde7
        doesn't exist """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 73d120
        data = {"user_type": "group", "name": "nosuchgroup", "acl": "commit"}
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Group not found",
Pierre-Yves Chibon 73d120
            "error_code": "ENOGROUP",
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_no_permission(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when the user
Pierre-Yves Chibon 6ffde7
        doesn't have permissions """
Pierre-Yves Chibon 6ffde7
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="foo_token2",
Pierre-Yves Chibon 6ffde7
            user_id=2,
Pierre-Yves Chibon 6ffde7
            project_id=None,
Pierre-Yves Chibon 6ffde7
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
        self.session.add(item)
Pierre-Yves Chibon 6ffde7
        self.session.commit()
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "foo_token2", "modify_project")
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foo_token2"}
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "foo", "acl": "commit"}
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "You are not allowed to modify this project",
Pierre-Yves Chibon 73d120
            "error_code": "EMODIFYPROJECTNOTALLOWED",
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_neither_user_nor_group(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when neither
Pierre-Yves Chibon 6ffde7
        user nor group was set """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        data = {"acl": "commit"}
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            "errors": {
Pierre-Yves Chibon 73d120
                "name": ["This field is required."],
Pierre-Yves Chibon 73d120
                "user_type": ["Not a valid choice"],
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_invalid_acl(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when the ACL
Pierre-Yves Chibon 6ffde7
        doesn't exist. Must be one of ticket, commit or admin. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "bar", "acl": "invalidacl"}
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            "errors": {"acl": ["Not a valid choice"]},
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_user(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api for
Pierre-Yves Chibon 6ffde7
        setting an ACL for a user. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "foo", "acl": "commit"}
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1510742566"
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6ffde7
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
            "access_users": {
Pierre-Yves Chibon 73d120
                "admin": [],
Pierre-Yves Chibon 73d120
                "commit": ["foo"],
Pierre-Yves Chibon 73d120
                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "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
            ],
Pierre-Yves Chibon 73d120
            "custom_keys": [],
Pierre-Yves Chibon 73d120
            "date_created": "1510742565",
Pierre-Yves Chibon 73d120
            "date_modified": "1510742566",
Pierre-Yves Chibon 73d120
            "description": "test project #1",
Pierre-Yves Chibon 73d120
            "fullname": "test",
Pierre-Yves Chibon 73d120
            "id": 1,
Pierre-Yves Chibon 73d120
            "milestones": {},
Pierre-Yves Chibon 73d120
            "name": "test",
Pierre-Yves Chibon 73d120
            "namespace": None,
Pierre-Yves Chibon 73d120
            "parent": None,
Pierre-Yves Chibon 73d120
            "priorities": {},
Pierre-Yves Chibon 73d120
            "tags": [],
Pierre-Yves Chibon 73d120
            "url_path": "test",
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon abcbf7
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_group(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api for
Pierre-Yves Chibon 6ffde7
        setting an ACL for a group. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon abcbf7
Pierre-Yves Chibon 6ffde7
        # Create a group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon 6ffde7
            self.session,
Pierre-Yves Chibon 73d120
            group_name="baz",
Pierre-Yves Chibon 73d120
            display_name="baz group",
Pierre-Yves Chibon 6ffde7
            description=None,
Pierre-Yves Chibon 73d120
            group_type="bar",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 6ffde7
            is_admin=False,
Pierre-Yves Chibon 6ffde7
            blacklist=[],
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User `foo` added to the group `baz`.")
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 73d120
        data = {"user_type": "group", "name": "baz", "acl": "ticket"}
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1510742566"
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": ["baz"]},
Pierre-Yves Chibon 73d120
            "access_users": {
Pierre-Yves Chibon 73d120
                "admin": [],
Pierre-Yves Chibon 73d120
                "commit": [],
Pierre-Yves Chibon 73d120
                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon 6ffde7
            },
Pierre-Yves Chibon 73d120
            "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 6ffde7
            ],
Pierre-Yves Chibon 73d120
            "custom_keys": [],
Pierre-Yves Chibon 73d120
            "date_created": "1510742565",
Pierre-Yves Chibon 73d120
            "date_modified": "1510742566",
Pierre-Yves Chibon 73d120
            "description": "test project #1",
Pierre-Yves Chibon 73d120
            "fullname": "test",
Pierre-Yves Chibon 73d120
            "id": 1,
Pierre-Yves Chibon 73d120
            "milestones": {},
Pierre-Yves Chibon 73d120
            "name": "test",
Pierre-Yves Chibon 73d120
            "namespace": None,
Pierre-Yves Chibon 73d120
            "parent": None,
Pierre-Yves Chibon 73d120
            "priorities": {},
Pierre-Yves Chibon 73d120
            "tags": [],
Pierre-Yves Chibon 73d120
            "url_path": "test",
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6ffde7
        }
Pierre-Yves Chibon 6ffde7
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_no_acl(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when no ACL
Pierre-Yves Chibon 6ffde7
        are specified. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 6ffde7
        self.assertEquals(
Pierre-Yves Chibon 73d120
            project.access_users, {"admin": [], "commit": [], "ticket": []}
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "foo"}
Pierre-Yves Chibon 6ffde7
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 6ffde7
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6ffde7
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            "errors": "User does not have any access on the repo",
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_remove_own_acl_no_access(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when no ACL
Pierre-Yves Chibon 6ffde7
        are specified, so the user tries to remove their own access but the
Pierre-Yves Chibon 6ffde7
        user is the project owner. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "pingou"}
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6ffde7
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 73d120
            "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
            "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            "errors": "User does not have any access on the repo",
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_remove_own_acl_(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api when no ACL
Pierre-Yves Chibon 6ffde7
        are specified, so the user tries to remove their own access but the
Pierre-Yves Chibon 6ffde7
        user is the project owner. """
Karsten Hopp a5d5f1
        # Add the user `foo` to the project
Pierre-Yves Chibon 6ffde7
        self.test_api_modify_acls_user()
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6ffde7
        # Ensure `foo` was properly added:
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        user_foo = pagure.lib.query.search_user(self.session, username="foo")
Pierre-Yves Chibon 6ffde7
        self.assertEquals(
Pierre-Yves Chibon 6ffde7
            project.access_users,
Pierre-Yves Chibon 73d120
            {"admin": [], "commit": [user_foo], "ticket": []},
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6ffde7
        # Create an API token for `foo` for the project `test`
Pierre-Yves Chibon 6ffde7
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="foo_test_token",
Pierre-Yves Chibon 6ffde7
            user_id=2,  # foo
Pierre-Yves Chibon 6ffde7
            project_id=1,  # test
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=10),
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
        self.session.add(item)
Pierre-Yves Chibon 6882b8
        self.session.commit()
Pierre-Yves Chibon 6ffde7
        tests.create_tokens_acl(
Pierre-Yves Chibon 73d120
            self.session, "foo_test_token", "modify_project"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foo_test_token"}
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "foo"}
Pierre-Yves Chibon 6882b8
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6882b8
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1510742566"
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6882b8
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
            "access_users": {
Pierre-Yves Chibon 73d120
                "admin": [],
Pierre-Yves Chibon 73d120
                "commit": [],
Pierre-Yves Chibon 73d120
                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "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 6ffde7
            ],
Pierre-Yves Chibon 73d120
            "custom_keys": [],
Pierre-Yves Chibon 73d120
            "date_created": "1510742565",
Pierre-Yves Chibon 73d120
            "date_modified": "1510742566",
Pierre-Yves Chibon 73d120
            "description": "test project #1",
Pierre-Yves Chibon 73d120
            "fullname": "test",
Pierre-Yves Chibon 73d120
            "id": 1,
Pierre-Yves Chibon 73d120
            "milestones": {},
Pierre-Yves Chibon 73d120
            "name": "test",
Pierre-Yves Chibon 73d120
            "namespace": None,
Pierre-Yves Chibon 73d120
            "parent": None,
Pierre-Yves Chibon 73d120
            "priorities": {},
Pierre-Yves Chibon 73d120
            "tags": [],
Pierre-Yves Chibon 73d120
            "url_path": "test",
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6882b8
        }
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6882b8
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6ffde7
        # Ensure `foo` was properly removed
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 6ffde7
        self.assertEquals(
Pierre-Yves Chibon 73d120
            project.access_users, {"admin": [], "commit": [], "ticket": []}
Pierre-Yves Chibon 6882b8
        )
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6ffde7
    def test_api_modify_acls_remove_someone_else_acl(self):
Pierre-Yves Chibon 6ffde7
        """ Test the api_modify_acls method of the flask api an admin tries
Pierre-Yves Chibon 6ffde7
        to remove access from someone else. """
Pierre-Yves Chibon 6ffde7
        # Add the user `foo` to the project
Pierre-Yves Chibon 6ffde7
        self.test_api_modify_acls_user()
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 6ffde7
        # Ensure `foo` was properly added:
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        user_foo = pagure.lib.query.search_user(self.session, username="foo")
Pierre-Yves Chibon 6ffde7
        self.assertEquals(
Pierre-Yves Chibon 6ffde7
            project.access_users,
Pierre-Yves Chibon 73d120
            {"admin": [], "commit": [user_foo], "ticket": []},
Pierre-Yves Chibon 6ffde7
        )
Slavek Kabrda 45252f
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {"user_type": "user", "name": "foo"}
Slavek Kabrda 45252f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/git/modifyacls", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 3e4583
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["date_modified"] = "1510742566"
Slavek Kabrda 3e4583
Slavek Kabrda 3e4583
        expected_output = {
Pierre-Yves Chibon 73d120
            "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
            "access_users": {
Pierre-Yves Chibon 73d120
                "admin": [],
Pierre-Yves Chibon 73d120
                "commit": [],
Pierre-Yves Chibon 73d120
                "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                "ticket": [],
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "close_status": [
Pierre-Yves Chibon 73d120
                "Invalid",
Pierre-Yves Chibon 73d120
                "Insufficient data",
Pierre-Yves Chibon 73d120
                "Fixed",
Pierre-Yves Chibon 73d120
                "Duplicate",
Slavek Kabrda 3e4583
            ],
Pierre-Yves Chibon 73d120
            "custom_keys": [],
Pierre-Yves Chibon 73d120
            "date_created": "1510742565",
Pierre-Yves Chibon 73d120
            "date_modified": "1510742566",
Pierre-Yves Chibon 73d120
            "description": "test project #1",
Pierre-Yves Chibon 73d120
            "fullname": "test",
Pierre-Yves Chibon 73d120
            "id": 1,
Pierre-Yves Chibon 73d120
            "milestones": {},
Pierre-Yves Chibon 73d120
            "name": "test",
Pierre-Yves Chibon 73d120
            "namespace": None,
Pierre-Yves Chibon 73d120
            "parent": None,
Pierre-Yves Chibon 73d120
            "priorities": {},
Pierre-Yves Chibon 73d120
            "tags": [],
Pierre-Yves Chibon 73d120
            "url_path": "test",
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Slavek Kabrda 3e4583
        }
Slavek Kabrda 3e4583
Slavek Kabrda 3e4583
        self.assertEqual(data, expected_output)
Slavek Kabrda 3e4583
Pierre-Yves Chibon 6ffde7
        # Ensure `foo` was properly removed
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 6ffde7
        self.assertEquals(
Pierre-Yves Chibon 73d120
            project.access_users, {"admin": [], "commit": [], "ticket": []}
Pierre-Yves Chibon 6ffde7
        )
Pierre-Yves Chibon 6ffde7
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 0ede45
class PagureFlaskApiProjectOptionsTests(tests.Modeltests):
Pierre-Yves Chibon 0ede45
    """ Tests for the flask API of pagure for modifying options ofs a project
Pierre-Yves Chibon 0ede45
    """
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 0ede45
    maxDiff = None
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 0ede45
    def setUp(self):
Pierre-Yves Chibon 0ede45
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 0ede45
        super(PagureFlaskApiProjectOptionsTests, self).setUp()
Pierre-Yves Chibon 0ede45
        tests.create_projects(self.session)
Pierre-Yves Chibon 0ede45
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 0ede45
        self.assertEquals(
Pierre-Yves Chibon 73d120
            project.access_users, {"admin": [], "commit": [], "ticket": []}
Pierre-Yves Chibon 0ede45
        )
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 0ede45
    def test_api_get_project_options_wrong_project(self):
Pierre-Yves Chibon 0ede45
        """ Test accessing api_get_project_options w/o auth header. """
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/unknown/options", headers=headers)
Pierre-Yves Chibon 0ede45
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 0ede45
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ede45
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 0ede45
        )
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 0ede45
    def test_api_get_project_options_wo_header(self):
Pierre-Yves Chibon 0ede45
        """ Test accessing api_get_project_options w/o auth header. """
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/options")
Pierre-Yves Chibon 0ede45
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 0ede45
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ede45
        self.assertEqual(
Pierre-Yves Chibon 0ede45
            data,
Pierre-Yves Chibon 0ede45
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or expired token. Please visit "
Pierre-Yves Chibon 73d120
                "http://localhost.localdomain/settings#api-keys to get "
Pierre-Yves Chibon 73d120
                "or renew your API token.",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 73d120
                "errors": "Invalid token",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 0ede45
        )
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 0ede45
    def test_api_get_project_options_w_header(self):
Pierre-Yves Chibon 0ede45
        """ Test accessing api_get_project_options w/ auth header. """
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/options", headers=headers)
Pierre-Yves Chibon 0ede45
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ede45
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ede45
        self.assertEqual(
Pierre-Yves Chibon 0ede45
            data,
Pierre-Yves Chibon 0ede45
            {
Pierre-Yves Chibon 73d120
                "settings": {
Pierre-Yves Chibon 73d120
                    "Enforce_signed-off_commits_in_pull-request": False,
Pierre-Yves Chibon 73d120
                    "Minimum_score_to_merge_pull-request": -1,
Pierre-Yves Chibon 73d120
                    "Only_assignee_can_merge_pull-request": False,
Pierre-Yves Chibon 73d120
                    "Web-hooks": None,
Pierre-Yves Chibon 73d120
                    "always_merge": False,
Pierre-Yves Chibon 73d120
                    "disable_non_fast-forward_merges": False,
Pierre-Yves Chibon 73d120
                    "fedmsg_notifications": True,
Pierre-Yves Chibon 73d120
                    "issue_tracker": True,
Pierre-Yves Chibon 73d120
                    "issue_tracker_read_only": False,
Pierre-Yves Chibon 73d120
                    "issues_default_to_private": False,
Pierre-Yves Chibon 73d120
                    "mqtt_notifications": True,
Pierre-Yves Chibon 73d120
                    "notify_on_commit_flag": False,
Pierre-Yves Chibon 73d120
                    "notify_on_pull-request_flag": False,
Pierre-Yves Chibon 73d120
                    "open_metadata_access_to_all": False,
Pierre-Yves Chibon 73d120
                    "project_documentation": False,
Pierre-Yves Chibon 73d120
                    "pull_request_access_only": False,
Pierre-Yves Chibon 73d120
                    "pull_requests": True,
Pierre-Yves Chibon 73d120
                    "stomp_notifications": True,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "ok",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 0ede45
        )
Pierre-Yves Chibon 0ede45
Pierre-Yves Chibon 473ccb
    def test_api_modify_project_options_wrong_project(self):
Pierre-Yves Chibon 473ccb
        """ Test accessing api_modify_project_options w/ an invalid project.
Pierre-Yves Chibon 473ccb
        """
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/unknown/options/update", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 473ccb
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 473ccb
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 473ccb
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 473ccb
        )
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
    def test_api_modify_project_options_wo_header(self):
Pierre-Yves Chibon 473ccb
        """ Test accessing api_modify_project_options w/o auth header. """
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/options/update")
Pierre-Yves Chibon 473ccb
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 473ccb
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 473ccb
        self.assertEqual(
Pierre-Yves Chibon 473ccb
            data,
Pierre-Yves Chibon 473ccb
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or expired token. Please visit "
Pierre-Yves Chibon 73d120
                "http://localhost.localdomain/settings#api-keys to get "
Pierre-Yves Chibon 73d120
                "or renew your API token.",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 73d120
                "errors": "Invalid token",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 473ccb
        )
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
    def test_api_modify_project_options_no_data(self):
Pierre-Yves Chibon 473ccb
        """ Test accessing api_modify_project_options w/ auth header. """
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
        # check before
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/options", headers=headers)
Pierre-Yves Chibon 473ccb
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 473ccb
        before = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 473ccb
        self.assertEqual(
Pierre-Yves Chibon 473ccb
            before,
Pierre-Yves Chibon 473ccb
            {
Pierre-Yves Chibon 73d120
                "settings": {
Pierre-Yves Chibon 73d120
                    "Enforce_signed-off_commits_in_pull-request": False,
Pierre-Yves Chibon 73d120
                    "Minimum_score_to_merge_pull-request": -1,
Pierre-Yves Chibon 73d120
                    "Only_assignee_can_merge_pull-request": False,
Pierre-Yves Chibon 73d120
                    "Web-hooks": None,
Pierre-Yves Chibon 73d120
                    "always_merge": False,
Pierre-Yves Chibon 73d120
                    "disable_non_fast-forward_merges": False,
Pierre-Yves Chibon 73d120
                    "fedmsg_notifications": True,
Pierre-Yves Chibon 73d120
                    "issue_tracker": True,
Pierre-Yves Chibon 73d120
                    "issue_tracker_read_only": False,
Pierre-Yves Chibon 73d120
                    "issues_default_to_private": False,
Pierre-Yves Chibon 73d120
                    "mqtt_notifications": True,
Pierre-Yves Chibon 73d120
                    "notify_on_commit_flag": False,
Pierre-Yves Chibon 73d120
                    "notify_on_pull-request_flag": False,
Pierre-Yves Chibon 73d120
                    "open_metadata_access_to_all": False,
Pierre-Yves Chibon 73d120
                    "project_documentation": False,
Pierre-Yves Chibon 73d120
                    "pull_request_access_only": False,
Pierre-Yves Chibon 73d120
                    "pull_requests": True,
Pierre-Yves Chibon 73d120
                    "stomp_notifications": True,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "ok",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 473ccb
        )
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
        # Do not update anything
Pierre-Yves Chibon 473ccb
        data = {}
Pierre-Yves Chibon 473ccb
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/options/update", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 473ccb
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 473ccb
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 473ccb
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"message": "No settings to change", "status": "ok"}
Pierre-Yves Chibon 473ccb
        )
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
        # check after
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/options", headers=headers)
Pierre-Yves Chibon 473ccb
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 473ccb
        after = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 473ccb
        self.assertEqual(after, before)
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
    def test_api_modify_project_options(self):
Pierre-Yves Chibon 473ccb
        """ Test accessing api_modify_project_options w/ auth header. """
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
        # check before
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/options", headers=headers)
Pierre-Yves Chibon 473ccb
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 473ccb
        before = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 473ccb
        self.assertEqual(
Pierre-Yves Chibon 473ccb
            before,
Pierre-Yves Chibon 473ccb
            {
Pierre-Yves Chibon 73d120
                "settings": {
Pierre-Yves Chibon 73d120
                    "Enforce_signed-off_commits_in_pull-request": False,
Pierre-Yves Chibon 73d120
                    "Minimum_score_to_merge_pull-request": -1,
Pierre-Yves Chibon 73d120
                    "Only_assignee_can_merge_pull-request": False,
Pierre-Yves Chibon 73d120
                    "Web-hooks": None,
Pierre-Yves Chibon 73d120
                    "always_merge": False,
Pierre-Yves Chibon 73d120
                    "disable_non_fast-forward_merges": False,
Pierre-Yves Chibon 73d120
                    "fedmsg_notifications": True,
Pierre-Yves Chibon 73d120
                    "issue_tracker": True,
Pierre-Yves Chibon 73d120
                    "issue_tracker_read_only": False,
Pierre-Yves Chibon 73d120
                    "issues_default_to_private": False,
Pierre-Yves Chibon 73d120
                    "mqtt_notifications": True,
Pierre-Yves Chibon 73d120
                    "notify_on_commit_flag": False,
Pierre-Yves Chibon 73d120
                    "notify_on_pull-request_flag": False,
Pierre-Yves Chibon 73d120
                    "open_metadata_access_to_all": False,
Pierre-Yves Chibon 73d120
                    "project_documentation": False,
Pierre-Yves Chibon 73d120
                    "pull_request_access_only": False,
Pierre-Yves Chibon 73d120
                    "pull_requests": True,
Pierre-Yves Chibon 73d120
                    "stomp_notifications": True,
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "ok",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 473ccb
        )
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
        # Update: `issues_default_to_private`.
Pierre-Yves Chibon 473ccb
        data = {"issues_default_to_private": True}
Pierre-Yves Chibon 473ccb
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/options/update", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 473ccb
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 473ccb
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 473ccb
        self.assertEqual(
Pierre-Yves Chibon 473ccb
            data,
Pierre-Yves Chibon 473ccb
            {
Pierre-Yves Chibon 73d120
                "message": "Edited successfully settings of repo: test",
Pierre-Yves Chibon 73d120
                "status": "ok",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 473ccb
        )
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 473ccb
        # check after
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/options", headers=headers)
Pierre-Yves Chibon 473ccb
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 473ccb
        after = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 473ccb
        self.assertNotEqual(before, after)
Pierre-Yves Chibon 473ccb
        before["settings"]["issues_default_to_private"] = True
Pierre-Yves Chibon 473ccb
        self.assertEqual(after, before)
Pierre-Yves Chibon 473ccb
Pierre-Yves Chibon 73d120
Fabien Boucher 0bf7de
class PagureFlaskApiProjectCreateAPITokenTests(tests.Modeltests):
Fabien Boucher 0bf7de
    """ Tests for the flask API of pagure for creating user project API token
Fabien Boucher 0bf7de
    """
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
    maxDiff = None
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
    def setUp(self):
Fabien Boucher 0bf7de
        """ Set up the environnment, ran before every tests. """
Fabien Boucher 0bf7de
        super(PagureFlaskApiProjectCreateAPITokenTests, self).setUp()
Fabien Boucher 0bf7de
        tests.create_projects(self.session)
Fabien Boucher 0bf7de
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
    def test_api_createapitoken_as_owner(self):
Fabien Boucher 53b792
        """ Test accessing api_project_create_token as owner. """
Fabien Boucher 0bf7de
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        tdescription = "my new token"
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Call the api with pingou user token and verify content
Fabien Boucher 0bf7de
        data = {
Pierre-Yves Chibon 73d120
            "description": tdescription,
Pierre-Yves Chibon 73d120
            "acls": ["pull_request_merge", "pull_request_comment"],
Fabien Boucher 0bf7de
        }
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/token/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bf7de
        self.assertEqual(output.status_code, 200)
Fabien Boucher 0bf7de
        data = json.loads(output.get_data(as_text=True))
Fabien Boucher 0bf7de
        tid = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, None, description=tdescription
Pierre-Yves Chibon 73d120
        )[0].id
Fabien Boucher 0bf7de
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"token": {"description": tdescription, "id": tid}}
Fabien Boucher 0bf7de
        )
Fabien Boucher 0bf7de
        # Create a second token but with faulty acl
Fabien Boucher 0bf7de
        # Call the api with pingou user token and error code
Pierre-Yves Chibon 73d120
        data = {"description": tdescription, "acl": ["foo", "bar"]}
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/token/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bf7de
        self.assertEqual(output.status_code, 400)
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
    def test_api_createapitoken_as_admin(self):
Fabien Boucher 53b792
        """ Test accessing api_project_create_token as admin. """
Fabien Boucher 0bf7de
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Set the foo user as test project admin
Fabien Boucher 0bf7de
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            project,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Fabien Boucher 0bf7de
        )
Fabien Boucher 0bf7de
        self.session.commit()
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Create modify_project token for foo user
Fabien Boucher 579887
        token = pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon 73d120
            self.session, project=None, acls=["modify_project"], username="foo"
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Call the connector with foo user token and verify content
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token %s" % token.id}
Pierre-Yves Chibon 73d120
        tdescription = "my new token"
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Call the api with pingou user token and verify content
Fabien Boucher 0bf7de
        data = {
Pierre-Yves Chibon 73d120
            "description": tdescription,
Pierre-Yves Chibon 73d120
            "acls": ["pull_request_merge", "pull_request_comment"],
Fabien Boucher 0bf7de
        }
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/token/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bf7de
        self.assertEqual(output.status_code, 200)
Fabien Boucher 0bf7de
        data = json.loads(output.get_data(as_text=True))
Fabien Boucher 0bf7de
        tid = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, None, user="foo", description=tdescription
Pierre-Yves Chibon 73d120
        )[0].id
Fabien Boucher 0bf7de
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"token": {"description": tdescription, "id": tid}}
Fabien Boucher 0bf7de
        )
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
    def test_api_createapitoken_as_unauthorized(self):
Fabien Boucher 53b792
        """ Test accessing api_project_create_token as project admin
Fabien Boucher 0bf7de
        but with unauthorized token ACL.
Fabien Boucher 0bf7de
        """
Fabien Boucher 0bf7de
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Set the foo user as test project admin
Fabien Boucher 0bf7de
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            project,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Fabien Boucher 0bf7de
        )
Fabien Boucher 0bf7de
        self.session.commit()
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Create modify_project token for foo user
Fabien Boucher 0bf7de
        pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon 73d120
            self.session, project=None, acls=["create_branch"], username="foo"
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bf7de
        mtoken = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, ["create_branch"], user="foo"
Pierre-Yves Chibon 73d120
        )[0]
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Call the connector with foo user token and verify content
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token %s" % mtoken.id}
Pierre-Yves Chibon 73d120
        tdescription = "my new token"
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Call the api with pingou user token and verify content
Fabien Boucher 0bf7de
        data = {
Pierre-Yves Chibon 73d120
            "description": tdescription,
Pierre-Yves Chibon 73d120
            "acls": ["pull_request_merge", "pull_request_comment"],
Fabien Boucher 0bf7de
        }
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/token/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bf7de
        self.assertEqual(output.status_code, 401)
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
    def test_api_createapitoken_as_unauthorized_2(self):
Fabien Boucher 53b792
        """ Test accessing api_project_create_token as project user
Fabien Boucher 0bf7de
        with unauthorized token ACL.
Fabien Boucher 0bf7de
        """
Fabien Boucher 0bf7de
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Set the foo user as test project admin
Fabien Boucher 0bf7de
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            project,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Fabien Boucher 0bf7de
        )
Fabien Boucher 0bf7de
        self.session.commit()
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Create modify_project token for foo user
Fabien Boucher 0bf7de
        pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon 73d120
            self.session, project=None, acls=["modify_project"], username="foo"
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bf7de
        mtoken = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, ["modify_project"], user="foo"
Pierre-Yves Chibon 73d120
        )[0]
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Call the connector with foo user token and verify content
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token %s" % mtoken.id}
Pierre-Yves Chibon 73d120
        tdescription = "my new token"
Fabien Boucher 0bf7de
Fabien Boucher 0bf7de
        # Call the api with pingou user token and verify content
Fabien Boucher 0bf7de
        data = {
Pierre-Yves Chibon 73d120
            "description": tdescription,
Pierre-Yves Chibon 73d120
            "acls": ["pull_request_merge", "pull_request_comment"],
Fabien Boucher 0bf7de
        }
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/token/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bf7de
        self.assertEqual(output.status_code, 401)
Fabien Boucher 0bf7de
Pierre-Yves Chibon 0ede45
Fabien Boucher dfb30c
class PagureFlaskApiProjectConnectorTests(tests.Modeltests):
Fabien Boucher dfb30c
    """ Tests for the flask API of pagure for getting connector of a project
Fabien Boucher dfb30c
    """
Fabien Boucher dfb30c
Fabien Boucher dfb30c
    maxDiff = None
Fabien Boucher dfb30c
Fabien Boucher dfb30c
    def setUp(self):
Fabien Boucher dfb30c
        """ Set up the environnment, ran before every tests. """
Fabien Boucher dfb30c
        super(PagureFlaskApiProjectConnectorTests, self).setUp()
Fabien Boucher dfb30c
        tests.create_projects(self.session)
Fabien Boucher dfb30c
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, "aaabbbcccddd", "modify_project")
Fabien Boucher dfb30c
Fabien Boucher dfb30c
    def test_api_get_project_connector_as_owner(self):
Fabien Boucher dfb30c
        """ Test accessing api_get_project_connector as project owner. """
Fabien Boucher dfb30c
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Create witness project Token for pingou user
Fabien Boucher dfb30c
        pagure.lib.query.add_token_to_user(
Fabien Boucher dfb30c
            self.session,
Fabien Boucher dfb30c
            project=project,
Pierre-Yves Chibon 73d120
            acls=["pull_request_merge"],
Pierre-Yves Chibon 73d120
            username="pingou",
Pierre-Yves Chibon 73d120
        )
Fabien Boucher dfb30c
        ctokens = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, ["pull_request_merge"], user="pingou"
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bccc8
        self.assertEqual(len(ctokens), 1)
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Call the connector with pingou user token and verify content
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/connector", headers=headers)
Fabien Boucher dfb30c
        self.assertEqual(output.status_code, 200)
Fabien Boucher dfb30c
        data = json.loads(output.get_data(as_text=True))
Fabien Boucher dfb30c
        self.assertEqual(
Fabien Boucher dfb30c
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "connector": {
Pierre-Yves Chibon 73d120
                    "hook_token": project.hook_token,
Pierre-Yves Chibon 73d120
                    "api_tokens": [
Pierre-Yves Chibon 73d120
                        {
Pierre-Yves Chibon 73d120
                            "description": t.description,
Pierre-Yves Chibon 73d120
                            "id": t.id,
Pierre-Yves Chibon 73d120
                            "expired": False,
Pierre-Yves Chibon 73d120
                        }
Pierre-Yves Chibon 73d120
                        for t in ctokens
Pierre-Yves Chibon 73d120
                    ],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "ok",
Fabien Boucher dfb30c
            },
Fabien Boucher dfb30c
        )
Fabien Boucher dfb30c
Fabien Boucher dfb30c
    def test_api_get_project_connector_as_admin(self):
Fabien Boucher dfb30c
        """ Test accessing api_get_project_connector as project admin """
Fabien Boucher dfb30c
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Set the foo user as test project admin
Fabien Boucher dfb30c
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            project,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Fabien Boucher dfb30c
        )
Fabien Boucher dfb30c
        self.session.commit()
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Create modify_project token for foo user
Fabien Boucher dfb30c
        pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon 73d120
            self.session, project=None, acls=["modify_project"], username="foo"
Pierre-Yves Chibon 73d120
        )
Fabien Boucher dfb30c
        mtoken = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, ["modify_project"], user="foo"
Pierre-Yves Chibon 73d120
        )[0]
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Create witness project Token for foo user
Fabien Boucher dfb30c
        pagure.lib.query.add_token_to_user(
Fabien Boucher dfb30c
            self.session,
Fabien Boucher dfb30c
            project=project,
Pierre-Yves Chibon 73d120
            acls=["pull_request_merge"],
Pierre-Yves Chibon 73d120
            username="foo",
Pierre-Yves Chibon 73d120
        )
Fabien Boucher dfb30c
        ctokens = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, ["pull_request_merge"], user="foo"
Pierre-Yves Chibon 73d120
        )
Fabien Boucher 0bccc8
        self.assertEqual(len(ctokens), 1)
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Call the connector with foo user token and verify content
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token %s" % mtoken.id}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/connector", headers=headers)
Fabien Boucher dfb30c
        self.assertEqual(output.status_code, 200)
Fabien Boucher dfb30c
        data = json.loads(output.get_data(as_text=True))
Fabien Boucher dfb30c
        self.assertEqual(
Fabien Boucher dfb30c
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "connector": {
Pierre-Yves Chibon 73d120
                    "hook_token": project.hook_token,
Pierre-Yves Chibon 73d120
                    "api_tokens": [
Pierre-Yves Chibon 73d120
                        {
Pierre-Yves Chibon 73d120
                            "description": t.description,
Pierre-Yves Chibon 73d120
                            "id": t.id,
Pierre-Yves Chibon 73d120
                            "expired": False,
Pierre-Yves Chibon 73d120
                        }
Pierre-Yves Chibon 73d120
                        for t in ctokens
Pierre-Yves Chibon 73d120
                    ],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "ok",
Fabien Boucher dfb30c
            },
Fabien Boucher dfb30c
        )
Fabien Boucher dfb30c
Fabien Boucher dfb30c
    def test_api_get_project_connector_as_unauthorized(self):
Fabien Boucher dfb30c
        """ Test accessing api_get_project_connector as project admin
Fabien Boucher dfb30c
        but with unauthorized token ACL
Fabien Boucher dfb30c
        """
Fabien Boucher dfb30c
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Set the foo user as test project admin
Fabien Boucher dfb30c
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            project,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Fabien Boucher dfb30c
        )
Fabien Boucher dfb30c
        self.session.commit()
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Create modify_project token for foo user
Fabien Boucher dfb30c
        pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon 73d120
            self.session, project=None, acls=["create_project"], username="foo"
Pierre-Yves Chibon 73d120
        )
Fabien Boucher dfb30c
        mtoken = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, ["create_project"], user="foo"
Pierre-Yves Chibon 73d120
        )[0]
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Call the connector with foo user token and verify unauthorized
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token %s" % mtoken.id}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/connector", headers=headers)
Fabien Boucher dfb30c
        self.assertEqual(output.status_code, 401)
Fabien Boucher dfb30c
Fabien Boucher dfb30c
    def test_api_get_project_connector_as_unauthorized_2(self):
Fabien Boucher dfb30c
        """ Test accessing api_get_project_connector as project
Fabien Boucher dfb30c
        but with unauthorized token ACL
Fabien Boucher dfb30c
        """
Fabien Boucher dfb30c
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Set the foo user as test project admin
Fabien Boucher dfb30c
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            project,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Fabien Boucher dfb30c
        )
Fabien Boucher dfb30c
        self.session.commit()
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Create modify_project token for foo user
Fabien Boucher dfb30c
        pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon 73d120
            self.session, project=None, acls=["modify_project"], username="foo"
Pierre-Yves Chibon 73d120
        )
Fabien Boucher dfb30c
        mtoken = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, ["modify_project"], user="foo"
Pierre-Yves Chibon 73d120
        )[0]
Fabien Boucher dfb30c
Fabien Boucher dfb30c
        # Call the connector with foo user token and verify unauthorized
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token %s" % mtoken.id}
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/connector", headers=headers)
Fabien Boucher dfb30c
        self.assertEqual(output.status_code, 401)
Fabien Boucher dfb30c
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)