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
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
Pierre-Yves Chibon 1c1b5d
            tests.HERE, 'forks')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 1c1b5d
            tests.HERE, 'requests')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 1c1b5d
            tests.HERE, 'tickets')
Pierre-Yves Chibon 1c1b5d
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 1c1b5d
            tests.HERE, '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
Pierre-Yves Chibon f15f18
        gitrepo = os.path.join(tests.HERE, '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
Pierre-Yves Chibon ceb262
        output = pagure.lib.update_tags(
Pierre-Yves Chibon ceb262
            self.session, repo, 'infra', 'pingou',
Pierre-Yves Chibon 2e464a
            ticketfolder=None)
Pierre-Yves Chibon ceb262
        self.assertEqual(output, ['Tag added: 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 226628
              "total_projects": 1,
Pierre-Yves Chibon 07a462
              "projects": [
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon 07a462
                  "date_created": "1436527638",
Pierre-Yves Chibon 07a462
                  "description": "test project #1",
Pierre-Yves Chibon 07a462
                  "id": 1,
Pierre-Yves Chibon 07a462
                  "name": "test",
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"
Pierre-Yves Chibon 7485e2
        self.assertDictEqual(
Pierre-Yves Chibon 7485e2
            data,
Pierre-Yves Chibon 07a462
            {
Pierre-Yves Chibon 226628
              "total_projects": 2,
Pierre-Yves Chibon 07a462
              "projects": [
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon 07a462
                  "date_created": "1436527638",
Pierre-Yves Chibon 07a462
                  "description": "test project #1",
Pierre-Yves Chibon 07a462
                  "id": 1,
Pierre-Yves Chibon 07a462
                  "name": "test",
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
                  "date_created": "1436527638",
Pierre-Yves Chibon 07a462
                  "description": "test project #2",
Pierre-Yves Chibon 07a462
                  "id": 2,
Pierre-Yves Chibon 07a462
                  "name": "test2",
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
                  }
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 226628
              "total_projects": 1,
Pierre-Yves Chibon 07a462
              "projects": [
Pierre-Yves Chibon 07a462
                {
Pierre-Yves Chibon 07a462
                  "date_created": "1436527638",
Pierre-Yves Chibon 07a462
                  "description": "test project #1",
Pierre-Yves Chibon 07a462
                  "id": 1,
Pierre-Yves Chibon 07a462
                  "name": "test",
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 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 1c1b5d
        tests.create_projects(self.session)
Pierre-Yves Chibon 1c1b5d
        tests.create_projects_git(os.path.join(tests.HERE, '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 1c1b5d
        self.assertDictEqual(
Pierre-Yves Chibon 1c1b5d
            data,
Pierre-Yves Chibon 1c1b5d
            {
Pierre-Yves Chibon 1c1b5d
                "error": "Invalid or expired token. Please visit " \
Pierre-Yves Chibon 1c1b5d
                "https://pagure.org/ to get or renew your API token.",
Pierre-Yves Chibon 1c1b5d
                "error_code": "EINVALIDTOK"
Pierre-Yves Chibon 1c1b5d
            }
Pierre-Yves Chibon 1c1b5d
        )
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 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 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 f15f18
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)