Blame tests/test_pagure_flask_api_issue_create.py

Pierre-Yves Chibon d794f0
# -*- coding: utf-8 -*-
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
"""
Pierre-Yves Chibon d794f0
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
 Authors:
Pierre-Yves Chibon d794f0
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
"""
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
import datetime
Pierre-Yves Chibon d794f0
import unittest
Pierre-Yves Chibon d794f0
import sys
Pierre-Yves Chibon d794f0
import os
Pierre-Yves Chibon d794f0
import json
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
from mock import patch, MagicMock
Pierre-Yves Chibon d794f0
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 d794f0
Pierre-Yves Chibon 930073
import pagure.lib.query  # noqa: E402
Pierre-Yves Chibon d794f0
import tests  # noqa: E402
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
class PagureFlaskApiIssueCreatetests(tests.Modeltests):
Pierre-Yves Chibon d794f0
    """ Tests for the flask API of pagure for creating an issue
Pierre-Yves Chibon d794f0
    """
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon d794f0
    def setUp(self):
Pierre-Yves Chibon d794f0
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon d794f0
        super(PagureFlaskApiIssueCreatetests, self).setUp()
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon 73d120
        pagure.config.config["TICKETS_FOLDER"] = None
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "tickets"))
Pierre-Yves Chibon d794f0
        tests.create_tokens(self.session)
Pierre-Yves Chibon d794f0
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # Create project-less token for user foo
Pierre-Yves Chibon d794f0
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="project-less-foo",
Pierre-Yves Chibon d794f0
            user_id=2,
Pierre-Yves Chibon d794f0
            project_id=None,
Pierre-Yves Chibon d794f0
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
        self.session.add(item)
Pierre-Yves Chibon d794f0
        self.session.commit()
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, token_id="project-less-foo")
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # Create project-specific token for user foo
Pierre-Yves Chibon d794f0
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="project-specific-foo",
Pierre-Yves Chibon d794f0
            user_id=2,
Pierre-Yves Chibon d794f0
            project_id=1,
Pierre-Yves Chibon d794f0
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
        self.session.add(item)
Pierre-Yves Chibon d794f0
        self.session.commit()
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, token_id="project-specific-foo")
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
    def test_create_issue_own_project_no_data(self):
Pierre-Yves Chibon d794f0
        """ Test creating a new ticket on a project for which you're the
Pierre-Yves Chibon d794f0
        main maintainer.
Pierre-Yves Chibon d794f0
        """
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # pingou's token with all the ACLs
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # Create an issue on /test/ where pingou is the main admin
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/new_issue", headers=headers)
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDREQ.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDREQ.value, data["error"])
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data["errors"],
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon 73d120
                "issue_content": ["This field is required."],
Pierre-Yves Chibon 73d120
                "title": ["This field is required."],
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
    def test_create_issue_own_project_incomplete_data(self):
Pierre-Yves Chibon d794f0
        """ Test creating a new ticket on a project for which you're the
Pierre-Yves Chibon d794f0
        main maintainer.
Pierre-Yves Chibon d794f0
        """
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # pingou's token with all the ACLs
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # complete data set
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # Create an issue on /test/ where pingou is the main admin
Pierre-Yves Chibon d794f0
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/new_issue", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDREQ.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDREQ.value, data["error"])
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data["errors"], {"issue_content": ["This field is required."]}
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
    def test_create_issue_own_project(self):
Pierre-Yves Chibon d794f0
        """ Test creating a new ticket on a project for which you're the
Pierre-Yves Chibon d794f0
        main maintainer.
Pierre-Yves Chibon d794f0
        """
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # pingou's token with all the ACLs
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # complete data set
Pierre-Yves Chibon d794f0
        data = {
Pierre-Yves Chibon 73d120
            "title": "test issue",
Pierre-Yves Chibon 73d120
            "issue_content": "This issue needs attention",
Pierre-Yves Chibon d794f0
        }
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # Create an issue on /test/ where pingou is the main admin
Pierre-Yves Chibon d794f0
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/new_issue", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["issue"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issue"]["last_updated"] = "1431414800"
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            data,
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon 73d120
                "issue": {
Pierre-Yves Chibon 73d120
                    "assignee": None,
Pierre-Yves Chibon 73d120
                    "blocks": [],
Pierre-Yves Chibon 73d120
                    "close_status": None,
Pierre-Yves Chibon 73d120
                    "closed_at": None,
Pierre-Yves Chibon 73d120
                    "closed_by": None,
Pierre-Yves Chibon 73d120
                    "comments": [],
Pierre-Yves Chibon 73d120
                    "content": "This issue needs attention",
Pierre-Yves Chibon 73d120
                    "custom_fields": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1431414800",
Pierre-Yves Chibon 73d120
                    "depends": [],
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "last_updated": "1431414800",
Pierre-Yves Chibon 73d120
                    "milestone": None,
Pierre-Yves Chibon 73d120
                    "priority": None,
Pierre-Yves Chibon 73d120
                    "private": False,
Pierre-Yves Chibon 73d120
                    "status": "Open",
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "title": "test issue",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Issue created",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon d794f0
    def test_create_issue_someone_else_project_project_less_token(self):
Pierre-Yves Chibon d794f0
        """ Test creating a new ticket on a project with which you have
Pierre-Yves Chibon d794f0
        nothing to do.
Pierre-Yves Chibon d794f0
        """
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # pingou's token with all the ACLs
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token project-less-foo"}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # complete data set
Pierre-Yves Chibon d794f0
        data = {
Pierre-Yves Chibon 73d120
            "title": "test issue",
Pierre-Yves Chibon 73d120
            "issue_content": "This issue needs attention",
Pierre-Yves Chibon d794f0
        }
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # Create an issue on /test/ where pingou is the main admin
Pierre-Yves Chibon d794f0
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/new_issue", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["issue"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issue"]["last_updated"] = "1431414800"
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            data,
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon 73d120
                "issue": {
Pierre-Yves Chibon 73d120
                    "assignee": None,
Pierre-Yves Chibon 73d120
                    "blocks": [],
Pierre-Yves Chibon 73d120
                    "close_status": None,
Pierre-Yves Chibon 73d120
                    "closed_at": None,
Pierre-Yves Chibon 73d120
                    "closed_by": None,
Pierre-Yves Chibon 73d120
                    "comments": [],
Pierre-Yves Chibon 73d120
                    "content": "This issue needs attention",
Pierre-Yves Chibon 73d120
                    "custom_fields": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1431414800",
Pierre-Yves Chibon 73d120
                    "depends": [],
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "last_updated": "1431414800",
Pierre-Yves Chibon 73d120
                    "milestone": None,
Pierre-Yves Chibon 73d120
                    "priority": None,
Pierre-Yves Chibon 73d120
                    "private": False,
Pierre-Yves Chibon 73d120
                    "status": "Open",
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "title": "test issue",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "foo bar", "name": "foo"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Issue created",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon d794f0
    def test_create_issue_project_specific_token(self):
Pierre-Yves Chibon d794f0
        """ Test creating a new ticket on a project with a regular
Pierre-Yves Chibon d794f0
        project-specific token.
Pierre-Yves Chibon d794f0
        """
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # pingou's token with all the ACLs
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token project-specific-foo"}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # complete data set
Pierre-Yves Chibon d794f0
        data = {
Pierre-Yves Chibon 73d120
            "title": "test issue",
Pierre-Yves Chibon 73d120
            "issue_content": "This issue needs attention",
Pierre-Yves Chibon d794f0
        }
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # Create an issue on /test/ where pingou is the main admin
Pierre-Yves Chibon d794f0
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/new_issue", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["issue"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["issue"]["last_updated"] = "1431414800"
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            data,
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon 73d120
                "issue": {
Pierre-Yves Chibon 73d120
                    "assignee": None,
Pierre-Yves Chibon 73d120
                    "blocks": [],
Pierre-Yves Chibon 73d120
                    "close_status": None,
Pierre-Yves Chibon 73d120
                    "closed_at": None,
Pierre-Yves Chibon 73d120
                    "closed_by": None,
Pierre-Yves Chibon 73d120
                    "comments": [],
Pierre-Yves Chibon 73d120
                    "content": "This issue needs attention",
Pierre-Yves Chibon 73d120
                    "custom_fields": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1431414800",
Pierre-Yves Chibon 73d120
                    "depends": [],
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "last_updated": "1431414800",
Pierre-Yves Chibon 73d120
                    "milestone": None,
Pierre-Yves Chibon 73d120
                    "priority": None,
Pierre-Yves Chibon 73d120
                    "private": False,
Pierre-Yves Chibon 73d120
                    "status": "Open",
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "title": "test issue",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "foo bar", "name": "foo"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Issue created",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon d794f0
    def test_create_issue_invalid_project_specific_token(self):
Pierre-Yves Chibon d794f0
        """ Test creating a new ticket on a project with a regular
Pierre-Yves Chibon d794f0
        project-specific token but for another project.
Pierre-Yves Chibon d794f0
        """
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # pingou's token with all the ACLs
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token project-specific-foo"}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # complete data set
Pierre-Yves Chibon d794f0
        data = {
Pierre-Yves Chibon 73d120
            "title": "test issue",
Pierre-Yves Chibon 73d120
            "issue_content": "This issue needs attention",
Pierre-Yves Chibon d794f0
        }
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # Create an issue on /test/ where pingou is the main admin
Pierre-Yves Chibon d794f0
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/new_issue", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon d794f0
        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 d794f0
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon d794f0
    unittest.main(verbosity=2)