Blame tests/test_pagure_flask_ui_app_browse.py

Pierre-Yves Chibon 5872c5
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
"""
Pierre-Yves Chibon 5872c5
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
 Authors:
Pierre-Yves Chibon 5872c5
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
"""
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
from __future__ import unicode_literals
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 5872c5
import pkg_resources
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
import unittest
Pierre-Yves Chibon 5872c5
import sys
Pierre-Yves Chibon 5872c5
import os
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
from mock import patch, MagicMock
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 5872c5
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 5872c5
import tests
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
class PagureFlaskAppBrowsetests(tests.Modeltests):
Pierre-Yves Chibon 5872c5
    """ Tests for the browse pages of flask app controller of pagure """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    def setUp(self):
Pierre-Yves Chibon 5872c5
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 5872c5
        super(PagureFlaskAppBrowsetests, self).setUp()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        tests.create_projects(self.session)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add a 3rd project with a long description
Pierre-Yves Chibon 5872c5
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 5872c5
            user_id=2,  # foo
Pierre-Yves Chibon 5872c5
            name='test3',
Pierre-Yves Chibon 5872c5
            description='test project #3 with a very long description',
Pierre-Yves Chibon 5872c5
            hook_token='aaabbbeeefff',
Pierre-Yves Chibon 5872c5
            private=True,
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.add(item)
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_logged_in_private_project(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in with a private
Pierre-Yves Chibon 5872c5
        project. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='foo')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '4',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 1)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_unauth_private_project(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged out with a private
Pierre-Yves Chibon 5872c5
        project. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
        self.assertIn(
Pierre-Yves Chibon 5872c5
            '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
            output_text)
Pierre-Yves Chibon 5872c5
        self.assertIn(
Pierre-Yves Chibon 5872c5
            '

All Projects '

Pierre-Yves Chibon 5872c5
            '3',
Pierre-Yves Chibon 5872c5
            output_text)
Pierre-Yves Chibon 5872c5
        self.assertNotIn(
Pierre-Yves Chibon 837e8c
            '
Pierre-Yves Chibon 5872c5
            'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
            output_text)
Pierre-Yves Chibon 837e8c
        self.assertEqual(output_text.count('title="Private Project"'), 0)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_logged_in_no_access_private_project(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '3',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertNotIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 0)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_logged_in_ticket_private_project(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add user 'pingou' with ticket access on repo
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test3')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            repo,
Pierre-Yves Chibon 5872c5
            new_user='pingou',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            access='ticket',
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'User added')
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Ticket access level isn't sufficient to access private projects
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '3',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertNotIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 0)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_logged_in_commit_private_project(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add user 'pingou' with commit access on repo
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test3')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            repo,
Pierre-Yves Chibon 5872c5
            new_user='pingou',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            access='commit',
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'User added')
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '4',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 1)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_logged_in_admin_private_project(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add user 'pingou' with admin access on repo
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test3')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            repo,
Pierre-Yves Chibon 5872c5
            new_user='pingou',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            access='admin',
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'User added')
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '4',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 1)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
class PagureFlaskAppBrowseGroupAdmintests(tests.Modeltests):
Pierre-Yves Chibon 5872c5
    """ Tests for the browse pages of flask app controller of pagure """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    def setUp(self):
Pierre-Yves Chibon 5872c5
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 5872c5
        super(PagureFlaskAppBrowseGroupAdmintests, self).setUp()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        tests.create_projects(self.session)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add a 3rd project with a long description
Pierre-Yves Chibon 5872c5
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 5872c5
            user_id=2,  # foo
Pierre-Yves Chibon 5872c5
            name='test3',
Pierre-Yves Chibon 5872c5
            description='test project #3 with a very long description',
Pierre-Yves Chibon 5872c5
            hook_token='aaabbbeeefff',
Pierre-Yves Chibon 5872c5
            private=True,
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.add(item)
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Create a group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            group_name='JL',
Pierre-Yves Chibon 5872c5
            display_name='Justice League',
Pierre-Yves Chibon 5872c5
            description='Nope, it\'s not JLA anymore',
Pierre-Yves Chibon 5872c5
            group_type='user',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            is_admin=False,
Pierre-Yves Chibon 5872c5
            blacklist=pagure.config.config.get('BLACKLISTED_PROJECTS')
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'User `foo` added to the group `JL`.')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add the group to project we just created, test3
Pierre-Yves Chibon 5872c5
        # Add it with admin ACL
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(self.session, 'test3')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            project=project,
Pierre-Yves Chibon 5872c5
            new_group='JL',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            access='admin'
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'Group added')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_user_not_in_group(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project via a group as admin. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '3',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertNotIn(
Pierre-Yves Chibon 5872c5
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertEqual(output_text.count('title="Private project"'), 0)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_user_in_group(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project via a group as admin. """
Pierre-Yves Chibon 930073
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 5872c5
            self.session, group_name='JL')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon 5872c5
            session=self.session,
Pierre-Yves Chibon 5872c5
            username='pingou',
Pierre-Yves Chibon 5872c5
            group=group,
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            is_admin=False,
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '4',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 1)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
class PagureFlaskAppBrowseGroupCommittests(tests.Modeltests):
Pierre-Yves Chibon 5872c5
    """ Tests for the browse pages of flask app controller of pagure """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    def setUp(self):
Pierre-Yves Chibon 5872c5
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 5872c5
        super(PagureFlaskAppBrowseGroupCommittests, self).setUp()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        tests.create_projects(self.session)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add a 3rd project with a long description
Pierre-Yves Chibon 5872c5
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 5872c5
            user_id=2,  # foo
Pierre-Yves Chibon 5872c5
            name='test3',
Pierre-Yves Chibon 5872c5
            description='test project #3 with a very long description',
Pierre-Yves Chibon 5872c5
            hook_token='aaabbbeeefff',
Pierre-Yves Chibon 5872c5
            private=True,
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.add(item)
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Create a group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            group_name='JL',
Pierre-Yves Chibon 5872c5
            display_name='Justice League',
Pierre-Yves Chibon 5872c5
            description='Nope, it\'s not JLA anymore',
Pierre-Yves Chibon 5872c5
            group_type='user',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            is_admin=False,
Pierre-Yves Chibon 5872c5
            blacklist=pagure.config.config.get('BLACKLISTED_PROJECTS')
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'User `foo` added to the group `JL`.')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add the group to project we just created, test3
Pierre-Yves Chibon 5872c5
        # Add it with commit ACL
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(self.session, 'test3')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            project=project,
Pierre-Yves Chibon 5872c5
            new_group='JL',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            access='commit'
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'Group added')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_user_not_in_group(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project via a group as admin. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '3',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertNotIn(
Pierre-Yves Chibon 5872c5
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertEqual(output_text.count('title="Private project"'), 0)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_user_in_group(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project via a group as admin. """
Pierre-Yves Chibon 930073
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 5872c5
            self.session, group_name='JL')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon 5872c5
            session=self.session,
Pierre-Yves Chibon 5872c5
            username='pingou',
Pierre-Yves Chibon 5872c5
            group=group,
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            is_admin=False,
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '4',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 1)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
class PagureFlaskAppBrowseGroupTickettests(tests.Modeltests):
Pierre-Yves Chibon 5872c5
    """ Tests for the browse pages of flask app controller of pagure """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    def setUp(self):
Pierre-Yves Chibon 5872c5
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 5872c5
        super(PagureFlaskAppBrowseGroupTickettests, self).setUp()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        tests.create_projects(self.session)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add a 3rd project with a long description
Pierre-Yves Chibon 5872c5
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 5872c5
            user_id=2,  # foo
Pierre-Yves Chibon 5872c5
            name='test3',
Pierre-Yves Chibon 5872c5
            description='test project #3 with a very long description',
Pierre-Yves Chibon 5872c5
            hook_token='aaabbbeeefff',
Pierre-Yves Chibon 5872c5
            private=True,
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.add(item)
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Create a group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            group_name='JL',
Pierre-Yves Chibon 5872c5
            display_name='Justice League',
Pierre-Yves Chibon 5872c5
            description='Nope, it\'s not JLA anymore',
Pierre-Yves Chibon 5872c5
            group_type='user',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            is_admin=False,
Pierre-Yves Chibon 5872c5
            blacklist=pagure.config.config.get('BLACKLISTED_PROJECTS')
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'User `foo` added to the group `JL`.')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Add the group to project we just created, test3
Pierre-Yves Chibon 5872c5
        # Add it with ticket ACL
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(self.session, 'test3')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 5872c5
            self.session,
Pierre-Yves Chibon 5872c5
            project=project,
Pierre-Yves Chibon 5872c5
            new_group='JL',
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            access='ticket'
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
        self.assertEqual(msg, 'Group added')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_user_not_in_group(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project via a group as admin. """
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '3',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertNotIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 0)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon 5872c5
    def test_browse_project_user_in_group(self):
Pierre-Yves Chibon 5872c5
        """ Test the browse project endpoint when logged in as an user that
Pierre-Yves Chibon 5872c5
        has no access to the private project via a group as admin. """
Pierre-Yves Chibon 930073
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 5872c5
            self.session, group_name='JL')
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon 5872c5
            session=self.session,
Pierre-Yves Chibon 5872c5
            username='pingou',
Pierre-Yves Chibon 5872c5
            group=group,
Pierre-Yves Chibon 5872c5
            user='foo',
Pierre-Yves Chibon 5872c5
            is_admin=False,
Pierre-Yves Chibon 5872c5
        )
Pierre-Yves Chibon 5872c5
        self.session.commit()
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
        # Ticket ACL isn't enough to grant you access
Pierre-Yves Chibon 5872c5
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 5872c5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5872c5
            output = self.app.get('/browse/projects/')
Pierre-Yves Chibon 5872c5
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5872c5
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '<title>Home - Pagure</title>\n',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertIn(
Pierre-Yves Chibon 5872c5
                '

All Projects '

Pierre-Yves Chibon 5872c5
                '3',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 5872c5
            self.assertNotIn(
Pierre-Yves Chibon 837e8c
                '
Pierre-Yves Chibon 5872c5
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 5872c5
                output_text)
Pierre-Yves Chibon 837e8c
            self.assertEqual(output_text.count('title="Private Project"'), 0)
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
Pierre-Yves Chibon 5872c5
if __name__ == '__main__':
Pierre-Yves Chibon 5872c5
    unittest.main(verbosity=2)