Blame tests/test_pagure_flask_ui_app_userdash.py

Ryan Lerch 336cd6
# -*- coding: utf-8 -*-
Ryan Lerch 336cd6
Ryan Lerch 336cd6
"""
Ryan Lerch 336cd6
 (c) 2018 - Copyright Red Hat Inc
Ryan Lerch 336cd6
Ryan Lerch 336cd6
 Authors:
Ryan Lerch 336cd6
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Ryan Lerch 336cd6
   Ryan Lerch <rlerch@redhat.com></rlerch@redhat.com>
Ryan Lerch 336cd6
"""
Ryan Lerch 336cd6
Ryan Lerch 336cd6
from __future__ import unicode_literals
Ryan Lerch 336cd6
Ryan Lerch 336cd6
__requires__ = ['SQLAlchemy >= 0.8']
Ryan Lerch 336cd6
import pkg_resources
Ryan Lerch 336cd6
Ryan Lerch 336cd6
import datetime
Ryan Lerch 336cd6
import unittest
Ryan Lerch 336cd6
import shutil
Ryan Lerch 336cd6
import sys
Ryan Lerch 336cd6
import os
Ryan Lerch 336cd6
Ryan Lerch 336cd6
import six
Ryan Lerch 336cd6
import json
Ryan Lerch 336cd6
import pygit2
Ryan Lerch 336cd6
from mock import patch, MagicMock
Ryan Lerch 336cd6
Ryan Lerch 336cd6
sys.path.insert(0, os.path.join(os.path.dirname(
Ryan Lerch 336cd6
    os.path.abspath(__file__)), '..'))
Ryan Lerch 336cd6
Ryan Lerch 336cd6
import pagure.lib
Ryan Lerch 336cd6
import tests
Ryan Lerch 336cd6
Ryan Lerch 336cd6
Ryan Lerch 336cd6
class PagureFlaskAppUserdashTests(tests.Modeltests):
Ryan Lerch 336cd6
    """ Tests for the index page of flask app controller of pagure """
Ryan Lerch 336cd6
Ryan Lerch 336cd6
    def test_index_commit_access_while_admin(self):
Ryan Lerch 336cd6
        """ Test the index endpoint filter for commit access only when user
Ryan Lerch 336cd6
        is an admin. """
Ryan Lerch 336cd6
        tests.create_projects(self.session)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        # Add a 3rd project just for foo
Ryan Lerch 336cd6
        item = pagure.lib.model.Project(
Ryan Lerch 336cd6
            user_id=2,  # foo
Ryan Lerch 336cd6
            name='test3',
Ryan Lerch 336cd6
            description='test project #3 with a very long description',
Ryan Lerch 336cd6
            hook_token='aaabbbeeefff',
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        user = tests.FakeUser(username='foo')
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=commit')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '
Pierre-Yves Chibon 837e8c
                ' border-0 ml-auto font-weight-bold">1 Projects',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '
No Projects match this filter
',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with admin level
Ryan Lerch 336cd6
            project = pagure.lib._get_project(self.session, 'test')
Ryan Lerch 336cd6
            msg = pagure.lib.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Ryan Lerch 336cd6
                new_user='foo',
Ryan Lerch 336cd6
                user='pingou',
Ryan Lerch 336cd6
                access='admin')
Ryan Lerch 336cd6
            self.session.commit()
Ryan Lerch 336cd6
            self.assertEqual(msg, 'User added')
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '
Pierre-Yves Chibon 837e8c
                ' border-0 ml-auto font-weight-bold">1 Projects',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '
No Projects match this filter
',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
    def test_index_commit_access_while_commit(self):
Ryan Lerch 336cd6
        """ Test the index endpoint filter for commit access only when user
Ryan Lerch 336cd6
        is an committer. """
Ryan Lerch 336cd6
        tests.create_projects(self.session)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        # Add a 3rd project just for foo
Ryan Lerch 336cd6
        item = pagure.lib.model.Project(
Ryan Lerch 336cd6
            user_id=2,  # foo
Ryan Lerch 336cd6
            name='test3',
Ryan Lerch 336cd6
            description='test project #3 with a very long description',
Ryan Lerch 336cd6
            hook_token='aaabbbeeefff',
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        user = tests.FakeUser(username='foo')
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=commit')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with commit level
Ryan Lerch 336cd6
            project = pagure.lib._get_project(self.session, 'test')
Ryan Lerch 336cd6
            msg = pagure.lib.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Ryan Lerch 336cd6
                new_user='foo',
Ryan Lerch 336cd6
                user='pingou',
Ryan Lerch 336cd6
                access='commit')
Ryan Lerch 336cd6
            self.session.commit()
Ryan Lerch 336cd6
            self.assertEqual(msg, 'User added')
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=commit')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
    def test_index_commit_access_while_ticket(self):
Ryan Lerch 336cd6
        """ Test the index endpoint filter for commit access only when user
Ryan Lerch 336cd6
        is has ticket access. """
Ryan Lerch 336cd6
        tests.create_projects(self.session)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        # Add a 3rd project just for foo
Ryan Lerch 336cd6
        item = pagure.lib.model.Project(
Ryan Lerch 336cd6
            user_id=2,  # foo
Ryan Lerch 336cd6
            name='test3',
Ryan Lerch 336cd6
            description='test project #3 with a very long description',
Ryan Lerch 336cd6
            hook_token='aaabbbeeefff',
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        user = tests.FakeUser(username='foo')
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=ticket')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with ticket level
Ryan Lerch 336cd6
            project = pagure.lib._get_project(self.session, 'test')
Ryan Lerch 336cd6
            msg = pagure.lib.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Ryan Lerch 336cd6
                new_user='foo',
Ryan Lerch 336cd6
                user='pingou',
Ryan Lerch 336cd6
                access='ticket')
Ryan Lerch 336cd6
            self.session.commit()
Ryan Lerch 336cd6
            self.assertEqual(msg, 'User added')
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After  -  projects with ticket access aren't shown
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=ticket')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
    def test_index_admin_access_while_admin(self):
Ryan Lerch 336cd6
        """ Test the index endpoint filter for admin access only when user
Ryan Lerch 336cd6
        is an admin. """
Ryan Lerch 336cd6
        tests.create_projects(self.session)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        # Add a 3rd project just for foo
Ryan Lerch 336cd6
        item = pagure.lib.model.Project(
Ryan Lerch 336cd6
            user_id=2,  # foo
Ryan Lerch 336cd6
            name='test3',
Ryan Lerch 336cd6
            description='test project #3 with a very long description',
Ryan Lerch 336cd6
            hook_token='aaabbbeeefff',
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        user = tests.FakeUser(username='foo')
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=admin')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with admin level
Ryan Lerch 336cd6
            project = pagure.lib._get_project(self.session, 'test')
Ryan Lerch 336cd6
            msg = pagure.lib.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Ryan Lerch 336cd6
                new_user='foo',
Ryan Lerch 336cd6
                user='pingou',
Ryan Lerch 336cd6
                access='admin')
Ryan Lerch 336cd6
            self.session.commit()
Ryan Lerch 336cd6
            self.assertEqual(msg, 'User added')
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=admin')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
    def test_index_admin_access_while_commit(self):
Ryan Lerch 336cd6
        """ Test the index endpoint filter for admin access only when user
Ryan Lerch 336cd6
        is an committer. """
Ryan Lerch 336cd6
        tests.create_projects(self.session)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        # Add a 3rd project just for foo
Ryan Lerch 336cd6
        item = pagure.lib.model.Project(
Ryan Lerch 336cd6
            user_id=2,  # foo
Ryan Lerch 336cd6
            name='test3',
Ryan Lerch 336cd6
            description='test project #3 with a very long description',
Ryan Lerch 336cd6
            hook_token='aaabbbeeefff',
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        user = tests.FakeUser(username='foo')
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=commit')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with commit level
Ryan Lerch 336cd6
            project = pagure.lib._get_project(self.session, 'test')
Ryan Lerch 336cd6
            msg = pagure.lib.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Ryan Lerch 336cd6
                new_user='foo',
Ryan Lerch 336cd6
                user='pingou',
Ryan Lerch 336cd6
                access='commit')
Ryan Lerch 336cd6
            self.session.commit()
Ryan Lerch 336cd6
            self.assertEqual(msg, 'User added')
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=commit')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            # The total number no longer changes
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
    def test_index_main_admin_access_while_commit(self):
Ryan Lerch 336cd6
        """ Test the index endpoint filter for main admin access only when
Ryan Lerch 336cd6
        user is an committer. """
Ryan Lerch 336cd6
        tests.create_projects(self.session)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        # Add a 3rd project just for foo
Ryan Lerch 336cd6
        item = pagure.lib.model.Project(
Ryan Lerch 336cd6
            user_id=2,  # foo
Ryan Lerch 336cd6
            name='test3',
Ryan Lerch 336cd6
            description='test project #3 with a very long description',
Ryan Lerch 336cd6
            hook_token='aaabbbeeefff',
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        user = tests.FakeUser(username='foo')
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=main admin')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with commit level
Ryan Lerch 336cd6
            project = pagure.lib._get_project(self.session, 'test')
Ryan Lerch 336cd6
            msg = pagure.lib.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Ryan Lerch 336cd6
                new_user='foo',
Ryan Lerch 336cd6
                user='pingou',
Ryan Lerch 336cd6
                access='commit')
Ryan Lerch 336cd6
            self.session.commit()
Ryan Lerch 336cd6
            self.assertEqual(msg, 'User added')
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects?acl=main admin')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
Ryan Lerch 336cd6
    @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
Ryan Lerch 336cd6
    def test_index_logged_in_private_project(self):
Ryan Lerch 336cd6
        """ Test the index endpoint when logged in with a private project. """
Ryan Lerch 336cd6
        tests.create_projects(self.session)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        # Add a 3rd project with a long description
Ryan Lerch 336cd6
        item = pagure.lib.model.Project(
Ryan Lerch 336cd6
            user_id=2,  # foo
Ryan Lerch 336cd6
            name='test3',
Ryan Lerch 336cd6
            description='test project #3 with a very long description',
Ryan Lerch 336cd6
            hook_token='aaabbbeeefff',
Ryan Lerch 336cd6
            private=True,
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Ryan Lerch 336cd6
        user = tests.FakeUser(username='foo')
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            output = self.app.get('/dashboard/projects')
Ryan Lerch 336cd6
            self.assertEqual(output.status_code, 200)
Ryan Lerch 336cd6
            output_text = output.get_data(as_text=True)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '

My Projects

\n'
Ryan Lerch 336cd6
                '          
Pierre-Yves Chibon 837e8c
                ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects\n',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '
Ryan Lerch 336cd6
                'fa fa-fw fa-lock">',
Ryan Lerch 336cd6
                output_text)
Ryan Lerch 336cd6
            self.assertEqual(output_text.count('title="Private project"'), 1)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
Ryan Lerch 336cd6
if __name__ == '__main__':
Ryan Lerch 336cd6
    unittest.main(verbosity=2)