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 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
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 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 930073
import pagure.lib.query  # 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 73d120
    @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 73d120
        pagure.config.config["TICKETS_FOLDER"] = None
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 91e044
            session=self.session,
Pierre-Yves Chibon 91e044
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #1",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 91e044
            private=False,
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #1")
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Create private issue
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 91e044
            session=self.session,
Pierre-Yves Chibon 91e044
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #2",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 91e044
            private=True,
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
        self.session.commit()
Pierre-Yves Chibon 73d120
        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 73d120
            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 73d120
            + 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 73d120
        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 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Invalid project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/foo/issue/1/comment", headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
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 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid token, wrong project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test2/issue/1/comment", headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        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 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 91e044
        # Invalid issue
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/issue/10/comment", headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Issue not found", "error_code": "ENOISSUE"}
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 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 91e044
        # Check comments before
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Incomplete request
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
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 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # No change
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 73d120
        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 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid request
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "Comment added",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # One comment added
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 73d120
        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 73d120
            "/api/0/test/issue/2/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # No comment added
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
    @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 73d120
            id="foo_token2",
Pierre-Yves Chibon 91e044
            user_id=2,
Pierre-Yves Chibon 91e044
            project_id=1,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + 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 73d120
        tests.create_tokens_acl(self.session, token_id="foo_token2")
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 73d120
        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 73d120
            "/api/0/test/issue/2/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "Comment added",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "foo",
Pierre-Yves Chibon 73d120
            },
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 73d120
        headers = {"Authorization": "token project-less-foo"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Invalid project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/foo/issue/1/comment", headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
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 73d120
        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 73d120
        output = self.app.post("/api/0/test2/issue/1/comment", headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Issue not found", "error_code": "ENOISSUE"}
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 73d120
        headers = {"Authorization": "token project-less-foo"}
Pierre-Yves Chibon 91e044
        # Invalid issue
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/issue/10/comment", headers=headers)
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Issue not found", "error_code": "ENOISSUE"}
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 73d120
        headers = {"Authorization": "token project-less-foo"}
Pierre-Yves Chibon 91e044
        # Check comments before
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Incomplete request
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
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 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # No change
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 73d120
        self.assertEqual(issue.status, "Open")
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
    @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 73d120
        headers = {"Authorization": "token project-less-foo"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # Valid request
Pierre-Yves Chibon 91e044
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "Comment added",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "foo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # One comment added
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 73d120
        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 73d120
            "/api/0/test/issue/2/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
        # No comment added
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
Pierre-Yves Chibon 91e044
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
    @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 73d120
            id="foo_token2",
Pierre-Yves Chibon 91e044
            user_id=2,
Pierre-Yves Chibon 91e044
            project_id=None,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + 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 73d120
        tests.create_tokens_acl(self.session, token_id="foo_token2")
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 73d120
        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 73d120
            "/api/0/test/issue/2/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 91e044
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 91e044
        self.assertDictEqual(
Pierre-Yves Chibon 91e044
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "Comment added",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "foo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 91e044
        )
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 91e044
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 91e044
    unittest.main(verbosity=2)