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 b63f28
 (c) 2015-2017 - 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>
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
"""
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon f15f18
import pkg_resources
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
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
Pierre-Yves Chibon f15f18
import pygit2
Pierre-Yves Chibon f15f18
from mock import patch
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon f15f18
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
import pagure.lib
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
Pierre-Yves Chibon f15f18
    def setUp(self):
Pierre-Yves Chibon f15f18
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon f15f18
        super(PagureFlaskApiProjecttests, self).setUp()
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon f15f18
        pagure.SESSION = self.session
Pierre-Yves Chibon f15f18
        pagure.api.SESSION = self.session
Pierre-Yves Chibon f15f18
        pagure.api.project.SESSION = self.session
Pierre-Yves Chibon f15f18
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon f15f18
Jeremy Cline 20109f
        pagure.APP.config['GIT_FOLDER'] = os.path.join(self.path, 'repos')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'requests')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'tickets')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'docs')
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon f15f18
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
Jeremy Cline 20109f
        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 f15f18
        newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
Pierre-Yves Chibon f15f18
        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 f15f18
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon f15f18
            stream.write('foo\n bar')
Pierre-Yves Chibon f15f18
        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 f15f18
        author = pygit2.Signature(
Pierre-Yves Chibon f15f18
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon f15f18
        committer = pygit2.Signature(
Pierre-Yves Chibon f15f18
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon f15f18
        clone_repo.create_commit(
Pierre-Yves Chibon f15f18
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon f15f18
            author,
Pierre-Yves Chibon f15f18
            committer,
Pierre-Yves Chibon f15f18
            '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 f15f18
            []
Pierre-Yves Chibon f15f18
        )
Pierre-Yves Chibon f15f18
        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 f15f18
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon f15f18
        tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0)
Pierre-Yves Chibon f15f18
        repo.create_tag(
Pierre-Yves Chibon f15f18
            "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger,
Pierre-Yves Chibon f15f18
            "Release 0.0.1")
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 0197ec
        # Check tags
Pierre-Yves Chibon f15f18
        output = self.app.get('/api/0/test/git/tags')
Pierre-Yves Chibon f15f18
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f15f18
        data = json.loads(output.data)
Pierre-Yves Chibon f15f18
        self.assertDictEqual(
Pierre-Yves Chibon f15f18
            data,
Pierre-Yves Chibon 226628
            {'tags': ['0.0.1'], 'total_tags': 1}
Pierre-Yves Chibon f15f18
        )
Pierre-Yves Chibon f15f18
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)
Matt Prahl cd940d
        repo_path = os.path.join(self.path, 'repos', 'test.git')
Matt Prahl cd940d
        tests.add_content_git_repo(repo_path)
Matt Prahl cd940d
        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
Matt Prahl cd940d
        for branch in ['pats-win-49', 'pats-win-51']:
Matt Prahl cd940d
            clone_repo.create_branch(branch, clone_repo.head.get_object())
Matt Prahl cd940d
            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
Matt Prahl cd940d
        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)
Matt Prahl cd940d
        data = json.loads(output.data)
Matt Prahl cd940d
        self.assertDictEqual(
Matt Prahl cd940d
            data,
Matt Prahl cd940d
            {
Matt Prahl cd940d
                'branches': ['master', 'pats-win-49', 'pats-win-51'],
Matt Prahl cd940d
                'total_branches': 3
Matt Prahl cd940d
            }
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)
Matt Prahl cd940d
        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
Matt Prahl cd940d
        output = self.app.get('/api/0/test/git/branches')
Matt Prahl cd940d
        self.assertEqual(output.status_code, 200)
Matt Prahl cd940d
        data = json.loads(output.data)
Matt Prahl cd940d
        self.assertDictEqual(
Matt Prahl cd940d
            data,
Matt Prahl cd940d
            {
Matt Prahl cd940d
                'branches': [],
Matt Prahl cd940d
                'total_branches': 0
Matt Prahl cd940d
            }
Matt Prahl cd940d
        )
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)
Matt Prahl cd940d
        output = self.app.get('/api/0/test/git/branches')
Matt Prahl cd940d
        self.assertEqual(output.status_code, 404)
Matt Prahl cd940d
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
Farhaan Bukhsh 72e9db
        repo = pagure.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
Mark Reynolds eabdc8
        output = pagure.lib.update_tags(
Pierre-Yves Chibon ceb262
            self.session, repo, 'infra', 'pingou',
Pierre-Yves Chibon a18547
            None)
Pierre-Yves Chibon a18547
        self.assertEqual(output, ['Issue tagged with: infra'])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Check after adding
Farhaan Bukhsh 72e9db
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon ceb262
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon ceb262
        self.assertEqual(repo.tags_text, ['infra'])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Check the API
Pierre-Yves Chibon ceb262
        output = self.app.get('/api/0/projects?tags=inf')
Pierre-Yves Chibon ceb262
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ceb262
        data = json.loads(output.data)
Pierre-Yves Chibon ceb262
        self.assertDictEqual(
Pierre-Yves Chibon ceb262
            data,
Pierre-Yves Chibon ceb262
            {'error_code': 'ENOPROJECTS', 'error': 'No projects found'}
Pierre-Yves Chibon ceb262
        )
Pierre-Yves Chibon ceb262
        output = self.app.get('/api/0/projects?tags=infra')
Pierre-Yves Chibon ceb262
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ceb262
        data = json.loads(output.data)
Pierre-Yves Chibon 07a462
        data['projects'][0]['date_created'] = "1436527638"
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl ada420
            "args": {
Matt Prahl ada420
                "fork": None,
Matt Prahl ada420
                "namespace": None,
Matt Prahl ada420
                "pattern": None,
Matt Prahl ada420
                "tags": ["infra"],
Matt Prahl ada420
                "username": None
Matt Prahl 6bf79a
            },
Matt Prahl ada420
            "projects": [{
Matt Prahl ada420
                "access_groups": {
Matt Prahl ada420
                    "admin": [],
Matt Prahl ada420
                    "commit": [],
Matt Prahl ada420
                    "ticket": []},
Matt Prahl ada420
                "access_users": {
Matt Prahl ada420
                     "admin": [],
Matt Prahl ada420
                     "commit": [],
Matt Prahl ada420
                     "owner": ["pingou"],
Matt Prahl ada420
                     "ticket": []},
Matt Prahl ada420
                "close_status": [
Matt Prahl ada420
                    "Invalid",
Matt Prahl ada420
                    "Insufficient data",
Matt Prahl ada420
                    "Fixed",
Matt Prahl ada420
                    "Duplicate"
Pierre-Yves Chibon e33592
                ],
Matt Prahl ada420
                "custom_keys": [],
Matt Prahl ada420
                "date_created": "1436527638",
Matt Prahl ada420
                "description": "test project #1",
Matt Prahl ada420
                "fullname": "test",
Matt Prahl ada420
                "id": 1,
Matt Prahl ada420
                "milestones": {},
Matt Prahl ada420
                "name": "test",
Matt Prahl ada420
                "namespace": None,
Matt Prahl ada420
                "parent": None,
Matt Prahl ada420
                "priorities": {},
Matt Prahl ada420
                "tags": ["infra"],
Matt Prahl ada420
                "user": {
Matt Prahl ada420
                    "fullname": "PY C",
Matt Prahl ada420
                    "name": "pingou"
Pierre-Yves Chibon 07a462
                }
Matt Prahl 6bf79a
            }],
Matt Prahl ada420
            "total_projects": 1
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Matt Prahl 6bf79a
Pierre-Yves Chibon 7485e2
        output = self.app.get('/api/0/projects?username=pingou')
Pierre-Yves Chibon 7485e2
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 7485e2
        data = json.loads(output.data)
Pierre-Yves Chibon 07a462
        data['projects'][0]['date_created'] = "1436527638"
Pierre-Yves Chibon 07a462
        data['projects'][1]['date_created'] = "1436527638"
clime afed57
        data['projects'][2]['date_created'] = "1436527638"
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl 6bf79a
            "args": {
Pierre-Yves Chibon e33592
                "fork": None,
Matt Prahl ada420
                "namespace": None,
Pierre-Yves Chibon e33592
                "pattern": None,
Pierre-Yves Chibon e33592
                "tags": [],
Matt Prahl 6bf79a
                "username": "pingou"
Matt Prahl 6bf79a
            },
Matt Prahl 6bf79a
            "projects": [
Pierre-Yves Chibon 07a462
                {
Matt Prahl 6bf79a
                    "access_groups": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "ticket": []},
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Matt Prahl 6bf79a
                        "Duplicate"
Pierre-Yves Chibon f254bf
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1436527638",
Matt Prahl 6bf79a
                    "description": "test project #1",
Matt Prahl 6bf79a
                    "fullname": "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"],
Matt Prahl 6bf79a
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Matt Prahl 6bf79a
                    }
Pierre-Yves Chibon 07a462
                },
Pierre-Yves Chibon 07a462
                {
Matt Prahl 6bf79a
                    "access_groups": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Matt Prahl 6bf79a
                        "Duplicate"
Pierre-Yves Chibon f254bf
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1436527638",
Matt Prahl 6bf79a
                    "description": "test project #2",
Matt Prahl 6bf79a
                    "fullname": "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": [],
Matt Prahl 6bf79a
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Matt Prahl 6bf79a
                    }
clime afed57
                },
clime afed57
                {
Matt Prahl 6bf79a
                    "access_groups": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "ticket": []},
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Matt Prahl 6bf79a
                        "ticket": []},
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Matt Prahl 6bf79a
                        "Duplicate"
clime afed57
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1436527638",
Matt Prahl 6bf79a
                    "description": "namespaced test project",
Matt Prahl 6bf79a
                    "fullname": "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": [],
Matt Prahl 6bf79a
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Matt Prahl 6bf79a
                    }
Pierre-Yves Chibon 07a462
                }
Matt Prahl 6bf79a
            ],
Matt Prahl 6bf79a
            "total_projects": 3
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Matt Prahl 6bf79a
Pierre-Yves Chibon 7485e2
        output = self.app.get('/api/0/projects?username=pingou&tags=infra')
Pierre-Yves Chibon 7485e2
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 7485e2
        data = json.loads(output.data)
Pierre-Yves Chibon 07a462
        data['projects'][0]['date_created'] = "1436527638"
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl 6bf79a
            "args": {
Pierre-Yves Chibon e33592
                "fork": None,
Matt Prahl ada420
                "namespace": None,
Pierre-Yves Chibon e33592
                "pattern": None,
Matt Prahl 6bf79a
                "tags": ["infra"],
Matt Prahl 6c3d54
                "username": "pingou",
Matt Prahl 6bf79a
            },
Matt Prahl 6bf79a
            "projects": [{
Matt Prahl 6bf79a
                "access_groups": {
Matt Prahl 6bf79a
                    "admin": [],
Matt Prahl 6bf79a
                    "commit": [],
Matt Prahl 6bf79a
                    "ticket": []
Matt Prahl 6bf79a
                },
Matt Prahl 6bf79a
                "access_users": {
Matt Prahl 6bf79a
                    "admin": [],
Matt Prahl 6bf79a
                    "commit": [],
Matt Prahl 6bf79a
                    "owner": ["pingou"],
Matt Prahl 6bf79a
                    "ticket": []},
Matt Prahl 6bf79a
                "close_status": [
Matt Prahl 6bf79a
                    "Invalid",
Matt Prahl 6bf79a
                    "Insufficient data",
Matt Prahl 6bf79a
                    "Fixed",
Matt Prahl 6bf79a
                    "Duplicate"],
Matt Prahl 6bf79a
                "custom_keys": [],
Matt Prahl 6bf79a
                "date_created": "1436527638",
Matt Prahl 6bf79a
                "description": "test project #1",
Matt Prahl 6bf79a
                "fullname": "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"],
Matt Prahl 6bf79a
                "user": {
Pierre-Yves Chibon 07a462
                    "fullname": "PY C",
Pierre-Yves Chibon 07a462
                    "name": "pingou"
Pierre-Yves Chibon 07a462
                }
Matt Prahl 6bf79a
            }],
Matt Prahl 6bf79a
            "total_projects": 1
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon ceb262
Matt Prahl 6c3d54
        output = self.app.get('/api/0/projects?namespace=somenamespace')
Matt Prahl 6c3d54
        self.assertEqual(output.status_code, 200)
Matt Prahl 6c3d54
        data = json.loads(output.data)
Matt Prahl 6c3d54
        data['projects'][0]['date_created'] = "1436527638"
Matt Prahl 6c3d54
        expected_data = {
Matt Prahl 6c3d54
            "args": {
Matt Prahl 6c3d54
                "fork": None,
Matt Prahl ada420
                "namespace": "somenamespace",
Matt Prahl 6c3d54
                "pattern": None,
Matt Prahl 6c3d54
                "tags": [],
Matt Prahl 6c3d54
                "username": None
Matt Prahl 6c3d54
            },
Matt Prahl 6c3d54
            "projects": [
Matt Prahl 6c3d54
                {
Matt Prahl 6c3d54
                    "access_groups": {
Matt Prahl 6c3d54
                        "admin": [],
Matt Prahl 6c3d54
                        "commit": [],
Matt Prahl 6c3d54
                        "ticket": []},
Matt Prahl 6c3d54
                    "access_users": {
Matt Prahl 6c3d54
                        "admin": [],
Matt Prahl 6c3d54
                        "commit": [],
Matt Prahl 6c3d54
                        "owner": ["pingou"],
Matt Prahl 6c3d54
                        "ticket": []},
Matt Prahl 6c3d54
                    "close_status": [
Matt Prahl 6c3d54
                        "Invalid",
Matt Prahl 6c3d54
                        "Insufficient data",
Matt Prahl 6c3d54
                        "Fixed",
Matt Prahl 6c3d54
                        "Duplicate"
Matt Prahl 6c3d54
                    ],
Matt Prahl 6c3d54
                    "custom_keys": [],
Matt Prahl 6c3d54
                    "date_created": "1436527638",
Matt Prahl 6c3d54
                    "description": "namespaced test project",
Matt Prahl 6c3d54
                    "fullname": "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": [],
Matt Prahl 6c3d54
                    "user": {
Matt Prahl 6c3d54
                        "fullname": "PY C",
Matt Prahl 6c3d54
                        "name": "pingou"
Matt Prahl 6c3d54
                    }
Matt Prahl 6c3d54
                }
Matt Prahl 6c3d54
            ],
Matt Prahl 6c3d54
            "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
Farhaan Bukhsh 481209
        repo = pagure.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
Mark Reynolds eabdc8
        output = pagure.lib.update_tags(
Pierre-Yves Chibon b42ba1
            self.session, repo, 'infra', 'pingou',
Pierre-Yves Chibon a18547
            ticketfolder=None)
Pierre-Yves Chibon a18547
        self.assertEqual(output, ['Issue tagged with: infra'])
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Check after adding
Farhaan Bukhsh 481209
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon b42ba1
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon b42ba1
        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 b42ba1
        output = self.app.get('/api/0/random')
Pierre-Yves Chibon b42ba1
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon b42ba1
        data = json.loads(output.data)
Pierre-Yves Chibon b42ba1
        self.assertDictEqual(
Pierre-Yves Chibon b42ba1
            data,
Pierre-Yves Chibon b42ba1
            {'error_code': 'ENOPROJECT', 'error': 'Project not found'}
Pierre-Yves Chibon b42ba1
        )
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Existing project
Pierre-Yves Chibon b42ba1
        output = self.app.get('/api/0/test')
Pierre-Yves Chibon b42ba1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b42ba1
        data = json.loads(output.data)
Pierre-Yves Chibon b42ba1
        data['date_created'] = "1436527638"
Matt Prahl 6bf79a
        expected_data ={
Matt Prahl 6bf79a
            "access_groups": {
Matt Prahl 6bf79a
                "admin": [],
Matt Prahl 6bf79a
                "commit": [],
Matt Prahl 6bf79a
                "ticket": []
Matt Prahl 6bf79a
            },
Matt Prahl 6bf79a
            "access_users": {
Matt Prahl 6bf79a
                "admin": [],
Matt Prahl 6bf79a
                "commit": [],
Matt Prahl 6bf79a
                "owner": ["pingou"],
Matt Prahl 6bf79a
                "ticket": []},
Matt Prahl 6bf79a
            "close_status": [
Pierre-Yves Chibon b42ba1
                "Invalid",
Pierre-Yves Chibon b42ba1
                "Insufficient data",
Pierre-Yves Chibon b42ba1
                "Fixed",
Pierre-Yves Chibon b42ba1
                "Duplicate"
Matt Prahl 6bf79a
            ],
Matt Prahl 6bf79a
            "custom_keys": [],
Matt Prahl 6bf79a
            "date_created": "1436527638",
Matt Prahl 6bf79a
            "description": "test project #1",
Matt Prahl 6bf79a
            "fullname": "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"],
Matt Prahl 6bf79a
            "user": {
Pierre-Yves Chibon b42ba1
                "fullname": "PY C",
Pierre-Yves Chibon b42ba1
                "name": "pingou"
Pierre-Yves Chibon b42ba1
            }
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon 1c1b5d
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon 1c1b5d
    def test_api_new_project(self, p_gga):
Pierre-Yves Chibon 1c1b5d
        """ Test the api_new_project method of the flask api. """
Pierre-Yves Chibon 1c1b5d
        p_gga.return_value = True
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon b63f28
        tests.create_projects(self.session)
Jeremy Cline 20109f
        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 1c1b5d
        headers = {'Authorization': 'token foo_token'}
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Invalid token
Pierre-Yves Chibon 1c1b5d
        output = self.app.post('/api/0/new', headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Jeremy Cline 099538
                         data['error_code'])
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # No input
Pierre-Yves Chibon 1c1b5d
        output = self.app.post('/api/0/new', headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 1c1b5d
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 1c1b5d
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {
Pierre-Yves Chibon f7fcaa
                "name": ["This field is required."],
Pierre-Yves Chibon f7fcaa
                "description": ["This field is required."]
Pierre-Yves Chibon f7fcaa
              }
Pierre-Yves Chibon 1c1b5d
            }
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        data = {
Pierre-Yves Chibon 1c1b5d
            'name': 'test',
Pierre-Yves Chibon 1c1b5d
        }
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Incomplete request
Pierre-Yves Chibon 1c1b5d
        output = self.app.post(
Pierre-Yves Chibon 1c1b5d
            '/api/0/new', data=data, headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 1c1b5d
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 1c1b5d
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {"description": ["This field is required."]}
Pierre-Yves Chibon 1c1b5d
            }
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        data = {
Pierre-Yves Chibon 1c1b5d
            'name': 'test',
Pierre-Yves Chibon 1c1b5d
            'description': 'Just a small test project',
Pierre-Yves Chibon 1c1b5d
        }
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Valid request but repo already exists
Pierre-Yves Chibon 1c1b5d
        output = self.app.post(
Pierre-Yves Chibon 1c1b5d
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon b63f28
                "error": "The project repo \"test\" already exists "
Pierre-Yves Chibon b63f28
                    "in the database",
Pierre-Yves Chibon 1c1b5d
                "error_code": "ENOCODE"
Pierre-Yves Chibon 1c1b5d
            }
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        data = {
Pierre-Yves Chibon 1c1b5d
            'name': 'test_42',
Pierre-Yves Chibon 1c1b5d
            'description': 'Just another small test project',
Pierre-Yves Chibon 1c1b5d
        }
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Valid request
Pierre-Yves Chibon 1c1b5d
        output = self.app.post(
Pierre-Yves Chibon 1c1b5d
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {'message': 'Project "test_42" created'}
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 3a7944
    @patch.dict('pagure.APP.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 3a7944
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon 3a7944
    def test_api_new_project_private(self, p_gga):
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
        p_gga.return_value = True
Pierre-Yves Chibon 3a7944
Pierre-Yves Chibon 3a7944
        tests.create_projects(self.session)
Pierre-Yves Chibon 3a7944
        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 3a7944
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3a7944
Pierre-Yves Chibon 3a7944
        data = {
Pierre-Yves Chibon 3a7944
            'name': 'test',
Pierre-Yves Chibon 3a7944
            'description': 'Just a small test project',
Pierre-Yves Chibon 3a7944
            'private': True,
Pierre-Yves Chibon 3a7944
        }
Pierre-Yves Chibon 3a7944
Pierre-Yves Chibon 3a7944
        # Valid request
Pierre-Yves Chibon 3a7944
        output = self.app.post(
Pierre-Yves Chibon 3a7944
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon 3a7944
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 3a7944
        data = json.loads(output.data)
Pierre-Yves Chibon 3a7944
        self.assertDictEqual(
Pierre-Yves Chibon 3a7944
            data,
Pierre-Yves Chibon 3a7944
            {'message': 'Project "pingou/test" created'}
Pierre-Yves Chibon 3a7944
        )
Pierre-Yves Chibon 3a7944
Pierre-Yves Chibon f0e08e
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon 1dfd94
    def test_api_new_project_user_token(self, p_gga):
Pierre-Yves Chibon 1dfd94
        """ Test the api_new_project method of the flask api. """
Pierre-Yves Chibon 1dfd94
        p_gga.return_value = True
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        tests.create_projects(self.session)
Pierre-Yves Chibon 1dfd94
        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 1dfd94
        headers = {'Authorization': 'token foo_token'}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Invalid token
Pierre-Yves Chibon 1dfd94
        output = self.app.post('/api/0/new', headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 1dfd94
                         data['error_code'])
Pierre-Yves Chibon 1dfd94
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # No input
Pierre-Yves Chibon 1dfd94
        output = self.app.post('/api/0/new', headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 1dfd94
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 1dfd94
              "errors": {
Pierre-Yves Chibon 1dfd94
                "name": ["This field is required."],
Pierre-Yves Chibon 1dfd94
                "description": ["This field is required."]
Pierre-Yves Chibon 1dfd94
              }
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'name': 'test',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Incomplete request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/new', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 1dfd94
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 1dfd94
              "errors": {"description": ["This field is required."]}
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'name': 'test',
Pierre-Yves Chibon 1dfd94
            'description': 'Just a small test project',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid request but repo already exists
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
                "error": "The project repo \"test\" already exists "
Pierre-Yves Chibon 1dfd94
                    "in the database",
Pierre-Yves Chibon 1dfd94
                "error_code": "ENOCODE"
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'name': 'test_42',
Pierre-Yves Chibon 1dfd94
            'description': 'Just another small test project',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {'message': 'Project "test_42" created'}
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 2aa56f
        # Project with a namespace
Pierre-Yves Chibon 2aa56f
        pagure.APP.config['ALLOWED_PREFIX'] = ['rpms']
Pierre-Yves Chibon 2aa56f
        data = {
Pierre-Yves Chibon 2aa56f
            'name': 'test_42',
Pierre-Yves Chibon 2aa56f
            'namespace': 'pingou',
Pierre-Yves Chibon 2aa56f
            'description': 'Just another small test project',
Pierre-Yves Chibon 2aa56f
        }
Pierre-Yves Chibon 2aa56f
Pierre-Yves Chibon 2aa56f
        # Invalid namespace
Pierre-Yves Chibon 2aa56f
        output = self.app.post(
Pierre-Yves Chibon 2aa56f
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon 2aa56f
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 2aa56f
        data = json.loads(output.data)
Pierre-Yves Chibon 2aa56f
        self.assertDictEqual(
Pierre-Yves Chibon 2aa56f
            data,
Pierre-Yves Chibon 2aa56f
            {
Pierre-Yves Chibon 2aa56f
                "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 2aa56f
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 2aa56f
                "errors": {
Pierre-Yves Chibon 2aa56f
                    "namespace": [
Pierre-Yves Chibon 2aa56f
                        "Not a valid choice"
Pierre-Yves Chibon 2aa56f
                    ]
Pierre-Yves Chibon 2aa56f
                }
Pierre-Yves Chibon 2aa56f
            }
Pierre-Yves Chibon 2aa56f
        )
Pierre-Yves Chibon 2aa56f
Pierre-Yves Chibon 2aa56f
        data = {
Pierre-Yves Chibon 2aa56f
            'name': 'test_42',
Pierre-Yves Chibon 2aa56f
            'namespace': 'rpms',
Pierre-Yves Chibon 2aa56f
            'description': 'Just another small test project',
Pierre-Yves Chibon 2aa56f
        }
Pierre-Yves Chibon 2aa56f
Pierre-Yves Chibon 2aa56f
        # All good
Pierre-Yves Chibon 2aa56f
        output = self.app.post(
Pierre-Yves Chibon 2aa56f
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon 2aa56f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2aa56f
        data = json.loads(output.data)
Pierre-Yves Chibon 2aa56f
        self.assertDictEqual(
Pierre-Yves Chibon 2aa56f
            data,
Pierre-Yves Chibon 2aa56f
            {'message': 'Project "rpms/test_42" created'}
Pierre-Yves Chibon 2aa56f
        )
Pierre-Yves Chibon 2aa56f
Pierre-Yves Chibon 1dfd94
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon baec09
    def test_api_new_project_user_ns(self, p_gga):
Pierre-Yves Chibon baec09
        """ Test the api_new_project method of the flask api. """
Pierre-Yves Chibon baec09
        pagure.APP.config['USER_NAMESPACE'] = True
Pierre-Yves Chibon baec09
        p_gga.return_value = True
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        tests.create_projects(self.session)
Pierre-Yves Chibon baec09
        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 baec09
        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 baec09
            'name': 'testproject',
Pierre-Yves Chibon baec09
            'description': 'Just another small test project',
Pierre-Yves Chibon baec09
        }
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Valid request
Pierre-Yves Chibon baec09
        output = self.app.post(
Pierre-Yves Chibon baec09
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon baec09
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon baec09
        data = json.loads(output.data)
Pierre-Yves Chibon baec09
        self.assertDictEqual(
Pierre-Yves Chibon baec09
            data,
Pierre-Yves Chibon baec09
            {'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
        pagure.APP.config['ALLOWED_PREFIX'] = ['testns']
Pierre-Yves Chibon baec09
        data = {
Pierre-Yves Chibon baec09
            'name': 'testproject2',
Pierre-Yves Chibon baec09
            'namespace': 'testns',
Pierre-Yves Chibon baec09
            'description': 'Just another small test project',
Pierre-Yves Chibon baec09
        }
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Valid request
Pierre-Yves Chibon baec09
        output = self.app.post(
Pierre-Yves Chibon baec09
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon baec09
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon baec09
        data = json.loads(output.data)
Pierre-Yves Chibon baec09
        self.assertDictEqual(
Pierre-Yves Chibon baec09
            data,
Pierre-Yves Chibon baec09
            {'message': 'Project "testns/testproject2" created'}
Pierre-Yves Chibon baec09
        )
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        pagure.APP.config['USER_NAMESPACE'] = False
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon f0e08e
    def test_api_fork_project(self, p_gga):
Pierre-Yves Chibon f0e08e
        """ Test the api_fork_project method of the flask api. """
Pierre-Yves Chibon f0e08e
        p_gga.return_value = True
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        tests.create_projects(self.session)
Pierre-Yves Chibon f0e08e
        for folder in ['docs', 'tickets', 'requests', 'repos']:
Pierre-Yves Chibon f0e08e
            tests.create_projects_git(
Jeremy Cline 20109f
                os.path.join(self.path, folder), bare=True)
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 f0e08e
        headers = {'Authorization': 'token foo_token'}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Invalid token
Pierre-Yves Chibon f0e08e
        output = self.app.post('/api/0/fork', headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Jeremy Cline 099538
                         data['error_code'])
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # No input
Pierre-Yves Chibon 1dfd94
        output = self.app.post('/api/0/fork', headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 1dfd94
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 1dfd94
              "errors": {"repo": ["This field is required."]}
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'name': 'test',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Incomplete request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/fork', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 1dfd94
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 1dfd94
              "errors": {"repo": ["This field is required."]}
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'repo': 'test',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
                "message": "Repo \"test\" cloned to \"pingou/test\""
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'repo': 'test',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # project already forked
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
                "error": "Repo \"forks/pingou/test\" already exists",
Pierre-Yves Chibon 1dfd94
                "error_code": "ENOCODE"
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'repo': 'test',
Pierre-Yves Chibon 1dfd94
            'username': 'pingou',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Fork already exists
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
                "error": "Repo \"forks/pingou/test\" already exists",
Pierre-Yves Chibon 1dfd94
                "error_code": "ENOCODE"
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'repo': 'test',
Pierre-Yves Chibon 1dfd94
            'namespace': 'pingou',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Repo does not exists
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
                "error": "Project not found",
Pierre-Yves Chibon 1dfd94
                "error_code": "ENOPROJECT"
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon 1dfd94
    def test_api_fork_project_user_token(self, p_gga):
Pierre-Yves Chibon 1dfd94
        """ Test the api_fork_project method of the flask api. """
Pierre-Yves Chibon 1dfd94
        p_gga.return_value = True
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        tests.create_projects(self.session)
Pierre-Yves Chibon 1dfd94
        for folder in ['docs', 'tickets', 'requests', 'repos']:
Pierre-Yves Chibon 1dfd94
            tests.create_projects_git(
Pierre-Yves Chibon 1dfd94
                os.path.join(self.path, folder), bare=True)
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 1dfd94
        headers = {'Authorization': 'token foo_token'}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Invalid token
Pierre-Yves Chibon 1dfd94
        output = self.app.post('/api/0/fork', headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 1dfd94
        data = json.loads(output.data)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 1dfd94
                         data['error_code'])
Pierre-Yves Chibon 1dfd94
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # No input
Pierre-Yves Chibon f0e08e
        output = self.app.post('/api/0/fork', headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon f0e08e
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {"repo": ["This field is required."]}
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        data = {
Pierre-Yves Chibon f0e08e
            'name': 'test',
Pierre-Yves Chibon f0e08e
        }
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Incomplete request
Pierre-Yves Chibon f0e08e
        output = self.app.post(
Pierre-Yves Chibon f0e08e
            '/api/0/fork', data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon f0e08e
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {"repo": ["This field is required."]}
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        data = {
Pierre-Yves Chibon f0e08e
            'repo': 'test',
Pierre-Yves Chibon f0e08e
        }
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Valid request
Pierre-Yves Chibon f0e08e
        output = self.app.post(
Pierre-Yves Chibon f0e08e
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
                "message": "Repo \"test\" cloned to \"pingou/test\""
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        data = {
Pierre-Yves Chibon f0e08e
            'repo': 'test',
Pierre-Yves Chibon f0e08e
        }
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # project already forked
Pierre-Yves Chibon f0e08e
        output = self.app.post(
Pierre-Yves Chibon f0e08e
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
                "error": "Repo \"forks/pingou/test\" already exists",
Pierre-Yves Chibon f0e08e
                "error_code": "ENOCODE"
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        data = {
Pierre-Yves Chibon f0e08e
            'repo': 'test',
Pierre-Yves Chibon f0e08e
            'username': 'pingou',
Pierre-Yves Chibon f0e08e
        }
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Fork already exists
Pierre-Yves Chibon f0e08e
        output = self.app.post(
Pierre-Yves Chibon f0e08e
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
                "error": "Repo \"forks/pingou/test\" already exists",
Pierre-Yves Chibon f0e08e
                "error_code": "ENOCODE"
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 2c43bb
        data = {
Pierre-Yves Chibon 2c43bb
            'repo': 'test',
Pierre-Yves Chibon 2c43bb
            'namespace': 'pingou',
Pierre-Yves Chibon 2c43bb
        }
Pierre-Yves Chibon 2c43bb
Pierre-Yves Chibon 2c43bb
        # Repo does not exists
Pierre-Yves Chibon 2c43bb
        output = self.app.post(
Pierre-Yves Chibon 2c43bb
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon 2c43bb
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 2c43bb
        data = json.loads(output.data)
Pierre-Yves Chibon 2c43bb
        self.assertDictEqual(
Pierre-Yves Chibon 2c43bb
            data,
Pierre-Yves Chibon 2c43bb
            {
Pierre-Yves Chibon 2c43bb
                "error": "Project not found",
Pierre-Yves Chibon 2c43bb
                "error_code": "ENOPROJECT"
Pierre-Yves Chibon 2c43bb
            }
Pierre-Yves Chibon 2c43bb
        )
Pierre-Yves Chibon 2c43bb
Pierre-Yves Chibon f15f18
if __name__ == '__main__':
Pierre-Yves Chibon f15f18
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon f15f18
        PagureFlaskApiProjecttests)
Pierre-Yves Chibon f15f18
    unittest.TextTestRunner(verbosity=2).run(SUITE)