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>
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 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
Matt Prahl 489ad1
from mock import patch, Mock
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 b130e5
import pagure.flask_app
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 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
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)
Matt Prahl ad5147
        output = self.app.get('/api/0/test/git/urls')
Matt Prahl ad5147
        self.assertEqual(output.status_code, 200)
Matt Prahl ad5147
        expected_rv = {
Matt Prahl ad5147
            'urls': {
Matt Prahl ad5147
                'git': 'git://pagure.org/test.git',
Matt Prahl ad5147
                'ssh': 'ssh://git@pagure.org/test.git'
Matt Prahl ad5147
            },
Matt Prahl ad5147
            'total_urls': 2
Matt Prahl ad5147
        }
Matt Prahl ad5147
        data = json.loads(output.data)
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
        """
Matt Prahl ad5147
        output = self.app.get('/api/0/test1234/git/urls')
Matt Prahl ad5147
        self.assertEqual(output.status_code, 404)
Matt Prahl ad5147
        expected_rv = {
Matt Prahl ad5147
            'error': 'Project not found',
Matt Prahl ad5147
            'error_code': 'ENOPROJECT'
Matt Prahl ad5147
        }
Matt Prahl ad5147
        data = json.loads(output.data)
Matt Prahl ad5147
        self.assertDictEqual(data, expected_rv)
Matt Prahl ad5147
Pierre-Yves Chibon b130e5
    @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)
Matt Prahl ad5147
        tests.create_tokens_acl(self.session, 'aaabbbcccddd')
Matt Prahl ad5147
        headers = {'Authorization': 'token aaabbbcccddd'}
Matt Prahl ad5147
Matt Prahl ad5147
        test_project = pagure.lib._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
Matt Prahl ad5147
        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 = {
Matt Prahl ad5147
            'urls': {
Matt Prahl ad5147
                'git': 'git://pagure.org/test.git',
Matt Prahl ad5147
                'ssh': 'ssh://git@pagure.org/test.git'
Matt Prahl ad5147
            },
Matt Prahl ad5147
            'total_urls': 2
Matt Prahl ad5147
        }
Matt Prahl ad5147
        data = json.loads(output.data)
Matt Prahl ad5147
        self.assertDictEqual(data, expected_rv)
Matt Prahl ad5147
Pierre-Yves Chibon b130e5
    @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)
Matt Prahl ad5147
        test_project = pagure.lib._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
Matt Prahl ad5147
        output = self.app.get('/api/0/test/git/urls')
Matt Prahl ad5147
        self.assertEqual(output.status_code, 404)
Matt Prahl ad5147
        expected_rv = {
Matt Prahl ad5147
            'error': 'Project not found',
Matt Prahl ad5147
            'error_code': 'ENOPROJECT'
Matt Prahl ad5147
        }
Matt Prahl ad5147
        data = json.loads(output.data)
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 1ca90b
        output = self.app.get('/api/0/projects?pattern=test')
Pierre-Yves Chibon 1ca90b
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1ca90b
        data = json.loads(output.data)
Pierre-Yves Chibon 1ca90b
        data['projects'][0]['date_created'] = "1436527638"
Clement Verna cd6e30
        data['projects'][0]['date_modified'] = "1436527638"
Pierre-Yves Chibon 1ca90b
        expected_data = {
Pierre-Yves Chibon 1ca90b
          "args": {
Pierre-Yves Chibon 1ca90b
            "fork": None,
Pierre-Yves Chibon 1ca90b
            "namespace": None,
Pierre-Yves Chibon 1ca90b
            "owner": None,
Pierre-Yves Chibon 1ca90b
            "pattern": "test",
Pierre-Yves Chibon 1ca90b
            "short": False,
Pierre-Yves Chibon 1ca90b
            "tags": [],
Pierre-Yves Chibon 1ca90b
            "username": None
Pierre-Yves Chibon 1ca90b
          },
Pierre-Yves Chibon 1ca90b
          "projects": [
Pierre-Yves Chibon 1ca90b
            {
Pierre-Yves Chibon 1ca90b
              "access_groups": {
Pierre-Yves Chibon 1ca90b
                "admin": [],
Pierre-Yves Chibon 1ca90b
                "commit": [],
Pierre-Yves Chibon 1ca90b
                "ticket": []
Pierre-Yves Chibon 1ca90b
              },
Pierre-Yves Chibon 1ca90b
              "access_users": {
Pierre-Yves Chibon 1ca90b
                "admin": [],
Pierre-Yves Chibon 1ca90b
                "commit": [],
Pierre-Yves Chibon 1ca90b
                "owner": [
Pierre-Yves Chibon 1ca90b
                  "pingou"
Pierre-Yves Chibon 1ca90b
                ],
Pierre-Yves Chibon 1ca90b
                "ticket": []
Pierre-Yves Chibon 1ca90b
              },
Pierre-Yves Chibon 1ca90b
              "close_status": [
Pierre-Yves Chibon 1ca90b
                "Invalid",
Pierre-Yves Chibon 1ca90b
                "Insufficient data",
Pierre-Yves Chibon 1ca90b
                "Fixed",
Pierre-Yves Chibon 1ca90b
                "Duplicate"
Pierre-Yves Chibon 1ca90b
              ],
Pierre-Yves Chibon 1ca90b
              "custom_keys": [],
Pierre-Yves Chibon 1ca90b
              "date_created": "1436527638",
Clement Verna cd6e30
              "date_modified": "1436527638",
Pierre-Yves Chibon 1ca90b
              "description": "test project #1",
Pierre-Yves Chibon 1ca90b
              "fullname": "test",
Pierre-Yves Chibon 469e24
              "url_path": "test",
Pierre-Yves Chibon 1ca90b
              "id": 1,
Pierre-Yves Chibon 1ca90b
              "milestones": {},
Pierre-Yves Chibon 1ca90b
              "name": "test",
Pierre-Yves Chibon 1ca90b
              "namespace": None,
Pierre-Yves Chibon 1ca90b
              "parent": None,
Pierre-Yves Chibon 1ca90b
              "priorities": {},
Pierre-Yves Chibon 1ca90b
              "tags": [],
Pierre-Yves Chibon 1ca90b
              "user": {
Pierre-Yves Chibon 1ca90b
                "fullname": "PY C",
Pierre-Yves Chibon 1ca90b
                "name": "pingou"
Pierre-Yves Chibon 1ca90b
              }
Pierre-Yves Chibon 1ca90b
            }
Pierre-Yves Chibon 1ca90b
          ],
Pierre-Yves Chibon 1ca90b
          "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 1ca90b
        output = self.app.get('/api/0/projects?pattern=te*&short=1')
Pierre-Yves Chibon 1ca90b
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1ca90b
        data = json.loads(output.data)
Pierre-Yves Chibon 1ca90b
        expected_data = {
Pierre-Yves Chibon 1ca90b
          "args": {
Pierre-Yves Chibon 1ca90b
            "fork": None,
Pierre-Yves Chibon 1ca90b
            "namespace": None,
Pierre-Yves Chibon 1ca90b
            "owner": None,
Pierre-Yves Chibon 1ca90b
            "pattern": "te*",
Pierre-Yves Chibon 1ca90b
            "short": True,
Pierre-Yves Chibon 1ca90b
            "tags": [],
Pierre-Yves Chibon 1ca90b
            "username": None
Pierre-Yves Chibon 1ca90b
          },
Pierre-Yves Chibon 1ca90b
          "projects": [
Pierre-Yves Chibon 1ca90b
            {
Pierre-Yves Chibon 1ca90b
              "description": "test project #1",
Pierre-Yves Chibon 1ca90b
              "fullname": "test",
Pierre-Yves Chibon 1ca90b
              "name": "test",
Pierre-Yves Chibon 1ca90b
              "namespace": None
Pierre-Yves Chibon 1ca90b
            },
Pierre-Yves Chibon 1ca90b
            {
Pierre-Yves Chibon 1ca90b
              "description": "test project #2",
Pierre-Yves Chibon 1ca90b
              "fullname": "test2",
Pierre-Yves Chibon 1ca90b
              "name": "test2",
Pierre-Yves Chibon 1ca90b
              "namespace": None
Pierre-Yves Chibon 1ca90b
            },
Pierre-Yves Chibon 1ca90b
            {
Pierre-Yves Chibon 1ca90b
              "description": "namespaced test project",
Pierre-Yves Chibon 1ca90b
              "fullname": "somenamespace/test3",
Pierre-Yves Chibon 1ca90b
              "name": "test3",
Pierre-Yves Chibon 1ca90b
              "namespace": "somenamespace"
Pierre-Yves Chibon 1ca90b
            }
Pierre-Yves Chibon 1ca90b
          ],
Pierre-Yves Chibon 1ca90b
          "total_projects": 3
Pierre-Yves Chibon 1ca90b
        }
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 b130e5
        repo = pagure.lib.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 ef1fe1
        self.assertEqual(output, ['Project tagged with: infra'])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Check after adding
Pierre-Yves Chibon b130e5
        repo = pagure.lib.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')
Vivek Anand dfe82f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ceb262
        data = json.loads(output.data)
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,
Vivek Anand dfe82f
                    "pattern": None,
Vivek Anand dfe82f
                    "short": False,
Vivek Anand dfe82f
                    "tags": ["inf"],
Vivek Anand dfe82f
                    "username": None
Vivek Anand dfe82f
                }
Vivek Anand dfe82f
            }
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"
Clement Verna cd6e30
        data['projects'][0]['date_modified'] = "1436527638"
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl ada420
            "args": {
Matt Prahl ada420
                "fork": None,
Matt Prahl ada420
                "namespace": None,
Matt Prahl 7403a2
                "owner": None,
Matt Prahl ada420
                "pattern": None,
Pierre-Yves Chibon 1ca90b
                "short": False,
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",
Clement Verna cd6e30
                "date_modified": "1436527638",
Matt Prahl ada420
                "description": "test project #1",
Matt Prahl ada420
                "fullname": "test",
Pierre-Yves Chibon 469e24
                "url_path": "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
Matt Prahl 7403a2
        output = self.app.get('/api/0/projects?owner=pingou')
Matt Prahl 7403a2
        self.assertEqual(output.status_code, 200)
Matt Prahl 7403a2
        data = json.loads(output.data)
Matt Prahl 7403a2
        data['projects'][0]['date_created'] = "1436527638"
Clement Verna cd6e30
        data['projects'][0]['date_modified'] = "1436527638"
Matt Prahl 7403a2
        data['projects'][1]['date_created'] = "1436527638"
Clement Verna cd6e30
        data['projects'][1]['date_modified'] = "1436527638"
Matt Prahl 7403a2
        data['projects'][2]['date_created'] = "1436527638"
Clement Verna cd6e30
        data['projects'][2]['date_modified'] = "1436527638"
Matt Prahl 7403a2
        expected_data = {
Matt Prahl 7403a2
            "args": {
Matt Prahl 7403a2
                "fork": None,
Matt Prahl 7403a2
                "namespace": None,
Matt Prahl 7403a2
                "owner": "pingou",
Matt Prahl 7403a2
                "pattern": None,
Pierre-Yves Chibon 1ca90b
                "short": False,
Matt Prahl 7403a2
                "tags": [],
Matt Prahl 7403a2
                "username": None
Matt Prahl 7403a2
            },
Matt Prahl 7403a2
            "projects": [
Matt Prahl 7403a2
                {
Matt Prahl 7403a2
                    "access_groups": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "ticket": []
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "access_users": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "owner": ["pingou"],
Matt Prahl 7403a2
                        "ticket": []
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "close_status": [
Matt Prahl 7403a2
                        "Invalid",
Matt Prahl 7403a2
                        "Insufficient data",
Matt Prahl 7403a2
                        "Fixed",
Matt Prahl 7403a2
                        "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"],
Matt Prahl 7403a2
                    "user": {
Matt Prahl 7403a2
                        "fullname": "PY C",
Matt Prahl 7403a2
                        "name": "pingou"
Matt Prahl 7403a2
                    }
Matt Prahl 7403a2
                },
Matt Prahl 7403a2
                {
Matt Prahl 7403a2
                    "access_groups": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "ticket": []
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "access_users": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "owner": ["pingou"],
Matt Prahl 7403a2
                        "ticket": []
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "close_status": [
Matt Prahl 7403a2
                        "Invalid",
Matt Prahl 7403a2
                        "Insufficient data",
Matt Prahl 7403a2
                        "Fixed",
Matt Prahl 7403a2
                        "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": [],
Matt Prahl 7403a2
                    "user": {
Matt Prahl 7403a2
                        "fullname": "PY C",
Matt Prahl 7403a2
                        "name": "pingou"
Matt Prahl 7403a2
                    }
Matt Prahl 7403a2
                },
Matt Prahl 7403a2
                {
Matt Prahl 7403a2
                    "access_groups": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "ticket": []
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "access_users": {
Matt Prahl 7403a2
                        "admin": [],
Matt Prahl 7403a2
                        "commit": [],
Matt Prahl 7403a2
                        "owner": ["pingou"],
Matt Prahl 7403a2
                        "ticket": []
Matt Prahl 7403a2
                    },
Matt Prahl 7403a2
                    "close_status": [
Matt Prahl 7403a2
                        "Invalid",
Matt Prahl 7403a2
                        "Insufficient data",
Matt Prahl 7403a2
                        "Fixed",
Matt Prahl 7403a2
                        "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": [],
Matt Prahl 7403a2
                    "user": {
Matt Prahl 7403a2
                        "fullname": "PY C",
Matt Prahl 7403a2
                        "name": "pingou"
Matt Prahl 7403a2
                    }
Matt Prahl 7403a2
                }
Matt Prahl 7403a2
            ],
Matt Prahl 7403a2
            "total_projects": 3
Matt Prahl 7403a2
        }
Matt Prahl 7403a2
        self.assertDictEqual(data, expected_data)
Matt Prahl 7403a2
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"
Clement Verna cd6e30
        data['projects'][0]['date_modified'] = "1436527638"
Pierre-Yves Chibon 07a462
        data['projects'][1]['date_created'] = "1436527638"
Clement Verna cd6e30
        data['projects'][1]['date_modified'] = "1436527638"
clime afed57
        data['projects'][2]['date_created'] = "1436527638"
Clement Verna cd6e30
        data['projects'][2]['date_modified'] = "1436527638"
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,
Pierre-Yves Chibon e33592
                "pattern": None,
Pierre-Yves Chibon 1ca90b
                "short": False,
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",
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"],
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",
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": [],
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",
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": [],
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"
Clement Verna cd6e30
        data['projects'][0]['date_modified'] = "1436527638"
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,
Pierre-Yves Chibon e33592
                "pattern": None,
Pierre-Yves Chibon 1ca90b
                "short": False,
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",
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"],
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"
Clement Verna cd6e30
        data['projects'][0]['date_modified'] = "1436527638"
Matt Prahl 6c3d54
        expected_data = {
Matt Prahl 6c3d54
            "args": {
Matt Prahl 6c3d54
                "fork": None,
Matt Prahl 7403a2
                "owner": None,
Matt Prahl ada420
                "namespace": "somenamespace",
Matt Prahl 6c3d54
                "pattern": None,
Pierre-Yves Chibon 1ca90b
                "short": False,
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",
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": [],
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
Pierre-Yves Chibon b130e5
        repo = pagure.lib.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 ef1fe1
            gitfolder=None)
Pierre-Yves Chibon ef1fe1
        self.assertEqual(output, ['Project tagged with: infra'])
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Check after adding
Pierre-Yves Chibon b130e5
        repo = pagure.lib.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"
Clement Verna cd6e30
        data['date_modified'] = "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"],
Pierre-Yves Chibon fd3701
                "ticket": []},
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 fd3701
                "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 fd3701
            "user": {
Pierre-Yves Chibon fd3701
                "fullname": "PY C",
Pierre-Yves Chibon fd3701
                "name": "pingou"
Pierre-Yves Chibon fd3701
            }
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 b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Adding a tag
Pierre-Yves Chibon fd3701
        output = pagure.lib.update_tags(
Pierre-Yves Chibon fd3701
            self.session, repo, 'infra', 'pingou',
Pierre-Yves Chibon ef1fe1
            gitfolder=None)
Pierre-Yves Chibon ef1fe1
        self.assertEqual(output, ['Project tagged with: infra'])
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Check after adding
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fd3701
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon fd3701
        self.assertEqual(repo.tags_text, ['infra'])
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Add a group to the project
Pierre-Yves Chibon fd3701
        msg = pagure.lib.add_group(
Pierre-Yves Chibon fd3701
            self.session,
Pierre-Yves Chibon fd3701
            group_name='some_group',
Pierre-Yves Chibon fd3701
            display_name='Some Group',
Pierre-Yves Chibon fd3701
            description=None,
Pierre-Yves Chibon fd3701
            group_type='bar',
Pierre-Yves Chibon fd3701
            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 b130e5
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fd3701
        group = pagure.lib.search_groups(
Pierre-Yves Chibon fd3701
            self.session, group_name='some_group')
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        pagure.lib.add_group_to_project(
Pierre-Yves Chibon fd3701
            self.session,
Pierre-Yves Chibon fd3701
            project,
Pierre-Yves Chibon fd3701
            new_group='some_group',
Pierre-Yves Chibon fd3701
            user='pingou',
Pierre-Yves Chibon fd3701
            access='commit',
Pierre-Yves Chibon fd3701
            create=False,
Pierre-Yves Chibon fd3701
            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 fd3701
        output = self.app.get('/api/0/test?expand_group=1')
Pierre-Yves Chibon fd3701
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fd3701
        data = json.loads(output.data)
Pierre-Yves Chibon fd3701
        data['date_created'] = "1436527638"
Pierre-Yves Chibon fd3701
        data['date_modified'] = "1436527638"
Pierre-Yves Chibon fd3701
        expected_data ={
Pierre-Yves Chibon fd3701
            "access_groups": {
Pierre-Yves Chibon fd3701
                "admin": [],
Pierre-Yves Chibon fd3701
                "commit": ["some_group"],
Pierre-Yves Chibon fd3701
                "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 fd3701
                "ticket": []},
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 fd3701
                "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
            "group_details": {
Pierre-Yves Chibon fd3701
              "some_group": [
Pierre-Yves Chibon fd3701
                "foo"
Pierre-Yves Chibon fd3701
              ]
Pierre-Yves Chibon fd3701
            },
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 fd3701
            "user": {
Pierre-Yves Chibon fd3701
                "fullname": "PY C",
Pierre-Yves Chibon fd3701
                "name": "pingou"
Pierre-Yves Chibon fd3701
            }
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 b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Adding a tag
Pierre-Yves Chibon fd3701
        output = pagure.lib.update_tags(
Pierre-Yves Chibon fd3701
            self.session, repo, 'infra', 'pingou',
Pierre-Yves Chibon ef1fe1
            gitfolder=None)
Pierre-Yves Chibon ef1fe1
        self.assertEqual(output, ['Project tagged with: infra'])
Pierre-Yves Chibon fd3701
Pierre-Yves Chibon fd3701
        # Check after adding
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fd3701
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon fd3701
        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 fd3701
        output = self.app.get('/api/0/test?expand_group=0')
Pierre-Yves Chibon fd3701
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fd3701
        data = json.loads(output.data)
Pierre-Yves Chibon fd3701
        data['date_created'] = "1436527638"
Pierre-Yves Chibon fd3701
        data['date_modified'] = "1436527638"
Pierre-Yves Chibon fd3701
        expected_data ={
Pierre-Yves Chibon fd3701
            "access_groups": {
Pierre-Yves Chibon fd3701
                "admin": [],
Pierre-Yves Chibon fd3701
                "commit": [],
Pierre-Yves Chibon fd3701
                "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"],
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",
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"],
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
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
Matt Prahl a01c25
        output = self.app.get('/api/0/projects?page=1')
Matt Prahl a01c25
        self.assertEqual(output.status_code, 200)
Matt Prahl a01c25
        data = json.loads(output.data)
Clement Verna cd6e30
        for i in range(3):
Clement Verna cd6e30
            data['projects'][i]['date_created'] = "1436527638"
Clement Verna cd6e30
            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": [],
Matt Prahl a01c25
                "username": None
Matt Prahl a01c25
            },
Matt Prahl a01c25
            "pagination": {
Matt Prahl a01c25
                "first": "http://localhost/api/0/projects?per_page=20&page=1",
Matt Prahl a01c25
                "last": "http://localhost/api/0/projects?per_page=20&page=1",
Matt Prahl a01c25
                "next": None,
Matt Prahl a01c25
                "page": 1,
Matt Prahl a01c25
                "pages": 1,
Matt Prahl a01c25
                "per_page": 20,
Matt Prahl a01c25
                "prev": None
Matt Prahl a01c25
            },
Matt Prahl a01c25
            "projects": [
Matt Prahl a01c25
                {
Matt Prahl a01c25
                    "access_groups": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "ticket": []},
Matt Prahl a01c25
                    "access_users": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "owner": ["pingou"],
Matt Prahl a01c25
                        "ticket": []
Matt Prahl a01c25
                    },
Matt Prahl a01c25
                    "close_status": [
Matt Prahl a01c25
                        "Invalid",
Matt Prahl a01c25
                        "Insufficient data",
Matt Prahl a01c25
                        "Fixed",
Matt Prahl a01c25
                        "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": [],
Matt Prahl a01c25
                    "user": {
Matt Prahl a01c25
                        "fullname": "PY C",
Matt Prahl a01c25
                        "name": "pingou"
Matt Prahl a01c25
                    }
Matt Prahl a01c25
                },
Matt Prahl a01c25
                {
Matt Prahl a01c25
                    "access_groups": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "ticket": []
Matt Prahl a01c25
                    },
Matt Prahl a01c25
                    "access_users": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "owner": ["pingou"],
Matt Prahl a01c25
                        "ticket": []
Matt Prahl a01c25
                    },
Matt Prahl a01c25
                    "close_status": [
Matt Prahl a01c25
                        "Invalid",
Matt Prahl a01c25
                        "Insufficient data",
Matt Prahl a01c25
                        "Fixed",
Matt Prahl a01c25
                        "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": [],
Matt Prahl a01c25
                    "user": {
Matt Prahl a01c25
                        "fullname": "PY C",
Matt Prahl a01c25
                        "name": "pingou"
Matt Prahl a01c25
                    }
Matt Prahl a01c25
                },
Matt Prahl a01c25
                {
Matt Prahl a01c25
                    "access_groups": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "ticket": []},
Matt Prahl a01c25
                    "access_users": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "owner": ["pingou"],
Matt Prahl a01c25
                        "ticket": []},
Matt Prahl a01c25
                    "close_status": [
Matt Prahl a01c25
                        "Invalid",
Matt Prahl a01c25
                        "Insufficient data",
Matt Prahl a01c25
                        "Fixed",
Matt Prahl a01c25
                        "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": [],
Matt Prahl a01c25
                    "user": {
Matt Prahl a01c25
                        "fullname": "PY C",
Matt Prahl a01c25
                        "name": "pingou"
Matt Prahl a01c25
                    }
Matt Prahl a01c25
                }
Matt Prahl a01c25
            ],
Matt Prahl a01c25
            "total_projects": 3
Matt Prahl a01c25
        }
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
Matt Prahl a01c25
        output = self.app.get('/api/0/projects?page=2&per_page=2')
Matt Prahl a01c25
        self.assertEqual(output.status_code, 200)
Matt Prahl a01c25
        data = json.loads(output.data)
Matt Prahl a01c25
        data['projects'][0]['date_created'] = "1436527638"
Clement Verna cd6e30
        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": [],
Matt Prahl a01c25
                "username": None
Matt Prahl a01c25
            },
Matt Prahl a01c25
            "pagination": {
Matt Prahl a01c25
                "first": "http://localhost/api/0/projects?per_page=2&page=1",
Matt Prahl a01c25
                "last": "http://localhost/api/0/projects?per_page=2&page=2",
Matt Prahl a01c25
                "next": None,
Matt Prahl a01c25
                "page": 2,
Matt Prahl a01c25
                "pages": 2,
Matt Prahl a01c25
                "per_page": 2,
Matt Prahl a01c25
                "prev": "http://localhost/api/0/projects?per_page=2&page=1",
Matt Prahl a01c25
            },
Matt Prahl a01c25
            "projects": [
Matt Prahl a01c25
                {
Matt Prahl a01c25
                    "access_groups": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "ticket": []
Matt Prahl a01c25
                    },
Matt Prahl a01c25
                    "access_users": {
Matt Prahl a01c25
                        "admin": [],
Matt Prahl a01c25
                        "commit": [],
Matt Prahl a01c25
                        "owner": ["pingou"],
Matt Prahl a01c25
                        "ticket": []
Matt Prahl a01c25
                    },
Matt Prahl a01c25
                    "close_status": [
Matt Prahl a01c25
                        "Invalid",
Matt Prahl a01c25
                        "Insufficient data",
Matt Prahl a01c25
                        "Fixed",
Matt Prahl a01c25
                        "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": [],
Matt Prahl a01c25
                    "user": {
Matt Prahl a01c25
                        "fullname": "PY C",
Matt Prahl a01c25
                        "name": "pingou"
Matt Prahl a01c25
                    }
Matt Prahl a01c25
                }
Matt Prahl a01c25
            ],
Matt Prahl a01c25
            "total_projects": 3
Matt Prahl a01c25
        }
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
Matt Prahl a01c25
        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
Matt Prahl a01c25
        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
Matt Prahl a01c25
        output = self.app.get('/api/0/projects?page=1&per_page=0')
Matt Prahl a01c25
        self.assertEqual(output.status_code, 400)
Matt Prahl a01c25
        error = json.loads(output.data)
Matt Prahl a01c25
        self.assertEqual(
Matt Prahl a01c25
            error['error'], 'The per_page value must be between 1 and 100')
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
Matt Prahl a01c25
        output = self.app.get('/api/0/projects?page=1&per_page=101')
Matt Prahl a01c25
        self.assertEqual(output.status_code, 400)
Matt Prahl a01c25
        error = json.loads(output.data)
Matt Prahl a01c25
        self.assertEqual(
Matt Prahl a01c25
            error['error'], 'The per_page value must be between 1 and 100')
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
Matt Prahl a01c25
        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
Matt Prahl a01c25
        output = self.app.get('/api/0/projects?page=99999')
Vivek Anand dfe82f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b13c25
        data = json.loads(output.data)
Pierre-Yves Chibon b13c25
        self.assertEqual(
Pierre-Yves Chibon b13c25
            data,
Pierre-Yves Chibon b13c25
            {
Pierre-Yves Chibon b13c25
              "args": {
Pierre-Yves Chibon b13c25
                "fork": None,
Pierre-Yves Chibon b13c25
                "namespace": None,
Pierre-Yves Chibon b13c25
                "owner": None,
Pierre-Yves Chibon b13c25
                "page": 99999,
Pierre-Yves Chibon b13c25
                "pattern": None,
Pierre-Yves Chibon b13c25
                "per_page": 20,
Pierre-Yves Chibon b13c25
                "short": False,
Pierre-Yves Chibon b13c25
                "tags": [],
Pierre-Yves Chibon b13c25
                "username": None
Pierre-Yves Chibon b13c25
              },
Pierre-Yves Chibon b13c25
              "pagination": {
Pierre-Yves Chibon b13c25
                "first": "http://localhost/api/0/projects?per_page=20&page=1",
Pierre-Yves Chibon b13c25
                "last": "http://localhost/api/0/projects?per_page=20&page=1",
Pierre-Yves Chibon b13c25
                "next": None,
Pierre-Yves Chibon b13c25
                "page": 99999,
Pierre-Yves Chibon b13c25
                "pages": 1,
Pierre-Yves Chibon b13c25
                "per_page": 20,
Pierre-Yves Chibon b13c25
                "prev": "http://localhost/api/0/projects?per_page=20&page=99998"
Pierre-Yves Chibon b13c25
              },
Pierre-Yves Chibon b13c25
              "projects": [],
Pierre-Yves Chibon b13c25
              "total_projects": 3
Pierre-Yves Chibon b13c25
            }
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)
Matt Prahl 30f8b1
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
Matt Prahl 30f8b1
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 594b28
        user = pagure.lib.get_user(self.session, 'pingou')
Matt Prahl ab0f46
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Matt Prahl ab0f46
            output = self.app.patch(
Matt Prahl ab0f46
                '/api/0/test', headers=headers,
mprahl 0f9ac5
                data={'main_admin': 'foo'})
mprahl 0f9ac5
            self.assertEqual(output.status_code, 200)
mprahl 0f9ac5
            data = json.loads(output.data)
mprahl 0f9ac5
            data['date_created'] = '1496338274'
mprahl 0f9ac5
            data['date_modified'] = '1496338274'
mprahl 0f9ac5
            expected_output = {
mprahl 0f9ac5
                "access_groups": {
mprahl 0f9ac5
                    "admin": [],
mprahl 0f9ac5
                    "commit": [],
mprahl 0f9ac5
                    "ticket": []
mprahl 0f9ac5
                },
mprahl 0f9ac5
                "access_users": {
mprahl 0f9ac5
                    "admin": [],
mprahl 0f9ac5
                    "commit": [],
mprahl 0f9ac5
                    "owner": [
mprahl 0f9ac5
                      "foo"
mprahl 0f9ac5
                    ],
mprahl 0f9ac5
                    "ticket": []
mprahl 0f9ac5
                },
mprahl 0f9ac5
                "close_status": [
mprahl 0f9ac5
                    "Invalid",
mprahl 0f9ac5
                    "Insufficient data",
mprahl 0f9ac5
                    "Fixed",
mprahl 0f9ac5
                    "Duplicate"
mprahl 0f9ac5
                ],
mprahl 0f9ac5
                "custom_keys": [],
mprahl 0f9ac5
                "date_created": "1496338274",
mprahl 0f9ac5
                "date_modified": "1496338274",
mprahl 0f9ac5
                "description": "test project #1",
mprahl 0f9ac5
                "fullname": "test",
Pierre-Yves Chibon 469e24
                "url_path": "test",
mprahl 0f9ac5
                "id": 1,
mprahl 0f9ac5
                "milestones": {},
mprahl 0f9ac5
                "name": "test",
mprahl 0f9ac5
                "namespace": None,
mprahl 0f9ac5
                "parent": None,
mprahl 0f9ac5
                "priorities": {},
mprahl 0f9ac5
                "tags": [],
mprahl 0f9ac5
                "user": {
mprahl 0f9ac5
                    "default_email": "foo@bar.com",
mprahl 0f9ac5
                    "emails": [
mprahl 0f9ac5
                        "foo@bar.com"
mprahl 0f9ac5
                    ],
mprahl 0f9ac5
                    "fullname": "foo bar",
mprahl 0f9ac5
                    "name": "foo"
mprahl 0f9ac5
                }
mprahl 0f9ac5
            }
mprahl 0f9ac5
            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)
mprahl 8f40ee
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
mprahl 8f40ee
        headers = {'Authorization': 'token aaabbbcccddd'}
mprahl 8f40ee
mprahl 8f40ee
        user = pagure.lib.get_user(self.session, 'pingou')
mprahl 8f40ee
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
mprahl 8f40ee
            output = self.app.patch(
mprahl 10dcea
                '/api/0/test', headers=headers,
mprahl 10dcea
                data={'main_admin': 'foo', 'retain_access': True})
mprahl 10dcea
            self.assertEqual(output.status_code, 200)
mprahl 10dcea
            data = json.loads(output.data)
mprahl 10dcea
            data['date_created'] = '1496338274'
mprahl 10dcea
            data['date_modified'] = '1496338274'
mprahl 10dcea
            expected_output = {
mprahl 10dcea
                "access_groups": {
mprahl 10dcea
                    "admin": [],
mprahl 10dcea
                    "commit": [],
mprahl 10dcea
                    "ticket": []
mprahl 10dcea
                },
mprahl 10dcea
                "access_users": {
mprahl 10dcea
                    "admin": [
mprahl 10dcea
                        "pingou"
mprahl 10dcea
                    ],
mprahl 10dcea
                    "commit": [],
mprahl 10dcea
                    "owner": [
mprahl 10dcea
                        "foo"
mprahl 10dcea
                    ],
mprahl 10dcea
                    "ticket": []
mprahl 10dcea
                },
mprahl 10dcea
                "close_status": [
mprahl 10dcea
                    "Invalid",
mprahl 10dcea
                    "Insufficient data",
mprahl 10dcea
                    "Fixed",
mprahl 10dcea
                    "Duplicate"
mprahl 10dcea
                ],
mprahl 10dcea
                "custom_keys": [],
mprahl 10dcea
                "date_created": "1496338274",
mprahl 10dcea
                "date_modified": "1496338274",
mprahl 10dcea
                "description": "test project #1",
mprahl 10dcea
                "fullname": "test",
Pierre-Yves Chibon 469e24
                "url_path": "test",
mprahl 10dcea
                "id": 1,
mprahl 10dcea
                "milestones": {},
mprahl 10dcea
                "name": "test",
mprahl 10dcea
                "namespace": None,
mprahl 10dcea
                "parent": None,
mprahl 10dcea
                "priorities": {},
mprahl 10dcea
                "tags": [],
mprahl 10dcea
                "user": {
mprahl 10dcea
                    "default_email": "foo@bar.com",
mprahl 10dcea
                    "emails": [
mprahl 10dcea
                        "foo@bar.com"
mprahl 10dcea
                    ],
mprahl 10dcea
                    "fullname": "foo bar",
mprahl 10dcea
                    "name": "foo"
mprahl 10dcea
                }
mprahl 10dcea
            }
mprahl 10dcea
            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)
mprahl 10dcea
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
mprahl 10dcea
        headers = {'Authorization': 'token aaabbbcccddd'}
mprahl 10dcea
mprahl 10dcea
        project = pagure.lib._get_project(self.session, 'test')
mprahl 10dcea
        pagure.lib.add_user_to_project(
mprahl 10dcea
            self.session, project,
mprahl 10dcea
            new_user='foo',
mprahl 10dcea
            user='pingou',
mprahl 10dcea
            access='commit'
mprahl 10dcea
        )
mprahl 10dcea
        self.session.commit()
mprahl 10dcea
mprahl 10dcea
        user = pagure.lib.get_user(self.session, 'pingou')
mprahl 10dcea
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
mprahl 10dcea
            output = self.app.patch(
mprahl 8f40ee
                '/api/0/test', headers=headers,
mprahl 8f40ee
                data={'main_admin': 'foo', 'retain_access': True})
mprahl 8f40ee
            self.assertEqual(output.status_code, 200)
mprahl 8f40ee
            data = json.loads(output.data)
mprahl 8f40ee
            data['date_created'] = '1496338274'
mprahl 8f40ee
            data['date_modified'] = '1496338274'
mprahl 8f40ee
            expected_output = {
mprahl 8f40ee
                "access_groups": {
mprahl 8f40ee
                    "admin": [],
mprahl 8f40ee
                    "commit": [],
mprahl 8f40ee
                    "ticket": []
mprahl 8f40ee
                },
mprahl 8f40ee
                "access_users": {
mprahl 8f40ee
                    "admin": [
mprahl 8f40ee
                        "pingou"
mprahl 8f40ee
                    ],
mprahl 8f40ee
                    "commit": [],
mprahl 8f40ee
                    "owner": [
mprahl 8f40ee
                        "foo"
mprahl 8f40ee
                    ],
mprahl 8f40ee
                    "ticket": []
mprahl 8f40ee
                },
mprahl 8f40ee
                "close_status": [
mprahl 8f40ee
                    "Invalid",
mprahl 8f40ee
                    "Insufficient data",
mprahl 8f40ee
                    "Fixed",
mprahl 8f40ee
                    "Duplicate"
mprahl 8f40ee
                ],
mprahl 8f40ee
                "custom_keys": [],
mprahl 8f40ee
                "date_created": "1496338274",
mprahl 8f40ee
                "date_modified": "1496338274",
mprahl 8f40ee
                "description": "test project #1",
mprahl 8f40ee
                "fullname": "test",
Pierre-Yves Chibon 469e24
                "url_path": "test",
mprahl 8f40ee
                "id": 1,
mprahl 8f40ee
                "milestones": {},
mprahl 8f40ee
                "name": "test",
mprahl 8f40ee
                "namespace": None,
mprahl 8f40ee
                "parent": None,
mprahl 8f40ee
                "priorities": {},
mprahl 8f40ee
                "tags": [],
mprahl 8f40ee
                "user": {
mprahl 8f40ee
                    "default_email": "foo@bar.com",
mprahl 8f40ee
                    "emails": [
mprahl 8f40ee
                        "foo@bar.com"
mprahl 8f40ee
                    ],
mprahl 8f40ee
                    "fullname": "foo bar",
mprahl 8f40ee
                    "name": "foo"
mprahl 8f40ee
                }
mprahl 8f40ee
            }
mprahl 8f40ee
            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)
mprahl 0f9ac5
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
mprahl 0f9ac5
        headers = {'Authorization': 'token aaabbbcccddd',
mprahl 0f9ac5
                   'Content-Type': 'application/json'}
mprahl 0f9ac5
mprahl 0f9ac5
        user = pagure.lib.get_user(self.session, 'pingou')
mprahl 0f9ac5
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
mprahl 0f9ac5
            output = self.app.patch(
mprahl 0f9ac5
                '/api/0/test', headers=headers,
Matt Prahl ab0f46
                data=json.dumps({'main_admin': 'foo'}))
Matt Prahl ab0f46
            self.assertEqual(output.status_code, 200)
Matt Prahl ab0f46
            data = json.loads(output.data)
Matt Prahl ab0f46
            data['date_created'] = '1496338274'
Matt Prahl ab0f46
            data['date_modified'] = '1496338274'
Matt Prahl ab0f46
            expected_output = {
Matt Prahl ab0f46
                "access_groups": {
Matt Prahl ab0f46
                    "admin": [],
Matt Prahl ab0f46
                    "commit": [],
Matt Prahl ab0f46
                    "ticket": []
Matt Prahl ab0f46
                },
Matt Prahl ab0f46
                "access_users": {
Matt Prahl ab0f46
                    "admin": [],
Matt Prahl ab0f46
                    "commit": [],
Matt Prahl ab0f46
                    "owner": [
Matt Prahl ab0f46
                      "foo"
Matt Prahl ab0f46
                    ],
Matt Prahl ab0f46
                    "ticket": []
Matt Prahl ab0f46
                },
Matt Prahl ab0f46
                "close_status": [
Matt Prahl ab0f46
                    "Invalid",
Matt Prahl ab0f46
                    "Insufficient data",
Matt Prahl ab0f46
                    "Fixed",
Matt Prahl ab0f46
                    "Duplicate"
Matt Prahl ab0f46
                ],
Matt Prahl ab0f46
                "custom_keys": [],
Matt Prahl ab0f46
                "date_created": "1496338274",
Matt Prahl ab0f46
                "date_modified": "1496338274",
Matt Prahl ab0f46
                "description": "test project #1",
Matt Prahl ab0f46
                "fullname": "test",
Pierre-Yves Chibon 469e24
                "url_path": "test",
Matt Prahl ab0f46
                "id": 1,
Matt Prahl ab0f46
                "milestones": {},
Matt Prahl ab0f46
                "name": "test",
Matt Prahl ab0f46
                "namespace": None,
Matt Prahl ab0f46
                "parent": None,
Matt Prahl ab0f46
                "priorities": {},
Matt Prahl ab0f46
                "tags": [],
Matt Prahl ab0f46
                "user": {
Matt Prahl ab0f46
                    "default_email": "foo@bar.com",
Matt Prahl ab0f46
                    "emails": [
Matt Prahl ab0f46
                        "foo@bar.com"
Matt Prahl ab0f46
                    ],
Matt Prahl ab0f46
                    "fullname": "foo bar",
Matt Prahl ab0f46
                    "name": "foo"
Matt Prahl ab0f46
                }
Matt Prahl ab0f46
            }
Matt Prahl ab0f46
            self.assertEqual(data, expected_output)
Matt Prahl ab0f46
Pierre-Yves Chibon b130e5
    @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)
Matt Prahl ab0f46
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
Matt Prahl ab0f46
        headers = {'Authorization': 'token aaabbbcccddd'}
Matt Prahl ab0f46
Matt Prahl ab0f46
        user = pagure.lib.get_user(self.session, 'foo')
Matt Prahl ab0f46
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 594b28
            output = self.app.patch(
Pierre-Yves Chibon 594b28
                '/api/0/test', headers=headers,
mprahl 0f9ac5
                data={'main_admin': 'foo'})
Matt Prahl 30f8b1
            self.assertEqual(output.status_code, 200)
Matt Prahl 30f8b1
            data = json.loads(output.data)
Matt Prahl 30f8b1
            data['date_created'] = '1496338274'
Clement Verna cd6e30
            data['date_modified'] = '1496338274'
Matt Prahl 30f8b1
            expected_output = {
Matt Prahl 30f8b1
                "access_groups": {
Matt Prahl 30f8b1
                    "admin": [],
Matt Prahl 30f8b1
                    "commit": [],
Matt Prahl 30f8b1
                    "ticket": []
Matt Prahl 30f8b1
                },
Matt Prahl 30f8b1
                "access_users": {
Matt Prahl 30f8b1
                    "admin": [],
Matt Prahl 30f8b1
                    "commit": [],
Matt Prahl 30f8b1
                    "owner": [
Matt Prahl 30f8b1
                      "foo"
Matt Prahl 30f8b1
                    ],
Matt Prahl 30f8b1
                    "ticket": []
Matt Prahl 30f8b1
                },
Matt Prahl 30f8b1
                "close_status": [
Matt Prahl 30f8b1
                    "Invalid",
Matt Prahl 30f8b1
                    "Insufficient data",
Matt Prahl 30f8b1
                    "Fixed",
Matt Prahl 30f8b1
                    "Duplicate"
Matt Prahl 30f8b1
                ],
Matt Prahl 30f8b1
                "custom_keys": [],
Matt Prahl 30f8b1
                "date_created": "1496338274",
Clement Verna cd6e30
                "date_modified": "1496338274",
Matt Prahl 30f8b1
                "description": "test project #1",
Matt Prahl 30f8b1
                "fullname": "test",
Pierre-Yves Chibon 469e24
                "url_path": "test",
Matt Prahl 30f8b1
                "id": 1,
Matt Prahl 30f8b1
                "milestones": {},
Matt Prahl 30f8b1
                "name": "test",
Matt Prahl 30f8b1
                "namespace": None,
Matt Prahl 30f8b1
                "parent": None,
Matt Prahl 30f8b1
                "priorities": {},
Matt Prahl 30f8b1
                "tags": [],
Matt Prahl 30f8b1
                "user": {
Matt Prahl 30f8b1
                    "default_email": "foo@bar.com",
Matt Prahl 30f8b1
                    "emails": [
Matt Prahl 30f8b1
                        "foo@bar.com"
Matt Prahl 30f8b1
                    ],
Matt Prahl 30f8b1
                    "fullname": "foo bar",
Matt Prahl 30f8b1
                    "name": "foo"
Matt Prahl 30f8b1
                }
Matt Prahl 30f8b1
            }
Matt Prahl 30f8b1
            self.assertEqual(data, expected_output)
Matt Prahl 30f8b1
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)
Matt Prahl 30f8b1
        project_user = pagure.lib.model.ProjectUser(
Matt Prahl 30f8b1
            project_id=1,
Matt Prahl 30f8b1
            user_id=2,
Matt Prahl 30f8b1
            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)
Matt Prahl 30f8b1
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
Matt Prahl 30f8b1
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 594b28
        user = pagure.lib.get_user(self.session, 'foo')
Matt Prahl ab0f46
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 594b28
            output = self.app.patch(
Pierre-Yves Chibon 594b28
                '/api/0/test', headers=headers,
mprahl 0f9ac5
                data={'main_admin': 'foo'})
Matt Prahl 30f8b1
            self.assertEqual(output.status_code, 401)
Matt Prahl 30f8b1
            expected_error = {
Matt Prahl 30f8b1
                'error': ('Only the main admin can set the main admin of a '
Matt Prahl 30f8b1
                          'project'),
Matt Prahl 30f8b1
                'error_code': 'ENOTMAINADMIN'
Matt Prahl 30f8b1
            }
Matt Prahl 30f8b1
            self.assertEqual(json.loads(output.data), expected_error)
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)
Matt Prahl 30f8b1
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
Matt Prahl 30f8b1
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 594b28
        user = pagure.lib.get_user(self.session, 'foo')
Matt Prahl ab0f46
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 594b28
            output = self.app.patch(
Pierre-Yves Chibon 594b28
                '/api/0/test', headers=headers,
mprahl 0f9ac5
                data={'main_admin': 'foo'})
Matt Prahl 30f8b1
            self.assertEqual(output.status_code, 401)
Matt Prahl 30f8b1
            expected_error = {
Matt Prahl 30f8b1
                'error': 'You are not allowed to modify this project',
Matt Prahl 30f8b1
                'error_code': 'EMODIFYPROJECTNOTALLOWED'
Matt Prahl 30f8b1
            }
Matt Prahl 30f8b1
            self.assertEqual(json.loads(output.data), expected_error)
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)
Matt Prahl 30f8b1
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
Matt Prahl 30f8b1
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 594b28
        user = pagure.lib.get_user(self.session, 'pingou')
Matt Prahl ab0f46
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 594b28
            output = self.app.patch(
Pierre-Yves Chibon 594b28
                '/api/0/test', headers=headers,
Pierre-Yves Chibon 594b28
                data='invalid')
Matt Prahl 30f8b1
            self.assertEqual(output.status_code, 400)
Matt Prahl 30f8b1
            expected_error = {
René Genz dadd94
                'error': 'Invalid or incomplete input submitted',
Matt Prahl 30f8b1
                'error_code': 'EINVALIDREQ'
Matt Prahl 30f8b1
            }
Matt Prahl 30f8b1
            self.assertEqual(json.loads(output.data), expected_error)
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)
Matt Prahl 30f8b1
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
Matt Prahl 30f8b1
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 594b28
        user = pagure.lib.get_user(self.session, 'pingou')
Matt Prahl ab0f46
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 594b28
            output = self.app.patch(
Pierre-Yves Chibon 594b28
                '/api/0/test', headers=headers,
mprahl 0f9ac5
                data={'invalid': 'invalid'})
Matt Prahl 30f8b1
            self.assertEqual(output.status_code, 400)
Matt Prahl 30f8b1
            expected_error = {
René Genz dadd94
                'error': 'Invalid or incomplete input submitted',
Matt Prahl 30f8b1
                'error_code': 'EINVALIDREQ'
Matt Prahl 30f8b1
            }
Matt Prahl 30f8b1
            self.assertEqual(json.loads(output.data), expected_error)
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)
Matt Prahl 30f8b1
        tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project')
Matt Prahl 30f8b1
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 594b28
        user = pagure.lib.get_user(self.session, 'pingou')
Matt Prahl ab0f46
        user.cla_done = True
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 594b28
            output = self.app.patch(
Pierre-Yves Chibon 594b28
                '/api/0/test', headers=headers,
mprahl 0f9ac5
                data={'main_admin': 'tbrady'})
Matt Prahl 30f8b1
            self.assertEqual(output.status_code, 400)
Matt Prahl 30f8b1
            expected_error = {
Matt Prahl 30f8b1
                'error': 'No such user found',
Matt Prahl 30f8b1
                'error_code': 'ENOUSER'
Matt Prahl 30f8b1
            }
Matt Prahl 30f8b1
            self.assertEqual(json.loads(output.data), expected_error)
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
Matt Prahl 733eaa
        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,
Matt Prahl 733eaa
            "watchers": {
Matt Prahl 733eaa
                "pingou": [
Matt Prahl 733eaa
                    "issues"
Matt Prahl 733eaa
                ]
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
        }
Matt Prahl 733eaa
        self.assertDictEqual(json.loads(output.data), expected_data)
Matt Prahl 733eaa
Pierre-Yves Chibon 594b28
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Matt Prahl 733eaa
            # Non-existing project
Matt Prahl 733eaa
            output = self.app.get('/api/0/random/watchers')
Matt Prahl 733eaa
            self.assertEqual(output.status_code, 404)
Matt Prahl 733eaa
            data = json.loads(output.data)
Matt Prahl 733eaa
            self.assertDictEqual(
Matt Prahl 733eaa
                data,
Matt Prahl 733eaa
                {'error_code': 'ENOPROJECT', 'error': 'Project not found'}
Matt Prahl 733eaa
            )
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching issues implicitly
Matt Prahl 733eaa
            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,
Matt Prahl 733eaa
                "watchers": {
Matt Prahl 733eaa
                    "pingou": [
Matt Prahl 733eaa
                        "issues"
Matt Prahl 733eaa
                    ]
Matt Prahl 733eaa
                }
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
            self.assertDictEqual(json.loads(output.data), expected_data)
Matt Prahl 733eaa
Pierre-Yves Chibon b130e5
            project = pagure.lib.get_authorized_project(self.session, 'test')
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching issues and commits explicitly
Matt Prahl 733eaa
            pagure.lib.update_watch_status(
Pierre-Yves Chibon 594b28
                self.session, project, 'pingou', '3')
Pierre-Yves Chibon b130e5
            self.session.commit()
Matt Prahl 733eaa
            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,
Matt Prahl 733eaa
                "watchers": {
Matt Prahl 733eaa
                    "pingou": [
Matt Prahl 733eaa
                        "issues",
Matt Prahl 733eaa
                        "commits"
Matt Prahl 733eaa
                    ]
Matt Prahl 733eaa
                }
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
            self.assertDictEqual(json.loads(output.data), expected_data)
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching issues explicitly
Matt Prahl 733eaa
            pagure.lib.update_watch_status(
Pierre-Yves Chibon 594b28
                self.session, project, 'pingou', '1')
Pierre-Yves Chibon b130e5
            self.session.commit()
Matt Prahl 733eaa
            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,
Matt Prahl 733eaa
                "watchers": {
Matt Prahl 733eaa
                    "pingou": [
Matt Prahl 733eaa
                        "issues"
Matt Prahl 733eaa
                    ]
Matt Prahl 733eaa
                }
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
            self.assertDictEqual(json.loads(output.data), expected_data)
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner is watching commits explicitly
Matt Prahl 733eaa
            pagure.lib.update_watch_status(
Pierre-Yves Chibon 594b28
                self.session, project, 'pingou', '2')
Pierre-Yves Chibon b130e5
            self.session.commit()
Matt Prahl 733eaa
            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,
Matt Prahl 733eaa
                "watchers": {
Matt Prahl 733eaa
                    "pingou": [
Matt Prahl 733eaa
                        "commits"
Matt Prahl 733eaa
                    ]
Matt Prahl 733eaa
                }
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
            self.assertDictEqual(json.loads(output.data), expected_data)
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(
Matt Prahl 733eaa
                project_id=project.id,
Matt Prahl 733eaa
                user_id=2,
Matt Prahl 733eaa
                access='commit',
Matt Prahl 733eaa
            )
Matt Prahl 733eaa
            pagure.lib.update_watch_status(
Pierre-Yves Chibon 594b28
                self.session, project, 'pingou', '2')
Pierre-Yves Chibon 594b28
            self.session.add(project_user)
Pierre-Yves Chibon 594b28
            self.session.commit()
Matt Prahl 733eaa
Matt Prahl 733eaa
            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,
Matt Prahl 733eaa
                "watchers": {
Matt Prahl 733eaa
                    "foo": ["issues"],
Matt Prahl 733eaa
                    "pingou": ["commits"]
Matt Prahl 733eaa
                }
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
            self.assertDictEqual(json.loads(output.data), expected_data)
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner and foo are watching issues implicitly
Matt Prahl 733eaa
            pagure.lib.update_watch_status(
Pierre-Yves Chibon 594b28
                self.session, project, 'pingou', '-1')
Pierre-Yves Chibon b130e5
            self.session.commit()
Matt Prahl 733eaa
Matt Prahl 733eaa
            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,
Matt Prahl 733eaa
                "watchers": {
Matt Prahl 733eaa
                    "foo": ["issues"],
Matt Prahl 733eaa
                    "pingou": ["issues"]
Matt Prahl 733eaa
                }
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
            self.assertDictEqual(json.loads(output.data), expected_data)
Matt Prahl 733eaa
Matt Prahl 733eaa
            # The owner and foo through group membership are watching issues
Matt Prahl 733eaa
            # implicitly
Matt Prahl 733eaa
            pagure.lib.update_watch_status(
Pierre-Yves Chibon 594b28
                self.session, project, 'pingou', '-1')
Pierre-Yves Chibon 594b28
            project_membership = self.session.query(
Matt Prahl 733eaa
                pagure.lib.model.ProjectUser).filter_by(
Matt Prahl 733eaa
                    user_id=2, project_id=project.id).one()
Pierre-Yves Chibon 594b28
            self.session.delete(project_membership)
Pierre-Yves Chibon 594b28
            self.session.commit()
Pierre-Yves Chibon 594b28
Matt Prahl 733eaa
            msg = pagure.lib.add_group(
Matt Prahl 733eaa
                self.session,
Matt Prahl 733eaa
                group_name='some_group',
Matt Prahl 733eaa
                display_name='Some Group',
Matt Prahl 733eaa
                description=None,
Matt Prahl 733eaa
                group_type='bar',
Matt Prahl 733eaa
                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 b130e5
            project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 594b28
            group = pagure.lib.search_groups(
Pierre-Yves Chibon 594b28
                self.session, group_name='some_group')
Matt Prahl 733eaa
            pagure.lib.add_user_to_group(
Pierre-Yves Chibon 594b28
                self.session, 'foo', group, 'pingou', False)
Pierre-Yves Chibon 594b28
Pierre-Yves Chibon 594b28
            pagure.lib.add_group_to_project(
Pierre-Yves Chibon 594b28
                self.session,
Pierre-Yves Chibon 594b28
                project,
Pierre-Yves Chibon 594b28
                new_group='some_group',
Pierre-Yves Chibon 594b28
                user='pingou',
Matt Prahl 733eaa
                access='commit',
Pierre-Yves Chibon 594b28
                create=False,
Pierre-Yves Chibon 594b28
                is_admin=True
Matt Prahl 733eaa
            )
Pierre-Yves Chibon 594b28
            self.session.commit()
Matt Prahl 733eaa
Matt Prahl 733eaa
            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,
Matt Prahl 733eaa
                "watchers": {
Ralph Bean e69883
                    "@some_group": ["issues"],
Matt Prahl 733eaa
                    "pingou": ["issues"]
Matt Prahl 733eaa
                }
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
            self.assertDictEqual(json.loads(output.data), expected_data)
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
Matt Prahl 733eaa
            pagure.lib.update_watch_status(
Pierre-Yves Chibon 594b28
                self.session, project, 'pingou', '-1')
Matt Prahl 733eaa
            pagure.lib.update_watch_status(
Pierre-Yves Chibon 594b28
                self.session, project, 'foo', '2')
Pierre-Yves Chibon fd2f36
            self.session.commit()
Matt Prahl 733eaa
Matt Prahl 733eaa
            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"],
Matt Prahl 733eaa
                    "pingou": ["issues"]
Matt Prahl 733eaa
                }
Matt Prahl 733eaa
            }
Matt Prahl 733eaa
            self.assertDictEqual(json.loads(output.data), expected_data)
Matt Prahl 733eaa
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)
Pierre-Yves Chibon 20753d
        self.assertEqual(sorted(data.keys()), ['error', 'error_code'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
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
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
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
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
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 b130e5
                "error": "It is not possible to create the repo \"test\"",
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 b130e5
    @patch.dict('pagure.config.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 20753d
        self.assertEqual(sorted(data.keys()), ['error', 'error_code'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
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
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
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
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
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 b130e5
                "error": "It is not possible to create the repo \"test\"",
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 b130e5
        pagure.config.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
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
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 b130e5
        pagure.config.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 b130e5
        pagure.config.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 b130e5
        pagure.config.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)
Pierre-Yves Chibon 20753d
        self.assertEqual(sorted(data.keys()), ['error', 'error_code'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
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
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
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
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
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 20753d
        self.assertEqual(sorted(data.keys()), ['error', 'error_code'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
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
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
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
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
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
Matt Prahl 489ad1
    @patch('pagure.lib.tasks.generate_gitolite_acls.delay')
Matt Prahl 489ad1
    def test_api_generate_acls(self, mock_gen_acls):
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(
Matt Prahl 489ad1
            self.session, 'aaabbbcccddd', 'generate_acls_project')
Matt Prahl 489ad1
        headers = {'Authorization': 'token aaabbbcccddd'}
Matt Prahl 489ad1
Matt Prahl 489ad1
        mock_gen_acls_rv = Mock()
Matt Prahl 489ad1
        mock_gen_acls_rv.id = 'abc-1234'
Matt Prahl 489ad1
        mock_gen_acls.return_value = mock_gen_acls_rv
Matt Prahl 489ad1
Matt Prahl 489ad1
        user = pagure.lib.get_user(self.session, 'pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Matt Prahl 489ad1
            output = self.app.post(
Matt Prahl 489ad1
                '/api/0/test/git/generateacls', headers=headers,
mprahl 0f9ac5
                data={'wait': False})
mprahl 0f9ac5
            self.assertEqual(output.status_code, 200)
mprahl 0f9ac5
            data = json.loads(output.data)
mprahl 0f9ac5
            expected_output = {
mprahl 0f9ac5
                'message': 'Project ACL generation queued',
mprahl 0f9ac5
                'taskid': 'abc-1234'
mprahl 0f9ac5
            }
mprahl 0f9ac5
            self.assertEqual(data, expected_output)
mprahl 0f9ac5
            mock_gen_acls.assert_called_once_with(
mprahl 0f9ac5
                name='test', namespace=None, user=None, group=None)
mprahl 0f9ac5
mprahl 0f9ac5
    @patch('pagure.lib.tasks.generate_gitolite_acls.delay')
mprahl 0f9ac5
    def test_api_generate_acls_json(self, mock_gen_acls):
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(
mprahl 0f9ac5
            self.session, 'aaabbbcccddd', 'generate_acls_project')
mprahl 0f9ac5
        headers = {'Authorization': 'token aaabbbcccddd',
mprahl 0f9ac5
                   'Content-Type': 'application/json'}
mprahl 0f9ac5
mprahl 0f9ac5
        mock_gen_acls_rv = Mock()
mprahl 0f9ac5
        mock_gen_acls_rv.id = 'abc-1234'
mprahl 0f9ac5
        mock_gen_acls.return_value = mock_gen_acls_rv
mprahl 0f9ac5
mprahl 0f9ac5
        user = pagure.lib.get_user(self.session, 'pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
mprahl 0f9ac5
            output = self.app.post(
mprahl 0f9ac5
                '/api/0/test/git/generateacls', headers=headers,
Matt Prahl 489ad1
                data=json.dumps({'wait': False}))
Matt Prahl 489ad1
            self.assertEqual(output.status_code, 200)
Matt Prahl 489ad1
            data = json.loads(output.data)
Matt Prahl 489ad1
            expected_output = {
Matt Prahl 489ad1
                'message': 'Project ACL generation queued',
Matt Prahl 489ad1
                'taskid': 'abc-1234'
Matt Prahl 489ad1
            }
Matt Prahl 489ad1
            self.assertEqual(data, expected_output)
Matt Prahl 489ad1
            mock_gen_acls.assert_called_once_with(
Pierre-Yves Chibon d4f94d
                name='test', namespace=None, user=None, group=None)
Matt Prahl 489ad1
Matt Prahl 489ad1
    @patch('pagure.lib.tasks.get_result')
Matt Prahl 489ad1
    @patch('pagure.lib.tasks.generate_gitolite_acls.delay')
Matt Prahl 489ad1
    def test_api_generate_acls_wait_true(self, mock_gen_acls, mock_get_result):
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(
Matt Prahl 489ad1
            self.session, 'aaabbbcccddd', 'generate_acls_project')
Matt Prahl 489ad1
        headers = {'Authorization': 'token aaabbbcccddd'}
Matt Prahl 489ad1
Matt Prahl 489ad1
        mock_gen_acls_rv = Mock()
Matt Prahl 489ad1
        mock_gen_acls_rv.id = 'abc-1234'
Matt Prahl 489ad1
        mock_gen_acls.return_value = mock_gen_acls_rv
Matt Prahl 489ad1
Matt Prahl 489ad1
        mock_get_result_rv = Mock()
Matt Prahl 489ad1
        mock_get_result.return_value = mock_get_result_rv
Matt Prahl 489ad1
Matt Prahl 489ad1
        user = pagure.lib.get_user(self.session, 'pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Matt Prahl 489ad1
            output = self.app.post(
Matt Prahl 489ad1
                '/api/0/test/git/generateacls', headers=headers,
mprahl 0f9ac5
                data={'wait': True})
Matt Prahl 489ad1
            self.assertEqual(output.status_code, 200)
Matt Prahl 489ad1
            data = json.loads(output.data)
Matt Prahl 489ad1
            expected_output = {
Matt Prahl 489ad1
                'message': 'Project ACLs generated',
Matt Prahl 489ad1
            }
Matt Prahl 489ad1
            self.assertEqual(data, expected_output)
Matt Prahl 489ad1
            mock_gen_acls.assert_called_once_with(
Pierre-Yves Chibon d4f94d
                name='test', namespace=None, user=None, group=None)
Matt Prahl 489ad1
            mock_get_result.assert_called_once_with('abc-1234')
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(
Matt Prahl 489ad1
            self.session, 'aaabbbcccddd', 'generate_acls_project')
Matt Prahl 489ad1
        headers = {'Authorization': 'token aaabbbcccddd'}
Matt Prahl 489ad1
Matt Prahl 489ad1
        user = pagure.lib.get_user(self.session, 'pingou')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Matt Prahl 489ad1
            output = self.app.post(
Matt Prahl 489ad1
                '/api/0/test12345123/git/generateacls', headers=headers,
mprahl 0f9ac5
                data={'wait': False})
Matt Prahl 489ad1
            self.assertEqual(output.status_code, 404)
Matt Prahl 489ad1
            data = json.loads(output.data)
Matt Prahl 489ad1
            expected_output = {
Matt Prahl 489ad1
                'error_code': 'ENOPROJECT',
Matt Prahl 489ad1
                'error': 'Project not found'
Matt Prahl 489ad1
            }
Matt Prahl 489ad1
            self.assertEqual(data, expected_output)
Matt Prahl 489ad1
mprahl 7d811e
    def test_api_new_git_branch(self):
mprahl 7d811e
        """ Test the api_new_branch method of the flask api """
mprahl 7d811e
        tests.create_projects(self.session)
mprahl 7d811e
        repo_path = os.path.join(self.path, 'repos')
mprahl 7d811e
        tests.create_projects_git(repo_path, bare=True)
mprahl 7d811e
        tests.add_content_git_repo(os.path.join(repo_path, 'test.git'))
mprahl 7d811e
        tests.create_tokens(self.session, project_id=None)
mprahl 7d811e
        tests.create_tokens_acl(
mprahl 7d811e
            self.session, 'aaabbbcccddd', 'modify_project')
mprahl 7d811e
        headers = {'Authorization': 'token aaabbbcccddd'}
mprahl 7d811e
        args = {'branch': 'test123'}
mprahl 7d811e
        output = self.app.post('/api/0/test/git/branch', headers=headers,
mprahl 7d811e
                               data=args)
mprahl 7d811e
        self.assertEqual(output.status_code, 200)
mprahl 7d811e
        data = json.loads(output.data)
mprahl 7d811e
        expected_output = {
mprahl 7d811e
            'message': 'Project branch was created',
mprahl 7d811e
        }
mprahl 7d811e
        self.assertEqual(data, expected_output)
mprahl 7d811e
        git_path = os.path.join(self.path, 'repos', 'test.git')
mprahl 7d811e
        repo_obj = pygit2.Repository(git_path)
mprahl 7d811e
        self.assertIn('test123', repo_obj.listall_branches())
mprahl 7d811e
mprahl 7d811e
mprahl 7d811e
    def test_api_new_git_branch_json(self):
mprahl 7d811e
        """ Test the api_new_branch method of the flask api """
mprahl 7d811e
        tests.create_projects(self.session)
mprahl 7d811e
        repo_path = os.path.join(self.path, 'repos')
mprahl 7d811e
        tests.create_projects_git(repo_path, bare=True)
mprahl 7d811e
        tests.add_content_git_repo(os.path.join(repo_path, 'test.git'))
mprahl 7d811e
        tests.create_tokens(self.session, project_id=None)
mprahl 7d811e
        tests.create_tokens_acl(
mprahl 7d811e
            self.session, 'aaabbbcccddd', 'modify_project')
mprahl 7d811e
        headers = {'Authorization': 'token aaabbbcccddd',
mprahl 7d811e
                   'Content-Type': 'application/json'}
mprahl 7d811e
        args = {'branch': 'test123'}
mprahl 7d811e
        output = self.app.post('/api/0/test/git/branch', headers=headers,
mprahl 7d811e
                               data=json.dumps(args))
mprahl 7d811e
        self.assertEqual(output.status_code, 200)
mprahl 7d811e
        data = json.loads(output.data)
mprahl 7d811e
        expected_output = {
mprahl 7d811e
            'message': 'Project branch was created',
mprahl 7d811e
        }
mprahl 7d811e
        self.assertEqual(data, expected_output)
mprahl 7d811e
        git_path = os.path.join(self.path, 'repos', 'test.git')
mprahl 7d811e
        repo_obj = pygit2.Repository(git_path)
mprahl 7d811e
        self.assertIn('test123', repo_obj.listall_branches())
mprahl 7d811e
mprahl 7d811e
    def test_api_new_git_branch_from_branch(self):
mprahl 7d811e
        """ Test the api_new_branch method of the flask api """
mprahl 7d811e
        tests.create_projects(self.session)
mprahl 7d811e
        repo_path = os.path.join(self.path, 'repos')
mprahl 7d811e
        tests.create_projects_git(repo_path, bare=True)
mprahl 7d811e
        tests.add_content_git_repo(os.path.join(repo_path, 'test.git'))
mprahl 7d811e
        tests.create_tokens(self.session, project_id=None)
mprahl 7d811e
        tests.create_tokens_acl(
mprahl 7d811e
            self.session, 'aaabbbcccddd', 'modify_project')
mprahl 7d811e
        git_path = os.path.join(self.path, 'repos', 'test.git')
mprahl 7d811e
        repo_obj = pygit2.Repository(git_path)
mprahl 7d811e
        parent = pagure.lib.git.get_branch_ref(repo_obj, 'master').get_object()
mprahl 7d811e
        repo_obj.create_branch('dev123', parent)
mprahl 7d811e
        headers = {'Authorization': 'token aaabbbcccddd'}
mprahl 7d811e
        args = {'branch': 'test123', 'from_branch': 'dev123'}
mprahl 7d811e
        output = self.app.post('/api/0/test/git/branch', headers=headers,
mprahl 7d811e
                               data=args)
mprahl 7d811e
        self.assertEqual(output.status_code, 200)
mprahl 7d811e
        data = json.loads(output.data)
mprahl 7d811e
        expected_output = {
mprahl 7d811e
            'message': 'Project branch was created',
mprahl 7d811e
        }
mprahl 7d811e
        self.assertEqual(data, expected_output)
mprahl 7d811e
        self.assertIn('test123', repo_obj.listall_branches())
mprahl 7d811e
mprahl 7d811e
    def test_api_new_git_branch_already_exists(self):
mprahl 7d811e
        """ Test the api_new_branch method of the flask api when branch already
mprahl 7d811e
        exists """
mprahl 7d811e
        tests.create_projects(self.session)
mprahl 7d811e
        repo_path = os.path.join(self.path, 'repos')
mprahl 7d811e
        tests.create_projects_git(repo_path, bare=True)
mprahl 7d811e
        tests.add_content_git_repo(os.path.join(repo_path, 'test.git'))
mprahl 7d811e
        tests.create_tokens(self.session, project_id=None)
mprahl 7d811e
        tests.create_tokens_acl(
mprahl 7d811e
            self.session, 'aaabbbcccddd', 'modify_project')
mprahl 7d811e
        headers = {'Authorization': 'token aaabbbcccddd'}
mprahl 7d811e
        args = {'branch': 'master'}
mprahl 7d811e
        output = self.app.post('/api/0/test/git/branch', headers=headers,
mprahl 7d811e
                               data=args)
mprahl 7d811e
        self.assertEqual(output.status_code, 400)
mprahl 7d811e
        data = json.loads(output.data)
mprahl 7d811e
        expected_output = {
mprahl 7d811e
            'error': 'The branch "master" already exists',
mprahl 7d811e
            'error_code': 'ENOCODE'
mprahl 7d811e
        }
mprahl 7d811e
        self.assertEqual(data, expected_output)
mprahl 7d811e
mprahl 7d811e
    def test_api_new_git_branch_from_commit(self):
mprahl 7d811e
        """ Test the api_new_branch method of the flask api """
mprahl 7d811e
        tests.create_projects(self.session)
mprahl 7d811e
        repos_path = os.path.join(self.path, 'repos')
mprahl 7d811e
        tests.create_projects_git(repos_path, bare=True)
mprahl 7d811e
        git_path = os.path.join(repos_path, 'test.git')
mprahl 7d811e
        tests.add_content_git_repo(git_path)
mprahl 7d811e
        tests.create_tokens(self.session, project_id=None)
mprahl 7d811e
        tests.create_tokens_acl(
mprahl 7d811e
            self.session, 'aaabbbcccddd', 'modify_project')
mprahl 7d811e
        repo_obj = pygit2.Repository(git_path)
mprahl 7d811e
        from_commit = repo_obj.revparse_single('HEAD').oid.hex
mprahl 7d811e
        headers = {'Authorization': 'token aaabbbcccddd'}
mprahl 7d811e
        args = {'branch': 'test123', 'from_commit': from_commit}
mprahl 7d811e
        output = self.app.post('/api/0/test/git/branch', headers=headers,
mprahl 7d811e
                               data=args)
mprahl 7d811e
        self.assertEqual(output.status_code, 200)
mprahl 7d811e
        data = json.loads(output.data)
mprahl 7d811e
        expected_output = {
mprahl 7d811e
            'message': 'Project branch was created',
mprahl 7d811e
        }
mprahl 7d811e
        self.assertEqual(data, expected_output)
mprahl 7d811e
        self.assertIn('test123', repo_obj.listall_branches())
mprahl 7d811e
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
class PagureFlaskApiProjectFlagtests(tests.Modeltests):
Pierre-Yves Chibon 8715b5
    """ Tests for the flask API of pagure for flagging commit in project
Pierre-Yves Chibon 8715b5
    """
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
    def setUp(self):
Pierre-Yves Chibon 8715b5
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 8715b5
        super(PagureFlaskApiProjectFlagtests, self).setUp()
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        tests.create_projects(self.session)
Pierre-Yves Chibon 8715b5
        repo_path = os.path.join(self.path, 'repos')
Pierre-Yves Chibon 8715b5
        self.git_path = os.path.join(repo_path, 'test.git')
Pierre-Yves Chibon 8715b5
        tests.create_projects_git(repo_path, bare=True)
Pierre-Yves Chibon 8715b5
        tests.add_content_git_repo(self.git_path)
Pierre-Yves Chibon 8715b5
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 8715b5
        tests.create_tokens_acl(
Pierre-Yves Chibon 8715b5
            self.session, 'aaabbbcccddd', 'commit_flag')
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon abcbf7
    def test_flag_commit_missing_status(self):
Pierre-Yves Chibon 8715b5
        """ Test flagging a commit with missing precentage. """
Pierre-Yves Chibon 8715b5
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 8715b5
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 8715b5
        data = {
Pierre-Yves Chibon 8715b5
            'username': 'Jenkins',
Pierre-Yves Chibon 8715b5
            'comment': 'Tests passed',
Pierre-Yves Chibon 8715b5
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 8715b5
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 8715b5
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon 8715b5
            headers=headers, data=data)
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 8715b5
        data = json.loads(output.data)
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 2b23a1
          "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 8715b5
          "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 8715b5
          "errors": {
Pierre-Yves Chibon abcbf7
            "status": [
Pierre-Yves Chibon abcbf7
              "Not a valid choice"
Pierre-Yves Chibon 8715b5
            ]
Pierre-Yves Chibon 8715b5
          }
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
    def test_flag_commit_missing_username(self):
Pierre-Yves Chibon 8715b5
        """ Test flagging a commit with missing username. """
Pierre-Yves Chibon 8715b5
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 8715b5
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 8715b5
        data = {
Pierre-Yves Chibon 8715b5
            'percent': 100,
Pierre-Yves Chibon 8715b5
            'comment': 'Tests passed',
Pierre-Yves Chibon 8715b5
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 8715b5
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon abcbf7
            'status': 'success',
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 8715b5
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon 8715b5
            headers=headers, data=data)
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 8715b5
        data = json.loads(output.data)
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 2b23a1
          "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 8715b5
          "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 8715b5
          "errors": {
Pierre-Yves Chibon 8715b5
            "username": [
Pierre-Yves Chibon 8715b5
              "This field is required."
Pierre-Yves Chibon 8715b5
            ]
Pierre-Yves Chibon 8715b5
          }
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
    def test_flag_commit_missing_comment(self):
Pierre-Yves Chibon 8715b5
        """ Test flagging a commit with missing comment. """
Pierre-Yves Chibon 8715b5
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 8715b5
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 8715b5
        data = {
Pierre-Yves Chibon 8715b5
            'username': 'Jenkins',
Pierre-Yves Chibon 8715b5
            'percent': 100,
Pierre-Yves Chibon 8715b5
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 8715b5
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon abcbf7
            'status': 'success',
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 8715b5
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon 8715b5
            headers=headers, data=data)
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 8715b5
        data = json.loads(output.data)
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 2b23a1
          "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 8715b5
          "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 8715b5
          "errors": {
Pierre-Yves Chibon 8715b5
            "comment": [
Pierre-Yves Chibon 8715b5
              "This field is required."
Pierre-Yves Chibon 8715b5
            ]
Pierre-Yves Chibon 8715b5
          }
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
    def test_flag_commit_missing_url(self):
Pierre-Yves Chibon 8715b5
        """ Test flagging a commit with missing url. """
Pierre-Yves Chibon 8715b5
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 8715b5
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 8715b5
        data = {
Pierre-Yves Chibon 8715b5
            'username': 'Jenkins',
Pierre-Yves Chibon 8715b5
            'percent': 100,
Pierre-Yves Chibon 8715b5
            'comment': 'Tests passed',
Pierre-Yves Chibon 8715b5
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon abcbf7
            'status': 'success',
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 8715b5
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon 8715b5
            headers=headers, data=data)
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 8715b5
        data = json.loads(output.data)
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 2b23a1
          "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 8715b5
          "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 8715b5
          "errors": {
Pierre-Yves Chibon 8715b5
            "url": [
Pierre-Yves Chibon 8715b5
              "This field is required."
Pierre-Yves Chibon 8715b5
            ]
Pierre-Yves Chibon 8715b5
          }
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
    def test_flag_commit_invalid_token(self):
Pierre-Yves Chibon 8715b5
        """ Test flagging a commit with missing info. """
Pierre-Yves Chibon 8715b5
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 8715b5
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        headers = {'Authorization': 'token 123'}
Pierre-Yves Chibon 8715b5
        data = {
Pierre-Yves Chibon 8715b5
            'username': 'Jenkins',
Pierre-Yves Chibon 8715b5
            'percent': 100,
Pierre-Yves Chibon 8715b5
            'comment': 'Tests passed',
Pierre-Yves Chibon 8715b5
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 8715b5
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 8715b5
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon 8715b5
            headers=headers, data=data)
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 8715b5
        data = json.loads(output.data)
Pierre-Yves Chibon 20753d
        self.assertEqual(sorted(data.keys()), ['error', 'error_code'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 20753d
        self.assertEqual(
Pierre-Yves Chibon 20753d
            pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon abcbf7
    def test_flag_commit_invalid_status(self):
Pierre-Yves Chibon abcbf7
        """ Test flagging a commit with an invalid status. """
Pierre-Yves Chibon abcbf7
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon abcbf7
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon abcbf7
Pierre-Yves Chibon abcbf7
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon abcbf7
        data = {
Pierre-Yves Chibon abcbf7
            'username': 'Jenkins',
Pierre-Yves Chibon abcbf7
            'percent': 100,
Pierre-Yves Chibon abcbf7
            'comment': 'Tests passed',
Pierre-Yves Chibon abcbf7
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon abcbf7
            'status': 'foobar',
Pierre-Yves Chibon abcbf7
        }
Pierre-Yves Chibon abcbf7
        output = self.app.post(
Pierre-Yves Chibon abcbf7
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon abcbf7
            headers=headers, data=data)
Pierre-Yves Chibon abcbf7
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon abcbf7
        data = json.loads(output.data)
Pierre-Yves Chibon abcbf7
        self.assertEqual(
Pierre-Yves Chibon abcbf7
            data,
Pierre-Yves Chibon abcbf7
            {
Pierre-Yves Chibon abcbf7
              u'errors': {u'status': [u'Not a valid choice']},
Pierre-Yves Chibon abcbf7
              u'error_code': u'EINVALIDREQ',
Pierre-Yves Chibon 2b23a1
              u'error': u'Invalid or incomplete input submitted'
Pierre-Yves Chibon abcbf7
            }
Pierre-Yves Chibon abcbf7
        )
Pierre-Yves Chibon abcbf7
Pierre-Yves Chibon 8715b5
    def test_flag_commit_with_uid(self):
Pierre-Yves Chibon 8715b5
        """ Test flagging a commit with provided uid. """
Pierre-Yves Chibon 8715b5
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 8715b5
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 8715b5
        data = {
Pierre-Yves Chibon 8715b5
            'username': 'Jenkins',
Pierre-Yves Chibon 8715b5
            'percent': 100,
Pierre-Yves Chibon 8715b5
            'comment': 'Tests passed',
Pierre-Yves Chibon 8715b5
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 8715b5
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon abcbf7
            'status': 'success',
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 8715b5
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon 8715b5
            headers=headers, data=data)
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 8715b5
        data = json.loads(output.data)
Pierre-Yves Chibon 8715b5
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 8715b5
        data['flag']['commit_hash'] = u'62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 8715b5
            u'flag': {
Pierre-Yves Chibon 8715b5
                u'comment': u'Tests passed',
Pierre-Yves Chibon 8715b5
                u'commit_hash': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 8715b5
                u'date_created': u'1510742565',
Pierre-Yves Chibon 8715b5
                u'percent': 100,
Pierre-Yves Chibon abcbf7
                u'status': 'success',
Pierre-Yves Chibon 8715b5
                u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 8715b5
                u'user': {
Pierre-Yves Chibon 8715b5
                    u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 8715b5
                    u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 8715b5
                    u'fullname': u'PY C',
Pierre-Yves Chibon 8715b5
                    u'name': u'pingou'},
Pierre-Yves Chibon 8715b5
                u'username': u'Jenkins'
Pierre-Yves Chibon 8715b5
            },
Pierre-Yves Chibon 8715b5
            u'message': u'Flag added',
Pierre-Yves Chibon 8715b5
            u'uid': u'jenkins_build_pagure_100+seed'
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6882b8
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 6882b8
    def test_flag_commit_without_uid(self, mock_email):
Pierre-Yves Chibon 6882b8
        """ Test flagging a commit with missing info.
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6882b8
        Also ensure notifications aren't sent when they are not asked for.
Pierre-Yves Chibon 6882b8
        """
Pierre-Yves Chibon 8715b5
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 8715b5
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 8715b5
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 8715b5
        data = {
Pierre-Yves Chibon 8715b5
            'username': 'Jenkins',
Pierre-Yves Chibon 8715b5
            'percent': 100,
Pierre-Yves Chibon 8715b5
            'comment': 'Tests passed',
Pierre-Yves Chibon 8715b5
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon abcbf7
            'status': 'success',
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        output = self.app.post(
Pierre-Yves Chibon 8715b5
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon 8715b5
            headers=headers, data=data)
Pierre-Yves Chibon 8715b5
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 8715b5
        data = json.loads(output.data)
Pierre-Yves Chibon 8715b5
        self.assertNotEqual(
Pierre-Yves Chibon 8715b5
            data['uid'],
Pierre-Yves Chibon 8715b5
            u'jenkins_build_pagure_100+seed'
Pierre-Yves Chibon 8715b5
        )
Pierre-Yves Chibon 8715b5
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 8715b5
        data['uid'] = 'b1de8f80defd4a81afe2e09f39678087'
Pierre-Yves Chibon 8715b5
        expected_output = {
Pierre-Yves Chibon 8715b5
            u'flag': {
Pierre-Yves Chibon 8715b5
                u'comment': u'Tests passed',
Pierre-Yves Chibon 6882b8
                u'commit_hash': commit.oid.hex,
Pierre-Yves Chibon 8715b5
                u'date_created': u'1510742565',
Pierre-Yves Chibon 8715b5
                u'percent': 100,
Pierre-Yves Chibon abcbf7
                u'status': 'success',
Pierre-Yves Chibon 8715b5
                u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 8715b5
                u'user': {
Pierre-Yves Chibon 8715b5
                    u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 8715b5
                    u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 8715b5
                    u'fullname': u'PY C',
Pierre-Yves Chibon 8715b5
                    u'name': u'pingou'},
Pierre-Yves Chibon 8715b5
                u'username': u'Jenkins'
Pierre-Yves Chibon 8715b5
            },
Pierre-Yves Chibon 8715b5
            u'message': u'Flag added',
Pierre-Yves Chibon 8715b5
            u'uid': u'b1de8f80defd4a81afe2e09f39678087'
Pierre-Yves Chibon 8715b5
        }
Pierre-Yves Chibon 8715b5
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon 6882b8
        mock_email.assert_not_called()
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6882b8
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 6882b8
    def test_flag_commit_with_notification(self, mock_email):
Pierre-Yves Chibon 6882b8
        """ Test flagging a commit with notification enabled. """
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6882b8
        # Enable commit notifications
Pierre-Yves Chibon 6882b8
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 6882b8
        settings = repo.settings
Pierre-Yves Chibon 6882b8
        settings['notify_on_commit_flag'] = True
Pierre-Yves Chibon 6882b8
        repo.settings = settings
Pierre-Yves Chibon 6882b8
        self.session.add(repo)
Pierre-Yves Chibon 6882b8
        self.session.commit()
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6882b8
        repo_obj = pygit2.Repository(self.git_path)
Pierre-Yves Chibon 6882b8
        commit = repo_obj.revparse_single('HEAD')
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6882b8
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 6882b8
        data = {
Pierre-Yves Chibon 6882b8
            'username': 'Jenkins',
Pierre-Yves Chibon 6882b8
            'percent': 100,
Pierre-Yves Chibon 6882b8
            'comment': 'Tests passed',
Pierre-Yves Chibon 6882b8
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 6882b8
            'status': 'success',
Pierre-Yves Chibon 6882b8
        }
Pierre-Yves Chibon 6882b8
        output = self.app.post(
Pierre-Yves Chibon 6882b8
            '/api/0/test/c/%s/flag' % commit.oid.hex,
Pierre-Yves Chibon 6882b8
            headers=headers, data=data)
Pierre-Yves Chibon 6882b8
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6882b8
        data = json.loads(output.data)
Pierre-Yves Chibon 6882b8
        self.assertNotEqual(
Pierre-Yves Chibon 6882b8
            data['uid'],
Pierre-Yves Chibon 6882b8
            u'jenkins_build_pagure_100+seed'
Pierre-Yves Chibon 6882b8
        )
Pierre-Yves Chibon 6882b8
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 6882b8
        data['uid'] = 'b1de8f80defd4a81afe2e09f39678087'
Pierre-Yves Chibon 6882b8
        expected_output = {
Pierre-Yves Chibon 6882b8
            u'flag': {
Pierre-Yves Chibon 6882b8
                u'comment': u'Tests passed',
Pierre-Yves Chibon 6882b8
                u'commit_hash': commit.oid.hex,
Pierre-Yves Chibon 6882b8
                u'date_created': u'1510742565',
Pierre-Yves Chibon 6882b8
                u'percent': 100,
Pierre-Yves Chibon 6882b8
                u'status': 'success',
Pierre-Yves Chibon 6882b8
                u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 6882b8
                u'user': {
Pierre-Yves Chibon 6882b8
                    u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 6882b8
                    u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 6882b8
                    u'fullname': u'PY C',
Pierre-Yves Chibon 6882b8
                    u'name': u'pingou'},
Pierre-Yves Chibon 6882b8
                u'username': u'Jenkins'
Pierre-Yves Chibon 6882b8
            },
Pierre-Yves Chibon 6882b8
            u'message': u'Flag added',
Pierre-Yves Chibon 6882b8
            u'uid': u'b1de8f80defd4a81afe2e09f39678087'
Pierre-Yves Chibon 6882b8
        }
Pierre-Yves Chibon 6882b8
        self.assertEqual(data, expected_output)
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 6882b8
        mock_email.assert_called_once_with(
Pierre-Yves Chibon 6882b8
            u'\nJenkins flagged the commit '
Pierre-Yves Chibon 6882b8
            u'`' + commit.oid.hex + u'` as success: '
Pierre-Yves Chibon 6882b8
            u'Tests passed\n\n'
Pierre-Yves Chibon 6882b8
            u'https://pagure.org/test/c/' + commit.oid.hex + u'\n',
Pierre-Yves Chibon 6882b8
            u'Coommit #' + commit.oid.hex + u' - Jenkins: success',
Pierre-Yves Chibon 6882b8
            u'bar@pingou.com',
Pierre-Yves Chibon 6882b8
            in_reply_to=u'test-project-1',
Pierre-Yves Chibon 6882b8
            mail_id=u'test-commit-1-1',
Pierre-Yves Chibon 6882b8
            project_name=u'test',
Pierre-Yves Chibon 6882b8
            user_from=u'Jenkins'
Pierre-Yves Chibon 6882b8
        )
Pierre-Yves Chibon 6882b8
Pierre-Yves Chibon 8715b5
Pierre-Yves Chibon f15f18
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)