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
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
            requestfolder=None,
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
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 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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'error': u'Invalid or incomplete input submitted',
Pierre-Yves Chibon 3ffb2e
                u'error_code': u'EINVALIDREQ',
Pierre-Yves Chibon 3ffb2e
                u'errors': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': [u'This field is required.'],
Pierre-Yves Chibon 3ffb2e
                    u'url': [u'This field is required.'],
Pierre-Yves Chibon 3ffb2e
                    u'username': [u'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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
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
    @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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 3ffb2e
        pr_uid = data['flag']['pull_request_uid']
Pierre-Yves Chibon 3ffb2e
        data['flag']['pull_request_uid'] = u'62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'flag': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': u'Tests running',
Pierre-Yves Chibon 3ffb2e
                    u'date_created': u'1510742565',
Pierre-Yves Chibon 3ffb2e
                    u'percent': None,
Pierre-Yves Chibon 3ffb2e
                    u'pull_request_uid': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 3ffb2e
                    u'status': u'pending',
Pierre-Yves Chibon 3ffb2e
                    u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
                    u'user': {
Pierre-Yves Chibon 3ffb2e
                        u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
                        u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 3ffb2e
                        u'fullname': u'PY C',
Pierre-Yves Chibon 3ffb2e
                        u'name': u'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 3ffb2e
                    u'username': u'Jenkins'},
Pierre-Yves Chibon 3ffb2e
                u'message': u'Flag added',
Pierre-Yves Chibon 3ffb2e
                u'uid': u'jenkins_build_pagure_100+seed'
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(
Pierre-Yves Chibon 3ffb2e
            u'\nJenkins flagged the pull-request `test pull-request` '
Pierre-Yves Chibon 3ffb2e
            u'as pending: Tests running\n\n'
Pierre-Yves Chibon 3ffb2e
            u'https://pagure.org/test/pull-request/1\n',
Pierre-Yves Chibon 3ffb2e
            u'PR #1 - Jenkins: pending',
Pierre-Yves Chibon 3ffb2e
            u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
            in_reply_to=u'test-pull-request-' + pr_uid,
Pierre-Yves Chibon 3ffb2e
            mail_id=u'test-pull-request-' + pr_uid + '-1',
Pierre-Yves Chibon 3ffb2e
            project_name=u'test',
Pierre-Yves Chibon 3ffb2e
            user_from=u'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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 3ffb2e
        data['flag']['pull_request_uid'] = u'62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'flag': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': u'Tests running',
Pierre-Yves Chibon 3ffb2e
                    u'date_created': u'1510742565',
Pierre-Yves Chibon 3ffb2e
                    u'percent': None,
Pierre-Yves Chibon 3ffb2e
                    u'pull_request_uid': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 3ffb2e
                    u'status': u'pending',
Pierre-Yves Chibon 3ffb2e
                    u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
                    u'user': {
Pierre-Yves Chibon 3ffb2e
                        u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
                        u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 3ffb2e
                        u'fullname': u'PY C',
Pierre-Yves Chibon 3ffb2e
                        u'name': u'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 3ffb2e
                    u'username': u'Jenkins'},
Pierre-Yves Chibon 3ffb2e
                u'message': u'Flag added',
Pierre-Yves Chibon 3ffb2e
                u'uid': u'jenkins_build_pagure_100+seed'
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 3ffb2e
        data['flag']['pull_request_uid'] = u'62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'flag': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': u'Tests passed',
Pierre-Yves Chibon 3ffb2e
                    u'date_created': u'1510742565',
Pierre-Yves Chibon 3ffb2e
                    u'percent': 100,
Pierre-Yves Chibon 3ffb2e
                    u'pull_request_uid': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 3ffb2e
                    u'status': u'success',
Pierre-Yves Chibon 3ffb2e
                    u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
                    u'user': {
Pierre-Yves Chibon 3ffb2e
                        u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
                        u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 3ffb2e
                        u'fullname': u'PY C',
Pierre-Yves Chibon 3ffb2e
                        u'name': u'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 3ffb2e
                    u'username': u'Jenkins'},
Pierre-Yves Chibon 3ffb2e
                u'message': u'Flag updated',
Pierre-Yves Chibon 3ffb2e
                u'uid': u'jenkins_build_pagure_100+seed'
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 3ffb2e
        data['flag']['pull_request_uid'] = u'62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'flag': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': u'Tests passed',
Pierre-Yves Chibon 3ffb2e
                    u'date_created': u'1510742565',
Pierre-Yves Chibon 3ffb2e
                    u'percent': 100,
Pierre-Yves Chibon 3ffb2e
                    u'pull_request_uid': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 3ffb2e
                    u'status': u'success',
Pierre-Yves Chibon 3ffb2e
                    u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
                    u'user': {
Pierre-Yves Chibon 3ffb2e
                        u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
                        u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 3ffb2e
                        u'fullname': u'PY C',
Pierre-Yves Chibon 3ffb2e
                        u'name': u'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 3ffb2e
                    u'username': u'Jenkins'},
Pierre-Yves Chibon 3ffb2e
                u'message': u'Flag added',
Pierre-Yves Chibon 3ffb2e
                u'uid': u'jenkins_build_pagure_100+seed'
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 3ffb2e
        data['flag']['pull_request_uid'] = u'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 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'flag': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': u'Tests running again',
Pierre-Yves Chibon 3ffb2e
                    u'date_created': u'1510742565',
Pierre-Yves Chibon 3ffb2e
                    u'percent': None,
Pierre-Yves Chibon 3ffb2e
                    u'pull_request_uid': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 3ffb2e
                    u'status': u'pending',
Pierre-Yves Chibon 3ffb2e
                    u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
                    u'user': {
Pierre-Yves Chibon 3ffb2e
                        u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
                        u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 3ffb2e
                        u'fullname': u'PY C',
Pierre-Yves Chibon 3ffb2e
                        u'name': u'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 3ffb2e
                    u'username': u'Jenkins'},
Pierre-Yves Chibon 3ffb2e
                u'message': u'Flag added',
Pierre-Yves Chibon 3ffb2e
                u'uid': u'jenkins_build_pagure_100+seed'
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 3ffb2e
        self.assertEqual(request.flags[0].comment, 'Tests passed')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 100)
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[1].comment, 'Tests running again')
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[1].percent, None)
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)
Slavek Kabrda 45252f
        data = json.loads(output.data)
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)
Slavek Kabrda 45252f
        data = json.loads(output.data)
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)
Slavek Kabrda 45252f
        data = json.loads(output.data)
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)
Slavek Kabrda 45252f
        data = json.loads(output.data)
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)
Slavek Kabrda 45252f
        data = json.loads(output.data)
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
            requestfolder=None,
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'error': u'Invalid or incomplete input submitted',
Pierre-Yves Chibon 3ffb2e
                u'error_code': u'EINVALIDREQ',
Pierre-Yves Chibon 3ffb2e
                u'errors': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': [u'This field is required.'],
Pierre-Yves Chibon 3ffb2e
                    u'url': [u'This field is required.'],
Pierre-Yves Chibon 3ffb2e
                    u'username': [u'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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 3ffb2e
        data['flag']['pull_request_uid'] = u'62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'flag': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': u'Tests failed',
Pierre-Yves Chibon 3ffb2e
                    u'date_created': u'1510742565',
Pierre-Yves Chibon 3ffb2e
                    u'percent': 0,
Pierre-Yves Chibon 3ffb2e
                    u'pull_request_uid': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 3ffb2e
                    u'status': u'failure',
Pierre-Yves Chibon 3ffb2e
                    u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
                    u'user': {
Pierre-Yves Chibon 3ffb2e
                        u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
                        u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 3ffb2e
                        u'fullname': u'PY C',
Pierre-Yves Chibon 3ffb2e
                        u'name': u'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 3ffb2e
                    u'username': u'Jenkins'},
Pierre-Yves Chibon 3ffb2e
                u'message': u'Flag added',
Pierre-Yves Chibon 3ffb2e
                u'uid': u'jenkins_build_pagure_100+seed'
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 3ffb2e
        data['flag']['pull_request_uid'] = u'62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'flag': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': u'Tests failed',
Pierre-Yves Chibon 3ffb2e
                    u'date_created': u'1510742565',
Pierre-Yves Chibon 3ffb2e
                    u'percent': None,
Pierre-Yves Chibon 3ffb2e
                    u'pull_request_uid': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 3ffb2e
                    u'status': u'failure',
Pierre-Yves Chibon 3ffb2e
                    u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
                    u'user': {
Pierre-Yves Chibon 3ffb2e
                        u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
                        u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 3ffb2e
                        u'fullname': u'PY C',
Pierre-Yves Chibon 3ffb2e
                        u'name': u'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 3ffb2e
                    u'username': u'Jenkins'},
Pierre-Yves Chibon 3ffb2e
                u'message': u'Flag added',
Pierre-Yves Chibon 3ffb2e
                u'uid': u'jenkins_build_pagure_100+seed'
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)
Pierre-Yves Chibon 3ffb2e
        data = json.loads(output.data)
Pierre-Yves Chibon 3ffb2e
        data['flag']['date_created'] = u'1510742565'
Pierre-Yves Chibon 3ffb2e
        data['flag']['pull_request_uid'] = u'62b49f00d489452994de5010565fab81'
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                u'flag': {
Pierre-Yves Chibon 3ffb2e
                    u'comment': u'Tests passed',
Pierre-Yves Chibon 3ffb2e
                    u'date_created': u'1510742565',
Pierre-Yves Chibon 3ffb2e
                    u'percent': 100,
Pierre-Yves Chibon 3ffb2e
                    u'pull_request_uid': u'62b49f00d489452994de5010565fab81',
Pierre-Yves Chibon 3ffb2e
                    u'status': u'success',
Pierre-Yves Chibon 3ffb2e
                    u'url': u'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon 3ffb2e
                    u'user': {
Pierre-Yves Chibon 3ffb2e
                        u'default_email': u'bar@pingou.com',
Pierre-Yves Chibon 3ffb2e
                        u'emails': [u'bar@pingou.com', u'foo@pingou.com'],
Pierre-Yves Chibon 3ffb2e
                        u'fullname': u'PY C',
Pierre-Yves Chibon 3ffb2e
                        u'name': u'pingou'
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 3ffb2e
                    u'username': u'Jenkins'},
Pierre-Yves Chibon 3ffb2e
                u'message': u'Flag updated',
Pierre-Yves Chibon 3ffb2e
                u'uid': u'jenkins_build_pagure_100+seed'
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)