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