Blame tests/test_pagure_flask_api_fork_assign.py

Pierre-Yves Chibon bc397c
# -*- coding: utf-8 -*-
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
"""
Pierre-Yves Chibon bc397c
 (c) 2019 - Copyright Red Hat Inc
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
 Authors:
Pierre-Yves Chibon bc397c
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
"""
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
import arrow
Pierre-Yves Chibon bc397c
import copy
Pierre-Yves Chibon bc397c
import datetime
Pierre-Yves Chibon bc397c
import unittest
Pierre-Yves Chibon bc397c
import shutil
Pierre-Yves Chibon bc397c
import sys
Pierre-Yves Chibon bc397c
import time
Pierre-Yves Chibon bc397c
import os
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
import flask
Pierre-Yves Chibon bc397c
import json
Pierre-Yves Chibon bc397c
import munch
Pierre-Yves Chibon bc397c
from mock import patch, MagicMock
Pierre-Yves Chibon bc397c
from sqlalchemy.exc import SQLAlchemyError
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon bc397c
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
import pagure.lib.query
Pierre-Yves Chibon bc397c
import tests
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
class PagureFlaskApiForkAssigntests(tests.SimplePagureTest):
Pierre-Yves Chibon bc397c
    """ Tests for the flask API of pagure for assigning a PR """
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    maxDiff = None
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
Pierre-Yves Chibon bc397c
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon bc397c
    def setUp(self):
Pierre-Yves Chibon bc397c
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon bc397c
        super(PagureFlaskApiForkAssigntests, self).setUp()
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        tests.create_projects(self.session)
Pierre-Yves Chibon bc397c
        tests.add_content_git_repo(
Pierre-Yves Chibon bc397c
            os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Fork
Pierre-Yves Chibon bc397c
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon bc397c
            self.session, 'test')
Pierre-Yves Chibon bc397c
        task = pagure.lib.query.fork_project(
Pierre-Yves Chibon bc397c
            session=self.session,
Pierre-Yves Chibon bc397c
            user='pingou',
Pierre-Yves Chibon bc397c
            repo=project,
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
        self.session.commit()
Pierre-Yves Chibon bc397c
        self.assertEqual(
Pierre-Yves Chibon bc397c
            task.get(),
Pierre-Yves Chibon bc397c
            {'endpoint': 'ui_ns.view_repo',
Pierre-Yves Chibon bc397c
             'repo': 'test',
Pierre-Yves Chibon bc397c
             'namespace': None,
Pierre-Yves Chibon bc397c
             'username': 'pingou'})
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        tests.add_readme_git_repo(
Pierre-Yves Chibon bc397c
            os.path.join(self.path, "repos", "forks", "pingou", "test.git"))
Pierre-Yves Chibon bc397c
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon bc397c
            self.session, 'test')
Pierre-Yves Chibon bc397c
        fork = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon bc397c
            self.session,
Pierre-Yves Chibon bc397c
            'test',
Pierre-Yves Chibon bc397c
            user='pingou',
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        tests.create_tokens(self.session)
Pierre-Yves Chibon bc397c
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon bc397c
            session=self.session,
Pierre-Yves Chibon bc397c
            repo_from=fork,
Pierre-Yves Chibon bc397c
            branch_from='master',
Pierre-Yves Chibon bc397c
            repo_to=project,
Pierre-Yves Chibon bc397c
            branch_to='master',
Pierre-Yves Chibon bc397c
            title='test pull-request',
Pierre-Yves Chibon bc397c
            user='pingou',
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
        self.session.commit()
Pierre-Yves Chibon bc397c
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon bc397c
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Assert the PR is open
Pierre-Yves Chibon bc397c
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon bc397c
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon bc397c
            self.session, 'test')
Pierre-Yves Chibon bc397c
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon bc397c
        self.assertEqual(project.requests[0].status, "Open")
Pierre-Yves Chibon bc397c
        # Check how the PR renders in the API and the UI
Pierre-Yves Chibon bc397c
        output = self.app.get('/api/0/test/pull-request/1')
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bc397c
        output = self.app.get('/test/pull-request/1')
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_invalid_project_namespace(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when the project doesn't exist.
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Valid token, wrong project
Pierre-Yves Chibon bc397c
        output = self.app.post(
Pierre-Yves Chibon bc397c
            '/api/0/somenamespace/test3/pull-request/1/assign', headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertDictEqual(
Pierre-Yves Chibon bc397c
            data,
Pierre-Yves Chibon bc397c
            {'error': 'Invalid or expired token. Please visit '
Pierre-Yves Chibon bc397c
                      'http://localhost.localdomain/settings#api-keys to get or renew your '
Pierre-Yves Chibon bc397c
                      'API token.',
Pierre-Yves Chibon bc397c
             'error_code': 'EINVALIDTOK'}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_invalid_project(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when the project doesn't exist.
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Invalid project
Pierre-Yves Chibon bc397c
        output = self.app.post('/api/0/foo/pull-request/1/assign', headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertDictEqual(
Pierre-Yves Chibon bc397c
            data,
Pierre-Yves Chibon bc397c
            {
Pierre-Yves Chibon bc397c
              "error": "Project not found",
Pierre-Yves Chibon bc397c
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon bc397c
            }
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_invalid_project_token(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when the token doesn't correspond
Pierre-Yves Chibon bc397c
        to the project.
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Valid token, wrong project
Pierre-Yves Chibon bc397c
        output = self.app.post('/api/0/test2/pull-request/1/assign', headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertEqual(sorted(data.keys()), ['error', 'error_code'])
Pierre-Yves Chibon bc397c
        self.assertEqual(
Pierre-Yves Chibon bc397c
            pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon bc397c
        self.assertEqual(
Pierre-Yves Chibon bc397c
            pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_invalid_pr(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when asking for an invalid PR
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # No input
Pierre-Yves Chibon bc397c
        output = self.app.post('/api/0/test/pull-request/404/assign', headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertDictEqual(
Pierre-Yves Chibon bc397c
            data,
Pierre-Yves Chibon bc397c
            {'error': 'Pull-Request not found', 'error_code': 'ENOREQ'}
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_no_input(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when no input is specified
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # No input
Pierre-Yves Chibon bc397c
        output = self.app.post('/api/0/test/pull-request/1/assign', headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertDictEqual(
Pierre-Yves Chibon bc397c
            data,
Pierre-Yves Chibon bc397c
            {'message': 'Nothing to change'}
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_assigned(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when with valid input
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        data = {
Pierre-Yves Chibon bc397c
            'assignee': 'pingou',
Pierre-Yves Chibon bc397c
        }
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Valid request
Pierre-Yves Chibon bc397c
        output = self.app.post(
Pierre-Yves Chibon bc397c
            '/api/0/test/pull-request/1/assign', data=data, headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertDictEqual(
Pierre-Yves Chibon bc397c
            data,
Pierre-Yves Chibon bc397c
            {'message': 'Request assigned'}
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_unassigned(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when unassigning
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
        self.test_api_assign_pr_assigned()
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
        data = {}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Un-assign
Pierre-Yves Chibon bc397c
        output = self.app.post(
Pierre-Yves Chibon bc397c
            '/api/0/test/pull-request/1/assign', data=data, headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertDictEqual(
Pierre-Yves Chibon bc397c
            data,
Pierre-Yves Chibon bc397c
            {'message': 'Request assignee reset'}
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_unassigned_twice(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when unassigning
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
        self.test_api_assign_pr_unassigned()
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
        data = {'assignee': None}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Un-assign
Pierre-Yves Chibon bc397c
        output = self.app.post(
Pierre-Yves Chibon bc397c
            '/api/0/test/pull-request/1/assign', data=data, headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertDictEqual(
Pierre-Yves Chibon bc397c
            data,
Pierre-Yves Chibon bc397c
            {'message': 'Nothing to change'}
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
    def test_api_assign_pr_unassigned_empty_string(self):
Pierre-Yves Chibon bc397c
        """ Test api_pull_request_assign method when unassigning with an
Pierre-Yves Chibon bc397c
        empty string
Pierre-Yves Chibon bc397c
        """
Pierre-Yves Chibon bc397c
        self.test_api_assign_pr_assigned()
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
        # Un-assign
Pierre-Yves Chibon bc397c
        data = {'assignee': ''}
Pierre-Yves Chibon bc397c
        output = self.app.post(
Pierre-Yves Chibon bc397c
            '/api/0/test/pull-request/1/assign', data=data, headers=headers)
Pierre-Yves Chibon bc397c
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bc397c
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bc397c
        self.assertDictEqual(
Pierre-Yves Chibon bc397c
            data,
Pierre-Yves Chibon bc397c
            {'message': 'Request assignee reset'}
Pierre-Yves Chibon bc397c
        )
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
Pierre-Yves Chibon bc397c
if __name__ == '__main__':
Pierre-Yves Chibon bc397c
    unittest.main(verbosity=2)