Blame tests/test_pagure_flask_api_group.py

Matt Prahl 1a51d9
# -*- coding: utf-8 -*-
Matt Prahl 1a51d9
Matt Prahl 1a51d9
"""
Pierre-Yves Chibon baaf54
 (c) 2017-2018 - Copyright Red Hat Inc
Matt Prahl 1a51d9
Matt Prahl 1a51d9
 Authors:
Matt Prahl 1a51d9
   Matt Prahl <mprahl@redhat.com></mprahl@redhat.com>
Pierre-Yves Chibon baaf54
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Matt Prahl 1a51d9
Matt Prahl 1a51d9
"""
Matt Prahl 1a51d9
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Matt Prahl 1a51d9
import unittest
Matt Prahl 1a51d9
import sys
Matt Prahl 1a51d9
import os
Matt Prahl 1a51d9
import json
Matt Prahl 1a51d9
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Matt Prahl 1a51d9
Matt Prahl 1a51d9
import pagure.api
Pierre-Yves Chibon 930073
import pagure.lib.query
Matt Prahl 1a51d9
import tests
Matt Prahl 1a51d9
Matt Prahl 1a51d9
Clement Verna 109c4b
class PagureFlaskApiGroupTests(tests.SimplePagureTest):
Matt Prahl 1a51d9
    """ Tests for the flask API of pagure for issue """
Matt Prahl 1a51d9
Pierre-Yves Chibon 6fa1ec
    maxDiff = None
Pierre-Yves Chibon 6fa1ec
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
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Matt Prahl 1a51d9
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Matt Prahl 1a51d9
            self.session,
Pierre-Yves Chibon 73d120
            group_name="some_group",
Pierre-Yves Chibon 73d120
            display_name="Some Group",
Matt Prahl 1a51d9
            description=None,
Pierre-Yves Chibon 73d120
            group_type="bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Matt Prahl 1a51d9
            is_admin=False,
Matt Prahl 1a51d9
            blacklist=[],
Matt Prahl 1a51d9
        )
Matt Prahl 1a51d9
        self.session.commit()
Matt Prahl 1a51d9
Pierre-Yves Chibon 6fa1ec
        tests.create_projects(self.session)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test2")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 6fa1ec
            session=self.session,
Pierre-Yves Chibon 6fa1ec
            project=project,
Pierre-Yves Chibon 73d120
            new_group="some_group",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 6fa1ec
        )
Pierre-Yves Chibon 6fa1ec
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Group added")
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(
Pierre-Yves Chibon 73d120
            group_name="group1",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            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(
Pierre-Yves Chibon 73d120
            group_name="rel-eng",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            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
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/groups")
Matt Prahl 802ab8
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["groups"], ["some_group", "group1", "rel-eng"])
Pierre-Yves Chibon 75809f
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(data.keys()), ["groups", "pagination", "total_groups"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_groups"], 3)
Matt Prahl 802ab8
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/groups?pattern=re")
Matt Prahl 802ab8
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["groups"], ["rel-eng"])
Pierre-Yves Chibon 75809f
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(data.keys()), ["groups", "pagination", "total_groups"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_groups"], 1)
Matt Prahl 802ab8
Pierre-Yves Chibon 2231a1
    def test_api_groups_extended(self):
Pierre-Yves Chibon 2231a1
        """ Test the api_groups function.  """
Pierre-Yves Chibon 2231a1
Pierre-Yves Chibon 2231a1
        # Add a couple of groups so that we can list them
Pierre-Yves Chibon 2231a1
        item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
            group_name="group1",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            display_name="User group",
Pierre-Yves Chibon 2231a1
            user_id=1,  # pingou
Pierre-Yves Chibon 2231a1
        )
Pierre-Yves Chibon 2231a1
        self.session.add(item)
Pierre-Yves Chibon 2231a1
Pierre-Yves Chibon 2231a1
        item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
            group_name="rel-eng",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            display_name="Release engineering group",
Pierre-Yves Chibon 2231a1
            user_id=1,  # pingou
Pierre-Yves Chibon 2231a1
        )
Pierre-Yves Chibon 2231a1
        self.session.add(item)
Pierre-Yves Chibon 2231a1
        self.session.commit()
Pierre-Yves Chibon 2231a1
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/groups?extended=1")
Pierre-Yves Chibon 2231a1
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
            self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
            data["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon 2231a1
        self.assertEqual(
Pierre-Yves Chibon 2231a1
            data,
Pierre-Yves Chibon 2231a1
            {
Pierre-Yves Chibon 6fa1ec
                "groups": [
Pierre-Yves Chibon 73d120
                    {"description": None, "name": "some_group"},
Pierre-Yves Chibon 73d120
                    {"description": None, "name": "group1"},
Pierre-Yves Chibon 73d120
                    {"description": None, "name": "rel-eng"},
Pierre-Yves Chibon 6fa1ec
                ],
Pierre-Yves Chibon 73d120
                "pagination": {
Pierre-Yves Chibon 73d120
                    "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "next": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "pages": 1,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "prev": None,
Pierre-Yves Chibon 75809f
                },
Pierre-Yves Chibon 73d120
                "total_groups": 3,
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 2231a1
        )
Pierre-Yves Chibon 2231a1
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
        """
Matt Prahl 9264b1
        tests.create_tokens(self.session)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        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",
Pierre-Yves Chibon 73d120
                "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                "name": "pingou",
Matt Prahl 1a51d9
            },
Matt Prahl 1a51d9
            "members": ["pingou"],
Matt Prahl 1a51d9
            "date_created": "1492020239",
Matt Prahl 1a51d9
            "group_type": "user",
Pierre-Yves Chibon 73d120
            "name": "some_group",
Matt Prahl 1a51d9
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        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
        """
Pierre-Yves Chibon 73d120
        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,
Pierre-Yves Chibon 73d120
            "creator": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 9264b1
            "members": ["pingou"],
Matt Prahl 9264b1
            "date_created": "1492020239",
Matt Prahl 9264b1
            "group_type": "user",
Pierre-Yves Chibon 73d120
            "name": "some_group",
Matt Prahl 9264b1
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        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(
Pierre-Yves Chibon 73d120
            user="mprahl",
Pierre-Yves Chibon 73d120
            fullname="Matt Prahl",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="mprahl@redhat.com",
Matt Prahl 1a51d9
        )
Matt Prahl 1a51d9
        self.session.add(user)
Matt Prahl 1a51d9
        self.session.commit()
Pierre-Yves Chibon 73d120
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
            self.session, group_name="some_group"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        result = pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon 73d120
            self.session, user.username, group, user.username, True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6fa1ec
        self.assertEqual(
Pierre-Yves Chibon 73d120
            result, "User `mprahl` added to the group `some_group`."
Pierre-Yves Chibon 73d120
        )
Matt Prahl 1a51d9
        self.session.commit()
Matt Prahl 9264b1
Matt Prahl 9264b1
        tests.create_tokens(self.session)
Pierre-Yves Chibon 14e826
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        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",
Pierre-Yves Chibon 73d120
                "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                "name": "pingou",
Matt Prahl 1a51d9
            },
Matt Prahl 1a51d9
            "members": ["pingou", "mprahl"],
Matt Prahl 1a51d9
            "date_created": "1492020239",
Matt Prahl 1a51d9
            "group_type": "user",
Pierre-Yves Chibon 73d120
            "name": "some_group",
Matt Prahl 1a51d9
        }
Matt Prahl 1a51d9
        self.maxDiff = None
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1492020239"
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)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data["error"], "Group not found")
Pierre-Yves Chibon 73d120
        self.assertEqual(data["error_code"], "ENOGROUP")
Matt Prahl 1a51d9
Pierre-Yves Chibon 6fa1ec
    def test_api_view_group_w_projects_and_acl(self):
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
            Test the api_view_group method with project info and restricted
Pierre-Yves Chibon 6fa1ec
            to the admin ACL
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
        tests.create_tokens(self.session)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 6fa1ec
        output = self.app.get(
Pierre-Yves Chibon 73d120
            "/api/0/group/some_group?projects=1", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 6fa1ec
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6fa1ec
        exp = {
Pierre-Yves Chibon 6fa1ec
            "display_name": "Some Group",
Pierre-Yves Chibon 6fa1ec
            "description": None,
Pierre-Yves Chibon 6fa1ec
            "creator": {
Pierre-Yves Chibon 6fa1ec
                "fullname": "PY C",
Pierre-Yves Chibon 6fa1ec
                "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                "name": "pingou",
Pierre-Yves Chibon 6fa1ec
            },
Pierre-Yves Chibon 6fa1ec
            "members": ["pingou"],
Pierre-Yves Chibon 6fa1ec
            "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
            "group_type": "user",
Pierre-Yves Chibon 6fa1ec
            "name": "some_group",
Pierre-Yves Chibon 6fa1ec
            "projects": [
Pierre-Yves Chibon 6fa1ec
                {
Pierre-Yves Chibon 6fa1ec
                    "access_groups": {
Pierre-Yves Chibon 73d120
                        "admin": ["some_group"],
Pierre-Yves Chibon 6fa1ec
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 6fa1ec
                    },
Pierre-Yves Chibon 6fa1ec
                    "access_users": {
Pierre-Yves Chibon 6fa1ec
                        "admin": [],
Pierre-Yves Chibon 6fa1ec
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 6fa1ec
                    },
Pierre-Yves Chibon 6fa1ec
                    "close_status": [
Pierre-Yves Chibon 6fa1ec
                        "Invalid",
Pierre-Yves Chibon 6fa1ec
                        "Insufficient data",
Pierre-Yves Chibon 6fa1ec
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Pierre-Yves Chibon 6fa1ec
                    ],
Pierre-Yves Chibon 6fa1ec
                    "custom_keys": [],
Pierre-Yves Chibon 6fa1ec
                    "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
                    "date_modified": "1492020239",
Pierre-Yves Chibon 6fa1ec
                    "description": "test project #2",
Pierre-Yves Chibon 6fa1ec
                    "fullname": "test2",
Pierre-Yves Chibon 6fa1ec
                    "id": 2,
Pierre-Yves Chibon 6fa1ec
                    "milestones": {},
Pierre-Yves Chibon 6fa1ec
                    "name": "test2",
Pierre-Yves Chibon 6fa1ec
                    "namespace": None,
Pierre-Yves Chibon 6fa1ec
                    "parent": None,
Pierre-Yves Chibon 6fa1ec
                    "priorities": {},
Pierre-Yves Chibon 6fa1ec
                    "tags": [],
Pierre-Yves Chibon 6fa1ec
                    "url_path": "test2",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6fa1ec
                }
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 6fa1ec
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
        projects = []
Pierre-Yves Chibon 73d120
        for p in data["projects"]:
Pierre-Yves Chibon 73d120
            p["date_created"] = "1492020239"
Pierre-Yves Chibon 73d120
            p["date_modified"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
            projects.append(p)
Pierre-Yves Chibon 73d120
        data["projects"] = projects
Pierre-Yves Chibon 6fa1ec
        self.assertDictEqual(data, exp)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
        output2 = self.app.get(
Pierre-Yves Chibon 73d120
            "/api/0/group/some_group?projects=1&acl=admin", headers=headers
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 626417
        self.assertListEqual(
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True).split("\n"),
Pierre-Yves Chibon 73d120
            output2.get_data(as_text=True).split("\n"),
Aurélien Bompard 626417
        )
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    def test_api_view_group_w_projects_and_acl_commit(self):
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
            Test the api_view_group method with project info and restricted
Pierre-Yves Chibon 6fa1ec
            to the commit ACL
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
Pierre-Yves Chibon 6fa1ec
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6fa1ec
        exp = {
Pierre-Yves Chibon 6fa1ec
            "display_name": "Some Group",
Pierre-Yves Chibon 6fa1ec
            "description": None,
Pierre-Yves Chibon 73d120
            "creator": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6fa1ec
            "members": ["pingou"],
Pierre-Yves Chibon 6fa1ec
            "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
            "group_type": "user",
Pierre-Yves Chibon 6fa1ec
            "name": "some_group",
Pierre-Yves Chibon 6fa1ec
            "projects": [
Pierre-Yves Chibon 6fa1ec
                {
Pierre-Yves Chibon 6fa1ec
                    "access_groups": {
Pierre-Yves Chibon 73d120
                        "admin": ["some_group"],
Pierre-Yves Chibon 6fa1ec
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 6fa1ec
                    },
Pierre-Yves Chibon 6fa1ec
                    "access_users": {
Pierre-Yves Chibon 6fa1ec
                        "admin": [],
Pierre-Yves Chibon 6fa1ec
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 6fa1ec
                    },
Pierre-Yves Chibon 6fa1ec
                    "close_status": [
Pierre-Yves Chibon 6fa1ec
                        "Invalid",
Pierre-Yves Chibon 6fa1ec
                        "Insufficient data",
Pierre-Yves Chibon 6fa1ec
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Pierre-Yves Chibon 6fa1ec
                    ],
Pierre-Yves Chibon 6fa1ec
                    "custom_keys": [],
Pierre-Yves Chibon 6fa1ec
                    "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
                    "date_modified": "1492020239",
Pierre-Yves Chibon 6fa1ec
                    "description": "test project #2",
Pierre-Yves Chibon 6fa1ec
                    "fullname": "test2",
Pierre-Yves Chibon 6fa1ec
                    "id": 2,
Pierre-Yves Chibon 6fa1ec
                    "milestones": {},
Pierre-Yves Chibon 6fa1ec
                    "name": "test2",
Pierre-Yves Chibon 6fa1ec
                    "namespace": None,
Pierre-Yves Chibon 6fa1ec
                    "parent": None,
Pierre-Yves Chibon 6fa1ec
                    "priorities": {},
Pierre-Yves Chibon 6fa1ec
                    "tags": [],
Pierre-Yves Chibon 6fa1ec
                    "url_path": "test2",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6fa1ec
                }
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 6fa1ec
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
        projects = []
Pierre-Yves Chibon 73d120
        for p in data["projects"]:
Pierre-Yves Chibon 73d120
            p["date_created"] = "1492020239"
Pierre-Yves Chibon 73d120
            p["date_modified"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
            projects.append(p)
Pierre-Yves Chibon 73d120
        data["projects"] = projects
Pierre-Yves Chibon 6fa1ec
        self.assertDictEqual(data, exp)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    def test_api_view_group_w_projects_and_acl_ticket(self):
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
            Test the api_view_group method with project info and restricted
Pierre-Yves Chibon 6fa1ec
            to the ticket ACL
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/group/some_group?projects=1&acl=ticket")
Pierre-Yves Chibon 6fa1ec
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6fa1ec
        exp = {
Pierre-Yves Chibon 6fa1ec
            "display_name": "Some Group",
Pierre-Yves Chibon 6fa1ec
            "description": None,
Pierre-Yves Chibon 73d120
            "creator": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6fa1ec
            "members": ["pingou"],
Pierre-Yves Chibon 6fa1ec
            "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
            "group_type": "user",
Pierre-Yves Chibon 6fa1ec
            "name": "some_group",
Pierre-Yves Chibon 6fa1ec
            "projects": [
Pierre-Yves Chibon 6fa1ec
                {
Pierre-Yves Chibon 6fa1ec
                    "access_groups": {
Pierre-Yves Chibon 73d120
                        "admin": ["some_group"],
Pierre-Yves Chibon 6fa1ec
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 6fa1ec
                    },
Pierre-Yves Chibon 6fa1ec
                    "access_users": {
Pierre-Yves Chibon 6fa1ec
                        "admin": [],
Pierre-Yves Chibon 6fa1ec
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 6fa1ec
                    },
Pierre-Yves Chibon 6fa1ec
                    "close_status": [
Pierre-Yves Chibon 6fa1ec
                        "Invalid",
Pierre-Yves Chibon 6fa1ec
                        "Insufficient data",
Pierre-Yves Chibon 6fa1ec
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Pierre-Yves Chibon 6fa1ec
                    ],
Pierre-Yves Chibon 6fa1ec
                    "custom_keys": [],
Pierre-Yves Chibon 6fa1ec
                    "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
                    "date_modified": "1492020239",
Pierre-Yves Chibon 6fa1ec
                    "description": "test project #2",
Pierre-Yves Chibon 6fa1ec
                    "fullname": "test2",
Pierre-Yves Chibon 6fa1ec
                    "id": 2,
Pierre-Yves Chibon 6fa1ec
                    "milestones": {},
Pierre-Yves Chibon 6fa1ec
                    "name": "test2",
Pierre-Yves Chibon 6fa1ec
                    "namespace": None,
Pierre-Yves Chibon 6fa1ec
                    "parent": None,
Pierre-Yves Chibon 6fa1ec
                    "priorities": {},
Pierre-Yves Chibon 6fa1ec
                    "tags": [],
Pierre-Yves Chibon 6fa1ec
                    "url_path": "test2",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6fa1ec
                }
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 6fa1ec
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
        projects = []
Pierre-Yves Chibon 73d120
        for p in data["projects"]:
Pierre-Yves Chibon 73d120
            p["date_created"] = "1492020239"
Pierre-Yves Chibon 73d120
            p["date_modified"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
            projects.append(p)
Pierre-Yves Chibon 73d120
        data["projects"] = projects
Pierre-Yves Chibon 6fa1ec
        self.assertDictEqual(data, exp)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    def test_api_view_group_w_projects_and_acl_admin_no_project(self):
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
            Test the api_view_group method with project info and restricted
Pierre-Yves Chibon 6fa1ec
            to the admin ACL
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
        # Make the group having only commit access
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test2")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 6fa1ec
            session=self.session,
Pierre-Yves Chibon 6fa1ec
            project=project,
Pierre-Yves Chibon 73d120
            new_group="some_group",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Pierre-Yves Chibon 6fa1ec
        )
Pierre-Yves Chibon 6fa1ec
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Group access updated")
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/group/some_group?projects=1&acl=admin")
Pierre-Yves Chibon 6fa1ec
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6fa1ec
        exp = {
Pierre-Yves Chibon 6fa1ec
            "display_name": "Some Group",
Pierre-Yves Chibon 6fa1ec
            "description": None,
Pierre-Yves Chibon 73d120
            "creator": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6fa1ec
            "members": ["pingou"],
Pierre-Yves Chibon 6fa1ec
            "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
            "group_type": "user",
Pierre-Yves Chibon 6fa1ec
            "name": "some_group",
Pierre-Yves Chibon 73d120
            "projects": [],
Pierre-Yves Chibon 6fa1ec
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
        self.assertDictEqual(data, exp)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    def test_api_view_group_w_projects_and_acl_commit_no_project(self):
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
            Test the api_view_group method with project info and restricted
Pierre-Yves Chibon 6fa1ec
            to the commit ACL
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
        # Make the group having only ticket access
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test2")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 6fa1ec
            session=self.session,
Pierre-Yves Chibon 6fa1ec
            project=project,
Pierre-Yves Chibon 73d120
            new_group="some_group",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="ticket",
Pierre-Yves Chibon 6fa1ec
        )
Pierre-Yves Chibon 6fa1ec
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Group access updated")
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
Pierre-Yves Chibon 6fa1ec
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6fa1ec
        exp = {
Pierre-Yves Chibon 6fa1ec
            "display_name": "Some Group",
Pierre-Yves Chibon 6fa1ec
            "description": None,
Pierre-Yves Chibon 73d120
            "creator": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6fa1ec
            "members": ["pingou"],
Pierre-Yves Chibon 6fa1ec
            "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
            "group_type": "user",
Pierre-Yves Chibon 6fa1ec
            "name": "some_group",
Pierre-Yves Chibon 73d120
            "projects": [],
Pierre-Yves Chibon 6fa1ec
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
        self.assertDictEqual(data, exp)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
    def test_api_view_group_w_projects_and_acl_ticket_no_project(self):
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
            Test the api_view_group method with project info and restricted
Pierre-Yves Chibon 6fa1ec
            to the ticket ACL
Pierre-Yves Chibon 6fa1ec
        """
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
        # Create a group not linked to any project
Pierre-Yves Chibon 6fa1ec
        item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
            group_name="rel-eng",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            display_name="Release engineering group",
Pierre-Yves Chibon 6fa1ec
            user_id=1,  # pingou
Pierre-Yves Chibon 6fa1ec
        )
Pierre-Yves Chibon 6fa1ec
        self.session.add(item)
Pierre-Yves Chibon 6fa1ec
        self.session.commit()
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/group/rel-eng?projects=1&acl=ticket")
Pierre-Yves Chibon 6fa1ec
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6fa1ec
        exp = {
Pierre-Yves Chibon 6fa1ec
            "display_name": "Release engineering group",
Pierre-Yves Chibon 6fa1ec
            "description": None,
Pierre-Yves Chibon 73d120
            "creator": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 6fa1ec
            "members": [],
Pierre-Yves Chibon 6fa1ec
            "date_created": "1492020239",
Pierre-Yves Chibon 6fa1ec
            "group_type": "user",
Pierre-Yves Chibon 6fa1ec
            "name": "rel-eng",
Pierre-Yves Chibon 73d120
            "projects": [],
Pierre-Yves Chibon 6fa1ec
        }
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1492020239"
Pierre-Yves Chibon 6fa1ec
        self.assertDictEqual(data, exp)
Pierre-Yves Chibon 6fa1ec
Pierre-Yves Chibon 6fa1ec
Matt Prahl 1a51d9
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)