Blame tests/test_pagure_flask_ui_quick_reply.py

Lubomír Sedlář 9ca352
# -*- coding: utf-8 -*-
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
"""
Lubomír Sedlář 9ca352
 (c) 2015-2016 - 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
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
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
Lubomír Sedlář 9ca352
import pagure.lib.plugins
Lubomír Sedlář 9ca352
import pagure.lib.model
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
        pagure.APP.config['TESTING'] = True
Lubomír Sedlář 9ca352
        pagure.SESSION = self.session
Lubomír Sedlář 9ca352
        pagure.ui.app.SESSION = self.session
Lubomír Sedlář 9ca352
        pagure.ui.filters.SESSION = self.session
Lubomír Sedlář 9ca352
        pagure.ui.fork.SESSION = self.session
Lubomír Sedlář 9ca352
        pagure.ui.issues.SESSION = self.session
Lubomír Sedlář 9ca352
        pagure.ui.plugins.SESSION = self.session
Lubomír Sedlář 9ca352
        pagure.ui.repo.SESSION = self.session
Lubomír Sedlář 9ca352
        pagure.ui.SESSION = self.session
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
        pagure.APP.config['GIT_FOLDER'] = self.path
Lubomír Sedlář 9ca352
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Lubomír Sedlář 9ca352
            self.path, 'tickets')
Lubomír Sedlář 9ca352
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Lubomír Sedlář 9ca352
            self.path, 'docs')
Lubomír Sedlář 9ca352
        self.app = pagure.APP.test_client()
Lubomír Sedlář 9ca352
        tests.create_projects(self.session)
Lubomír Sedlář 9ca352
        tests.create_projects_git(os.path.join(self.path), 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')
Farhaan Bukhsh 940880
        self.repo = pagure.lib._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)
Lubomír Sedlář 9ca352
        self.assertIn(
Lubomír Sedlář 9ca352
            u'<title>Settings - %s - Pagure</title>' % project, output.data)
Lubomír Sedlář 9ca352
        self.assertIn(u'

Settings for %s

' % project, output.data)
Lubomír Sedlář 9ca352
        if notice:
Lubomír Sedlář 9ca352
            self.assertIn(notice, output.data)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def assertQuickReplies(self, quick_replies, project='test'):
Farhaan Bukhsh 940880
        repo = pagure.lib._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."""
Lubomír Sedlář 9ca352
        link = 'data-qr="%s">\s*%s\s*'
Lubomír Sedlář 9ca352
        self.assertRegexpMatches(
Lubomír Sedlář 9ca352
            output.data, link % (self.r1, self.sr1))
Lubomír Sedlář 9ca352
        self.assertRegexpMatches(
Lubomír Sedlář 9ca352
            output.data, 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):
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, 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):
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, 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(
Lubomír Sedlář 9ca352
                output, notice=u'quick replies updated')
Lubomír Sedlář 9ca352
            self.assertQuickReplies(['Ship it!'])
Lubomír Sedlář 9ca352
            self.assertIn(u'>Ship it!', output.data)
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_update_quick_replies_multiple(self):
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, self.admin):
Lubomír Sedlář 9ca352
            data = {
Lubomír Sedlář 9ca352
                'quick_reply': [u'Ship it!', u'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(
Lubomír Sedlář 9ca352
                output, notice=u'quick replies updated')
Lubomír Sedlář 9ca352
            self.assertQuickReplies([u'Ship it!', u'Nah.'])
Lubomír Sedlář 9ca352
            # Check page has filled in textarea.
Lubomír Sedlář 9ca352
            self.assertIn(u'>Ship it!', output.data)
Lubomír Sedlář 9ca352
            self.assertIn(u'>Nah.', output.data)
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
Farhaan Bukhsh 940880
        repo = pagure.lib._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
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, 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(
Lubomír Sedlář 9ca352
                output, notice=u'quick replies updated')
Lubomír Sedlář 9ca352
            self.assertQuickReplies([])
Lubomír Sedlář 9ca352
Lubomír Sedlář 9ca352
    def test_update_quick_replies_unprivileged(self):
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, 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
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, self.admin):
Lubomír Sedlář 9ca352
            output = self.app.get('/test/settings')
Lubomír Sedlář 9ca352
            self.assertNotIn('Quick replies', output.data)
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
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, 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):
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, 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
Lubomír Sedlář 9ca352
        issue = pagure.lib.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
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, 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
Lubomír Sedlář 9ca352
        pr = pagure.lib.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
Lubomír Sedlář 9ca352
        with tests.user_set(pagure.APP, 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__':
Lubomír Sedlář 9ca352
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Lubomír Sedlář 9ca352
        PagureFlaskQuickReplytest)
Lubomír Sedlář 9ca352
    unittest.TextTestRunner(verbosity=2).run(SUITE)