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
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
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
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
)
Ryan Lerch 336cd6
Pierre-Yves Chibon 930073
import pagure.lib.query
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
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project #3 with a very long description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeefff",
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '
No Projects match this filter
',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with admin level
Pierre-Yves Chibon 73d120
            project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
            msg = pagure.lib.query.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Pierre-Yves Chibon 73d120
                new_user="foo",
Pierre-Yves Chibon 73d120
                user="pingou",
Pierre-Yves Chibon 73d120
                access="admin",
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.session.commit()
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '
No Projects match this filter
',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
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
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project #3 with a very long description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeefff",
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with commit level
Pierre-Yves Chibon 73d120
            project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
            msg = pagure.lib.query.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Pierre-Yves Chibon 73d120
                new_user="foo",
Pierre-Yves Chibon 73d120
                user="pingou",
Pierre-Yves Chibon 73d120
                access="commit",
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.session.commit()
Pierre-Yves Chibon 73d120
            self.assertEqual(msg, "User added")
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
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
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project #3 with a very long description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeefff",
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with ticket level
Pierre-Yves Chibon 73d120
            project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
            msg = pagure.lib.query.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Pierre-Yves Chibon 73d120
                new_user="foo",
Pierre-Yves Chibon 73d120
                user="pingou",
Pierre-Yves Chibon 73d120
                access="ticket",
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.session.commit()
Pierre-Yves Chibon 73d120
            self.assertEqual(msg, "User added")
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After  -  projects with ticket access aren't shown
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
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
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project #3 with a very long description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeefff",
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with admin level
Pierre-Yves Chibon 73d120
            project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
            msg = pagure.lib.query.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Pierre-Yves Chibon 73d120
                new_user="foo",
Pierre-Yves Chibon 73d120
                user="pingou",
Pierre-Yves Chibon 73d120
                access="admin",
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.session.commit()
Pierre-Yves Chibon 73d120
            self.assertEqual(msg, "User added")
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
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
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project #3 with a very long description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeefff",
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with commit level
Pierre-Yves Chibon 73d120
            project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
            msg = pagure.lib.query.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Pierre-Yves Chibon 73d120
                new_user="foo",
Pierre-Yves Chibon 73d120
                user="pingou",
Pierre-Yves Chibon 73d120
                access="commit",
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.session.commit()
Pierre-Yves Chibon 73d120
            self.assertEqual(msg, "User added")
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
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
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project #3 with a very long description",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeeefff",
Ryan Lerch 336cd6
        )
Ryan Lerch 336cd6
        self.session.add(item)
Ryan Lerch 336cd6
        self.session.commit()
Ryan Lerch 336cd6
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Ryan Lerch 336cd6
            # Before
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # Add foo to test with commit level
Pierre-Yves Chibon 73d120
            project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
            msg = pagure.lib.query.add_user_to_project(
Ryan Lerch 336cd6
                self.session,
Ryan Lerch 336cd6
                project=project,
Pierre-Yves Chibon 73d120
                new_user="foo",
Pierre-Yves Chibon 73d120
                user="pingou",
Pierre-Yves Chibon 73d120
                access="commit",
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.session.commit()
Pierre-Yves Chibon 73d120
            self.assertEqual(msg, "User added")
Ryan Lerch 336cd6
Ryan Lerch 336cd6
            # After
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
Pierre-Yves Chibon 73d120
    @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
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            description="test project #3 with a very long description",
Pierre-Yves Chibon 73d120
            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
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Ryan Lerch 336cd6
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            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',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.assertIn(
Ryan Lerch 336cd6
                '
Ryan Lerch 336cd6
                'fa fa-fw fa-lock">',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Ryan Lerch 336cd6
            self.assertEqual(output_text.count('title="Private project"'), 1)
Ryan Lerch 336cd6
Ryan Lerch 336cd6
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Ryan Lerch 336cd6
    unittest.main(verbosity=2)