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 f15f18
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
 Authors:
Pierre-Yves Chibon f15f18
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
"""
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon f15f18
import pkg_resources
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
import datetime
Pierre-Yves Chibon f15f18
import json
Pierre-Yves Chibon f15f18
import unittest
Pierre-Yves Chibon f15f18
import shutil
Pierre-Yves Chibon f15f18
import sys
Pierre-Yves Chibon f15f18
import tempfile
Pierre-Yves Chibon f15f18
import os
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
import pygit2
Pierre-Yves Chibon f15f18
from mock import patch
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon f15f18
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
import pagure.lib
Pierre-Yves Chibon f15f18
import tests
Pierre-Yves Chibon 27a73d
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
class PagureFlaskApiProjecttests(tests.Modeltests):
Pierre-Yves Chibon f15f18
    """ Tests for the flask API of pagure for issue """
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
    def setUp(self):
Pierre-Yves Chibon f15f18
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon f15f18
        super(PagureFlaskApiProjecttests, self).setUp()
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon f15f18
        pagure.SESSION = self.session
Pierre-Yves Chibon f15f18
        pagure.api.SESSION = self.session
Pierre-Yves Chibon f15f18
        pagure.api.project.SESSION = self.session
Pierre-Yves Chibon f15f18
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon f15f18
Jeremy Cline 20109f
        pagure.APP.config['GIT_FOLDER'] = os.path.join(self.path, 'repos')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'requests')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'tickets')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'docs')
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
    def test_api_git_tags(self):
Pierre-Yves Chibon f15f18
        """ Test the api_git_tags method of the flask api. """
Pierre-Yves Chibon f15f18
        tests.create_projects(self.session)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        # Create a git repo to play with
Jeremy Cline 20109f
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon f15f18
        repo = pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
Pierre-Yves Chibon f15f18
        repopath = os.path.join(newpath, 'test')
Pierre-Yves Chibon f15f18
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        # Create a file in that git repo
Pierre-Yves Chibon f15f18
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon f15f18
            stream.write('foo\n bar')
Pierre-Yves Chibon f15f18
        clone_repo.index.add('sources')
Pierre-Yves Chibon f15f18
        clone_repo.index.write()
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        # Commits the files added
Pierre-Yves Chibon f15f18
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon f15f18
        author = pygit2.Signature(
Pierre-Yves Chibon f15f18
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon f15f18
        committer = pygit2.Signature(
Pierre-Yves Chibon f15f18
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon f15f18
        clone_repo.create_commit(
Pierre-Yves Chibon f15f18
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon f15f18
            author,
Pierre-Yves Chibon f15f18
            committer,
Pierre-Yves Chibon f15f18
            'Add sources file for testing',
Pierre-Yves Chibon f15f18
            # binary string representing the tree object ID
Pierre-Yves Chibon f15f18
            tree,
Pierre-Yves Chibon f15f18
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon f15f18
            []
Pierre-Yves Chibon f15f18
        )
Pierre-Yves Chibon f15f18
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon f15f18
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 27a73d
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon f15f18
        # Tag our first commit
Pierre-Yves Chibon f15f18
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon f15f18
        tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0)
Pierre-Yves Chibon f15f18
        repo.create_tag(
Pierre-Yves Chibon f15f18
            "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger,
Pierre-Yves Chibon f15f18
            "Release 0.0.1")
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 0197ec
        # Check tags
Pierre-Yves Chibon f15f18
        output = self.app.get('/api/0/test/git/tags')
Pierre-Yves Chibon f15f18
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f15f18
        data = json.loads(output.data)
Pierre-Yves Chibon f15f18
        self.assertDictEqual(
Pierre-Yves Chibon f15f18
            data,
Pierre-Yves Chibon 226628
            {'tags': ['0.0.1'], 'total_tags': 1}
Pierre-Yves Chibon f15f18
        )
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 62ffe0
        shutil.rmtree(newpath)
Pierre-Yves Chibon 62ffe0
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 ceb262
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon ceb262
        self.assertEqual(repo.tags, [])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Adding a tag
Mark Reynolds eabdc8
        output = pagure.lib.update_tags(
Pierre-Yves Chibon ceb262
            self.session, repo, 'infra', 'pingou',
Pierre-Yves Chibon a18547
            None)
Pierre-Yves Chibon a18547
        self.assertEqual(output, ['Issue tagged with: infra'])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Check after adding
Pierre-Yves Chibon ceb262
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon ceb262
        self.assertEqual(len(repo.tags), 1)
Pierre-Yves Chibon ceb262
        self.assertEqual(repo.tags_text, ['infra'])
Pierre-Yves Chibon ceb262
Pierre-Yves Chibon ceb262
        # Check the API
Pierre-Yves Chibon ceb262
        output = self.app.get('/api/0/projects?tags=inf')
Pierre-Yves Chibon ceb262
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ceb262
        data = json.loads(output.data)
Pierre-Yves Chibon ceb262
        self.assertDictEqual(
Pierre-Yves Chibon ceb262
            data,
Pierre-Yves Chibon ceb262
            {'error_code': 'ENOPROJECTS', 'error': 'No projects found'}
Pierre-Yves Chibon ceb262
        )
Pierre-Yves Chibon ceb262
        output = self.app.get('/api/0/projects?tags=infra')
Pierre-Yves Chibon ceb262
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ceb262
        data = json.loads(output.data)
Pierre-Yves Chibon 07a462
        data['projects'][0]['date_created'] = "1436527638"
Pierre-Yves Chibon ceb262
        self.assertDictEqual(
Pierre-Yves Chibon ceb262
            data,
Pierre-Yves Chibon 07a462
            {
Pierre-Yves Chibon e33592
              "args": {
Pierre-Yves Chibon e33592
                "fork": None,
Pierre-Yves Chibon e33592
                "pattern": None,
Pierre-Yves Chibon e33592
                "tags": [
Pierre-Yves Chibon e33592
                  "infra"
Pierre-Yves Chibon e33592
                ],
Pierre-Yves Chibon e33592
                "username": None
Pierre-Yves Chibon e33592
              },
Pierre-Yves Chibon 226628
              "total_projects": 1,
Pierre-Yves Chibon 07a462
              "projects": [
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon f254bf
                  "close_status": [
Pierre-Yves Chibon f254bf
                    "Invalid",
Pierre-Yves Chibon f254bf
                    "Insufficient data",
Pierre-Yves Chibon f254bf
                    "Fixed",
Pierre-Yves Chibon f254bf
                    "Duplicate"
Pierre-Yves Chibon f254bf
                  ],
Pierre-Yves Chibon 022564
                  "custom_keys": [],
Pierre-Yves Chibon 07a462
                  "date_created": "1436527638",
Pierre-Yves Chibon 07a462
                  "description": "test project #1",
Pierre-Yves Chibon 07a462
                  "id": 1,
Pierre-Yves Chibon f254bf
                  "milestones": {},
Pierre-Yves Chibon 07a462
                  "name": "test",
Pierre-Yves Chibon 733a9d
                  "namespace": None,
Pierre-Yves Chibon 07a462
                  "parent": None,
Pierre-Yves Chibon 14bde2
                  "priorities": {},
Pierre-Yves Chibon a30916
                  "tags": ["infra"],
Pierre-Yves Chibon 07a462
                  "user": {
Pierre-Yves Chibon 07a462
                    "fullname": "PY C",
Pierre-Yves Chibon 07a462
                    "name": "pingou"
Pierre-Yves Chibon 07a462
                  }
Pierre-Yves Chibon 07a462
                }
Pierre-Yves Chibon 07a462
              ]
Pierre-Yves Chibon 07a462
            }
Pierre-Yves Chibon ceb262
        )
Pierre-Yves Chibon 7485e2
        output = self.app.get('/api/0/projects?username=pingou')
Pierre-Yves Chibon 7485e2
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 7485e2
        data = json.loads(output.data)
Pierre-Yves Chibon 07a462
        data['projects'][0]['date_created'] = "1436527638"
Pierre-Yves Chibon 07a462
        data['projects'][1]['date_created'] = "1436527638"
clime afed57
        data['projects'][2]['date_created'] = "1436527638"
Pierre-Yves Chibon 7485e2
        self.assertDictEqual(
Pierre-Yves Chibon 7485e2
            data,
Pierre-Yves Chibon 07a462
            {
Pierre-Yves Chibon e33592
              "args": {
Pierre-Yves Chibon e33592
                "fork": None,
Pierre-Yves Chibon e33592
                "pattern": None,
Pierre-Yves Chibon e33592
                "tags": [],
Pierre-Yves Chibon e33592
                "username": "pingou",
Pierre-Yves Chibon e33592
              },
clime afed57
              "total_projects": 3,
Pierre-Yves Chibon 07a462
              "projects": [
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon f254bf
                  "close_status": [
Pierre-Yves Chibon f254bf
                      "Invalid",
Pierre-Yves Chibon f254bf
                      "Insufficient data",
Pierre-Yves Chibon f254bf
                      "Fixed",
Pierre-Yves Chibon f254bf
                      "Duplicate"
Pierre-Yves Chibon f254bf
                    ],
Pierre-Yves Chibon 022564
                  "custom_keys": [],
Pierre-Yves Chibon 07a462
                  "date_created": "1436527638",
Pierre-Yves Chibon 07a462
                  "description": "test project #1",
Pierre-Yves Chibon 07a462
                  "id": 1,
Pierre-Yves Chibon f254bf
                  "milestones": {},
Pierre-Yves Chibon 07a462
                  "name": "test",
Pierre-Yves Chibon 733a9d
                  "namespace": None,
Pierre-Yves Chibon 07a462
                  "parent": None,
Pierre-Yves Chibon 14bde2
                  "priorities": {},
Pierre-Yves Chibon a30916
                  "tags": ["infra"],
Pierre-Yves Chibon 07a462
                  "user": {
Pierre-Yves Chibon 07a462
                    "fullname": "PY C",
Pierre-Yves Chibon 07a462
                    "name": "pingou"
Pierre-Yves Chibon 07a462
                  }
Pierre-Yves Chibon 07a462
                },
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon f254bf
                  "close_status": [
Pierre-Yves Chibon f254bf
                      "Invalid",
Pierre-Yves Chibon f254bf
                      "Insufficient data",
Pierre-Yves Chibon f254bf
                      "Fixed",
Pierre-Yves Chibon f254bf
                      "Duplicate"
Pierre-Yves Chibon f254bf
                    ],
Pierre-Yves Chibon 022564
                  "custom_keys": [],
Pierre-Yves Chibon 07a462
                  "date_created": "1436527638",
Pierre-Yves Chibon 07a462
                  "description": "test project #2",
Pierre-Yves Chibon 07a462
                  "id": 2,
Pierre-Yves Chibon f254bf
                  "milestones": {},
Pierre-Yves Chibon 07a462
                  "name": "test2",
Pierre-Yves Chibon 733a9d
                  "namespace": None,
Pierre-Yves Chibon 07a462
                  "parent": None,
Pierre-Yves Chibon 14bde2
                  "priorities": {},
Pierre-Yves Chibon a30916
                  "tags": [],
Pierre-Yves Chibon 07a462
                  "user": {
Pierre-Yves Chibon 07a462
                    "fullname": "PY C",
Pierre-Yves Chibon 07a462
                    "name": "pingou"
Pierre-Yves Chibon 07a462
                  }
clime afed57
                },
clime afed57
                {
clime afed57
                  "close_status": [
clime afed57
                      "Invalid",
clime afed57
                      "Insufficient data",
clime afed57
                      "Fixed",
clime afed57
                      "Duplicate"
clime afed57
                    ],
clime afed57
                  "custom_keys": [],
clime afed57
                  "date_created": "1436527638",
clime afed57
                  "description": "namespaced test project",
clime afed57
                  "id": 3,
clime afed57
                  "milestones": {},
clime afed57
                  "name": "test3",
clime afed57
                  "namespace": "somenamespace",
clime afed57
                  "parent": None,
clime afed57
                  "priorities": {},
clime afed57
                  "tags": [],
clime afed57
                  "user": {
clime afed57
                    "fullname": "PY C",
clime afed57
                    "name": "pingou"
clime afed57
                  }
Pierre-Yves Chibon 07a462
                }
Pierre-Yves Chibon 07a462
              ]
Pierre-Yves Chibon 07a462
            }
Pierre-Yves Chibon 7485e2
        )
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"
Pierre-Yves Chibon 7485e2
        self.assertDictEqual(
Pierre-Yves Chibon 7485e2
            data,
Pierre-Yves Chibon 07a462
            {
Pierre-Yves Chibon e33592
              "args": {
Pierre-Yves Chibon e33592
                "fork": None,
Pierre-Yves Chibon e33592
                "pattern": None,
Pierre-Yves Chibon e33592
                "tags": [
Pierre-Yves Chibon e33592
                  "infra"
Pierre-Yves Chibon e33592
                ],
Pierre-Yves Chibon e33592
                "username": "pingou"
Pierre-Yves Chibon e33592
              },
Pierre-Yves Chibon 226628
              "total_projects": 1,
Pierre-Yves Chibon 07a462
              "projects": [
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon f254bf
                  "close_status": [
Pierre-Yves Chibon f254bf
                      "Invalid",
Pierre-Yves Chibon f254bf
                      "Insufficient data",
Pierre-Yves Chibon f254bf
                      "Fixed",
Pierre-Yves Chibon f254bf
                      "Duplicate"
Pierre-Yves Chibon f254bf
                    ],
Pierre-Yves Chibon 022564
                  "custom_keys": [],
Pierre-Yves Chibon 07a462
                  "date_created": "1436527638",
Pierre-Yves Chibon 07a462
                  "description": "test project #1",
Pierre-Yves Chibon 07a462
                  "id": 1,
Pierre-Yves Chibon f254bf
                  "milestones": {},
Pierre-Yves Chibon 07a462
                  "name": "test",
Pierre-Yves Chibon 733a9d
                  "namespace": None,
Pierre-Yves Chibon 07a462
                  "parent": None,
Pierre-Yves Chibon 14bde2
                  "priorities": {},
Pierre-Yves Chibon a30916
                  "tags": ["infra"],
Pierre-Yves Chibon 07a462
                  "user": {
Pierre-Yves Chibon 07a462
                    "fullname": "PY C",
Pierre-Yves Chibon 07a462
                    "name": "pingou"
Pierre-Yves Chibon 07a462
                  }
Pierre-Yves Chibon 07a462
                }
Pierre-Yves Chibon 07a462
              ]
Pierre-Yves Chibon 07a462
            }
Pierre-Yves Chibon 7485e2
        )
Pierre-Yves Chibon ceb262
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 b42ba1
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon b42ba1
        self.assertEqual(repo.tags, [])
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Adding a tag
Mark Reynolds eabdc8
        output = pagure.lib.update_tags(
Pierre-Yves Chibon b42ba1
            self.session, repo, 'infra', 'pingou',
Pierre-Yves Chibon a18547
            ticketfolder=None)
Pierre-Yves Chibon a18547
        self.assertEqual(output, ['Issue tagged with: infra'])
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon b42ba1
        # Check after adding
Pierre-Yves Chibon b42ba1
        repo = pagure.lib.get_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"
Pierre-Yves Chibon b42ba1
        self.assertDictEqual(
Pierre-Yves Chibon b42ba1
            data,
Pierre-Yves Chibon b42ba1
            {
Pierre-Yves Chibon b42ba1
              "close_status": [
Pierre-Yves Chibon b42ba1
                "Invalid",
Pierre-Yves Chibon b42ba1
                "Insufficient data",
Pierre-Yves Chibon b42ba1
                "Fixed",
Pierre-Yves Chibon b42ba1
                "Duplicate"
Pierre-Yves Chibon b42ba1
              ],
Pierre-Yves Chibon b42ba1
              "custom_keys": [],
Pierre-Yves Chibon b42ba1
              "date_created": "1436527638",
Pierre-Yves Chibon b42ba1
              "description": "test project #1",
Pierre-Yves Chibon b42ba1
              "id": 1,
Pierre-Yves Chibon b42ba1
              "milestones": {},
Pierre-Yves Chibon b42ba1
              "name": "test",
Pierre-Yves Chibon b42ba1
              "namespace": None,
Pierre-Yves Chibon b42ba1
              "parent": None,
Pierre-Yves Chibon b42ba1
              "priorities": {},
Pierre-Yves Chibon b42ba1
              "tags": ["infra"],
Pierre-Yves Chibon b42ba1
              "user": {
Pierre-Yves Chibon b42ba1
                "fullname": "PY C",
Pierre-Yves Chibon b42ba1
                "name": "pingou"
Pierre-Yves Chibon b42ba1
              }
Pierre-Yves Chibon b42ba1
            }
Pierre-Yves Chibon b42ba1
        )
Pierre-Yves Chibon b42ba1
Pierre-Yves Chibon 1c1b5d
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon 1c1b5d
    def test_api_new_project(self, p_gga):
Pierre-Yves Chibon 1c1b5d
        """ Test the api_new_project method of the flask api. """
Pierre-Yves Chibon 1c1b5d
        p_gga.return_value = True
Pierre-Yves Chibon 1c1b5d
Jeremy Cline 20109f
        tests.create_projects_git(os.path.join(self.path, 'tickets'))
Pierre-Yves Chibon 1c1b5d
        tests.create_tokens(self.session)
Pierre-Yves Chibon 1c1b5d
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        headers = {'Authorization': 'token foo_token'}
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Invalid token
Pierre-Yves Chibon 1c1b5d
        output = self.app.post('/api/0/new', headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Jeremy Cline 099538
                         data['error_code'])
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # No input
Pierre-Yves Chibon 1c1b5d
        output = self.app.post('/api/0/new', headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 1c1b5d
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 1c1b5d
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {
Pierre-Yves Chibon f7fcaa
                "name": ["This field is required."],
Pierre-Yves Chibon f7fcaa
                "description": ["This field is required."]
Pierre-Yves Chibon f7fcaa
              }
Pierre-Yves Chibon 1c1b5d
            }
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        data = {
Pierre-Yves Chibon 1c1b5d
            'name': 'test',
Pierre-Yves Chibon 1c1b5d
        }
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Incomplete request
Pierre-Yves Chibon 1c1b5d
        output = self.app.post(
Pierre-Yves Chibon 1c1b5d
            '/api/0/new', data=data, headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 1c1b5d
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 1c1b5d
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {"description": ["This field is required."]}
Pierre-Yves Chibon 1c1b5d
            }
Pierre-Yves Chibon 1c1b5d
        )
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        data = {
Pierre-Yves Chibon 1c1b5d
            'name': 'test',
Pierre-Yves Chibon 1c1b5d
            'description': 'Just a small test project',
Pierre-Yves Chibon 1c1b5d
        }
Pierre-Yves Chibon 1c1b5d
Pierre-Yves Chibon 1c1b5d
        # Valid request but repo already exists
Pierre-Yves Chibon 1c1b5d
        output = self.app.post(
Pierre-Yves Chibon 1c1b5d
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon 1c1b5d
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1c1b5d
        data = json.loads(output.data)
Pierre-Yves Chibon 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 1c1b5d
                "error": "The tickets repo \"test.git\" already exists",
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 f0e08e
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon baec09
    def test_api_new_project_user_ns(self, p_gga):
Pierre-Yves Chibon baec09
        """ Test the api_new_project method of the flask api. """
Pierre-Yves Chibon baec09
        pagure.APP.config['USER_NAMESPACE'] = True
Pierre-Yves Chibon baec09
        p_gga.return_value = True
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        tests.create_projects(self.session)
Pierre-Yves Chibon baec09
        tests.create_projects_git(os.path.join(self.path, 'tickets'))
Pierre-Yves Chibon baec09
        tests.create_tokens(self.session)
Pierre-Yves Chibon baec09
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Create a project with the user namespace feature on
Pierre-Yves Chibon baec09
        data = {
Pierre-Yves Chibon baec09
            'name': 'testproject',
Pierre-Yves Chibon baec09
            'description': 'Just another small test project',
Pierre-Yves Chibon baec09
        }
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Valid request
Pierre-Yves Chibon baec09
        output = self.app.post(
Pierre-Yves Chibon baec09
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon baec09
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon baec09
        data = json.loads(output.data)
Pierre-Yves Chibon baec09
        self.assertDictEqual(
Pierre-Yves Chibon baec09
            data,
Pierre-Yves Chibon baec09
            {'message': 'Project "pingou/testproject" created'}
Pierre-Yves Chibon baec09
        )
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Create a project with a namespace and the user namespace feature on
Pierre-Yves Chibon baec09
        pagure.APP.config['ALLOWED_PREFIX'] = ['testns']
Pierre-Yves Chibon baec09
        data = {
Pierre-Yves Chibon baec09
            'name': 'testproject2',
Pierre-Yves Chibon baec09
            'namespace': 'testns',
Pierre-Yves Chibon baec09
            'description': 'Just another small test project',
Pierre-Yves Chibon baec09
        }
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        # Valid request
Pierre-Yves Chibon baec09
        output = self.app.post(
Pierre-Yves Chibon baec09
            '/api/0/new/', data=data, headers=headers)
Pierre-Yves Chibon baec09
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon baec09
        data = json.loads(output.data)
Pierre-Yves Chibon baec09
        self.assertDictEqual(
Pierre-Yves Chibon baec09
            data,
Pierre-Yves Chibon baec09
            {'message': 'Project "testns/testproject2" created'}
Pierre-Yves Chibon baec09
        )
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
        pagure.APP.config['USER_NAMESPACE'] = False
Pierre-Yves Chibon baec09
Pierre-Yves Chibon baec09
    @patch('pagure.lib.git.generate_gitolite_acls')
Pierre-Yves Chibon f0e08e
    def test_api_fork_project(self, p_gga):
Pierre-Yves Chibon f0e08e
        """ Test the api_fork_project method of the flask api. """
Pierre-Yves Chibon f0e08e
        p_gga.return_value = True
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        tests.create_projects(self.session)
Pierre-Yves Chibon f0e08e
        for folder in ['docs', 'tickets', 'requests', 'repos']:
Pierre-Yves Chibon f0e08e
            tests.create_projects_git(
Jeremy Cline 20109f
                os.path.join(self.path, folder), bare=True)
Pierre-Yves Chibon f0e08e
        tests.create_tokens(self.session)
Pierre-Yves Chibon f0e08e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        headers = {'Authorization': 'token foo_token'}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Invalid token
Pierre-Yves Chibon f0e08e
        output = self.app.post('/api/0/fork', headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Jeremy Cline 099538
                         data['error_code'])
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # No input
Pierre-Yves Chibon f0e08e
        output = self.app.post('/api/0/fork', headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon f0e08e
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {"repo": ["This field is required."]}
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        data = {
Pierre-Yves Chibon f0e08e
            'name': 'test',
Pierre-Yves Chibon f0e08e
        }
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Incomplete request
Pierre-Yves Chibon f0e08e
        output = self.app.post(
Pierre-Yves Chibon f0e08e
            '/api/0/fork', data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon f0e08e
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {"repo": ["This field is required."]}
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        data = {
Pierre-Yves Chibon f0e08e
            'repo': 'test',
Pierre-Yves Chibon f0e08e
        }
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Valid request
Pierre-Yves Chibon f0e08e
        output = self.app.post(
Pierre-Yves Chibon f0e08e
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
                "message": "Repo \"test\" cloned to \"pingou/test\""
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        data = {
Pierre-Yves Chibon f0e08e
            'repo': 'test',
Pierre-Yves Chibon f0e08e
        }
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # project already forked
Pierre-Yves Chibon f0e08e
        output = self.app.post(
Pierre-Yves Chibon f0e08e
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
                "error": "Repo \"forks/pingou/test\" already exists",
Pierre-Yves Chibon f0e08e
                "error_code": "ENOCODE"
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        data = {
Pierre-Yves Chibon f0e08e
            'repo': 'test',
Pierre-Yves Chibon f0e08e
            'username': 'pingou',
Pierre-Yves Chibon f0e08e
        }
Pierre-Yves Chibon f0e08e
Pierre-Yves Chibon f0e08e
        # Fork already exists
Pierre-Yves Chibon f0e08e
        output = self.app.post(
Pierre-Yves Chibon f0e08e
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon f0e08e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f0e08e
        data = json.loads(output.data)
Pierre-Yves Chibon f0e08e
        self.assertDictEqual(
Pierre-Yves Chibon f0e08e
            data,
Pierre-Yves Chibon f0e08e
            {
Pierre-Yves Chibon f0e08e
                "error": "Repo \"forks/pingou/test\" already exists",
Pierre-Yves Chibon f0e08e
                "error_code": "ENOCODE"
Pierre-Yves Chibon f0e08e
            }
Pierre-Yves Chibon f0e08e
        )
Pierre-Yves Chibon f15f18
Pierre-Yves Chibon 2c43bb
        data = {
Pierre-Yves Chibon 2c43bb
            'repo': 'test',
Pierre-Yves Chibon 2c43bb
            'namespace': 'pingou',
Pierre-Yves Chibon 2c43bb
        }
Pierre-Yves Chibon 2c43bb
Pierre-Yves Chibon 2c43bb
        # Repo does not exists
Pierre-Yves Chibon 2c43bb
        output = self.app.post(
Pierre-Yves Chibon 2c43bb
            '/api/0/fork/', data=data, headers=headers)
Pierre-Yves Chibon 2c43bb
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 2c43bb
        data = json.loads(output.data)
Pierre-Yves Chibon 2c43bb
        self.assertDictEqual(
Pierre-Yves Chibon 2c43bb
            data,
Pierre-Yves Chibon 2c43bb
            {
Pierre-Yves Chibon 2c43bb
                "error": "Project not found",
Pierre-Yves Chibon 2c43bb
                "error_code": "ENOPROJECT"
Pierre-Yves Chibon 2c43bb
            }
Pierre-Yves Chibon 2c43bb
        )
Pierre-Yves Chibon 2c43bb
Pierre-Yves Chibon f15f18
if __name__ == '__main__':
Pierre-Yves Chibon f15f18
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon f15f18
        PagureFlaskApiProjecttests)
Pierre-Yves Chibon f15f18
    unittest.TextTestRunner(verbosity=2).run(SUITE)