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
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 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 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 f9c5f9
        req = 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 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)
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 44258a
        data['requests'][0]['updated_on'] = '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 226628
              "total_requests": 1,
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 bf72a2
                  "closed_at": None,
Pierre-Yves Chibon 94a9aa
                  "closed_by": None,
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 10ca8a
                  "initial_comment": None,
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 733a9d
                    "namespace": None,
Pierre-Yves Chibon ad85b9
                    "parent": None,
Pierre-Yves Chibon 14bde2
                    "priorities": {},
Pierre-Yves Chibon a30916
                    "tags": [],
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 ee5445
                  "remote_git": None,
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 733a9d
                    "namespace": None,
Pierre-Yves Chibon ad85b9
                    "parent": None,
Pierre-Yves Chibon 14bde2
                    "priorities": {},
Pierre-Yves Chibon a30916
                    "tags": [],
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 01a29a
                  "status": 'Open',
Pierre-Yves Chibon ad85b9
                  "title": "test pull-request",
Pierre-Yves Chibon ad85b9
                  "uid": "1431414800",
Pierre-Yves Chibon 44258a
                  "updated_on": "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 44258a
        data2['requests'][0]['updated_on'] = '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 7cc2d4
    @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 7cc2d4
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 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 f9c5f9
        req = 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 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)
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 44258a
        data['updated_on'] = '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 bf72a2
              "closed_at": None,
Pierre-Yves Chibon 94a9aa
              "closed_by": None,
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 10ca8a
              "initial_comment": None,
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 733a9d
                "namespace": None,
Pierre-Yves Chibon c71c2d
                "parent": None,
Pierre-Yves Chibon 14bde2
                "priorities": {},
Pierre-Yves Chibon a30916
                "tags": [],
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 ee5445
              "remote_git": None,
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 733a9d
                "namespace": None,
Pierre-Yves Chibon c71c2d
                "parent": None,
Pierre-Yves Chibon 14bde2
                "priorities": {},
Pierre-Yves Chibon a30916
                "tags": [],
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 01a29a
              "status": 'Open',
Pierre-Yves Chibon c71c2d
              "title": "test pull-request",
Pierre-Yves Chibon c71c2d
              "uid": "1431414800",
Pierre-Yves Chibon 44258a
              "updated_on": "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 44258a
        data2['updated_on'] = '1431414800'
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(data, data2)
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 7cc2d4
    @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 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 f9c5f9
        req = 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 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)
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 c02e60
Pierre-Yves Chibon c02e60
        # Allow the token to close PR
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon f12b4a
            token_id='foobar_token',
Pierre-Yves Chibon d60504
            acl_id=7,
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 7cc2d4
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon a9c964
    @patch('pagure.lib.git.merge_pull_request')
Pierre-Yves Chibon 7cc2d4
    def test_api_pull_request_merge(self, mpr, send_email):
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 7cc2d4
        send_email.return_value = True
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_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 f9c5f9
        req = 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 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)
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 c02e60
Pierre-Yves Chibon c02e60
        # Allow the token to merge PR
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon f12b4a
            token_id='foobar_token',
Pierre-Yves Chibon d60504
            acl_id=10,
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 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)
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 f9c5f9
        req = 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 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
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 e18f81
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon e18f81
    def test_api_pull_request_add_flag(self, mockemail):
Pierre-Yves Chibon e18f81
        """ Test the api_pull_request_add_flag method of the flask api. """
Pierre-Yves Chibon e18f81
        mockemail.return_value = True
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        tests.create_projects(self.session)
Pierre-Yves Chibon e18f81
        tests.create_tokens(self.session)
Pierre-Yves Chibon e18f81
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # Invalid project
Pierre-Yves Chibon e18f81
        output = self.app.post(
Pierre-Yves Chibon e18f81
            '/api/0/foo/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon e18f81
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon e18f81
        data = json.loads(output.data)
Pierre-Yves Chibon e18f81
        self.assertDictEqual(
Pierre-Yves Chibon e18f81
            data,
Pierre-Yves Chibon e18f81
            {
Pierre-Yves Chibon e18f81
              "error": "Project not found",
Pierre-Yves Chibon e18f81
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon e18f81
            }
Pierre-Yves Chibon e18f81
        )
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # Valid token, wrong project
Pierre-Yves Chibon e18f81
        output = self.app.post(
Pierre-Yves Chibon e18f81
            '/api/0/test2/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon e18f81
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon e18f81
        data = json.loads(output.data)
Pierre-Yves Chibon e18f81
        self.assertDictEqual(
Pierre-Yves Chibon e18f81
            data,
Pierre-Yves Chibon e18f81
            {
Pierre-Yves Chibon e18f81
              "error": "Invalid or expired token. Please visit " \
Pierre-Yves Chibon e18f81
                  "https://pagure.org/ to get or renew your API token.",
Pierre-Yves Chibon e18f81
              "error_code": "EINVALIDTOK",
Pierre-Yves Chibon e18f81
            }
Pierre-Yves Chibon e18f81
        )
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # No input
Pierre-Yves Chibon e18f81
        output = self.app.post(
Pierre-Yves Chibon e18f81
            '/api/0/test/pull-request/1/flag', headers=headers)
Pierre-Yves Chibon e18f81
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon e18f81
        data = json.loads(output.data)
Pierre-Yves Chibon e18f81
        self.assertDictEqual(
Pierre-Yves Chibon e18f81
            data,
Pierre-Yves Chibon e18f81
            {
Pierre-Yves Chibon e18f81
              "error": "Pull-Request not found",
Pierre-Yves Chibon e18f81
              "error_code": "ENOREQ",
Pierre-Yves Chibon e18f81
            }
Pierre-Yves Chibon e18f81
        )
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # Create a pull-request
Pierre-Yves Chibon e18f81
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon e18f81
        forked_repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon f9c5f9
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon e18f81
            session=self.session,
Pierre-Yves Chibon e18f81
            repo_from=forked_repo,
Pierre-Yves Chibon e18f81
            branch_from='master',
Pierre-Yves Chibon e18f81
            repo_to=repo,
Pierre-Yves Chibon e18f81
            branch_to='master',
Pierre-Yves Chibon e18f81
            title='test pull-request',
Pierre-Yves Chibon e18f81
            user='pingou',
Pierre-Yves Chibon e18f81
            requestfolder=None,
Pierre-Yves Chibon e18f81
        )
Pierre-Yves Chibon e18f81
        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 e18f81
Pierre-Yves Chibon e18f81
        # Check comments before
Pierre-Yves Chibon e18f81
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon e18f81
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon e18f81
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        data = {
Pierre-Yves Chibon e18f81
            'username': 'Jenkins',
Pierre-Yves Chibon e18f81
            'percent': 100,
Pierre-Yves Chibon e18f81
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon e18f81
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e18f81
        }
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # Incomplete request
Pierre-Yves Chibon e18f81
        output = self.app.post(
Pierre-Yves Chibon e18f81
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon e18f81
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon e18f81
        data = json.loads(output.data)
Pierre-Yves Chibon e18f81
        self.assertDictEqual(
Pierre-Yves Chibon e18f81
            data,
Pierre-Yves Chibon e18f81
            {
Pierre-Yves Chibon e18f81
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon e18f81
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon e18f81
            }
Pierre-Yves Chibon e18f81
        )
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # No change
Pierre-Yves Chibon e18f81
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon e18f81
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon e18f81
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        data = {
Pierre-Yves Chibon e18f81
            'username': 'Jenkins',
Pierre-Yves Chibon e18f81
            'percent': 0,
Pierre-Yves Chibon e18f81
            'comment': 'Tests failed',
Pierre-Yves Chibon e18f81
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon e18f81
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e18f81
        }
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # Valid request
Pierre-Yves Chibon e18f81
        output = self.app.post(
Pierre-Yves Chibon e18f81
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon e18f81
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon e18f81
        data = json.loads(output.data)
Pierre-Yves Chibon e18f81
        self.assertDictEqual(
Pierre-Yves Chibon e18f81
            data,
Pierre-Yves Chibon e18f81
            {'message': 'Flag added'}
Pierre-Yves Chibon e18f81
        )
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # One flag added
Pierre-Yves Chibon e18f81
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon e18f81
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon e18f81
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon e18f81
        self.assertEqual(request.flags[0].comment, 'Tests failed')
Pierre-Yves Chibon e18f81
        self.assertEqual(request.flags[0].percent, 0)
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # Update flag
Pierre-Yves Chibon e18f81
        data = {
Pierre-Yves Chibon e18f81
            'username': 'Jenkins',
Pierre-Yves Chibon e18f81
            'percent': 100,
Pierre-Yves Chibon e18f81
            'comment': 'Tests passed',
Pierre-Yves Chibon e18f81
            'url': 'http://jenkins.cloud.fedoraproject.org/',
Pierre-Yves Chibon e18f81
            'uid': 'jenkins_build_pagure_100+seed',
Pierre-Yves Chibon e18f81
        }
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        output = self.app.post(
Pierre-Yves Chibon e18f81
            '/api/0/test/pull-request/1/flag', data=data, headers=headers)
Pierre-Yves Chibon e18f81
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon e18f81
        data = json.loads(output.data)
Pierre-Yves Chibon e18f81
        self.assertDictEqual(
Pierre-Yves Chibon e18f81
            data,
Pierre-Yves Chibon e18f81
            {'message': 'Flag updated'}
Pierre-Yves Chibon e18f81
        )
Pierre-Yves Chibon e18f81
Pierre-Yves Chibon e18f81
        # One flag added
Pierre-Yves Chibon e18f81
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon e18f81
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon e18f81
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon e18f81
        self.assertEqual(request.flags[0].comment, 'Tests passed')
Pierre-Yves Chibon e18f81
        self.assertEqual(request.flags[0].percent, 100)
Pierre-Yves Chibon e18f81
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)