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
Lubomír Sedlář 9ca352
sys.path.insert(0, os.path.join(os.path.dirname(
Lubomír Sedlář 9ca352
    os.path.abspath(__file__)), '..'))
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)
Patrick Uiterwijk 170974
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
        self.admin = tests.FakeUser(username='pingou')
Lubomír Sedlář 9ca352
        self.user = tests.FakeUser(username='ralph')
Pierre-Yves Chibon 930073
        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
Lubomír Sedlář 9ca352
        settings['issue_tracker'] = False
Lubomír Sedlář 9ca352
        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
        """
Lubomír Sedlář 9ca352
        self.r1 = 'Ship it!'
Lubomír Sedlář 9ca352
        self.r2 = ('Nah. I would prefer if you did not submit this, as there '
Lubomír Sedlář 9ca352
                   'are problems.')
Lubomír Sedlář 9ca352
        self.sr1 = self.r1
Lubomír Sedlář 9ca352
        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
Lubomír Sedlář 9ca352
    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 77bdcd
            '<title>Settings - %s - Pagure</title>' % project,
Pierre-Yves Chibon 77bdcd
            output_text)
Pierre-Yves Chibon 77bdcd
        self.assertIn(
Pierre-Yves Chibon 77bdcd
                '
'
Pierre-Yves Chibon 77bdcd
                'Project Settings\n', output_text)
Lubomír Sedlář 9ca352
        if notice:
Pierre-Yves Chibon 77bdcd
            self.assertIn(notice, output_text)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    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(
Aurélien Bompard 626417
            self,
Aurélien Bompard 626417
            output.get_data(as_text=True),
Aurélien Bompard 626417
            link % (self.r1, self.sr1))
Aurélien Bompard 626417
        six.assertRegex(
Aurélien Bompard 626417
            self,
Aurélien Bompard 626417
            output.get_data(as_text=True),
Aurélien Bompard 626417
            link % (self.r2, self.sr2))
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):
Lubomír Sedlář 9ca352
            output = self.app.get('/test/settings')
Lubomír Sedlář 9ca352
            self.assertEqual(output.status_code, 200)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
            data = {
Lubomír Sedlář 9ca352
                'quick_reply': 'Ship it!',
Lubomír Sedlář 9ca352
            }
Lubomír Sedlář 9ca352
            output = self.app.post(
Lubomír Sedlář 9ca352
                '/test/update/quick_replies', data=data, follow_redirects=True)
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):
Lubomír Sedlář 9ca352
            data = {
Lubomír Sedlář 9ca352
                'quick_reply': 'Ship it!',
Lubomír Sedlář 9ca352
                'csrf_token': self.get_csrf(),
Lubomír Sedlář 9ca352
            }
Lubomír Sedlář 9ca352
            output = self.app.post(
Lubomír Sedlář 9ca352
                '/test/update/quick_replies', data=data, follow_redirects=True)
Lubomír Sedlář 9ca352
            self.assertRedirectToSettings(
Aurélien Bompard 626417
                output, notice='quick replies updated')
Lubomír Sedlář 9ca352
            self.assertQuickReplies(['Ship it!'])
Aurélien Bompard 626417
            self.assertIn('>Ship it!', output.get_data(as_text=True))
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 = {
Aurélien Bompard 626417
                'quick_reply': ['Ship it!', 'Nah.'],
Lubomír Sedlář 9ca352
                'csrf_token': self.get_csrf(),
Lubomír Sedlář 9ca352
            }
Lubomír Sedlář 9ca352
            output = self.app.post(
Lubomír Sedlář 9ca352
                '/test/update/quick_replies', data=data, follow_redirects=True)
Lubomír Sedlář 9ca352
            self.assertRedirectToSettings(
Aurélien Bompard 626417
                output, notice='quick replies updated')
Aurélien Bompard 626417
            self.assertQuickReplies(['Ship it!', 'Nah.'])
Lubomír Sedlář 9ca352
            # Check page has filled in textarea.
Aurélien Bompard 626417
            self.assertIn('>Ship it!', output.get_data(as_text=True))
Aurélien Bompard 626417
            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 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Lubomír Sedlář 9ca352
        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):
Lubomír Sedlář 9ca352
            data = {
Lubomír Sedlář 9ca352
                'quick_reply': [],
Lubomír Sedlář 9ca352
                'csrf_token': self.get_csrf(),
Lubomír Sedlář 9ca352
            }
Lubomír Sedlář 9ca352
            output = self.app.post(
Lubomír Sedlář 9ca352
                '/test/update/quick_replies', data=data, follow_redirects=True)
Lubomír Sedlář 9ca352
            self.assertRedirectToSettings(
Aurélien Bompard 626417
                output, notice='quick replies updated')
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):
Lubomír Sedlář 9ca352
            data = {
Lubomír Sedlář 9ca352
                'quick_reply': 'Ship it!',
Lubomír Sedlář 9ca352
                'csrf_token': 'a guess',
Lubomír Sedlář 9ca352
            }
Lubomír Sedlář 9ca352
            output = self.app.post(
Lubomír Sedlář 9ca352
                '/test/update/quick_replies', data=data, follow_redirects=True)
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):
Lubomír Sedlář 9ca352
            output = self.app.get('/test/settings')
Aurélien Bompard 626417
            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):
Lubomír Sedlář 9ca352
            data = {
Lubomír Sedlář 9ca352
                'quick_reply': 'Ship it!',
Lubomír Sedlář 9ca352
                'csrf_token': 'a guess',
Lubomír Sedlář 9ca352
            }
Lubomír Sedlář 9ca352
            output = self.app.post(
Lubomír Sedlář 9ca352
                '/test/update/quick_replies', data=data, follow_redirects=True)
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):
Lubomír Sedlář 9ca352
            data = {
Lubomír Sedlář 9ca352
                'quick_reply': 'Ship it!',
Lubomír Sedlář 9ca352
                'csrf_token': 'a guess',
Lubomír Sedlář 9ca352
            }
Lubomír Sedlář 9ca352
            output = self.app.post(
Lubomír Sedlář 9ca352
                '/boom/update/quick_replies', data=data, follow_redirects=True)
Lubomír Sedlář 9ca352
            self.assertEqual(output.status_code, 404)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    @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,
Lubomír Sedlář 9ca352
            'Dummy issue',
Lubomír Sedlář 9ca352
            'Just a lonely issue.',
Lubomír Sedlář 9ca352
            'pingou',
Lubomír Sedlář 9ca352
            None,
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):
Lubomír Sedlář 9ca352
            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
Lubomír Sedlář 9ca352
    @mock.patch('pagure.lib.git.update_git')
Lubomír Sedlář 9ca352
    @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,
Lubomír Sedlář 9ca352
            'pr',
Lubomír Sedlář 9ca352
            self.repo,
Lubomír Sedlář 9ca352
            'master',
Lubomír Sedlář 9ca352
            'Dummy PR', '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):
Lubomír Sedlář 9ca352
            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
Lubomír Sedlář 9ca352
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)