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
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
Pierre-Yves Chibon fe5017
import pagure.lib
Pierre-Yves Chibon 62f012
import tests
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon fe5017
class PagureFlaskApitests(tests.Modeltests):
Pierre-Yves Chibon fe5017
    """ Tests for flask API controller of pagure """
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
    def setUp(self):
Pierre-Yves Chibon 62f012
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon fe5017
        super(PagureFlaskApitests, self).setUp()
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon fe5017
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon fe5017
        pagure.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.api.SESSION = self.session
Pierre-Yves Chibon fe5017
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon 62f012
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)
Pierre-Yves Chibon 62f012
        data = json.loads(output.data)
Pierre-Yves Chibon fe5017
        self.assertEqual(data['version'], pagure.__api_version__)
Pierre-Yves Chibon 62f012
        self.assertEqual(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)
Pierre-Yves Chibon dbea26
        data = json.loads(output.data)
Pierre-Yves Chibon dbea26
        self.assertEqual(data.keys(), ['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)
Pierre-Yves Chibon dbea26
        data = json.loads(output.data)
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 fe5017
        item = pagure.lib.model.Tag(
Pierre-Yves Chibon dbea26
            tag='tag1',
Pierre-Yves Chibon dbea26
        )
Pierre-Yves Chibon dbea26
        self.session.add(item)
Pierre-Yves Chibon dbea26
        self.session.commit()
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.TagIssue(
Pierre-Yves Chibon dbea26
            tag='tag1',
Pierre-Yves Chibon dbea26
            issue_uid='foobar',
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)
Pierre-Yves Chibon dbea26
        data = json.loads(output.data)
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)
Pierre-Yves Chibon dbea26
        data = json.loads(output.data)
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)
Pierre-Yves Chibon dbea26
        data = json.loads(output.data)
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 b4ef8a
    def test_api_groups(self):
Pierre-Yves Chibon b4ef8a
        """ Test the api_groups function.  """
Pierre-Yves Chibon b4ef8a
Pierre-Yves Chibon b4ef8a
        # Add a couple of groups so that we can list them
Pierre-Yves Chibon b4ef8a
        item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon b4ef8a
            group_name='group1',
Pierre-Yves Chibon b4ef8a
            group_type='user',
Pierre-Yves Chibon ce6666
            display_name='User group',
Pierre-Yves Chibon b4ef8a
            user_id=1,  # pingou
Pierre-Yves Chibon b4ef8a
        )
Pierre-Yves Chibon b4ef8a
        self.session.add(item)
Pierre-Yves Chibon b4ef8a
Pierre-Yves Chibon b4ef8a
        item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon b4ef8a
            group_name='rel-eng',
Pierre-Yves Chibon b4ef8a
            group_type='user',
Pierre-Yves Chibon ce6666
            display_name='Release engineering group',
Pierre-Yves Chibon b4ef8a
            user_id=1,  # pingou
Pierre-Yves Chibon b4ef8a
        )
Pierre-Yves Chibon b4ef8a
        self.session.add(item)
Pierre-Yves Chibon b4ef8a
        self.session.commit()
Pierre-Yves Chibon b4ef8a
Pierre-Yves Chibon b4ef8a
        output = self.app.get('/api/0/groups')
Pierre-Yves Chibon b4ef8a
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b4ef8a
        data = json.loads(output.data)
Pierre-Yves Chibon b4ef8a
        self.assertEqual(data['groups'], ['group1', 'rel-eng'])
Pierre-Yves Chibon 226628
        self.assertEqual(sorted(data.keys()), ['groups', 'total_groups'])
Pierre-Yves Chibon 226628
        self.assertEqual(data['total_groups'], 2)
Pierre-Yves Chibon b4ef8a
Pierre-Yves Chibon b4ef8a
        output = self.app.get('/api/0/groups?pattern=re')
Pierre-Yves Chibon b4ef8a
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b4ef8a
        data = json.loads(output.data)
Pierre-Yves Chibon b4ef8a
        self.assertEqual(data['groups'], ['rel-eng'])
Pierre-Yves Chibon 226628
        self.assertEqual(sorted(data.keys()), ['groups', 'total_groups'])
Pierre-Yves Chibon 226628
        self.assertEqual(data['total_groups'], 1)
Pierre-Yves Chibon b4ef8a
Pierre-Yves Chibon 62f012
Pierre-Yves Chibon 62f012
if __name__ == '__main__':
Pierre-Yves Chibon fe5017
    SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskApitests)
Pierre-Yves Chibon 62f012
    unittest.TextTestRunner(verbosity=2).run(SUITE)