Blame tests/test_pagure_flask_api_group.py

Matt Prahl 1a51d9
# -*- coding: utf-8 -*-
Matt Prahl 1a51d9
Matt Prahl 1a51d9
"""
Matt Prahl 1a51d9
 (c) 2017 - Copyright Red Hat Inc
Matt Prahl 1a51d9
Matt Prahl 1a51d9
 Authors:
Matt Prahl 1a51d9
   Matt Prahl <mprahl@redhat.com></mprahl@redhat.com>
Matt Prahl 1a51d9
Matt Prahl 1a51d9
"""
Matt Prahl 1a51d9
Matt Prahl 1a51d9
__requires__ = ['SQLAlchemy >= 0.8']
Matt Prahl 1a51d9
import unittest
Matt Prahl 1a51d9
import sys
Matt Prahl 1a51d9
import os
Matt Prahl 1a51d9
import json
Matt Prahl 1a51d9
Matt Prahl 1a51d9
sys.path.insert(0, os.path.join(os.path.dirname(
Matt Prahl 1a51d9
    os.path.abspath(__file__)), '..'))
Matt Prahl 1a51d9
Matt Prahl 1a51d9
import pagure.api
Matt Prahl 1a51d9
import pagure.lib
Matt Prahl 1a51d9
import tests
Matt Prahl 1a51d9
Matt Prahl 1a51d9
Matt Prahl 1a51d9
class PagureFlaskApiGroupTests(tests.Modeltests):
Matt Prahl 1a51d9
    """ Tests for the flask API of pagure for issue """
Matt Prahl 1a51d9
Matt Prahl 1a51d9
    def setUp(self):
Matt Prahl 1a51d9
        """ Set up the environnment, ran before every tests. """
Matt Prahl 1a51d9
        super(PagureFlaskApiGroupTests, self).setUp()
Matt Prahl 1a51d9
Matt Prahl 1a51d9
        pagure.APP.config['TESTING'] = True
Matt Prahl 1a51d9
        pagure.SESSION = self.session
Matt Prahl 1a51d9
        pagure.api.SESSION = self.session
Matt Prahl 1a51d9
        pagure.api.group.SESSION = self.session
Matt Prahl 1a51d9
        pagure.api.user.SESSION = self.session
Matt Prahl 1a51d9
        pagure.lib.SESSION = self.session
Matt Prahl 1a51d9
Matt Prahl 1a51d9
        pagure.APP.config['REQUESTS_FOLDER'] = None
Matt Prahl 1a51d9
Matt Prahl 1a51d9
        msg = pagure.lib.add_group(
Matt Prahl 1a51d9
            self.session,
Matt Prahl 1a51d9
            group_name='some_group',
Matt Prahl 1a51d9
            display_name='Some Group',
Matt Prahl 1a51d9
            description=None,
Matt Prahl 1a51d9
            group_type='bar',
Matt Prahl 1a51d9
            user='pingou',
Matt Prahl 1a51d9
            is_admin=False,
Matt Prahl 1a51d9
            blacklist=[],
Matt Prahl 1a51d9
        )
Matt Prahl 1a51d9
        self.session.commit()
Matt Prahl 1a51d9
Matt Prahl 1a51d9
        self.app = pagure.APP.test_client()
Matt Prahl 1a51d9
Matt Prahl 802ab8
    def test_api_groups(self):
Matt Prahl 802ab8
        """ Test the api_groups function.  """
Matt Prahl 802ab8
Matt Prahl 802ab8
        # Add a couple of groups so that we can list them
Matt Prahl 802ab8
        item = pagure.lib.model.PagureGroup(
Matt Prahl 802ab8
            group_name='group1',
Matt Prahl 802ab8
            group_type='user',
Matt Prahl 802ab8
            display_name='User group',
Matt Prahl 802ab8
            user_id=1,  # pingou
Matt Prahl 802ab8
        )
Matt Prahl 802ab8
        self.session.add(item)
Matt Prahl 802ab8
Matt Prahl 802ab8
        item = pagure.lib.model.PagureGroup(
Matt Prahl 802ab8
            group_name='rel-eng',
Matt Prahl 802ab8
            group_type='user',
Matt Prahl 802ab8
            display_name='Release engineering group',
Matt Prahl 802ab8
            user_id=1,  # pingou
Matt Prahl 802ab8
        )
Matt Prahl 802ab8
        self.session.add(item)
Matt Prahl 802ab8
        self.session.commit()
Matt Prahl 802ab8
Matt Prahl 802ab8
        output = self.app.get('/api/0/groups')
Matt Prahl 802ab8
        self.assertEqual(output.status_code, 200)
Matt Prahl 802ab8
        data = json.loads(output.data)
Matt Prahl 802ab8
        self.assertEqual(data['groups'], ['some_group', 'group1', 'rel-eng'])
Matt Prahl 802ab8
        self.assertEqual(sorted(data.keys()), ['groups', 'total_groups'])
Matt Prahl 802ab8
        self.assertEqual(data['total_groups'], 3)
Matt Prahl 802ab8
Matt Prahl 802ab8
        output = self.app.get('/api/0/groups?pattern=re')
Matt Prahl 802ab8
        self.assertEqual(output.status_code, 200)
Matt Prahl 802ab8
        data = json.loads(output.data)
Matt Prahl 802ab8
        self.assertEqual(data['groups'], ['rel-eng'])
Matt Prahl 802ab8
        self.assertEqual(sorted(data.keys()), ['groups', 'total_groups'])
Matt Prahl 802ab8
        self.assertEqual(data['total_groups'], 1)
Matt Prahl 802ab8
Matt Prahl 9264b1
    def test_api_view_group_authenticated(self):
Matt Prahl 1a51d9
        """
Matt Prahl 9264b1
            Test the api_view_group method of the flask api with an
Matt Prahl 9264b1
            authenticated user. The tested group has one member.
Matt Prahl 1a51d9
        """
Pierre-Yves Chibon 14e826
        tests.create_projects(self.session)
Matt Prahl 9264b1
        tests.create_tokens(self.session)
Matt Prahl 9264b1
        headers = {'Authorization': 'token aaabbbcccddd'}
Matt Prahl 9264b1
        output = self.app.get('/api/0/group/some_group', headers=headers)
Matt Prahl 1a51d9
        self.assertEqual(output.status_code, 200)
Matt Prahl 1a51d9
        exp = {
Matt Prahl 1a51d9
            "display_name": "Some Group",
Matt Prahl 1a51d9
            "description": None,
Matt Prahl 1a51d9
            "creator": {
Matt Prahl 1a51d9
                "fullname": "PY C",
Matt Prahl 1a51d9
                "default_email": "bar@pingou.com",
Matt Prahl 1a51d9
                "emails": [
Matt Prahl 1a51d9
                    "bar@pingou.com",
Matt Prahl 1a51d9
                    "foo@pingou.com"
Matt Prahl 1a51d9
                ],
Matt Prahl 1a51d9
                "name": "pingou"
Matt Prahl 1a51d9
            },
Matt Prahl 1a51d9
            "members": ["pingou"],
Matt Prahl 1a51d9
            "date_created": "1492020239",
Matt Prahl 1a51d9
            "group_type": "user",
Matt Prahl 1a51d9
            "name": "some_group"
Matt Prahl 1a51d9
        }
Matt Prahl 1a51d9
        data = json.loads(output.data)
Matt Prahl 1a51d9
        data['date_created'] = '1492020239'
Matt Prahl 1a51d9
        self.assertDictEqual(data, exp)
Matt Prahl 1a51d9
Matt Prahl 9264b1
    def test_api_view_group_unauthenticated(self):
Matt Prahl 1a51d9
        """
Matt Prahl 9264b1
            Test the api_view_group method of the flask api with an
Matt Prahl 9264b1
            unauthenticated user. The tested group has one member.
Matt Prahl 9264b1
        """
Matt Prahl 9264b1
        output = self.app.get('/api/0/group/some_group')
Matt Prahl 9264b1
        self.assertEqual(output.status_code, 200)
Matt Prahl 9264b1
        exp = {
Matt Prahl 9264b1
            "display_name": "Some Group",
Matt Prahl 9264b1
            "description": None,
Matt Prahl 9264b1
            "creator": {
Matt Prahl 9264b1
                "fullname": "PY C",
Matt Prahl 9264b1
                "name": "pingou"
Matt Prahl 9264b1
            },
Matt Prahl 9264b1
            "members": ["pingou"],
Matt Prahl 9264b1
            "date_created": "1492020239",
Matt Prahl 9264b1
            "group_type": "user",
Matt Prahl 9264b1
            "name": "some_group"
Matt Prahl 9264b1
        }
Matt Prahl 9264b1
        data = json.loads(output.data)
Matt Prahl 9264b1
        data['date_created'] = '1492020239'
Matt Prahl 9264b1
        self.assertDictEqual(data, exp)
Matt Prahl 9264b1
Matt Prahl 9264b1
    def test_api_view_group_two_members_authenticated(self):
Matt Prahl 9264b1
        """
Matt Prahl 9264b1
            Test the api_view_group method of the flask api with an
Matt Prahl 9264b1
            authenticated user. The tested group has two members.
Matt Prahl 1a51d9
        """
Matt Prahl 1a51d9
        user = pagure.lib.model.User(
Matt Prahl 1a51d9
            user='mprahl',
Matt Prahl 1a51d9
            fullname='Matt Prahl',
Matt Prahl 1a51d9
            password='foo',
Matt Prahl 1a51d9
            default_email='mprahl@redhat.com',
Matt Prahl 1a51d9
        )
Matt Prahl 1a51d9
        self.session.add(user)
Matt Prahl 1a51d9
        self.session.commit()
Matt Prahl 1a51d9
        group = pagure.lib.search_groups(self.session, group_name='some_group')
Matt Prahl 1a51d9
        result = pagure.lib.add_user_to_group(
Matt Prahl 1a51d9
            self.session, user.username, group, user.username, True)
Matt Prahl 1a51d9
        self.session.commit()
Matt Prahl 9264b1
Pierre-Yves Chibon 14e826
        tests.create_projects(self.session)
Matt Prahl 9264b1
        tests.create_tokens(self.session)
Pierre-Yves Chibon 14e826
Matt Prahl 9264b1
        headers = {'Authorization': 'token aaabbbcccddd'}
Matt Prahl 9264b1
        output = self.app.get('/api/0/group/some_group', headers=headers)
Matt Prahl 1a51d9
        self.assertEqual(output.status_code, 200)
Matt Prahl 1a51d9
        exp = {
Matt Prahl 1a51d9
            "display_name": "Some Group",
Matt Prahl 1a51d9
            "description": None,
Matt Prahl 1a51d9
            "creator": {
Matt Prahl 1a51d9
                "fullname": "PY C",
Matt Prahl 1a51d9
                "default_email": "bar@pingou.com",
Matt Prahl 1a51d9
                "emails": [
Matt Prahl 1a51d9
                    "bar@pingou.com",
Matt Prahl 1a51d9
                    "foo@pingou.com"
Matt Prahl 1a51d9
                ],
Matt Prahl 1a51d9
                "name": "pingou"
Matt Prahl 1a51d9
            },
Matt Prahl 1a51d9
            "members": ["pingou", "mprahl"],
Matt Prahl 1a51d9
            "date_created": "1492020239",
Matt Prahl 1a51d9
            "group_type": "user",
Matt Prahl 1a51d9
            "name": "some_group"
Matt Prahl 1a51d9
        }
Matt Prahl 1a51d9
        self.maxDiff = None
Matt Prahl 1a51d9
        data = json.loads(output.data)
Matt Prahl 1a51d9
        data['date_created'] = '1492020239'
Matt Prahl 1a51d9
        from pprint import pprint
Matt Prahl 1a51d9
        pprint(data)
Matt Prahl 1a51d9
        self.assertDictEqual(data, exp)
Matt Prahl 1a51d9
Matt Prahl 1a51d9
    def test_api_view_group_no_group_error(self):
Matt Prahl 1a51d9
        """
Matt Prahl 1a51d9
            Test the api_view_group method of the flask api
Matt Prahl 1a51d9
            The tested group has one member.
Matt Prahl 1a51d9
        """
Matt Prahl 1a51d9
        output = self.app.get("/api/0/group/some_group3")
Matt Prahl 1a51d9
        self.assertEqual(output.status_code, 404)
Matt Prahl 1a51d9
        data = json.loads(output.data)
Matt Prahl 1a51d9
        self.assertEqual(data['error'], 'Group not found')
Matt Prahl 1a51d9
        self.assertEqual(data['error_code'], 'ENOGROUP')
Matt Prahl 1a51d9
Matt Prahl 1a51d9
if __name__ == "__main__":
Matt Prahl 1a51d9
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Matt Prahl 1a51d9
        PagureFlaskApiGroupTests)
Matt Prahl 1a51d9
    unittest.TextTestRunner(verbosity=2).run(SUITE)