|
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 |
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 |
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 |
73d120 |
@patch("pagure.lib.git.update_git", MagicMock(return_value=True))
|
|
Pierre-Yves Chibon |
73d120 |
@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 |
73d120 |
os.path.join(self.path, "repos", "test.git")
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
# Fork
|
|
Pierre-Yves Chibon |
73d120 |
project = pagure.lib.query.get_authorized_project(self.session, "test")
|
|
Pierre-Yves Chibon |
bc397c |
task = pagure.lib.query.fork_project(
|
|
Pierre-Yves Chibon |
73d120 |
session=self.session, user="pingou", 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 |
73d120 |
{
|
|
Pierre-Yves Chibon |
73d120 |
"endpoint": "ui_ns.view_repo",
|
|
Pierre-Yves Chibon |
73d120 |
"repo": "test",
|
|
Pierre-Yves Chibon |
73d120 |
"namespace": None,
|
|
Pierre-Yves Chibon |
73d120 |
"username": "pingou",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
tests.add_readme_git_repo(
|
|
Pierre-Yves Chibon |
73d120 |
os.path.join(self.path, "repos", "forks", "pingou", "test.git")
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
73d120 |
project = pagure.lib.query.get_authorized_project(self.session, "test")
|
|
Pierre-Yves Chibon |
bc397c |
fork = pagure.lib.query.get_authorized_project(
|
|
Pierre-Yves Chibon |
73d120 |
self.session, "test", 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 |
73d120 |
branch_from="master",
|
|
Pierre-Yves Chibon |
bc397c |
repo_to=project,
|
|
Pierre-Yves Chibon |
73d120 |
branch_to="master",
|
|
Pierre-Yves Chibon |
73d120 |
title="test pull-request",
|
|
Pierre-Yves Chibon |
73d120 |
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 |
73d120 |
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 |
73d120 |
project = pagure.lib.query.get_authorized_project(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 |
73d120 |
output = self.app.get("/api/0/test/pull-request/1")
|
|
Pierre-Yves Chibon |
bc397c |
self.assertEqual(output.status_code, 200)
|
|
Pierre-Yves Chibon |
73d120 |
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 |
73d120 |
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 |
73d120 |
"/api/0/somenamespace/test3/pull-request/1/assign", headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
{
|
|
Pierre-Yves Chibon |
73d120 |
"error": "Invalid or expired token. Please visit "
|
|
Pierre-Yves Chibon |
73d120 |
"http://localhost.localdomain/settings#api-keys to get or renew your "
|
|
Pierre-Yves Chibon |
73d120 |
"API token.",
|
|
Pierre-Yves Chibon |
73d120 |
"error_code": "EINVALIDTOK",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
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 |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
# Invalid project
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/foo/pull-request/1/assign", headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
data, {"error": "Project not found", "error_code": "ENOPROJECT"}
|
|
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 |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
# Valid token, wrong project
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test2/pull-request/1/assign", headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
self.assertEqual(sorted(data.keys()), ["error", "error_code"])
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
|
|
Pierre-Yves Chibon |
bc397c |
self.assertEqual(
|
|
Pierre-Yves Chibon |
73d120 |
pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
# No input
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/pull-request/404/assign", headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
data, {"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 |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
# No input
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/pull-request/1/assign", headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
self.assertDictEqual(data, {"message": "Nothing to change"})
|
|
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 |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
73d120 |
data = {"assignee": "pingou"}
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
# Valid request
|
|
Pierre-Yves Chibon |
bc397c |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/pull-request/1/assign", data=data, headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
self.assertDictEqual(data, {"message": "Request assigned"})
|
|
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 |
73d120 |
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 |
73d120 |
"/api/0/test/pull-request/1/assign", data=data, headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
self.assertDictEqual(data, {"message": "Request assignee reset"})
|
|
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 |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"assignee": None}
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
# Un-assign
|
|
Pierre-Yves Chibon |
bc397c |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/pull-request/1/assign", data=data, headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
self.assertDictEqual(data, {"message": "Nothing to change"})
|
|
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 |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
# Un-assign
|
|
Pierre-Yves Chibon |
73d120 |
data = {"assignee": ""}
|
|
Pierre-Yves Chibon |
bc397c |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/pull-request/1/assign", data=data, headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 |
73d120 |
self.assertDictEqual(data, {"message": "Request assignee reset"})
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
bc397c |
|
|
Pierre-Yves Chibon |
73d120 |
if __name__ == "__main__":
|
|
Pierre-Yves Chibon |
bc397c |
unittest.main(verbosity=2)
|