Blame tests/test_pagure_flask_ui_issues_private.py

Pierre-Yves Chibon 30d656
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
"""
Pierre-Yves Chibon 30d656
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
 Authors:
Pierre-Yves Chibon 30d656
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
"""
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon 30d656
import unittest
Pierre-Yves Chibon 30d656
import sys
Pierre-Yves Chibon 30d656
import os
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
from mock import patch, MagicMock
Pierre-Yves Chibon 30d656
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
)
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 930073
import pagure.lib.query  # noqa
Pierre-Yves Chibon 30d656
import tests  # noqa
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
class PagureFlaskIssuesPrivatetests(tests.Modeltests):
Pierre-Yves Chibon 30d656
    """ Tests for flask issues controller of pagure with private tickets
Pierre-Yves Chibon 30d656
    """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 30d656
    def setUp(self):
Pierre-Yves Chibon 30d656
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 30d656
        super(PagureFlaskIssuesPrivatetests, self).setUp()
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
        # Create a 3rd user
Pierre-Yves Chibon 30d656
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="random",
Pierre-Yves Chibon 73d120
            fullname="Random user",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="random@bar.com",
Pierre-Yves Chibon 30d656
        )
Pierre-Yves Chibon 30d656
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="random@bar.com")
Pierre-Yves Chibon 30d656
        self.session.add(item)
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 30d656
            session=self.session,
Pierre-Yves Chibon 30d656
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #1",
Pierre-Yves Chibon 73d120
            content="We should work on this for the second time",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            status="Open",
Pierre-Yves Chibon 30d656
            private=True,
Pierre-Yves Chibon 30d656
        )
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #1")
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 30d656
            session=self.session,
Pierre-Yves Chibon 30d656
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #2",
Pierre-Yves Chibon 73d120
            content="We should work on this for the second time",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            status="Open",
Pierre-Yves Chibon 30d656
            private=False,
Pierre-Yves Chibon 30d656
        )
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #2")
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_issue_list_anonymous(self):
Pierre-Yves Chibon 30d656
        """ Test the list of issues when user is logged out. """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/issues")
Pierre-Yves Chibon 30d656
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        self.assertIn("<title>Issues - test - Pagure</title>", output_text)
Pierre-Yves Chibon 30d656
        self.assertIn(
Pierre-Yves Chibon 73d120
            ' 1 Open Issues\n',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_issue_list_admin(self):
Pierre-Yves Chibon 30d656
        """ Test the list of issues when user is an admin of the project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issues")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            self.assertIn("<title>Issues - test - Pagure</title>", output_text)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                ' 2 Open Issues\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_issue_list_author(self):
Pierre-Yves Chibon 30d656
        """ Test the list of issues when user is an admin of the project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issues")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            self.assertIn("<title>Issues - test - Pagure</title>", output_text)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                ' 2 Open Issues\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_issue_list_authenticated(self):
Pierre-Yves Chibon 30d656
        """ Test the list of issues when user is authenticated but has no
Pierre-Yves Chibon 30d656
        special access to the project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="random")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issues")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            self.assertIn("<title>Issues - test - Pagure</title>", output_text)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                ' 1 Open Issues\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_issue_list_authenticated_ticket(self):
Pierre-Yves Chibon 30d656
        """ Test the list of issues when user is authenticated but has
Pierre-Yves Chibon 30d656
        ticket level access to the project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 30d656
            session=self.session,
Pierre-Yves Chibon 30d656
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="random",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="ticket",
Pierre-Yves Chibon 30d656
        )
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="random")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issues")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            self.assertIn("<title>Issues - test - Pagure</title>", output_text)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                ' 1 Open Issues\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_issue_list_authenticated_commit(self):
Pierre-Yves Chibon 30d656
        """ Test the list of issues when user is authenticated but has
Pierre-Yves Chibon 30d656
        commit level access to the project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 30d656
            session=self.session,
Pierre-Yves Chibon 30d656
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="random",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Pierre-Yves Chibon 30d656
        )
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="random")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issues")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            self.assertIn("<title>Issues - test - Pagure</title>", output_text)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                ' 2 Open Issues\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_issue_list_authenticated_assigned(self):
Pierre-Yves Chibon 30d656
        """ Test the list of issues when user is authenticated and is
Pierre-Yves Chibon 30d656
        assigned to one of the issue.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 30d656
        issue.assignee_id = 3  # random
Pierre-Yves Chibon 30d656
        self.session.add(issue)
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="random")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issues")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            self.assertIn("<title>Issues - test - Pagure</title>", output_text)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                ' 2 Open Issues\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_view_issue_anonymous(self):
Pierre-Yves Chibon 30d656
        """ Test accessing a private ticket when user is logged out. """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/issue/1")
Pierre-Yves Chibon 30d656
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_view_issue_admin(self):
Pierre-Yves Chibon 30d656
        """ Test accessing a private ticket when user is an admin of the
Pierre-Yves Chibon 30d656
        project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issue/1")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Issue #1: Test issue #1 - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 9071b6
            self.assertIn(
Ryan Lerch e85d33
                '\n'
Pierre-Yves Chibon 3821e6
                '            #1\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_view_issue_author(self):
Pierre-Yves Chibon 30d656
        """ Test accessing a private ticket when user opened the ticket.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issue/1")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Issue #1: Test issue #1 - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 9071b6
            self.assertIn(
Ryan Lerch e85d33
                '\n'
Pierre-Yves Chibon 3821e6
                '            #1\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_view_issue_authenticated(self):
Pierre-Yves Chibon 30d656
        """ Test accessing a private ticket when user is authenticated but
Pierre-Yves Chibon 30d656
        has no special access to the project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="random")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issue/1")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_view_issue_authenticated_ticket(self):
Pierre-Yves Chibon 30d656
        """ Test accessing a private ticket when user is authenticated and
Pierre-Yves Chibon 30d656
        has ticket level access to the project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 30d656
            session=self.session,
Pierre-Yves Chibon 30d656
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="random",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="ticket",
Pierre-Yves Chibon 30d656
        )
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="random")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issue/1")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_view_issue_authenticated_commit(self):
Pierre-Yves Chibon 30d656
        """ Test accessing a private ticket when user is authenticated and
Pierre-Yves Chibon 30d656
        has commit level access to the project.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 30d656
            session=self.session,
Pierre-Yves Chibon 30d656
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="random",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Pierre-Yves Chibon 30d656
        )
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="random")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issue/1")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Issue #1: Test issue #1 - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 9071b6
            self.assertIn(
Ryan Lerch e85d33
                '\n'
Pierre-Yves Chibon 3821e6
                '            #1\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
    def test_view_issue_authenticated_assigned(self):
Pierre-Yves Chibon 30d656
        """ Test accessing a private ticket when user is authenticated and
Pierre-Yves Chibon 30d656
        is assigned to one of the issue.
Pierre-Yves Chibon 30d656
        """
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 30d656
        issue.assignee_id = 3  # random
Pierre-Yves Chibon 30d656
        self.session.add(issue)
Pierre-Yves Chibon 30d656
        self.session.commit()
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="random")
Pierre-Yves Chibon 30d656
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issue/1")
Pierre-Yves Chibon 30d656
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9071b6
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 30d656
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Issue #1: Test issue #1 - test - Pagure</title>",
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 9071b6
            self.assertIn(
Ryan Lerch e85d33
                '\n'
Pierre-Yves Chibon 3821e6
                '            #1\n',
Pierre-Yves Chibon 73d120
                output_text,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 30d656
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 30d656
    unittest.main(verbosity=2)