Blame tests/test_progit_flask_api_issue.py

Pierre-Yves Chibon 993597
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
"""
Pierre-Yves Chibon 993597
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
 Authors:
Pierre-Yves Chibon 993597
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
"""
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 993597
import pkg_resources
Pierre-Yves Chibon 993597
Pierre-Yves Chibon f69837
import datetime
Pierre-Yves Chibon 993597
import unittest
Pierre-Yves Chibon 993597
import shutil
Pierre-Yves Chibon 993597
import sys
Pierre-Yves Chibon 993597
import os
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
import json
Pierre-Yves Chibon 993597
from mock import patch
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 993597
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
import pagure.lib
Pierre-Yves Chibon 993597
import tests
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
class PagureFlaskApiIssuetests(tests.Modeltests):
Pierre-Yves Chibon 993597
    """ Tests for the flask API of pagure for issue """
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
    def setUp(self):
Pierre-Yves Chibon 993597
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 993597
        super(PagureFlaskApiIssuetests, self).setUp()
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 993597
        pagure.SESSION = self.session
Pierre-Yves Chibon 993597
        pagure.api.SESSION = self.session
Pierre-Yves Chibon 993597
        pagure.api.issue.SESSION = self.session
Pierre-Yves Chibon 993597
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon e5c5fc
Pierre-Yves Chibon 0e5cb2
        pagure.APP.config['TICKETS_FOLDER'] = None
Pierre-Yves Chibon e5c5fc
Pierre-Yves Chibon 993597
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
    def test_api_new_issue(self):
Pierre-Yves Chibon 3e3362
        """ Test the api_new_issue method of the flask api. """
Pierre-Yves Chibon 993597
        tests.create_projects(self.session)
Pierre-Yves Chibon e5c5fc
        tests.create_projects_git(os.path.join(tests.HERE, 'tickets'))
Pierre-Yves Chibon 993597
        tests.create_tokens(self.session)
Pierre-Yves Chibon 993597
        tests.create_acls(self.session)
Pierre-Yves Chibon 993597
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
        # Valid token, wrong project
Pierre-Yves Chibon 993597
        output = self.app.post('/api/0/test2/new_issue', headers=headers)
Pierre-Yves Chibon 993597
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 993597
        data = json.loads(output.data)
Pierre-Yves Chibon 993597
        self.assertDictEqual(
Pierre-Yves Chibon 993597
            data,
Pierre-Yves Chibon 993597
            {
Pierre-Yves Chibon 993597
              "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 993597
            }
Pierre-Yves Chibon 993597
        )
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
        # No input
Pierre-Yves Chibon 993597
        output = self.app.post('/api/0/test/new_issue', headers=headers)
Pierre-Yves Chibon 993597
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 993597
        data = json.loads(output.data)
Pierre-Yves Chibon 993597
        self.assertDictEqual(
Pierre-Yves Chibon 993597
            data,
Pierre-Yves Chibon 993597
            {
Pierre-Yves Chibon 993597
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 993597
            }
Pierre-Yves Chibon 993597
        )
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
        data = {
Pierre-Yves Chibon 993597
            'title': 'test issue',
Pierre-Yves Chibon 993597
        }
Pierre-Yves Chibon 993597
Pierre-Yves Chibon f69837
        # Invalid repo
Pierre-Yves Chibon f69837
        output = self.app.post(
Pierre-Yves Chibon f69837
            '/api/0/foo/new_issue', data=data, headers=headers)
Pierre-Yves Chibon f69837
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon f69837
        data = json.loads(output.data)
Pierre-Yves Chibon f69837
        self.assertDictEqual(
Pierre-Yves Chibon f69837
            data,
Pierre-Yves Chibon f69837
            {
Pierre-Yves Chibon f69837
              "error": "Project not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon f69837
            }
Pierre-Yves Chibon f69837
        )
Pierre-Yves Chibon f69837
Pierre-Yves Chibon 993597
        # Incomplete request
Pierre-Yves Chibon 993597
        output = self.app.post(
Pierre-Yves Chibon 993597
            '/api/0/test/new_issue', data=data, headers=headers)
Pierre-Yves Chibon 993597
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 993597
        data = json.loads(output.data)
Pierre-Yves Chibon 993597
        self.assertDictEqual(
Pierre-Yves Chibon 993597
            data,
Pierre-Yves Chibon 993597
            {
Pierre-Yves Chibon 993597
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 993597
            }
Pierre-Yves Chibon 993597
        )
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
        data = {
Pierre-Yves Chibon 993597
            'title': 'test issue',
Pierre-Yves Chibon 993597
            'issue_content': 'This issue needs attention',
Pierre-Yves Chibon 993597
            'status': 'Open',
Pierre-Yves Chibon 993597
        }
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
        # Valid request
Pierre-Yves Chibon 993597
        output = self.app.post(
Pierre-Yves Chibon 993597
            '/api/0/test/new_issue', data=data, headers=headers)
Pierre-Yves Chibon 993597
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993597
        data = json.loads(output.data)
Pierre-Yves Chibon 993597
        self.assertDictEqual(
Pierre-Yves Chibon 993597
            data,
Pierre-Yves Chibon ee68c9
            {'message': 'Issue created'}
Pierre-Yves Chibon 993597
        )
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 13e2e9
    def test_api_view_issues(self):
Pierre-Yves Chibon 13e2e9
        """ Test the api_view_issues method of the flask api. """
Pierre-Yves Chibon 13e2e9
        self.test_api_new_issue()
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        # Invalid repo
Pierre-Yves Chibon 13e2e9
        output = self.app.get('/api/0/foo/issues')
Pierre-Yves Chibon 13e2e9
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 13e2e9
        data = json.loads(output.data)
Pierre-Yves Chibon 13e2e9
        self.assertDictEqual(
Pierre-Yves Chibon 13e2e9
            data,
Pierre-Yves Chibon 13e2e9
            {
Pierre-Yves Chibon 13e2e9
              "error": "Project not found",
Pierre-Yves Chibon 13e2e9
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon 13e2e9
            }
Pierre-Yves Chibon 13e2e9
        )
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        # List all opened issues
Pierre-Yves Chibon 13e2e9
        output = self.app.get('/api/0/test/issues')
Pierre-Yves Chibon 13e2e9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 13e2e9
        data = json.loads(output.data)
Pierre-Yves Chibon 13e2e9
        data['issues'][0]['date_created'] = '1431414800'
Pierre-Yves Chibon 13e2e9
        self.assertDictEqual(
Pierre-Yves Chibon 13e2e9
            data,
Pierre-Yves Chibon 13e2e9
            {
Pierre-Yves Chibon 316f15
              "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Pierre-Yves Chibon 316f15
                "status": None,
Pierre-Yves Chibon 316f15
                "tags": []
Pierre-Yves Chibon 316f15
              },
Pierre-Yves Chibon 13e2e9
              "issues": [
Pierre-Yves Chibon 13e2e9
                {
Pierre-Yves Chibon 13e2e9
                  "assignee": None,
Pierre-Yves Chibon 13e2e9
                  "blocks": [],
Pierre-Yves Chibon 13e2e9
                  "comments": [],
Pierre-Yves Chibon 13e2e9
                  "content": "This issue needs attention",
Pierre-Yves Chibon 13e2e9
                  "date_created": "1431414800",
Pierre-Yves Chibon 13e2e9
                  "depends": [],
Pierre-Yves Chibon 13e2e9
                  "id": 1,
Pierre-Yves Chibon 13e2e9
                  "private": False,
Pierre-Yves Chibon 13e2e9
                  "status": "Open",
Pierre-Yves Chibon 13e2e9
                  "tags": [],
Pierre-Yves Chibon 13e2e9
                  "title": "test issue",
Pierre-Yves Chibon 13e2e9
                  "user": {
Pierre-Yves Chibon 13e2e9
                    "fullname": "PY C",
Pierre-Yves Chibon 13e2e9
                    "name": "pingou"
Pierre-Yves Chibon 13e2e9
                  }
Pierre-Yves Chibon 13e2e9
                }
Pierre-Yves Chibon 316f15
              ]
Pierre-Yves Chibon 13e2e9
            }
Pierre-Yves Chibon 13e2e9
        )
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        # Create private issue
Pierre-Yves Chibon 13e2e9
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 13e2e9
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 13e2e9
            session=self.session,
Pierre-Yves Chibon 13e2e9
            repo=repo,
Pierre-Yves Chibon 13e2e9
            title='Test issue',
Pierre-Yves Chibon 13e2e9
            content='We should work on this',
Pierre-Yves Chibon 13e2e9
            user='pingou',
Pierre-Yves Chibon 13e2e9
            ticketfolder=None,
Pierre-Yves Chibon 13e2e9
            private=True,
Pierre-Yves Chibon 13e2e9
        )
Pierre-Yves Chibon 13e2e9
        self.session.commit()
Pierre-Yves Chibon 13e2e9
        self.assertEqual(msg.title, 'Test issue')
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        # Access issues un-authenticated
Pierre-Yves Chibon 13e2e9
        output = self.app.get('/api/0/test/issues')
Pierre-Yves Chibon 13e2e9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 13e2e9
        data = json.loads(output.data)
Pierre-Yves Chibon 13e2e9
        data['issues'][0]['date_created'] = '1431414800'
Pierre-Yves Chibon 13e2e9
        self.assertDictEqual(
Pierre-Yves Chibon 13e2e9
            data,
Pierre-Yves Chibon 13e2e9
            {
Pierre-Yves Chibon 316f15
              "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Pierre-Yves Chibon 316f15
                "status": None,
Pierre-Yves Chibon 316f15
                "tags": []
Pierre-Yves Chibon 316f15
              },
Pierre-Yves Chibon 13e2e9
              "issues": [
Pierre-Yves Chibon 13e2e9
                {
Pierre-Yves Chibon 13e2e9
                  "assignee": None,
Pierre-Yves Chibon 13e2e9
                  "blocks": [],
Pierre-Yves Chibon 13e2e9
                  "comments": [],
Pierre-Yves Chibon 13e2e9
                  "content": "This issue needs attention",
Pierre-Yves Chibon 13e2e9
                  "date_created": "1431414800",
Pierre-Yves Chibon 13e2e9
                  "depends": [],
Pierre-Yves Chibon 13e2e9
                  "id": 1,
Pierre-Yves Chibon 13e2e9
                  "private": False,
Pierre-Yves Chibon 13e2e9
                  "status": "Open",
Pierre-Yves Chibon 13e2e9
                  "tags": [],
Pierre-Yves Chibon 13e2e9
                  "title": "test issue",
Pierre-Yves Chibon 13e2e9
                  "user": {
Pierre-Yves Chibon 13e2e9
                    "fullname": "PY C",
Pierre-Yves Chibon 13e2e9
                    "name": "pingou"
Pierre-Yves Chibon 13e2e9
                  }
Pierre-Yves Chibon 13e2e9
                }
Pierre-Yves Chibon 316f15
              ]
Pierre-Yves Chibon 13e2e9
            }
Pierre-Yves Chibon 13e2e9
        )
Pierre-Yves Chibon 13e2e9
        headers = {'Authorization': 'token aaabbbccc'}
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        # Access issues authenticated but wrong token
Pierre-Yves Chibon 13e2e9
        output = self.app.get('/api/0/test/issues', headers=headers)
Pierre-Yves Chibon 13e2e9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 13e2e9
        data = json.loads(output.data)
Pierre-Yves Chibon 13e2e9
        data['issues'][0]['date_created'] = '1431414800'
Pierre-Yves Chibon 13e2e9
        self.assertDictEqual(
Pierre-Yves Chibon 13e2e9
            data,
Pierre-Yves Chibon 13e2e9
            {
Pierre-Yves Chibon 316f15
              "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Pierre-Yves Chibon 316f15
                "status": None,
Pierre-Yves Chibon 316f15
                "tags": []
Pierre-Yves Chibon 316f15
              },
Pierre-Yves Chibon 13e2e9
              "issues": [
Pierre-Yves Chibon 13e2e9
                {
Pierre-Yves Chibon 13e2e9
                  "assignee": None,
Pierre-Yves Chibon 13e2e9
                  "blocks": [],
Pierre-Yves Chibon 13e2e9
                  "comments": [],
Pierre-Yves Chibon 13e2e9
                  "content": "This issue needs attention",
Pierre-Yves Chibon 13e2e9
                  "date_created": "1431414800",
Pierre-Yves Chibon 13e2e9
                  "depends": [],
Pierre-Yves Chibon 13e2e9
                  "id": 1,
Pierre-Yves Chibon 13e2e9
                  "private": False,
Pierre-Yves Chibon 13e2e9
                  "status": "Open",
Pierre-Yves Chibon 13e2e9
                  "tags": [],
Pierre-Yves Chibon 13e2e9
                  "title": "test issue",
Pierre-Yves Chibon 13e2e9
                  "user": {
Pierre-Yves Chibon 13e2e9
                    "fullname": "PY C",
Pierre-Yves Chibon 13e2e9
                    "name": "pingou"
Pierre-Yves Chibon 13e2e9
                  }
Pierre-Yves Chibon 13e2e9
                }
Pierre-Yves Chibon 316f15
              ]
Pierre-Yves Chibon 13e2e9
            }
Pierre-Yves Chibon 13e2e9
        )
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        # Access issues authenticated correctly
Pierre-Yves Chibon 13e2e9
        output = self.app.get('/api/0/test/issues', headers=headers)
Pierre-Yves Chibon 13e2e9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 13e2e9
        data = json.loads(output.data)
Pierre-Yves Chibon 13e2e9
        data['issues'][0]['date_created'] = '1431414800'
Pierre-Yves Chibon 13e2e9
        data['issues'][1]['date_created'] = '1431414800'
Pierre-Yves Chibon 13e2e9
        self.assertDictEqual(
Pierre-Yves Chibon 13e2e9
            data,
Pierre-Yves Chibon 13e2e9
            {
Pierre-Yves Chibon 316f15
              "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Pierre-Yves Chibon 316f15
                "status": None,
Pierre-Yves Chibon 316f15
                "tags": []
Pierre-Yves Chibon 316f15
              },
Pierre-Yves Chibon 13e2e9
              "issues": [
Pierre-Yves Chibon 13e2e9
                {
Pierre-Yves Chibon 13e2e9
                  "assignee": None,
Pierre-Yves Chibon 13e2e9
                  "blocks": [],
Pierre-Yves Chibon 13e2e9
                  "comments": [],
Pierre-Yves Chibon 13e2e9
                  "content": "This issue needs attention",
Pierre-Yves Chibon 13e2e9
                  "date_created": "1431414800",
Pierre-Yves Chibon 13e2e9
                  "depends": [],
Pierre-Yves Chibon 13e2e9
                  "id": 1,
Pierre-Yves Chibon 13e2e9
                  "private": False,
Pierre-Yves Chibon 13e2e9
                  "status": "Open",
Pierre-Yves Chibon 13e2e9
                  "tags": [],
Pierre-Yves Chibon 13e2e9
                  "title": "test issue",
Pierre-Yves Chibon 13e2e9
                  "user": {
Pierre-Yves Chibon 13e2e9
                    "fullname": "PY C",
Pierre-Yves Chibon 13e2e9
                    "name": "pingou"
Pierre-Yves Chibon 13e2e9
                  }
Pierre-Yves Chibon 13e2e9
                },
Pierre-Yves Chibon 13e2e9
                {
Pierre-Yves Chibon 13e2e9
                  "assignee": None,
Pierre-Yves Chibon 13e2e9
                  "blocks": [],
Pierre-Yves Chibon 13e2e9
                  "comments": [],
Pierre-Yves Chibon 13e2e9
                  "content": "We should work on this",
Pierre-Yves Chibon 13e2e9
                  "date_created": "1431414800",
Pierre-Yves Chibon 13e2e9
                  "depends": [],
Pierre-Yves Chibon 13e2e9
                  "id": 2,
Pierre-Yves Chibon 13e2e9
                  "private": True,
Pierre-Yves Chibon 13e2e9
                  "status": "Open",
Pierre-Yves Chibon 13e2e9
                  "tags": [],
Pierre-Yves Chibon 13e2e9
                  "title": "Test issue",
Pierre-Yves Chibon 13e2e9
                  "user": {
Pierre-Yves Chibon 13e2e9
                    "fullname": "PY C",
Pierre-Yves Chibon 13e2e9
                    "name": "pingou"
Pierre-Yves Chibon 13e2e9
                  }
Pierre-Yves Chibon 13e2e9
                }
Pierre-Yves Chibon 316f15
              ]
Pierre-Yves Chibon 13e2e9
            }
Pierre-Yves Chibon 13e2e9
        )
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        # List closed issue
Pierre-Yves Chibon 13e2e9
        output = self.app.get('/api/0/test/issues?status=Closed', headers=headers)
Pierre-Yves Chibon 13e2e9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 13e2e9
        data = json.loads(output.data)
Pierre-Yves Chibon 13e2e9
        self.assertDictEqual(
Pierre-Yves Chibon 13e2e9
            data,
Pierre-Yves Chibon 13e2e9
            {
Pierre-Yves Chibon 316f15
              "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Pierre-Yves Chibon 316f15
                "status": "Closed",
Pierre-Yves Chibon 316f15
                "tags": []
Pierre-Yves Chibon 316f15
              },
Pierre-Yves Chibon 316f15
              "issues": []
Pierre-Yves Chibon 13e2e9
            }
Pierre-Yves Chibon 13e2e9
        )
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 13e2e9
        # List closed issue
Pierre-Yves Chibon 13e2e9
        output = self.app.get('/api/0/test/issues?status=Invalid', headers=headers)
Pierre-Yves Chibon 13e2e9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 13e2e9
        data = json.loads(output.data)
Pierre-Yves Chibon 13e2e9
        self.assertDictEqual(
Pierre-Yves Chibon 13e2e9
            data,
Pierre-Yves Chibon 13e2e9
            {
Pierre-Yves Chibon 316f15
              "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Pierre-Yves Chibon 316f15
                "status": "Invalid",
Pierre-Yves Chibon 316f15
                "tags": []
Pierre-Yves Chibon 316f15
              },
Pierre-Yves Chibon 316f15
              "issues": []
Pierre-Yves Chibon 13e2e9
            }
Pierre-Yves Chibon 13e2e9
        )
Pierre-Yves Chibon 13e2e9
Pierre-Yves Chibon 1c76b9
    def test_api_view_issue(self):
Pierre-Yves Chibon 1c76b9
        """ Test the api_view_issue method of the flask api. """
Pierre-Yves Chibon 1c76b9
        self.test_api_new_issue()
Pierre-Yves Chibon 1c76b9
Pierre-Yves Chibon 4a277d
        # Invalid repo
Pierre-Yves Chibon 4a277d
        output = self.app.get('/api/0/foo/issue/1')
Pierre-Yves Chibon 4a277d
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 4a277d
        data = json.loads(output.data)
Pierre-Yves Chibon 4a277d
        self.assertDictEqual(
Pierre-Yves Chibon 4a277d
            data,
Pierre-Yves Chibon 4a277d
            {
Pierre-Yves Chibon 4a277d
              "error": "Project not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon 4a277d
            }
Pierre-Yves Chibon 4a277d
        )
Pierre-Yves Chibon 4a277d
Pierre-Yves Chibon 4a277d
        # Invalid issue for this repo
Pierre-Yves Chibon 1c76b9
        output = self.app.get('/api/0/test2/issue/1')
Pierre-Yves Chibon 1c76b9
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 1c76b9
        data = json.loads(output.data)
Pierre-Yves Chibon 1c76b9
        self.assertDictEqual(
Pierre-Yves Chibon 1c76b9
            data,
Pierre-Yves Chibon 1c76b9
            {
Pierre-Yves Chibon 1c76b9
              "error": "Issue not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOISSUE",
Pierre-Yves Chibon 1c76b9
            }
Pierre-Yves Chibon 1c76b9
        )
Pierre-Yves Chibon 1c76b9
Pierre-Yves Chibon 4a277d
        # Valid issue
Pierre-Yves Chibon 1c76b9
        output = self.app.get('/api/0/test/issue/1')
Pierre-Yves Chibon 1c76b9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1c76b9
        data = json.loads(output.data)
Pierre-Yves Chibon 1c76b9
        data['date_created'] = '1431414800'
Pierre-Yves Chibon 1c76b9
        self.assertDictEqual(
Pierre-Yves Chibon 1c76b9
            data,
Pierre-Yves Chibon 1c76b9
            {
Pierre-Yves Chibon 1c76b9
              "assignee": None,
Pierre-Yves Chibon 1c76b9
              "blocks": [],
Pierre-Yves Chibon 1c76b9
              "comments": [],
Pierre-Yves Chibon 1c76b9
              "content": "This issue needs attention",
Pierre-Yves Chibon 1c76b9
              "date_created": "1431414800",
Pierre-Yves Chibon 1c76b9
              "depends": [],
Pierre-Yves Chibon 1c76b9
              "id": 1,
Pierre-Yves Chibon 1c76b9
              "private": False,
Pierre-Yves Chibon 1c76b9
              "status": "Open",
Pierre-Yves Chibon 1c76b9
              "tags": [],
Pierre-Yves Chibon 1c76b9
              "title": "test issue",
Pierre-Yves Chibon 1c76b9
              "user": {
Pierre-Yves Chibon 1c76b9
                "fullname": "PY C",
Pierre-Yves Chibon 1c76b9
                "name": "pingou"
Pierre-Yves Chibon 1c76b9
              }
Pierre-Yves Chibon 1c76b9
            }
Pierre-Yves Chibon 1c76b9
        )
Pierre-Yves Chibon 1c76b9
Pierre-Yves Chibon 4a277d
        # Create private issue
Pierre-Yves Chibon 4a277d
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 4a277d
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 4a277d
            session=self.session,
Pierre-Yves Chibon 4a277d
            repo=repo,
Pierre-Yves Chibon 4a277d
            title='Test issue',
Pierre-Yves Chibon 4a277d
            content='We should work on this',
Pierre-Yves Chibon 4a277d
            user='pingou',
Pierre-Yves Chibon 4a277d
            ticketfolder=None,
Pierre-Yves Chibon 4a277d
            private=True,
Pierre-Yves Chibon 4a277d
        )
Pierre-Yves Chibon 4a277d
        self.session.commit()
Pierre-Yves Chibon 4a277d
        self.assertEqual(msg.title, 'Test issue')
Pierre-Yves Chibon 4a277d
Pierre-Yves Chibon 4a277d
        # Access private issue un-authenticated
Pierre-Yves Chibon 4a277d
        output = self.app.get('/api/0/test/issue/2')
Pierre-Yves Chibon 4a277d
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 4a277d
        data = json.loads(output.data)
Pierre-Yves Chibon 4a277d
        self.assertDictEqual(
Pierre-Yves Chibon 4a277d
            data,
Pierre-Yves Chibon 4a277d
            {
Pierre-Yves Chibon 4a277d
              "error": "You are not allowed to view this issue",
Pierre-Yves Chibon 76338b
              "error_code": "EISSUENOTALLOWED",
Pierre-Yves Chibon 4a277d
            }
Pierre-Yves Chibon 4a277d
        )
Pierre-Yves Chibon 4a277d
Pierre-Yves Chibon 4a277d
        headers = {'Authorization': 'token aaabbbccc'}
Pierre-Yves Chibon 4a277d
Pierre-Yves Chibon 4a277d
        # Access private issue authenticated but wrong token
Pierre-Yves Chibon 4a277d
        output = self.app.get('/api/0/test/issue/2', headers=headers)
Pierre-Yves Chibon 4a277d
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 4a277d
        data = json.loads(output.data)
Pierre-Yves Chibon 4a277d
        self.assertDictEqual(
Pierre-Yves Chibon 4a277d
            data,
Pierre-Yves Chibon 4a277d
            {
Pierre-Yves Chibon 4a277d
              "error": "You are not allowed to view this issue",
Pierre-Yves Chibon 76338b
              "error_code": "EISSUENOTALLOWED",
Pierre-Yves Chibon 4a277d
            }
Pierre-Yves Chibon 4a277d
        )
Pierre-Yves Chibon 4a277d
Pierre-Yves Chibon 4a277d
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 4a277d
Pierre-Yves Chibon 4a277d
        # Access private issue authenticated correctly
Pierre-Yves Chibon 4a277d
        output = self.app.get('/api/0/test/issue/2', headers=headers)
Pierre-Yves Chibon 4a277d
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 4a277d
        data = json.loads(output.data)
Pierre-Yves Chibon 4a277d
        data['date_created'] = '1431414800'
Pierre-Yves Chibon 4a277d
        self.assertDictEqual(
Pierre-Yves Chibon 4a277d
            data,
Pierre-Yves Chibon 4a277d
            {
Pierre-Yves Chibon 4a277d
              "assignee": None,
Pierre-Yves Chibon 4a277d
              "blocks": [],
Pierre-Yves Chibon 4a277d
              "comments": [],
Pierre-Yves Chibon 4a277d
              "content": "We should work on this",
Pierre-Yves Chibon 4a277d
              "date_created": "1431414800",
Pierre-Yves Chibon 4a277d
              "depends": [],
Pierre-Yves Chibon 4a277d
              "id": 2,
Pierre-Yves Chibon 4a277d
              "private": True,
Pierre-Yves Chibon 4a277d
              "status": "Open",
Pierre-Yves Chibon 4a277d
              "tags": [],
Pierre-Yves Chibon 4a277d
              "title": "Test issue",
Pierre-Yves Chibon 4a277d
              "user": {
Pierre-Yves Chibon 4a277d
                "fullname": "PY C",
Pierre-Yves Chibon 4a277d
                "name": "pingou"
Pierre-Yves Chibon 4a277d
              }
Pierre-Yves Chibon 4a277d
            }
Pierre-Yves Chibon 4a277d
        )
Pierre-Yves Chibon 4a277d
Pierre-Yves Chibon 218ed6
    def test_api_change_status_issue(self):
Pierre-Yves Chibon 218ed6
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 218ed6
        tests.create_projects(self.session)
Pierre-Yves Chibon 218ed6
        tests.create_projects_git(os.path.join(tests.HERE, 'tickets'))
Pierre-Yves Chibon 218ed6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 218ed6
        tests.create_acls(self.session)
Pierre-Yves Chibon 218ed6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon f69837
        # Invalid project
Pierre-Yves Chibon f69837
        output = self.app.post('/api/0/foo/issue/1/status', headers=headers)
Pierre-Yves Chibon f69837
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon f69837
        data = json.loads(output.data)
Pierre-Yves Chibon f69837
        self.assertDictEqual(
Pierre-Yves Chibon f69837
            data,
Pierre-Yves Chibon f69837
            {
Pierre-Yves Chibon f69837
              "error": "Project not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon f69837
            }
Pierre-Yves Chibon f69837
        )
Pierre-Yves Chibon f69837
Pierre-Yves Chibon 218ed6
        # Valid token, wrong project
Pierre-Yves Chibon 218ed6
        output = self.app.post('/api/0/test2/issue/1/status', headers=headers)
Pierre-Yves Chibon 218ed6
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 218ed6
        data = json.loads(output.data)
Pierre-Yves Chibon 218ed6
        self.assertDictEqual(
Pierre-Yves Chibon 218ed6
            data,
Pierre-Yves Chibon 218ed6
            {
Pierre-Yves Chibon 218ed6
              "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 218ed6
            }
Pierre-Yves Chibon 218ed6
        )
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        # No input
Pierre-Yves Chibon 218ed6
        output = self.app.post('/api/0/test/issue/1/status', headers=headers)
Pierre-Yves Chibon 218ed6
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 218ed6
        data = json.loads(output.data)
Pierre-Yves Chibon 218ed6
        self.assertDictEqual(
Pierre-Yves Chibon 218ed6
            data,
Pierre-Yves Chibon 218ed6
            {
Pierre-Yves Chibon 218ed6
              "error": "Issue not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOISSUE",
Pierre-Yves Chibon 218ed6
            }
Pierre-Yves Chibon 218ed6
        )
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        # Create normal issue
Pierre-Yves Chibon 218ed6
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 218ed6
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 218ed6
            session=self.session,
Pierre-Yves Chibon 218ed6
            repo=repo,
Pierre-Yves Chibon 218ed6
            title='Test issue #1',
Pierre-Yves Chibon 218ed6
            content='We should work on this',
Pierre-Yves Chibon 218ed6
            user='pingou',
Pierre-Yves Chibon 218ed6
            ticketfolder=None,
Pierre-Yves Chibon 218ed6
            private=False,
Pierre-Yves Chibon 218ed6
        )
Pierre-Yves Chibon 218ed6
        self.session.commit()
Pierre-Yves Chibon 218ed6
        self.assertEqual(msg.title, 'Test issue #1')
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon f69837
        # Create another project
Pierre-Yves Chibon f69837
        item = pagure.lib.model.Project(
Pierre-Yves Chibon f69837
            user_id=2,  # pingou
Pierre-Yves Chibon f69837
            name='foo',
Pierre-Yves Chibon f69837
            description='test project #3',
Pierre-Yves Chibon f69837
            hook_token='aaabbbdddeee',
Pierre-Yves Chibon f69837
        )
Pierre-Yves Chibon f69837
        self.session.add(item)
Pierre-Yves Chibon f69837
        self.session.commit()
Pierre-Yves Chibon f69837
Pierre-Yves Chibon f69837
        # Create a token for pingou for this project
Pierre-Yves Chibon f69837
        item = pagure.lib.model.Token(
Pierre-Yves Chibon f69837
            id='pingou_foo',
Pierre-Yves Chibon f69837
            user_id=1,
Pierre-Yves Chibon f69837
            project_id=3,
Pierre-Yves Chibon f69837
            expiration=datetime.datetime.utcnow() + datetime.timedelta(
Pierre-Yves Chibon f69837
                days=30)
Pierre-Yves Chibon f69837
        )
Pierre-Yves Chibon f69837
        self.session.add(item)
Pierre-Yves Chibon f69837
        self.session.commit()
Pierre-Yves Chibon f69837
Pierre-Yves Chibon f69837
        # Give `change_status_issue` to this token
Pierre-Yves Chibon f69837
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon f69837
            token_id='pingou_foo',
Pierre-Yves Chibon f69837
            acl_id=4,
Pierre-Yves Chibon f69837
        )
Pierre-Yves Chibon f69837
        self.session.add(item)
Pierre-Yves Chibon f69837
        self.session.commit()
Pierre-Yves Chibon f69837
Pierre-Yves Chibon f69837
        repo = pagure.lib.get_project(self.session, 'foo')
Pierre-Yves Chibon 218ed6
        # Create private issue
Pierre-Yves Chibon 218ed6
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 218ed6
            session=self.session,
Pierre-Yves Chibon 218ed6
            repo=repo,
Pierre-Yves Chibon 218ed6
            title='Test issue',
Pierre-Yves Chibon 218ed6
            content='We should work on this',
Pierre-Yves Chibon f69837
            user='foo',
Pierre-Yves Chibon 218ed6
            ticketfolder=None,
Pierre-Yves Chibon 218ed6
            private=True,
Pierre-Yves Chibon 218ed6
        )
Pierre-Yves Chibon 218ed6
        self.session.commit()
Pierre-Yves Chibon 218ed6
        self.assertEqual(msg.title, 'Test issue')
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        # Check status before
Pierre-Yves Chibon 218ed6
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 218ed6
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 218ed6
        self.assertEqual(issue.status, 'Open')
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        data = {
Pierre-Yves Chibon 218ed6
            'title': 'test issue',
Pierre-Yves Chibon 218ed6
        }
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        # Incomplete request
Pierre-Yves Chibon 218ed6
        output = self.app.post(
Pierre-Yves Chibon 218ed6
            '/api/0/test/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 218ed6
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 218ed6
        data = json.loads(output.data)
Pierre-Yves Chibon 218ed6
        self.assertDictEqual(
Pierre-Yves Chibon 218ed6
            data,
Pierre-Yves Chibon 218ed6
            {
Pierre-Yves Chibon 218ed6
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 218ed6
            }
Pierre-Yves Chibon 218ed6
        )
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        # No change
Pierre-Yves Chibon 218ed6
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 218ed6
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 218ed6
        self.assertEqual(issue.status, 'Open')
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        data = {
Pierre-Yves Chibon 218ed6
            'status': 'Open',
Pierre-Yves Chibon 218ed6
        }
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        # Valid request but no change
Pierre-Yves Chibon 218ed6
        output = self.app.post(
Pierre-Yves Chibon 218ed6
            '/api/0/test/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 218ed6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 218ed6
        data = json.loads(output.data)
Pierre-Yves Chibon 218ed6
        self.assertDictEqual(
Pierre-Yves Chibon 218ed6
            data,
Pierre-Yves Chibon 218ed6
            {'message': 'No changes'}
Pierre-Yves Chibon 218ed6
        )
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        # No change
Pierre-Yves Chibon 218ed6
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 218ed6
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 218ed6
        self.assertEqual(issue.status, 'Open')
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        data = {
Pierre-Yves Chibon 218ed6
            'status': 'Fixed',
Pierre-Yves Chibon 218ed6
        }
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon 218ed6
        # Valid request
Pierre-Yves Chibon 218ed6
        output = self.app.post(
Pierre-Yves Chibon 218ed6
            '/api/0/test/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 218ed6
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 218ed6
        data = json.loads(output.data)
Pierre-Yves Chibon 218ed6
        self.assertDictEqual(
Pierre-Yves Chibon 218ed6
            data,
Pierre-Yves Chibon 218ed6
            {'message': 'Edited successfully issue #1'}
Pierre-Yves Chibon 218ed6
        )
Pierre-Yves Chibon 218ed6
Pierre-Yves Chibon f69837
        headers = {'Authorization': 'token pingou_foo'}
Pierre-Yves Chibon f69837
Pierre-Yves Chibon f69837
        # Un-authorized issue
Pierre-Yves Chibon f69837
        output = self.app.post(
Pierre-Yves Chibon f69837
            '/api/0/foo/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon f69837
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon f69837
        data = json.loads(output.data)
Pierre-Yves Chibon f69837
        self.assertDictEqual(
Pierre-Yves Chibon f69837
            data,
Pierre-Yves Chibon f69837
            {
Pierre-Yves Chibon f69837
              "error": "You are not allowed to view this issue",
Pierre-Yves Chibon 76338b
              "error_code": "EISSUENOTALLOWED",
Pierre-Yves Chibon f69837
            }
Pierre-Yves Chibon f69837
        )
Pierre-Yves Chibon f69837
Pierre-Yves Chibon fc6bd4
    def test_api_comment_issue(self):
Pierre-Yves Chibon fc6bd4
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon fc6bd4
        tests.create_projects(self.session)
Pierre-Yves Chibon fc6bd4
        tests.create_tokens(self.session)
Pierre-Yves Chibon fc6bd4
        tests.create_acls(self.session)
Pierre-Yves Chibon fc6bd4
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Invalid project
Pierre-Yves Chibon fc6bd4
        output = self.app.post('/api/0/foo/issue/1/comment', headers=headers)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon fc6bd4
        data = json.loads(output.data)
Pierre-Yves Chibon fc6bd4
        self.assertDictEqual(
Pierre-Yves Chibon fc6bd4
            data,
Pierre-Yves Chibon fc6bd4
            {
Pierre-Yves Chibon fc6bd4
              "error": "Project not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon fc6bd4
            }
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Valid token, wrong project
Pierre-Yves Chibon fc6bd4
        output = self.app.post('/api/0/test2/issue/1/comment', headers=headers)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon fc6bd4
        data = json.loads(output.data)
Pierre-Yves Chibon fc6bd4
        self.assertDictEqual(
Pierre-Yves Chibon fc6bd4
            data,
Pierre-Yves Chibon fc6bd4
            {
Pierre-Yves Chibon fc6bd4
              "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 fc6bd4
            }
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # No input
Pierre-Yves Chibon fc6bd4
        output = self.app.post('/api/0/test/issue/1/comment', headers=headers)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon fc6bd4
        data = json.loads(output.data)
Pierre-Yves Chibon fc6bd4
        self.assertDictEqual(
Pierre-Yves Chibon fc6bd4
            data,
Pierre-Yves Chibon fc6bd4
            {
Pierre-Yves Chibon fc6bd4
              "error": "Issue not found",
Pierre-Yves Chibon d661a4
              "error_code": "ENOISSUE",
Pierre-Yves Chibon fc6bd4
            }
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Create normal issue
Pierre-Yves Chibon fc6bd4
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon fc6bd4
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon fc6bd4
            session=self.session,
Pierre-Yves Chibon fc6bd4
            repo=repo,
Pierre-Yves Chibon fc6bd4
            title='Test issue #1',
Pierre-Yves Chibon fc6bd4
            content='We should work on this',
Pierre-Yves Chibon fc6bd4
            user='pingou',
Pierre-Yves Chibon fc6bd4
            ticketfolder=None,
Pierre-Yves Chibon fc6bd4
            private=False,
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
        self.session.commit()
Pierre-Yves Chibon fc6bd4
        self.assertEqual(msg.title, 'Test issue #1')
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Check comments before
Pierre-Yves Chibon fc6bd4
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon fc6bd4
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        data = {
Pierre-Yves Chibon fc6bd4
            'title': 'test issue',
Pierre-Yves Chibon fc6bd4
        }
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Incomplete request
Pierre-Yves Chibon fc6bd4
        output = self.app.post(
Pierre-Yves Chibon fc6bd4
            '/api/0/test/issue/1/comment', data=data, headers=headers)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon fc6bd4
        data = json.loads(output.data)
Pierre-Yves Chibon fc6bd4
        self.assertDictEqual(
Pierre-Yves Chibon fc6bd4
            data,
Pierre-Yves Chibon fc6bd4
            {
Pierre-Yves Chibon fc6bd4
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon d661a4
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon fc6bd4
            }
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # No change
Pierre-Yves Chibon fc6bd4
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon fc6bd4
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(issue.status, 'Open')
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        data = {
Pierre-Yves Chibon fc6bd4
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon fc6bd4
        }
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Valid request
Pierre-Yves Chibon fc6bd4
        output = self.app.post(
Pierre-Yves Chibon fc6bd4
            '/api/0/test/issue/1/comment', data=data, headers=headers)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fc6bd4
        data = json.loads(output.data)
Pierre-Yves Chibon fc6bd4
        self.assertDictEqual(
Pierre-Yves Chibon fc6bd4
            data,
Pierre-Yves Chibon fc6bd4
            {'message': 'Comment added'}
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # One comment added
Pierre-Yves Chibon fc6bd4
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon fc6bd4
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(len(issue.comments), 1)
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Create another project
Pierre-Yves Chibon fc6bd4
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc6bd4
            user_id=2,  # foo
Pierre-Yves Chibon fc6bd4
            name='foo',
Pierre-Yves Chibon fc6bd4
            description='test project #3',
Pierre-Yves Chibon fc6bd4
            hook_token='aaabbbdddeee',
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
        self.session.add(item)
Pierre-Yves Chibon fc6bd4
        self.session.commit()
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Create a token for pingou for this project
Pierre-Yves Chibon fc6bd4
        item = pagure.lib.model.Token(
Pierre-Yves Chibon fc6bd4
            id='pingou_foo',
Pierre-Yves Chibon fc6bd4
            user_id=1,
Pierre-Yves Chibon fc6bd4
            project_id=3,
Pierre-Yves Chibon fc6bd4
            expiration=datetime.datetime.utcnow() + datetime.timedelta(
Pierre-Yves Chibon fc6bd4
                days=30)
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
        self.session.add(item)
Pierre-Yves Chibon fc6bd4
        self.session.commit()
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Give `change_status_issue` to this token
Pierre-Yves Chibon fc6bd4
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon fc6bd4
            token_id='pingou_foo',
Pierre-Yves Chibon fc6bd4
            acl_id=5,
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
        self.session.add(item)
Pierre-Yves Chibon fc6bd4
        self.session.commit()
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        repo = pagure.lib.get_project(self.session, 'foo')
Pierre-Yves Chibon fc6bd4
        # Create private issue
Pierre-Yves Chibon fc6bd4
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon fc6bd4
            session=self.session,
Pierre-Yves Chibon fc6bd4
            repo=repo,
Pierre-Yves Chibon fc6bd4
            title='Test issue',
Pierre-Yves Chibon fc6bd4
            content='We should work on this',
Pierre-Yves Chibon fc6bd4
            user='foo',
Pierre-Yves Chibon fc6bd4
            ticketfolder=None,
Pierre-Yves Chibon fc6bd4
            private=True,
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
        self.session.commit()
Pierre-Yves Chibon fc6bd4
        self.assertEqual(msg.title, 'Test issue')
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Check before
Pierre-Yves Chibon fc6bd4
        repo = pagure.lib.get_project(self.session, 'foo')
Pierre-Yves Chibon fc6bd4
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        data = {
Pierre-Yves Chibon fc6bd4
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon fc6bd4
        }
Pierre-Yves Chibon fc6bd4
        headers = {'Authorization': 'token pingou_foo'}
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # Valid request but un-authorized
Pierre-Yves Chibon fc6bd4
        output = self.app.post(
Pierre-Yves Chibon fc6bd4
            '/api/0/foo/issue/1/comment', data=data, headers=headers)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon fc6bd4
        data = json.loads(output.data)
Pierre-Yves Chibon fc6bd4
        self.assertDictEqual(
Pierre-Yves Chibon fc6bd4
            data,
Pierre-Yves Chibon fc6bd4
            {
Pierre-Yves Chibon fc6bd4
              "error": "You are not allowed to view this issue",
Pierre-Yves Chibon 76338b
              "error_code": "EISSUENOTALLOWED",
Pierre-Yves Chibon fc6bd4
            }
Pierre-Yves Chibon fc6bd4
        )
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon fc6bd4
        # No comment added
Pierre-Yves Chibon fc6bd4
        repo = pagure.lib.get_project(self.session, 'foo')
Pierre-Yves Chibon fc6bd4
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon fc6bd4
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon fc6bd4
Pierre-Yves Chibon 993597
Pierre-Yves Chibon 993597
if __name__ == '__main__':
Pierre-Yves Chibon 993597
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon 993597
        PagureFlaskApiIssuetests)
Pierre-Yves Chibon 993597
    unittest.TextTestRunner(verbosity=2).run(SUITE)