|
Pierre-Yves Chibon |
62f012 |
# -*- coding: utf-8 -*-
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
62f012 |
"""
|
|
Pierre-Yves Chibon |
62f012 |
(c) 2015 - Copyright Red Hat Inc
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
62f012 |
Authors:
|
|
Pierre-Yves Chibon |
62f012 |
Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
62f012 |
"""
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Aurélien Bompard |
dcf6f6 |
from __future__ import unicode_literals
|
|
Aurélien Bompard |
dcf6f6 |
|
|
Pierre-Yves Chibon |
62f012 |
__requires__ = ['SQLAlchemy >= 0.8']
|
|
Pierre-Yves Chibon |
62f012 |
import pkg_resources
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
62f012 |
import unittest
|
|
Pierre-Yves Chibon |
62f012 |
import shutil
|
|
Pierre-Yves Chibon |
62f012 |
import sys
|
|
Pierre-Yves Chibon |
62f012 |
import os
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
62f012 |
import json
|
|
Pierre-Yves Chibon |
62f012 |
from mock import patch
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
62f012 |
sys.path.insert(0, os.path.join(os.path.dirname(
|
|
Pierre-Yves Chibon |
62f012 |
os.path.abspath(__file__)), '..'))
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Slavek Kabrda |
727932 |
import pagure.api
|
|
Pierre-Yves Chibon |
b130e5 |
import pagure.flask_app
|
|
Pierre-Yves Chibon |
fe5017 |
import pagure.lib
|
|
Pierre-Yves Chibon |
62f012 |
import tests
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Clement Verna |
109c4b |
class PagureFlaskApitests(tests.SimplePagureTest):
|
|
Pierre-Yves Chibon |
fe5017 |
""" Tests for flask API controller of pagure """
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
437cd0 |
maxDiff = None
|
|
Pierre-Yves Chibon |
437cd0 |
|
|
Pierre-Yves Chibon |
674045 |
def test_api_doc(self):
|
|
Pierre-Yves Chibon |
674045 |
""" Test the API documentation page. """
|
|
Pierre-Yves Chibon |
674045 |
output = self.app.get('/api/0/')
|
|
Pierre-Yves Chibon |
674045 |
output_text = output.get_data(as_text=True)
|
|
Pierre-Yves Chibon |
674045 |
self.assertIn(
|
|
Pierre-Yves Chibon |
674045 |
'<title> API | pagure - Pagure</title>\n', output_text)
|
|
Pierre-Yves Chibon |
674045 |
self.assertIn(
|
|
Pierre-Yves Chibon |
674045 |
' Pagure API Reference\n \n', output_text)
|
|
Pierre-Yves Chibon |
674045 |
|
|
Pierre-Yves Chibon |
674045 |
def test_api_doc_authenticated(self):
|
|
Pierre-Yves Chibon |
674045 |
""" Test the API documentation page. """
|
|
Pierre-Yves Chibon |
674045 |
user = tests.FakeUser(username='foo')
|
|
Pierre-Yves Chibon |
674045 |
with tests.user_set(self.app.application, user):
|
|
Pierre-Yves Chibon |
674045 |
output = self.app.get('/api/0/')
|
|
Pierre-Yves Chibon |
674045 |
output_text = output.get_data(as_text=True)
|
|
Pierre-Yves Chibon |
674045 |
self.assertIn(
|
|
Pierre-Yves Chibon |
674045 |
'<title> API | pagure - Pagure</title>\n', output_text)
|
|
Pierre-Yves Chibon |
674045 |
self.assertIn(
|
|
Pierre-Yves Chibon |
674045 |
' Pagure API Reference\n \n', output_text)
|
|
Pierre-Yves Chibon |
674045 |
|
|
Slavek Kabrda |
727932 |
def test_api_get_request_data(self):
|
|
Slavek Kabrda |
727932 |
data = {'foo': 'bar'}
|
|
Slavek Kabrda |
727932 |
# test_request_context doesn't set flask.g, but some teardown
|
|
Slavek Kabrda |
727932 |
# functions try to use that, so let's exclude them
|
|
Slavek Kabrda |
727932 |
self._app.teardown_request_funcs = {}
|
|
Slavek Kabrda |
727932 |
with self._app.test_request_context('/api/0/version', data=data):
|
|
Slavek Kabrda |
727932 |
self.assertEqual(pagure.api.get_request_data()['foo'], 'bar')
|
|
Slavek Kabrda |
727932 |
data = json.dumps(data)
|
|
Slavek Kabrda |
727932 |
with self._app.test_request_context('/api/0/version', data=data,
|
|
Slavek Kabrda |
727932 |
content_type='application/json'):
|
|
Slavek Kabrda |
727932 |
self.assertEqual(pagure.api.get_request_data()['foo'], 'bar')
|
|
Slavek Kabrda |
727932 |
|
|
Pierre-Yves Chibon |
0b9b6b |
def test_api_version_old_url(self):
|
|
Pierre-Yves Chibon |
62f012 |
""" Test the api_version function. """
|
|
Pierre-Yves Chibon |
62f012 |
output = self.app.get('/api/0/version')
|
|
Pierre-Yves Chibon |
62f012 |
self.assertEqual(output.status_code, 200)
|
|
Aurélien Bompard |
626417 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
fe5017 |
self.assertEqual(data['version'], pagure.__api_version__)
|
|
Aurélien Bompard |
626417 |
self.assertEqual(sorted(data.keys()), ['version'])
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
0b9b6b |
def test_api_version_new_url(self):
|
|
Pierre-Yves Chibon |
0b9b6b |
""" Test the api_version function at its new url. """
|
|
Pierre-Yves Chibon |
0b9b6b |
output = self.app.get('/api/0/-/version')
|
|
Pierre-Yves Chibon |
0b9b6b |
self.assertEqual(output.status_code, 200)
|
|
Pierre-Yves Chibon |
0b9b6b |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
0b9b6b |
self.assertEqual(data['version'], pagure.__api_version__)
|
|
Pierre-Yves Chibon |
0b9b6b |
self.assertEqual(sorted(data.keys()), ['version'])
|
|
Pierre-Yves Chibon |
0b9b6b |
|
|
Pierre-Yves Chibon |
dbea26 |
def test_api_project_tags(self):
|
|
Pierre-Yves Chibon |
dbea26 |
""" Test the api_project_tags function. """
|
|
Pierre-Yves Chibon |
dbea26 |
tests.create_projects(self.session)
|
|
Pierre-Yves Chibon |
dbea26 |
|
|
Pierre-Yves Chibon |
dbea26 |
output = self.app.get('/api/0/foo/tags/')
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(output.status_code, 404)
|
|
Aurélien Bompard |
626417 |
data = json.loads(output.get_data(as_text=True))
|
|
Aurélien Bompard |
f61bb3 |
self.assertEqual(set(data.keys()), set(['output', 'error']))
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(data['output'], 'notok')
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(data['error'], 'Project not found')
|
|
Pierre-Yves Chibon |
dbea26 |
|
|
Pierre-Yves Chibon |
dbea26 |
output = self.app.get('/api/0/test/tags/')
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(output.status_code, 200)
|
|
Aurélien Bompard |
626417 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
226628 |
self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(data['tags'], [])
|
|
Pierre-Yves Chibon |
226628 |
self.assertEqual(data['total_tags'], 0)
|
|
Pierre-Yves Chibon |
dbea26 |
|
|
Pierre-Yves Chibon |
dbea26 |
# Add an issue and tag it so that we can list them
|
|
Pierre-Yves Chibon |
fe5017 |
item = pagure.lib.model.Issue(
|
|
Pierre-Yves Chibon |
dbea26 |
id=1,
|
|
Pierre-Yves Chibon |
dbea26 |
uid='foobar',
|
|
Pierre-Yves Chibon |
dbea26 |
project_id=1,
|
|
Pierre-Yves Chibon |
dbea26 |
title='issue',
|
|
Pierre-Yves Chibon |
dbea26 |
content='a bug report',
|
|
Pierre-Yves Chibon |
dbea26 |
user_id=1, # pingou
|
|
Pierre-Yves Chibon |
dbea26 |
)
|
|
Pierre-Yves Chibon |
dbea26 |
self.session.add(item)
|
|
Pierre-Yves Chibon |
dbea26 |
self.session.commit()
|
|
Pierre-Yves Chibon |
a8f682 |
item = pagure.lib.model.TagColored(
|
|
Mark Reynolds |
403d8f |
tag='tag1', tag_color='DeepBlueSky', project_id=1,
|
|
Pierre-Yves Chibon |
dbea26 |
)
|
|
Pierre-Yves Chibon |
dbea26 |
self.session.add(item)
|
|
Pierre-Yves Chibon |
dbea26 |
self.session.commit()
|
|
Pierre-Yves Chibon |
a8f682 |
item = pagure.lib.model.TagIssueColored(
|
|
Pierre-Yves Chibon |
dbea26 |
issue_uid='foobar',
|
|
Pierre-Yves Chibon |
a8f682 |
tag_id=item.id
|
|
Pierre-Yves Chibon |
dbea26 |
)
|
|
Pierre-Yves Chibon |
dbea26 |
self.session.add(item)
|
|
Pierre-Yves Chibon |
dbea26 |
self.session.commit()
|
|
Pierre-Yves Chibon |
dbea26 |
|
|
Pierre-Yves Chibon |
dbea26 |
output = self.app.get('/api/0/test/tags/')
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(output.status_code, 200)
|
|
Aurélien Bompard |
626417 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
226628 |
self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(data['tags'], ['tag1'])
|
|
Pierre-Yves Chibon |
226628 |
self.assertEqual(data['total_tags'], 1)
|
|
Pierre-Yves Chibon |
dbea26 |
|
|
Pierre-Yves Chibon |
dbea26 |
output = self.app.get('/api/0/test/tags/?pattern=t')
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(output.status_code, 200)
|
|
Aurélien Bompard |
626417 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
226628 |
self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(data['tags'], ['tag1'])
|
|
Pierre-Yves Chibon |
226628 |
self.assertEqual(data['total_tags'], 1)
|
|
Pierre-Yves Chibon |
dbea26 |
|
|
Pierre-Yves Chibon |
dbea26 |
output = self.app.get('/api/0/test/tags/?pattern=p')
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(output.status_code, 200)
|
|
Aurélien Bompard |
626417 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
226628 |
self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
|
|
Pierre-Yves Chibon |
dbea26 |
self.assertEqual(data['tags'], [])
|
|
Pierre-Yves Chibon |
226628 |
self.assertEqual(data['total_tags'], 0)
|
|
Pierre-Yves Chibon |
dbea26 |
|
|
Aurélien Bompard |
626417 |
def test_api_groups(self):
|
|
Aurélien Bompard |
626417 |
""" Test the api_groups function. """
|
|
Aurélien Bompard |
626417 |
|
|
Aurélien Bompard |
626417 |
# Add a couple of groups so that we can list them
|
|
Aurélien Bompard |
626417 |
item = pagure.lib.model.PagureGroup(
|
|
Aurélien Bompard |
626417 |
group_name='group1',
|
|
Aurélien Bompard |
626417 |
group_type='user',
|
|
Aurélien Bompard |
626417 |
display_name='User group',
|
|
Aurélien Bompard |
626417 |
user_id=1, # pingou
|
|
Aurélien Bompard |
626417 |
)
|
|
Aurélien Bompard |
626417 |
self.session.add(item)
|
|
Aurélien Bompard |
626417 |
|
|
Aurélien Bompard |
626417 |
item = pagure.lib.model.PagureGroup(
|
|
Aurélien Bompard |
626417 |
group_name='rel-eng',
|
|
Aurélien Bompard |
626417 |
group_type='user',
|
|
Aurélien Bompard |
626417 |
display_name='Release engineering group',
|
|
Aurélien Bompard |
626417 |
user_id=1, # pingou
|
|
Aurélien Bompard |
626417 |
)
|
|
Aurélien Bompard |
626417 |
self.session.add(item)
|
|
Aurélien Bompard |
626417 |
self.session.commit()
|
|
Aurélien Bompard |
626417 |
|
|
Aurélien Bompard |
626417 |
output = self.app.get('/api/0/groups')
|
|
Aurélien Bompard |
626417 |
self.assertEqual(output.status_code, 200)
|
|
Aurélien Bompard |
626417 |
data = json.loads(output.get_data(as_text=True))
|
|
Aurélien Bompard |
626417 |
self.assertEqual(data['groups'], ['group1', 'rel-eng'])
|
|
Pierre-Yves Chibon |
75809f |
self.assertEqual(
|
|
Pierre-Yves Chibon |
75809f |
sorted(data.keys()),
|
|
Pierre-Yves Chibon |
75809f |
['groups', 'pagination', 'total_groups'])
|
|
Aurélien Bompard |
626417 |
self.assertEqual(data['total_groups'], 2)
|
|
Aurélien Bompard |
626417 |
|
|
Aurélien Bompard |
626417 |
output = self.app.get('/api/0/groups?pattern=re')
|
|
Aurélien Bompard |
626417 |
self.assertEqual(output.status_code, 200)
|
|
Aurélien Bompard |
626417 |
data = json.loads(output.get_data(as_text=True))
|
|
Aurélien Bompard |
626417 |
self.assertEqual(data['groups'], ['rel-eng'])
|
|
Pierre-Yves Chibon |
75809f |
self.assertEqual(
|
|
Pierre-Yves Chibon |
75809f |
sorted(data.keys()),
|
|
Pierre-Yves Chibon |
75809f |
['groups', 'pagination', 'total_groups'])
|
|
Aurélien Bompard |
626417 |
self.assertEqual(data['total_groups'], 1)
|
|
Aurélien Bompard |
626417 |
|
|
Pierre-Yves Chibon |
48ad71 |
def test_api_whoami_unauth(self):
|
|
Pierre-Yves Chibon |
48ad71 |
""" Test the api_whoami function. """
|
|
Pierre-Yves Chibon |
48ad71 |
|
|
Pierre-Yves Chibon |
48ad71 |
output = self.app.post('/api/0/-/whoami')
|
|
Pierre-Yves Chibon |
48ad71 |
self.assertEqual(output.status_code, 401)
|
|
Pierre-Yves Chibon |
48ad71 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
48ad71 |
self.assertEqual(
|
|
Pierre-Yves Chibon |
48ad71 |
data,
|
|
Pierre-Yves Chibon |
48ad71 |
{
|
|
Pierre-Yves Chibon |
48ad71 |
u'error': u'Invalid or expired token. Please visit '
|
|
Pierre-Yves Chibon |
48ad71 |
'http://localhost.localdomain/settings#api-keys to get or '
|
|
Pierre-Yves Chibon |
48ad71 |
'renew your API token.',
|
|
Pierre-Yves Chibon |
48ad71 |
u'error_code': u'EINVALIDTOK'
|
|
Pierre-Yves Chibon |
48ad71 |
}
|
|
Pierre-Yves Chibon |
48ad71 |
)
|
|
Pierre-Yves Chibon |
48ad71 |
|
|
Pierre-Yves Chibon |
48ad71 |
def test_api_whoami_invalid_auth(self):
|
|
Pierre-Yves Chibon |
48ad71 |
""" Test the api_whoami function with an invalid token. """
|
|
Pierre-Yves Chibon |
48ad71 |
tests.create_projects(self.session)
|
|
Pierre-Yves Chibon |
48ad71 |
tests.create_tokens(self.session)
|
|
Pierre-Yves Chibon |
48ad71 |
|
|
Pierre-Yves Chibon |
48ad71 |
headers = {'Authorization': 'token invalid'}
|
|
Pierre-Yves Chibon |
48ad71 |
|
|
Pierre-Yves Chibon |
48ad71 |
output = self.app.post('/api/0/-/whoami', headers=headers)
|
|
Pierre-Yves Chibon |
48ad71 |
self.assertEqual(output.status_code, 401)
|
|
Pierre-Yves Chibon |
48ad71 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
48ad71 |
self.assertEqual(
|
|
Pierre-Yves Chibon |
48ad71 |
data,
|
|
Pierre-Yves Chibon |
48ad71 |
{
|
|
Pierre-Yves Chibon |
48ad71 |
u'error': u'Invalid or expired token. Please visit '
|
|
Pierre-Yves Chibon |
48ad71 |
'http://localhost.localdomain/settings#api-keys to get or '
|
|
Pierre-Yves Chibon |
48ad71 |
'renew your API token.',
|
|
Pierre-Yves Chibon |
48ad71 |
u'error_code': u'EINVALIDTOK'
|
|
Pierre-Yves Chibon |
48ad71 |
}
|
|
Pierre-Yves Chibon |
48ad71 |
)
|
|
Pierre-Yves Chibon |
48ad71 |
|
|
Pierre-Yves Chibon |
48ad71 |
def test_api_whoami_auth(self):
|
|
Pierre-Yves Chibon |
48ad71 |
""" Test the api_whoami function with a valid token. """
|
|
Pierre-Yves Chibon |
48ad71 |
tests.create_projects(self.session)
|
|
Pierre-Yves Chibon |
48ad71 |
tests.create_tokens(self.session)
|
|
Pierre-Yves Chibon |
48ad71 |
|
|
Pierre-Yves Chibon |
48ad71 |
headers = {'Authorization': 'token aaabbbcccddd'}
|
|
Pierre-Yves Chibon |
48ad71 |
|
|
Pierre-Yves Chibon |
48ad71 |
output = self.app.post('/api/0/-/whoami', headers=headers)
|
|
Pierre-Yves Chibon |
48ad71 |
self.assertEqual(output.status_code, 200)
|
|
Pierre-Yves Chibon |
48ad71 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
48ad71 |
self.assertEqual(data, {u'username': u'pingou'})
|
|
Pierre-Yves Chibon |
48ad71 |
|
|
Pierre-Yves Chibon |
437cd0 |
def test_api_error_codes(self):
|
|
Pierre-Yves Chibon |
437cd0 |
""" Test the api_error_codes endpoint. """
|
|
Pierre-Yves Chibon |
437cd0 |
output = self.app.get('/api/0/-/error_codes')
|
|
Pierre-Yves Chibon |
437cd0 |
self.assertEqual(output.status_code, 200)
|
|
Pierre-Yves Chibon |
437cd0 |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
437cd0 |
self.assertEqual(len(data), 32)
|
|
Pierre-Yves Chibon |
437cd0 |
self.assertEqual(
|
|
Pierre-Yves Chibon |
437cd0 |
sorted(data.keys()),
|
|
Pierre-Yves Chibon |
437cd0 |
[
|
|
Pierre-Yves Chibon |
437cd0 |
u'EDATETIME', u'EDBERROR', u'EGITERROR',
|
|
Pierre-Yves Chibon |
437cd0 |
u'EINVALIDISSUEFIELD', u'EINVALIDISSUEFIELD_LINK',
|
|
Pierre-Yves Chibon |
437cd0 |
u'EINVALIDPERPAGEVALUE', u'EINVALIDPRIORITY', u'EINVALIDREQ',
|
|
Pierre-Yves Chibon |
437cd0 |
u'EINVALIDTOK', u'EISSUENOTALLOWED',
|
|
Pierre-Yves Chibon |
437cd0 |
u'EMODIFYPROJECTNOTALLOWED', u'ENEWPROJECTDISABLED',
|
|
Pierre-Yves Chibon |
437cd0 |
u'ENOCODE', u'ENOCOMMENT', u'ENOCOMMIT', u'ENOGROUP',
|
|
Pierre-Yves Chibon |
437cd0 |
u'ENOISSUE', u'ENOPRCLOSE', u'ENOPROJECT', u'ENOPROJECTS',
|
|
Pierre-Yves Chibon |
437cd0 |
u'ENOREQ', u'ENOSIGNEDOFF', u'ENOTASSIGNED', u'ENOTASSIGNEE',
|
|
Pierre-Yves Chibon |
437cd0 |
u'ENOTHIGHENOUGH', u'ENOTMAINADMIN', u'ENOUSER', u'EPRSCORE',
|
|
Pierre-Yves Chibon |
437cd0 |
u'EPULLREQUESTSDISABLED', u'ETIMESTAMP', u'ETRACKERDISABLED',
|
|
Pierre-Yves Chibon |
437cd0 |
u'ETRACKERREADONLY'
|
|
Pierre-Yves Chibon |
437cd0 |
]
|
|
Pierre-Yves Chibon |
437cd0 |
)
|
|
Pierre-Yves Chibon |
437cd0 |
|
|
Pierre-Yves Chibon |
62f012 |
|
|
Pierre-Yves Chibon |
62f012 |
if __name__ == '__main__':
|
|
Pierre-Yves Chibon |
393f31 |
unittest.main(verbosity=2)
|