Blame tests/test_pagure_flask_api.py

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
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 62f012
    def test_api_version(self):
Pierre-Yves Chibon 62f012
        """ Test the api_version function.  """
Pierre-Yves Chibon 62f012
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 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'])
Aurélien Bompard 626417
        self.assertEqual(sorted(data.keys()), ['groups', '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'])
Aurélien Bompard 626417
        self.assertEqual(sorted(data.keys()), ['groups', 'total_groups'])
Aurélien Bompard 626417
        self.assertEqual(data['total_groups'], 1)
Aurélien Bompard 626417
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)