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 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 d794f0
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon d794f0
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
import pagure  # noqa: E402
Pierre-Yves Chibon d794f0
import pagure.lib  # 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 d794f0
    @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 d794f0
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon d794f0
        pagure.SESSION = self.session
Pierre-Yves Chibon d794f0
        pagure.api.SESSION = self.session
Pierre-Yves Chibon d794f0
        pagure.api.issue.SESSION = self.session
Pierre-Yves Chibon d794f0
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        pagure.APP.config['TICKETS_FOLDER'] = None
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        tests.create_projects(self.session)
Pierre-Yves Chibon d794f0
        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 d794f0
            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 d794f0
            + 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 d794f0
        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 d794f0
            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 d794f0
            + 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 d794f0
        tests.create_tokens_acl(
Pierre-Yves Chibon d794f0
            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 d794f0
        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 d794f0
        output = self.app.post('/api/0/test/new_issue', headers=headers)
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon d794f0
        data = json.loads(output.data)
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            pagure.api.APIERROR.EINVALIDREQ.name, data['error_code'])
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            pagure.api.APIERROR.EINVALIDREQ.value, data['error'])
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            data['errors'],
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon d794f0
                u'issue_content': [u'This field is required.'],
Pierre-Yves Chibon d794f0
                u'title': [u'This field is required.']
Pierre-Yves Chibon d794f0
            }
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 d794f0
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # complete data set
Pierre-Yves Chibon d794f0
        data = {
Pierre-Yves Chibon d794f0
            'title': 'test issue',
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 d794f0
            '/api/0/test/new_issue',
Pierre-Yves Chibon d794f0
            headers=headers,
Pierre-Yves Chibon d794f0
            data=data)
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon d794f0
        data = json.loads(output.data)
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            pagure.api.APIERROR.EINVALIDREQ.name, data['error_code'])
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            pagure.api.APIERROR.EINVALIDREQ.value, data['error'])
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            data['errors'],
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon d794f0
                u'issue_content': [u'This field is required.']
Pierre-Yves Chibon d794f0
            }
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 d794f0
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
        # complete data set
Pierre-Yves Chibon d794f0
        data = {
Pierre-Yves Chibon d794f0
            'title': 'test issue',
Pierre-Yves Chibon d794f0
            '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 d794f0
            '/api/0/test/new_issue',
Pierre-Yves Chibon d794f0
            headers=headers,
Pierre-Yves Chibon d794f0
            data=data)
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon d794f0
        data = json.loads(output.data)
Pierre-Yves Chibon d794f0
        data['issue']['date_created'] = '1431414800'
Pierre-Yves Chibon d794f0
        data['issue']['last_updated'] = '1431414800'
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            data,
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon d794f0
              "issue": {
Pierre-Yves Chibon d794f0
                "assignee": None,
Pierre-Yves Chibon d794f0
                "blocks": [],
Pierre-Yves Chibon d794f0
                "close_status": None,
Pierre-Yves Chibon d794f0
                "closed_at": None,
Pierre-Yves Chibon d794f0
                "comments": [],
Pierre-Yves Chibon d794f0
                "content": "This issue needs attention",
Pierre-Yves Chibon d794f0
                "custom_fields": [],
Pierre-Yves Chibon d794f0
                "date_created": "1431414800",
Pierre-Yves Chibon d794f0
                "depends": [],
Pierre-Yves Chibon d794f0
                "id": 1,
Pierre-Yves Chibon d794f0
                "last_updated": "1431414800",
Pierre-Yves Chibon d794f0
                "milestone": "",
Pierre-Yves Chibon d794f0
                "priority": None,
Pierre-Yves Chibon d794f0
                "private": False,
Pierre-Yves Chibon d794f0
                "status": "Open",
Pierre-Yves Chibon d794f0
                "tags": [],
Pierre-Yves Chibon d794f0
                "title": "test issue",
Pierre-Yves Chibon d794f0
                "user": {
Pierre-Yves Chibon d794f0
                  "fullname": "PY C",
Pierre-Yves Chibon d794f0
                  "name": "pingou"
Pierre-Yves Chibon d794f0
                }
Pierre-Yves Chibon d794f0
              },
Pierre-Yves Chibon d794f0
              "message": "Issue created"
Pierre-Yves Chibon d794f0
            }
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
    @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 d794f0
        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 d794f0
            'title': 'test issue',
Pierre-Yves Chibon d794f0
            '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 d794f0
            '/api/0/test/new_issue',
Pierre-Yves Chibon d794f0
            headers=headers,
Pierre-Yves Chibon d794f0
            data=data)
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon d794f0
        data = json.loads(output.data)
Pierre-Yves Chibon d794f0
        data['issue']['date_created'] = '1431414800'
Pierre-Yves Chibon d794f0
        data['issue']['last_updated'] = '1431414800'
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            data,
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon d794f0
              "issue": {
Pierre-Yves Chibon d794f0
                "assignee": None,
Pierre-Yves Chibon d794f0
                "blocks": [],
Pierre-Yves Chibon d794f0
                "close_status": None,
Pierre-Yves Chibon d794f0
                "closed_at": None,
Pierre-Yves Chibon d794f0
                "comments": [],
Pierre-Yves Chibon d794f0
                "content": "This issue needs attention",
Pierre-Yves Chibon d794f0
                "custom_fields": [],
Pierre-Yves Chibon d794f0
                "date_created": "1431414800",
Pierre-Yves Chibon d794f0
                "depends": [],
Pierre-Yves Chibon d794f0
                "id": 1,
Pierre-Yves Chibon d794f0
                "last_updated": "1431414800",
Pierre-Yves Chibon d794f0
                "milestone": "",
Pierre-Yves Chibon d794f0
                "priority": None,
Pierre-Yves Chibon d794f0
                "private": False,
Pierre-Yves Chibon d794f0
                "status": "Open",
Pierre-Yves Chibon d794f0
                "tags": [],
Pierre-Yves Chibon d794f0
                "title": "test issue",
Pierre-Yves Chibon d794f0
                "user": {
Pierre-Yves Chibon d794f0
                  "fullname": "foo bar",
Pierre-Yves Chibon d794f0
                  "name": "foo"
Pierre-Yves Chibon d794f0
                }
Pierre-Yves Chibon d794f0
              },
Pierre-Yves Chibon d794f0
              "message": "Issue created"
Pierre-Yves Chibon d794f0
            }
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
    @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 d794f0
        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 d794f0
            'title': 'test issue',
Pierre-Yves Chibon d794f0
            '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 d794f0
            '/api/0/test/new_issue',
Pierre-Yves Chibon d794f0
            headers=headers,
Pierre-Yves Chibon d794f0
            data=data)
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon d794f0
        data = json.loads(output.data)
Pierre-Yves Chibon d794f0
        data['issue']['date_created'] = '1431414800'
Pierre-Yves Chibon d794f0
        data['issue']['last_updated'] = '1431414800'
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            data,
Pierre-Yves Chibon d794f0
            {
Pierre-Yves Chibon d794f0
              "issue": {
Pierre-Yves Chibon d794f0
                "assignee": None,
Pierre-Yves Chibon d794f0
                "blocks": [],
Pierre-Yves Chibon d794f0
                "close_status": None,
Pierre-Yves Chibon d794f0
                "closed_at": None,
Pierre-Yves Chibon d794f0
                "comments": [],
Pierre-Yves Chibon d794f0
                "content": "This issue needs attention",
Pierre-Yves Chibon d794f0
                "custom_fields": [],
Pierre-Yves Chibon d794f0
                "date_created": "1431414800",
Pierre-Yves Chibon d794f0
                "depends": [],
Pierre-Yves Chibon d794f0
                "id": 1,
Pierre-Yves Chibon d794f0
                "last_updated": "1431414800",
Pierre-Yves Chibon d794f0
                "milestone": "",
Pierre-Yves Chibon d794f0
                "priority": None,
Pierre-Yves Chibon d794f0
                "private": False,
Pierre-Yves Chibon d794f0
                "status": "Open",
Pierre-Yves Chibon d794f0
                "tags": [],
Pierre-Yves Chibon d794f0
                "title": "test issue",
Pierre-Yves Chibon d794f0
                "user": {
Pierre-Yves Chibon d794f0
                  "fullname": "foo bar",
Pierre-Yves Chibon d794f0
                  "name": "foo"
Pierre-Yves Chibon d794f0
                }
Pierre-Yves Chibon d794f0
              },
Pierre-Yves Chibon d794f0
              "message": "Issue created"
Pierre-Yves Chibon d794f0
            }
Pierre-Yves Chibon d794f0
        )
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
    @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 d794f0
        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 d794f0
            'title': 'test issue',
Pierre-Yves Chibon d794f0
            '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 d794f0
            '/api/0/test2/new_issue',
Pierre-Yves Chibon d794f0
            headers=headers,
Pierre-Yves Chibon d794f0
            data=data)
Pierre-Yves Chibon d794f0
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon d794f0
        data = json.loads(output.data)
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
Pierre-Yves Chibon d794f0
        self.assertEqual(
Pierre-Yves Chibon d794f0
            pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
Pierre-Yves Chibon d794f0
if __name__ == '__main__':
Pierre-Yves Chibon d794f0
    unittest.main(verbosity=2)