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 929a2a
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 929a2a
import pkg_resources
Pierre-Yves Chibon 929a2a
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 929a2a
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 929a2a
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
import pagure
Pierre-Yves Chibon 929a2a
import pagure.lib
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 929a2a
    @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 929a2a
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 929a2a
        pagure.SESSION = self.session
Pierre-Yves Chibon 929a2a
        pagure.api.SESSION = self.session
Pierre-Yves Chibon 929a2a
        pagure.api.issue.SESSION = self.session
Pierre-Yves Chibon 929a2a
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        pagure.APP.config['TICKETS_FOLDER'] = None
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        tests.create_projects(self.session)
Pierre-Yves Chibon 929a2a
        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 929a2a
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 929a2a
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 929a2a
            session=self.session,
Pierre-Yves Chibon 929a2a
            repo=repo,
Pierre-Yves Chibon 929a2a
            title='Test issue #1',
Pierre-Yves Chibon 929a2a
            content='We should work on this',
Pierre-Yves Chibon 929a2a
            user='pingou',
Pierre-Yves Chibon 929a2a
            ticketfolder=None,
Pierre-Yves Chibon 929a2a
            private=False,
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
        self.session.commit()
Pierre-Yves Chibon 929a2a
        self.assertEqual(msg.title, 'Test issue #1')
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Create private issue
Pierre-Yves Chibon 929a2a
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 929a2a
            session=self.session,
Pierre-Yves Chibon 929a2a
            repo=repo,
Pierre-Yves Chibon 929a2a
            title='Test issue #2',
Pierre-Yves Chibon 929a2a
            content='We should work on this',
Pierre-Yves Chibon 929a2a
            user='foo',
Pierre-Yves Chibon 929a2a
            ticketfolder=None,
Pierre-Yves Chibon 929a2a
            private=True,
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
        self.session.commit()
Pierre-Yves Chibon 929a2a
        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 649607
            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 649607
            + 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 649607
        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 649607
            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 649607
            + 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 649607
        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 929a2a
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Invalid project
Pierre-Yves Chibon 929a2a
        output = self.app.post(
Pierre-Yves Chibon 929a2a
            '/api/0/foobar/issue/1/status', headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 929a2a
        data = json.loads(output.data)
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 929a2a
            data,
Pierre-Yves Chibon 929a2a
            {
Pierre-Yves Chibon 929a2a
              "error": "Project not found",
Pierre-Yves Chibon 929a2a
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon 929a2a
            }
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 929a2a
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Valid token, wrong project
Pierre-Yves Chibon 929a2a
        output = self.app.post('/api/0/test2/issue/1/status', headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 929a2a
        data = json.loads(output.data)
Pierre-Yves Chibon 929a2a
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 929a2a
                         data['error_code'])
Pierre-Yves Chibon 929a2a
        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 929a2a
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # No issue
Pierre-Yves Chibon 929a2a
        output = self.app.post('/api/0/test/issue/42/status', headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 929a2a
        data = json.loads(output.data)
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 929a2a
            data,
Pierre-Yves Chibon 929a2a
            {
Pierre-Yves Chibon 929a2a
              "error": "Issue not found",
Pierre-Yves Chibon 929a2a
              "error_code": "ENOISSUE",
Pierre-Yves Chibon 929a2a
            }
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 929a2a
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Check status before
Pierre-Yves Chibon 929a2a
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 929a2a
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 929a2a
        self.assertEqual(issue.status, 'Open')
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        data = {
Pierre-Yves Chibon 929a2a
            'title': 'test issue',
Pierre-Yves Chibon 929a2a
        }
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Incomplete request
Pierre-Yves Chibon 929a2a
        output = self.app.post(
Pierre-Yves Chibon 929a2a
            '/api/0/test/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 929a2a
        data = json.loads(output.data)
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 929a2a
            data,
Pierre-Yves Chibon 929a2a
            {
Pierre-Yves Chibon 929a2a
              "error": "Invalid or incomplete input submited",
Pierre-Yves Chibon 929a2a
              "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 929a2a
              "errors": {"status": ["Not a valid choice"]}
Pierre-Yves Chibon 929a2a
            }
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # No change
Pierre-Yves Chibon 929a2a
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 929a2a
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 929a2a
        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 929a2a
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        data = {
Pierre-Yves Chibon 929a2a
            'status': 'Open',
Pierre-Yves Chibon 929a2a
        }
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Valid request but no change
Pierre-Yves Chibon 929a2a
        output = self.app.post(
Pierre-Yves Chibon 929a2a
            '/api/0/test/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 929a2a
        data = json.loads(output.data)
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 929a2a
            data,
Pierre-Yves Chibon 929a2a
            {'message': 'No changes'}
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # No change
Pierre-Yves Chibon 929a2a
        repo = pagure.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 929a2a
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Pierre-Yves Chibon 929a2a
        self.assertEqual(issue.status, 'Open')
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
    @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 929a2a
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        data = {
Pierre-Yves Chibon 929a2a
            'status': 'Fixed',
Pierre-Yves Chibon 929a2a
        }
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        # Valid request
Pierre-Yves Chibon 929a2a
        output = self.app.post(
Pierre-Yves Chibon 929a2a
            '/api/0/test/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 929a2a
        data = json.loads(output.data)
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        self.assertDictEqual(
Pierre-Yves Chibon 929a2a
            data,
Pierre-Yves Chibon 929a2a
            {'message':[
Pierre-Yves Chibon 929a2a
                'Issue status updated to: Closed (was: Open)',
Pierre-Yves Chibon 929a2a
                'Issue close_status updated to: Fixed'
Pierre-Yves Chibon 929a2a
            ]}
Pierre-Yves Chibon 929a2a
        )
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
        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 929a2a
            '/api/0/foo/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 929a2a
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 929a2a
        data = json.loads(output.data)
Pierre-Yves Chibon 929a2a
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon 929a2a
                         data['error_code'])
Pierre-Yves Chibon 929a2a
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 649607
    @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 649607
        headers = {'Authorization': 'token project-less-foo'}
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        data = {
Pierre-Yves Chibon 649607
            'status': 'Fixed',
Pierre-Yves Chibon 649607
        }
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        # Valid request
Pierre-Yves Chibon 649607
        output = self.app.post(
Pierre-Yves Chibon 649607
            '/api/0/test/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 649607
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 649607
        data = json.loads(output.data)
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 649607
                "error_code": "EISSUENOTALLOWED"
Pierre-Yves Chibon 649607
            }
Pierre-Yves Chibon 649607
        )
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
    @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 649607
        headers = {'Authorization': 'token project-less-pingou'}
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        data = {
Pierre-Yves Chibon 649607
            'status': 'Fixed',
Pierre-Yves Chibon 649607
        }
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 649607
        # Valid request
Pierre-Yves Chibon 649607
        output = self.app.post(
Pierre-Yves Chibon 649607
            '/api/0/test/issue/1/status', data=data, headers=headers)
Pierre-Yves Chibon 649607
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 649607
        data = json.loads(output.data)
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 649607
                    "Issue close_status updated to: Fixed"
Pierre-Yves Chibon 649607
                ]
Pierre-Yves Chibon 649607
            }
Pierre-Yves Chibon 649607
        )
Pierre-Yves Chibon 649607
Pierre-Yves Chibon 929a2a
Pierre-Yves Chibon 929a2a
if __name__ == '__main__':
Pierre-Yves Chibon 929a2a
    unittest.main(verbosity=2)