Blame tests/test_pagure_flask_api_pr_flag.py

Pierre-Yves Chibon 3ffb2e
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
"""
Pierre-Yves Chibon 3ffb2e
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
 Authors:
Pierre-Yves Chibon 3ffb2e
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
"""
Pierre-Yves Chibon 3ffb2e
Aurélien Bompard 626417
from __future__ import unicode_literals
Aurélien Bompard 626417
Pierre-Yves Chibon 3ffb2e
import unittest
Pierre-Yves Chibon 3ffb2e
import sys
Pierre-Yves Chibon 3ffb2e
import os
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
import json
Pierre-Yves Chibon 3ffb2e
from mock import patch, MagicMock
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 3ffb2e
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
import pagure  # noqa
Slavek Kabrda 45252f
import pagure.config  # noqa
Pierre-Yves Chibon 3ffb2e
import pagure.lib  # noqa
Pierre-Yves Chibon 3ffb2e
import tests  # noqa
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
class PagureFlaskApiPRFlagtests(tests.Modeltests):
Pierre-Yves Chibon 3ffb2e
    """ Tests for the flask API of pagure for flagging pull-requests """
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    maxDiff = None
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 3ffb2e
    def setUp(self):
Pierre-Yves Chibon 3ffb2e
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 3ffb2e
        super(PagureFlaskApiPRFlagtests, self).setUp()
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        pagure.config.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        tests.create_projects(self.session)
Pierre-Yves Chibon 3ffb2e
        tests.create_tokens(self.session)
Pierre-Yves Chibon 3ffb2e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Create a pull-request
Pierre-Yves Chibon 3ffb2e
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 3ffb2e
        forked_repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 3ffb2e
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon 3ffb2e
            session=self.session,
Pierre-Yves Chibon 3ffb2e
            repo_from=forked_repo,
Pierre-Yves Chibon 3ffb2e
            branch_from='master',
Pierre-Yves Chibon 3ffb2e
            repo_to=repo,
Pierre-Yves Chibon 3ffb2e
            branch_to='master',
Pierre-Yves Chibon 3ffb2e
            title='test pull-request',
Pierre-Yves Chibon 3ffb2e
            user='pingou',
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Check flags before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_invalid_project(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging a PR on an invalid project. """
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Invalid project
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/foo/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Project not found",
Pierre-Yves Chibon 3ffb2e
                "error_code": "ENOPROJECT",
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_incorrect_project(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging a PR on the wrong project. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid token, wrong project
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test2/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 3ffb2e
                         data['error_code'])
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 0bd61f
    def test_pr_disabled(self):
Pierre-Yves Chibon 0bd61f
        """ Test the flagging a PR when PRs are disabled. """
Pierre-Yves Chibon 0bd61f
Pierre-Yves Chibon 0bd61f
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 0bd61f
        settings = repo.settings
Pierre-Yves Chibon 0bd61f
        settings['pull_requests'] = False
Pierre-Yves Chibon 0bd61f
        repo.settings = settings
Pierre-Yves Chibon 0bd61f
        self.session.add(repo)
Pierre-Yves Chibon 0bd61f
        self.session.commit()
Pierre-Yves Chibon 0bd61f
Pierre-Yves Chibon 0bd61f
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 0bd61f
Pierre-Yves Chibon 0bd61f
        # PRs disabled
Pierre-Yves Chibon 0bd61f
        output = self.app.post(
Pierre-Yves Chibon 0bd61f
            '/api/0/test/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon 0bd61f
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 0bd61f
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0bd61f
        self.assertDictEqual(
Pierre-Yves Chibon 0bd61f
            data,
Pierre-Yves Chibon 0bd61f
            {
Pierre-Yves Chibon 0bd61f
                u'error': u'Pull-Request have been deactivated for this project',
Pierre-Yves Chibon 0bd61f
                u'error_code': u'EPULLREQUESTSDISABLED'
Pierre-Yves Chibon 0bd61f
            }
Pierre-Yves Chibon 0bd61f
        )
Pierre-Yves Chibon 0bd61f
Pierre-Yves Chibon 3ffb2e
    def test_no_pr(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging a PR when the PR doesn't exist. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No PR
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/10/flag', headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Pull-Request not found",
Pierre-Yves Chibon 3ffb2e
                "error_code": "ENOREQ",
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_input(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging an existing PR but with no data. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No input
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'error': 'Invalid or incomplete input submitted',
Aurélien Bompard 626417
                'error_code': 'EINVALIDREQ',
Aurélien Bompard 626417
                'errors': {
Aurélien Bompard 626417
                    'comment': ['This field is required.'],
Aurélien Bompard 626417
                    'url': ['This field is required.'],
Aurélien Bompard 626417
                    'username': ['This field is required.']
Pierre-Yves Chibon 3ffb2e
                }
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_comment(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging an existing PR but with incomplete data. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'percent': 100,
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Incomplete request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 3ffb2e
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 3ffb2e
                "errors": {"comment": ["This field is required."]}
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon df049b
    @patch(
Pierre-Yves Chibon df049b
        'pagure.lib.add_pull_request_flag',
Pierre-Yves Chibon df049b
        MagicMock(side_effect=pagure.exceptions.PagureException('error')))
Pierre-Yves Chibon df049b
    def test_raise_exception(self):
Pierre-Yves Chibon df049b
        """ Test the flagging a PR when adding a flag raises an exception. """
Pierre-Yves Chibon df049b
Pierre-Yves Chibon df049b
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon df049b
        data = {
Pierre-Yves Chibon df049b
            'username': 'Jenkins',
Pierre-Yves Chibon df049b
            'comment': 'Tests running',
Pierre-Yves Chibon df049b
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon df049b
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon df049b
        }
Pierre-Yves Chibon df049b
Pierre-Yves Chibon df049b
        # Adding a flag raises an exception
Pierre-Yves Chibon df049b
        output = self.app.post(
Pierre-Yves Chibon df049b
            '/api/0/test/pull-request/1/flag', headers=headers, data=data)
Pierre-Yves Chibon df049b
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon df049b
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon df049b
        self.assertDictEqual(
Pierre-Yves Chibon df049b
            data, {u'error': u'error', u'error_code': u'ENOCODE'})
Pierre-Yves Chibon df049b
Pierre-Yves Chibon 3ffb2e
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 3ffb2e
    def test_flagging_a_pul_request_with_notification(self, mock_email):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging a PR. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Enable PR notifications
Pierre-Yves Chibon 3ffb2e
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 3ffb2e
        settings = repo.settings
Pierre-Yves Chibon 3ffb2e
        settings['notify_on_pull-request_flag'] = True
Pierre-Yves Chibon 3ffb2e
        repo.settings = settings
Pierre-Yves Chibon 3ffb2e
        self.session.add(repo)
Pierre-Yves Chibon 3ffb2e
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'comment': 'Tests running',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['flag']['date_created'] = '1510742565'
Pierre-Yves Chibon 3ffb2e
        pr_uid = data['flag']['pull_request_uid']
Aurélien Bompard 626417
        data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'flag': {
Aurélien Bompard 626417
                    'comment': 'Tests running',
Aurélien Bompard 626417
                    'date_created': '1510742565',
Aurélien Bompard 626417
                    'percent': None,
Aurélien Bompard 626417
                    'pull_request_uid': '62b49f00d489452994de5010565fab81',
Aurélien Bompard 626417
                    'status': 'pending',
Aurélien Bompard 626417
                    'url': 'http://jenkins.cloud.fedoraproject.org/',
Aurélien Bompard 626417
                    'user': {
Aurélien Bompard 626417
                        'default_email': 'bar@pingou.com',
Aurélien Bompard 626417
                        'emails': ['bar@pingou.com', 'foo@pingou.com'],
Aurélien Bompard 626417
                        'fullname': 'PY C',
Aurélien Bompard 626417
                        'name': 'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Aurélien Bompard 626417
                    'username': 'Jenkins'},
Aurélien Bompard 626417
                'message': 'Flag added',
Pierre-Yves Chibon e2c468
                'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Pierre-Yves Chibon e2c468
                'user': 'pingou'
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].comment, 'Tests running')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, None)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Check the notification sent
Pierre-Yves Chibon 3ffb2e
        mock_email.assert_called_once_with(
Aurélien Bompard 626417
            '\nJenkins flagged the pull-request `test pull-request` '
Aurélien Bompard 626417
            'as pending: Tests running\n\n'
Neal Gompa 9c34c4
            'http://localhost.localdomain/test/pull-request/1\n',
Aurélien Bompard 626417
            'PR #1 - Jenkins: pending',
Aurélien Bompard 626417
            'bar@pingou.com',
Pierre-Yves Chibon 9ef9df
            assignee=None,
Aurélien Bompard 626417
            in_reply_to='test-pull-request-' + pr_uid,
Aurélien Bompard 626417
            mail_id='test-pull-request-' + pr_uid + '-1',
Aurélien Bompard 626417
            project_name='test',
Pierre-Yves Chibon 9ef9df
            reporter='pingou',
Aurélien Bompard 626417
            user_from='Jenkins'
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_updating_flag(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the updating the flag of a PR. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'comment': 'Tests running',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['flag']['date_created'] = '1510742565'
Aurélien Bompard 626417
        data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'flag': {
Aurélien Bompard 626417
                    'comment': 'Tests running',
Aurélien Bompard 626417
                    'date_created': '1510742565',
Aurélien Bompard 626417
                    'percent': None,
Aurélien Bompard 626417
                    'pull_request_uid': '62b49f00d489452994de5010565fab81',
Aurélien Bompard 626417
                    'status': 'pending',
Aurélien Bompard 626417
                    'url': 'http://jenkins.cloud.fedoraproject.org/',
Aurélien Bompard 626417
                    'user': {
Aurélien Bompard 626417
                        'default_email': 'bar@pingou.com',
Aurélien Bompard 626417
                        'emails': ['bar@pingou.com', 'foo@pingou.com'],
Aurélien Bompard 626417
                        'fullname': 'PY C',
Aurélien Bompard 626417
                        'name': 'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Aurélien Bompard 626417
                    'username': 'Jenkins'},
Aurélien Bompard 626417
                'message': 'Flag added',
Pierre-Yves Chibon e2c468
                'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Pierre-Yves Chibon e2c468
                'user': 'pingou'
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].comment, 'Tests running')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, None)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Update flag  -  w/o providing the status
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'percent': 100,
Pierre-Yves Chibon 3ffb2e
            'comment': 'Tests passed',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['flag']['date_created'] = '1510742565'
Aurélien Bompard 626417
        data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'flag': {
Aurélien Bompard 626417
                    'comment': 'Tests passed',
Aurélien Bompard 626417
                    'date_created': '1510742565',
Aurélien Bompard 626417
                    'percent': 100,
Aurélien Bompard 626417
                    'pull_request_uid': '62b49f00d489452994de5010565fab81',
Aurélien Bompard 626417
                    'status': 'success',
Aurélien Bompard 626417
                    'url': 'http://jenkins.cloud.fedoraproject.org/',
Aurélien Bompard 626417
                    'user': {
Aurélien Bompard 626417
                        'default_email': 'bar@pingou.com',
Aurélien Bompard 626417
                        'emails': ['bar@pingou.com', 'foo@pingou.com'],
Aurélien Bompard 626417
                        'fullname': 'PY C',
Aurélien Bompard 626417
                        'name': 'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Aurélien Bompard 626417
                    'username': 'Jenkins'},
Aurélien Bompard 626417
                'message': 'Flag updated',
Pierre-Yves Chibon e2c468
                'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Pierre-Yves Chibon e2c468
                'user': 'pingou'
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].comment, 'Tests passed')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 100)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_adding_two_flags(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the adding two flags to a PR. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'comment': 'Tests passed',
Pierre-Yves Chibon 3ffb2e
            'status': 'success',
Pierre-Yves Chibon 3ffb2e
            'percent': '100',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['flag']['date_created'] = '1510742565'
Aurélien Bompard 626417
        data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'flag': {
Aurélien Bompard 626417
                    'comment': 'Tests passed',
Aurélien Bompard 626417
                    'date_created': '1510742565',
Aurélien Bompard 626417
                    'percent': 100,
Aurélien Bompard 626417
                    'pull_request_uid': '62b49f00d489452994de5010565fab81',
Aurélien Bompard 626417
                    'status': 'success',
Aurélien Bompard 626417
                    'url': 'http://jenkins.cloud.fedoraproject.org/',
Aurélien Bompard 626417
                    'user': {
Aurélien Bompard 626417
                        'default_email': 'bar@pingou.com',
Aurélien Bompard 626417
                        'emails': ['bar@pingou.com', 'foo@pingou.com'],
Aurélien Bompard 626417
                        'fullname': 'PY C',
Aurélien Bompard 626417
                        'name': 'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Aurélien Bompard 626417
                    'username': 'Jenkins'},
Aurélien Bompard 626417
                'message': 'Flag added',
Pierre-Yves Chibon e2c468
                'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Pierre-Yves Chibon e2c468
                'user': 'pingou'
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].comment, 'Tests passed')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 100)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'comment': 'Tests running again',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['flag']['date_created'] = '1510742565'
Aurélien Bompard 626417
        data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 3ffb2e
        self.assertNotEqual(
Pierre-Yves Chibon 3ffb2e
            data['uid'], 'jenkins_build_pagure_100+seed')
Pierre-Yves Chibon 3ffb2e
        data['uid'] = 'jenkins_build_pagure_100+seed'
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'flag': {
Aurélien Bompard 626417
                    'comment': 'Tests running again',
Aurélien Bompard 626417
                    'date_created': '1510742565',
Aurélien Bompard 626417
                    'percent': None,
Aurélien Bompard 626417
                    'pull_request_uid': '62b49f00d489452994de5010565fab81',
Aurélien Bompard 626417
                    'status': 'pending',
Aurélien Bompard 626417
                    'url': 'http://jenkins.cloud.fedoraproject.org/',
Aurélien Bompard 626417
                    'user': {
Aurélien Bompard 626417
                        'default_email': 'bar@pingou.com',
Aurélien Bompard 626417
                        'emails': ['bar@pingou.com', 'foo@pingou.com'],
Aurélien Bompard 626417
                        'fullname': 'PY C',
Aurélien Bompard 626417
                        'name': 'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Aurélien Bompard 626417
                    'username': 'Jenkins'},
Aurélien Bompard 626417
                'message': 'Flag added',
Pierre-Yves Chibon e2c468
                'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Pierre-Yves Chibon e2c468
                'user': 'pingou'
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Two flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 2)
Pierre-Yves Chibon f523b9
        self.assertEqual(request.flags[0].comment, 'Tests running again')
Pierre-Yves Chibon f523b9
        self.assertEqual(request.flags[0].percent, None)
Pierre-Yves Chibon f523b9
        self.assertEqual(request.flags[1].comment, 'Tests passed')
Pierre-Yves Chibon f523b9
        self.assertEqual(request.flags[1].percent, 100)
Pierre-Yves Chibon 3ffb2e
Slavek Kabrda 45252f
    @patch.dict('pagure.config.config',
Slavek Kabrda 45252f
                {
Slavek Kabrda 45252f
                    'FLAG_STATUSES_LABELS':
Slavek Kabrda 45252f
                        {
Slavek Kabrda 45252f
                            'pend!': 'label-info',
Slavek Kabrda 45252f
                            'succeed!': 'label-success',
Slavek Kabrda 45252f
                            'fail!': 'label-danger',
Slavek Kabrda 45252f
                            'what?': 'label-warning',
Slavek Kabrda 45252f
                        },
Slavek Kabrda 45252f
                    'FLAG_PENDING': 'pend!',
Slavek Kabrda 45252f
                    'FLAG_SUCCESS': 'succeed!',
Slavek Kabrda 45252f
                    'FLAG_FAILURE': 'fail!',
Slavek Kabrda 45252f
                })
Slavek Kabrda 45252f
    def test_flagging_a_pull_request_while_having_custom_statuses(self):
Slavek Kabrda 45252f
        """ Test flagging a PR while having custom statuses. """
Slavek Kabrda 45252f
        headers = {'Authorization': 'token aaabbbcccddd'}
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # No status and no percent => should use FLAG_PENDING
Slavek Kabrda 45252f
        send_data = {
Slavek Kabrda 45252f
            'username': 'Jenkins',
Slavek Kabrda 45252f
            'comment': 'Tests running',
Slavek Kabrda 45252f
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Slavek Kabrda 45252f
            'uid': 'jenkins_build_pagure_100+seed',
Slavek Kabrda 45252f
        }
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        output = self.app.post(
Slavek Kabrda 45252f
            '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 45252f
        self.assertEqual(data['flag']['status'], 'pend!')
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # No status and 50 % => should use FLAG_SUCCESS
Slavek Kabrda 45252f
        send_data['percent'] = 50
Slavek Kabrda 45252f
        output = self.app.post(
Slavek Kabrda 45252f
            '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 45252f
        self.assertEqual(data['flag']['status'], 'succeed!')
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # No status and 0 % => should use FLAG_FAILURE
Slavek Kabrda 45252f
        send_data['percent'] = 0
Slavek Kabrda 45252f
        output = self.app.post(
Slavek Kabrda 45252f
            '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 45252f
        self.assertEqual(data['flag']['status'], 'fail!')
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # Explicitly set status
Slavek Kabrda 45252f
        send_data['status'] = 'what?'
Slavek Kabrda 45252f
        output = self.app.post(
Slavek Kabrda 45252f
            '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 45252f
        self.assertEqual(data['flag']['status'], 'what?')
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # Explicitly set wrong status
Slavek Kabrda 45252f
        send_data['status'] = 'nooo.....'
Slavek Kabrda 45252f
        output = self.app.post(
Slavek Kabrda 45252f
            '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 400)
Slavek Kabrda 45252f
        self.assertDictEqual(
Slavek Kabrda 45252f
            data,
Slavek Kabrda 45252f
            {
Slavek Kabrda 45252f
                "error": "Invalid or incomplete input submitted",
Slavek Kabrda 45252f
                "error_code": "EINVALIDREQ",
Slavek Kabrda 45252f
                "errors": {"status": ["Not a valid choice"]}
Slavek Kabrda 45252f
            }
Slavek Kabrda 45252f
        )
Slavek Kabrda 45252f
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
class PagureFlaskApiPRFlagUserTokentests(tests.Modeltests):
Pierre-Yves Chibon 3ffb2e
    """ Tests for the flask API of pagure for flagging pull-requests using
Pierre-Yves Chibon 3ffb2e
    an user token (ie: not restricted to a specific project).
Pierre-Yves Chibon 3ffb2e
    """
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    maxDiff = None
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 3ffb2e
    def setUp(self):
Pierre-Yves Chibon 3ffb2e
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 3ffb2e
        super(PagureFlaskApiPRFlagUserTokentests, self).setUp()
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        pagure.config.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        tests.create_projects(self.session)
Pierre-Yves Chibon 3ffb2e
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 3ffb2e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Create a pull-request
Pierre-Yves Chibon 3ffb2e
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 3ffb2e
        forked_repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 3ffb2e
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon 3ffb2e
            session=self.session,
Pierre-Yves Chibon 3ffb2e
            repo_from=forked_repo,
Pierre-Yves Chibon 3ffb2e
            branch_from='master',
Pierre-Yves Chibon 3ffb2e
            repo_to=repo,
Pierre-Yves Chibon 3ffb2e
            branch_to='master',
Pierre-Yves Chibon 3ffb2e
            title='test pull-request',
Pierre-Yves Chibon 3ffb2e
            user='pingou',
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Check flags before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_pr(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging a non-existing PR. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Invalid project
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/foo/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Project not found",
Pierre-Yves Chibon 3ffb2e
                "error_code": "ENOPROJECT",
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_pr_other_project(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging a non-existing PR on a different project. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
        # Valid token, wrong project
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test2/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Pull-Request not found",
Pierre-Yves Chibon 3ffb2e
                "error_code": "ENOREQ",
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_input(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR but without submitting any data. """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No input
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'error': 'Invalid or incomplete input submitted',
Aurélien Bompard 626417
                'error_code': 'EINVALIDREQ',
Aurélien Bompard 626417
                'errors': {
Aurélien Bompard 626417
                    'comment': ['This field is required.'],
Aurélien Bompard 626417
                    'url': ['This field is required.'],
Aurélien Bompard 626417
                    'username': ['This field is required.']
Pierre-Yves Chibon 3ffb2e
                }
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_comment(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR but without all the required info.
Pierre-Yves Chibon 3ffb2e
        """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'percent': 100,
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Incomplete request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 3ffb2e
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 3ffb2e
                "errors": {"comment": ["This field is required."]}
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_invalid_status(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR but with an invalid status.
Pierre-Yves Chibon 3ffb2e
        """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'status': 'failed',
Pierre-Yves Chibon 3ffb2e
            'comment': 'Failed to run the tests',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Invalid status submitted
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 3ffb2e
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 3ffb2e
                "errors": {"status": ["Not a valid choice"]}
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 3ffb2e
    def test_flag_pr_no_status(self, mock_email):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR without providing a status.
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        Also check that no notifications have been sent.
Pierre-Yves Chibon 3ffb2e
        """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'percent': 0,
Pierre-Yves Chibon 3ffb2e
            'comment': 'Tests failed',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request  -  w/o providing the status
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['flag']['date_created'] = '1510742565'
Aurélien Bompard 626417
        data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'flag': {
Aurélien Bompard 626417
                    'comment': 'Tests failed',
Aurélien Bompard 626417
                    'date_created': '1510742565',
Aurélien Bompard 626417
                    'percent': 0,
Aurélien Bompard 626417
                    'pull_request_uid': '62b49f00d489452994de5010565fab81',
Aurélien Bompard 626417
                    'status': 'failure',
Aurélien Bompard 626417
                    'url': 'http://jenkins.cloud.fedoraproject.org/',
Aurélien Bompard 626417
                    'user': {
Aurélien Bompard 626417
                        'default_email': 'bar@pingou.com',
Aurélien Bompard 626417
                        'emails': ['bar@pingou.com', 'foo@pingou.com'],
Aurélien Bompard 626417
                        'fullname': 'PY C',
Aurélien Bompard 626417
                        'name': 'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Aurélien Bompard 626417
                    'username': 'Jenkins'},
Aurélien Bompard 626417
                'message': 'Flag added',
Pierre-Yves Chibon e2c468
                'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Pierre-Yves Chibon e2c468
                'user': 'pingou'
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].comment, 'Tests failed')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # no notifications sent
Pierre-Yves Chibon 3ffb2e
        mock_email.assert_not_called()
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_editing_flag(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR without providing a status.
Pierre-Yves Chibon 3ffb2e
        """
Pierre-Yves Chibon 3ffb2e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'status': 'failure',
Pierre-Yves Chibon 3ffb2e
            'comment': 'Tests failed',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request  -  w/o providing the status
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['flag']['date_created'] = '1510742565'
Aurélien Bompard 626417
        data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'flag': {
Aurélien Bompard 626417
                    'comment': 'Tests failed',
Aurélien Bompard 626417
                    'date_created': '1510742565',
Aurélien Bompard 626417
                    'percent': None,
Aurélien Bompard 626417
                    'pull_request_uid': '62b49f00d489452994de5010565fab81',
Aurélien Bompard 626417
                    'status': 'failure',
Aurélien Bompard 626417
                    'url': 'http://jenkins.cloud.fedoraproject.org/',
Aurélien Bompard 626417
                    'user': {
Aurélien Bompard 626417
                        'default_email': 'bar@pingou.com',
Aurélien Bompard 626417
                        'emails': ['bar@pingou.com', 'foo@pingou.com'],
Aurélien Bompard 626417
                        'fullname': 'PY C',
Aurélien Bompard 626417
                        'name': 'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Aurélien Bompard 626417
                    'username': 'Jenkins'},
Aurélien Bompard 626417
                'message': 'Flag added',
Pierre-Yves Chibon e2c468
                'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Pierre-Yves Chibon e2c468
                'user': 'pingou'
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].comment, 'Tests failed')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, None)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Update flag
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 3ffb2e
            'username': 'Jenkins',
Pierre-Yves Chibon 3ffb2e
            'percent': 100,
Pierre-Yves Chibon 3ffb2e
            'comment': 'Tests passed',
Pierre-Yves Chibon 3ffb2e
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon 3ffb2e
            'status': 'success',
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 3ffb2e
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['flag']['date_created'] = '1510742565'
Aurélien Bompard 626417
        data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Aurélien Bompard 626417
                'flag': {
Aurélien Bompard 626417
                    'comment': 'Tests passed',
Aurélien Bompard 626417
                    'date_created': '1510742565',
Aurélien Bompard 626417
                    'percent': 100,
Aurélien Bompard 626417
                    'pull_request_uid': '62b49f00d489452994de5010565fab81',
Aurélien Bompard 626417
                    'status': 'success',
Aurélien Bompard 626417
                    'url': 'http://jenkins.cloud.fedoraproject.org/',
Aurélien Bompard 626417
                    'user': {
Aurélien Bompard 626417
                        'default_email': 'bar@pingou.com',
Aurélien Bompard 626417
                        'emails': ['bar@pingou.com', 'foo@pingou.com'],
Aurélien Bompard 626417
                        'fullname': 'PY C',
Aurélien Bompard 626417
                        'name': 'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Aurélien Bompard 626417
                    'username': 'Jenkins'},
Aurélien Bompard 626417
                'message': 'Flag updated',
Pierre-Yves Chibon e2c468
                'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Pierre-Yves Chibon e2c468
                'user': 'pingou'
Pierre-Yves Chibon 3ffb2e
            }
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Still only one flag
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon 3ffb2e
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].comment, 'Tests passed')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 100)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
if __name__ == '__main__':
Pierre-Yves Chibon 3ffb2e
    unittest.main(verbosity=2)