Blame tests/test_pagure_flask_api_issue_change_status.py

Pierre-Yves Chibon 929a2a
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
"""
Pierre-Yves Chibon 929a2a
 (c) 2015-2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
 Authors:
Pierre-Yves Chibon 929a2a
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
"""
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 929a2a
import copy
Pierre-Yves Chibon 929a2a
import datetime
Pierre-Yves Chibon 929a2a
import unittest
Pierre-Yves Chibon 929a2a
import shutil
Pierre-Yves Chibon 929a2a
import sys
Pierre-Yves Chibon 929a2a
import time
Pierre-Yves Chibon 929a2a
import os
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
import json
Pierre-Yves Chibon 929a2a
from mock import patch, MagicMock
Pierre-Yves Chibon 929a2a
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 929a2a
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon d86db9
import pagure.lib.model
Pierre-Yves Chibon 929a2a
import tests
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
class PagureFlaskApiIssueChangeStatustests(tests.Modeltests):
Pierre-Yves Chibon 929a2a
    """ Tests for the flask API of pagure for changing the status of an
Pierre-Yves Chibon 929a2a
    issue
Pierre-Yves Chibon 929a2a
    """
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 929a2a
    def setUp(self):
Pierre-Yves Chibon 929a2a
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 929a2a
        super(PagureFlaskApiIssueChangeStatustests, self).setUp()
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        pagure.config.config["TICKETS_FOLDER"] = None
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "tickets"))
Pierre-Yves Chibon 929a2a
        tests.create_tokens(self.session)
Pierre-Yves Chibon 929a2a
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # 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 929a2a
            session=self.session,
Pierre-Yves Chibon 929a2a
            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 929a2a
            private=False,
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #1")
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Create private issue
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 929a2a
            session=self.session,
Pierre-Yves Chibon 929a2a
            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 929a2a
            private=True,
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue #2")
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 649607
        # Create project-less token for user foo
Pierre-Yves Chibon 649607
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="project-less-foo",
Pierre-Yves Chibon 649607
            user_id=2,
Pierre-Yves Chibon 649607
            project_id=None,
Pierre-Yves Chibon 649607
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon 649607
        )
Pierre-Yves Chibon 649607
        self.session.add(item)
Pierre-Yves Chibon 649607
        self.session.commit()
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, token_id="project-less-foo")
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        # Create project-less token for user pingou
Pierre-Yves Chibon 649607
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="project-less-pingou",
Pierre-Yves Chibon 649607
            user_id=1,
Pierre-Yves Chibon 649607
            project_id=None,
Pierre-Yves Chibon 649607
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon 649607
        )
Pierre-Yves Chibon 649607
        self.session.add(item)
Pierre-Yves Chibon 649607
        self.session.commit()
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, token_id="project-less-pingou")
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 929a2a
    def test_api_change_status_issue_invalid_project(self):
Pierre-Yves Chibon 929a2a
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Invalid project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/foobar/issue/1/status", headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
    def test_api_change_status_issue_token_not_for_project(self):
Pierre-Yves Chibon 929a2a
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Valid token, wrong project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test2/issue/1/status", headers=headers)
Pierre-Yves Chibon 929a2a
        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 929a2a
Pierre-Yves Chibon 929a2a
    def test_api_change_status_issue_invalid_issue(self):
Pierre-Yves Chibon 929a2a
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # No issue
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/issue/42/status", headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Issue not found", "error_code": "ENOISSUE"}
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
    def test_api_change_status_issue_incomplete(self):
Pierre-Yves Chibon 929a2a
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Check status 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 73d120
        self.assertEqual(issue.status, "Open")
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Incomplete request
Pierre-Yves Chibon 929a2a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 929a2a
            data,
Pierre-Yves Chibon 929a2a
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"status": ["Not a valid choice"]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # 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 929a2a
Pierre-Yves Chibon 929a2a
    def test_api_change_status_issue_no_change(self):
Pierre-Yves Chibon 929a2a
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        data = {"status": "Open"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Valid request but no change
Pierre-Yves Chibon 929a2a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "No changes"})
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # 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 929a2a
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9a16eb
    @patch(
Pierre-Yves Chibon 73d120
        "pagure.lib.query.edit_issue",
Pierre-Yves Chibon 73d120
        MagicMock(side_effect=pagure.exceptions.PagureException("error")),
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 9a16eb
    def test_api_change_status_issue_raise_error(self):
Pierre-Yves Chibon 9a16eb
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9a16eb
        close_status = repo.close_status
Pierre-Yves Chibon 73d120
        close_status = ["Fixed", "Upstream", "Invalid"]
Pierre-Yves Chibon 9a16eb
        repo.close_status = close_status
Pierre-Yves Chibon 9a16eb
        self.session.add(repo)
Pierre-Yves Chibon 9a16eb
        self.session.commit()
Pierre-Yves Chibon 9a16eb
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9a16eb
Pierre-Yves Chibon 73d120
        data = {"status": "Closed", "close_status": "Fixed"}
Pierre-Yves Chibon 9a16eb
Pierre-Yves Chibon 9a16eb
        # Valid request
Pierre-Yves Chibon 9a16eb
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9a16eb
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 9a16eb
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9a16eb
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"error": "error", "error_code": "ENOCODE"})
Pierre-Yves Chibon d86db9
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 929a2a
    def test_api_change_status_issue(self):
Pierre-Yves Chibon 929a2a
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        data = {"status": "Fixed"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Valid request
Pierre-Yves Chibon 929a2a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 929a2a
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": [
Pierre-Yves Chibon 73d120
                    "Issue status updated to: Closed (was: Open)",
Pierre-Yves Chibon 73d120
                    "Issue close_status updated to: Fixed",
Pierre-Yves Chibon 73d120
                ]
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token pingou_foo"}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Un-authorized issue
Pierre-Yves Chibon 929a2a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 929a2a
        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 929a2a
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon d86db9
    def test_api_change_status_issue_closed_status(self):
Pierre-Yves Chibon d86db9
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon d86db9
        close_status = repo.close_status
Pierre-Yves Chibon 73d120
        close_status = ["Fixed", "Upstream", "Invalid"]
Pierre-Yves Chibon d86db9
        repo.close_status = close_status
Pierre-Yves Chibon d86db9
        self.session.add(repo)
Pierre-Yves Chibon d86db9
        self.session.commit()
Pierre-Yves Chibon d86db9
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon d86db9
Pierre-Yves Chibon 73d120
        data = {"status": "Closed", "close_status": "Fixed"}
Pierre-Yves Chibon d86db9
Pierre-Yves Chibon d86db9
        # Valid request
Pierre-Yves Chibon d86db9
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon d86db9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon d86db9
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon d86db9
Pierre-Yves Chibon d86db9
        self.assertDictEqual(
Pierre-Yves Chibon d86db9
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": [
Pierre-Yves Chibon 73d120
                    "Issue status updated to: Closed (was: Open)",
Pierre-Yves Chibon 73d120
                    "Issue close_status updated to: Fixed",
Pierre-Yves Chibon 73d120
                ]
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon d86db9
        )
Pierre-Yves Chibon d86db9
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 649607
    def test_api_change_status_issue_no_ticket_project_less(self):
Pierre-Yves Chibon 649607
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token project-less-foo"}
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 73d120
        data = {"status": "Fixed"}
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        # Valid request
Pierre-Yves Chibon 649607
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 649607
        self.assertEqual(output.status_code, 403)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        self.assertDictEqual(
Pierre-Yves Chibon 649607
            data,
Pierre-Yves Chibon 649607
            {
Pierre-Yves Chibon 649607
                "error": "You are not allowed to view this issue",
Pierre-Yves Chibon 73d120
                "error_code": "EISSUENOTALLOWED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 649607
        )
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 649607
    def test_api_change_status_issue_project_less(self):
Pierre-Yves Chibon 649607
        """ Test the api_change_status_issue method of the flask api. """
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token project-less-pingou"}
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 73d120
        data = {"status": "Fixed"}
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        # Valid request
Pierre-Yves Chibon 649607
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/status", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 649607
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        self.assertDictEqual(
Pierre-Yves Chibon 649607
            data,
Pierre-Yves Chibon 649607
            {
Pierre-Yves Chibon 649607
                "message": [
Pierre-Yves Chibon 649607
                    "Issue status updated to: Closed (was: Open)",
Pierre-Yves Chibon 73d120
                    "Issue close_status updated to: Fixed",
Pierre-Yves Chibon 649607
                ]
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 649607
        )
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 929a2a
    unittest.main(verbosity=2)