|
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 |
f15f18 |
pagure.APP.config['REQUESTS_FOLDER'] = None
|
|
Pierre-Yves Chibon |
f15f18 |
pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
|
|
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 |
f15f18 |
{'tags': ['0.0.1']}
|
|
Pierre-Yves Chibon |
f15f18 |
)
|
|
Pierre-Yves Chibon |
f15f18 |
|
|
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 |
ceb262 |
ticketfolder=None, redis=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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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)
|