Blame tests/test_pagure_flask_api_fork.py

Pierre-Yves Chibon c71c2d
# -*- coding: utf-8 -*-
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
"""
Pierre-Yves Chibon c71c2d
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
 Authors:
Pierre-Yves Chibon c71c2d
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
"""
Pierre-Yves Chibon c71c2d
Aurélien Bompard 626417
from __future__ import unicode_literals
Aurélien Bompard 626417
Pierre-Yves Chibon c71c2d
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon c71c2d
import pkg_resources
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
import datetime
Pierre-Yves Chibon c71c2d
import unittest
Pierre-Yves Chibon c71c2d
import shutil
Pierre-Yves Chibon c71c2d
import sys
Pierre-Yves Chibon c71c2d
import os
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
import json
Pierre-Yves Chibon 9811e6
from mock import patch, MagicMock
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon c71c2d
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon c71c2d
import tests
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
class PagureFlaskApiForktests(tests.Modeltests):
Pierre-Yves Chibon c71c2d
    """ Tests for the flask API of pagure for issue """
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c27d61
    maxDiff = None
Pierre-Yves Chibon c27d61
Pierre-Yves Chibon c71c2d
    def setUp(self):
Pierre-Yves Chibon c71c2d
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon c71c2d
        super(PagureFlaskApiForktests, self).setUp()
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon b130e5
        pagure.config.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon fe796e
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon fe796e
    def test_api_pull_request_views_pr_disabled(self):
Pierre-Yves Chibon fe796e
        """ Test the api_pull_request_views method of the flask api when PR
Pierre-Yves Chibon fe796e
        are disabled. """
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        tests.create_projects(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon fe796e
            self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fe796e
            session=self.session,
Pierre-Yves Chibon fe796e
            repo_from=forked_repo,
Pierre-Yves Chibon fe796e
            branch_from='master',
Pierre-Yves Chibon fe796e
            repo_to=repo,
Pierre-Yves Chibon fe796e
            branch_to='master',
Pierre-Yves Chibon fe796e
            title='test pull-request',
Pierre-Yves Chibon fe796e
            user='pingou',
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.session.commit()
Pierre-Yves Chibon fe796e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon fe796e
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fe796e
        settings = repo.settings
Pierre-Yves Chibon fe796e
        settings['pull_requests'] = False
Pierre-Yves Chibon fe796e
        repo.settings = settings
Pierre-Yves Chibon fe796e
        self.session.add(repo)
Pierre-Yves Chibon fe796e
        self.session.commit()
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        output = self.app.get('/api/0/test/pull-requests')
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon fe796e
            data,
Pierre-Yves Chibon fe796e
            {
Pierre-Yves Chibon fe796e
                u'error': u'Pull-Request have been deactivated for this project',
Pierre-Yves Chibon fe796e
                u'error_code': u'EPULLREQUESTSDISABLED'
Pierre-Yves Chibon fe796e
            }
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon fe796e
    def test_api_pull_request_views_pr_closed(self):
Pierre-Yves Chibon fe796e
        """ Test the api_pull_request_views method of the flask api to list
Pierre-Yves Chibon fe796e
        the closed PRs. """
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        tests.create_projects(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon fe796e
            self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fe796e
            session=self.session,
Pierre-Yves Chibon fe796e
            repo_from=forked_repo,
Pierre-Yves Chibon fe796e
            branch_from='master',
Pierre-Yves Chibon fe796e
            repo_to=repo,
Pierre-Yves Chibon fe796e
            branch_to='master',
Pierre-Yves Chibon fe796e
            title='test pull-request',
Pierre-Yves Chibon fe796e
            user='pingou',
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.session.commit()
Pierre-Yves Chibon fe796e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon fe796e
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        output = self.app.get('/api/0/test/pull-requests?status=closed')
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Karsten Hopp 566852
        for k in ['first', 'last']:
Karsten Hopp 566852
            self.assertIsNotNone(data['pagination'][k])
Karsten Hopp 566852
            data['pagination'][k] = 'http://localhost...'
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon fe796e
            data,
Pierre-Yves Chibon fe796e
            {
Karsten Hopp 566852
                u'args': {
Karsten Hopp 566852
                    u'assignee': None,
Karsten Hopp 566852
                    u'author': None,
Karsten Hopp 566852
                    u'page': 1,
Karsten Hopp 566852
                    u'per_page': 20,
Karsten Hopp 566852
                    u'status': u'closed'
Karsten Hopp 566852
                },
Karsten Hopp 566852
                u'pagination': {
Karsten Hopp 566852
                    u'first': u'http://localhost...',
Karsten Hopp 566852
                    u'last': u'http://localhost...',
Karsten Hopp 566852
                    u'next': None,
Karsten Hopp 566852
                    u'page': 1,
Karsten Hopp 566852
                    u'pages': 0,
Karsten Hopp 566852
                    u'per_page': 20,
Karsten Hopp 566852
                    u'prev': None
Karsten Hopp 566852
                },
Pierre-Yves Chibon fe796e
                u'requests': [],
Pierre-Yves Chibon fe796e
                u'total_requests': 0
Pierre-Yves Chibon fe796e
            }
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Close the PR and try again
Pierre-Yves Chibon 930073
        pagure.lib.query.close_pull_request(
Patrick Uiterwijk 3f97f6
            self.session, request=req, user='pingou',
Pierre-Yves Chibon fe796e
            merged=False)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        output = self.app.get('/api/0/test/pull-requests?status=closed')
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fe796e
        self.assertEqual(
Pierre-Yves Chibon dd03b1
            sorted(data.keys()),
Pierre-Yves Chibon dd03b1
            ['args', 'pagination', 'requests', 'total_requests'])
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon fe796e
            data['args'],
Karsten Hopp 566852
            {
Karsten Hopp 566852
                u'assignee': None,
Karsten Hopp 566852
                u'author': None,
Karsten Hopp 566852
                u'page': 1,
Karsten Hopp 566852
                u'per_page': 20,
Karsten Hopp 566852
                u'status': u'closed'
Karsten Hopp 566852
            }
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.assertEqual(data['total_requests'], 1)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon fe796e
    def test_api_pull_request_views_all_pr(self):
Pierre-Yves Chibon fe796e
        """ Test the api_pull_request_views method of the flask api to list
Pierre-Yves Chibon fe796e
        all PRs. """
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        tests.create_projects(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon fe796e
            self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fe796e
            session=self.session,
Pierre-Yves Chibon fe796e
            repo_from=forked_repo,
Pierre-Yves Chibon fe796e
            branch_from='master',
Pierre-Yves Chibon fe796e
            repo_to=repo,
Pierre-Yves Chibon fe796e
            branch_to='master',
Pierre-Yves Chibon fe796e
            title='test pull-request',
Pierre-Yves Chibon fe796e
            user='pingou',
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.session.commit()
Pierre-Yves Chibon fe796e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon fe796e
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        output = self.app.get('/api/0/test/pull-requests?status=all')
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fe796e
        self.assertEqual(
Pierre-Yves Chibon dd03b1
            sorted(data.keys()),
Pierre-Yves Chibon dd03b1
            ['args', 'pagination', 'requests', 'total_requests'])
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon fe796e
            data['args'],
Karsten Hopp 566852
            {
Karsten Hopp 566852
                u'assignee': None,
Karsten Hopp 566852
                u'author': None,
Karsten Hopp 566852
                u'page': 1,
Karsten Hopp 566852
                u'per_page': 20,
Karsten Hopp 566852
                u'status': u'all'
Karsten Hopp 566852
            }
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.assertEqual(data['total_requests'], 1)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Close the PR and try again
Pierre-Yves Chibon 930073
        pagure.lib.query.close_pull_request(
Patrick Uiterwijk 3f97f6
            self.session, request=req, user='pingou',
Pierre-Yves Chibon fe796e
            merged=False)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        output = self.app.get('/api/0/test/pull-requests?status=all')
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fe796e
        self.assertEqual(
Pierre-Yves Chibon dd03b1
            sorted(data.keys()),
Pierre-Yves Chibon dd03b1
            ['args', 'pagination', 'requests', 'total_requests'])
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon fe796e
            data['args'],
Karsten Hopp 566852
            {
Karsten Hopp 566852
                u'assignee': None,
Karsten Hopp 566852
                u'author': None,
Karsten Hopp 566852
                u'page': 1,
Karsten Hopp 566852
                u'per_page': 20,
Karsten Hopp 566852
                u'status': u'all'
Karsten Hopp 566852
            }
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.assertEqual(data['total_requests'], 1)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 7cc2d4
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 7cc2d4
    def test_api_pull_request_views(self, send_email):
Pierre-Yves Chibon ad85b9
        """ Test the api_pull_request_views method of the flask api. """
Pierre-Yves Chibon 7cc2d4
        send_email.return_value = True
Pierre-Yves Chibon 7cc2d4
Pierre-Yves Chibon ad85b9
        tests.create_projects(self.session)
Pierre-Yves Chibon ad85b9
        tests.create_tokens(self.session)
Pierre-Yves Chibon ad85b9
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon b130e5
            self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon ad85b9
            session=self.session,
Pierre-Yves Chibon ad85b9
            repo_from=forked_repo,
Pierre-Yves Chibon ad85b9
            branch_from='master',
Pierre-Yves Chibon ad85b9
            repo_to=repo,
Pierre-Yves Chibon ad85b9
            branch_to='master',
Pierre-Yves Chibon ad85b9
            title='test pull-request',
Pierre-Yves Chibon ad85b9
            user='pingou',
Pierre-Yves Chibon ad85b9
        )
Pierre-Yves Chibon ad85b9
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        # Invalid repo
Pierre-Yves Chibon ad85b9
        output = self.app.get('/api/0/foo/pull-requests')
Pierre-Yves Chibon ad85b9
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ad85b9
        self.assertDictEqual(
Pierre-Yves Chibon ad85b9
            data,
Pierre-Yves Chibon ad85b9
            {
Pierre-Yves Chibon ad85b9
              "error": "Project not found",
Pierre-Yves Chibon ad85b9
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon ad85b9
            }
Pierre-Yves Chibon ad85b9
        )
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        # List pull-requests
Pierre-Yves Chibon ad85b9
        output = self.app.get('/api/0/test/pull-requests')
Pierre-Yves Chibon ad85b9
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ad85b9
        data['requests'][0]['date_created'] = '1431414800'
Pierre-Yves Chibon 44258a
        data['requests'][0]['updated_on'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data['requests'][0]['project']['date_created'] = '1431414800'
Clement Verna cd6e30
        data['requests'][0]['project']['date_modified'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data['requests'][0]['repo_from']['date_created'] = '1431414800'
Clement Verna cd6e30
        data['requests'][0]['repo_from']['date_modified'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data['requests'][0]['uid'] = '1431414800'
Mark Reynolds 23b4d2
        data['requests'][0]['last_updated'] = '1431414800'
Karsten Hopp 566852
        for k in ['first', 'last']:
Karsten Hopp 566852
            self.assertIsNotNone(data['pagination'][k])
Karsten Hopp 566852
            data['pagination'][k] = 'http://localhost...'
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl 6bf79a
            "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Karsten Hopp 566852
                "page": 1,
Karsten Hopp 566852
                "per_page": 20,
Pierre-Yves Chibon 316f15
                "status": True
Matt Prahl 6bf79a
            },
Karsten Hopp 566852
            'pagination': {
Karsten Hopp 566852
              "first": 'http://localhost...',
Karsten Hopp 566852
              "last": 'http://localhost...',
Karsten Hopp 566852
              "next": None,
Karsten Hopp 566852
              "page": 1,
Karsten Hopp 566852
              "pages": 1,
Karsten Hopp 566852
              "per_page": 20,
Karsten Hopp 566852
              "prev": None
Karsten Hopp 566852
            },
Matt Prahl 6bf79a
            "requests": [{
Matt Prahl 6bf79a
                "assignee": None,
Matt Prahl 6bf79a
                "branch": "master",
Matt Prahl 6bf79a
                "branch_from": "master",
Pierre-Yves Chibon f21734
                "cached_merge_status": "unknown",
Matt Prahl 6bf79a
                "closed_at": None,
Matt Prahl 6bf79a
                "closed_by": None,
Matt Prahl 6bf79a
                "comments": [],
Matt Prahl 6bf79a
                "commit_start": None,
Matt Prahl 6bf79a
                "commit_stop": None,
Matt Prahl 6bf79a
                "date_created": "1431414800",
Matt Prahl 6bf79a
                "id": 1,
Matt Prahl 6bf79a
                "initial_comment": None,
Matt Prahl 6bf79a
                "last_updated": "1431414800",
Matt Prahl 6bf79a
                "project": {
Matt Prahl 6bf79a
                    "access_groups": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Pierre-Yves Chibon f254bf
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Matt Prahl 6bf79a
                        "Duplicate"
Pierre-Yves Chibon f254bf
                    ],
Pierre-Yves Chibon 022564
                    "custom_keys": [],
Pierre-Yves Chibon ad85b9
                    "date_created": "1431414800",
Clement Verna cd6e30
                    "date_modified": "1431414800",
Pierre-Yves Chibon ad85b9
                    "description": "test project #1",
Matt Prahl 6bf79a
                    "fullname": "test",
Pierre-Yves Chibon 469e24
                    "url_path": "test",
Pierre-Yves Chibon ad85b9
                    "id": 1,
Pierre-Yves Chibon f254bf
                    "milestones": {},
Pierre-Yves Chibon ad85b9
                    "name": "test",
Pierre-Yves Chibon 733a9d
                    "namespace": None,
Pierre-Yves Chibon ad85b9
                    "parent": None,
Pierre-Yves Chibon 14bde2
                    "priorities": {},
Pierre-Yves Chibon a30916
                    "tags": [],
Pierre-Yves Chibon ad85b9
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Pierre-Yves Chibon ad85b9
                    }
Matt Prahl 6bf79a
                },
Matt Prahl 6bf79a
                "remote_git": None,
Matt Prahl 6bf79a
                "repo_from": {
Matt Prahl 6bf79a
                    "access_groups": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "ticket": []},
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Pierre-Yves Chibon f254bf
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Matt Prahl 6bf79a
                        "Duplicate"
Pierre-Yves Chibon f254bf
                    ],
Pierre-Yves Chibon 022564
                    "custom_keys": [],
Pierre-Yves Chibon ad85b9
                    "date_created": "1431414800",
Clement Verna cd6e30
                    "date_modified": "1431414800",
Pierre-Yves Chibon ad85b9
                    "description": "test project #1",
Matt Prahl 6bf79a
                    "fullname": "test",
Pierre-Yves Chibon 469e24
                    "url_path": "test",
Pierre-Yves Chibon ad85b9
                    "id": 1,
Pierre-Yves Chibon f254bf
                    "milestones": {},
Pierre-Yves Chibon ad85b9
                    "name": "test",
Pierre-Yves Chibon 733a9d
                    "namespace": None,
Pierre-Yves Chibon ad85b9
                    "parent": None,
Pierre-Yves Chibon 14bde2
                    "priorities": {},
Pierre-Yves Chibon a30916
                    "tags": [],
Pierre-Yves Chibon ad85b9
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Pierre-Yves Chibon ad85b9
                    }
Matt Prahl 6bf79a
                },
Matt Prahl 6bf79a
                "status": "Open",
Matt Prahl 6bf79a
                "title": "test pull-request",
Matt Prahl 6bf79a
                "uid": "1431414800",
Matt Prahl 6bf79a
                "updated_on": "1431414800",
Matt Prahl 6bf79a
                "user": {
Pierre-Yves Chibon ad85b9
                    "fullname": "PY C",
Pierre-Yves Chibon ad85b9
                    "name": "pingou"
Pierre-Yves Chibon ad85b9
                }
Matt Prahl 6bf79a
            }],
Matt Prahl 6bf79a
            "total_requests": 1
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        # Access Pull-Request authenticated
Pierre-Yves Chibon ad85b9
        output = self.app.get('/api/0/test/pull-requests', headers=headers)
Pierre-Yves Chibon ad85b9
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ad85b9
        data2['requests'][0]['date_created'] = '1431414800'
Pierre-Yves Chibon 44258a
        data2['requests'][0]['updated_on'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data2['requests'][0]['project']['date_created'] = '1431414800'
Clement Verna cd6e30
        data2['requests'][0]['project']['date_modified'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data2['requests'][0]['repo_from']['date_created'] = '1431414800'
Clement Verna cd6e30
        data2['requests'][0]['repo_from']['date_modified'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data2['requests'][0]['uid'] = '1431414800'
Mark Reynolds 23b4d2
        data2['requests'][0]['last_updated'] = '1431414800'
Pierre-Yves Chibon dd03b1
        for k in ['first', 'last']:
Pierre-Yves Chibon dd03b1
            self.assertIsNotNone(data['pagination'][k])
Pierre-Yves Chibon dd03b1
            data2['pagination'][k] = 'http://localhost...'
Pierre-Yves Chibon ad85b9
        self.assertDictEqual(data, data2)
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon 7cc2d4
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 417805
    def test_api_pull_request_view_pr_disabled(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_view method of the flask api. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 417805
            branch_from='master',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 417805
        settings['pull_requests'] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.get('/api/0/test/pull-request/1')
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'Pull-Request have been deactivated for this project',
Pierre-Yves Chibon 417805
                u'error_code': u'EPULLREQUESTSDISABLED'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 7cc2d4
    def test_api_pull_request_view(self, send_email):
Pierre-Yves Chibon c71c2d
        """ Test the api_pull_request_view method of the flask api. """
Pierre-Yves Chibon 7cc2d4
        send_email.return_value = True
Pierre-Yves Chibon c71c2d
        tests.create_projects(self.session)
Pierre-Yves Chibon c71c2d
        tests.create_tokens(self.session)
Pierre-Yves Chibon c71c2d
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon c71c2d
            session=self.session,
Pierre-Yves Chibon c71c2d
            repo_from=forked_repo,
Pierre-Yves Chibon c71c2d
            branch_from='master',
Pierre-Yves Chibon c71c2d
            repo_to=repo,
Pierre-Yves Chibon c71c2d
            branch_to='master',
Pierre-Yves Chibon c71c2d
            title='test pull-request',
Pierre-Yves Chibon c71c2d
            user='pingou',
Pierre-Yves Chibon c71c2d
        )
Pierre-Yves Chibon c71c2d
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Invalid repo
Pierre-Yves Chibon c71c2d
        output = self.app.get('/api/0/foo/pull-request/1')
Pierre-Yves Chibon c71c2d
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(
Pierre-Yves Chibon c71c2d
            data,
Pierre-Yves Chibon c71c2d
            {
Pierre-Yves Chibon c71c2d
              "error": "Project not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon c71c2d
            }
Pierre-Yves Chibon c71c2d
        )
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Invalid issue for this repo
Pierre-Yves Chibon c71c2d
        output = self.app.get('/api/0/test2/pull-request/1')
Pierre-Yves Chibon c71c2d
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(
Pierre-Yves Chibon c71c2d
            data,
Pierre-Yves Chibon c71c2d
            {
Pierre-Yves Chibon c71c2d
              "error": "Pull-Request not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOREQ",
Pierre-Yves Chibon c71c2d
            }
Pierre-Yves Chibon c71c2d
        )
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Valid issue
Pierre-Yves Chibon c71c2d
        output = self.app.get('/api/0/test/pull-request/1')
Pierre-Yves Chibon c71c2d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c71c2d
        data['date_created'] = '1431414800'
Pierre-Yves Chibon 44258a
        data['updated_on'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data['project']['date_created'] = '1431414800'
Clement Verna cd6e30
        data['project']['date_modified'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data['repo_from']['date_created'] = '1431414800'
Clement Verna cd6e30
        data['repo_from']['date_modified'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data['uid'] = '1431414800'
Mark Reynolds 23b4d2
        data['last_updated'] = '1431414800'
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl 6bf79a
            "assignee": None,
Matt Prahl 6bf79a
            "branch": "master",
Matt Prahl 6bf79a
            "branch_from": "master",
Pierre-Yves Chibon f21734
            "cached_merge_status": "unknown",
Matt Prahl 6bf79a
            "closed_at": None,
Matt Prahl 6bf79a
            "closed_by": None,
Matt Prahl 6bf79a
            "comments": [],
Matt Prahl 6bf79a
            "commit_start": None,
Matt Prahl 6bf79a
            "commit_stop": None,
Matt Prahl 6bf79a
            "date_created": "1431414800",
Matt Prahl 6bf79a
            "id": 1,
Matt Prahl 6bf79a
            "initial_comment": None,
Matt Prahl 6bf79a
            "last_updated": "1431414800",
Matt Prahl 6bf79a
            "project": {
Matt Prahl 6bf79a
                "access_groups": {
Matt Prahl 6bf79a
                    "admin": [],
Matt Prahl 6bf79a
                    "commit": [],
Matt Prahl 6bf79a
                    "ticket": []
Matt Prahl 6bf79a
                },
Matt Prahl 6bf79a
                "access_users": {
Matt Prahl 6bf79a
                    "admin": [],
Matt Prahl 6bf79a
                    "commit": [],
Matt Prahl 6bf79a
                    "owner": ["pingou"],
Matt Prahl 6bf79a
                    "ticket": []
Matt Prahl 6bf79a
                },
Pierre-Yves Chibon f254bf
                "close_status": [
Matt Prahl 6bf79a
                    "Invalid",
Matt Prahl 6bf79a
                    "Insufficient data",
Matt Prahl 6bf79a
                    "Fixed",
Matt Prahl 6bf79a
                    "Duplicate"
Pierre-Yves Chibon f254bf
                ],
Pierre-Yves Chibon 022564
                "custom_keys": [],
Pierre-Yves Chibon c71c2d
                "date_created": "1431414800",
Clement Verna cd6e30
                "date_modified": "1431414800",
Pierre-Yves Chibon c71c2d
                "description": "test project #1",
Pierre-Yves Chibon c9e619
                "fullname": "test",
Pierre-Yves Chibon 469e24
                "url_path": "test",
Pierre-Yves Chibon c71c2d
                "id": 1,
Pierre-Yves Chibon f254bf
                "milestones": {},
Pierre-Yves Chibon c71c2d
                "name": "test",
Pierre-Yves Chibon 733a9d
                "namespace": None,
Pierre-Yves Chibon c71c2d
                "parent": None,
Pierre-Yves Chibon 14bde2
                "priorities": {},
Pierre-Yves Chibon a30916
                "tags": [],
Pierre-Yves Chibon c71c2d
                "user": {
Matt Prahl 6bf79a
                    "fullname": "PY C",
Matt Prahl 6bf79a
                    "name": "pingou"
Pierre-Yves Chibon c71c2d
                }
Matt Prahl 6bf79a
            },
Matt Prahl 6bf79a
            "remote_git": None,
Matt Prahl 6bf79a
            "repo_from": {
Matt Prahl 6bf79a
                "access_groups": {
Matt Prahl 6bf79a
                    "admin": [],
Matt Prahl 6bf79a
                    "commit": [],
Matt Prahl 6bf79a
                    "ticket": []},
Matt Prahl 6bf79a
                "access_users": {
Matt Prahl 6bf79a
                    "admin": [],
Matt Prahl 6bf79a
                    "commit": [],
Matt Prahl 6bf79a
                    "owner": ["pingou"],
Matt Prahl 6bf79a
                    "ticket": []},
Matt Prahl 6bf79a
                "close_status": [
Matt Prahl 6bf79a
                    "Invalid",
Matt Prahl 6bf79a
                    "Insufficient data",
Matt Prahl 6bf79a
                    "Fixed",
Matt Prahl 6bf79a
                    "Duplicate"],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1431414800",
Clement Verna cd6e30
                    "date_modified": "1431414800",
Matt Prahl 6bf79a
                    "description": "test project #1",
Matt Prahl 6bf79a
                    "fullname": "test",
Pierre-Yves Chibon 469e24
                    "url_path": "test",
Matt Prahl 6bf79a
                    "id": 1,
Matt Prahl 6bf79a
                    "milestones": {},
Matt Prahl 6bf79a
                    "name": "test",
Matt Prahl 6bf79a
                    "namespace": None,
Matt Prahl 6bf79a
                    "parent": None,
Matt Prahl 6bf79a
                    "priorities": {},
Matt Prahl 6bf79a
                    "tags": [],
Matt Prahl 6bf79a
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Matt Prahl 6bf79a
                    }
Matt Prahl 6bf79a
            },
Matt Prahl 6bf79a
            "status": "Open",
Matt Prahl 6bf79a
            "title": "test pull-request",
Matt Prahl 6bf79a
            "uid": "1431414800",
Matt Prahl 6bf79a
            "updated_on": "1431414800",
Matt Prahl 6bf79a
            "user": {
Pierre-Yves Chibon c71c2d
                "fullname": "PY C",
Pierre-Yves Chibon c71c2d
                "name": "pingou"
Pierre-Yves Chibon c71c2d
            }
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Access Pull-Request authenticated
Pierre-Yves Chibon c71c2d
        output = self.app.get('/api/0/test/pull-request/1', headers=headers)
Pierre-Yves Chibon c71c2d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c71c2d
        data2['date_created'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data2['project']['date_created'] = '1431414800'
Clement Verna cd6e30
        data2['project']['date_modified'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data2['repo_from']['date_created'] = '1431414800'
Clement Verna cd6e30
        data2['repo_from']['date_modified'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data2['uid'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data2['date_created'] = '1431414800'
Pierre-Yves Chibon 44258a
        data2['updated_on'] = '1431414800'
Mark Reynolds 23b4d2
        data2['last_updated'] = '1431414800'
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(data, data2)
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 7cc2d4
    @patch('pagure.lib.notify.send_email')
Slavek Kabrda 1a6e21
    def test_api_pull_request_by_uid_view(self, send_email):
Slavek Kabrda 1a6e21
        """ Test the api_pull_request_by_uid_view method of the flask api. """
Slavek Kabrda 1a6e21
        send_email.return_value = True
Slavek Kabrda 1a6e21
        tests.create_projects(self.session)
Slavek Kabrda 1a6e21
        tests.create_tokens(self.session)
Slavek Kabrda 1a6e21
        tests.create_tokens_acl(self.session)
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Slavek Kabrda 1a6e21
            session=self.session,
Slavek Kabrda 1a6e21
            repo_from=forked_repo,
Slavek Kabrda 1a6e21
            branch_from='master',
Slavek Kabrda 1a6e21
            repo_to=repo,
Slavek Kabrda 1a6e21
            branch_to='master',
Slavek Kabrda 1a6e21
            title='test pull-request',
Slavek Kabrda 1a6e21
            user='pingou',
Slavek Kabrda 1a6e21
        )
Slavek Kabrda 1a6e21
        self.session.commit()
Slavek Kabrda 1a6e21
        self.assertEqual(req.id, 1)
Slavek Kabrda 1a6e21
        self.assertEqual(req.title, 'test pull-request')
Slavek Kabrda 1a6e21
        uid = req.uid
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        # Invalid request
Slavek Kabrda 1a6e21
        output = self.app.get('/api/0/pull-requests/{}'.format(uid + 'aaa'))
Slavek Kabrda 1a6e21
        self.assertEqual(output.status_code, 404)
Slavek Kabrda 1a6e21
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 1a6e21
        self.assertDictEqual(
Slavek Kabrda 1a6e21
            data,
Slavek Kabrda 1a6e21
            {
Slavek Kabrda 1a6e21
              "error": "Pull-Request not found",
Slavek Kabrda 1a6e21
              "error_code": "ENOREQ",
Slavek Kabrda 1a6e21
            }
Slavek Kabrda 1a6e21
        )
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        # Valid issue
Slavek Kabrda 1a6e21
        output = self.app.get('/api/0/pull-requests/{}'.format(uid))
Slavek Kabrda 1a6e21
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 1a6e21
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 1a6e21
        data['date_created'] = '1431414800'
Slavek Kabrda 1a6e21
        data['updated_on'] = '1431414800'
Slavek Kabrda 1a6e21
        data['project']['date_created'] = '1431414800'
Slavek Kabrda 1a6e21
        data['project']['date_modified'] = '1431414800'
Slavek Kabrda 1a6e21
        data['repo_from']['date_created'] = '1431414800'
Slavek Kabrda 1a6e21
        data['repo_from']['date_modified'] = '1431414800'
Slavek Kabrda 1a6e21
        data['last_updated'] = '1431414800'
Slavek Kabrda 1a6e21
        expected_data = {
Slavek Kabrda 1a6e21
            "assignee": None,
Slavek Kabrda 1a6e21
            "branch": "master",
Slavek Kabrda 1a6e21
            "branch_from": "master",
Slavek Kabrda 1a6e21
            "cached_merge_status": "unknown",
Slavek Kabrda 1a6e21
            "closed_at": None,
Slavek Kabrda 1a6e21
            "closed_by": None,
Slavek Kabrda 1a6e21
            "comments": [],
Slavek Kabrda 1a6e21
            "commit_start": None,
Slavek Kabrda 1a6e21
            "commit_stop": None,
Slavek Kabrda 1a6e21
            "date_created": "1431414800",
Slavek Kabrda 1a6e21
            "id": 1,
Slavek Kabrda 1a6e21
            "initial_comment": None,
Slavek Kabrda 1a6e21
            "last_updated": "1431414800",
Slavek Kabrda 1a6e21
            "project": {
Slavek Kabrda 1a6e21
                "access_groups": {
Slavek Kabrda 1a6e21
                    "admin": [],
Slavek Kabrda 1a6e21
                    "commit": [],
Slavek Kabrda 1a6e21
                    "ticket": []
Slavek Kabrda 1a6e21
                },
Slavek Kabrda 1a6e21
                "access_users": {
Slavek Kabrda 1a6e21
                    "admin": [],
Slavek Kabrda 1a6e21
                    "commit": [],
Slavek Kabrda 1a6e21
                    "owner": ["pingou"],
Slavek Kabrda 1a6e21
                    "ticket": []
Slavek Kabrda 1a6e21
                },
Slavek Kabrda 1a6e21
                "close_status": [
Slavek Kabrda 1a6e21
                    "Invalid",
Slavek Kabrda 1a6e21
                    "Insufficient data",
Slavek Kabrda 1a6e21
                    "Fixed",
Slavek Kabrda 1a6e21
                    "Duplicate"
Slavek Kabrda 1a6e21
                ],
Slavek Kabrda 1a6e21
                "custom_keys": [],
Slavek Kabrda 1a6e21
                "date_created": "1431414800",
Slavek Kabrda 1a6e21
                "date_modified": "1431414800",
Slavek Kabrda 1a6e21
                "description": "test project #1",
Slavek Kabrda 1a6e21
                "fullname": "test",
Slavek Kabrda 1a6e21
                "url_path": "test",
Slavek Kabrda 1a6e21
                "id": 1,
Slavek Kabrda 1a6e21
                "milestones": {},
Slavek Kabrda 1a6e21
                "name": "test",
Slavek Kabrda 1a6e21
                "namespace": None,
Slavek Kabrda 1a6e21
                "parent": None,
Slavek Kabrda 1a6e21
                "priorities": {},
Slavek Kabrda 1a6e21
                "tags": [],
Slavek Kabrda 1a6e21
                "user": {
Slavek Kabrda 1a6e21
                    "fullname": "PY C",
Slavek Kabrda 1a6e21
                    "name": "pingou"
Slavek Kabrda 1a6e21
                }
Slavek Kabrda 1a6e21
            },
Slavek Kabrda 1a6e21
            "remote_git": None,
Slavek Kabrda 1a6e21
            "repo_from": {
Slavek Kabrda 1a6e21
                "access_groups": {
Slavek Kabrda 1a6e21
                    "admin": [],
Slavek Kabrda 1a6e21
                    "commit": [],
Slavek Kabrda 1a6e21
                    "ticket": []},
Slavek Kabrda 1a6e21
                "access_users": {
Slavek Kabrda 1a6e21
                    "admin": [],
Slavek Kabrda 1a6e21
                    "commit": [],
Slavek Kabrda 1a6e21
                    "owner": ["pingou"],
Slavek Kabrda 1a6e21
                    "ticket": []},
Slavek Kabrda 1a6e21
                "close_status": [
Slavek Kabrda 1a6e21
                    "Invalid",
Slavek Kabrda 1a6e21
                    "Insufficient data",
Slavek Kabrda 1a6e21
                    "Fixed",
Slavek Kabrda 1a6e21
                    "Duplicate"],
Slavek Kabrda 1a6e21
                    "custom_keys": [],
Slavek Kabrda 1a6e21
                    "date_created": "1431414800",
Slavek Kabrda 1a6e21
                    "date_modified": "1431414800",
Slavek Kabrda 1a6e21
                    "description": "test project #1",
Slavek Kabrda 1a6e21
                    "fullname": "test",
Slavek Kabrda 1a6e21
                    "url_path": "test",
Slavek Kabrda 1a6e21
                    "id": 1,
Slavek Kabrda 1a6e21
                    "milestones": {},
Slavek Kabrda 1a6e21
                    "name": "test",
Slavek Kabrda 1a6e21
                    "namespace": None,
Slavek Kabrda 1a6e21
                    "parent": None,
Slavek Kabrda 1a6e21
                    "priorities": {},
Slavek Kabrda 1a6e21
                    "tags": [],
Slavek Kabrda 1a6e21
                    "user": {
Slavek Kabrda 1a6e21
                        "fullname": "PY C",
Slavek Kabrda 1a6e21
                        "name": "pingou"
Slavek Kabrda 1a6e21
                    }
Slavek Kabrda 1a6e21
            },
Slavek Kabrda 1a6e21
            "status": "Open",
Slavek Kabrda 1a6e21
            "title": "test pull-request",
Slavek Kabrda 1a6e21
            "uid": uid,
Slavek Kabrda 1a6e21
            "updated_on": "1431414800",
Slavek Kabrda 1a6e21
            "user": {
Slavek Kabrda 1a6e21
                "fullname": "PY C",
Slavek Kabrda 1a6e21
                "name": "pingou"
Slavek Kabrda 1a6e21
            }
Slavek Kabrda 1a6e21
        }
Slavek Kabrda 1a6e21
        self.assertDictEqual(data, expected_data)
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        headers = {'Authorization': 'token aaabbbcccddd'}
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        # Access Pull-Request authenticated
Slavek Kabrda 1a6e21
        output = self.app.get('/api/0/pull-requests/{}'.format(uid), headers=headers)
Slavek Kabrda 1a6e21
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 1a6e21
        data2 = json.loads(output.get_data(as_text=True))
Slavek Kabrda 1a6e21
        data2['date_created'] = '1431414800'
Slavek Kabrda 1a6e21
        data2['project']['date_created'] = '1431414800'
Slavek Kabrda 1a6e21
        data2['project']['date_modified'] = '1431414800'
Slavek Kabrda 1a6e21
        data2['repo_from']['date_created'] = '1431414800'
Slavek Kabrda 1a6e21
        data2['repo_from']['date_modified'] = '1431414800'
Slavek Kabrda 1a6e21
        data2['date_created'] = '1431414800'
Slavek Kabrda 1a6e21
        data2['updated_on'] = '1431414800'
Slavek Kabrda 1a6e21
        data2['last_updated'] = '1431414800'
Slavek Kabrda 1a6e21
        self.assertDictEqual(data, data2)
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 417805
    def test_api_pull_request_close_pr_disabled(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_close method of the flask api. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 417805
            branch_from='master',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 417805
        settings['pull_requests'] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 417805
            '/api/0/test/pull-request/1/close', headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'Pull-Request have been deactivated for this project',
Pierre-Yves Chibon 417805
                u'error_code': u'EPULLREQUESTSDISABLED'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 7cc2d4
    def test_api_pull_request_close(self, send_email):
Pierre-Yves Chibon 209c7e
        """ Test the api_pull_request_close method of the flask api. """
Pierre-Yves Chibon 7cc2d4
        send_email.return_value = True
Pierre-Yves Chibon 7cc2d4
Pierre-Yves Chibon 209c7e
        tests.create_projects(self.session)
Pierre-Yves Chibon 209c7e
        tests.create_tokens(self.session)
Pierre-Yves Chibon 209c7e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 209c7e
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 209c7e
            session=self.session,
Pierre-Yves Chibon 209c7e
            repo_from=forked_repo,
Pierre-Yves Chibon 209c7e
            branch_from='master',
Pierre-Yves Chibon 209c7e
            repo_to=repo,
Pierre-Yves Chibon 209c7e
            branch_to='master',
Pierre-Yves Chibon 209c7e
            title='test pull-request',
Pierre-Yves Chibon 209c7e
            user='pingou',
Pierre-Yves Chibon 209c7e
        )
Pierre-Yves Chibon 209c7e
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 209c7e
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 209c7e
        # Invalid project
Pierre-Yves Chibon 209c7e
        output = self.app.post(
Pierre-Yves Chibon 209c7e
            '/api/0/foo/pull-request/1/close', headers=headers)
Pierre-Yves Chibon 209c7e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 209c7e
        self.assertDictEqual(
Pierre-Yves Chibon 209c7e
            data,
Pierre-Yves Chibon 209c7e
            {
Pierre-Yves Chibon 209c7e
              "error": "Project not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon 209c7e
            }
Pierre-Yves Chibon 209c7e
        )
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 209c7e
        # Valid token, wrong project
Pierre-Yves Chibon 209c7e
        output = self.app.post(
Pierre-Yves Chibon 209c7e
            '/api/0/test2/pull-request/1/close', headers=headers)
Pierre-Yves Chibon 209c7e
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Jeremy Cline 099538
                         data['error_code'])
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon f12b4a
        # Invalid PR
Pierre-Yves Chibon f12b4a
        output = self.app.post(
Pierre-Yves Chibon f12b4a
            '/api/0/test/pull-request/2/close', headers=headers)
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f12b4a
        self.assertDictEqual(
Pierre-Yves Chibon f12b4a
            data,
Pierre-Yves Chibon d661a4
            {'error': 'Pull-Request not found', 'error_code': "ENOREQ"}
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # Create a token for foo for this project
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.Token(
Pierre-Yves Chibon f12b4a
            id='foobar_token',
Pierre-Yves Chibon f12b4a
            user_id=2,
Pierre-Yves Chibon f12b4a
            project_id=1,
Pierre-Yves Chibon f12b4a
            expiration=datetime.datetime.utcnow() + datetime.timedelta(
Pierre-Yves Chibon f12b4a
                days=30)
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
        self.session.add(item)
Pierre-Yves Chibon f12b4a
        self.session.commit()
Pierre-Yves Chibon c02e60
Pierre-Yves Chibon c02e60
        # Allow the token to close PR
Pierre-Yves Chibon 930073
        acls = pagure.lib.query.get_acls(self.session)
Pierre-Yves Chibon b11d54
        for acl in acls:
Pierre-Yves Chibon b11d54
            if acl.name == 'pull_request_close':
Pierre-Yves Chibon b11d54
                break
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon f12b4a
            token_id='foobar_token',
Pierre-Yves Chibon b11d54
            acl_id=acl.id,
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
        self.session.add(item)
Pierre-Yves Chibon f12b4a
        self.session.commit()
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        headers = {'Authorization': 'token foobar_token'}
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # User not admin
Pierre-Yves Chibon f12b4a
        output = self.app.post(
Pierre-Yves Chibon f12b4a
            '/api/0/test/pull-request/1/close', headers=headers)
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 403)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f12b4a
        self.assertDictEqual(
Pierre-Yves Chibon f12b4a
            data,
Pierre-Yves Chibon f12b4a
            {
Pierre-Yves Chibon f12b4a
                'error': 'You are not allowed to merge/close pull-request '
Pierre-Yves Chibon f12b4a
                    'for this project',
Pierre-Yves Chibon d661a4
                'error_code': "ENOPRCLOSE",
Pierre-Yves Chibon f12b4a
            }
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon 209c7e
        # Close PR
Pierre-Yves Chibon 209c7e
        output = self.app.post(
Pierre-Yves Chibon 209c7e
            '/api/0/test/pull-request/1/close', headers=headers)
Pierre-Yves Chibon 209c7e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 209c7e
        self.assertDictEqual(
Pierre-Yves Chibon 209c7e
            data,
Pierre-Yves Chibon 316f15
            {"message": "Pull-request closed!"}
Pierre-Yves Chibon 209c7e
        )
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 7cc2d4
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 417805
    def test_api_pull_request_merge_pr_disabled(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_merge method of the flask api when PR
Pierre-Yves Chibon 417805
        are disabled. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 417805
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 417805
                                  bare=True)
Pierre-Yves Chibon 417805
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 417805
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 417805
                                  branch='test')
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 417805
            branch_from='test',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 417805
        settings['pull_requests'] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 417805
            '/api/0/test/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'Pull-Request have been deactivated for this project',
Pierre-Yves Chibon 417805
                u'error_code': u'EPULLREQUESTSDISABLED'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 417805
    def test_api_pull_request_merge_only_assigned(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_merge method of the flask api when
Pierre-Yves Chibon 417805
        only assignee can merge the PR and the PR isn't assigned. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 417805
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 417805
                                  bare=True)
Pierre-Yves Chibon 417805
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 417805
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 417805
                                  branch='test')
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 417805
            branch_from='test',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 417805
        settings['Only_assignee_can_merge_pull-request'] = True
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 417805
            '/api/0/test/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'This request must be assigned to be merged',
Pierre-Yves Chibon 417805
                u'error_code': u'ENOTASSIGNED'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 417805
    def test_api_pull_request_merge_only_assigned_not_assignee(
Pierre-Yves Chibon 417805
            self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_merge method of the flask api when
Pierre-Yves Chibon 417805
        only assignee can merge the PR and the PR isn't assigned to the
Pierre-Yves Chibon 417805
        user asking to merge. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 417805
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 417805
                                  bare=True)
Pierre-Yves Chibon 417805
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 417805
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 417805
                                  branch='test')
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 417805
            branch_from='test',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 930073
        req.assignee = pagure.lib.query.search_user(self.session, 'foo')
Pierre-Yves Chibon 417805
        self.session.add(req)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 417805
        settings['Only_assignee_can_merge_pull-request'] = True
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 417805
            '/api/0/test/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'Only the assignee can merge this review',
Pierre-Yves Chibon 417805
                u'error_code': u'ENOTASSIGNEE'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 417805
    def test_api_pull_request_merge_minimal_score(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_merge method of the flask api when
Pierre-Yves Chibon 417805
        a PR requires a certain minimal score to be merged. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 417805
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 417805
                                  bare=True)
Pierre-Yves Chibon 417805
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 417805
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 417805
                                  branch='test')
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 417805
            branch_from='test',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 417805
        settings['Minimum_score_to_merge_pull-request'] = 2
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 417805
            '/api/0/test/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'This request does not have the minimum review '
Pierre-Yves Chibon 417805
                'score necessary to be merged',
Pierre-Yves Chibon 417805
                u'error_code': u'EPRSCORE'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email')
Patrick Uiterwijk 02a7cc
    def test_api_pull_request_merge(self, send_email):
Pierre-Yves Chibon a9c964
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 7cc2d4
        send_email.return_value = True
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        tests.create_projects(self.session)
Patrick Uiterwijk 02a7cc
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Patrick Uiterwijk 02a7cc
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Patrick Uiterwijk 02a7cc
                                  bare=True)
Patrick Uiterwijk 02a7cc
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Patrick Uiterwijk 02a7cc
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Patrick Uiterwijk 02a7cc
                                  branch='test')
Pierre-Yves Chibon a9c964
        tests.create_tokens(self.session)
Pierre-Yves Chibon a9c964
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon a9c964
            session=self.session,
Pierre-Yves Chibon a9c964
            repo_from=forked_repo,
Patrick Uiterwijk 02a7cc
            branch_from='test',
Pierre-Yves Chibon a9c964
            repo_to=repo,
Pierre-Yves Chibon a9c964
            branch_to='master',
Pierre-Yves Chibon a9c964
            title='test pull-request',
Pierre-Yves Chibon a9c964
            user='pingou',
Pierre-Yves Chibon a9c964
        )
Pierre-Yves Chibon a9c964
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        # Invalid project
Pierre-Yves Chibon a9c964
        output = self.app.post(
Pierre-Yves Chibon a9c964
            '/api/0/foo/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon a9c964
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon a9c964
        self.assertDictEqual(
Pierre-Yves Chibon a9c964
            data,
Pierre-Yves Chibon a9c964
            {
Pierre-Yves Chibon a9c964
              "error": "Project not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon a9c964
            }
Pierre-Yves Chibon a9c964
        )
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        # Valid token, wrong project
Pierre-Yves Chibon a9c964
        output = self.app.post(
Pierre-Yves Chibon a9c964
            '/api/0/test2/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon a9c964
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Jeremy Cline 099538
                         data['error_code'])
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon f12b4a
        # Invalid PR
Pierre-Yves Chibon f12b4a
        output = self.app.post(
Pierre-Yves Chibon f12b4a
            '/api/0/test/pull-request/2/merge', headers=headers)
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f12b4a
        self.assertDictEqual(
Pierre-Yves Chibon f12b4a
            data,
Pierre-Yves Chibon d661a4
            {'error': 'Pull-Request not found', 'error_code': "ENOREQ"}
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # Create a token for foo for this project
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.Token(
Pierre-Yves Chibon f12b4a
            id='foobar_token',
Pierre-Yves Chibon f12b4a
            user_id=2,
Pierre-Yves Chibon f12b4a
            project_id=1,
Pierre-Yves Chibon f12b4a
            expiration=datetime.datetime.utcnow() + datetime.timedelta(
Pierre-Yves Chibon f12b4a
                days=30)
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
        self.session.add(item)
Pierre-Yves Chibon f12b4a
        self.session.commit()
Pierre-Yves Chibon c02e60
Pierre-Yves Chibon c02e60
        # Allow the token to merge PR
Pierre-Yves Chibon 930073
        acls = pagure.lib.query.get_acls(self.session)
Pierre-Yves Chibon b11d54
        for acl in acls:
Pierre-Yves Chibon b11d54
            if acl.name == 'pull_request_merge':
Pierre-Yves Chibon b11d54
                break
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon f12b4a
            token_id='foobar_token',
Pierre-Yves Chibon b11d54
            acl_id=acl.id,
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
        self.session.add(item)
Pierre-Yves Chibon dea6ca
        self.session.commit()
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        headers = {'Authorization': 'token foobar_token'}
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # User not admin
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon dea6ca
            '/api/0/test/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 403)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon dea6ca
            data,
Pierre-Yves Chibon dea6ca
            {
Pierre-Yves Chibon dea6ca
                'error': 'You are not allowed to merge/close pull-request '
Pierre-Yves Chibon dea6ca
                    'for this project',
Pierre-Yves Chibon dea6ca
                'error_code': "ENOPRCLOSE",
Pierre-Yves Chibon dea6ca
            }
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Merge PR
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon dea6ca
            '/api/0/test/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon dea6ca
            data,
Pierre-Yves Chibon dea6ca
            {"message": "Changes merged!"}
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
    @patch('pagure.lib.notify.send_email')
Patrick Uiterwijk 02a7cc
    def test_api_pull_request_merge_user_token(self, send_email):
Pierre-Yves Chibon dea6ca
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon dea6ca
        send_email.return_value = True
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        tests.create_projects(self.session)
Patrick Uiterwijk 02a7cc
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Patrick Uiterwijk 02a7cc
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Patrick Uiterwijk 02a7cc
                                  bare=True)
Patrick Uiterwijk 02a7cc
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Patrick Uiterwijk 02a7cc
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Patrick Uiterwijk 02a7cc
                                  branch='test')
Pierre-Yves Chibon dea6ca
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon dea6ca
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon dea6ca
            session=self.session,
Pierre-Yves Chibon dea6ca
            repo_from=forked_repo,
Patrick Uiterwijk 02a7cc
            branch_from='test',
Pierre-Yves Chibon dea6ca
            repo_to=repo,
Pierre-Yves Chibon dea6ca
            branch_to='master',
Pierre-Yves Chibon dea6ca
            title='test pull-request',
Pierre-Yves Chibon dea6ca
            user='pingou',
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
        self.session.commit()
Pierre-Yves Chibon dea6ca
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon dea6ca
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Invalid project
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon dea6ca
            '/api/0/foo/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon dea6ca
            data,
Pierre-Yves Chibon dea6ca
            {
Pierre-Yves Chibon dea6ca
              "error": "Project not found",
Pierre-Yves Chibon dea6ca
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon dea6ca
            }
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Valid token, invalid PR
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon dea6ca
            '/api/0/test2/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon dea6ca
            data,
Pierre-Yves Chibon dea6ca
            {'error': 'Pull-Request not found', 'error_code': "ENOREQ"}
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Valid token, invalid PR - other project
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon dea6ca
            '/api/0/test/pull-request/2/merge', headers=headers)
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon dea6ca
            data,
Pierre-Yves Chibon dea6ca
            {'error': 'Pull-Request not found', 'error_code': "ENOREQ"}
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Create a token for foo for this project
Pierre-Yves Chibon dea6ca
        item = pagure.lib.model.Token(
Pierre-Yves Chibon dea6ca
            id='foobar_token',
Pierre-Yves Chibon dea6ca
            user_id=2,
Pierre-Yves Chibon dea6ca
            project_id=1,
Pierre-Yves Chibon dea6ca
            expiration=datetime.datetime.utcnow() + datetime.timedelta(
Pierre-Yves Chibon dea6ca
                days=30)
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
        self.session.add(item)
Pierre-Yves Chibon dea6ca
        self.session.commit()
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Allow the token to merge PR
Pierre-Yves Chibon 930073
        acls = pagure.lib.query.get_acls(self.session)
Pierre-Yves Chibon dea6ca
        acl = None
Pierre-Yves Chibon dea6ca
        for acl in acls:
Pierre-Yves Chibon dea6ca
            if acl.name == 'pull_request_merge':
Pierre-Yves Chibon dea6ca
                break
Pierre-Yves Chibon dea6ca
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon dea6ca
            token_id='foobar_token',
Pierre-Yves Chibon dea6ca
            acl_id=acl.id,
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
        self.session.add(item)
Pierre-Yves Chibon f12b4a
        self.session.commit()
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        headers = {'Authorization': 'token foobar_token'}
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # User not admin
Pierre-Yves Chibon f12b4a
        output = self.app.post(
Pierre-Yves Chibon f12b4a
            '/api/0/test/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 403)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f12b4a
        self.assertDictEqual(
Pierre-Yves Chibon f12b4a
            data,
Pierre-Yves Chibon f12b4a
            {
Pierre-Yves Chibon f12b4a
                'error': 'You are not allowed to merge/close pull-request '
Pierre-Yves Chibon f12b4a
                    'for this project',
Pierre-Yves Chibon d661a4
                'error_code': "ENOPRCLOSE",
Pierre-Yves Chibon f12b4a
            }
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # Merge PR
Pierre-Yves Chibon a9c964
        output = self.app.post(
Pierre-Yves Chibon a9c964
            '/api/0/test/pull-request/1/merge', headers=headers)
Pierre-Yves Chibon a9c964
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon a9c964
        self.assertDictEqual(
Pierre-Yves Chibon a9c964
            data,
Pierre-Yves Chibon a9c964
            {"message": "Changes merged!"}
Pierre-Yves Chibon a9c964
        )
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon 4eb5ad
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 4eb5ad
    def test_api_pull_request_add_comment(self, mockemail):
Pierre-Yves Chibon 9ba77b
        """ Test the api_pull_request_add_comment method of the flask api. """
Pierre-Yves Chibon 4eb5ad
        mockemail.return_value = True
Pierre-Yves Chibon 4eb5ad
Pierre-Yves Chibon 9ba77b
        tests.create_projects(self.session)
Pierre-Yves Chibon 9ba77b
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9ba77b
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Invalid project
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 9ba77b
            '/api/0/foo/pull-request/1/comment', headers=headers)
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 9ba77b
            data,
Pierre-Yves Chibon 9ba77b
            {
Pierre-Yves Chibon 9ba77b
              "error": "Project not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon 9ba77b
            }
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Valid token, wrong project
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 9ba77b
            '/api/0/test2/pull-request/1/comment', headers=headers)
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Jeremy Cline 099538
                         data['error_code'])
Jeremy Cline 099538
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # No input
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 9ba77b
            '/api/0/test/pull-request/1/comment', headers=headers)
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 9ba77b
            data,
Pierre-Yves Chibon 9ba77b
            {
Pierre-Yves Chibon 9ba77b
              "error": "Pull-Request not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOREQ",
Pierre-Yves Chibon 9ba77b
            }
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 9ba77b
            session=self.session,
Pierre-Yves Chibon 9ba77b
            repo_from=forked_repo,
Pierre-Yves Chibon 9ba77b
            branch_from='master',
Pierre-Yves Chibon 9ba77b
            repo_to=repo,
Pierre-Yves Chibon 9ba77b
            branch_to='master',
Pierre-Yves Chibon 9ba77b
            title='test pull-request',
Pierre-Yves Chibon 9ba77b
            user='pingou',
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Check comments before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 9ba77b
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 9ba77b
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        data = {
Pierre-Yves Chibon 9ba77b
            'title': 'test issue',
Pierre-Yves Chibon 9ba77b
        }
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Incomplete request
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 9ba77b
            '/api/0/test/pull-request/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 9ba77b
            data,
Pierre-Yves Chibon 9ba77b
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon f7fcaa
              "errors": {"comment": ["This field is required."]}
Pierre-Yves Chibon 9ba77b
            }
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 9ba77b
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 9ba77b
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        data = {
Pierre-Yves Chibon 9ba77b
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 9ba77b
        }
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Valid request
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 9ba77b
            '/api/0/test/pull-request/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 9ba77b
            data,
Pierre-Yves Chibon 9ba77b
            {'message': 'Comment added'}
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # One comment added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 9ba77b
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 9ba77b
        self.assertEqual(len(request.comments), 1)
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 417805
    def test_api_pull_request_add_comment_wrong_user(self):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_add_comment method of the flask api
Pierre-Yves Chibon 417805
        when the user is not found in the DB. """
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 417805
            branch_from='master',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        data = {
Pierre-Yves Chibon 417805
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 417805
        }
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Valid request
Pierre-Yves Chibon 930073
        with patch('pagure.lib.query.add_pull_request_comment',
Pierre-Yves Chibon 417805
                   side_effect=pagure.exceptions.PagureException('error')):
Pierre-Yves Chibon 417805
            output = self.app.post(
Pierre-Yves Chibon 417805
                '/api/0/test/pull-request/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 417805
            self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 417805
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
            self.assertDictEqual(
Pierre-Yves Chibon 417805
                data,
Pierre-Yves Chibon 417805
                {u'error': u'error', u'error_code': u'ENOCODE'}
Pierre-Yves Chibon 417805
            )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 417805
    def test_api_pull_request_add_comment_pr_disabled(self):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_add_comment method of the flask api
Pierre-Yves Chibon 417805
        when PRs are disabled. """
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 417805
            branch_from='master',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 417805
        settings['pull_requests'] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        data = {
Pierre-Yves Chibon 417805
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 417805
        }
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Valid request
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 417805
            '/api/0/test/pull-request/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'Pull-Request have been deactivated for this project',
Pierre-Yves Chibon 417805
                u'error_code': u'EPULLREQUESTSDISABLED'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # no comment added
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 417805
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 417805
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon e18f81
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 1dfd94
    def test_api_pull_request_add_comment_user_token(self, mockemail):
Pierre-Yves Chibon 1dfd94
        """ Test the api_pull_request_add_comment method of the flask api. """
Pierre-Yves Chibon 1dfd94
        mockemail.return_value = True
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        tests.create_projects(self.session)
Pierre-Yves Chibon 1dfd94
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 1dfd94
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Invalid project
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/foo/pull-request/1/comment', headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
              "error": "Project not found",
Pierre-Yves Chibon 1dfd94
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid token, invalid request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/test2/pull-request/1/comment', headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
              "error": "Pull-Request not found",
Pierre-Yves Chibon 1dfd94
              "error_code": "ENOREQ",
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid token, invalid request in another project
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/test/pull-request/1/comment', headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 1dfd94
              "error": "Pull-Request not found",
Pierre-Yves Chibon 1dfd94
              "error_code": "ENOREQ",
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 1dfd94
            session=self.session,
Pierre-Yves Chibon 1dfd94
            repo_from=forked_repo,
Pierre-Yves Chibon 1dfd94
            branch_from='master',
Pierre-Yves Chibon 1dfd94
            repo_to=repo,
Pierre-Yves Chibon 1dfd94
            branch_to='master',
Pierre-Yves Chibon 1dfd94
            title='test pull-request',
Pierre-Yves Chibon 1dfd94
            user='pingou',
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
        self.session.commit()
Pierre-Yves Chibon 1dfd94
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Check comments before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 1dfd94
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'title': 'test issue',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Incomplete request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/test/pull-request/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
René Genz dadd94
              "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 1dfd94
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 1dfd94
              "errors": {"comment": ["This field is required."]}
Pierre-Yves Chibon 1dfd94
            }
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 1dfd94
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        data = {
Pierre-Yves Chibon 1dfd94
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 1dfd94
        }
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 1dfd94
            '/api/0/test/pull-request/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {'message': 'Comment added'}
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # One comment added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 1dfd94
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 1dfd94
        self.assertEqual(len(request.comments), 1)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 417805
    def test_api_subscribe_pull_request_pr_disabled(self, p_send_email):
Pierre-Yves Chibon 417805
        """ Test the api_subscribe_pull_request method of the flask api. """
Pierre-Yves Chibon 417805
        p_send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 417805
        settings['pull_requests'] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Invalid project
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 417805
            '/api/0/test/pull-request/1/subscribe', headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'Pull-Request have been deactivated for this project',
Pierre-Yves Chibon 417805
                u'error_code': u'EPULLREQUESTSDISABLED'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
    @patch('pagure.lib.git.update_git')
Pierre-Yves Chibon 417805
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 417805
    def test_api_subscribe_pull_request_invalid_token(self, p_send_email, p_ugt):
Pierre-Yves Chibon 417805
        """ Test the api_subscribe_pull_request method of the flask api. """
Pierre-Yves Chibon 417805
        p_send_email.return_value = True
Pierre-Yves Chibon 417805
        p_ugt.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        item = pagure.lib.model.User(
Pierre-Yves Chibon 417805
            user='bar',
Pierre-Yves Chibon 417805
            fullname='bar foo',
Pierre-Yves Chibon 417805
            password='foo',
Pierre-Yves Chibon 417805
            default_email='bar@bar.com',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.add(item)
Pierre-Yves Chibon 417805
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 417805
            user_id=3,
Pierre-Yves Chibon 417805
            email='bar@bar.com')
Pierre-Yves Chibon 417805
        self.session.add(item)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session, user_id=3, project_id=2)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=repo,
Pierre-Yves Chibon 417805
            branch_from='feature',
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 417805
            branch_to='master',
Pierre-Yves Chibon 417805
            title='test pull-request',
Pierre-Yves Chibon 417805
            user='pingou',
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 417805
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Check subscribtion before
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 417805
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 417805
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 417805
            set(['pingou']))
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        data = {}
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 417805
            '/api/0/test/pull-request/1/subscribe',
Pierre-Yves Chibon 417805
            data=data, headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 417805
                u'error': u'Invalid or expired token. Please visit '
Pierre-Yves Chibon 417805
                'http://localhost.localdomain/settings#api-keys to get or '
Pierre-Yves Chibon 417805
                'renew your API token.',
Pierre-Yves Chibon 417805
                u'error_code': u'EINVALIDTOK'
Pierre-Yves Chibon 417805
            }
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 4f5301
    @patch('pagure.lib.git.update_git')
Pierre-Yves Chibon 4f5301
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 4f5301
    def test_api_subscribe_pull_request(self, p_send_email, p_ugt):
Pierre-Yves Chibon 4f5301
        """ Test the api_subscribe_pull_request method of the flask api. """
Pierre-Yves Chibon 4f5301
        p_send_email.return_value = True
Pierre-Yves Chibon 4f5301
        p_ugt.return_value = True
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        item = pagure.lib.model.User(
Pierre-Yves Chibon 4f5301
            user='bar',
Pierre-Yves Chibon 4f5301
            fullname='bar foo',
Pierre-Yves Chibon 4f5301
            password='foo',
Pierre-Yves Chibon 4f5301
            default_email='bar@bar.com',
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
        self.session.add(item)
Pierre-Yves Chibon 4f5301
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 4f5301
            user_id=3,
Pierre-Yves Chibon 4f5301
            email='bar@bar.com')
Pierre-Yves Chibon 4f5301
        self.session.add(item)
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        self.session.commit()
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        tests.create_projects(self.session)
Pierre-Yves Chibon 4f5301
        tests.create_tokens(self.session, user_id=3)
Pierre-Yves Chibon 4f5301
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Invalid project
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 4f5301
            '/api/0/foo/pull-request/1/subscribe', headers=headers)
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Pierre-Yves Chibon 4f5301
            {
Pierre-Yves Chibon 4f5301
              "error": "Project not found",
Pierre-Yves Chibon 4f5301
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon 4f5301
            }
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Valid token, wrong project
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 4f5301
            '/api/0/test2/pull-request/1/subscribe', headers=headers)
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 4f5301
                         data['error_code'])
Pierre-Yves Chibon 417805
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # No input
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 4f5301
            '/api/0/test/pull-request/1/subscribe', headers=headers)
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Pierre-Yves Chibon 4f5301
            {
Aurélien Bompard 626417
                'error': 'Pull-Request not found',
Aurélien Bompard 626417
                'error_code': 'ENOREQ'
Pierre-Yves Chibon 4f5301
            }
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Create pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 4f5301
            session=self.session,
Pierre-Yves Chibon 4f5301
            repo_from=repo,
Pierre-Yves Chibon 4f5301
            branch_from='feature',
Pierre-Yves Chibon 4f5301
            repo_to=repo,
Pierre-Yves Chibon 4f5301
            branch_to='master',
Pierre-Yves Chibon 4f5301
            title='test pull-request',
Pierre-Yves Chibon 4f5301
            user='pingou',
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
        self.session.commit()
Pierre-Yves Chibon 4f5301
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 4f5301
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Check subscribtion before
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 4f5301
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 4f5301
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 4f5301
            set(['pingou']))
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Unsubscribe - no changes
Pierre-Yves Chibon 4f5301
        data = {}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 4f5301
            '/api/0/test/pull-request/1/subscribe',
Pierre-Yves Chibon 4f5301
            data=data, headers=headers)
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Ryan Lerch 5682fd
            {'message': 'You are no longer watching this pull-request',
Pierre-Yves Chibon e2c468
            'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Ryan Lerch 5682fd
            'user': 'bar',
Ryan Lerch 5682fd
            }
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        data = {}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 4f5301
            '/api/0/test/pull-request/1/subscribe',
Pierre-Yves Chibon 4f5301
            data=data, headers=headers)
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Ryan Lerch 5682fd
            {'message': 'You are no longer watching this pull-request',
Pierre-Yves Chibon e2c468
            'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Ryan Lerch 5682fd
            'user': 'bar',
Ryan Lerch 5682fd
            }
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # No change
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 4f5301
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 4f5301
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 4f5301
            set(['pingou']))
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Subscribe
Pierre-Yves Chibon 4f5301
        data = {'status': True}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 4f5301
            '/api/0/test/pull-request/1/subscribe',
Pierre-Yves Chibon 4f5301
            data=data, headers=headers)
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Ryan Lerch 5682fd
            {'message': 'You are now watching this pull-request',
Pierre-Yves Chibon e2c468
            'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Ryan Lerch 5682fd
            'user': 'bar',
Ryan Lerch 5682fd
            }
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Subscribe - no changes
Pierre-Yves Chibon 4f5301
        data = {'status': True}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 4f5301
            '/api/0/test/pull-request/1/subscribe',
Pierre-Yves Chibon 4f5301
            data=data, headers=headers)
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Ryan Lerch 5682fd
            {'message': 'You are now watching this pull-request',
Pierre-Yves Chibon e2c468
            'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Ryan Lerch 5682fd
            'user': 'bar',
Ryan Lerch 5682fd
            }
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 4f5301
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 4f5301
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 4f5301
            set(['pingou', 'bar']))
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Unsubscribe
Pierre-Yves Chibon 4f5301
        data = {}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 4f5301
            '/api/0/test/pull-request/1/subscribe',
Pierre-Yves Chibon 4f5301
            data=data, headers=headers)
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Ryan Lerch 5682fd
            {'message': 'You are no longer watching this pull-request',
Pierre-Yves Chibon e2c468
            'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Ryan Lerch 5682fd
            'user': 'bar',
Ryan Lerch 5682fd
            }
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 4f5301
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 4f5301
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 4f5301
            set(['pingou']))
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 335a73
    @patch('pagure.lib.git.update_git')
Pierre-Yves Chibon 335a73
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 335a73
    def test_api_subscribe_pull_request_logged_in(self, p_send_email, p_ugt):
Pierre-Yves Chibon 335a73
        """ Test the api_subscribe_pull_request method of the flask api
Pierre-Yves Chibon 335a73
        when the user is logged in via the UI. """
Pierre-Yves Chibon 335a73
        p_send_email.return_value = True
Pierre-Yves Chibon 335a73
        p_ugt.return_value = True
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        item = pagure.lib.model.User(
Pierre-Yves Chibon 335a73
            user='bar',
Pierre-Yves Chibon 335a73
            fullname='bar foo',
Pierre-Yves Chibon 335a73
            password='foo',
Pierre-Yves Chibon 335a73
            default_email='bar@bar.com',
Pierre-Yves Chibon 335a73
        )
Pierre-Yves Chibon 335a73
        self.session.add(item)
Pierre-Yves Chibon 335a73
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 335a73
            user_id=3,
Pierre-Yves Chibon 335a73
            email='bar@bar.com')
Pierre-Yves Chibon 335a73
        self.session.add(item)
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        self.session.commit()
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        tests.create_projects(self.session)
Pierre-Yves Chibon 335a73
        tests.create_tokens(self.session, user_id=3)
Pierre-Yves Chibon 335a73
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        # Create pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 335a73
            session=self.session,
Pierre-Yves Chibon 335a73
            repo_from=repo,
Pierre-Yves Chibon 335a73
            branch_from='feature',
Pierre-Yves Chibon 335a73
            repo_to=repo,
Pierre-Yves Chibon 335a73
            branch_to='master',
Pierre-Yves Chibon 335a73
            title='test pull-request',
Pierre-Yves Chibon 335a73
            user='pingou',
Pierre-Yves Chibon 335a73
        )
Pierre-Yves Chibon 335a73
        self.session.commit()
Pierre-Yves Chibon 335a73
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 335a73
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        # Check subscribtion before
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 335a73
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 335a73
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 335a73
            set(['pingou']))
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        # Subscribe
Pierre-Yves Chibon 335a73
        user = tests.FakeUser(username='foo')
Pierre-Yves Chibon 335a73
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 335a73
            data = {'status': True}
Pierre-Yves Chibon 335a73
            output = self.app.post(
Pierre-Yves Chibon 335a73
                '/api/0/test/pull-request/1/subscribe', data=data)
Pierre-Yves Chibon 335a73
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 335a73
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
            data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 335a73
            self.assertDictEqual(
Pierre-Yves Chibon 335a73
                data,
Ryan Lerch 5682fd
                {'message': 'You are now watching this pull-request',
Pierre-Yves Chibon e2c468
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
Ryan Lerch 5682fd
                'user': 'foo',
Ryan Lerch 5682fd
                }
Pierre-Yves Chibon 335a73
            )
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        # Check subscribtions after
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 335a73
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 335a73
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 335a73
            set(['pingou', 'foo']))
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_invalid_project(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not the project doesn't exist.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_to': 'master',
Pierre-Yves Chibon 9811e6
            'branch_from': 'test',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/foobar/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Aurélien Bompard 626417
            {'error': 'Project not found', 'error_code': 'ENOPROJECT'}
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_missing_title(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not title is submitted.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_to': 'master',
Pierre-Yves Chibon 9811e6
            'branch_from': 'test',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'error': 'Invalid or incomplete input submitted',
Aurélien Bompard 626417
                'error_code': 'EINVALIDREQ',
Aurélien Bompard 626417
                'errors': {'title': ['This field is required.']}
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_missing_branch_to(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not branch to is submitted.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'title': 'Test PR',
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_from': 'test',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'error': 'Invalid or incomplete input submitted',
Aurélien Bompard 626417
                'error_code': 'EINVALIDREQ',
Aurélien Bompard 626417
                'errors': {'branch_to': ['This field is required.']}
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_missing_branch_from(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not branch from is submitted.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'title': 'Test PR',
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_to': 'master',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'error': 'Invalid or incomplete input submitted',
Aurélien Bompard 626417
                'error_code': 'EINVALIDREQ',
Aurélien Bompard 626417
                'errors': {'branch_from': ['This field is required.']}
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_pr_disabled(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        the parent repo disabled pull-requests.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        # Check the behavior if the project disabled the issue tracker
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 9811e6
        settings = repo.settings
Pierre-Yves Chibon 9811e6
        settings['pull_requests'] = False
Pierre-Yves Chibon 9811e6
        repo.settings = settings
Pierre-Yves Chibon 9811e6
        self.session.add(repo)
Pierre-Yves Chibon 9811e6
        self.session.commit()
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'title': 'Test PR',
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_to': 'master',
Pierre-Yves Chibon 9811e6
            'branch_from': 'test',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'error': 'Pull-Request have been deactivated for this project',
Aurélien Bompard 626417
                'error_code': 'EPULLREQUESTSDISABLED'
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_signed_pr(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        the parent repo enforces signed-off pull-requests.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        # Check the behavior if the project disabled the issue tracker
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 9811e6
        settings = repo.settings
Pierre-Yves Chibon 9811e6
        settings['Enforce_signed-off_commits_in_pull-request'] = True
Pierre-Yves Chibon 9811e6
        repo.settings = settings
Pierre-Yves Chibon 9811e6
        self.session.add(repo)
Pierre-Yves Chibon 9811e6
        self.session.commit()
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'title': 'Test PR',
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_to': 'master',
Pierre-Yves Chibon 9811e6
            'branch_from': 'test',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'error': 'This repo enforces that all commits are signed '
Pierre-Yves Chibon 9811e6
                   'off by their author.',
Aurélien Bompard 626417
                'error_code': 'ENOSIGNEDOFF'
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_invalid_branch_from(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        the branch from does not exist.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        # Check the behavior if the project disabled the issue tracker
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 9811e6
        settings = repo.settings
Pierre-Yves Chibon 9811e6
        settings['Enforce_signed-off_commits_in_pull-request'] = True
Pierre-Yves Chibon 9811e6
        repo.settings = settings
Pierre-Yves Chibon 9811e6
        self.session.add(repo)
Pierre-Yves Chibon 9811e6
        self.session.commit()
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'title': 'Test PR',
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_to': 'master',
Pierre-Yves Chibon 9811e6
            'branch_from': 'foobarbaz',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'error': 'Invalid or incomplete input submitted',
Aurélien Bompard 626417
                'error_code': 'EINVALIDREQ',
Aurélien Bompard 626417
                'errors': 'Branch foobarbaz does not exist'
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 625a97
    def test_api_pull_request_open_invalid_token(self):
Pierre-Yves Chibon 625a97
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 625a97
        queried with an invalid token.
Pierre-Yves Chibon 625a97
        """
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        tests.create_projects(self.session)
Pierre-Yves Chibon 625a97
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 625a97
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 625a97
                                  bare=True)
Pierre-Yves Chibon 625a97
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 625a97
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 625a97
                                  branch='test')
Pierre-Yves Chibon 625a97
        tests.create_tokens(self.session)
Pierre-Yves Chibon 625a97
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 625a97
        data = {
Pierre-Yves Chibon 625a97
            'title': 'Test PR',
Pierre-Yves Chibon 625a97
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 625a97
            'branch_to': 'master',
Pierre-Yves Chibon 625a97
            'branch_from': 'foobarbaz',
Pierre-Yves Chibon 625a97
        }
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        output = self.app.post(
Pierre-Yves Chibon 625a97
            '/api/0/test2/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 625a97
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 625a97
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 625a97
        self.assertDictEqual(
Pierre-Yves Chibon 625a97
            data,
Pierre-Yves Chibon 625a97
            {
Pierre-Yves Chibon 625a97
                u'error': u'Invalid or expired token. Please visit '
Pierre-Yves Chibon 625a97
                'http://localhost.localdomain/settings#api-keys to get or '
Pierre-Yves Chibon 625a97
                'renew your API token.',
Pierre-Yves Chibon 625a97
                u'error_code': u'EINVALIDTOK',
Pierre-Yves Chibon 625a97
            }
Pierre-Yves Chibon 625a97
        )
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 625a97
    def test_api_pull_request_open_invalid_access(self):
Pierre-Yves Chibon 625a97
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 625a97
        the user opening the PR doesn't have commit access.
Pierre-Yves Chibon 625a97
        """
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        tests.create_projects(self.session)
Pierre-Yves Chibon 625a97
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 625a97
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 625a97
                                  bare=True)
Pierre-Yves Chibon 625a97
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 625a97
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 625a97
                                  branch='test')
Pierre-Yves Chibon 625a97
        tests.create_tokens(self.session, user_id=2)
Pierre-Yves Chibon 625a97
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 625a97
        data = {
Pierre-Yves Chibon 625a97
            'title': 'Test PR',
Pierre-Yves Chibon 625a97
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 625a97
            'branch_to': 'master',
Pierre-Yves Chibon 625a97
            'branch_from': 'foobarbaz',
Pierre-Yves Chibon 625a97
        }
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        output = self.app.post(
Pierre-Yves Chibon 625a97
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 625a97
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 625a97
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 625a97
        self.assertDictEqual(
Pierre-Yves Chibon 625a97
            data,
Pierre-Yves Chibon 625a97
            {
Pierre-Yves Chibon 625a97
                u'error': u'You do not have sufficient permissions to '
Pierre-Yves Chibon 625a97
                u'perform this action',
Pierre-Yves Chibon 625a97
                u'error_code': u'ENOTHIGHENOUGH'
Pierre-Yves Chibon 625a97
            }
Pierre-Yves Chibon 625a97
        )
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_invalid_branch_to(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        the branch to does not exist.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        # Check the behavior if the project disabled the issue tracker
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 9811e6
        settings = repo.settings
Pierre-Yves Chibon 9811e6
        settings['Enforce_signed-off_commits_in_pull-request'] = True
Pierre-Yves Chibon 9811e6
        repo.settings = settings
Pierre-Yves Chibon 9811e6
        self.session.add(repo)
Pierre-Yves Chibon 9811e6
        self.session.commit()
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'title': 'Test PR',
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_to': 'foobarbaz',
Pierre-Yves Chibon 9811e6
            'branch_from': 'test',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'error': 'Invalid or incomplete input submitted',
Aurélien Bompard 626417
                'error_code': 'EINVALIDREQ',
Aurélien Bompard 626417
                'errors': 'Branch foobarbaz could not be found in the '
Pierre-Yves Chibon 9811e6
                    'target repo'
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api. """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'title': 'Test PR',
Pierre-Yves Chibon 9811e6
            'initial_comment': 'Nothing much, the changes speak for themselves',
Pierre-Yves Chibon 9811e6
            'branch_to': 'master',
Pierre-Yves Chibon 9811e6
            'branch_from': 'test',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['project']['date_created'] = '1516348115'
Aurélien Bompard 626417
        data['project']['date_modified'] = '1516348115'
Aurélien Bompard 626417
        data['repo_from']['date_created'] = '1516348115'
Aurélien Bompard 626417
        data['repo_from']['date_modified'] = '1516348115'
Aurélien Bompard 626417
        data['uid'] = 'e8b68df8711648deac67c3afed15a798'
Aurélien Bompard 626417
        data['commit_start'] = '114f1b468a5f05e635fcb6394273f3f907386eab'
Aurélien Bompard 626417
        data['commit_stop'] = '114f1b468a5f05e635fcb6394273f3f907386eab'
Aurélien Bompard 626417
        data['date_created'] = '1516348115'
Aurélien Bompard 626417
        data['last_updated'] = '1516348115'
Aurélien Bompard 626417
        data['updated_on'] = '1516348115'
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'assignee': None,
Aurélien Bompard 626417
                'branch': 'master',
Aurélien Bompard 626417
                'branch_from': 'test',
Aurélien Bompard 626417
                'cached_merge_status': 'unknown',
Aurélien Bompard 626417
                'closed_at': None,
Aurélien Bompard 626417
                'closed_by': None,
Aurélien Bompard 626417
                'comments': [],
Aurélien Bompard 626417
                'commit_start': '114f1b468a5f05e635fcb6394273f3f907386eab',
Aurélien Bompard 626417
                'commit_stop': '114f1b468a5f05e635fcb6394273f3f907386eab',
Aurélien Bompard 626417
                'date_created': '1516348115',
Aurélien Bompard 626417
                'id': 1,
Aurélien Bompard 626417
                'initial_comment': 'Nothing much, the changes speak for themselves',
Aurélien Bompard 626417
                'last_updated': '1516348115',
Aurélien Bompard 626417
                'project': {'access_groups': {'admin': [],
Aurélien Bompard 626417
                                                'commit': [],
Aurélien Bompard 626417
                                                'ticket':[]},
Aurélien Bompard 626417
                             'access_users': {'admin': [],
Aurélien Bompard 626417
                                               'commit': [],
Aurélien Bompard 626417
                                               'owner': ['pingou'],
Aurélien Bompard 626417
                                               'ticket': []},
Aurélien Bompard 626417
                             'close_status': ['Invalid',
Aurélien Bompard 626417
                                               'Insufficient data',
Aurélien Bompard 626417
                                               'Fixed',
Aurélien Bompard 626417
                                               'Duplicate'],
Aurélien Bompard 626417
                             'custom_keys': [],
Aurélien Bompard 626417
                             'date_created': '1516348115',
Aurélien Bompard 626417
                             'date_modified': '1516348115',
Aurélien Bompard 626417
                             'description': 'test project #1',
Aurélien Bompard 626417
                             'fullname': 'test',
Aurélien Bompard 626417
                             'id': 1,
Aurélien Bompard 626417
                             'milestones': {},
Aurélien Bompard 626417
                             'name': 'test',
Aurélien Bompard 626417
                             'namespace': None,
Aurélien Bompard 626417
                             'parent': None,
Aurélien Bompard 626417
                             'priorities': {},
Aurélien Bompard 626417
                             'tags': [],
Aurélien Bompard 626417
                             'url_path': 'test',
Aurélien Bompard 626417
                             'user': {'fullname': 'PY C', 'name': 'pingou'}},
Aurélien Bompard 626417
                'remote_git': None,
Aurélien Bompard 626417
                'repo_from': {'access_groups': {'admin': [],
Aurélien Bompard 626417
                                                  'commit': [],
Aurélien Bompard 626417
                                                  'ticket': []},
Aurélien Bompard 626417
                               'access_users': {'admin': [],
Aurélien Bompard 626417
                                                 'commit': [],
Aurélien Bompard 626417
                                                 'owner': ['pingou'],
Aurélien Bompard 626417
                                                 'ticket': []},
Aurélien Bompard 626417
                               'close_status': ['Invalid',
Aurélien Bompard 626417
                                                 'Insufficient data',
Aurélien Bompard 626417
                                                 'Fixed',
Aurélien Bompard 626417
                                                 'Duplicate'],
Aurélien Bompard 626417
                               'custom_keys': [],
Aurélien Bompard 626417
                               'date_created': '1516348115',
Aurélien Bompard 626417
                               'date_modified': '1516348115',
Aurélien Bompard 626417
                               'description': 'test project #1',
Aurélien Bompard 626417
                               'fullname': 'test',
Aurélien Bompard 626417
                               'id': 1,
Aurélien Bompard 626417
                               'milestones': {},
Aurélien Bompard 626417
                               'name': 'test',
Aurélien Bompard 626417
                               'namespace': None,
Aurélien Bompard 626417
                               'parent': None,
Aurélien Bompard 626417
                               'priorities': {},
Aurélien Bompard 626417
                               'tags': [],
Aurélien Bompard 626417
                               'url_path': 'test',
Aurélien Bompard 626417
                               'user': {'fullname': 'PY C', 'name': 'pingou'}},
Aurélien Bompard 626417
                'status': 'Open',
Aurélien Bompard 626417
                'title': 'Test PR',
Aurélien Bompard 626417
                'uid': 'e8b68df8711648deac67c3afed15a798',
Aurélien Bompard 626417
                'updated_on': '1516348115',
Aurélien Bompard 626417
                'user': {'fullname': 'PY C', 'name': 'pingou'}
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_missing_initial_comment(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not initial comment is submitted.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 9811e6
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon 9811e6
                                  bare=True)
Pierre-Yves Chibon 9811e6
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 9811e6
        tests.add_commit_git_repo(os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon 9811e6
                                  branch='test')
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 9811e6
            'title': 'Test PR',
Pierre-Yves Chibon 9811e6
            'branch_to': 'master',
Pierre-Yves Chibon 9811e6
            'branch_from': 'test',
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 9811e6
            '/api/0/test/pull-request/new', headers=headers, data=data)
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data['project']['date_created'] = '1516348115'
Aurélien Bompard 626417
        data['project']['date_modified'] = '1516348115'
Aurélien Bompard 626417
        data['repo_from']['date_created'] = '1516348115'
Aurélien Bompard 626417
        data['repo_from']['date_modified'] = '1516348115'
Aurélien Bompard 626417
        data['uid'] = 'e8b68df8711648deac67c3afed15a798'
Aurélien Bompard 626417
        data['commit_start'] = '114f1b468a5f05e635fcb6394273f3f907386eab'
Aurélien Bompard 626417
        data['commit_stop'] = '114f1b468a5f05e635fcb6394273f3f907386eab'
Aurélien Bompard 626417
        data['date_created'] = '1516348115'
Aurélien Bompard 626417
        data['last_updated'] = '1516348115'
Aurélien Bompard 626417
        data['updated_on'] = '1516348115'
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Aurélien Bompard 626417
                'assignee': None,
Aurélien Bompard 626417
                'branch': 'master',
Aurélien Bompard 626417
                'branch_from': 'test',
Aurélien Bompard 626417
                'cached_merge_status': 'unknown',
Aurélien Bompard 626417
                'closed_at': None,
Aurélien Bompard 626417
                'closed_by': None,
Aurélien Bompard 626417
                'comments': [],
Aurélien Bompard 626417
                'commit_start': '114f1b468a5f05e635fcb6394273f3f907386eab',
Aurélien Bompard 626417
                'commit_stop': '114f1b468a5f05e635fcb6394273f3f907386eab',
Aurélien Bompard 626417
                'date_created': '1516348115',
Aurélien Bompard 626417
                'id': 1,
Aurélien Bompard 626417
                'initial_comment': None,
Aurélien Bompard 626417
                'last_updated': '1516348115',
Aurélien Bompard 626417
                'project': {'access_groups': {'admin': [],
Aurélien Bompard 626417
                                                'commit': [],
Aurélien Bompard 626417
                                                'ticket':[]},
Aurélien Bompard 626417
                             'access_users': {'admin': [],
Aurélien Bompard 626417
                                               'commit': [],
Aurélien Bompard 626417
                                               'owner': ['pingou'],
Aurélien Bompard 626417
                                               'ticket': []},
Aurélien Bompard 626417
                             'close_status': ['Invalid',
Aurélien Bompard 626417
                                               'Insufficient data',
Aurélien Bompard 626417
                                               'Fixed',
Aurélien Bompard 626417
                                               'Duplicate'],
Aurélien Bompard 626417
                             'custom_keys': [],
Aurélien Bompard 626417
                             'date_created': '1516348115',
Aurélien Bompard 626417
                             'date_modified': '1516348115',
Aurélien Bompard 626417
                             'description': 'test project #1',
Aurélien Bompard 626417
                             'fullname': 'test',
Aurélien Bompard 626417
                             'id': 1,
Aurélien Bompard 626417
                             'milestones': {},
Aurélien Bompard 626417
                             'name': 'test',
Aurélien Bompard 626417
                             'namespace': None,
Aurélien Bompard 626417
                             'parent': None,
Aurélien Bompard 626417
                             'priorities': {},
Aurélien Bompard 626417
                             'tags': [],
Aurélien Bompard 626417
                             'url_path': 'test',
Aurélien Bompard 626417
                             'user': {'fullname': 'PY C', 'name': 'pingou'}},
Aurélien Bompard 626417
                'remote_git': None,
Aurélien Bompard 626417
                'repo_from': {'access_groups': {'admin': [],
Aurélien Bompard 626417
                                                  'commit': [],
Aurélien Bompard 626417
                                                  'ticket': []},
Aurélien Bompard 626417
                               'access_users': {'admin': [],
Aurélien Bompard 626417
                                                 'commit': [],
Aurélien Bompard 626417
                                                 'owner': ['pingou'],
Aurélien Bompard 626417
                                                 'ticket': []},
Aurélien Bompard 626417
                               'close_status': ['Invalid',
Aurélien Bompard 626417
                                                 'Insufficient data',
Aurélien Bompard 626417
                                                 'Fixed',
Aurélien Bompard 626417
                                                 'Duplicate'],
Aurélien Bompard 626417
                               'custom_keys': [],
Aurélien Bompard 626417
                               'date_created': '1516348115',
Aurélien Bompard 626417
                               'date_modified': '1516348115',
Aurélien Bompard 626417
                               'description': 'test project #1',
Aurélien Bompard 626417
                               'fullname': 'test',
Aurélien Bompard 626417
                               'id': 1,
Aurélien Bompard 626417
                               'milestones': {},
Aurélien Bompard 626417
                               'name': 'test',
Aurélien Bompard 626417
                               'namespace': None,
Aurélien Bompard 626417
                               'parent': None,
Aurélien Bompard 626417
                               'priorities': {},
Aurélien Bompard 626417
                               'tags': [],
Aurélien Bompard 626417
                               'url_path': 'test',
Aurélien Bompard 626417
                               'user': {'fullname': 'PY C', 'name': 'pingou'}},
Aurélien Bompard 626417
                'status': 'Open',
Aurélien Bompard 626417
                'title': 'Test PR',
Aurélien Bompard 626417
                'uid': 'e8b68df8711648deac67c3afed15a798',
Aurélien Bompard 626417
                'updated_on': '1516348115',
Aurélien Bompard 626417
                'user': {'fullname': 'PY C', 'name': 'pingou'}
Pierre-Yves Chibon 9811e6
            }
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon c67f41
class PagureFlaskApiForkPRDiffStatstests(tests.Modeltests):
Pierre-Yves Chibon c67f41
    """ Tests for the flask API of pagure for the diff stats endpoint of PRs
Pierre-Yves Chibon c67f41
    """
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
    maxDiff = None
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
    def setUp(self):
Pierre-Yves Chibon c67f41
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon c67f41
        super(PagureFlaskApiForkPRDiffStatstests, self).setUp()
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
        pagure.config.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
        tests.create_projects(self.session)
Pierre-Yves Chibon c67f41
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon c67f41
        tests.create_projects_git(os.path.join(self.path, 'requests'),
Pierre-Yves Chibon c67f41
                                  bare=True)
Pierre-Yves Chibon c67f41
        tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon c67f41
        tests.add_commit_git_repo(
Pierre-Yves Chibon c67f41
            os.path.join(self.path, 'repos', 'test.git'), ncommits=5)
Pierre-Yves Chibon c67f41
        tests.add_commit_git_repo(
Pierre-Yves Chibon c67f41
            os.path.join(self.path, 'repos', 'test.git'), branch='test')
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
        # Create the pull-request to close
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon c67f41
            session=self.session,
Pierre-Yves Chibon c67f41
            repo_from=repo,
Pierre-Yves Chibon c67f41
            branch_from='test',
Pierre-Yves Chibon c67f41
            repo_to=repo,
Pierre-Yves Chibon c67f41
            branch_to='master',
Pierre-Yves Chibon c67f41
            title='test pull-request',
Pierre-Yves Chibon c67f41
            user='pingou',
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
        self.session.commit()
Pierre-Yves Chibon c67f41
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon c67f41
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_no_repo(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon c67f41
        output = self.app.get('/api/0/invalid/pull-request/404/diffstats')
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertEqual(
Pierre-Yves Chibon c67f41
            data,
Pierre-Yves Chibon c67f41
            {'error': 'Project not found', 'error_code': 'ENOPROJECT'}
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_no_pr(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon c67f41
        output = self.app.get('/api/0/test/pull-request/404/diffstats')
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertEqual(
Pierre-Yves Chibon c67f41
            data,
Pierre-Yves Chibon c67f41
            {'error': 'Pull-Request not found', 'error_code': 'ENOREQ'}
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_file_modified(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon c67f41
        output = self.app.get('/api/0/test/pull-request/1/diffstats')
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertEqual(
Pierre-Yves Chibon c67f41
            data,
Pierre-Yves Chibon c67f41
            {
Pierre-Yves Chibon c67f41
                'sources': {
Pierre-Yves Chibon c67f41
                    'lines_added': 10,
Pierre-Yves Chibon c67f41
                    'lines_removed': 0,
Pierre-Yves Chibon c67f41
                    'old_path': 'sources',
Pierre-Yves Chibon c67f41
                    'status': 'M'
Pierre-Yves Chibon c67f41
                }
Pierre-Yves Chibon c67f41
            }
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_file_added_mofidied(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon c67f41
        tests.add_commit_git_repo(
Pierre-Yves Chibon c67f41
            os.path.join(self.path, 'repos', 'test.git'), ncommits=5)
Pierre-Yves Chibon c67f41
        tests.add_readme_git_repo(
Pierre-Yves Chibon c67f41
            os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon c67f41
            readme_name='README.md', branch='test')
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon c67f41
        self.assertEqual(len(repo.requests), 1)
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
        output = self.app.get('/api/0/test/pull-request/1/diffstats')
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertTrue(
Pierre-Yves Chibon c67f41
            data in
Pierre-Yves Chibon c67f41
            [
Pierre-Yves Chibon c67f41
                {
Pierre-Yves Chibon c67f41
                  "README.md": {
Pierre-Yves Chibon c67f41
                    "lines_added": 5,
Pierre-Yves Chibon c67f41
                    "lines_removed": 0,
Pierre-Yves Chibon c67f41
                    "old_path": "README.md",
Pierre-Yves Chibon c67f41
                    "status": "A"
Pierre-Yves Chibon c67f41
                  },
Pierre-Yves Chibon c67f41
                  "sources": {
Pierre-Yves Chibon c67f41
                    "lines_added": 5,
Pierre-Yves Chibon c67f41
                    "lines_removed": 0,
Pierre-Yves Chibon c67f41
                    "old_path": "sources",
Pierre-Yves Chibon c67f41
                    "status": "M"
Pierre-Yves Chibon c67f41
                  }
Pierre-Yves Chibon c67f41
                },
Pierre-Yves Chibon c67f41
                {
Pierre-Yves Chibon c67f41
                  "README.md": {
Pierre-Yves Chibon c67f41
                    "lines_added": 5,
Pierre-Yves Chibon c67f41
                    "lines_removed": 0,
Pierre-Yves Chibon c67f41
                    "old_path": "README.md",
Pierre-Yves Chibon c67f41
                    "status": "A"
Pierre-Yves Chibon c67f41
                  },
Pierre-Yves Chibon c67f41
                  "sources": {
Pierre-Yves Chibon c67f41
                    "lines_added": 10,
Pierre-Yves Chibon c67f41
                    "lines_removed": 0,
Pierre-Yves Chibon c67f41
                    "old_path": "sources",
Pierre-Yves Chibon c67f41
                    "status": "M"
Pierre-Yves Chibon c67f41
                  }
Pierre-Yves Chibon c67f41
                }
Pierre-Yves Chibon c67f41
            ]
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_file_modified_deleted(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon c67f41
        self.assertEqual(len(repo.requests), 1)
Pierre-Yves Chibon c67f41
        pagure.lib.tasks.update_pull_request(repo.requests[0].uid)
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
        tests.add_readme_git_repo(
Pierre-Yves Chibon c67f41
            os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon c67f41
            readme_name='README.md', branch='test')
Pierre-Yves Chibon c67f41
        tests.remove_file_git_repo(
Pierre-Yves Chibon c67f41
            os.path.join(self.path, 'repos', 'test.git'),
Pierre-Yves Chibon c67f41
            filename='sources', branch='test')
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon c67f41
        self.assertEqual(len(repo.requests), 1)
Pierre-Yves Chibon c67f41
        pagure.lib.tasks.update_pull_request(repo.requests[0].uid)
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
        output = self.app.get('/api/0/test/pull-request/1/diffstats')
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertEqual(
Pierre-Yves Chibon c67f41
            data,
Pierre-Yves Chibon c67f41
            {
Pierre-Yves Chibon c67f41
              "README.md": {
Pierre-Yves Chibon c67f41
                "lines_added": 5,
Pierre-Yves Chibon c67f41
                "lines_removed": 0,
Pierre-Yves Chibon c67f41
                "old_path": "README.md",
Pierre-Yves Chibon c67f41
                "status": "A"
Pierre-Yves Chibon c67f41
              },
Pierre-Yves Chibon c67f41
              "sources": {
Pierre-Yves Chibon c67f41
                "lines_added": 0,
Pierre-Yves Chibon c67f41
                "lines_removed": 5,
Pierre-Yves Chibon c67f41
                "old_path": "sources",
Pierre-Yves Chibon c67f41
                "status": "D"
Pierre-Yves Chibon c67f41
              }
Pierre-Yves Chibon c67f41
            }
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)