Blame tests/test_pagure_flask_api_issue_comment.py

Pierre-Yves Chibon 91e044
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
"""
Pierre-Yves Chibon 91e044
 (c) 2015-2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
 Authors:
Pierre-Yves Chibon 91e044
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
"""
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
import datetime
Pierre-Yves Chibon 91e044
import unittest
Pierre-Yves Chibon 91e044
import sys
Pierre-Yves Chibon 91e044
import os
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
import json
Pierre-Yves Chibon 91e044
from mock import patch, MagicMock
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 91e044
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
import pagure  # noqa: E402
Pierre-Yves Chibon 91e044
import pagure.lib  # noqa: E402
Pierre-Yves Chibon 91e044
import tests  # noqa: E402
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
class PagureFlaskApiIssueCommenttests(tests.Modeltests):
Pierre-Yves Chibon 91e044
    """ Tests for the flask API of pagure for changing the status of an
Pierre-Yves Chibon 91e044
    issue
Pierre-Yves Chibon 91e044
    """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 91e044
    def setUp(self):
Pierre-Yves Chibon 91e044
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 91e044
        super(PagureFlaskApiIssueCommenttests, self).setUp()
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 91e044
        pagure.SESSION = self.session
Pierre-Yves Chibon 91e044
        pagure.api.SESSION = self.session
Pierre-Yves Chibon 91e044
        pagure.api.issue.SESSION = self.session
Pierre-Yves Chibon 91e044
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        pagure.APP.config['TICKETS_FOLDER'] = None
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        tests.create_projects(self.session)
Pierre-Yves Chibon 91e044
        tests.create_projects_git(os.path.join(self.path, 'tickets'))
Pierre-Yves Chibon 91e044
        tests.create_tokens(self.session)
Pierre-Yves Chibon 91e044
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Create normal issue
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 91e044
            session=self.session,
Pierre-Yves Chibon 91e044
            repo=repo,
Pierre-Yves Chibon 91e044
            title='Test issue #1',
Pierre-Yves Chibon 91e044
            content='We should work on this',
Pierre-Yves Chibon 91e044
            user='pingou',
Pierre-Yves Chibon 91e044
            ticketfolder=None,
Pierre-Yves Chibon 91e044
            private=False,
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
        self.session.commit()
Pierre-Yves Chibon 91e044
        self.assertEqual(msg.title, 'Test issue #1')
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Create private issue
Pierre-Yves Chibon 91e044
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 91e044
            session=self.session,
Pierre-Yves Chibon 91e044
            repo=repo,
Pierre-Yves Chibon 91e044
            title='Test issue #2',
Pierre-Yves Chibon 91e044
            content='We should work on this',
Pierre-Yves Chibon 91e044
            user='foo',
Pierre-Yves Chibon 91e044
            ticketfolder=None,
Pierre-Yves Chibon 91e044
            private=True,
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
        self.session.commit()
Pierre-Yves Chibon 91e044
        self.assertEqual(msg.title, 'Test issue #2')
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Create project-less token for user foo
Pierre-Yves Chibon 91e044
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 91e044
            id='project-less-foo',
Pierre-Yves Chibon 91e044
            user_id=2,
Pierre-Yves Chibon 91e044
            project_id=None,
Pierre-Yves Chibon 91e044
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 91e044
            + datetime.timedelta(days=30)
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
        self.session.add(item)
Pierre-Yves Chibon 91e044
        self.session.commit()
Pierre-Yves Chibon 91e044
        tests.create_tokens_acl(self.session, token_id='project-less-foo')
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_invalid_project(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Invalid project
Pierre-Yves Chibon 91e044
        output = self.app.post('/api/0/foo/issue/1/comment', headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {
Pierre-Yves Chibon 91e044
                "error": "Project not found",
Pierre-Yves Chibon 91e044
                "error_code": "ENOPROJECT",
Pierre-Yves Chibon 91e044
            }
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_invalid_project_token(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid token, wrong project
Pierre-Yves Chibon 91e044
        output = self.app.post('/api/0/test2/issue/1/comment', headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 91e044
                         data['error_code'])
Pierre-Yves Chibon 91e044
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_invalid_issue(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 91e044
        # Invalid issue
Pierre-Yves Chibon 91e044
        output = self.app.post('/api/0/test/issue/10/comment', headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {
Pierre-Yves Chibon 91e044
                "error": "Issue not found",
Pierre-Yves Chibon 91e044
                "error_code": "ENOISSUE",
Pierre-Yves Chibon 91e044
            }
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_incomplete_request(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 91e044
        # Check comments before
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        data = {
Pierre-Yves Chibon 91e044
            'title': 'test issue',
Pierre-Yves Chibon 91e044
        }
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Incomplete request
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 91e044
            '/api/0/test/issue/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 91e044
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 91e044
                "errors": {"comment": ["This field is required."]}
Pierre-Yves Chibon 91e044
            }
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # No change
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 91e044
        self.assertEqual(issue.status, 'Open')
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        data = {
Pierre-Yves Chibon 91e044
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 91e044
        }
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid request
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 91e044
            '/api/0/test/issue/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {'message': 'Comment added'}
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # One comment added
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 1)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_private_un_authorized(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Check before
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        data = {
Pierre-Yves Chibon 91e044
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 91e044
        }
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token pingou_foo'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid request but un-authorized
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 91e044
            '/api/0/test/issue/2/comment', data=data, headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 91e044
                         data['error_code'])
Pierre-Yves Chibon 91e044
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # No comment added
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_private(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Create token for user foo
Pierre-Yves Chibon 91e044
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 91e044
            id='foo_token2',
Pierre-Yves Chibon 91e044
            user_id=2,
Pierre-Yves Chibon 91e044
            project_id=1,
Pierre-Yves Chibon 91e044
            expiration=datetime.datetime.utcnow() + datetime.timedelta(days=30)
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
        self.session.add(item)
Pierre-Yves Chibon 91e044
        self.session.commit()
Pierre-Yves Chibon 91e044
        tests.create_tokens_acl(self.session, token_id='foo_token2')
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        data = {
Pierre-Yves Chibon 91e044
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 91e044
        }
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token foo_token2'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid request and authorized
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 91e044
            '/api/0/test/issue/2/comment', data=data, headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {'message': 'Comment added'}
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_invalid_project_project_less(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token project-less-foo'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Invalid project
Pierre-Yves Chibon 91e044
        output = self.app.post('/api/0/foo/issue/1/comment', headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {
Pierre-Yves Chibon 91e044
                "error": "Project not found",
Pierre-Yves Chibon 91e044
                "error_code": "ENOPROJECT",
Pierre-Yves Chibon 91e044
            }
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_invalid_project_token_project_less(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token project-less-foo'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid token, no such issue, project-less token so different failure
Pierre-Yves Chibon 91e044
        output = self.app.post('/api/0/test2/issue/1/comment', headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {
Pierre-Yves Chibon 91e044
                "error": "Issue not found",
Pierre-Yves Chibon 91e044
                "error_code": "ENOISSUE",
Pierre-Yves Chibon 91e044
            }
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_invalid_issue_project_less(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token project-less-foo'}
Pierre-Yves Chibon 91e044
        # Invalid issue
Pierre-Yves Chibon 91e044
        output = self.app.post('/api/0/test/issue/10/comment', headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {
Pierre-Yves Chibon 91e044
                "error": "Issue not found",
Pierre-Yves Chibon 91e044
                "error_code": "ENOISSUE",
Pierre-Yves Chibon 91e044
            }
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_incomplete_request_project_less(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token project-less-foo'}
Pierre-Yves Chibon 91e044
        # Check comments before
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        data = {
Pierre-Yves Chibon 91e044
            'title': 'test issue',
Pierre-Yves Chibon 91e044
        }
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Incomplete request
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 91e044
            '/api/0/test/issue/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 91e044
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 91e044
                "errors": {"comment": ["This field is required."]}
Pierre-Yves Chibon 91e044
            }
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # No change
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 91e044
        self.assertEqual(issue.status, 'Open')
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_project_less(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token project-less-foo'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        data = {
Pierre-Yves Chibon 91e044
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 91e044
        }
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid request
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 91e044
            '/api/0/test/issue/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {'message': 'Comment added'}
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # One comment added
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 1)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_private_un_authorized_project_less(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Check before
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        data = {
Pierre-Yves Chibon 91e044
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 91e044
        }
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token pingou_foo'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid request but un-authorized
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 91e044
            '/api/0/test/issue/2/comment', data=data, headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 91e044
                         data['error_code'])
Pierre-Yves Chibon 91e044
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # No comment added
Pierre-Yves Chibon 91e044
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 91e044
        issue = pagure.lib.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 91e044
    def test_api_comment_issue_private_project_less(self):
Pierre-Yves Chibon 91e044
        """ Test the api_comment_issue method of the flask api. """
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Create token for user foo
Pierre-Yves Chibon 91e044
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 91e044
            id='foo_token2',
Pierre-Yves Chibon 91e044
            user_id=2,
Pierre-Yves Chibon 91e044
            project_id=None,
Pierre-Yves Chibon 91e044
            expiration=datetime.datetime.utcnow() + datetime.timedelta(days=30)
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
        self.session.add(item)
Pierre-Yves Chibon 91e044
        self.session.commit()
Pierre-Yves Chibon 91e044
        tests.create_tokens_acl(self.session, token_id='foo_token2')
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        data = {
Pierre-Yves Chibon 91e044
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 91e044
        }
Pierre-Yves Chibon 91e044
        headers = {'Authorization': 'token foo_token2'}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid request and authorized
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 91e044
            '/api/0/test/issue/2/comment', data=data, headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 91e044
        data = json.loads(output.data)
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 91e044
            {'message': 'Comment added'}
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
if __name__ == '__main__':
Pierre-Yves Chibon 91e044
    unittest.main(verbosity=2)