Blame tests/test_pagure_flask_ui_quick_reply.py

Lubomír Sedlář 9ca352
# -*- coding: utf-8 -*-
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
"""
Pierre-Yves Chibon 77bdcd
 (c) 2015-2018 - Copyright Red Hat Inc
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
 Authors:
Lubomír Sedlář 9ca352
   Lubomír Sedlář <lsedlar@redhat.com></lsedlar@redhat.com>
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
"""
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Lubomír Sedlář 9ca352
import mock
Lubomír Sedlář 9ca352
import os
Lubomír Sedlář 9ca352
import sys
Lubomír Sedlář 9ca352
import unittest
Lubomír Sedlář 9ca352
Aurélien Bompard 626417
import six
Aurélien Bompard 626417
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
)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
import pagure.lib.model
Pierre-Yves Chibon 930073
import pagure.lib.plugins
Pierre-Yves Chibon 930073
import pagure.lib.query
Lubomír Sedlář 9ca352
import pagure.hooks
Lubomír Sedlář 9ca352
import tests
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
class PagureFlaskQuickReplytest(tests.Modeltests):
Lubomír Sedlář 9ca352
    """ Tests for configuring and displaying quick replies. """
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def setUp(self):
Lubomír Sedlář 9ca352
        """ Set up the environnment, ran before every tests. """
Lubomír Sedlář 9ca352
        super(PagureFlaskQuickReplytest, self).setUp()
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 73d120
        self.admin = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 73d120
        self.user = tests.FakeUser(username="ralph")
Pierre-Yves Chibon 73d120
        self.repo = pagure.lib.query._get_project(self.session, "test")
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def disable_issues_and_pull_requests(self):
Lubomír Sedlář 9ca352
        """Disable both issues and pull requests."""
Lubomír Sedlář 9ca352
        # This can not use direct access as repo.settings is a property that
Lubomír Sedlář 9ca352
        # serializes data into JSON. Direct modification is not preserved.
Lubomír Sedlář 9ca352
        settings = self.repo.settings
Pierre-Yves Chibon 73d120
        settings["issue_tracker"] = False
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Lubomír Sedlář 9ca352
        self.repo.settings = settings
Lubomír Sedlář 9ca352
        self.session.add(self.repo)
Lubomír Sedlář 9ca352
        self.session.commit()
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def setup_quick_replies(self):
Lubomír Sedlář 9ca352
        """Create some quick replies.
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
        The full replies are stored as r1 and r2 attributes, with shortened
Lubomír Sedlář 9ca352
        versions in sr1 and sr2.
Lubomír Sedlář 9ca352
        """
Pierre-Yves Chibon 73d120
        self.r1 = "Ship it!"
Pierre-Yves Chibon 73d120
        self.r2 = (
Pierre-Yves Chibon 73d120
            "Nah. I would prefer if you did not submit this, as there "
Pierre-Yves Chibon 73d120
            "are problems."
Pierre-Yves Chibon 73d120
        )
Lubomír Sedlář 9ca352
        self.sr1 = self.r1
Pierre-Yves Chibon 73d120
        self.sr2 = "Nah. I would prefer if you did not submit this, as..."
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
        # Set some quick replies
Lubomír Sedlář 9ca352
        self.repo.quick_replies = [self.r1, self.r2]
Lubomír Sedlář 9ca352
        self.session.add(self.repo)
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 73d120
    def assertRedirectToSettings(self, output, project="test", notice=None):
Lubomír Sedlář 9ca352
        """
Lubomír Sedlář 9ca352
        Check that user was redirected to settings page of a given project
Lubomír Sedlář 9ca352
        and that a given notice was printed.
Lubomír Sedlář 9ca352
        """
Lubomír Sedlář 9ca352
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 77bdcd
        output_text = output.get_data(as_text=True)
Lubomír Sedlář 9ca352
        self.assertIn(
Pierre-Yves Chibon 73d120
            "<title>Settings - %s - Pagure</title>" % project, output_text
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 77bdcd
        self.assertIn(
Pierre-Yves Chibon 73d120
            '
'
Pierre-Yves Chibon 73d120
            "Project Settings\n",
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Lubomír Sedlář 9ca352
        if notice:
Pierre-Yves Chibon 77bdcd
            self.assertIn(notice, output_text)
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 73d120
    def assertQuickReplies(self, quick_replies, project="test"):
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, project)
Lubomír Sedlář 9ca352
        self.assertEqual(repo.quick_replies, quick_replies)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def assertQuickReplyLinks(self, output):
Lubomír Sedlář 9ca352
        """Assert reply links created by setup_quick_replies are present."""
Karsten Hopp b310af
        link = r'data-qr="%s">\s*%s\s*'
Aurélien Bompard 626417
        six.assertRegex(
Pierre-Yves Chibon 73d120
            self, output.get_data(as_text=True), link % (self.r1, self.sr1)
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 626417
        six.assertRegex(
Pierre-Yves Chibon 73d120
            self, output.get_data(as_text=True), link % (self.r2, self.sr2)
Pierre-Yves Chibon 73d120
        )
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_new_project_has_none(self):
Lubomír Sedlář 9ca352
        self.assertQuickReplies([])
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_update_quick_reply_without_csrf(self):
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.admin):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/settings")
Lubomír Sedlář 9ca352
            self.assertEqual(output.status_code, 200)
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 73d120
            data = {"quick_reply": "Ship it!"}
Lubomír Sedlář 9ca352
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/update/quick_replies", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
            self.assertRedirectToSettings(output)
Lubomír Sedlář 9ca352
            self.assertQuickReplies([])
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_update_quick_replies_single(self):
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.admin):
Pierre-Yves Chibon 73d120
            data = {"quick_reply": "Ship it!", "csrf_token": self.get_csrf()}
Lubomír Sedlář 9ca352
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/update/quick_replies", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
            self.assertRedirectToSettings(
Pierre-Yves Chibon 73d120
                output, notice="quick replies updated"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertQuickReplies(["Ship it!"])
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                ">Ship it!", output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_update_quick_replies_multiple(self):
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.admin):
Lubomír Sedlář 9ca352
            data = {
Pierre-Yves Chibon 73d120
                "quick_reply": ["Ship it!", "Nah."],
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Lubomír Sedlář 9ca352
            }
Lubomír Sedlář 9ca352
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/update/quick_replies", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
            self.assertRedirectToSettings(
Pierre-Yves Chibon 73d120
                output, notice="quick replies updated"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertQuickReplies(["Ship it!", "Nah."])
Lubomír Sedlář 9ca352
            # Check page has filled in textarea.
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                ">Ship it!", output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn(">Nah.", output.get_data(as_text=True))
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_update_quick_replies_empty_to_reset(self):
Lubomír Sedlář 9ca352
        # Set some quick replies
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        repo.quick_replies = ["Ship it!", "Nah."]
Lubomír Sedlář 9ca352
        self.session.add(repo)
Lubomír Sedlář 9ca352
        self.session.commit()
Lubomír Sedlář 9ca352
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.admin):
Pierre-Yves Chibon 73d120
            data = {"quick_reply": [], "csrf_token": self.get_csrf()}
Lubomír Sedlář 9ca352
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/update/quick_replies", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
            self.assertRedirectToSettings(
Pierre-Yves Chibon 73d120
                output, notice="quick replies updated"
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
            self.assertQuickReplies([])
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_update_quick_replies_unprivileged(self):
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.user):
Pierre-Yves Chibon 73d120
            data = {"quick_reply": "Ship it!", "csrf_token": "a guess"}
Lubomír Sedlář 9ca352
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/update/quick_replies", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
            self.assertEqual(output.status_code, 403)
Lubomír Sedlář 9ca352
            self.assertQuickReplies([])
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_no_form_with_disabled_issues_and_pull_requests(self):
Lubomír Sedlář 9ca352
        self.disable_issues_and_pull_requests()
Lubomír Sedlář 9ca352
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.admin):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/settings")
Pierre-Yves Chibon 73d120
            self.assertNotIn("Quick replies", output.get_data(as_text=True))
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_no_submit_with_disabled_issues_and_pull_requests(self):
Lubomír Sedlář 9ca352
        self.disable_issues_and_pull_requests()
Lubomír Sedlář 9ca352
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.admin):
Pierre-Yves Chibon 73d120
            data = {"quick_reply": "Ship it!", "csrf_token": "a guess"}
Lubomír Sedlář 9ca352
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/update/quick_replies", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
            self.assertEqual(output.status_code, 404)
Lubomír Sedlář 9ca352
            self.assertQuickReplies([])
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_submit_for_bad_project(self):
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.admin):
Pierre-Yves Chibon 73d120
            data = {"quick_reply": "Ship it!", "csrf_token": "a guess"}
Lubomír Sedlář 9ca352
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/boom/update/quick_replies", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Lubomír Sedlář 9ca352
            self.assertEqual(output.status_code, 404)
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 73d120
    @mock.patch("pagure.lib.git.update_git")
Lubomír Sedlář 9ca352
    def test_issue_page_has_quick_replies(self, p_ugt):
Lubomír Sedlář 9ca352
        self.setup_quick_replies()
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.new_issue(
Lubomír Sedlář 9ca352
            self.session,
Lubomír Sedlář 9ca352
            self.repo,
Pierre-Yves Chibon 73d120
            "Dummy issue",
Pierre-Yves Chibon 73d120
            "Just a lonely issue.",
Pierre-Yves Chibon 73d120
            "pingou",
Lubomír Sedlář 9ca352
            None,
Pierre-Yves Chibon 73d120
            notify=False,
Lubomír Sedlář 9ca352
        )
Lubomír Sedlář 9ca352
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/issue/%s" % issue.id)
Lubomír Sedlář 9ca352
            self.assertEqual(output.status_code, 200)
Lubomír Sedlář 9ca352
            self.assertQuickReplyLinks(output)
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 73d120
    @mock.patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @mock.patch("pagure.lib.git.diff_pull_request")
Lubomír Sedlář 9ca352
    def test_pull_request_page_has_quick_replies(self, diff, p_ugt):
Lubomír Sedlář 9ca352
        diff.return_value = ([], [])
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
        self.setup_quick_replies()
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 930073
        pr = pagure.lib.query.new_pull_request(
Lubomír Sedlář 9ca352
            self.session,
Pierre-Yves Chibon 73d120
            "pr",
Lubomír Sedlář 9ca352
            self.repo,
Pierre-Yves Chibon 73d120
            "master",
Pierre-Yves Chibon 73d120
            "Dummy PR",
Pierre-Yves Chibon 73d120
            "pingou",
Lubomír Sedlář 9ca352
            None,
Lubomír Sedlář 9ca352
            repo_from=self.repo,
Lubomír Sedlář 9ca352
            notify=False,
Lubomír Sedlář 9ca352
        )
Lubomír Sedlář 9ca352
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, self.user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/pull-request/%s" % pr.id)
Lubomír Sedlář 9ca352
            self.assertEqual(output.status_code, 200)
Lubomír Sedlář 9ca352
            self.assertQuickReplyLinks(output)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)