Blame tests/test_progit_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
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 c71c2d
from mock import patch
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 c71c2d
import pagure.lib
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 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 c71c2d
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon c71c2d
        pagure.SESSION = self.session
Pierre-Yves Chibon c71c2d
        pagure.api.SESSION = self.session
Pierre-Yves Chibon c71c2d
        pagure.api.fork.SESSION = self.session
Pierre-Yves Chibon c71c2d
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        pagure.APP.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon ad85b9
    def test_api_pull_request_views(self):
Pierre-Yves Chibon ad85b9
        """ Test the api_pull_request_views method of the flask api. """
Pierre-Yves Chibon ad85b9
        tests.create_projects(self.session)
Pierre-Yves Chibon ad85b9
        tests.create_tokens(self.session)
Pierre-Yves Chibon ad85b9
        tests.create_acls(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 ad85b9
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon ad85b9
        forked_repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon ad85b9
        msg = pagure.lib.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
            requestfolder=None,
Pierre-Yves Chibon ad85b9
        )
Pierre-Yves Chibon ad85b9
        self.session.commit()
Pierre-Yves Chibon ad85b9
        self.assertEqual(msg, 'Request created')
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)
Pierre-Yves Chibon ad85b9
        data = json.loads(output.data)
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)
Pierre-Yves Chibon ad85b9
        data = json.loads(output.data)
Pierre-Yves Chibon ad85b9
        data['requests'][0]['date_created'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data['requests'][0]['project']['date_created'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data['requests'][0]['repo_from']['date_created'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data['requests'][0]['uid'] = '1431414800'
Pierre-Yves Chibon ad85b9
        self.assertDictEqual(
Pierre-Yves Chibon ad85b9
            data,
Pierre-Yves Chibon ad85b9
            {
Pierre-Yves Chibon 316f15
              "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Pierre-Yves Chibon 316f15
                "status": True
Pierre-Yves Chibon 316f15
              },
Pierre-Yves Chibon ad85b9
              "requests": [
Pierre-Yves Chibon ad85b9
                {
Pierre-Yves Chibon ad85b9
                  "assignee": None,
Pierre-Yves Chibon ad85b9
                  "branch": "master",
Pierre-Yves Chibon ad85b9
                  "branch_from": "master",
Pierre-Yves Chibon ad85b9
                  "comments": [],
Pierre-Yves Chibon ad85b9
                  "commit_start": None,
Pierre-Yves Chibon ad85b9
                  "commit_stop": None,
Pierre-Yves Chibon ad85b9
                  "date_created": "1431414800",
Pierre-Yves Chibon ad85b9
                  "id": 1,
Pierre-Yves Chibon ad85b9
                  "project": {
Pierre-Yves Chibon ad85b9
                    "date_created": "1431414800",
Pierre-Yves Chibon ad85b9
                    "description": "test project #1",
Pierre-Yves Chibon ad85b9
                    "id": 1,
Pierre-Yves Chibon ad85b9
                    "name": "test",
Pierre-Yves Chibon ad85b9
                    "parent": None,
Pierre-Yves Chibon ad85b9
                    "settings": {
Pierre-Yves Chibon ad85b9
                      "Minimum_score_to_merge_pull-request": -1,
Pierre-Yves Chibon ad85b9
                      "Only_assignee_can_merge_pull-request": False,
Pierre-Yves Chibon ad85b9
                      "Web-hooks": None,
Pierre-Yves Chibon ad85b9
                      "issue_tracker": True,
Pierre-Yves Chibon ad85b9
                      "project_documentation": True,
Pierre-Yves Chibon ad85b9
                      "pull_requests": True
Pierre-Yves Chibon ad85b9
                    },
Pierre-Yves Chibon ad85b9
                    "user": {
Pierre-Yves Chibon ad85b9
                      "fullname": "PY C",
Pierre-Yves Chibon ad85b9
                      "name": "pingou"
Pierre-Yves Chibon ad85b9
                    }
Pierre-Yves Chibon ad85b9
                  },
Pierre-Yves Chibon ad85b9
                  "repo_from": {
Pierre-Yves Chibon ad85b9
                    "date_created": "1431414800",
Pierre-Yves Chibon ad85b9
                    "description": "test project #1",
Pierre-Yves Chibon ad85b9
                    "id": 1,
Pierre-Yves Chibon ad85b9
                    "name": "test",
Pierre-Yves Chibon ad85b9
                    "parent": None,
Pierre-Yves Chibon ad85b9
                    "settings": {
Pierre-Yves Chibon ad85b9
                      "Minimum_score_to_merge_pull-request": -1,
Pierre-Yves Chibon ad85b9
                      "Only_assignee_can_merge_pull-request": False,
Pierre-Yves Chibon ad85b9
                      "Web-hooks": None,
Pierre-Yves Chibon ad85b9
                      "issue_tracker": True,
Pierre-Yves Chibon ad85b9
                      "project_documentation": True,
Pierre-Yves Chibon ad85b9
                      "pull_requests": True
Pierre-Yves Chibon ad85b9
                    },
Pierre-Yves Chibon ad85b9
                    "user": {
Pierre-Yves Chibon ad85b9
                      "fullname": "PY C",
Pierre-Yves Chibon ad85b9
                      "name": "pingou"
Pierre-Yves Chibon ad85b9
                    }
Pierre-Yves Chibon ad85b9
                  },
Pierre-Yves Chibon ad85b9
                  "status": True,
Pierre-Yves Chibon ad85b9
                  "title": "test pull-request",
Pierre-Yves Chibon ad85b9
                  "uid": "1431414800",
Pierre-Yves Chibon ad85b9
                  "user": {
Pierre-Yves Chibon ad85b9
                    "fullname": "PY C",
Pierre-Yves Chibon ad85b9
                    "name": "pingou"
Pierre-Yves Chibon ad85b9
                  }
Pierre-Yves Chibon ad85b9
                }
Pierre-Yves Chibon 316f15
              ]
Pierre-Yves Chibon ad85b9
            }
Pierre-Yves Chibon ad85b9
        )
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)
Pierre-Yves Chibon ad85b9
        data2 = json.loads(output.data)
Pierre-Yves Chibon ad85b9
        data2['requests'][0]['date_created'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data2['requests'][0]['project']['date_created'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data2['requests'][0]['repo_from']['date_created'] = '1431414800'
Pierre-Yves Chibon ad85b9
        data2['requests'][0]['uid'] = '1431414800'
Pierre-Yves Chibon ad85b9
        self.assertDictEqual(data, data2)
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon c71c2d
    def test_api_pull_request_view(self):
Pierre-Yves Chibon c71c2d
        """ Test the api_pull_request_view method of the flask api. """
Pierre-Yves Chibon c71c2d
        tests.create_projects(self.session)
Pierre-Yves Chibon c71c2d
        tests.create_tokens(self.session)
Pierre-Yves Chibon c71c2d
        tests.create_acls(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 c71c2d
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon c71c2d
        forked_repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon c71c2d
        msg = pagure.lib.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
            requestfolder=None,
Pierre-Yves Chibon c71c2d
        )
Pierre-Yves Chibon c71c2d
        self.session.commit()
Pierre-Yves Chibon c71c2d
        self.assertEqual(msg, 'Request created')
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)
Pierre-Yves Chibon c71c2d
        data = json.loads(output.data)
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)
Pierre-Yves Chibon c71c2d
        data = json.loads(output.data)
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)
Pierre-Yves Chibon c71c2d
        data = json.loads(output.data)
Pierre-Yves Chibon c71c2d
        data['date_created'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data['project']['date_created'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data['repo_from']['date_created'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data['uid'] = '1431414800'
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(
Pierre-Yves Chibon c71c2d
            data,
Pierre-Yves Chibon c71c2d
            {
Pierre-Yves Chibon c71c2d
              "assignee": None,
Pierre-Yves Chibon c71c2d
              "branch": "master",
Pierre-Yves Chibon c71c2d
              "branch_from": "master",
Pierre-Yves Chibon c71c2d
              "comments": [],
Pierre-Yves Chibon c71c2d
              "commit_start": None,
Pierre-Yves Chibon c71c2d
              "commit_stop": None,
Pierre-Yves Chibon c71c2d
              "date_created": "1431414800",
Pierre-Yves Chibon c71c2d
              "id": 1,
Pierre-Yves Chibon c71c2d
              "project": {
Pierre-Yves Chibon c71c2d
                "date_created": "1431414800",
Pierre-Yves Chibon c71c2d
                "description": "test project #1",
Pierre-Yves Chibon c71c2d
                "id": 1,
Pierre-Yves Chibon c71c2d
                "name": "test",
Pierre-Yves Chibon c71c2d
                "parent": None,
Pierre-Yves Chibon c71c2d
                "settings": {
Pierre-Yves Chibon c71c2d
                  "Minimum_score_to_merge_pull-request": -1,
Pierre-Yves Chibon c71c2d
                  "Only_assignee_can_merge_pull-request": False,
Pierre-Yves Chibon c71c2d
                  "Web-hooks": None,
Pierre-Yves Chibon c71c2d
                  "issue_tracker": True,
Pierre-Yves Chibon c71c2d
                  "project_documentation": True,
Pierre-Yves Chibon c71c2d
                  "pull_requests": True
Pierre-Yves Chibon c71c2d
                },
Pierre-Yves Chibon c71c2d
                "user": {
Pierre-Yves Chibon c71c2d
                  "fullname": "PY C",
Pierre-Yves Chibon c71c2d
                  "name": "pingou"
Pierre-Yves Chibon c71c2d
                }
Pierre-Yves Chibon c71c2d
              },
Pierre-Yves Chibon c71c2d
              "repo_from": {
Pierre-Yves Chibon c71c2d
                "date_created": "1431414800",
Pierre-Yves Chibon c71c2d
                "description": "test project #1",
Pierre-Yves Chibon c71c2d
                "id": 1,
Pierre-Yves Chibon c71c2d
                "name": "test",
Pierre-Yves Chibon c71c2d
                "parent": None,
Pierre-Yves Chibon c71c2d
                "settings": {
Pierre-Yves Chibon c71c2d
                  "Minimum_score_to_merge_pull-request": -1,
Pierre-Yves Chibon c71c2d
                  "Only_assignee_can_merge_pull-request": False,
Pierre-Yves Chibon c71c2d
                  "Web-hooks": None,
Pierre-Yves Chibon c71c2d
                  "issue_tracker": True,
Pierre-Yves Chibon c71c2d
                  "project_documentation": True,
Pierre-Yves Chibon c71c2d
                  "pull_requests": True
Pierre-Yves Chibon c71c2d
                },
Pierre-Yves Chibon c71c2d
                "user": {
Pierre-Yves Chibon c71c2d
                  "fullname": "PY C",
Pierre-Yves Chibon c71c2d
                  "name": "pingou"
Pierre-Yves Chibon c71c2d
                }
Pierre-Yves Chibon c71c2d
              },
Pierre-Yves Chibon c71c2d
              "status": True,
Pierre-Yves Chibon c71c2d
              "title": "test pull-request",
Pierre-Yves Chibon c71c2d
              "uid": "1431414800",
Pierre-Yves Chibon c71c2d
              "user": {
Pierre-Yves Chibon c71c2d
                "fullname": "PY C",
Pierre-Yves Chibon c71c2d
                "name": "pingou"
Pierre-Yves Chibon c71c2d
              }
Pierre-Yves Chibon c71c2d
            }
Pierre-Yves Chibon c71c2d
        )
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)
Pierre-Yves Chibon c71c2d
        data2 = json.loads(output.data)
Pierre-Yves Chibon c71c2d
        data2['date_created'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data2['project']['date_created'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data2['repo_from']['date_created'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data2['uid'] = '1431414800'
Pierre-Yves Chibon c71c2d
        data2['date_created'] = '1431414800'
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(data, data2)
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 209c7e
    def test_api_pull_request_close(self):
Pierre-Yves Chibon 209c7e
        """ Test the api_pull_request_close method of the flask api. """
Pierre-Yves Chibon 209c7e
        tests.create_projects(self.session)
Pierre-Yves Chibon 209c7e
        tests.create_tokens(self.session)
Pierre-Yves Chibon 209c7e
        tests.create_acls(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 209c7e
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 209c7e
        forked_repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 209c7e
        msg = pagure.lib.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
            requestfolder=None,
Pierre-Yves Chibon 209c7e
        )
Pierre-Yves Chibon 209c7e
        self.session.commit()
Pierre-Yves Chibon 209c7e
        self.assertEqual(msg, 'Request created')
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)
Pierre-Yves Chibon 209c7e
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 209c7e
        data = json.loads(output.data)
Pierre-Yves Chibon 209c7e
        self.assertDictEqual(
Pierre-Yves Chibon 209c7e
            data,
Pierre-Yves Chibon 209c7e
            {
Pierre-Yves Chibon 209c7e
              "error": "Invalid or expired token. Please visit " \
Pierre-Yves Chibon 1e2669
                  "https://pagure.org/ to get or renew your API token.",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 209c7e
            }
Pierre-Yves Chibon 209c7e
        )
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)
Pierre-Yves Chibon f12b4a
        data = json.loads(output.data)
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 f12b4a
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon f12b4a
            token_id='foobar_token',
Pierre-Yves Chibon f12b4a
            acl_id=6,
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)
Pierre-Yves Chibon f12b4a
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 209c7e
        data = json.loads(output.data)
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 a9c964
    @patch('pagure.lib.git.merge_pull_request')
Pierre-Yves Chibon a9c964
    def test_api_pull_request_merge(self, mpr):
Pierre-Yves Chibon a9c964
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon a9c964
        mpr.return_value = 'Changes merged!'
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        tests.create_projects(self.session)
Pierre-Yves Chibon a9c964
        tests.create_tokens(self.session)
Pierre-Yves Chibon a9c964
        tests.create_acls(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 a9c964
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon a9c964
        forked_repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon a9c964
        msg = pagure.lib.new_pull_request(
Pierre-Yves Chibon a9c964
            session=self.session,
Pierre-Yves Chibon a9c964
            repo_from=forked_repo,
Pierre-Yves Chibon a9c964
            branch_from='master',
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
            requestfolder=None,
Pierre-Yves Chibon a9c964
        )
Pierre-Yves Chibon a9c964
        self.session.commit()
Pierre-Yves Chibon a9c964
        self.assertEqual(msg, 'Request created')
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)
Pierre-Yves Chibon a9c964
        data = json.loads(output.data)
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)
Pierre-Yves Chibon a9c964
        data = json.loads(output.data)
Pierre-Yves Chibon a9c964
        self.assertDictEqual(
Pierre-Yves Chibon a9c964
            data,
Pierre-Yves Chibon a9c964
            {
Pierre-Yves Chibon a9c964
              "error": "Invalid or expired token. Please visit " \
Pierre-Yves Chibon 1e2669
                  "https://pagure.org/ to get or renew your API token.",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDTOK",
Pierre-Yves Chibon a9c964
            }
Pierre-Yves Chibon a9c964
        )
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)
Pierre-Yves Chibon f12b4a
        data = json.loads(output.data)
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 f12b4a
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon f12b4a
            token_id='foobar_token',
Pierre-Yves Chibon f12b4a
            acl_id=2,
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/merge', headers=headers)
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon f12b4a
        data = json.loads(output.data)
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)
Pierre-Yves Chibon a9c964
        data = json.loads(output.data)
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 9ba77b
    def test_api_pull_request_add_comment(self):
Pierre-Yves Chibon 9ba77b
        """ Test the api_pull_request_add_comment method of the flask api. """
Pierre-Yves Chibon 9ba77b
        tests.create_projects(self.session)
Pierre-Yves Chibon 9ba77b
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9ba77b
        tests.create_acls(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)
Pierre-Yves Chibon 9ba77b
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 9ba77b
        data = json.loads(output.data)
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 9ba77b
            data,
Pierre-Yves Chibon 9ba77b
            {
Pierre-Yves Chibon 9ba77b
              "error": "Invalid or expired token. Please visit " \
Pierre-Yves Chibon 1e2669
                  "https://pagure.org/ to get or renew your API token.",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 9ba77b
            }
Pierre-Yves Chibon 9ba77b
        )
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)
Pierre-Yves Chibon 9ba77b
        data = json.loads(output.data)
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 9ba77b
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 9ba77b
        forked_repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 9ba77b
        msg = pagure.lib.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
            requestfolder=None,
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
        self.session.commit()
Pierre-Yves Chibon 9ba77b
        self.assertEqual(msg, 'Request created')
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Check comments before
Pierre-Yves Chibon 9ba77b
        request = pagure.lib.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)
Pierre-Yves Chibon 9ba77b
        data = json.loads(output.data)
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 9ba77b
            data,
Pierre-Yves Chibon 9ba77b
            {
Pierre-Yves Chibon 9ba77b
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 9ba77b
            }
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # No change
Pierre-Yves Chibon 9ba77b
        request = pagure.lib.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)
Pierre-Yves Chibon 9ba77b
        data = json.loads(output.data)
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
Pierre-Yves Chibon 9ba77b
        request = pagure.lib.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 c71c2d
Pierre-Yves Chibon c71c2d
if __name__ == '__main__':
Pierre-Yves Chibon c71c2d
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon c71c2d
        PagureFlaskApiForktests)
Pierre-Yves Chibon c71c2d
    unittest.TextTestRunner(verbosity=2).run(SUITE)